summaryrefslogtreecommitdiff
path: root/tests/spec/gl-3.0/bound-resource-limits.c
blob: 1e5cf41c34e7b122588cf45a21826ad67d3d518e (plain)
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
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
/*
 * Copyright 2014 VMware, Inc.
 *
 * 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 (including the next
 * paragraph) 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
 * THE AUTHORS OR COPYRIGHT HOLDERS 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.
 */

/**
 * Test resource limits given the maximum supported by the implementation.
 *
 * Each component of the fragment output is derived using the following
 * expression (indexed at the scalar level):
 *
 *   result[i] = texture[i] * texture[i] * ... * attrib[i + l * numOutputs]
 *
 * Depending on the limits for vertex/fragment image units, the texture
 * contribution will vary. See the generation of g_expected at the end of
 * piglit_init.
 *
 * Since the scalar inputs are primes, multiplication will yield a unique
 * result. Results can be diagnosed by evaluating for their missing factor(s).
 *
 * Matthew McClure
 * 7 July 2014
 */

#include "piglit-util-gl.h"

PIGLIT_GL_TEST_CONFIG_BEGIN

        config.supports_gl_compat_version = 30;
        config.window_visual = PIGLIT_GL_VISUAL_DOUBLE | PIGLIT_GL_VISUAL_RGBA;

PIGLIT_GL_TEST_CONFIG_END


/*
 * Definitions for the runtime behavior.
 */
#define GLSL_VERSION                    "#version 130"
#define BUFFER_WIDTH                    32
#define BUFFER_HEIGHT                   32
#define MAX_SHADER_LINE_CHARS           256
#define MAX_SHADER_TEXT_CHARS           16*1024
#define MAX_COMPONENTS                  4
#define MAX_EXPRESSION_OUTPUTS          1
#define MAX_EXPRESSION_ARGUMENTS        2
#define MAX_EXPRESSION_TEMPS            1
#define TEMP_TEXT_SIZE                  (MAX_EXPRESSION_OUTPUTS +  \
                                         MAX_EXPRESSION_ARGUMENTS +  \
                                         MAX_EXPRESSION_ARGUMENTS)

#define NUM_PRIMES                      512
#define NUM_VERTICES                    3

#define DEBUG_INPUT                     0x01
#define DEBUG_READBACK                  0x02
#define DEBUG_SHADERS                   0x04
#define DEBUG_DRAW                      0x08
#define DEBUG_DONT_CLAMP_MAX_VARYINGS   0x10

/*
 * Type definitions for our working set.
 */
enum {
        DRAW_ARRAYS_VBO = 0,
        DRAW_ELEMENTS_VBO = 1,
        DRAW_IMMEDIATE = 2,
};

typedef struct MyVector4 {
        float x, y, z, w;
} MyVector4;

typedef struct PackedTypeDesc {
        char *typeName;
        unsigned numComponents;
        char *componentNames[MAX_COMPONENTS];
        char *defaultValue;
} PackedTypeDesc;

typedef struct PackedDesc {
        char *semanticName;
        char *variableName;
        bool isArray;
        unsigned count;
        const PackedTypeDesc *typeDesc;
} PackedDesc;


/*
 * Global variables for the test's state.
 */
static GLint g_maxVaryingFloats = 0;
static GLint g_maxVertexAttribs = 0;
static GLint g_maxVertexTextureImageUnits = 0;
static GLint g_maxTextureImageUnits = 0;
static GLint g_maxCombinedTextureImageUnits = 0;

static GLint g_maxAuxBuffers = 0;
static GLint g_maxDrawBuffers = 0;
static GLint g_maxColorAttachments = 0;

static GLint g_debugMask = 0x0;
static GLint g_drawMode = DRAW_IMMEDIATE;

static char *g_fragColor = "gl_FragColor";
static char *g_fragData = "gl_FragData";

