2 * Copyright © 2009 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
24 * Eric Anholt <eric@anholt.net>
28 /** @file glslparsertest.c
30 * Tests that compiling (but not linking or drawing with) a given
31 * shader either succeeds or fails as expected.
37 #include "piglit-util.h"
40 #define WIN_HEIGHT 100
42 static char *filename
;
43 static int expected_pass
;
60 if (strcmp(filename
+ strlen(filename
) - 4, "frag") == 0)
61 type
= GL_FRAGMENT_SHADER
;
62 else if (strcmp(filename
+ strlen(filename
) - 4, "vert") == 0)
63 type
= GL_VERTEX_SHADER
;
65 fprintf(stderr
, "Couldn't determine type of program %s\n",
67 piglit_report_result(PIGLIT_FAILURE
);
71 if (!GLEW_VERSION_2_0
) {
72 if (type
== GL_FRAGMENT_SHADER
&& !GLEW_ARB_fragment_shader
)
73 piglit_report_result(PIGLIT_SKIP
);
74 if (type
== GL_VERTEX_SHADER
&& !GLEW_ARB_vertex_shader
)
75 piglit_report_result(PIGLIT_SKIP
);
78 err
= stat(filename
, &st
);
80 fprintf(stderr
, "Couldn't stat program %s: %s\n",
81 filename
, strerror(errno
));
85 prog_string
= malloc(st
.st_size
+ 1);
86 if (prog_string
== NULL
) {
87 fprintf(stderr
, "malloc\n");
91 f
= fopen(filename
, "r");
93 fprintf(stderr
, "Couldn't open program %s: %s\n",
94 filename
, strerror(errno
));
97 fread(prog_string
, 1, st
.st_size
, f
);
98 prog_string
[st
.st_size
] = '\0';
101 if (GLEW_VERSION_2_0
) {
102 prog
= glCreateShader(type
);
103 glShaderSource(prog
, 1, (const GLchar
**)&prog_string
, NULL
);
104 glCompileShader(prog
);
105 glGetShaderiv(prog
, GL_COMPILE_STATUS
, &ok
);
107 prog
= glCreateShaderObjectARB(type
);
108 glShaderSourceARB(prog
, 1, (const GLchar
**)&prog_string
, NULL
);
109 glCompileShaderARB(prog
);
110 glGetObjectParameterivARB(prog
, GL_COMPILE_STATUS
, &ok
);
112 pass
= (expected_pass
== (ok
!= 0));
119 if (GLEW_VERSION_2_0
) {
120 glGetShaderiv(prog
, GL_INFO_LOG_LENGTH
, &size
);
122 glGetObjectParameterivARB(prog
, GL_INFO_LOG_LENGTH
, &size
);
126 if (GLEW_VERSION_2_0
) {
127 glGetShaderInfoLog(prog
, size
, NULL
, info
);
129 glGetObjectParameterivARB(prog
, GL_INFO_LOG_LENGTH
, &size
);
132 info
= "(no compiler output)";
136 fprintf(out
, "Failed to compile %s shader %s: %s\n",
137 type
== GL_FRAGMENT_SHADER
? "fragment" : "vertex",
140 printf("Shader source:\n");
141 printf("%s\n", prog_string
);
144 fprintf(out
, "Successfully compiled %s shader %s: %s\n",
145 type
== GL_FRAGMENT_SHADER
? "fragment" : "vertex",
147 if (!expected_pass
) {
148 printf("Shader source:\n");
149 printf("%s\n", prog_string
);
157 if (GLEW_VERSION_2_0
) {
158 glDeleteShader(prog
);
160 glDeleteObjectARB(prog
);
163 piglit_report_result (pass
? PIGLIT_SUCCESS
: PIGLIT_FAILURE
);
166 static void usage(char *name
)
168 printf("%s <filename.frag|filename.vert> <pass|fail> "
169 "{minimum GLSL vesion}\n", name
);
173 int main(int argc
, char**argv
)
175 const char *glsl_version_string
;
177 float minimum_version
= 1.10;
179 glutInit(&argc
, argv
);
180 if ((argc
!= 3) && (argc
!= 4))
183 if (strlen(argv
[1]) < 5)
187 if (strcmp(argv
[2], "pass") == 0)
189 else if (strcmp(argv
[2], "fail") == 0)
195 minimum_version
= strtod(argv
[3], NULL
);
197 glutInitDisplayMode(GLUT_DOUBLE
| GLUT_RGB
);
198 glutInitWindowSize(WIN_WIDTH
, WIN_HEIGHT
);
199 glutCreateWindow("glslparsertest");
202 if (!GLEW_VERSION_2_0
&& !GL_ARB_shader_objects
) {
203 printf("Requires OpenGL 2.0\n");
204 piglit_report_result(PIGLIT_SKIP
);
208 glsl_version_string
= (char *)
209 glGetString(GL_SHADING_LANGUAGE_VERSION
);
210 glsl_version
= (glsl_version_string
== NULL
)
211 ? 0.0 : strtod(glsl_version_string
, NULL
);
213 if (glsl_version
< minimum_version
) {
214 piglit_report_result(PIGLIT_SKIP
);