ext_gpu_shader4: add compiler tests for everything
[piglit.git] / tests / spec / arb_shader_atomic_counters / unique-id.c
blobdee11a511da17899e42f14bfb450c05690f711c4
1 /*
2 * Copyright © 2013 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 DEALINGS
21 * IN THE SOFTWARE.
24 /** @file unique-id.c
26 * Tests that concurrent execution of atomic operations on the same
27 * counter yield unique values for each vertex or fragment shader
28 * thread.
31 #include "common.h"
33 #define L 256
34 #define N (L * L)
36 PIGLIT_GL_TEST_CONFIG_BEGIN
38 config.supports_gl_core_version = 31;
40 config.window_width = 1;
41 config.window_height = 1;
42 config.window_visual = PIGLIT_GL_VISUAL_DOUBLE | PIGLIT_GL_VISUAL_RGBA;
43 config.khr_no_error_support = PIGLIT_NO_ERRORS;
45 PIGLIT_GL_TEST_CONFIG_END
47 static bool
48 check(int dx, int dy, uint32_t start_value, uint32_t end_value)
50 const uint32_t base = MIN2(start_value, end_value);
51 const uint32_t size = MAX2(start_value, end_value) - base;
52 uint32_t pixels[L][L];
53 uint32_t *frequency = malloc(size * sizeof(uint32_t));
54 int x, y;
56 memset(frequency, 0, size * sizeof(uint32_t));
57 glReadPixels(0, 0, L, L, GL_RED_INTEGER, GL_UNSIGNED_INT, pixels);
59 for (y = 0; y < L; y += dy) {
60 for (x = 0; x < L; x += dx) {
61 uint32_t p = pixels[y][x];
62 uint32_t v = p - base;
64 if (v >= size) {
65 printf("Probe value at (%d, %d)\n", x, y);
66 printf(" Observed: 0x%08x\n", p);
67 printf(" Value outside expected window.\n");
68 free(frequency);
69 return false;
72 if (size > 1 && frequency[v]++) {
73 printf("Probe value at (%d, %d)\n", x, y);
74 printf(" Observed: 0x%08x\n", p);
75 printf(" Value not unique.\n");
76 free(frequency);
77 return false;
82 free(frequency);
83 return true;
86 static bool
87 run_test_vertex(const char *op, uint32_t start_value, uint32_t end_value)
89 const char *fs_source = "#version 140\n"
90 "smooth in vec4 vcolor;\n"
91 "out ivec4 fcolor;\n"
92 "void main() {\n"
93 " fcolor.x = int(round(vcolor.x));\n"
94 "}\n";
95 const char *vs_template = "#version 140\n"
96 "#extension GL_ARB_shader_atomic_counters : enable\n"
97 "\n"
98 "#define OP %s\n"
99 "\n"
100 "layout(binding = 0, offset = 0) uniform atomic_uint x;\n"
101 "in vec4 piglit_vertex;\n"
102 "smooth out vec4 vcolor;\n"
103 "\n"
104 "void main() {\n"
105 " uint y = OP(x);"
106 " vcolor.x = float(y);\n"
107 " gl_Position = piglit_vertex;\n"
108 "}\n";
109 char *vs_source;
110 GLuint prog = glCreateProgram();
111 int ret;
113 ret = asprintf(&vs_source, vs_template, op);
114 assert(ret > 0);
116 ret = atomic_counters_compile(prog, GL_FRAGMENT_SHADER, fs_source) &&
117 atomic_counters_compile(prog, GL_VERTEX_SHADER, vs_source) &&
118 atomic_counters_draw_rect(prog, 1, &start_value) &&
119 check(L - 1, L - 1, start_value, end_value);
121 free(vs_source);
122 glDeleteProgram(prog);
123 return ret;
126 static bool
127 run_test_fragment(const char *op, uint32_t start_value, uint32_t end_value)
129 const char *fs_template = "#version 140\n"
130 "#extension GL_ARB_shader_atomic_counters : enable\n"
131 "\n"
132 "#define OP %s\n"
133 "\n"
134 "layout(binding = 0, offset = 0) uniform atomic_uint x;\n"
135 "out ivec4 fcolor;\n"
136 "\n"
137 "void main() {\n"
138 " fcolor.x = int(OP(x));\n"
139 "}\n";
140 char *fs_source;
141 const char *vs_source = "#version 140\n"
142 "in vec4 piglit_vertex;\n"
143 "\n"
144 "void main() {\n"
145 " gl_Position = piglit_vertex;\n"
146 "}\n";
147 GLuint prog = glCreateProgram();
148 int ret;
150 ret = asprintf(&fs_source, fs_template, op);
151 assert(ret > 0);
153 ret = atomic_counters_compile(prog, GL_FRAGMENT_SHADER, fs_source) &&
154 atomic_counters_compile(prog, GL_VERTEX_SHADER, vs_source) &&
155 atomic_counters_draw_rect(prog, 1, &start_value) &&
156 check(1, 1, start_value, end_value);
158 free(fs_source);
159 glDeleteProgram(prog);
160 return ret;
163 void
164 piglit_init(int argc, char **argv)
166 GLuint fb, rb, buffer;
167 enum piglit_result status = PIGLIT_PASS;
169 piglit_require_extension("GL_ARB_shader_atomic_counters");
171 glGenFramebuffers(1, &fb);
172 glGenRenderbuffers(1, &rb);
174 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fb);
175 glBindFramebuffer(GL_READ_FRAMEBUFFER, fb);
176 glBindRenderbuffer(GL_RENDERBUFFER, rb);
178 glViewport(0, 0, L, L);
179 glRenderbufferStorage(GL_RENDERBUFFER, GL_R32UI, L, L);
180 glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
181 GL_RENDERBUFFER, rb);
183 glGenBuffers(1, &buffer);
184 glBindBufferBase(GL_ATOMIC_COUNTER_BUFFER, 0, buffer);
186 atomic_counters_subtest(&status, GL_VERTEX_SHADER,
187 "Vertex atomic increment atomicity",
188 run_test_vertex,
189 "atomicCounterIncrement", 0, 4);
191 atomic_counters_subtest(&status, GL_VERTEX_SHADER,
192 "Vertex atomic decrement atomicity",
193 run_test_vertex,
194 "atomicCounterDecrement", 4, 0);
196 atomic_counters_subtest(&status, GL_VERTEX_SHADER,
197 "Vertex atomic read atomicity",
198 run_test_vertex,
199 "atomicCounter", 100, 101);
201 atomic_counters_subtest(&status, GL_FRAGMENT_SHADER,
202 "Fragment atomic increment atomicity",
203 run_test_fragment,
204 "atomicCounterIncrement", 0, N);
206 atomic_counters_subtest(&status, GL_FRAGMENT_SHADER,
207 "Fragment atomic decrement atomicity",
208 run_test_fragment,
209 "atomicCounterDecrement", N, 0);
211 atomic_counters_subtest(&status, GL_FRAGMENT_SHADER,
212 "Fragment atomic read atomicity",
213 run_test_fragment,
214 "atomicCounter", 0, 1);
216 piglit_report_result(status);
219 enum piglit_result
220 piglit_display(void)
222 /* UNREACHED */
223 return PIGLIT_FAIL;