glsl-1.30: add more loop unroll tests
[piglit.git] / tests / spec / arb_tessellation_shader / large-uniforms.c
blob4e7adf80930c373279716eb199180dc51404f7ed
1 /*
2 * Copyright © 2014 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 /**
25 * \file large-uniforms.c
27 * Test that shader with the maximum allowed uniforms links successfully
28 * and test link error when using more uniforms slots than allowed.
30 * From the ARB_tessellation_shader spec (Sections 2.X.1.1 and 2.X.3.1):
32 * A link error is generated if an attempt is made to utilize more than the
33 * space available for tessellation control shader uniform variables.
37 #include "piglit-util-gl.h"
39 PIGLIT_GL_TEST_CONFIG_BEGIN
41 config.supports_gl_compat_version = 32;
42 config.supports_gl_core_version = 32;
43 config.khr_no_error_support = PIGLIT_NO_ERRORS;
45 PIGLIT_GL_TEST_CONFIG_END
48 static const char *const vs_source =
49 "#version 150\n"
50 "void main() { gl_Position = vec4(0.0); }\n";
52 static const char *const tcs_dummy =
53 "#version 150\n"
54 "#extension GL_ARB_tessellation_shader: require\n"
55 "layout(vertices = 3) out;\n"
56 "void main() {\n"
57 " gl_TessLevelOuter = float[4](1.0, 1.0, 1.0, 1.0);\n"
58 " gl_TessLevelInner = float[2](1.0, 1.0);\n"
59 "}\n";
61 static const char *const tcs_source_uniform_array_template =
62 "#version 150\n"
63 "#extension GL_ARB_tessellation_shader: require\n"
64 "layout(vertices = 3) out;\n"
65 "uniform float large_array[%d];\n"
66 "void main() {\n"
67 " gl_TessLevelOuter = float[4](1.0, 1.0, 1.0, 1.0);\n"
68 " gl_TessLevelInner = float[2](1.0, 1.0);\n"
69 " for (int i = 0; i < large_array.length(); ++i)\n"
70 " gl_TessLevelInner[0] += large_array[i];\n"
71 "}\n";
73 static const char *const tcs_source_uniform_block_template =
74 "#version 150\n"
75 "#extension GL_ARB_tessellation_shader: require\n"
76 "layout(vertices = 3) out;\n"
77 "layout(std140) uniform block {\n"
78 " vec4 large_array[%d];\n"
79 "} large_block[%d];\n"
80 "void main() {\n"
81 " gl_TessLevelOuter = float[4](1.0, 1.0, 1.0, 1.0);\n"
82 " gl_TessLevelInner = float[2](1.0, 1.0);\n"
83 " for (int i = 0; i < large_block[0].large_array.length(); ++i)\n"
84 " gl_TessLevelInner[0] += %s;\n"
85 "}\n";
87 static const char *const tes_dummy =
88 "#version 150\n"
89 "#extension GL_ARB_tessellation_shader: require\n"
90 "layout(triangles) in;\n"
91 "void main() {\n"
92 " gl_Position = vec4(0.0);\n"
93 "}\n";
95 static const char *const tes_source_uniform_array_template =
96 "#version 150\n"
97 "#extension GL_ARB_tessellation_shader: require\n"
98 "layout(triangles) in;\n"
99 "uniform float large_array[%d];\n"
100 "void main() {\n"
101 " gl_Position = vec4(0.0);\n"
102 " for (int i = 0; i < large_array.length(); ++i)\n"
103 " gl_Position.x += large_array[i];\n"
104 "}\n";
106 static const char *const tes_source_uniform_block_template =
107 "#version 150\n"
108 "#extension GL_ARB_tessellation_shader: require\n"
109 "layout(triangles) in;\n"
110 "layout(std140) uniform block {\n"
111 " vec4 large_array[%d];\n"
112 "} large_block[%d];\n"
113 "void main() {\n"
114 " gl_Position = vec4(0.0);\n"
115 " for (int i = 0; i < large_block[0].large_array.length(); ++i)\n"
116 " gl_Position.x += %s;\n"
117 "}\n";
119 static const char *const fs_source =
120 "#version 150\n"
121 "void main() { gl_FragColor = vec4(0.0); }\n";
124 static bool
125 test_uniform_array(const GLenum shader, const int n, const bool expect_fail)
127 bool link_status;
128 unsigned int prog;
129 char *source_uniforms;
130 const char *source_template = (shader == GL_TESS_CONTROL_SHADER) ?
131 tcs_source_uniform_array_template :
132 tes_source_uniform_array_template;
134 source_uniforms = malloc(strlen(source_template) + 8);
135 sprintf(source_uniforms, source_template, n);
137 if (shader == GL_TESS_CONTROL_SHADER)
138 prog = piglit_build_simple_program_unlinked_multiple_shaders(
139 GL_VERTEX_SHADER, vs_source,
140 GL_TESS_CONTROL_SHADER, source_uniforms,
141 GL_TESS_EVALUATION_SHADER, tes_dummy,
142 GL_FRAGMENT_SHADER, fs_source,
144 else
145 prog = piglit_build_simple_program_unlinked_multiple_shaders(
146 GL_VERTEX_SHADER, vs_source,
147 GL_TESS_CONTROL_SHADER, tcs_dummy,
148 GL_TESS_EVALUATION_SHADER, source_uniforms,
149 GL_FRAGMENT_SHADER, fs_source,
152 glLinkProgram(prog);
153 link_status = piglit_link_check_status_quiet(prog);
155 if (link_status && expect_fail) {
156 fprintf(stderr, "Program with %d uniform components in %s "
157 "linked successfully\n", n,
158 piglit_get_gl_enum_name(shader));
159 free(source_uniforms);
160 return false;
162 if (!link_status && !expect_fail) {
163 fprintf(stderr, "Program with %d uniform components in %s "
164 "failed to link\n", n,
165 piglit_get_gl_enum_name(shader));
166 free(source_uniforms);
167 return false;
169 glDeleteProgram(prog);
171 free(source_uniforms);
173 return true;
177 static bool
178 test_uniform_block(const GLenum shader, const int num_blocks, const int size,
179 const bool expect_fail)
181 bool link_status;
182 unsigned int prog;
183 char *source_uniforms;
184 const char *source_template = shader == GL_TESS_CONTROL_SHADER ?
185 tcs_source_uniform_block_template :
186 tes_source_uniform_block_template;
187 const char *summand_template = " + large_block[%d].large_array[i].w";
188 char *sum;
189 int i;
192 /* From the GLSL 1.5 spec (chapter 4.3.7):
194 * All indexes used to index a uniform block
195 * array must be integral constant expressions.
197 * So we have to generate code from an unrolled loop.
199 sum = malloc((strlen(summand_template) + 8) * num_blocks);
200 sum[0] = '\0';
201 for(i = 0; i < num_blocks; ++i) {
202 char summand[44];
203 sprintf(summand, summand_template, i);
204 strcat(sum, summand);
207 source_uniforms = malloc(strlen(source_template) + 16 + strlen(sum));
208 sprintf(source_uniforms, source_template, size, num_blocks, sum);
209 free(sum);
211 if (shader == GL_TESS_CONTROL_SHADER)
212 prog = piglit_build_simple_program_unlinked_multiple_shaders(
213 GL_VERTEX_SHADER, vs_source,
214 GL_TESS_CONTROL_SHADER, source_uniforms,
215 GL_TESS_EVALUATION_SHADER, tes_dummy,
216 GL_FRAGMENT_SHADER, fs_source,
218 else
219 prog = piglit_build_simple_program_unlinked_multiple_shaders(
220 GL_VERTEX_SHADER, vs_source,
221 GL_TESS_CONTROL_SHADER, tcs_dummy,
222 GL_TESS_EVALUATION_SHADER, source_uniforms,
223 GL_FRAGMENT_SHADER, fs_source,
226 glLinkProgram(prog);
227 link_status = piglit_link_check_status_quiet(prog);
229 if (link_status && expect_fail) {
230 fprintf(stderr, "Program with %d uniform blocks of size %d (vec4s)"
231 "in %s linked successfully\n", num_blocks, size,
232 piglit_get_gl_enum_name(shader));
233 free(source_uniforms);
234 return false;
236 if (!link_status && !expect_fail) {
237 fprintf(stderr, "Program with %d uniform blocks of size %d (vec4s)"
238 "in %s failed to link\n", num_blocks, size,
239 piglit_get_gl_enum_name(shader));
240 free(source_uniforms);
241 return false;
243 glDeleteProgram(prog);
245 free(source_uniforms);
247 return true;
251 static bool
252 report(bool result, GLenum shader, char const *name)
254 piglit_report_subtest_result(result ? PIGLIT_PASS : PIGLIT_FAIL,
255 "%s-%s",
256 piglit_get_gl_enum_name(shader),
257 name);
258 return result;
262 static bool
263 test_shader(const GLenum shader, const int max_uniform_components,
264 const int max_combined_uniform_components,
265 const int max_uniform_blocks)
267 bool pass = true;
268 int max_uniform_block_size;
270 /* From the tessellation shader spec (New State section):
272 * The minimum values for MAX_COMBINED_*_UNIFORM_COMPONENTS by
273 * computing the value of:
274 * MAX_*_UNIFORM_COMPONENTS + MAX_*_UNIFORM_BLOCKS *
275 * (MAX_UNIFORM_BLOCK_SIZE/4)
276 * using the minimum values of the corresponding terms.
278 glGetIntegerv(GL_MAX_UNIFORM_BLOCK_SIZE, &max_uniform_block_size);
280 pass = report(max_combined_uniform_components >=
281 max_uniform_components +
282 max_uniform_blocks *
283 (max_uniform_block_size/4), shader, "combined-limit-large-enough") && pass;
285 pass = report(test_uniform_array(shader, max_uniform_components, false), shader, "array-at-limit") && pass;
286 pass = report(test_uniform_array(shader, max_uniform_components + 1, true), shader, "array-too-large") && pass;
289 pass = report(test_uniform_block(shader, max_uniform_blocks, max_uniform_block_size/16, false), shader, "blocks-at-limits") && pass;
290 pass = report(test_uniform_block(shader, max_uniform_blocks + 1, max_uniform_block_size/16, true), shader, "blocks-too-many-blocks") && pass;
291 /* For uniform blocks too large, the spec says a linker error *may* be
292 * emitted; it is not required. so don't test that.
295 return pass;
299 void
300 piglit_init(int argc, char **argv)
302 bool pass = true;
303 int max_uniform_components;
304 int max_combined_uniform_components;
305 int max_uniform_blocks;
307 piglit_require_extension("GL_ARB_tessellation_shader");
309 glGetIntegerv(GL_MAX_TESS_CONTROL_UNIFORM_COMPONENTS,
310 &max_uniform_components);
311 glGetIntegerv(GL_MAX_COMBINED_TESS_CONTROL_UNIFORM_COMPONENTS,
312 &max_combined_uniform_components);
313 glGetIntegerv(GL_MAX_TESS_CONTROL_UNIFORM_BLOCKS,
314 &max_uniform_blocks);
316 pass = test_shader(GL_TESS_CONTROL_SHADER, max_uniform_components, max_combined_uniform_components, max_uniform_blocks) && pass;
318 glGetIntegerv(GL_MAX_TESS_EVALUATION_UNIFORM_COMPONENTS,
319 &max_uniform_components);
320 glGetIntegerv(GL_MAX_COMBINED_TESS_EVALUATION_UNIFORM_COMPONENTS,
321 &max_combined_uniform_components);
322 glGetIntegerv(GL_MAX_TESS_EVALUATION_UNIFORM_BLOCKS,
323 &max_uniform_blocks);
325 pass = test_shader(GL_TESS_EVALUATION_SHADER, max_uniform_components, max_combined_uniform_components, max_uniform_blocks) && pass;
327 pass = piglit_check_gl_error(GL_NO_ERROR) && pass;
329 piglit_report_result(pass ? PIGLIT_PASS : PIGLIT_FAIL);
333 enum piglit_result
334 piglit_display(void)
336 return PIGLIT_PASS;