static const GLfloat g_primes[NUM_PRIMES] = {
2.0, 3.0, 5.0, 7.0, 11.0, 13.0, 17.0, 19.0,
23.0, 29.0, 31.0, 37.0, 41.0, 43.0, 47.0, 53.0,
59.0, 61.0, 67.0, 71.0, 73.0, 79.0, 83.0, 89.0,
97.0, 101.0, 103.0, 107.0, 109.0, 113.0, 127.0, 131.0,
137.0, 139.0, 149.0, 151.0, 157.0, 163.0, 167.0, 173.0,
179.0, 181.0, 191.0, 193.0, 197.0, 199.0, 211.0, 223.0,
227.0, 229.0, 233.0, 239.0, 241.0, 251.0, 257.0, 263.0,
269.0, 271.0, 277.0, 281.0, 283.0, 293.0, 307.0, 311.0,
313.0, 317.0, 331.0, 337.0, 347.0, 349.0, 353.0, 359.0,
367.0, 373.0, 379.0, 383.0, 389.0, 397.0, 401.0, 409.0,
419.0, 421.0, 431.0, 433.0, 439.0, 443.0, 449.0, 457.0,
461.0, 463.0, 467.0, 479.0, 487.0, 491.0, 499.0, 503.0,
509.0, 521.0, 523.0, 541.0, 547.0, 557.0, 563.0, 569.0,
571.0, 577.0, 587.0, 593.0, 599.0, 601.0, 607.0, 613.0,
617.0, 619.0, 631.0, 641.0, 643.0, 647.0, 653.0, 659.0,
661.0, 673.0, 677.0, 683.0, 691.0, 701.0, 709.0, 719.0,
727.0, 733.0, 739.0, 743.0, 751.0, 757.0, 761.0, 769.0,
773.0, 787.0, 797.0, 809.0, 811.0, 821.0, 823.0, 827.0,
829.0, 839.0, 853.0, 857.0, 859.0, 863.0, 877.0, 881.0,
883.0, 887.0, 907.0, 911.0, 919.0, 929.0, 937.0, 941.0,
947.0, 953.0, 967.0, 971.0, 977.0, 983.0, 991.0, 997.0,
1009.0, 1013.0, 1019.0, 1021.0, 1031.0, 1033.0, 1039.0, 1049.0,
1051.0, 1061.0, 1063.0, 1069.0, 1087.0, 1091.0, 1093.0, 1097.0,
1103.0, 1109.0, 1117.0, 1123.0, 1129.0, 1151.0, 1153.0, 1163.0,
1171.0, 1181.0, 1187.0, 1193.0, 1201.0, 1213.0, 1217.0, 1223.0,
1229.0, 1231.0, 1237.0, 1249.0, 1259.0, 1277.0, 1279.0, 1283.0,
1289.0, 1291.0, 1297.0, 1301.0, 1303.0, 1307.0, 1319.0, 1321.0,
1327.0, 1361.0, 1367.0, 1373.0, 1381.0, 1399.0, 1409.0, 1423.0,
1427.0, 1429.0, 1433.0, 1439.0, 1447.0, 1451.0, 1453.0, 1459.0,
1471.0, 1481.0, 1483.0, 1487.0, 1489.0, 1493.0, 1499.0, 1511.0,
1523.0, 1531.0, 1543.0, 1549.0, 1553.0, 1559.0, 1567.0, 1571.0,
1579.0, 1583.0, 1597.0, 1601.0, 1607.0, 1609.0, 1613.0, 1619.0,
1621.0, 1627.0, 1637.0, 1657.0, 1663.0, 1667.0, 1669.0, 1693.0,
1697.0, 1699.0, 1709.0, 1721.0, 1723.0, 1733.0, 1741.0, 1747.0,
1753.0, 1759.0, 1777.0, 1783.0, 1787.0, 1789.0, 1801.0, 1811.0,
1823.0, 1831.0, 1847.0, 1861.0, 1867.0, 1871.0, 1873.0, 1877.0,
1879.0, 1889.0, 1901.0, 1907.0, 1913.0, 1931.0, 1933.0, 1949.0,
1951.0, 1973.0, 1979.0, 1987.0, 1993.0, 1997.0, 1999.0, 2003.0,
2011.0, 2017.0, 2027.0, 2029.0, 2039.0, 2053.0, 2063.0, 2069.0,
2081.0, 2083.0, 2087.0, 2089.0, 2099.0, 2111.0, 2113.0, 2129.0,
2131.0, 2137.0, 2141.0, 2143.0, 2153.0, 2161.0, 2179.0, 2203.0,
2207.0, 2213.0, 2221.0, 2237.0, 2239.0, 2243.0, 2251.0, 2267.0,
2269.0, 2273.0, 2281.0, 2287.0, 2293.0, 2297.0, 2309.0, 2311.0,
2333.0, 2339.0, 2341.0, 2347.0, 2351.0, 2357.0, 2371.0, 2377.0,
2381.0, 2383.0, 2389.0, 2393.0, 2399.0, 2411.0, 2417.0, 2423.0,
2437.0, 2441.0, 2447.0, 2459.0, 2467.0, 2473.0, 2477.0, 2503.0,
2521.0, 2531.0, 2539.0, 2543.0, 2549.0, 2551.0, 2557.0, 2579.0,
2591.0, 2593.0, 2609.0, 2617.0, 2621.0, 2633.0, 2647.0, 2657.0,
2659.0, 2663.0, 2671.0, 2677.0, 2683.0, 2687.0, 2689.0, 2693.0,
2699.0, 2707.0, 2711.0, 2713.0, 2719.0, 2729.0, 2731.0, 2741.0,
2749.0, 2753.0, 2767.0, 2777.0, 2789.0, 2791.0, 2797.0, 2801.0,
2803.0, 2819.0, 2833.0, 2837.0, 2843.0, 2851.0, 2857.0, 2861.0,
2879.0, 2887.0, 2897.0, 2903.0, 2909.0, 2917.0, 2927.0, 2939.0,
2953.0, 2957.0, 2963.0, 2969.0, 2971.0, 2999.0, 3001.0, 3011.0,
3019.0, 3023.0, 3037.0, 3041.0, 3049.0, 3061.0, 3067.0, 3079.0,
3083.0, 3089.0, 3109.0, 3119.0, 3121.0, 3137.0, 3163.0, 3167.0,
3169.0, 3181.0, 3187.0, 3191.0, 3203.0, 3209.0, 3217.0, 3221.0,
3229.0, 3251.0, 3253.0, 3257.0, 3259.0, 3271.0, 3299.0, 3301.0,
3307.0, 3313.0, 3319.0, 3323.0, 3329.0, 3331.0, 3343.0, 3347.0,
3359.0, 3361.0, 3371.0, 3373.0, 3389.0, 3391.0, 3407.0, 3413.0,
3433.0, 3449.0, 3457.0, 3461.0, 3463.0, 3467.0, 3469.0, 3491.0,
3499.0, 3511.0, 3517.0, 3527.0, 3529.0, 3533.0, 3539.0, 3541.0,
3547.0, 3557.0, 3559.0, 3571.0, 3581.0, 3583.0, 3593.0, 3607.0,
3613.0, 3617.0, 3623.0, 3631.0, 3637.0, 3643.0, 3659.0, 3671.0,
};

static const PackedTypeDesc g_rgbaDesc = {
        "vec4", 4, { ".r", ".g", ".b", ".a" },
        "vec4(1.0, 1.0, 1.0, 1.0)"
};

static const PackedTypeDesc g_vec4Desc = {
        "vec4", 4, { ".x", ".y", ".z", ".w" },
        "vec4(1.0, 1.0, 1.0, 1.0)"
};

static const PackedTypeDesc g_floatDesc = {
        "float", 1, { "" },
        "1.0"
};

static const PackedTypeDesc g_sampler2DDesc = {
        "sampler2D", 4, { "" },
        "vec4(1.0, 1.0, 1.0, 1.0)"
};

static const char *g_vectorComponents[MAX_COMPONENTS] = {
        ".x", ".y", ".z", ".w"
};

//static const MyVector4 one = { 1.0f, 1.0f, 1.0f, 1.0f };
//static const MyVector4 zero = { 0.0f, 0.0f, 0.0f, 0.0f };

static const MyVector4 g_positionBuffer[NUM_VERTICES] = {
        { -1.0, -1.0, 0.0, 0.0 },
        { -1.0, 1.0, 0.0, 0.0 },
        { 1.0, 1.0, 0.0, 0.0 }
};

static GLuint g_elementVBO;
static const GLushort g_elementBuffer[NUM_VERTICES] = { 0, 1, 2 };
static const char *g_vertexShaderText =
        "#version 110 \n"
        " \n"
        "void main() \n"
        "{ \n"
        "   gl_Position = gl_Vertex; \n"
        "} \n";

//static char *g_geometryShaderText = NULL;

static const char *g_fragmentShaderText =
        "#version 110 \n"
        " \n"
        "void main()\n"
        "{ \n"
        "    gl_FragData[0] = vec4(1.0, 2.0, 3.0, 4.0);\n"
        "    gl_FragData[1] = vec4(5.0, 6.0, 7.0, 8.0);\n"
        "} \n";

static GLuint g_program = 0;
static GLfloat *g_expected;


