1 /*
2  * Copyright © 2014 Intel Corporation
3  *
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:
10  *
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.
14  *
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.
22  */
23 
24 /** \file
25  *
26  * Test that exceeding the implementation's size work group size
27  * limits results in a compile error.
28  *
29  * From the ARB_compute_shader specification:
30  *
31  *     If the local size of the shader in any dimension is greater
32  *     than the maximum size supported by the implementation for that
33  *     dimension, a compile-time error results.
34  *
35  * It is not clear from the spec how the error should be reported if
36  * the total size of the work group exceeds
37  * MAX_COMPUTE_WORK_GROUP_INVOCATIONS, but it seems reasonable to
38  * assume that this is reported at compile time as well.
39  */
40 
41 #include "piglit-util-gl.h"
42 #include <math.h>
43 #include <limits.h>
44 
45 PIGLIT_GL_TEST_CONFIG_BEGIN
46 
47 	config.supports_gl_compat_version = 33;
48 	config.supports_gl_core_version = 33;
49 
50 PIGLIT_GL_TEST_CONFIG_END
51 
52 
53 enum piglit_result
piglit_display(void)54 piglit_display(void)
55 {
56 	/* UNREACHED */
57 	return PIGLIT_FAIL;
58 }
59 
60 
61 static const char *cs_template =
62 	"#version 330\n"
63 	"#extension GL_ARB_compute_shader: enable\n"
64 	"\n"
65 	"layout(local_size_x = %d, local_size_y = %d, local_size_z = %d) in;\n"
66 	"\n"
67 	"void main()\n"
68 	"{\n"
69 	"}\n";
70 
71 
72 static bool
test_work_group_size(GLint * size,bool expect_ok)73 test_work_group_size(GLint *size, bool expect_ok)
74 {
75 	char *shader_text;
76 	GLint shader;
77 	GLint ok;
78 
79 	printf("Sizes %d, %d, %d should %s: ", size[0], size[1], size[2],
80 	       expect_ok ? "compile successfully" : "produce a compile error");
81 
82 	(void)!asprintf(&shader_text, cs_template, size[0], size[1], size[2]);
83 	shader = glCreateShader(GL_COMPUTE_SHADER);
84 	glShaderSource(shader, 1, (const GLchar **) &shader_text, NULL);
85 	glCompileShader(shader);
86 	glGetShaderiv(shader, GL_COMPILE_STATUS, &ok);
87 	if (!piglit_check_gl_error(GL_NO_ERROR)) {
88 		/* Details of the error have already been printed. */
89 		printf("GL Error occurred.\n");
90 		return false;
91 	}
92 	if (ok)
93 		printf("Successful compile.\n");
94 	else
95 		printf("Compile error.\n");
96 	return ok == expect_ok;
97 }
98 
99 
100 void
piglit_init(int argc,char ** argv)101 piglit_init(int argc, char **argv)
102 {
103 	GLint max_dims[3];
104 	GLint size[3];
105 	GLint max_invocations;
106 	int dim, i;
107 	double max_dims_product;
108 	bool pass = true;
109 
110 	piglit_require_extension("GL_ARB_compute_shader");
111 
112 	max_dims_product = 1.0;
113 	for (dim = 0; dim < 3; dim++) {
114 		glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_SIZE, dim,
115 				&max_dims[dim]);
116 		max_dims_product *= max_dims[dim];
117 	}
118 
119 	glGetIntegerv(GL_MAX_COMPUTE_WORK_GROUP_INVOCATIONS, &max_invocations);
120 
121 	if (!piglit_check_gl_error(GL_NO_ERROR))
122 		piglit_report_result(PIGLIT_FAIL);
123 
124 	for (dim = 0; dim < 3; dim++) {
125 		/* Constrain all dimensions except dim to be as small
126 		 * as possible.
127 		 */
128 		for (i = 0; i < 3; i++)
129 			if (i != dim)
130 				size[i] = 1;
131 
132 		/* Subject to that constraint, make dim as large as
133 		 * the implementation allows.
134 		 */
135 		if (max_dims[dim] < max_invocations)
136 			size[dim] = max_dims[dim];
137 		else
138 			size[dim] = max_invocations;
139 
140 		/* Test that this size is allowed. */
141 		pass = test_work_group_size(size, true) && pass;
142 
143 		/* Increase dim by 1 and make sure that the resulting
144 		 * size is not allowed.
145 		 */
146 		if (size[dim] < INT_MAX) {
147 			size[dim]++;
148 			test_work_group_size(size, false);
149 		}
150 	}
151 
152 	if (max_dims_product > (double) max_invocations) {
153 		/* Construct a size for which each dimension is in
154 		 * bounds but the product is greater than
155 		 * max_invocations.  We want to find a factor f we can
156 		 * multiply each of max_dims[] by so that the result
157 		 * has a product of max_invocations + 1.  That is, we
158 		 * need:
159 		 *
160 		 * product(f*max_dims[i]) == max_invocations + 1
161 		 *
162 		 * therefore:
163 		 *
164 		 * f^3 * product(max_dims[i]) == max_invocations + 1
165 		 */
166 		double f = pow((max_invocations + 1.0) / max_dims_product,
167 			       1.0/3.0);
168 
169 		/* Now we multiply each dimension by f, rounding up so
170 		 * that rounding errors don't push us back into the
171 		 * allowed range.
172 		 */
173 		for (dim = 0; dim < 3; dim++)
174 			size[dim] = (int) ceil(max_dims[dim] * f);
175 
176 		pass = test_work_group_size(size, false) && pass;
177 	}
178 
179 	piglit_report_result(pass ? PIGLIT_PASS : PIGLIT_FAIL);
180 }
181