1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
|
// BEGIN_COPYRIGHT -*- glean -*-
//
// Copyright (C) 1999 Allen Akin All Rights Reserved.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the
// Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
// KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL ALLEN AKIN BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
// AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
// OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
// END_COPYRIGHT
// tapi2.h: Test OpenGL 2.x API functions/features
// Brian Paul 9 March 2007
#define GL_GLEXT_PROTOTYPES
#include <stdlib.h>
#include <cstring>
#include <cassert>
#include <math.h>
#include "tapi2.h"
namespace GLEAN {
static PFNGLATTACHSHADERPROC glAttachShader_func = NULL;
static PFNGLBINDATTRIBLOCATIONPROC glBindAttribLocation_func = NULL;
static PFNGLCOMPILESHADERPROC glCompileShader_func = NULL;
static PFNGLCREATEPROGRAMPROC glCreateProgram_func = NULL;
static PFNGLCREATESHADERPROC glCreateShader_func = NULL;
static PFNGLDELETEPROGRAMPROC glDeleteProgram_func = NULL;
static PFNGLDELETESHADERPROC glDeleteShader_func = NULL;
static PFNGLDISABLEVERTEXATTRIBARRAYPROC glDisableVertexAttribArray_func = NULL;
static PFNGLENABLEVERTEXATTRIBARRAYPROC glEnableVertexAttribArray_func = NULL;
static PFNGLGETATTACHEDSHADERSPROC glGetAttachedShaders_func = NULL;
static PFNGLGETATTRIBLOCATIONPROC glGetAttribLocation_func = NULL;
static PFNGLGETPROGRAMINFOLOGPROC glGetProgramInfoLog_func = NULL;
static PFNGLGETSHADERINFOLOGPROC glGetShaderInfoLog_func = NULL;
static PFNGLGETSHADERIVPROC glGetShaderiv_func = NULL;
static PFNGLGETPROGRAMIVPROC glGetProgramiv_func = NULL;
static PFNGLGETSHADERSOURCEPROC glGetShaderSource_func = NULL;
static PFNGLGETUNIFORMFVPROC glGetUniformfv_func = NULL;
static PFNGLGETUNIFORMLOCATIONPROC glGetUniformLocation_func = NULL;
static PFNGLISPROGRAMPROC glIsProgram_func = NULL;
static PFNGLISSHADERPROC glIsShader_func = NULL;
static PFNGLLINKPROGRAMPROC glLinkProgram_func = NULL;
static PFNGLSHADERSOURCEPROC glShaderSource_func = NULL;
static PFNGLUNIFORM1FVPROC glUniform1fv_func = NULL;
static PFNGLUNIFORM2FVPROC glUniform2fv_func = NULL;
static PFNGLUNIFORM3FVPROC glUniform3fv_func = NULL;
static PFNGLUNIFORM4FVPROC glUniform4fv_func = NULL;
static PFNGLUNIFORM1FPROC glUniform1f_func = NULL;
static PFNGLUNIFORM2FPROC glUniform2f_func = NULL;
static PFNGLUNIFORM3FPROC glUniform3f_func = NULL;
static PFNGLUNIFORM4FPROC glUniform4f_func = NULL;
static PFNGLUNIFORM1IPROC glUniform1i_func = NULL;
static PFNGLUNIFORM2IPROC glUniform2i_func = NULL;
static PFNGLUNIFORM3IPROC glUniform3i_func = NULL;
static PFNGLUNIFORM4IPROC glUniform4i_func = NULL;
static PFNGLUNIFORM1IVPROC glUniform1iv_func = NULL;
static PFNGLUNIFORM2IVPROC glUniform2iv_func = NULL;
static PFNGLUNIFORM3IVPROC glUniform3iv_func = NULL;
static PFNGLUNIFORM4IVPROC glUniform4iv_func = NULL;
static PFNGLUNIFORMMATRIX2FVPROC glUniformMatrix2fv_func = NULL;
static PFNGLUNIFORMMATRIX3FVPROC glUniformMatrix3fv_func = NULL;
static PFNGLUNIFORMMATRIX4FVPROC glUniformMatrix4fv_func = NULL;
static PFNGLUSEPROGRAMPROC glUseProgram_func = NULL;
static PFNGLVALIDATEPROGRAMPROC glValidateProgram_func = NULL;
static PFNGLVERTEXATTRIB1FPROC glVertexAttrib1f_func = NULL;
static PFNGLVERTEXATTRIB2FPROC glVertexAttrib2f_func = NULL;
static PFNGLVERTEXATTRIB3FPROC glVertexAttrib3f_func = NULL;
static PFNGLVERTEXATTRIB4FPROC glVertexAttrib4f_func = NULL;
static PFNGLVERTEXATTRIBPOINTERPROC glVertexAttribPointer_func = NULL;
static PFNGLSTENCILOPSEPARATEPROC glStencilOpSeparate_func = NULL;
static PFNGLSTENCILFUNCSEPARATEPROC glStencilFuncSeparate_func = NULL;
static PFNGLSTENCILMASKSEPARATEPROC glStencilMaskSeparate_func = NULL;
static PFNGLBLENDEQUATIONSEPARATEPROC glBlendEquationSeparate_func = NULL;
static PFNGLDRAWBUFFERSPROC glDrawBuffers_func = NULL;
// Get ptrs to 2.0 API functions.
// \param errorFunc returns name of API function in case of error
// \return true for success, false for error
bool
API2Test::getFunctions_2_0(const char **errorFunc)
{
#define GET(PTR, TYPE, STR) \
PTR = (TYPE) GLUtils::getProcAddress(STR); \
if (!PTR) { \
*errorFunc = STR; \
return false; \
}
// shading language
GET(glAttachShader_func, PFNGLATTACHSHADERPROC, "glAttachShader");
GET(glBindAttribLocation_func, PFNGLBINDATTRIBLOCATIONPROC, "glBindAttribLocation");
GET(glCompileShader_func, PFNGLCOMPILESHADERPROC, "glCompileShader");
GET(glCreateProgram_func, PFNGLCREATEPROGRAMPROC, "glCreateProgram");
GET(glCreateShader_func, PFNGLCREATESHADERPROC, "glCreateShader");
GET(glDeleteProgram_func, PFNGLDELETEPROGRAMPROC, "glDeleteProgram");
GET(glDeleteShader_func, PFNGLDELETESHADERPROC, "glDeleteShader");
GET(glDisableVertexAttribArray_func, PFNGLDISABLEVERTEXATTRIBARRAYPROC, "glDisableVertexAttribArray");
GET(glEnableVertexAttribArray_func, PFNGLENABLEVERTEXATTRIBARRAYPROC, "glEnableVertexAttribArray");
GET(glGetAttachedShaders_func, PFNGLGETATTACHEDSHADERSPROC, "glGetAttachedShaders");
GET(glGetAttribLocation_func, PFNGLGETATTRIBLOCATIONPROC, "glGetAttribLocation");
GET(glGetProgramInfoLog_func, PFNGLGETPROGRAMINFOLOGPROC, "glGetProgramInfoLog");
GET(glGetShaderInfoLog_func, PFNGLGETSHADERINFOLOGPROC, "glGetShaderInfoLog");
GET(glGetProgramiv_func, PFNGLGETPROGRAMIVPROC, "glGetProgramiv");
GET(glGetShaderiv_func, PFNGLGETSHADERIVPROC, "glGetShaderiv");
GET(glGetShaderSource_func, PFNGLGETSHADERSOURCEPROC, "glGetShaderSource");
GET(glGetUniformLocation_func, PFNGLGETUNIFORMLOCATIONPROC, "glGetUniformLocation");
GET(glGetUniformfv_func, PFNGLGETUNIFORMFVPROC, "glGetUniformfv");
GET(glIsProgram_func, PFNGLISPROGRAMPROC, "glIsProgram");
GET(glIsShader_func, PFNGLISSHADERPROC, "glIsShader");
GET(glLinkProgram_func, PFNGLLINKPROGRAMPROC, "glLinkProgram");
GET(glShaderSource_func, PFNGLSHADERSOURCEPROC, "glShaderSource");
GET(glUniform1f_func, PFNGLUNIFORM1FPROC, "glUniform1f");
GET(glUniform2f_func, PFNGLUNIFORM2FPROC, "glUniform2f");
GET(glUniform3f_func, PFNGLUNIFORM3FPROC, "glUniform3f");
GET(glUniform4f_func, PFNGLUNIFORM4FPROC, "glUniform4f");
GET(glUniform1fv_func, PFNGLUNIFORM1FVPROC, "glUniform1fv");
GET(glUniform2fv_func, PFNGLUNIFORM2FVPROC, "glUniform2fv");
GET(glUniform3fv_func, PFNGLUNIFORM3FVPROC, "glUniform3fv");
GET(glUniform4fv_func, PFNGLUNIFORM3FVPROC, "glUniform4fv");
GET(glUniform1i_func, PFNGLUNIFORM1IPROC, "glUniform1i");
GET(glUniform2i_func, PFNGLUNIFORM2IPROC, "glUniform2i");
GET(glUniform3i_func, PFNGLUNIFORM3IPROC, "glUniform3i");
GET(glUniform4i_func, PFNGLUNIFORM4IPROC, "glUniform4i");
GET(glUniform1iv_func, PFNGLUNIFORM1IVPROC, "glUniform1iv");
GET(glUniform2iv_func, PFNGLUNIFORM2IVPROC, "glUniform2iv");
GET(glUniform3iv_func, PFNGLUNIFORM3IVPROC, "glUniform3iv");
GET(glUniform4iv_func, PFNGLUNIFORM4IVPROC, "glUniform4iv");
GET(glUniformMatrix2fv_func, PFNGLUNIFORMMATRIX2FVPROC, "glUniformMatrix2fv");
GET(glUniformMatrix3fv_func, PFNGLUNIFORMMATRIX3FVPROC, "glUniformMatrix3fv");
GET(glUniformMatrix4fv_func, PFNGLUNIFORMMATRIX4FVPROC, "glUniformMatrix4fv");
GET(glUseProgram_func, PFNGLUSEPROGRAMPROC, "glUseProgram");
GET(glValidateProgram_func, PFNGLVALIDATEPROGRAMPROC, "glValidateProgram");
GET(glVertexAttrib1f_func, PFNGLVERTEXATTRIB1FPROC, "glVertexAttrib1f");
GET(glVertexAttrib2f_func, PFNGLVERTEXATTRIB2FPROC, "glVertexAttrib2f");
GET(glVertexAttrib3f_func, PFNGLVERTEXATTRIB3FPROC, "glVertexAttrib3f");
GET(glVertexAttrib4f_func, PFNGLVERTEXATTRIB4FPROC, "glVertexAttrib4f");
GET(glVertexAttribPointer_func, PFNGLVERTEXATTRIBPOINTERPROC, "glVertexAttribPointer");
// stencil
GET(glStencilOpSeparate_func, PFNGLSTENCILOPSEPARATEPROC, "glStencilOpSeparate");
GET(glStencilFuncSeparate_func, PFNGLSTENCILFUNCSEPARATEPROC, "glStencilFuncSeparate");
GET(glStencilMaskSeparate_func, PFNGLSTENCILMASKSEPARATEPROC, "glStencilMaskSeparate");
// misc
GET(glBlendEquationSeparate_func, PFNGLBLENDEQUATIONSEPARATEPROC, "glBlendEquationSeparate");
GET(glDrawBuffers_func, PFNGLDRAWBUFFERSPROC, "glDrawBuffers");
return true;
#undef GET
}
bool
API2Test::setup(void)
{
// check that we have OpenGL 2.0
const char *version = (const char *) glGetString(GL_VERSION);
if (version[0] != '2' || version[1] != '.') {
//env->log << "OpenGL 2.0 not supported\n";
return false;
}
const char *errorFunc;
if (!getFunctions_2_0(&errorFunc)) {
env->log << "Unable to get pointer to OpenGL 2.0 function '"
<< errorFunc
<< "'\n";
return false;
}
GLenum err = glGetError();
assert(!err); // should be OK
// setup vertex transform (we'll draw a quad in middle of window)
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-4.0, 4.0, -4.0, 4.0, 0.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glDrawBuffer(GL_FRONT);
glReadBuffer(GL_FRONT);
// compute error tolerances (may need fine-tuning)
int bufferBits[5];
glGetIntegerv(GL_RED_BITS, &bufferBits[0]);
glGetIntegerv(GL_GREEN_BITS, &bufferBits[1]);
glGetIntegerv(GL_BLUE_BITS, &bufferBits[2]);
glGetIntegerv(GL_ALPHA_BITS, &bufferBits[3]);
glGetIntegerv(GL_DEPTH_BITS, &bufferBits[4]);
tolerance[0] = 2.0 / (1 << bufferBits[0]);
tolerance[1] = 2.0 / (1 << bufferBits[1]);
tolerance[2] = 2.0 / (1 << bufferBits[2]);
if (bufferBits[3])
tolerance[3] = 2.0 / (1 << bufferBits[3]);
else
tolerance[3] = 1.0;
if (bufferBits[4])
tolerance[4] = 16.0 / (1 << bufferBits[4]);
else
tolerance[4] = 1.0;
return true;
}
void
API2Test::reportFailure(const char *msg, int line) const
{
env->log << "FAILURE: " << msg << " (at tapi2.cpp:" << line << ")\n";
}
void
API2Test::reportFailure(const char *msg, GLenum target, int line) const
{
env->log << "FAILURE: " << msg;
if (target == GL_FRAGMENT_SHADER)
env->log << " (fragment)";
else
env->log << " (vertex)";
env->log << " (at tapi2.cpp:" << line << ")\n";
}
#define REPORT_FAILURE(MSG) reportFailure(MSG, __LINE__)
#define REPORT_FAILURE_T(MSG, TARGET) reportFailure(MSG, TARGET, __LINE__)
// Compare actual and expected colors
bool
API2Test::equalColors(const GLfloat act[4], const GLfloat exp[4]) const
{
if ((fabsf(act[0] - exp[0]) > tolerance[0]) ||
(fabsf(act[1] - exp[1]) > tolerance[1]) ||
(fabsf(act[2] - exp[2]) > tolerance[2]) ||
(fabsf(act[3] - exp[3]) > tolerance[3]))
return false;
else
return true;
}
// Render test quad w/ current shader program, return RGBA color of quad
void
API2Test::renderQuad(GLfloat *pixel) const
{
const GLfloat r = 0.62; // XXX draw 16x16 pixel quad
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_POLYGON);
glTexCoord2f(0, 0); glVertex2f(-r, -r);
glTexCoord2f(1, 0); glVertex2f( r, -r);
glTexCoord2f(1, 1); glVertex2f( r, r);
glTexCoord2f(0, 1); glVertex2f(-r, r);
glEnd();
// read a pixel from lower-left corner of rendered quad
glReadPixels(windowSize / 2 - 2, windowSize / 2 - 2, 1, 1,
GL_RGBA, GL_FLOAT, pixel);
}
// As above, but use vertex arrays
// \param attr which vertex attribute array to put colors into
// \param value 4-component valut to put into the attribute array
// \param pixel returns the rendered color obtained with glReadPixels
void
API2Test::renderQuadWithArrays(GLint attr, const GLfloat value[4],
GLfloat *pixel) const
{
const GLfloat r = 0.62; // XXX draw 16x16 pixel quad
static const GLfloat vertcoords[4][3] = {
{ -r, -r, 0 }, { r, -r, 0 }, { r, r, 0 }, { -r, r, 0 }
};
GLfloat values[4][4];
GLint i;
for (i = 0; i < 4; i++) {
values[i][0] = value[0];
values[i][1] = value[1];
values[i][2] = value[2];
values[i][3] = value[3];
};
glVertexPointer(3, GL_FLOAT, 0, vertcoords);
glEnable(GL_VERTEX_ARRAY);
glVertexAttribPointer_func(attr, 4, GL_FLOAT, GL_FALSE, 0, values);
glEnableVertexAttribArray_func(attr);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDrawArrays(GL_POLYGON, 0, 4);
glDisable(GL_VERTEX_ARRAY);
glDisableVertexAttribArray_func(attr);
// read a pixel from lower-left corner of rendered quad
glReadPixels(windowSize / 2 - 2, windowSize / 2 - 2, 1, 1,
GL_RGBA, GL_FLOAT, pixel);
}
GLuint
API2Test::loadAndCompileShader(GLenum target, const char *text)
{
GLint stat, val;
GLuint shader = glCreateShader_func(target);
if (!shader) {
REPORT_FAILURE("glCreateShader failed (fragment)");
return 0;
}
glShaderSource_func(shader, 1,
(const GLchar **) &text, NULL);
glCompileShader_func(shader);
glGetShaderiv_func(shader, GL_COMPILE_STATUS, &stat);
if (!stat) {
REPORT_FAILURE_T("glShaderSource or glCompileShader failed", target);
return 0;
}
if (!glIsShader_func(shader)) {
REPORT_FAILURE("glIsShader failed (fragment)");
return false;
}
glGetShaderiv_func(shader, GL_SHADER_TYPE, &val);
if (val != (GLint) target) {
REPORT_FAILURE_T("glGetShaderiv(GL_SHADER_TYPE) failed", target);
return 0;
}
glGetShaderiv_func(shader, GL_COMPILE_STATUS, &val);
if (val != GL_TRUE) {
REPORT_FAILURE_T("glGetShaderiv(GL_COMPILE_STATUS) failed", target);
return 0;
}
glGetShaderiv_func(shader, GL_SHADER_SOURCE_LENGTH, &val);
// Note: some OpenGLs return a 1-char shorter length than strlen(text)
if (abs(val - (int) strlen(text)) > 1) {
REPORT_FAILURE_T("glGetShaderiv(GL_SHADER_SOURCE_LENGTH) failed", target);
return 0;
}
return shader;
}
GLuint
API2Test::createProgram(GLuint vertShader, GLuint fragShader)
{
GLuint program = glCreateProgram_func();
if (vertShader)
glAttachShader_func(program, vertShader);
if (fragShader)
glAttachShader_func(program, fragShader);
glLinkProgram_func(program);
return program;
}
bool
API2Test::testShaderObjectFuncs(void)
{
static const char *vertShaderText =
"void main() { \n"
" gl_Position = ftransform(); \n"
"} \n";
static const char *fragShaderText =
"void main() { \n"
" gl_FragColor = vec4(1.0, 0.5, 0.25, 0.0); \n"
"} \n";
GLuint vertShader, fragShader, program;
GLint stat, val, err;
vertShader = loadAndCompileShader(GL_VERTEX_SHADER, vertShaderText);
if (!vertShader)
return false;
fragShader = loadAndCompileShader(GL_FRAGMENT_SHADER, fragShaderText);
if (!fragShader)
return false;
program = createProgram(vertShader, fragShader);
if (!program) {
REPORT_FAILURE("glCreateProgram failed");
return false;
}
glGetProgramiv_func(program, GL_LINK_STATUS, &stat);
if (!stat) {
REPORT_FAILURE("glLinkProgram failed");
return false;
}
glUseProgram_func(program);
glGetIntegerv(GL_CURRENT_PROGRAM, &val);
if (val != (GLint) program) {
REPORT_FAILURE("glGetInteger(GL_CURRENT_PROGRAM) failed");
return false;
}
err = glGetError();
if (err) {
REPORT_FAILURE("OpenGL error detected in testShaderFuncs");
return false;
}
if (!glIsProgram_func(program)) {
REPORT_FAILURE("glIsProgram failed");
return false;
}
GLuint objects[2];
GLsizei count;
glGetProgramiv_func(program, GL_ATTACHED_SHADERS, &val);
if (val != 2) {
REPORT_FAILURE("glGetProgramiv(GL_ATTACHED_SHADERS) failed");
return false;
}
glGetAttachedShaders_func(program, 2, &count, objects);
if (count != 2) {
REPORT_FAILURE("glGetAttachedShaders failed (wrong count)");
return false;
}
if (objects[0] != vertShader && objects[1] != vertShader) {
REPORT_FAILURE("glGetAttachedShaders failed (vertex shader missing)");
return false;
}
if (objects[0] != fragShader && objects[1] != fragShader) {
REPORT_FAILURE("glGetAttachedShaders failed (fragment shader missing)");
return false;
}
glValidateProgram_func(program);
glGetProgramiv_func(program, GL_VALIDATE_STATUS, &stat);
if (!stat) {
REPORT_FAILURE("glValidateProgram failed");
return false;
}
// Delete vertex shader
glDeleteShader_func(vertShader);
if (!glIsShader_func(vertShader)) {
// the shader is still attached so the handle should be valid
REPORT_FAILURE("glIsShader(deleted shader) failed");
return false;
}
glGetShaderiv_func(vertShader, GL_DELETE_STATUS, &stat);
if (stat != GL_TRUE) {
REPORT_FAILURE("Incorrect shader delete status");
return false;
}
// Delete fragment shader
glDeleteShader_func(fragShader);
// Delete program object
glDeleteProgram_func(program);
if (!glIsProgram_func(program)) {
// the program is still in use so the handle should be valid
REPORT_FAILURE("glIsProgram(deleted program) failed");
return false;
}
glGetProgramiv_func(program, GL_DELETE_STATUS, &stat);
if (stat != GL_TRUE) {
REPORT_FAILURE("Incorrect program delete status");
return false;
}
// now unbind the program
glUseProgram_func(0);
stat = glIsProgram_func(program);
if (stat) {
// the program and handle should have really been deleted now
REPORT_FAILURE("glIsProgram(deleted program) failed");
return false;
}
glGetProgramiv_func(program, GL_DELETE_STATUS, &stat);
err = glGetError();
if (!err) {
// the program and handle should have been deleted now
// so glGetProgramiv() should have generated an error
REPORT_FAILURE("glGetProgramiv(deleted program) failed");
return false;
}
return true;
}
bool
API2Test::testUniformfFuncs(void)
{
static const char *fragShaderText =
"uniform float uf1; \n"
"uniform vec2 uf2; \n"
"uniform vec3 uf3; \n"
"uniform vec4 uf4; \n"
"void main() { \n"
" gl_FragColor = vec4(uf1, uf2.y, uf3.z, uf4.w); \n"
"} \n";
GLuint fragShader, program;
GLint uf1, uf2, uf3, uf4;
GLfloat value[4];
fragShader = loadAndCompileShader(GL_FRAGMENT_SHADER, fragShaderText);
if (!fragShader) {
return false;
}
program = createProgram(0, fragShader);
if (!program) {
REPORT_FAILURE("glCreateProgram (uniform test) failed");
return false;
}
glUseProgram_func(program);
uf1 = glGetUniformLocation_func(program, "uf1");
if (uf1 < 0) {
REPORT_FAILURE("glGetUniform \"uf1\" failed");
return false;
}
uf2 = glGetUniformLocation_func(program, "uf2");
if (uf2 < 0) {
REPORT_FAILURE("glGetUniform \"uf2\" failed");
return false;
}
uf3 = glGetUniformLocation_func(program, "uf3");
if (uf3 < 0) {
REPORT_FAILURE("glGetUniform \"uf3\" failed");
return false;
}
uf4 = glGetUniformLocation_func(program, "uf4");
if (uf4 < 0) {
REPORT_FAILURE("glGetUniform \"uf4\" failed");
return false;
}
GLfloat pixel[4], expected[4];
// Test glUniform[1234]f()
expected[0] = 0.1;
expected[1] = 0.2;
expected[2] = 0.3;
expected[3] = 0.4;
glUniform1f_func(uf1, expected[0]);
glUniform2f_func(uf2, 0.0, expected[1]);
glUniform3f_func(uf3, 0.0, 0.0, expected[2]);
glUniform4f_func(uf4, 0.0, 0.0, 0.0, expected[3]);
renderQuad(pixel);
if (!equalColors(pixel, expected)) {
REPORT_FAILURE("glUniform[1234]f failed");
//printf("found: %f %f %f %f\n", pixel[0], pixel[1], pixel[2], pixel[3]);
//printf("expected: %f %f %f %f\n", expected[0], expected[1], expected[2], expected[3]);
return false;
}
// Test glUniform[1234]fv()
GLfloat u[4];
expected[0] = 0.9;
expected[1] = 0.8;
expected[2] = 0.7;
expected[3] = 0.6;
u[0] = expected[0];
glUniform1fv_func(uf1, 1, u);
u[0] = 0.0; u[1] = expected[1];
glUniform2fv_func(uf2, 1, u);
u[0] = 0.0; u[1] = 0.0; u[2] = expected[2];
glUniform3fv_func(uf3, 1, u);
u[0] = 0.0; u[1] = 0.0; u[2] = 0.0; u[3] = expected[3];
glUniform4fv_func(uf4, 1, u);
renderQuad(pixel);
if (!equalColors(pixel, expected)) {
REPORT_FAILURE("glUniform[1234]f failed");
return false;
}
// Test glGetUniformfv
glUniform4fv_func(uf4, 1, expected);
glGetUniformfv_func(program, uf4, value);
if (value[0] != expected[0] ||
value[1] != expected[1] ||
value[2] != expected[2] ||
value[3] != expected[3]) {
REPORT_FAILURE("glGetUniformfv failed");
return false;
}
return true;
}
bool
API2Test::testUniformiFuncs(void)
{
static const char *fragShaderText =
"uniform int ui1; \n"
"uniform ivec2 ui2; \n"
"uniform ivec3 ui3; \n"
"uniform ivec4 ui4; \n"
"void main() { \n"
" gl_FragColor = vec4(ui1, ui2.y, ui3.z, ui4.w) * 0.1; \n"
"} \n";
GLuint fragShader, program;
GLint ui1, ui2, ui3, ui4;
fragShader = loadAndCompileShader(GL_FRAGMENT_SHADER, fragShaderText);
if (!fragShader) {
return false;
}
program = createProgram(0, fragShader);
if (!program) {
REPORT_FAILURE("glCreateProgram (uniform test) failed");
return false;
}
glUseProgram_func(program);
ui1 = glGetUniformLocation_func(program, "ui1");
if (ui1 < 0) {
REPORT_FAILURE("glGetUniform \"ui1\" failed");
return false;
}
ui2 = glGetUniformLocation_func(program, "ui2");
if (ui2 < 0) {
REPORT_FAILURE("glGetUniform \"ui2\" failed");
return false;
}
ui3 = glGetUniformLocation_func(program, "ui3");
if (ui3 < 0) {
REPORT_FAILURE("glGetUniform \"ui3\" failed");
return false;
}
ui4 = glGetUniformLocation_func(program, "ui4");
if (ui4 < 0) {
REPORT_FAILURE("glGetUniform \"ui4\" failed");
return false;
}
GLfloat pixel[4], expected[4];
GLint expectedInt[4];
// Test glUniform[1234]i()
expectedInt[0] = 1;
expectedInt[1] = 2;
expectedInt[2] = 3;
expectedInt[3] = 4;
expected[0] = 0.1;
expected[1] = 0.2;
expected[2] = 0.3;
expected[3] = 0.4;
glUniform1i_func(ui1, expectedInt[0]);
glUniform2i_func(ui2, 0, expectedInt[1]);
glUniform3i_func(ui3, 0, 0, expectedInt[2]);
glUniform4i_func(ui4, 0, 0, 0, expectedInt[3]);
renderQuad(pixel);
if (!equalColors(pixel, expected)) {
REPORT_FAILURE("glUniform[1234]i failed");
//printf("%f %f %f %f\n", pixel[0], pixel[1], pixel[2], pixel[3]);
return false;
}
// Test glUniform[1234]iv()
GLint u[4];
expectedInt[0] = 9;
expectedInt[1] = 8;
expectedInt[2] = 7;
expectedInt[3] = 6;
expected[0] = 0.9;
expected[1] = 0.8;
expected[2] = 0.7;
expected[3] = 0.6;
u[0] = expectedInt[0];
glUniform1iv_func(ui1, 1, u);
u[0] = 0; u[1] = expectedInt[1];
glUniform2iv_func(ui2, 1, u);
u[0] = 0; u[1] = 0; u[2] = expectedInt[2];
glUniform3iv_func(ui3, 1, u);
u[0] = 0; u[1] = 0; u[2] = 0; u[3] = expectedInt[3];
glUniform4iv_func(ui4, 1, u);
renderQuad(pixel);
if (!equalColors(pixel, expected)) {
REPORT_FAILURE("glUniform[1234]i failed");
#if 0
printf("Expected color %f %f %f %f\n",
expected[0], expected[1], expected[2], expected[3]);
printf("Found color %f %f %f %f\n",
pixel[0], pixel[1], pixel[2], pixel[3]);
#endif
return false;
}
return true;
}
bool
API2Test::testShaderAttribs(void)
{
static const char *vertShaderText =
"attribute vec4 generic; \n"
"void main() { \n"
" gl_Position = ftransform(); \n"
" gl_FrontColor = generic; \n"
"} \n";
GLuint vertShader, program;
vertShader = loadAndCompileShader(GL_VERTEX_SHADER, vertShaderText);
if (!vertShader) {
return false;
}
program = createProgram(vertShader, 0);
if (!program) {
REPORT_FAILURE("glCreateProgram (uniform test) failed");
return false;
}
glUseProgram_func(program);
static const GLfloat testColors[3][4] = {
{ 1.0, 0.5, 0.25, 0.0 },
{ 0.0, 0.1, 0.2, 0.3 },
{ 0.5, 0.6, 0.7, 0.8 },
};
// let compiler allocate the attribute location
const GLint attr = glGetAttribLocation_func(program, "generic");
if (attr < 0) {
REPORT_FAILURE("glGetAttribLocation failed");
return false;
}
for (int i = 0; i < 3; i++) {
GLfloat pixel[4];
renderQuadWithArrays(attr, testColors[i], pixel);
if (!equalColors(pixel, testColors[i])) {
#if 0
printf("Expected color %f %f %f\n",
testColors[i][0],
testColors[i][1],
testColors[i][2]);
printf("Found color %f %f %f\n",
pixel[0], pixel[1], pixel[2]);
#endif
REPORT_FAILURE("Vertex array test failed");
return false;
}
}
// Test explicit attribute binding.
const GLint bindAttr = 6; // XXX a non-colliding alias
glBindAttribLocation_func(program, bindAttr, "generic");
glLinkProgram_func(program);
GLint loc = glGetAttribLocation_func(program, "generic");
if (loc != bindAttr) {
REPORT_FAILURE("glBindAttribLocation failed");
return false;
}
for (int i = 0; i < 3; i++) {
GLfloat pixel[4];
renderQuadWithArrays(bindAttr, testColors[i], pixel);
if (!equalColors(pixel, testColors[i])) {
REPORT_FAILURE("Vertex array test failed (2)");
return false;
}
}
return true;
}
#define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
bool
API2Test::testStencilFuncSeparate(void)
{
GLint val;
GLint stencilBits, stencilMax;
glGetIntegerv(GL_STENCIL_BITS, &stencilBits);
stencilMax = (1 << stencilBits) - 1;
glStencilFuncSeparate_func(GL_FRONT, GL_LEQUAL, 12, 0xf);
glStencilFuncSeparate_func(GL_BACK, GL_GEQUAL, 13, 0xe);
glGetIntegerv(GL_STENCIL_BACK_FUNC, &val);
if (val != GL_GEQUAL) {
REPORT_FAILURE("GL_STENCIL_BACK_FUNC query returned wrong value");
return false;
}
glGetIntegerv(GL_STENCIL_FUNC, &val);
if (val != GL_LEQUAL) {
REPORT_FAILURE("GL_STENCIL_FUNC (front) query returned wrong value");
return false;
}
glGetIntegerv(GL_STENCIL_BACK_REF, &val);
if (val != CLAMP(13, 0, stencilMax)) {
REPORT_FAILURE("GL_STENCIL_BACK_REF query returned wrong value");
return false;
}
glGetIntegerv(GL_STENCIL_REF, &val);
if (val != CLAMP(12, 0, stencilMax)) {
REPORT_FAILURE("GL_STENCIL_REF (front) query returned wrong value");
return false;
}
glGetIntegerv(GL_STENCIL_BACK_VALUE_MASK, &val);
if (val != 0xe) {
REPORT_FAILURE("GL_STENCIL_BACK_VALUE_MASK query returned wrong value");
return false;
}
glGetIntegerv(GL_STENCIL_VALUE_MASK, &val);
if (val != 0xf) {
REPORT_FAILURE("GL_STENCIL_VALUE_MASK (front) query returned wrong value");
return false;
}
return true;
}
bool
API2Test::testStencilOpSeparate(void)
{
GLint val;
// face, fail, zfail, zpass
glStencilOpSeparate_func(GL_FRONT, GL_INVERT, GL_ZERO, GL_INCR);
glStencilOpSeparate_func(GL_BACK, GL_INCR, GL_KEEP, GL_REPLACE);
glGetIntegerv(GL_STENCIL_BACK_FAIL, &val);
if (val != GL_INCR) {
REPORT_FAILURE("GL_STENCIL_BACK_FAIL query returned wrong value");
return false;
}
glGetIntegerv(GL_STENCIL_FAIL, &val);
if (val != GL_INVERT) {
REPORT_FAILURE("GL_STENCIL_FAIL (front) query returned wrong value");
return false;
}
glGetIntegerv(GL_STENCIL_BACK_PASS_DEPTH_FAIL, &val);
if (val != GL_KEEP) {
REPORT_FAILURE("GL_STENCIL_BACK_PASS_DEPTH_FAIL query returned wrong value");
return false;
}
glGetIntegerv(GL_STENCIL_PASS_DEPTH_FAIL, &val);
if (val != GL_ZERO) {
REPORT_FAILURE("GL_STENCIL_PASS_DEPTH_FAIL (front) query returned wrong value");
return false;
}
glGetIntegerv(GL_STENCIL_BACK_PASS_DEPTH_PASS, &val);
if (val != GL_REPLACE) {
REPORT_FAILURE("GL_STENCIL_BACK_PASS_DEPTH_PASS query returned wrong value");
return false;
}
glGetIntegerv(GL_STENCIL_PASS_DEPTH_PASS, &val);
if (val != GL_INCR) {
REPORT_FAILURE("GL_STENCIL_PASS_DEPTH_PASS (front) query returned wrong value");
return false;
}
return true;
}
bool
API2Test::testStencilMaskSeparate(void)
{
GLint val;
// face, fail, zfail, zpass
glStencilMaskSeparate_func(GL_BACK, 0xa);
glStencilMaskSeparate_func(GL_FRONT, 0xb);
glGetIntegerv(GL_STENCIL_BACK_WRITEMASK, &val);
if (val != 0xa) {
REPORT_FAILURE("GL_STENCIL_BACK_WRITEMASK query returned wrong value");
return false;
}
glGetIntegerv(GL_STENCIL_WRITEMASK, &val);
if (val != 0xb) {
REPORT_FAILURE("GL_STENCIL_WRITEMASK (front) query returned wrong value");
return false;
}
return true;
}
bool
API2Test::testBlendEquationSeparate(void)
{
GLint val;
glBlendEquationSeparate_func(GL_MAX, GL_FUNC_SUBTRACT);
glGetIntegerv(GL_BLEND_EQUATION, &val);
if (val != GL_MAX) {
REPORT_FAILURE("GL_BLEND_EQUATION (rgb) query returned wrong value");
return false;
}
glGetIntegerv(GL_BLEND_EQUATION_ALPHA, &val);
if (val != GL_FUNC_SUBTRACT) {
REPORT_FAILURE("GL_BLEND_EQUATION (rgb) query returned wrong value");
return false;
}
return true;
}
bool
API2Test::testDrawBuffers(void)
{
const int MAX = 2;
GLint maxBuf = -1, i, n, val;
GLenum buffers[MAX], err;
GLint initDrawBuffer;
glGetIntegerv(GL_DRAW_BUFFER, &initDrawBuffer);
glGetIntegerv(GL_MAX_DRAW_BUFFERS, &maxBuf);
if (maxBuf < 1) {
REPORT_FAILURE("GL_MAX_DRAW_BUFFERS query failed");
return false;
}
n = maxBuf < MAX ? maxBuf : MAX;
assert(n > 0);
for (i = 0; i < n; i++) {
buffers[i] = (i & 1) ? GL_FRONT_LEFT : GL_BACK_LEFT;
}
glDrawBuffers_func(n, buffers);
for (i = 0; i < n; i++) {
glGetIntegerv(GL_DRAW_BUFFER0 + i, &val);
if (val != (GLint) buffers[i]) {
REPORT_FAILURE("glDrawBuffers failed");
return false;
}
}
// restore
glDrawBuffer(initDrawBuffer);
err = glGetError();
if (err) {
REPORT_FAILURE("glDrawBuffers generrated an OpenGL error");
return false;
}
return true;
}
// Run all the subtests, incrementing numPassed, numFailed
void
API2Test::runSubTests(MultiTestResult &r)
{
static TestFunc funcs[] = {
&GLEAN::API2Test::testStencilFuncSeparate,
&GLEAN::API2Test::testStencilOpSeparate,
&GLEAN::API2Test::testStencilMaskSeparate,
&GLEAN::API2Test::testBlendEquationSeparate,
&GLEAN::API2Test::testDrawBuffers,
&GLEAN::API2Test::testShaderObjectFuncs,
&GLEAN::API2Test::testUniformfFuncs,
&GLEAN::API2Test::testUniformiFuncs,
&GLEAN::API2Test::testShaderAttribs,
NULL
};
for (int i = 0; funcs[i]; i++)
if ((this->*funcs[i])())
r.numPassed++;
else
r.numFailed++;
}
void
API2Test::runOne(MultiTestResult &r, Window &w)
{
(void) w;
if (!setup()) {
r.pass = false;
return;
}
runSubTests(r);
r.pass = (r.numFailed == 0);
}
// The test object itself:
API2Test api2Test("api2", "window, rgb, z, db",
"", // no extension filter (we'll test for version 2.x during setup)
"API2 test: check that OpenGL 2.x API functions work.\n"
);
} // namespace GLEAN
|