/**
 * Format a string which contains the requested declaration for the variable.
 * The desc structure will describe the variable, i.e. type, if it is an array,
 * and how large the array is.
 *
 * \param desc Descriptor of the variable referenced. See PackedDesc.
 * \param tmpStrSize Temporary string size for safe string formatting.
 * \param tmpStr Temporary staging string.
 * \param strSize Size of the formatted string.
 * \param str An allocated temporary string for this function to populate with
 *            the reference expression.
 * \return Pointer to str.
 */

static char *
get_packed_decl(const PackedDesc *desc,
                const unsigned tmpStrSize,
                char *tmpStr,
                const unsigned strSize,
                char *str)
{
        int i = 0;

        if (NULL == str) {
                return "";
        }

        if (desc->isArray) {
                snprintf(tmpStr, tmpStrSize,
                         "%s %s %s%s%u%s;\n",
                         desc->semanticName,
                         desc->typeDesc->typeName,
                         desc->variableName,
                         desc->isArray ? "[" : "",
                         desc->count,
                         desc->isArray ? "]" : "");
                strncat(str, tmpStr, strSize - strlen(str));
        } else if (desc->count > 1) {
                for (i = 0; i < desc->count; i++) {
                        snprintf(tmpStr, tmpStrSize,
                                 "%s %s %s%u;\n",
                                 desc->semanticName,
                                 desc->typeDesc->typeName,
                                 desc->variableName, i);
                        strncat(str, tmpStr, strSize - strlen(str));
                }
        } else {
                snprintf(tmpStr, tmpStrSize,
                         "%s %s %s;\n",
                         desc->semanticName,
                         desc->typeDesc->typeName,
                         desc->variableName);
                strncat(str, tmpStr, strSize - strlen(str));
        }

        return str;
}


/**
 * Format a string which contains the requested referenced value. The desc
 * structure will describe the variable which is sub-indexed by arrayIndex.
 * To address individual components, a componentIndex translated to (x,y,z,w)
 * is applied to the textual representation.
 *
 * \param desc Descriptor of the variable referenced. See PackedDesc.
 * \param arrayIndex Sub-index into the variable described by desc.
 * \param componentIndex Component of the variable, depends on the desc
 *                       configuration for the variable.
 * \param str An allocated temporary string for this function to populate with
 *            the reference expression.
 * \return Pointer to str.
 */

static char *
get_packed_reference(const PackedDesc *desc,
                     const unsigned arrayIndex,
                     const unsigned componentIndex,
                     const unsigned strSize,
                     char *str)
{
        if (g_debugMask & DEBUG_INPUT) {
                printf("desc=%p componentIndex=%u\n", desc, componentIndex);
                printf("desc->typeDesc=%p\n", desc->typeDesc);
                printf("desc->typeDesc->componentNames=%p\n",
                       desc->typeDesc->componentNames);
        }

        if (componentIndex > desc->typeDesc->numComponents) {
                snprintf(str, strSize, "%s", g_floatDesc.defaultValue);
                return str;
        }

        if (arrayIndex > desc->count) {
                snprintf(str, strSize, "%s%s",
                         desc->typeDesc->defaultValue,
                         desc->typeDesc->componentNames[componentIndex]);
                return str;
        }

        if (desc->count > 1) {
                snprintf(str, strSize, "%s%s%u%s%s",
                         desc->variableName,
                         desc->isArray ? "[" : "",
                         arrayIndex,
                         desc->isArray ? "]" : "",
                         desc->typeDesc->componentNames[componentIndex]);
        } else {
                snprintf(str, strSize, "%s%s",
                         desc->variableName,
                         desc->typeDesc->componentNames[componentIndex]);
        }

        return str;
}


/**
 * Set up an FBO for the test.
 *
 * \param colorTarget Color attachment index relative to GL_COLOR_ATTACHMENT0
 * \param internalFormat Internal GL format for the pixel data.
 * \param format GL format for the pixel data.
 * \param formatType Data type for the pixel data. 
 * \param width Width of the requested FBO backing store.
 * \param height Height of the requested FBO backing store.
 * \param fbo FBO object allocated to bind with this texture.
 * \param pTexture A pointer to the GLuint identifier storage for the allocated
 *                 texture.
 * \return true if the setup succeeded without error
 *         false otherwise.
 */

static bool
setup_fbo_2d(const GLuint colorTarget,
             const GLenum internalFormat,
             const GLenum format,
             const GLenum formatType,
             const GLuint width,
             const GLuint height,
             const GLuint fbo,
             GLuint *pTexture)
{
        GLenum status;

        glGenTextures(1, pTexture);
        glBindTexture(GL_TEXTURE_2D, *pTexture);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, width, height, 0,
                     format, formatType, NULL);

        /*
         * Framebuffer object is implied to be bound.
         */
        glFramebufferTexture2D(GL_FRAMEBUFFER,
                               GL_COLOR_ATTACHMENT0 + colorTarget,
                               GL_TEXTURE_2D, *pTexture, 0);

        if (!piglit_check_gl_error(GL_NO_ERROR)) {
                fprintf(stderr, "Failed to create FBO %u.\n", colorTarget);
                piglit_report_result(PIGLIT_FAIL);
                return false;
        }

        status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
        if (status != GL_FRAMEBUFFER_COMPLETE) {
                fprintf(stderr, "Incomplete fbo for format %s.%s (status %s)\n",
                        piglit_get_gl_enum_name(internalFormat),
                        piglit_get_gl_enum_name(format),
                        piglit_get_gl_enum_name(status));
                piglit_report_result(PIGLIT_FAIL);
                return false;
        }

        return true;
}


/**
 * Set up the vertex buffer objects and element buffer objects for the
 * test. To ensure constant values across the primitive, populate the same
 * prime attribute value in each vertex.
 *
 * \return true if the setup succeeded without error
 *         false otherwise.
 */

