2 * Copyright © 2009 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Pierre-Eric Pelloux-Prayer <pelloux@gmail.com>
27 /** @file draw-elements-instanced-base-vertex.c
28 * (Heavily based on draw-elements-base-vertex.c)
29 * Tests ARB_draw_elements_instanced_base_vertex functionality by drawing a series of
30 * pairs of quads using different base vertices, using the same vertex and
32 * Each pair of quads is drawn using 2 instances, and gl_InstanceID is used as a
33 * color modifier and an y offset.
36 #include "piglit-util-gl.h"
38 PIGLIT_GL_TEST_CONFIG_BEGIN
40 config
.supports_gl_compat_version
= 10;
42 config
.window_width
= 300;
43 config
.window_height
= 300;
44 config
.window_visual
= PIGLIT_GL_VISUAL_RGB
| PIGLIT_GL_VISUAL_DOUBLE
;
45 config
.khr_no_error_support
= PIGLIT_NO_ERRORS
;
47 PIGLIT_GL_TEST_CONFIG_END
51 static const char *VertShaderText
=
52 "#extension GL_ARB_draw_instanced : enable\n"
53 "attribute float yOffsetPerInstance; \n"
56 " vec4 p = gl_Vertex;\n"
57 " p.y += yOffsetPerInstance * float(gl_InstanceIDARB);\n"
58 " gl_Position = gl_ModelViewProjectionMatrix * p; \n"
59 " gl_FrontColor = vec4(1.0-float(gl_InstanceIDARB), 1.0, 1.0, 1.0); \n"
62 static const char *FragShaderText
=
65 " gl_FragColor = gl_Color; \n"
68 static GLuint Program
;
70 static uintptr_t ib_offset
;
73 piglit_init(int argc
, char **argv
)
79 GLboolean user_va
= GL_FALSE
;
82 for (i
= 1; i
< argc
; i
++) {
83 if (!strcmp(argv
[i
], "user_varrays")) {
85 puts("Testing user vertex arrays.");
89 piglit_require_GLSL();
91 piglit_require_extension("GL_ARB_vertex_buffer_object");
92 piglit_require_extension("GL_ARB_draw_instanced");
93 piglit_require_extension("GL_ARB_draw_elements_base_vertex");
95 glMatrixMode(GL_MODELVIEW
);
100 glGenBuffersARB(1, &vbo
);
101 glBindBufferARB(GL_ARRAY_BUFFER_ARB
, vbo
);
102 glBufferDataARB(GL_ARRAY_BUFFER_ARB
,
103 NUM_QUADS
* 8 * sizeof(GLfloat
) +
104 2 * 4 * sizeof(GLuint
),
105 NULL
, GL_DYNAMIC_DRAW
);
106 vb
= glMapBufferARB(GL_ARRAY_BUFFER_ARB
, GL_WRITE_ONLY_ARB
);
108 vb
= malloc(NUM_QUADS
* 8 * sizeof(GLfloat
) +
109 2 * 4 * sizeof(GLuint
));
112 for (i
= 0; i
< NUM_QUADS
; i
++) {
114 float y1
= 10 + i
* 20;
116 float y2
= 20 + i
* 20;
118 vb
[i
* 8 + 0] = x1
; vb
[i
* 8 + 1] = y1
;
119 vb
[i
* 8 + 2] = x2
; vb
[i
* 8 + 3] = y1
;
120 vb
[i
* 8 + 4] = x2
; vb
[i
* 8 + 5] = y2
;
121 vb
[i
* 8 + 6] = x1
; vb
[i
* 8 + 7] = y2
;
123 ib_offset
= NUM_QUADS
* 8 * sizeof(GLfloat
);
124 ib
= (GLuint
*)((char *)vb
+ ib_offset
);
125 for (i
= 0; i
< 8; i
++)
129 ib_offset
= (uintptr_t)ib
;
131 glUnmapBufferARB(GL_ARRAY_BUFFER_ARB
);
132 glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB
, vbo
);
135 glEnableClientState(GL_VERTEX_ARRAY
);
136 glVertexPointer(2, GL_FLOAT
, 0, user_va
? vb
: NULL
);
138 Program
= piglit_build_simple_program(VertShaderText
, FragShaderText
);
140 glUseProgram(Program
);
142 OffsetAttrib
= glGetAttribLocation(Program
, "yOffsetPerInstance");
143 glVertexAttrib1f(OffsetAttrib
, 20);
149 GLboolean pass
= GL_TRUE
;
150 float white
[3] = {1.0, 1.0, 1.0};
151 float blue
[3] = {0.0, 1.0, 1.0};
152 float clear
[3] = {0.0, 0.0, 0.0};
155 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
158 /* Draw columns with each successive pair of the quads. */
159 for (i
= 0; i
< NUM_QUADS
- 1; i
++) {
160 glMatrixMode(GL_PROJECTION
);
163 glOrtho(0, piglit_width
, 0, piglit_height
, -1, 1);
164 glTranslatef(i
* 20, 0, 0);
166 glDrawElementsInstancedBaseVertex(GL_QUADS
, 4, GL_UNSIGNED_INT
,
167 (void *)(uintptr_t)ib_offset
, 2, i
* 4);
172 for (i
= 0; i
< NUM_QUADS
- 1; i
++) {
173 for (j
= 0; j
< NUM_QUADS
; j
++) {
185 pass
= piglit_probe_pixel_rgb(x
, y
, expected
) && pass
;
189 piglit_present_results();
191 return pass
? PIGLIT_PASS
: PIGLIT_FAIL
;