aboutsummaryrefslogtreecommitdiff
path: root/tests/general/pos-array.c
blob: b1a91fb366c51ec256257906e0fc01a35e357847 (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
/*
 * Copyright (c) 2011 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
 * on the rights to use, copy, modify, merge, publish, distribute, sub
 * license, 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
 * NON-INFRINGEMENT.  IN NO EVENT SHALL VMWARE AND/OR THEIR SUPPLIERS
 * 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.
 */

/**
 * Tests rendering with vertex arrays when neither GL_VERTEX_ARRAY nor generic
 * attribute 0 array is enabled.  Test legacy/fixed-function, GLSL and ARB_vp.
 *
 * Brian Paul
 * May 2011
 */

#include "piglit-util-gl-common.h"

PIGLIT_GL_TEST_MAIN(
    100 /*window_width*/,
    100 /*window_height*/,
    PIGLIT_GL_VISUAL_RGB | PIGLIT_GL_VISUAL_ALPHA | PIGLIT_GL_VISUAL_DOUBLE)

static const char *TestName = "pos-array";

static const GLfloat VertexData[] = {
   /* positions */
   -1, -1,
   1, -1,
   1, 1,
   -1, 1,
   /* colors */
   1, 0, 0,
   1, 0, 0,
   0, 0, 1,
   0, 0, 1
};


static GLuint
setup_vbo(void)
{
   GLuint buf;

   glGenBuffersARB(1, &buf);
   glBindBufferARB(GL_ARRAY_BUFFER_ARB, buf);
   glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(VertexData), VertexData,
                   GL_DYNAMIC_DRAW_ARB);
   return buf;
}


/** Test legacy/fixed-function vertex array drawing */
static GLboolean
test_fixedfunc_arrays(void)
{
   static const GLfloat expected[4] = {0.5, 0.0, 0.5, 1.0};
   static const GLfloat black[4] = {0.0, 0.0, 0.0, 0.0};
   GLuint buf;
   GLboolean p,pass = GL_TRUE;

   buf = setup_vbo();
   glBindBufferARB(GL_ARRAY_BUFFER_ARB, buf);

   /*
    * Draw with conventional arrays.
    */
   {
      glVertexPointer(2, GL_FLOAT, 2 * sizeof(GLfloat), (void *) 0);
      glColorPointer(3, GL_FLOAT, 3 * sizeof(GLfloat),
                     (void *) (8 * sizeof(GLfloat)));
      glEnableClientState(GL_VERTEX_ARRAY);
      glEnableClientState(GL_COLOR_ARRAY);

      glClear(GL_COLOR_BUFFER_BIT);
      glDrawArrays(GL_QUADS, 0, 4);

      p = piglit_probe_pixel_rgba(piglit_width/2, piglit_height/2, expected);
      piglit_present_results();
      if (!p) {
         printf("%s: failed when drawing with ", TestName);
         printf("conventional vertex arrays\n");
         pass = GL_FALSE;
      }

      glDisableClientState(GL_VERTEX_ARRAY);
      glDisableClientState(GL_COLOR_ARRAY);
   }

   /*
    * Draw with generic array[0]=position
    * XXX this should only work when the driver aliases conventional
    * vertex attributes with the generic attributes (as w/ NVIDIA).
    * XXX check for that and enable this code some day.
    */
   if (0) {
      GLuint attrib = 0;
      glVertexAttribPointerARB(attrib, 2, GL_FLOAT, GL_FALSE,
                               2 * sizeof(GLfloat), (void *) 0);
      glColorPointer(3, GL_FLOAT, 3 * sizeof(GLfloat),
                     (void *) (8 * sizeof(GLfloat)));
      glEnableVertexAttribArrayARB(attrib);
      glEnableClientState(GL_COLOR_ARRAY);

      glClear(GL_COLOR_BUFFER_BIT);
      glDrawArrays(GL_QUADS, 0, 4);

      p = piglit_probe_pixel_rgba(piglit_width/2, piglit_height/2, expected);
      piglit_present_results();
      if (!p) {
         printf("%s: failed when drawing with ", TestName);
         printf("generic array [%u]\n", attrib);
         pass = GL_FALSE;
      }

      glDisableVertexAttribArrayARB(attrib);
      glDisableClientState(GL_COLOR_ARRAY);
   }

   /*
    * Draw without GL_VERTEX or GENERIC[0] array set (should NOT draw)
    */
   {
      GLuint attrib = 3;
      glVertexAttribPointerARB(attrib, 2, GL_FLOAT, GL_FALSE,
                               2 * sizeof(GLfloat), (void *) 0);
      glColorPointer(3, GL_FLOAT, 3 * sizeof(GLfloat),
                     (void *) (8 * sizeof(GLfloat)));
      glEnableVertexAttribArrayARB(attrib);
      glEnableClientState(GL_COLOR_ARRAY);

      glClear(GL_COLOR_BUFFER_BIT);
      glDrawArrays(GL_QUADS, 0, 4);

      p = piglit_probe_pixel_rgba(piglit_width/2, piglit_height/2, black);
      piglit_present_results();
      if (!p) {
         printf("%s: failed when drawing with ", TestName);
         printf("generic array [%u]\n", attrib);
         pass = GL_FALSE;
      }

      glDisableVertexAttribArrayARB(attrib);
      glDisableClientState(GL_COLOR_ARRAY);
   }

   piglit_present_results();

   glDeleteBuffersARB(1, &buf);

   return pass;
}


