glsl: test loop unroll with uint overflow
[piglit.git] / tests / spec / arb_draw_elements_base_vertex / drawelements-instanced.c
blob46d175e3d2ad98087ff4a22a025177b30d9eb392
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_vertex_shader();
90 piglit_require_fragment_shader();
91 if (!user_va)
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);
97 glPushMatrix();
98 glLoadIdentity();
100 if (!user_va) {
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);
108 } else {
109 vb = malloc(NUM_QUADS * 8 * sizeof(GLfloat) +
110 2 * 4 * sizeof(GLuint));
113 for (i = 0; i < NUM_QUADS; i++) {
114 float x1 = 10;
115 float y1 = 10 + i * 20;
116 float x2 = 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++)
127 ib[i] = i;
129 if (user_va) {
130 ib_offset = (uintptr_t)ib;
131 } else {
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);
147 enum piglit_result
148 piglit_display(void)
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};
154 int i, j;
156 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
158 glColor3fv(white);
159 /* Draw columns with each successive pair of the quads. */
160 for (i = 0; i < NUM_QUADS - 1; i++) {
161 glMatrixMode(GL_PROJECTION);
162 glPushMatrix();
163 glLoadIdentity();
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);
170 glPopMatrix();
173 for (i = 0; i < NUM_QUADS - 1; i++) {
174 for (j = 0; j < NUM_QUADS; j++) {
175 float *expected;
177 int x = 15 + i * 20;
178 int y = 15 + j * 20;
180 if (j == i)
181 expected = white;
182 else if (j == i + 1)
183 expected = blue;
184 else
185 expected = clear;
186 pass = piglit_probe_pixel_rgb(x, y, expected) && pass;
190 piglit_present_results();
192 return pass ? PIGLIT_PASS : PIGLIT_FAIL;