static bool
setup_vertex_element_buffers(void)
{
        MyVector4 attrib[NUM_VERTICES];
        GLuint buf;
        GLuint attribLoc;
        int i;

        /*
         * Setup the gl_Position attribute buffer.
         */
        glGenBuffers(1, &buf);
        glBindBuffer(GL_ARRAY_BUFFER, buf);
        glBufferData(GL_ARRAY_BUFFER, NUM_VERTICES*sizeof(MyVector4),
                     g_positionBuffer, GL_STATIC_DRAW);

        attribLoc = glGetAttribLocation(g_program, "InPosition");
        glEnableVertexAttribArray(attribLoc);
        glVertexAttribPointer(attribLoc, 4, GL_FLOAT, GL_FALSE, 0, 0);
        glBindBuffer(GL_ARRAY_BUFFER, 0);

        /*
         * Setup the vertex buffer objects.
         */
        for (i = 0; i < g_maxVertexAttribs - 1; i++) {
                char strTemp[16];

                if ((i + 1) * MAX_COMPONENTS < NUM_PRIMES) {
                        attrib[0].x = attrib[1].x =
                        attrib[2].x = g_primes[i*MAX_COMPONENTS + 0];
                        attrib[0].y = attrib[1].y =
                        attrib[2].y = g_primes[i*MAX_COMPONENTS + 1];
                        attrib[0].z = attrib[1].z =
                        attrib[2].z = g_primes[i*MAX_COMPONENTS + 2];
                        attrib[0].w = attrib[1].w =
                        attrib[2].w = g_primes[i*MAX_COMPONENTS + 3];
                } else {
                        attrib[0].x = attrib[1].x = attrib[2].x = 1.0;
                        attrib[0].y = attrib[1].y = attrib[2].y = 1.0;
                        attrib[0].z = attrib[1].z = attrib[2].z = 1.0;
                        attrib[0].w = attrib[1].w = attrib[2].w = 1.0;
                }

                glGenBuffers(1, &buf);
                glBindBuffer(GL_ARRAY_BUFFER, buf);
                glBufferData(GL_ARRAY_BUFFER, sizeof(MyVector4)*NUM_VERTICES,
                             attrib, GL_STATIC_DRAW);

                snprintf(strTemp, sizeof(strTemp), "InValue%u", i);
                attribLoc = glGetAttribLocation(g_program, strTemp);
                glEnableVertexAttribArray(attribLoc);
                glVertexAttribPointer(attribLoc, 4, GL_FLOAT, GL_FALSE, 0, 0);
                glBindBuffer(GL_ARRAY_BUFFER, 0);

                if (!piglit_check_gl_error(GL_NO_ERROR)) {
                         fprintf(stderr, "Failed to create VBO %u.\n", i);
                         piglit_report_result(PIGLIT_FAIL);
                         return false;
                }
        }

        /*
         * Setup the element buffers.
         */
        glGenBuffers(1, &g_elementVBO);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_elementVBO);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(g_elementBuffer),
                     g_elementBuffer, GL_STATIC_DRAW);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

        if (!piglit_check_gl_error(GL_NO_ERROR)) {
                fprintf(stderr, "Failed to create IBO.\n");
                piglit_report_result(PIGLIT_FAIL);
                return false;
        }

        return true;
}


/**
 * Build a GLSL shader for the given packedInput, packedUniform, packedOutput
 * descriptors. Each GLSL shader will have inputs, uniforms, and outputs. To
 * ensure all bound resources are made resident, this shader must use all the
 * inputs, uniforms, and outputs requested by the describing PackedDesc
 * structures. If the number of outputs are smaller than the number of inputs,
 * the inputs will map as follows:
 *
 *   outputs[0] = uniforms[0] inputs[0] * ... * inputs[(i % numOutputs == 0)]
 *
 * where i >= numOutputs and represents a subsequent pass of inputs up
 * until i == numInputs.
 *
 * If the number of outputs is larger than the number of inputs, the inputs
 * and uniforms are referenced until exhausted. Once exhausted, an input
 * or uniform will default to 1.0 to provide a multiplicative identity.
 *
 * \param packedInput A descriptor of the type and number of inputs to this
 *                    shader.
 * \param packedUniform A descriptor of the type and number of uniforms bound
 *                      to the shader.
 * \param packedOutput A descriptor of the type and number of outputs for this
 *                     shader.
 * \param outputDefaultSystemValue A required output for the GLSL shader.
 *                                 i.e. gl_Position for a vertex shader.
 * \param defaultSystemValueDecl Pre-defined output declaration.
 * \param defaultSystemValue Pre-defined output value.
 * \param pShaderText Output pointer for the allocated and generated shader
 *                    text.
 * \return true if the setup succeeded without error
 *         false otherwise.
 */

