Add more structure constructor tests.
[piglit/hramrach.git] / tests / glslparsertest / glslparsertest.c
blob5e1c781fbc5e1306321b03315eddc6262aac05ea
1 /*
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
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.
23 * Authors:
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.
34 #include <errno.h>
35 #include <sys/stat.h>
37 #include "piglit-util.h"
39 #define WIN_WIDTH 200
40 #define WIN_HEIGHT 100
42 static char *filename;
43 static int expected_pass;
45 static void
46 test(void)
48 GLint prog;
49 GLint ok;
50 struct stat st;
51 int err;
52 GLchar *prog_string;
53 FILE *f;
54 FILE *out;
55 GLboolean pass;
56 GLchar *info;
57 GLint size;
58 GLenum type;
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;
64 else {
65 fprintf(stderr, "Couldn't determine type of program %s\n",
66 filename);
67 piglit_report_result(PIGLIT_FAILURE);
68 exit(1);
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);
79 if (err == -1) {
80 fprintf(stderr, "Couldn't stat program %s: %s\n",
81 filename, strerror(errno));
82 exit(1);
85 prog_string = malloc(st.st_size + 1);
86 if (prog_string == NULL) {
87 fprintf(stderr, "malloc\n");
88 exit(1);
91 f = fopen(filename, "r");
92 if (f == NULL) {
93 fprintf(stderr, "Couldn't open program %s: %s\n",
94 filename, strerror(errno));
95 exit(1);
97 fread(prog_string, 1, st.st_size, f);
98 prog_string[st.st_size] = '\0';
99 fclose(f);
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);
106 } else {
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));
114 if (pass)
115 out = stdout;
116 else
117 out = stderr;
119 if (GLEW_VERSION_2_0) {
120 glGetShaderiv(prog, GL_INFO_LOG_LENGTH, &size);
121 } else {
122 glGetObjectParameterivARB(prog, GL_INFO_LOG_LENGTH, &size);
124 if (size != 0) {
125 info = malloc(size);
126 if (GLEW_VERSION_2_0) {
127 glGetShaderInfoLog(prog, size, NULL, info);
128 } else {
129 glGetObjectParameterivARB(prog, GL_INFO_LOG_LENGTH, &size);
131 } else {
132 info = "(no compiler output)";
135 if (!ok) {
136 fprintf(out, "Failed to compile %s shader %s: %s\n",
137 type == GL_FRAGMENT_SHADER ? "fragment" : "vertex",
138 filename, info);
139 if (expected_pass) {
140 printf("Shader source:\n");
141 printf("%s\n", prog_string);
143 } else {
144 fprintf(out, "Successfully compiled %s shader %s: %s\n",
145 type == GL_FRAGMENT_SHADER ? "fragment" : "vertex",
146 filename, info);
147 if (!expected_pass) {
148 printf("Shader source:\n");
149 printf("%s\n", prog_string);
153 if (size != 0)
154 free(info);
155 free(prog_string);
157 if (GLEW_VERSION_2_0) {
158 glDeleteShader(prog);
159 } else {
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);
170 exit(1);
173 int main(int argc, char**argv)
175 const char *glsl_version_string;
176 float glsl_version;
177 float minimum_version = 1.10;
179 glutInit(&argc, argv);
180 if ((argc != 3) && (argc != 4))
181 usage(argv[0]);
183 if (strlen(argv[1]) < 5)
184 usage(argv[0]);
185 filename = argv[1];
187 if (strcmp(argv[2], "pass") == 0)
188 expected_pass = 1;
189 else if (strcmp(argv[2], "fail") == 0)
190 expected_pass = 0;
191 else
192 usage(argv[0]);
194 if (argc > 3)
195 minimum_version = strtod(argv[3], NULL);
197 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
198 glutInitWindowSize(WIN_WIDTH, WIN_HEIGHT);
199 glutCreateWindow("glslparsertest");
200 glewInit();
202 if (!GLEW_VERSION_2_0 && !GL_ARB_shader_objects) {
203 printf("Requires OpenGL 2.0\n");
204 piglit_report_result(PIGLIT_SKIP);
205 exit(1);
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);
215 } else {
216 test();
219 return 0;