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_vertex_shader();
90 piglit_require_fragment_shader();
92 piglit_require_extension("GL_ARB_vertex_buffer_object");
93 piglit_require_extension("GL_ARB_draw_instanced");
94 piglit_require_extension("GL_ARB_draw_elements_base_vertex");
96 glMatrixMode(GL_MODELVIEW
);
101 glGenBuffersARB(1, &vbo
);
102 glBindBufferARB(GL_ARRAY_BUFFER_ARB
, vbo
);
103 glBufferDataARB(GL_ARRAY_BUFFER_ARB
,
104 NUM_QUADS
* 8 * sizeof(GLfloat
) +
105 2 * 4 * sizeof(GLuint
),
106 NULL
, GL_DYNAMIC_DRAW
);
107 vb
= glMapBufferARB(GL_ARRAY_BUFFER_ARB
, GL_WRITE_ONLY_ARB
);
109 vb
= malloc(NUM_QUADS
* 8 * sizeof(GLfloat
) +
110 2 * 4 * sizeof(GLuint
));
113 for (i
= 0; i
< NUM_QUADS
; i
++) {
115 float y1
= 10 + i
* 20;
117 float y2
= 20 + i
* 20;
119 vb
[i
* 8 + 0] = x1
; vb
[i
* 8 + 1] = y1
;
120 vb
[i
* 8 + 2] = x2
; vb
[i
* 8 + 3] = y1
;
121 vb
[i
* 8 + 4] = x2
; vb
[i
* 8 + 5] = y2
;
122 vb
[i
* 8 + 6] = x1
; vb
[i
* 8 + 7] = y2
;
124 ib_offset
= NUM_QUADS
* 8 * sizeof(GLfloat
);
125 ib
= (GLuint
*)((char *)vb
+ ib_offset
);
126 for (i
= 0; i
< 8; i
++)
130 ib_offset
= (uintptr_t)ib
;
132 glUnmapBufferARB(GL_ARRAY_BUFFER_ARB
);
133 glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB
, vbo
);
136 glEnableClientState(GL_VERTEX_ARRAY
);
137 glVertexPointer(2, GL_FLOAT
, 0, user_va
? vb
: NULL
);
139 Program
= piglit_build_simple_program(VertShaderText
, FragShaderText
);
141 glUseProgram(Program
);
143 OffsetAttrib
= glGetAttribLocation(Program
, "yOffsetPerInstance");
144 glVertexAttrib1f(OffsetAttrib
, 20);
150 GLboolean pass
= GL_TRUE
;
151 float white
[3] = {1.0, 1.0, 1.0};
152 float blue
[3] = {0.0, 1.0, 1.0};
153 float clear
[3] = {0.0, 0.0, 0.0};
156 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
159 /* Draw columns with each successive pair of the quads. */
160 for (i
= 0; i
< NUM_QUADS
- 1; i
++) {
161 glMatrixMode(GL_PROJECTION
);
164 glOrtho(0, piglit_width
, 0, piglit_height
, -1, 1);
165 glTranslatef(i
* 20, 0, 0);
167 glDrawElementsInstancedBaseVertex(GL_QUADS
, 4, GL_UNSIGNED_INT
,
168 (void *)(uintptr_t)ib_offset
, 2, i
* 4);
173 for (i
= 0; i
< NUM_QUADS
- 1; i
++) {
174 for (j
= 0; j
< NUM_QUADS
; j
++) {
186 pass
= piglit_probe_pixel_rgb(x
, y
, expected
) && pass
;
190 piglit_present_results();
192 return pass
? PIGLIT_PASS
: PIGLIT_FAIL
;