/** Test drawing with GLSL shaders */
static GLboolean
test_glsl_arrays(void)
{
   static const char *vertShaderText =
      "attribute vec4 color, pos; \n"
      "varying vec4 colorVar; \n"
      "void main() \n"
      "{ \n"
      "   colorVar = color; \n"
      "   gl_Position = gl_ModelViewProjectionMatrix * pos; \n"
      "} \n";

   static const char *fragShaderText =
      "varying vec4 colorVar; \n"
      "void main() \n"
      "{ \n"
      "   gl_FragColor = colorVar; \n"
      "} \n";

   static const GLfloat expected[4] = {0.5, 0.0, 0.5, 1.0};
   GLuint buf;
   GLboolean p, pass = GL_TRUE;
   GLint posAttrib, colorAttrib;
   GLuint vertShader, fragShader, program;

   buf = setup_vbo();
   glBindBufferARB(GL_ARRAY_BUFFER_ARB, buf);

   vertShader = piglit_compile_shader_text(GL_VERTEX_SHADER, vertShaderText);
   fragShader = piglit_compile_shader_text(GL_FRAGMENT_SHADER, fragShaderText);
   program = piglit_link_simple_program(vertShader, fragShader);

   piglit_UseProgram(program);

   /*
    * Draw with compiler-assigned attribute locations
    */
   {
      posAttrib = piglit_GetAttribLocation(program, "pos");
      colorAttrib = piglit_GetAttribLocation(program, "color");

      if (0)
         printf("%s: GLSL posAttrib = %d  colorAttrib = %d\n",
                TestName, posAttrib, colorAttrib);

      glVertexAttribPointerARB(posAttrib, 2, GL_FLOAT, GL_FALSE,
                               2 * sizeof(GLfloat), (void *) 0);
      glVertexAttribPointerARB(colorAttrib, 3, GL_FLOAT, GL_FALSE,
                               3 * sizeof(GLfloat),
                               (void *) (8 * sizeof(GLfloat)));
      glEnableVertexAttribArrayARB(posAttrib);
      glEnableVertexAttribArrayARB(colorAttrib);

      glClear(GL_COLOR_BUFFER_BIT);
      glDrawArrays(GL_QUADS, 0, 4);

      p = piglit_probe_pixel_rgba(piglit_width/2, piglit_height/2, expected);
      piglit_present_results();
      if (!p) {
         printf("%s: failed when drawing with ", TestName);
         printf("compiler-assigned attribute locations\n");
         pass = GL_FALSE;
      }

      glDisableVertexAttribArrayARB(posAttrib);
      glDisableVertexAttribArrayARB(colorAttrib);
   }

   /*
    * Draw with user-defined attribute bindings, not using 0.
    */
   {
      posAttrib = 5;
      colorAttrib = 7;

      piglit_BindAttribLocation(program, posAttrib, "pos");
      piglit_BindAttribLocation(program, colorAttrib, "color");

      piglit_LinkProgram(program);

      glVertexAttribPointerARB(posAttrib, 2, GL_FLOAT, GL_FALSE,
                               2 * sizeof(GLfloat), (void *) 0);
      glVertexAttribPointerARB(colorAttrib, 3, GL_FLOAT, GL_FALSE,
                               3 * sizeof(GLfloat),
                               (void *) (8 * sizeof(GLfloat)));
      glEnableVertexAttribArrayARB(posAttrib);
      glEnableVertexAttribArrayARB(colorAttrib);

      glClear(GL_COLOR_BUFFER_BIT);
      glDrawArrays(GL_QUADS, 0, 4);

      p = piglit_probe_pixel_rgba(piglit_width/2, piglit_height/2, expected);
      piglit_present_results();
      if (!p) {
         printf("%s: failed when drawing with ", TestName);
         printf("user-assigned attribute locations\n");
         pass = GL_FALSE;
      }

      glDisableVertexAttribArrayARB(posAttrib);
      glDisableVertexAttribArrayARB(colorAttrib);
   }

   piglit_DeleteShader(vertShader);
   piglit_DeleteProgram(program);
   glDeleteBuffersARB(1, &buf);

   return pass;
}