static bool
build_reduce_glsl_shader(const PackedDesc *packedInput,
                         const PackedDesc *packedUniform,
                         const PackedDesc *packedOutput,
                         const char *outputDefaultSystemValue,
                         const char *defaultSystemValueDecl,
                         const char *defaultSystemValue,
                         char **pShaderText)
{
        char *shaderTempText[TEMP_TEXT_SIZE];
        char *shaderText = NULL;
        unsigned shaderTextFree = 0;
        unsigned numScalarInputs =
                packedInput->count * packedInput->typeDesc->numComponents;
        unsigned numScalarUniforms =
                packedUniform->count * packedUniform->typeDesc->numComponents;
        unsigned numScalarOutputs =
                packedOutput->count * packedOutput->typeDesc->numComponents;

        /* Prepare for reduction of input contributions to the output slots */
        unsigned di_do = numScalarInputs / numScalarOutputs;
        unsigned r = numScalarInputs % numScalarOutputs;
        int i, j, k, l;

        if (g_debugMask & DEBUG_INPUT) {
                printf("di_do=%u r=%u\n", di_do, r);
        }

        for (i = 0; i < sizeof(shaderTempText) / sizeof(char *); i++) {
                shaderTempText[i] = calloc(MAX_SHADER_LINE_CHARS, sizeof(char));

                if (NULL == shaderTempText[i]) {
                        fprintf(stderr,
                                "Failed to allocate shader temporary text area\n");
                        piglit_report_result(PIGLIT_FAIL);
                        return false;
                }
        }

        shaderText = calloc(MAX_SHADER_TEXT_CHARS, sizeof(char));
        if (NULL == shaderText) {
                fprintf(stderr, "Failed to allocate space for shader text\n");
                piglit_report_result(PIGLIT_FAIL);
                return false;
        }

        shaderTextFree = MAX_SHADER_TEXT_CHARS;

        snprintf(shaderTempText[0], MAX_SHADER_LINE_CHARS,
        "%s\n", GLSL_VERSION);
        strncat(shaderText, shaderTempText[0], shaderTextFree);
        shaderTextFree -= strlen(shaderTempText[0]);

        /*
         * Declare the default system value input.
         */
        if (outputDefaultSystemValue) {
                snprintf(shaderTempText[0], MAX_SHADER_LINE_CHARS,
                         "%s\n", defaultSystemValueDecl);
                strncat(shaderText, shaderTempText[0], shaderTextFree);
                shaderTextFree -= strlen(defaultSystemValueDecl);
        }

        /*
         * Declare the input attributes.
         */
        get_packed_decl(packedInput,
        MAX_SHADER_LINE_CHARS, shaderTempText[0],
        MAX_SHADER_TEXT_CHARS, shaderText);

        /*
         * Declare the uniform samplers using the vertex texture functionality.
         */
        get_packed_decl(packedUniform,
        MAX_SHADER_LINE_CHARS, shaderTempText[0],
        MAX_SHADER_TEXT_CHARS, shaderText);

        /*
         * Declare the outputs.
         */
        if (packedOutput->semanticName) {
                get_packed_decl(packedOutput,
                                MAX_SHADER_LINE_CHARS, shaderTempText[0],
                                MAX_SHADER_TEXT_CHARS, shaderText);
        }

        shaderTextFree = MAX_SHADER_TEXT_CHARS - strlen(shaderText);

        /*
         * Begin main program block.
         */
        snprintf(shaderTempText[0], MAX_SHADER_LINE_CHARS,
                 "void main()\n{\n");
        strncat(shaderText, shaderTempText[0], shaderTextFree);
        shaderTextFree -= strlen(shaderTempText[0]);

        snprintf(shaderTempText[0], MAX_SHADER_LINE_CHARS,
                 "  vec4 texel;\n");
        strncat(shaderText, shaderTempText[0], shaderTextFree);
        shaderTextFree -= strlen(shaderTempText[0]);

        /*
         * Passthru the attributes to the outputs.
         */
        i = j = k = l = 0;

        while (i < numScalarOutputs)  {
                char *resultStr = " ", *srcStr = " ";

                if ((i < packedUniform->count*packedUniform->typeDesc->numComponents) &&
                    (j % packedUniform->typeDesc->numComponents) == 0) {
                        snprintf(shaderTempText[0], MAX_SHADER_LINE_CHARS,
                                 "  texel = texture2D(Texture[%u], vec2(0.0, 0.0));\n",
                                 i / packedUniform->typeDesc->numComponents);
                        strncat(shaderText, shaderTempText[0], shaderTextFree);
                        shaderTextFree -= strlen(shaderTempText[0]);
                }

                /*
                 * Iterate through the contents of the texel
                 */
                resultStr = get_packed_reference(packedOutput,
                                (i / packedOutput->typeDesc->numComponents),
                                (i % packedOutput->typeDesc->numComponents),
                                MAX_SHADER_LINE_CHARS,
                                shaderTempText[1]);

                /* Reset the temp string to avoid cumulative contributions. */
                memset(shaderTempText[2], 0, MAX_SHADER_LINE_CHARS);

                if (k < numScalarInputs) {
                        /* Reduce the scalar multiple of the input contributions. */
                        for (l = 0; l < di_do; l++) {
                                srcStr = get_packed_reference(packedInput,
                                                (i + (l * numScalarOutputs)) /
                                                packedInput->typeDesc->numComponents,
                                                (i + (l * numScalarOutputs)) %
                                                packedInput->typeDesc->numComponents,
                                                MAX_SHADER_LINE_CHARS,
                                                shaderTempText[3]);

                                if (l > 0) {
                                        strncat(shaderTempText[2], " * ",
                                                MAX_SHADER_LINE_CHARS - strlen(shaderTempText[2]));
                                }
                                strncat(shaderTempText[2], srcStr,
                                        MAX_SHADER_LINE_CHARS - strlen(shaderTempText[2]));

                                k++;
                        }

                        /* Reduce the remaining scalar contributions until exhausted */
                        if (r) {
                                srcStr = get_packed_reference(packedInput,
                                                (i + (l * numScalarOutputs)) /
                                                packedInput->typeDesc->numComponents,
                                                (i + (l * numScalarOutputs)) %
                                                packedInput->typeDesc->numComponents,
                                                MAX_SHADER_LINE_CHARS,
                                                shaderTempText[3]);
                                if (l > 0) {
                                        strncat(shaderTempText[2], " * ",
                                                MAX_SHADER_LINE_CHARS - strlen(shaderTempText[2]));
                                }
                                strncat(shaderTempText[2], srcStr,
                                        MAX_SHADER_LINE_CHARS - strlen(shaderTempText[2]));
                                k++;
                                r--;
                        }
                        srcStr = shaderTempText[2];
                } else {
                        snprintf(shaderTempText[2], MAX_SHADER_LINE_CHARS,
                                 "%s", g_floatDesc.defaultValue);
                        srcStr = shaderTempText[2];
                }

                if (j < numScalarUniforms) {
                        if ((g_debugMask & DEBUG_INPUT) && outputDefaultSystemValue) {
                                snprintf(shaderTempText[0], MAX_SHADER_LINE_CHARS,
                                         "  %s = texel%s;\n", resultStr,
                                         g_vectorComponents[j %
                                                packedUniform->typeDesc->numComponents]);
                        } else {
                                snprintf(shaderTempText[0], MAX_SHADER_LINE_CHARS,
                                         "  %s = texel%s * %s;\n", resultStr,
                                         g_vectorComponents[j %
                                                packedUniform->typeDesc->numComponents],
                                         srcStr);
                        }
                        strncat(shaderText, shaderTempText[0], shaderTextFree);
                        shaderTextFree -= strlen(shaderTempText[0]);
                        j++;
                } else {
                        snprintf(shaderTempText[0], MAX_SHADER_LINE_CHARS,
                                 "  %s = %s%s * %s;\n", resultStr,
                                 packedUniform->typeDesc->defaultValue,
                                 g_vectorComponents[0],
                                 srcStr);
                        strncat(shaderText, shaderTempText[0], shaderTextFree);
                        shaderTextFree -= strlen(shaderTempText[0]);
                }

                /* Increment to the next output. */
                i++;
        }

        /*
         * End the main program block.
         */
        if (outputDefaultSystemValue) {
                snprintf(shaderTempText[0], MAX_SHADER_LINE_CHARS,
                         "  %s = %s;\n",
                         outputDefaultSystemValue, defaultSystemValue);
                strncat(shaderText, shaderTempText[0], shaderTextFree);
                shaderTextFree -= strlen(shaderTempText[0]);
        }

        snprintf(shaderTempText[0], MAX_SHADER_LINE_CHARS, "}\n");
        strncat(shaderText, shaderTempText[0], shaderTextFree);
        shaderTextFree -= strlen(shaderTempText[0]);

        for (i = 0; i < sizeof(shaderTempText) / sizeof(char *); i++) {
                if (shaderTempText[i]) {
                        free(shaderTempText[i]);
                }
        }

        *pShaderText = shaderText;
        return true;
}


/**
 * Core piglit_display callback. This function will bind the FBOs, Textures,
 * VBOs, IBOs, and draw a primitive using the GLSL shader generated by
 * build_reduce_glsl_shader.
 *
 * Once rendered, a pixel from each of the color attachments is read back and
 * compared to the final expression value stored in g_expected given the
 * associated color attachment index.
 *
 * \return PIGLIT_PASS if the setup succeeded without error
 *         PIGLIT_FAIL otherwise.
 */

