ext_gpu_shader4: add compiler tests for everything
[piglit.git] / tests / spec / arb_draw_elements_base_vertex / drawelements-instanced.c
blob5d134b7929265dda665e5da8cf9b5a12504855ab
1 /*
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
13 * Software.
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.
23 * Authors:
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
31 * index buffers.
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
49 #define NUM_QUADS 10
51 static const char *VertShaderText =
52 "#extension GL_ARB_draw_instanced : enable\n"
53 "attribute float yOffsetPerInstance; \n"
54 "void main() \n"
55 "{\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"
60 "}\n";
62 static const char *FragShaderText =
63 "void main() \n"
64 "{ \n"
65 " gl_FragColor = gl_Color; \n"
66 "}\n";
68 static GLuint Program;
70 static uintptr_t ib_offset;
72 void
73 piglit_init(int argc, char **argv)
75 GLfloat *vb;
76 GLuint *ib;
77 GLuint vbo;
78 GLint OffsetAttrib;
79 GLboolean user_va = GL_FALSE;
80 int i;
82 for (i = 1; i < argc; i++) {
83 if (!strcmp(argv[i], "user_varrays")) {
84 user_va = GL_TRUE;
85 puts("Testing user vertex arrays.");
89 piglit_require_GLSL();
90 if (!user_va)
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);
96 glPushMatrix();
97 glLoadIdentity();
99 if (!user_va) {
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);
107 } else {
108 vb = malloc(NUM_QUADS * 8 * sizeof(GLfloat) +
109 2 * 4 * sizeof(GLuint));
112 for (i = 0; i < NUM_QUADS; i++) {
113 float x1 = 10;
114 float y1 = 10 + i * 20;
115 float x2 = 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++)
126 ib[i] = i;
128 if (user_va) {
129 ib_offset = (uintptr_t)ib;
130 } else {
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);
146 enum piglit_result
147 piglit_display(void)
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};
153 int i, j;
155 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
157 glColor3fv(white);
158 /* Draw columns with each successive pair of the quads. */
159 for (i = 0; i < NUM_QUADS - 1; i++) {
160 glMatrixMode(GL_PROJECTION);
161 glPushMatrix();
162 glLoadIdentity();
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);
169 glPopMatrix();
172 for (i = 0; i < NUM_QUADS - 1; i++) {
173 for (j = 0; j < NUM_QUADS; j++) {
174 float *expected;
176 int x = 15 + i * 20;
177 int y = 15 + j * 20;
179 if (j == i)
180 expected = white;
181 else if (j == i + 1)
182 expected = blue;
183 else
184 expected = clear;
185 pass = piglit_probe_pixel_rgb(x, y, expected) && pass;
189 piglit_present_results();
191 return pass ? PIGLIT_PASS : PIGLIT_FAIL;