/**
 * Test drawing with GLSL shaders and no vertex arrays.
 * Use a vertex shader with a hard-coded vertex position.
 */
static GLboolean
test_glsl_no_arrays(void)
{
   static const char *noVertexVertShaderText =
      "varying vec4 colorVar; \n"
      "void main() \n"
      "{ \n"
      "   colorVar = vec4(1.0, 1.0, 0.0, 1.0); \n"
      "   gl_Position = vec4(0.0, 0.0, 0.0, 1.0); \n"
      "} \n";

   static const char *fragShaderText =
      "varying vec4 colorVar; \n"
      "void main() \n"
      "{ \n"
      "   gl_FragColor = colorVar; \n"
      "} \n";

   static const GLfloat expected[4] = {1.0, 1.0, 0.0, 1.0};
   GLboolean p, pass = GL_TRUE;
   GLuint vertShader, fragShader, program;

   vertShader = piglit_compile_shader_text(GL_VERTEX_SHADER,
                                           noVertexVertShaderText);
   fragShader = piglit_compile_shader_text(GL_FRAGMENT_SHADER, fragShaderText);
   program = piglit_link_simple_program(vertShader, fragShader);

   piglit_UseProgram(program);

   glClear(GL_COLOR_BUFFER_BIT);
   glPointSize(3.0);
   glDrawArrays(GL_POINTS, 0, 1);
   glPointSize(1.0);

   p = piglit_probe_pixel_rgba(piglit_width/2, piglit_height/2, expected);
   piglit_present_results();
   if (!p) {
      printf("%s: failed when drawing with GLSL and no vertex arrays\n",
             TestName);
      pass = GL_FALSE;
   }

   piglit_DeleteShader(vertShader);
   piglit_DeleteProgram(program);

   return pass;
}