enum piglit_result
piglit_display(void)
{
        GLuint vao = 0;
        GLuint fbo = 0;
        GLuint *fboTextures = NULL;
        GLenum *colorBuffers = NULL;
        GLuint attribLoc;
        GLfloat result[4];
        int i = 0;

        /*
         * Reserve memory for the FBO texture objects.
         */
        fboTextures = calloc(g_maxColorAttachments, sizeof(GLuint));

        if (NULL == fboTextures) {
                fprintf(stderr, "Failed to create FBO texture object container.\n");
                goto fail;
        }

        /*
         * Build the color attachments
         */
        colorBuffers = calloc(g_maxColorAttachments, sizeof(GLenum));

        if (NULL == colorBuffers) {
                fprintf(stderr, "Failed to create draw buffers descriptor.\n");
                goto fail;
        }

        /*
         * Generate an FBO container to hold the color attachment hierarchy.
         */
        glGenFramebuffers(1, &fbo);
        glBindFramebuffer(GL_FRAMEBUFFER, fbo);

        for (i = 0; i < g_maxColorAttachments; i++) {
                setup_fbo_2d(i, GL_RGBA32F, GL_RGBA, GL_FLOAT,
                             BUFFER_WIDTH, BUFFER_HEIGHT,
                             fbo, &fboTextures[i]);
                colorBuffers[i] = GL_COLOR_ATTACHMENT0 + i;
        }

        /*
         * Build the textures sampled by the shaders
         */
        for (i = 0; i < g_maxCombinedTextureImageUnits; i++) {
                GLuint tex;
                GLuint uniformLoc;
                char strTemp[16];

                glGenTextures(1, &tex);
                glActiveTexture(GL_TEXTURE0 + i);
                glBindTexture(GL_TEXTURE_2D, tex);
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

                if (((i+1) * MAX_COMPONENTS) > sizeof(g_primes)) {
                        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, 1, 1, 0,
                                     GL_RGBA, GL_UNSIGNED_BYTE, NULL);
                } else {
                        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, 1, 1, 0,
                                     GL_RGBA, GL_FLOAT,
                                     &g_primes[i * MAX_COMPONENTS]);
                }

                if (!piglit_check_gl_error(GL_NO_ERROR)) {
                        fprintf(stderr, "Failed to create texture %u.\n", i);
                        goto fail;
                }

                snprintf(strTemp, sizeof(strTemp), "Texture[%u]", i);
                uniformLoc = glGetUniformLocation(g_program, strTemp);
                glUniform1i(uniformLoc, i);

                if (!piglit_check_gl_error(GL_NO_ERROR)) {
                        fprintf(stderr, "Unable to assign texture %u uniform.\n", i);
                        goto fail;
                }
        }

        /*
         * Setup the vertex and element buffers for drawing our points.
         */
        if (g_drawMode != DRAW_IMMEDIATE) {
                glGenVertexArrays(1, &vao);
                glBindVertexArray(vao);

                if (!piglit_check_gl_error(GL_NO_ERROR)) {
                        fprintf(stderr, "Unable to create VAO.\n");
                        goto fail;
                }

                setup_vertex_element_buffers();
        }

        /*
         * Setup the raster state.
         */
        glBindFramebuffer(GL_FRAMEBUFFER, fbo);
        glDrawBuffers(g_maxColorAttachments, colorBuffers);

        if (!piglit_check_gl_error(GL_NO_ERROR)) {
                fprintf(stderr, "Unable to assign draw buffers.\n");
                goto fail;
        }

        glClearColor(0.0, 1.0, 0.0, 0.0);
        glClear(GL_COLOR_BUFFER_BIT);

        /*
         * Bind the vertex array and enable each attribute.
         */
        if (g_drawMode != DRAW_IMMEDIATE) {
                glBindVertexArray(vao);
                attribLoc = glGetAttribLocation(g_program, "InPosition");
                glEnableVertexAttribArray(attribLoc);

                if (!piglit_check_gl_error(GL_NO_ERROR)) {
                        fprintf(stderr,
                                "Unable to enable vertex array attribute %u.\n",
                                attribLoc);
                        goto fail;
                }

                /*
                 * Enable the rest of the attributes.
                 */
                for (i = 0; i < g_maxVertexAttribs - 1; i++) {
                        char strTemp[16];

                        snprintf(strTemp, sizeof(strTemp), "InValue%u", i);
                        attribLoc = glGetAttribLocation(g_program, strTemp);
                        glEnableVertexAttribArray(attribLoc);

                        if (!piglit_check_gl_error(GL_NO_ERROR)) {
                                fprintf(stderr,
                                        "Unable to enable vertex array attribute %u.\n",
                                        attribLoc);
                                goto fail;
                        }
                }

                if (g_drawMode == DRAW_ARRAYS_VBO) {
                        if (g_debugMask & DEBUG_DRAW) {
                                fprintf(stderr, "Draw mode DRAW_ARRAYS_VBO\n");
                        }
                        glDrawArrays(GL_TRIANGLES, 0, NUM_VERTICES);
                }

                if (g_drawMode == DRAW_ELEMENTS_VBO) {
                        if (g_debugMask & DEBUG_DRAW) {
                                fprintf(stderr,
                                        "Draw mode DRAW_ELEMENTS_VBO\n");
                        }
                        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_elementVBO);
                        glDrawElements(GL_TRIANGLES, NUM_VERTICES,
                                       GL_UNSIGNED_SHORT, 0);
                        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
                }

                /*
                 * Blindly reset all the attributes.
                 */
                for (i = 0; i < g_maxVertexAttribs; i++) {
                        glDisableVertexAttribArray(i);
                }
        } else {
                glBegin(GL_TRIANGLES);
                glVertex3f(-1.0, -1.0, 0.0);
                glVertex3f(-1.0, 1.0, 0.0);
                glVertex3f(1.0, 1.0, 0.0);
                glEnd();
        }

        if (!piglit_check_gl_error(GL_NO_ERROR))
                goto fail;

        /*
         * Read back the FBO contents.
         */

        /*
         * Disable color clamping so we don't encounter result
         * collisions attempting to use a normalized color space.
         *
         * Requires OpenGL 3.0
         */
        glClampColor(GL_CLAMP_READ_COLOR, GL_FALSE);

        for (i = 0; i < g_maxColorAttachments; i++) {
                if (g_debugMask & DEBUG_READBACK) {
                        printf("GL_READ_FRAMEBUFFER <- fbo=%u\n", fbo);
                }

                glBindFramebuffer(GL_READ_FRAMEBUFFER, fbo);

                if (!piglit_check_gl_error(GL_NO_ERROR))
                        goto fail;

                glFramebufferTexture2D(GL_READ_FRAMEBUFFER,
                                       GL_COLOR_ATTACHMENT0,
                                       GL_TEXTURE_2D,
                                       fboTextures[i],
                                       0);

                if (!piglit_check_gl_error(GL_NO_ERROR))
                        goto fail;

                glPixelStorei(GL_PACK_ALIGNMENT, 1);
                glReadBuffer(GL_COLOR_ATTACHMENT0);

                if (!piglit_check_gl_error(GL_NO_ERROR))
                        goto fail;

                glReadPixels(0, BUFFER_HEIGHT - 1,
                             1, 1, GL_RGBA, GL_FLOAT, result);

                if ((g_expected[(i * MAX_COMPONENTS) + 0] != result[0]) ||
                    (g_expected[(i * MAX_COMPONENTS) + 1] != result[1]) ||
                    (g_expected[(i * MAX_COMPONENTS) + 2] != result[2]) ||
                    (g_expected[(i * MAX_COMPONENTS) + 3] != result[3])) {
                        fprintf(stderr, "GL_COLOR_ATTACHMENT%u: expected "
                                        "(%f, %f, %f, %f) != (%f, %f, %f, %f)\n",
                                i, g_expected[(i * MAX_COMPONENTS) + 0],
                                g_expected[(i * MAX_COMPONENTS) + 1],
                                g_expected[(i * MAX_COMPONENTS) + 2],
                                g_expected[(i * MAX_COMPONENTS) + 3],
                                result[0], result[1], result[2], result[3]);

                        goto fail;
                }
        }

        piglit_present_results();

        /*
         * Cleanup the allocations.
         */
        free(colorBuffers);
        free(fboTextures);

        piglit_report_result(PIGLIT_PASS);

        return PIGLIT_PASS;

