Add more structure constructor tests.
[piglit/hramrach.git] / tests / shaders / glsl-fs-raytrace-bug27060.c
blob4963ef364f256c3bf2ab8097677d970f0fa770c5
1 /* -*- mode: c; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; coding: utf-8-unix -*- */
2 /*
3 Copyright (c) 2010 Kristóf Ralovich
5 Permission is hereby granted, free of charge, to any person obtaining a copy
6 of this software and associated documentation files (the "Software"), to deal
7 in the Software without restriction, including without limitation the rights
8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the Software is
10 furnished to do so, subject to the following conditions:
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the 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 THE
18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 THE SOFTWARE.
25 #include "piglit-util.h"
26 #include "glsl-fs-raytrace-bug27060.h"
28 int piglit_width = 256, piglit_height = 256;
29 int piglit_window_mode = GLUT_RGB | GLUT_DOUBLE;
31 static GLint program = -1;
32 static float rot[9] = {1,0,0, 0,1,0, 0,0,1};
33 static const float failing_pixel_percentage = 0.15F;
35 static const char* vsSource =
36 "varying vec2 rayDir; \n"
37 " \n"
38 "void main() \n"
39 "{ \n"
40 " rayDir = gl_MultiTexCoord0.xy - vec2(0.5,0.5); \n"
41 " gl_Position = gl_ProjectionMatrix * gl_Vertex; \n"
42 "}\n";
44 static const char* fsSource =
45 "const float INF = 9999.9; \n"
46 "const float EPSILON = 0.00001; \n"
47 "const vec3 lightPos = vec3(0.0, 8.0, 1.0); \n"
48 "const vec4 backgroundColor = vec4(0.2,0.3,0.4,1); \n"
49 " \n"
50 "varying vec2 rayDir; \n"
51 " \n"
52 "uniform mat3 rot; \n"
53 " \n"
54 "struct Ray \n"
55 "{ \n"
56 "vec3 orig; \n"
57 "vec3 dir; \n"
58 "}; \n"
59 " \n"
60 "struct Sphere \n"
61 "{ \n"
62 " vec3 c; \n"
63 " float r; \n"
64 "}; \n"
65 " \n"
66 "struct Isec \n"
67 "{ \n"
68 " float t; \n"
69 " int idx; \n"
70 " vec3 hit; \n"
71 " vec3 n; \n"
72 "}; \n"
73 " \n"
74 #ifdef __APPLE__
75 "Sphere spheres0 = Sphere( vec3(0.0,0.0,-1.0), 0.5 ); \n"
76 "Sphere spheres1 = Sphere( vec3(-3.0,0.0,-1.0), 1.5 ); \n"
77 "Sphere spheres2 = Sphere( vec3(0.0,3.0,-1.0), 0.5 ); \n"
78 "Sphere spheres3 = Sphere( vec3(2.0,0.0,-1.0), 1.0 ); \n"
79 #else
80 "const Sphere spheres0 = Sphere( vec3(0.0,0.0,-1.0), 0.5 ); \n"
81 "const Sphere spheres1 = Sphere( vec3(-3.0,0.0,-1.0), 1.5 ); \n"
82 "const Sphere spheres2 = Sphere( vec3(0.0,3.0,-1.0), 0.5 ); \n"
83 "const Sphere spheres3 = Sphere( vec3(2.0,0.0,-1.0), 1.0 ); \n"
84 #endif
85 " \n"
86 "// Mesa intel gen4 generates \"unsupported IR in fragment shader 13\" for\n"
87 "// sqrt, let's work around. \n"
88 "float \n"
89 "sqrt_hack(float f2) \n"
90 "{ \n"
91 " vec3 v = vec3(f2,0.0,0.0); \n"
92 " return length(v); \n"
93 "} \n"
94 " \n"
95 "void \n"
96 "intersect(const in Ray ray, \n"
97 " const in Sphere sph, \n"
98 " const in int idx, \n"
99 " inout Isec isec) \n"
100 "{ \n"
101 " // Project both o and the sphere to the plane perpendicular to d \n"
102 " // and containing c. Let x be the point where the ray intersects \n"
103 " // the plane. If |x-c| < r, the ray intersects the sphere. \n"
104 " vec3 o = ray.orig; \n"
105 " vec3 d = ray.dir; \n"
106 " vec3 n = -d; \n"
107 " vec3 c = sph.c; \n"
108 " float r = sph.r; \n"
109 " float t = dot(c-o,n)/dot(n,d); \n"
110 " vec3 x = o+d*t; \n"
111 " float e = length(x-c); \n"
112 " if(e > r) \n"
113 " { \n"
114 " // no intersection \n"
115 " return; \n"
116 " } \n"
117 " \n"
118 " // Apply Pythagorean theorem on the (intersection,x,c) triangle \n"
119 " // to get the distance between c and the intersection. \n"
120 "//#define BUGGY_INTEL_GEN4_GLSL \n"
121 "#ifndef BUGGY_INTEL_GEN4_GLSL \n"
122 " float f = sqrt(r*r - e*e); \n"
123 "#else \n"
124 " float f = sqrt_hack(r*r - e*e); \n"
125 "#endif \n"
126 " float dist = t - f; \n"
127 " if(dist < 0.0) \n"
128 " { \n"
129 " // inside the sphere \n"
130 " return; \n"
131 " } \n"
132 " \n"
133 " if(dist < EPSILON) \n"
134 " return; \n"
135 " \n"
136 " if(dist > isec.t) \n"
137 " return; \n"
138 " \n"
139 " isec.t = dist; \n"
140 " isec.idx = idx; \n"
141 " \n"
142 " isec.hit = ray.orig + ray.dir * isec.t; \n"
143 " isec.n = (isec.hit - c) / r; \n"
144 "} \n"
145 " \n"
146 "Isec \n"
147 "intersect(const in Ray ray, \n"
148 " const in float max_t /*= INF*/) \n"
149 "{ \n"
150 " Isec nearest; \n"
151 " nearest.t = max_t; \n"
152 " nearest.idx = -1; \n"
153 " \n"
154 " intersect(ray, spheres0, 0, nearest); \n"
155 " intersect(ray, spheres1, 1, nearest); \n"
156 " intersect(ray, spheres2, 2, nearest); \n"
157 " intersect(ray, spheres3, 3, nearest); \n"
158 " \n"
159 " return nearest; \n"
160 "} \n"
161 " \n"
162 "vec4 \n"
163 "idx2color(const in int idx) \n"
164 "{ \n"
165 " vec4 diff; \n"
166 " if(idx == 0) \n"
167 " diff = vec4(1.0, 0.0, 0.0, 0.0); \n"
168 " else if(idx == 1) \n"
169 " diff = vec4(0.0, 1.0, 0.0, 0.0); \n"
170 " else if(idx == 2) \n"
171 " diff = vec4(0.0, 0.0, 1.0, 0.0); \n"
172 " else if(idx == 3) \n"
173 " diff = vec4(1.0, 1.0, 0.0, 0.0); \n"
174 " return diff; \n"
175 "} \n"
176 " \n"
177 "vec4 \n"
178 "trace0(const in Ray ray) \n"
179 "{ \n"
180 " Isec isec = intersect(ray, INF); \n"
181 " \n"
182 " if(isec.idx == -1) \n"
183 " { \n"
184 " return backgroundColor; \n"
185 " } \n"
186 " \n"
187 " vec4 diff = idx2color(isec.idx); \n"
188 " \n"
189 " vec3 N = isec.n; \n"
190 " vec3 L = normalize(lightPos-isec.hit); \n"
191 " vec3 camera_dir = normalize(ray.orig - isec.hit); \n"
192 " return dot(N,L)*diff + pow( \n"
193 " clamp(dot(reflect(-L,N),camera_dir),0.0,1.0),16.0); \n"
194 "} \n"
195 " \n"
196 "vec4 \n"
197 "trace1(const in Ray ray) \n"
198 "{ \n"
199 " Isec isec = intersect(ray, INF); \n"
200 " \n"
201 " if(isec.idx == -1) \n"
202 " { \n"
203 " return backgroundColor; \n"
204 " } \n"
205 " \n"
206 " Ray reflRay = Ray(isec.hit, reflect(ray.dir, isec.n)); \n"
207 " \n"
208 " vec4 reflCol = trace0(reflRay); \n"
209 " \n"
210 " vec4 diff = idx2color(isec.idx) + reflCol; \n"
211 " \n"
212 " vec3 N = isec.n; \n"
213 " vec3 L = normalize(lightPos-isec.hit); \n"
214 " vec3 camera_dir = normalize(ray.orig - isec.hit); \n"
215 " return dot(N,L)*diff + pow( \n"
216 " clamp(dot(reflect(-L,N),camera_dir),0.0,1.0),16.0); \n"
217 "} \n"
218 " \n"
219 "void main() \n"
220 "{ \n"
221 " const float z = -0.5; \n"
222 " const vec3 cameraPos = vec3(0,0,3); \n"
223 " Ray r = Ray(cameraPos, normalize(vec3(rayDir, z) * rot)); \n"
224 " gl_FragColor = trace1(r); \n"
225 "}\n";
227 enum piglit_result
228 piglit_display(void)
230 int passed_cnt = 0;
231 GLint location = glGetUniformLocation(program, "rot");
232 static const float m = -10.F;
233 static const float p = 10.F;
234 static const float d = -0.5F;
235 int x,y;
237 glUseProgram(program);
238 glUniformMatrix3fv(location, 1, 0, rot);
240 glBegin(GL_QUADS);
242 glTexCoord2f(0.0F, 0.0F); glVertex3f(m, m, d);
243 glTexCoord2f(1.0F, 0.0F); glVertex3f(p, m, d);
244 glTexCoord2f(1.0F, 1.0F); glVertex3f(p, p, d);
245 glTexCoord2f(0.0F, 1.0F); glVertex3f(m, p, d);
247 glEnd();
248 glUseProgram(0);
250 /* the pre-computed image is 256x256 */
251 assert(piglit_height == 256);
252 assert(piglit_width == 256);
254 for (y = 0; y < piglit_height; y++)
256 for (x = 0; x < piglit_width; x++)
258 float color[3];
260 color[0] = (float)gimp_image.pixel_data[(y*256+x)*3 +0] / 256.0F;
261 color[1] = (float)gimp_image.pixel_data[(y*256+x)*3 +1] / 256.0F;
262 color[2] = (float)gimp_image.pixel_data[(y*256+x)*3 +2] / 256.0F;
264 if(piglit_probe_pixel_rgb(x, 255-y, color))
265 passed_cnt++;
269 glutSwapBuffers();
271 return ((float)passed_cnt > (1.0F-failing_pixel_percentage)
272 *piglit_width*piglit_height)
273 ? PIGLIT_SUCCESS : PIGLIT_FAILURE;
276 void
277 piglit_init(int argc, char **argv)
279 GLint vs=-1, fs=-1;
281 glDisable(GL_DEPTH_TEST);
283 if (!GLEW_VERSION_2_0) {
284 printf("Requires OpenGL 2.0\n");
285 piglit_report_result(PIGLIT_SKIP);
288 glViewport(0, 0, piglit_width, piglit_height);
289 glMatrixMode(GL_PROJECTION);
290 glLoadIdentity();
291 glOrtho(-10, 10, -10, 10, -1, 1);
292 glMatrixMode(GL_MODELVIEW);
293 glLoadIdentity();
295 vs = piglit_compile_shader_text(GL_VERTEX_SHADER, vsSource);
296 fs = piglit_compile_shader_text(GL_FRAGMENT_SHADER, fsSource);
298 assert(glIsShader(vs));
299 assert(glIsShader(fs));
301 program = piglit_link_simple_program(vs, fs);
303 assert(glIsProgram(program));
305 if (!piglit_link_check_status(program))
306 piglit_report_result(PIGLIT_FAILURE);