/** Test drawing with GL_ARB_vertex_program */
static GLboolean
test_arbvp_arrays(void)
{
   /* Use legacy vertex array/attribute */
   static const char *legacyVertProgramText =
      "!!ARBvp1.0 \n"
      "ATTRIB iPos = vertex.position;\n"
      "OUTPUT oPos = result.position;\n"
      "PARAM mvp[4] = { state.matrix.mvp };\n"
      "DP4 oPos.x, mvp[0], iPos;\n"
      "DP4 oPos.y, mvp[1], iPos;\n"
      "DP4 oPos.z, mvp[2], iPos;\n"
      "DP4 oPos.w, mvp[3], iPos;\n"
      "MOV result.color, vertex.color;\n"
      "END";

   /* Use generic vertex array/attribute[0] */
   static const char *generic0VertProgramText =
      "!!ARBvp1.0 \n"
      "ATTRIB iPos = vertex.attrib[0];\n"
      "OUTPUT oPos = result.position;\n"
      "PARAM mvp[4] = { state.matrix.mvp };\n"
      "DP4 oPos.x, mvp[0], iPos;\n"
      "DP4 oPos.y, mvp[1], iPos;\n"
      "DP4 oPos.z, mvp[2], iPos;\n"
      "DP4 oPos.w, mvp[3], iPos;\n"
      "MOV result.color, vertex.color;\n"
      "END";

   /* Use generic vertex array/attribute[6] */
   static const char *generic6VertProgramText =
      "!!ARBvp1.0 \n"
      "ATTRIB iPos = vertex.attrib[6];\n"
      "OUTPUT oPos = result.position;\n"
      "PARAM mvp[4] = { state.matrix.mvp };\n"
      "DP4 oPos.x, mvp[0], iPos;\n"
      "DP4 oPos.y, mvp[1], iPos;\n"
      "DP4 oPos.z, mvp[2], iPos;\n"
      "DP4 oPos.w, mvp[3], iPos;\n"
      "MOV result.color, vertex.color;\n"
      "END";

   static const char *fragProgramText =
      "!!ARBfp1.0 \n"
      "MOV result.color, fragment.color;\n"
      "END";

   static const GLfloat expected[4] = {0.5, 0.0, 0.5, 1.0};
   GLuint buf;
   GLboolean p, pass = GL_TRUE;
   GLuint vertProg, fragProg;

   buf = setup_vbo();
   glBindBufferARB(GL_ARRAY_BUFFER_ARB, buf);

   fragProg = piglit_compile_program(GL_FRAGMENT_PROGRAM_ARB, fragProgramText);
   glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, fragProg);

   glEnable(GL_FRAGMENT_PROGRAM_ARB);
   glEnable(GL_VERTEX_PROGRAM_ARB);

   /*
    * Draw with pos in conventional arrays.
    */
   {
      vertProg = piglit_compile_program(GL_VERTEX_PROGRAM_ARB,
                                        legacyVertProgramText);
      glBindProgramARB(GL_VERTEX_PROGRAM_ARB, vertProg);
      glVertexPointer(2, GL_FLOAT, 2 * sizeof(GLfloat), (void *) 0);
      glColorPointer(3, GL_FLOAT, 3 * sizeof(GLfloat),
                     (void *) (8 * sizeof(GLfloat)));
      glEnableClientState(GL_VERTEX_ARRAY);
      glEnableClientState(GL_COLOR_ARRAY);

      glClear(GL_COLOR_BUFFER_BIT);
      glDrawArrays(GL_QUADS, 0, 4);

      p = piglit_probe_pixel_rgba(piglit_width/2, piglit_height/2, expected);
      piglit_present_results();
      if (!p) {
         printf("%s: failed when drawing with ", TestName);
         printf("ARB VP and conventional vertex arrays\n");
         pass = GL_FALSE;
      }

      glDisableClientState(GL_VERTEX_ARRAY);
      glDisableClientState(GL_COLOR_ARRAY);
      glDeleteProgramsARB(1, &vertProg);
   }

   /*
    * Draw with pos in generic array[0].
    */
   {
      GLuint attrib = 0;
      vertProg = piglit_compile_program(GL_VERTEX_PROGRAM_ARB,
                                        generic0VertProgramText);
      glBindProgramARB(GL_VERTEX_PROGRAM_ARB, vertProg);
      glVertexAttribPointerARB(attrib, 2, GL_FLOAT, GL_FALSE,
                               2 * sizeof(GLfloat), (void *) 0);
      glColorPointer(3, GL_FLOAT, 3 * sizeof(GLfloat),
                     (void *) (8 * sizeof(GLfloat)));
      glEnableVertexAttribArrayARB(attrib);
      glEnableClientState(GL_COLOR_ARRAY);

      glClear(GL_COLOR_BUFFER_BIT);
      glDrawArrays(GL_QUADS, 0, 4);

      p = piglit_probe_pixel_rgba(piglit_width/2, piglit_height/2, expected);
      piglit_present_results();
      if (!p) {
         printf("%s: failed when drawing with ", TestName);
         printf("ARB VP and generic vertex array[%u]\n", attrib);
         pass = GL_FALSE;
      }

      glDisableVertexAttribArrayARB(attrib);
      glDisableClientState(GL_COLOR_ARRAY);
      glDeleteProgramsARB(1, &vertProg);
   }

   /*
    * Draw with pos in generic array[6].
    */
   {
      GLuint attrib = 6;
      vertProg = piglit_compile_program(GL_VERTEX_PROGRAM_ARB,
                                        generic6VertProgramText);
      glBindProgramARB(GL_VERTEX_PROGRAM_ARB, vertProg);
      glVertexAttribPointerARB(attrib, 2, GL_FLOAT, GL_FALSE,
                               2 * sizeof(GLfloat), (void *) 0);
      glColorPointer(3, GL_FLOAT, 3 * sizeof(GLfloat),
                     (void *) (8 * sizeof(GLfloat)));
      glEnableVertexAttribArrayARB(attrib);
      glEnableClientState(GL_COLOR_ARRAY);

      glClear(GL_COLOR_BUFFER_BIT);
      glDrawArrays(GL_QUADS, 0, 4);

      p = piglit_probe_pixel_rgba(piglit_width/2, piglit_height/2, expected);
      piglit_present_results();
      if (!p) {
         printf("%s: failed when drawing with ", TestName);
         printf("ARB VP and generic vertex array[%u]\n", attrib);
         pass = GL_FALSE;
      }

      glDisableVertexAttribArrayARB(attrib);
      glDisableClientState(GL_COLOR_ARRAY);
      glDeleteProgramsARB(1, &vertProg);
   }

   glDisable(GL_FRAGMENT_PROGRAM_ARB);
   glDisable(GL_VERTEX_PROGRAM_ARB);

   glDeleteProgramsARB(1, &fragProg);
   glDeleteBuffersARB(1, &buf);

   return pass;
}


enum piglit_result
piglit_display(void)
{
   GLboolean pass = GL_TRUE;

   pass &= test_fixedfunc_arrays();

   if (piglit_is_extension_supported("GL_ARB_vertex_program") &&
       piglit_is_extension_supported("GL_ARB_fragment_program")) {
      pass &= test_arbvp_arrays();
   }

   if (piglit_is_extension_supported("GL_ARB_shader_objects") &&
       piglit_is_extension_supported("GL_ARB_vertex_shader") &&
       piglit_is_extension_supported("GL_ARB_fragment_shader")) {
      pass &= test_glsl_arrays();
      pass &= test_glsl_no_arrays();
   }

   return pass ? PIGLIT_PASS : PIGLIT_FAIL;
}


void
piglit_init(int argc, char **argv)
{
   piglit_require_extension("GL_ARB_vertex_buffer_object");
}