ext_gpu_shader4: add compiler tests for everything
[piglit.git] / tests / spec / arb_shader_atomic_counters / common.c
blobc732699aeeaf2610db45bd2032d51e6ee9ddd863
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 common.c
26 * Common utility functions for the ARB_shader_atomic_counters tests.
29 #include "common.h"
31 bool
32 atomic_counters_probe_buffer(unsigned base, unsigned count,
33 const uint32_t *expected)
35 uint32_t *p = glMapBufferRange(
36 GL_ATOMIC_COUNTER_BUFFER, base * sizeof(uint32_t),
37 count * sizeof(uint32_t), GL_MAP_READ_BIT);
38 bool pass = true;
39 unsigned i;
41 if (!p) {
42 printf("Couldn't map atomic counter buffer for read-back.\n");
43 return false;
46 for (i = 0; i < count; ++i) {
47 uint32_t found = p[i];
48 if (found != expected[i]) {
49 printf("Probe value at (%i)\n", i);
50 printf(" Expected: 0x%08x\n", expected[i]);
51 printf(" Observed: 0x%08x\n", found);
52 pass = false;
53 break;
57 glUnmapBuffer(GL_ATOMIC_COUNTER_BUFFER);
58 return pass;
61 bool
62 atomic_counters_compile(GLuint prog, GLuint stage, const char *src)
64 GLuint shader = glCreateShader(stage);
65 int status, log_size;
66 char *log;
68 glShaderSource(shader, 1, (const GLchar **)&src, NULL);
69 glCompileShader(shader);
71 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
73 if (status) {
74 glAttachShader(prog, shader);
75 } else {
76 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &log_size);
77 log = malloc(log_size);
78 glGetShaderInfoLog(shader, log_size, NULL, log);
80 printf("Failed to compile shader: %s\n", log);
81 printf("source:\n%s", src);
83 free(log);
86 glDeleteShader(shader);
88 return status;
91 bool
92 atomic_counters_link(GLuint prog)
94 int status;
96 glGetProgramiv(prog, GL_LINK_STATUS, &status);
97 if (!status) {
98 glLinkProgram(prog);
99 glGetProgramiv(prog, GL_LINK_STATUS, &status);
102 return status;
105 bool
106 atomic_counters_draw_point(GLuint prog, unsigned buf_size,
107 const uint32_t *buf)
109 GLuint vao;
111 /* Initialize the atomic counter buffer. */
112 glBufferData(GL_ATOMIC_COUNTER_BUFFER,
113 buf_size * sizeof(uint32_t),
114 buf, GL_STATIC_DRAW);
116 /* Link and set the current shader program. */
117 atomic_counters_link(prog);
118 glUseProgram(prog);
120 /* Draw. */
121 glClearColor(0.5, 0.5, 0.5, 0.5);
122 glClear(GL_COLOR_BUFFER_BIT);
124 glGenVertexArrays(1, &vao);
125 glBindVertexArray(vao);
126 glVertexAttrib4f(0, 0, 0, 0, 1);
128 glDrawArrays(GL_POINTS, 0, 1);
130 glDeleteVertexArrays(1, &vao);
132 return piglit_check_gl_error(GL_NO_ERROR);
135 bool
136 atomic_counters_draw_rect(GLuint prog, unsigned buf_size, const uint32_t *buf)
138 /* Initialize the atomic counter buffer. */
139 glBufferData(GL_ATOMIC_COUNTER_BUFFER,
140 buf_size * sizeof(uint32_t),
141 buf, GL_STATIC_DRAW);
143 /* Set current shader program. */
144 glLinkProgram(prog);
145 glUseProgram(prog);
147 /* Draw. */
148 glClearColor(0.5, 0.5, 0.5, 0.5);
149 glClear(GL_COLOR_BUFFER_BIT);
151 piglit_draw_rect(-1, -1, 2, 2);
153 return piglit_check_gl_error(GL_NO_ERROR);
156 bool
157 atomic_counters_draw_patch(GLuint prog, unsigned buf_size,
158 const uint32_t *buf)
161 const GLfloat verts[3][4] = { { 0.0, 0.0, 0.0, 1.0 },
162 { 1.0, 0.0, 0.0, 1.0 },
163 { 0.0, 1.0, 0.0, 1.0 } };
164 GLuint vao, vbo;
166 /* Initialize the atomic counter buffer. */
167 glBufferData(GL_ATOMIC_COUNTER_BUFFER,
168 buf_size * sizeof(uint32_t),
169 buf, GL_STATIC_DRAW);
171 /* Set current shader program. */
172 glLinkProgram(prog);
173 glUseProgram(prog);
175 /* Initialize a vertex array object and a vertex buffer object. */
176 glGenVertexArrays(1, &vao);
177 glBindVertexArray(vao);
178 glGenBuffers(1, &vbo);
179 glBindBuffer(GL_ARRAY_BUFFER, vbo);
180 glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
182 /* Set up the current vertex attributes. */
183 glVertexAttribPointer(PIGLIT_ATTRIB_POS, 4, GL_FLOAT,
184 GL_FALSE, 0, 0);
185 glEnableVertexAttribArray(PIGLIT_ATTRIB_POS);
187 /* Draw. */
188 glClearColor(0.5, 0.5, 0.5, 0.5);
189 glClear(GL_COLOR_BUFFER_BIT);
191 glDrawArrays(GL_PATCHES, 0, 3);
193 /* Clean up. */
194 glDisableVertexAttribArray(PIGLIT_ATTRIB_POS);
195 glDeleteBuffers(1, &vbo);
196 glDeleteVertexArrays(1, &vao);
198 return piglit_check_gl_error(GL_NO_ERROR);
201 bool
202 atomic_counters_supported(GLenum shader_stage)
204 int n = 0;
206 switch (shader_stage) {
207 case GL_NONE:
208 case GL_FRAGMENT_SHADER:
209 return true;
211 case GL_VERTEX_SHADER:
212 glGetIntegerv(GL_MAX_VERTEX_ATOMIC_COUNTERS, &n);
213 return n;
215 case GL_GEOMETRY_SHADER:
216 if (piglit_get_gl_version() >= 32)
217 glGetIntegerv(GL_MAX_GEOMETRY_ATOMIC_COUNTERS, &n);
218 return n;
220 case GL_TESS_CONTROL_SHADER:
221 if (piglit_is_extension_supported("GL_ARB_tessellation_shader"))
222 glGetIntegerv(GL_MAX_TESS_CONTROL_ATOMIC_COUNTERS, &n);
223 return n;
225 case GL_TESS_EVALUATION_SHADER:
226 if (piglit_is_extension_supported("GL_ARB_tessellation_shader"))
227 glGetIntegerv(GL_MAX_TESS_EVALUATION_ATOMIC_COUNTERS,
228 &n);
229 return n;
231 default:
232 assert(!"Unreachable");
233 abort();
237 struct atomic_counters_limits
238 atomic_counters_get_limits()
240 struct atomic_counters_limits ls = { 0 };
242 piglit_require_extension("GL_ARB_shader_atomic_counters");
244 glGetIntegerv(GL_MAX_FRAGMENT_ATOMIC_COUNTERS,
245 &ls.fragment_counters);
246 glGetIntegerv(GL_MAX_VERTEX_ATOMIC_COUNTERS,
247 &ls.vertex_counters);
248 glGetIntegerv(GL_MAX_COMBINED_ATOMIC_COUNTERS,
249 &ls.combined_counters);
250 glGetIntegerv(GL_MAX_FRAGMENT_ATOMIC_COUNTER_BUFFERS,
251 &ls.fragment_buffers);
252 glGetIntegerv(GL_MAX_VERTEX_ATOMIC_COUNTER_BUFFERS,
253 &ls.vertex_buffers);
254 glGetIntegerv(GL_MAX_COMBINED_ATOMIC_COUNTER_BUFFERS,
255 &ls.combined_buffers);
256 glGetIntegerv(GL_MAX_ATOMIC_COUNTER_BUFFER_BINDINGS,
257 &ls.bindings);
258 glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_COMPONENTS,
259 &ls.uniform_components);
261 return ls;