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
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
26 * Tests that concurrent execution of atomic operations on the same
27 * counter yield unique values for each vertex or fragment shader
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
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));
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
;
65 printf("Probe value at (%d, %d)\n", x
, y
);
66 printf(" Observed: 0x%08x\n", p
);
67 printf(" Value outside expected window.\n");
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");
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"
93 " fcolor.x = int(round(vcolor.x));\n"
95 const char *vs_template
= "#version 140\n"
96 "#extension GL_ARB_shader_atomic_counters : enable\n"
100 "layout(binding = 0, offset = 0) uniform atomic_uint x;\n"
101 "in vec4 piglit_vertex;\n"
102 "smooth out vec4 vcolor;\n"
106 " vcolor.x = float(y);\n"
107 " gl_Position = piglit_vertex;\n"
110 GLuint prog
= glCreateProgram();
113 ret
= asprintf(&vs_source
, vs_template
, op
);
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
);
122 glDeleteProgram(prog
);
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"
134 "layout(binding = 0, offset = 0) uniform atomic_uint x;\n"
135 "out ivec4 fcolor;\n"
138 " fcolor.x = int(OP(x));\n"
141 const char *vs_source
= "#version 140\n"
142 "in vec4 piglit_vertex;\n"
145 " gl_Position = piglit_vertex;\n"
147 GLuint prog
= glCreateProgram();
150 ret
= asprintf(&fs_source
, fs_template
, op
);
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
);
159 glDeleteProgram(prog
);
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",
189 "atomicCounterIncrement", 0, 4);
191 atomic_counters_subtest(&status
, GL_VERTEX_SHADER
,
192 "Vertex atomic decrement atomicity",
194 "atomicCounterDecrement", 4, 0);
196 atomic_counters_subtest(&status
, GL_VERTEX_SHADER
,
197 "Vertex atomic read atomicity",
199 "atomicCounter", 100, 101);
201 atomic_counters_subtest(&status
, GL_FRAGMENT_SHADER
,
202 "Fragment atomic increment atomicity",
204 "atomicCounterIncrement", 0, N
);
206 atomic_counters_subtest(&status
, GL_FRAGMENT_SHADER
,
207 "Fragment atomic decrement atomicity",
209 "atomicCounterDecrement", N
, 0);
211 atomic_counters_subtest(&status
, GL_FRAGMENT_SHADER
,
212 "Fragment atomic read atomicity",
214 "atomicCounter", 0, 1);
216 piglit_report_result(status
);