1 /* Copyright © 2009 Intel Corporation
3 * Permission is hereby granted, free of charge, to any person obtaining a
4 * copy of this software and associated documentation files (the "Software"),
5 * to deal in the Software without restriction, including without limitation
6 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
7 * and/or sell copies of the Software, and to permit persons to whom the
8 * Software is furnished to do so, subject to the following conditions:
10 * The above copyright notice and this permission notice (including the next
11 * paragraph) shall be included in all copies or substantial portions of the
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 /* Tests OES_draw_elements_base_vertex functionality by drawing a checkerboard
25 * of quads using different base vertices using the same vertex and
26 * index buffers and instancing
29 #include "piglit-util-gl.h"
31 PIGLIT_GL_TEST_CONFIG_BEGIN
33 config
.supports_gl_es_version
= 30;
35 config
.window_width
= 300;
36 config
.window_height
= 300;
37 config
.window_visual
= PIGLIT_GL_VISUAL_RGB
| PIGLIT_GL_VISUAL_DOUBLE
;
39 PIGLIT_GL_TEST_CONFIG_END
42 const GLfloat inc_amount
= 2.0 / NUM_QUADS
;
43 const int window_width
= 300;
44 const int window_height
= 300;
46 const char *vs_source
= {
49 "in float xOffsetPerInstance;\n"
52 " p.y -= 1.0 * float(gl_InstanceID);\n"
53 " p.x += xOffsetPerInstance * float(gl_InstanceID);\n"
54 " gl_Position = vec4(p, 0, 1);\n"
58 const char *fs_source
= {
60 "out highp vec4 ocol;\n"
62 " ocol = vec4(1, 1, 1, 1);\n"
66 static GLushort indices
[] = {
69 static GLsizei indices_size
= sizeof(indices
);
72 static GLuint vertexBuffer
;
73 static GLuint indexBuffer
;
76 piglit_init(int argc
, char **argv
)
78 piglit_require_extension("GL_OES_draw_elements_base_vertex");
84 GLfloat
* vertices
= malloc(4 * 2 * NUM_QUADS
* sizeof(GLfloat
));
85 GLsizei vertices_size
= 4 * 2 * NUM_QUADS
* sizeof(GLfloat
);
87 // Generate a checkerboard pattern
91 for (i
= 0; i
< NUM_QUADS
; ++i
)
93 GLfloat xoffset
, yoffset
;
96 xoffset
= inc_amount
* i
- 1.0;
104 x
[1] = xoffset
+ inc_amount
/ 2.0;
109 y
[2] = yoffset
- 1.0;
112 x
[3] = xoffset
+ inc_amount
/ 2.0;
113 y
[3] = yoffset
- 1.0;
115 vertices
[i
* 8 + 0] = x
[0];
116 vertices
[i
* 8 + 1] = y
[0];
118 vertices
[i
* 8 + 2] = x
[1];
119 vertices
[i
* 8 + 3] = y
[1];
121 vertices
[i
* 8 + 4] = x
[2];
122 vertices
[i
* 8 + 5] = y
[2];
124 vertices
[i
* 8 + 6] = x
[3];
125 vertices
[i
* 8 + 7] = y
[3];
129 program
= piglit_build_simple_program(vs_source
, fs_source
);
130 glUseProgram(program
);
132 /* Gen vertex array buffer */
133 glGenBuffers(1, &vertexBuffer
);
134 glBindBuffer(GL_ARRAY_BUFFER
, vertexBuffer
);
135 glBufferData(GL_ARRAY_BUFFER
, vertices_size
, vertices
, GL_STATIC_DRAW
);
137 /* Gen indices array buffer */
138 glGenBuffers(1, &indexBuffer
);
139 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER
, indexBuffer
);
140 glBufferData(GL_ELEMENT_ARRAY_BUFFER
, indices_size
,
141 indices
, GL_STATIC_DRAW
);
144 glGenVertexArrays(1, &vao
);
145 glBindVertexArray(vao
);
147 xoffset_index
= glGetAttribLocation(program
, "xOffsetPerInstance");
148 glVertexAttrib1f(xoffset_index
, inc_amount
/ 2.0);
150 /* Retrieve indices from vs */
151 vertex_index
= glGetAttribLocation(program
, "vertex");
153 /* Enable vertex attrib array */
154 glBindBuffer(GL_ARRAY_BUFFER
, vertexBuffer
);
155 glEnableVertexAttribArray(vertex_index
);
156 glVertexAttribPointer(vertex_index
, 2, GL_FLOAT
, GL_FALSE
, 0, 0);
162 GLboolean pass
= GL_TRUE
;
163 float white
[] = {1.0, 1.0, 1.0, 1.0};
166 glClear(GL_COLOR_BUFFER_BIT
);
168 glBindVertexArray(vao
);
169 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER
, indexBuffer
);
171 for (i
= 0; i
< NUM_QUADS
; ++i
)
172 glDrawElementsInstancedBaseVertexOES(GL_TRIANGLES
, 6, GL_UNSIGNED_SHORT
, NULL
, 2, i
* 4);
174 for (i
= 0; i
< (NUM_QUADS
* 2); ++i
)
176 GLfloat xoffset
[2], yoffset
[2];
178 xoffset
[0] = inc_amount
* i
/ 4.0 * window_width
;
179 xoffset
[1] = inc_amount
* (i
+ 1) / 4.0 * window_width
;
180 yoffset
[0] = (i
% 2 ? 0.0 : 0.5) * window_height
;
181 yoffset
[1] = yoffset
[0] + window_height
/ 2;
183 pass
= piglit_probe_rect_rgba(xoffset
[0], yoffset
[0],
184 xoffset
[1] - xoffset
[0], yoffset
[1] - yoffset
[0], white
) && pass
;
187 piglit_present_results();
189 return pass
? PIGLIT_PASS
: PIGLIT_FAIL
;