1 /* -*- mode: c; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; coding: utf-8-unix -*- */
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
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"
40 " rayDir = gl_MultiTexCoord0.xy - vec2(0.5,0.5); \n"
41 " gl_Position = gl_ProjectionMatrix * gl_Vertex; \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"
50 "varying vec2 rayDir; \n"
52 "uniform mat3 rot; \n"
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"
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"
86 "// Mesa intel gen4 generates \"unsupported IR in fragment shader 13\" for\n"
87 "// sqrt, let's work around. \n"
89 "sqrt_hack(float f2) \n"
91 " vec3 v = vec3(f2,0.0,0.0); \n"
92 " return length(v); \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"
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"
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"
114 " // no intersection \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"
124 " float f = sqrt_hack(r*r - e*e); \n"
126 " float dist = t - f; \n"
129 " // inside the sphere \n"
133 " if(dist < EPSILON) \n"
136 " if(dist > isec.t) \n"
140 " isec.idx = idx; \n"
142 " isec.hit = ray.orig + ray.dir * isec.t; \n"
143 " isec.n = (isec.hit - c) / r; \n"
147 "intersect(const in Ray ray, \n"
148 " const in float max_t /*= INF*/) \n"
151 " nearest.t = max_t; \n"
152 " nearest.idx = -1; \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"
159 " return nearest; \n"
163 "idx2color(const in int idx) \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"
178 "trace0(const in Ray ray) \n"
180 " Isec isec = intersect(ray, INF); \n"
182 " if(isec.idx == -1) \n"
184 " return backgroundColor; \n"
187 " vec4 diff = idx2color(isec.idx); \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"
197 "trace1(const in Ray ray) \n"
199 " Isec isec = intersect(ray, INF); \n"
201 " if(isec.idx == -1) \n"
203 " return backgroundColor; \n"
206 " Ray reflRay = Ray(isec.hit, reflect(ray.dir, isec.n)); \n"
208 " vec4 reflCol = trace0(reflRay); \n"
210 " vec4 diff = idx2color(isec.idx) + reflCol; \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"
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"
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
;
237 glUseProgram(program
);
238 glUniformMatrix3fv(location
, 1, 0, rot
);
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
);
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
++)
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
))
271 return ((float)passed_cnt
> (1.0F
-failing_pixel_percentage
)
272 *piglit_width
*piglit_height
)
273 ? PIGLIT_SUCCESS
: PIGLIT_FAILURE
;
277 piglit_init(int argc
, char **argv
)
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
);
291 glOrtho(-10, 10, -10, 10, -1, 1);
292 glMatrixMode(GL_MODELVIEW
);
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
);