fail:

        if (colorBuffers)
                free(colorBuffers);

        if (fboTextures)
                free(fboTextures);

        piglit_report_result(PIGLIT_FAIL);

        return PIGLIT_FAIL;
}


/**
 * Core piglit_init callback. This function will query the implementation for
 * the maximum number of supported resources. We attempt to create a GLSL
 * shader which references all supported resources bindable within the GL 3.0
 * specification.
 *
 * Each resource will be populated with a unique prime value. This function is
 * also responsible for calculating the expected value and storing this in
 * the g_expected array.
 *
 * Note: If the number of primes is insufficient to assign a unique prime to
 *       each resource, we will exit with PIGLIT_SKIP.
 *
 * Results: PIGLIT_PASS if the setup succeeded without error
 *          PIGLIT_FAIL otherwise.
 */

void
piglit_init(int argc, char **argv)
{
        GLfloat *var;
        PackedDesc vsInput = { "in", "InValue", false, 0, &g_vec4Desc };
        PackedDesc vsUniform = { "uniform", "Texture", true, 0, &g_sampler2DDesc };
        PackedDesc vsOutput = { "out", "Variable", true, 0, &g_floatDesc };
        PackedDesc fsInput = { "in", "Variable", true, 0, &g_floatDesc };
        PackedDesc fsUniform = { "uniform", "Texture", true, 0, &g_sampler2DDesc };
        PackedDesc fsOutput = { NULL, g_fragData, true, 0, &g_rgbaDesc };
        char shaderTempText[MAX_SHADER_LINE_CHARS];
        char *vertexShaderText = (char *)g_vertexShaderText;
        char *fragmentShaderText = (char *)g_fragmentShaderText;
        int i = 0;

        piglit_require_gl_version(30);

        for (i = 1; i < argc; i++) {
                if (strcmp(argv[i], "-drawArraysVBO") == 0) {
                        g_drawMode = DRAW_ARRAYS_VBO;
                }
                if (strcmp(argv[i], "-drawElementsVBO") == 0) {
                        g_drawMode = DRAW_ELEMENTS_VBO;
                }
                if (strcmp(argv[i], "-drawImmediate") == 0) {
                        g_drawMode = DRAW_IMMEDIATE;
                }
                if (strcmp(argv[i], "-debugInput") == 0) {
                        g_debugMask |= DEBUG_INPUT;
                }
                if (strcmp(argv[i], "-debugReadback") == 0) {
                        g_debugMask |= DEBUG_READBACK;
                }
                if (strcmp(argv[i], "-debugShaders") == 0) {
                        g_debugMask |= DEBUG_SHADERS;
                }
                if (strcmp(argv[i], "-debugDraw") == 0) {
                        g_debugMask |= DEBUG_DRAW;
                }
                if (strcmp(argv[i], "-dontClampMaxVaryings") == 0) {
                        g_debugMask |= DEBUG_DONT_CLAMP_MAX_VARYINGS;
                }
        }

        /*
         * Query the sampler capabilities.
         */
        glGetIntegerv(GL_MAX_VARYING_FLOATS, &g_maxVaryingFloats);
        glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &g_maxVertexAttribs);
        glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS,
                      &g_maxVertexTextureImageUnits);
        glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS,
                      &g_maxTextureImageUnits);
        glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS,
                      &g_maxCombinedTextureImageUnits);

        printf("GL_MAX_VARYING_FLOATS: %d\n", g_maxVaryingFloats);
        printf("GL_MAX_VERTEX_ATTRIBS: %d\n", g_maxVertexAttribs);
        printf("GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: %d\n",
               g_maxVertexTextureImageUnits);
        printf("GL_MAX_TEXTURE_IMAGE_UNITS: %d\n",
               g_maxTextureImageUnits);
        printf("GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: %d\n",
               g_maxCombinedTextureImageUnits);

        /*
         * Query the render target capabilities.
         */
        glGetIntegerv(GL_AUX_BUFFERS, &g_maxAuxBuffers);
        glGetIntegerv(GL_MAX_DRAW_BUFFERS, &g_maxDrawBuffers);
        glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, &g_maxColorAttachments);

        if (g_maxColorAttachments == 1) {
                fsOutput.variableName = g_fragColor;
        }

        printf("GL_AUX_BUFFERS: %d\n", g_maxAuxBuffers);
        printf("GL_MAX_DRAW_BUFFERS: %d\n", g_maxDrawBuffers);
        printf("GL_MAX_COLOR_ATTACHMENTS: %d\n", g_maxColorAttachments);

        if ((g_maxColorAttachments * MAX_COMPONENTS > NUM_PRIMES) ||
            (g_maxVaryingFloats > NUM_PRIMES) ||
            (g_maxVertexAttribs * MAX_COMPONENTS > NUM_PRIMES) ||
            (g_maxVertexTextureImageUnits * MAX_COMPONENTS > NUM_PRIMES) ||
            (g_maxTextureImageUnits * MAX_COMPONENTS > NUM_PRIMES)) {
                fprintf(stderr, "Unable to uniquely represent a result path.\n");
                piglit_report_result(PIGLIT_SKIP);
        }

        /*
         * Clamp the max varyings by default to work around large array
         * issues with some GLSL implementations.
         */
        if ((g_debugMask & DEBUG_DONT_CLAMP_MAX_VARYINGS) == 0) {
                g_maxVaryingFloats = MIN2(g_maxVaryingFloats, 32);
                fprintf(stderr, "Clamped max varying floats to %u.\n",
                g_maxVaryingFloats);
        }

        if (g_drawMode == DRAW_IMMEDIATE) {
                g_maxVertexAttribs = 1;
                fprintf(stderr,
                        "Immediate mode selected, using only one vertex attrib.\n");
        }

        /*
         * Build the shaders based upon the queried limits.
         */
        vsInput.count = g_maxVertexAttribs - 1;
        vsUniform.count = g_maxVertexTextureImageUnits;
        vsOutput.count = g_maxVaryingFloats;
        if (!build_reduce_glsl_shader(&vsInput,
                                      &vsUniform,
                                      &vsOutput,
                                      "gl_Position",
                                      "in vec4 InPosition;",
                                      "InPosition",
                                      &vertexShaderText)) {
                fprintf(stderr, "Failed to build GLSL vertex shader\n");
                piglit_report_result(PIGLIT_SKIP);
        }

        if (g_debugMask & DEBUG_SHADERS) {
                printf("vertexShaderText:\n%s", vertexShaderText);
        }

        fsInput.count = g_maxVaryingFloats;
        fsUniform.count = g_maxTextureImageUnits;
        fsOutput.count = g_maxColorAttachments;
        if (!build_reduce_glsl_shader(&fsInput,
                                      &fsUniform,
                                      &fsOutput,
                                      NULL,
                                      NULL,
                                      NULL,
                                      &fragmentShaderText)) {
                fprintf(stderr, "Failed to build GLSL vertex shader\n");
                piglit_report_result(PIGLIT_SKIP);
        }

        if (g_debugMask & DEBUG_SHADERS) {
                printf("fragmentShaderText:\n%s", fragmentShaderText);
        }

        /*
         * Build the vertex and fragment shaders.
         */
        g_program = piglit_build_simple_program(vertexShaderText,
                                                fragmentShaderText);
        if (!g_program) {
                fprintf(stderr, "Failed to compile/link program\n");
                piglit_report_result(PIGLIT_SKIP);
        }

        snprintf(shaderTempText, sizeof(shaderTempText), "InPosition");
        glBindAttribLocation(g_program, 0, shaderTempText);

        for (i = 0; i < g_maxVertexAttribs - 1; i++) {
                snprintf(shaderTempText, sizeof(shaderTempText), "InValue%u", i);
                glBindAttribLocation(g_program, i+1, shaderTempText);
        }

        if (g_debugMask & DEBUG_SHADERS) {
                printf("Linking program...\n");
        }

        glLinkProgram(g_program);
        glUseProgram(g_program);

        if (!piglit_check_gl_error(GL_NO_ERROR)) {
                fprintf(stderr, "Failure to link shaders\n");
                fprintf(stderr, "vertexShaderText:\n%s", vertexShaderText);
                fprintf(stderr, "fragmentShaderText:\n%s", fragmentShaderText);
                free(vertexShaderText);
                free(fragmentShaderText);
                piglit_report_result(PIGLIT_FAIL);
        }

        if (g_debugMask & DEBUG_SHADERS) {
                printf("Using program %u...\n", g_program);
        }

        /* Delete the interim copies generated by build_reduce_glsl_shader */ 
        free(vertexShaderText);
        free(fragmentShaderText);

        /*
         * Calculate the expected results for the bound resource limits.
         */
        var = calloc(g_maxVaryingFloats, sizeof(GLfloat));

        if (NULL == var) {
                fprintf(stderr,
                        "Failed to allocate temporary array for expected calculation.\n");
                piglit_report_result(PIGLIT_FAIL);
        }

        g_expected = calloc(g_maxColorAttachments, sizeof(GLfloat)*MAX_COMPONENTS);

        if (NULL == g_expected) {
                fprintf(stderr, "Failed to allocate array for expected calculation.\n");
                piglit_report_result(PIGLIT_FAIL);
        }

        /*
         * Sampler coverage up to the total number of varying floats.
         */
        for (i = 0; i < g_maxVaryingFloats; i++) {
                if ((i < g_maxVertexTextureImageUnits*MAX_COMPONENTS) &&
                    (i < NUM_PRIMES)) {
                        var[i] = g_primes[i];
                } else {
                        var[i] = 1.0;
                }
        }

        /*
         * Multiply in all vertex attributes.
         */
        i = 0;
        while (i < (g_maxVertexAttribs-1)*MAX_COMPONENTS) {
                if (i < NUM_PRIMES) {
                        var[i % g_maxVaryingFloats] *= g_primes[i];
                } else {
                        var[i % g_maxVaryingFloats] *= 1.0;
                }
                i++;
        }

        /*
         * Calculate the expected values for the FS stage.
         *
         * Sampler cover up to the total number of varying floats.
         */
        for (i = 0; i < g_maxColorAttachments*MAX_COMPONENTS; i++) {
                if ((i < g_maxVertexTextureImageUnits*MAX_COMPONENTS) &&
                    (i < NUM_PRIMES)) {
                        g_expected[i] = g_primes[i];
                } else {
                        g_expected[i] = 1.0;
                }
        }

        /*
         * Multiply in all the varying contributions generated by the VS.
         */
        i = 0;
        while (i < g_maxVaryingFloats) {
                g_expected[i % (g_maxColorAttachments*MAX_COMPONENTS)] *= var[i];
                i++;
        }

        for (i = 0; i < g_maxColorAttachments; i++) {
                printf("g_expected[%u]=(%f, %f, %f, %f)\n", i,
                       g_expected[(i * MAX_COMPONENTS) + 0],
                       g_expected[(i * MAX_COMPONENTS) + 1],
                       g_expected[(i * MAX_COMPONENTS) + 2],
                       g_expected[(i * MAX_COMPONENTS) + 3]);
        }

        free(var);
}