1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
|
/*
* Copyright © 2009 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* Authors:
* Eric Anholt <eric@anholt.net>
*
*/
/** @file glslparsertest.c
*
* Tests that compiling (but not linking or drawing with) a given
* shader either succeeds or fails as expected.
*/
#include <errno.h>
#include "piglit-util-gl-common.h"
PIGLIT_GL_TEST_CONFIG_BEGIN
if (argc > 3) {
const float version = strtod(argv[3], NULL);
const unsigned int int_version = (unsigned) (version * 10);
switch (int_version) {
case 11:
case 12:
case 13:
config.supports_gl_compat_version = 10;
config.supports_gl_core_version = 0;
config.supports_gl_es2 = false;
break;
case 14:
case 15:
case 33:
config.supports_gl_compat_version = 31;
config.supports_gl_core_version = 31;
config.supports_gl_es2 = false;
break;
case 40:
case 41:
case 42:
config.supports_gl_compat_version = 40;
config.supports_gl_core_version = 40;
config.supports_gl_es2 = false;
break;
default:
config.supports_gl_compat_version = 10;
config.supports_gl_es2 = true;
break;
}
} else {
config.supports_gl_compat_version = 10;
config.supports_gl_es2 = true;
}
config.window_width = 200;
config.window_height = 100;
config.window_visual = PIGLIT_GL_VISUAL_DOUBLE | PIGLIT_GL_VISUAL_RGB;
PIGLIT_GL_TEST_CONFIG_END
static char *filename;
static int expected_pass;
static int gl_version_times_10 = 0;
static int check_link = 0;
static float requested_version = 1.10;
static GLint
get_shader_compile_status(GLuint shader)
{
GLint status;
#if defined PIGLIT_USE_OPENGL
if (gl_version_times_10 >= 20) {
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
} else {
glGetObjectParameterivARB(shader, GL_OBJECT_COMPILE_STATUS_ARB, &status);
}
#elif defined PIGLIT_USE_OPENGL_ES2
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
#else
# error
#endif
return status;
}
static GLsizei
get_shader_info_log_length(GLuint shader)
{
GLsizei length;
#if defined PIGLIT_USE_OPENGL
if (gl_version_times_10 >= 20) {
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length);
} else {
glGetObjectParameterivARB(shader, GL_OBJECT_INFO_LOG_LENGTH_ARB, &length);
}
#elif defined PIGLIT_USE_OPENGL_ES2
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length);
#else
# error
#endif
return length;
}
/**
* GLES requires both vertex and fragment shaders to be present in
* order to link. From section 2.10.3 (Program Objects) of the GLES 2.0 spec:
*
* "Linking will also fail ... if program does not contain both a
* vertex shader and a fragment shader ..."
*
* So compile a dummy shader of type complementary to "type" and
* attach it to shader_prog.
*/
static void
attach_complementary_shader(GLuint shader_prog, GLenum type)
{
static const char *dummy_vertex_shader =
"#version 100\nvoid main() { gl_Position = vec4(0.0); }";
static const char *dummy_fragment_shader =
"#version 100\nvoid main() { }";
GLint shader;
switch (type) {
case GL_FRAGMENT_SHADER:
shader = piglit_compile_shader_text(GL_VERTEX_SHADER,
dummy_vertex_shader);
break;
case GL_VERTEX_SHADER:
shader = piglit_compile_shader_text(GL_FRAGMENT_SHADER,
dummy_fragment_shader);
break;
default:
fprintf(stderr,
"Unexpected type in attach_complementary_shader()");
piglit_report_result(PIGLIT_FAIL);
exit(1);
}
piglit_AttachShader(shader_prog, shader);
}
static void
test(void)
{
GLint prog;
GLint ok;
GLchar *prog_string;
FILE *out;
GLboolean pass;
GLchar *info;
GLint size;
GLenum type;
char *failing_stage = NULL;
if (strcmp(filename + strlen(filename) - 4, "frag") == 0)
type = GL_FRAGMENT_SHADER;
else if (strcmp(filename + strlen(filename) - 4, "vert") == 0)
type = GL_VERTEX_SHADER;
else {
fprintf(stderr, "Couldn't determine type of program %s\n",
filename);
piglit_report_result(PIGLIT_FAIL);
exit(1);
}
piglit_require_vertex_shader();
piglit_require_fragment_shader();
prog_string = piglit_load_text_file(filename, NULL);
if (prog_string == NULL) {
fprintf(stderr, "Couldn't open program %s: %s\n",
filename, strerror(errno));
exit(1);
}
prog = piglit_CreateShader(type);
piglit_ShaderSource(prog, 1, (const GLchar **)&prog_string, NULL);
piglit_CompileShader(prog);
ok = get_shader_compile_status(prog);
size = get_shader_info_log_length(prog);
if (size != 0) {
info = malloc(size);
piglit_GetShaderInfoLog(prog, size, NULL, info);
} else {
info = "(no compiler output)";
}
if (!ok) {
failing_stage = "compile";
} else {
/* Try linking the shader if it compiled. We do this
* even if --check-link wasn't specified, to increase
* coverage of linker code.
*/
GLuint shader_prog;
shader_prog = piglit_CreateProgram();
piglit_AttachShader(shader_prog, prog);
if (requested_version == 1.00)
attach_complementary_shader(shader_prog, type);
piglit_LinkProgram(shader_prog);
if (check_link) {
ok = piglit_link_check_status_quiet(shader_prog);
if (!ok) {
failing_stage = "link";
}
}
piglit_DeleteProgram(shader_prog);
}
pass = (expected_pass == ok);
if (pass)
out = stdout;
else
out = stderr;
if (!ok) {
fprintf(out, "Failed to %s %s shader %s: %s\n",
failing_stage,
type == GL_FRAGMENT_SHADER ? "fragment" : "vertex",
filename, info);
if (expected_pass) {
printf("Shader source:\n");
printf("%s\n", prog_string);
}
} else {
fprintf(out, "Successfully %s %s shader %s: %s\n",
check_link ? "compiled and linked" : "compiled",
type == GL_FRAGMENT_SHADER ? "fragment" : "vertex",
filename, info);
if (!expected_pass) {
printf("Shader source:\n");
printf("%s\n", prog_string);
}
}
if (size != 0)
free(info);
free(prog_string);
piglit_DeleteShader(prog);
piglit_report_result (pass ? PIGLIT_PASS : PIGLIT_FAIL);
}
static void usage(char *name)
{
printf("%s {options} <filename.frag|filename.vert> <pass|fail> "
"{requested GLSL vesion} {list of required GL extensions}\n", name);
printf("\nSupported options:\n");
printf(" --check-link: also detect link failures\n");
exit(1);
}
/**
* Process any options and remove them from the argv array. Return
* the new argc.
*/
int process_options(int argc, char **argv)
{
int i = 1;
int new_argc = 1;
while (i < argc) {
if (argv[i][0] == '-') {
if (strcmp(argv[i], "--check-link") == 0)
check_link = 1;
else
usage(argv[0]);
/* do not retain the option; we've processed it */
i++;
} else {
/* retain the option in the argv array */
argv[new_argc++] = argv[i++];
}
}
return new_argc;
}
void
piglit_init(int argc, char**argv)
{
const char *glsl_version_string;
float glsl_version;
int i;
argc = process_options(argc, argv);
if (argc < 3)
usage(argv[0]);
if (strlen(argv[1]) < 5)
usage(argv[0]);
filename = argv[1];
if (strcmp(argv[2], "pass") == 0)
expected_pass = 1;
else if (strcmp(argv[2], "fail") == 0)
expected_pass = 0;
else
usage(argv[0]);
if (argc > 3)
requested_version = strtod(argv[3], NULL);
gl_version_times_10 = piglit_get_gl_version();
if (gl_version_times_10 < 20
&& !piglit_is_extension_supported("GL_ARB_shader_objects")) {
printf("Requires OpenGL 2.0\n");
piglit_report_result(PIGLIT_SKIP);
exit(1);
}
glsl_version_string = (char *)
glGetString(GL_SHADING_LANGUAGE_VERSION);
glsl_version = (glsl_version_string == NULL)
? 0.0 : strtod(glsl_version_string, NULL);
if (requested_version == 1.00) {
piglit_require_extension("GL_ARB_ES2_compatibility");
} else if (glsl_version < requested_version) {
fprintf(stderr,
"GLSL version is %f, but requested version %f is required\n",
glsl_version,
requested_version);
piglit_report_result(PIGLIT_SKIP);
}
for (i = 4; i < argc; i++) {
if (argv[i][0] == '!')
piglit_require_not_extension(argv[i] + 1);
else
piglit_require_extension(argv[i]);
}
test();
}
enum piglit_result
piglit_display(void)
{
/* Should never be reached */
return PIGLIT_FAIL;
}
|