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-gl.h"
26 #include "glsl-fs-raytrace-bug27060.h"
28 PIGLIT_GL_TEST_CONFIG_BEGIN
30 config
.supports_gl_compat_version
= 10;
32 config
.window_width
= 256;
33 config
.window_height
= 256;
34 config
.window_visual
= PIGLIT_GL_VISUAL_RGB
| PIGLIT_GL_VISUAL_DOUBLE
;
36 PIGLIT_GL_TEST_CONFIG_END
38 static GLint program
= -1;
39 static float rot
[9] = {1,0,0, 0,1,0, 0,0,1};
40 static const float failing_pixel_percentage
= 0.15F
;
42 static const char* vsSource
=
43 "varying vec2 rayDir; \n"
47 " rayDir = gl_MultiTexCoord0.xy - vec2(0.5,0.5); \n"
48 " gl_Position = gl_ProjectionMatrix * gl_Vertex; \n"
51 static const char* fsSource
=
52 "const float INF = 9999.9; \n"
53 "const float EPSILON = 0.00001; \n"
54 "const vec3 lightPos = vec3(0.0, 8.0, 1.0); \n"
55 "const vec4 backgroundColor = vec4(0.2,0.3,0.4,1); \n"
57 "varying vec2 rayDir; \n"
59 "uniform mat3 rot; \n"
82 "Sphere spheres0 = Sphere( vec3(0.0,0.0,-1.0), 0.5 ); \n"
83 "Sphere spheres1 = Sphere( vec3(-3.0,0.0,-1.0), 1.5 ); \n"
84 "Sphere spheres2 = Sphere( vec3(0.0,3.0,-1.0), 0.5 ); \n"
85 "Sphere spheres3 = Sphere( vec3(2.0,0.0,-1.0), 1.0 ); \n"
87 "const Sphere spheres0 = Sphere( vec3(0.0,0.0,-1.0), 0.5 ); \n"
88 "const Sphere spheres1 = Sphere( vec3(-3.0,0.0,-1.0), 1.5 ); \n"
89 "const Sphere spheres2 = Sphere( vec3(0.0,3.0,-1.0), 0.5 ); \n"
90 "const Sphere spheres3 = Sphere( vec3(2.0,0.0,-1.0), 1.0 ); \n"
93 "// Mesa intel gen4 generates \"unsupported IR in fragment shader 13\" for\n"
94 "// sqrt, let's work around. \n"
96 "sqrt_hack(float f2) \n"
98 " vec3 v = vec3(f2,0.0,0.0); \n"
99 " return length(v); \n"
103 "intersect(const in Ray ray, \n"
104 " const in Sphere sph, \n"
105 " const in int idx, \n"
106 " inout Isec isec) \n"
108 " // Project both o and the sphere to the plane perpendicular to d \n"
109 " // and containing c. Let x be the point where the ray intersects \n"
110 " // the plane. If |x-c| < r, the ray intersects the sphere. \n"
111 " vec3 o = ray.orig; \n"
112 " vec3 d = ray.dir; \n"
114 " vec3 c = sph.c; \n"
115 " float r = sph.r; \n"
116 " float t = dot(c-o,n)/dot(n,d); \n"
117 " vec3 x = o+d*t; \n"
118 " float e = length(x-c); \n"
121 " // no intersection \n"
125 " // Apply Pythagorean theorem on the (intersection,x,c) triangle \n"
126 " // to get the distance between c and the intersection. \n"
127 "//#define BUGGY_INTEL_GEN4_GLSL \n"
128 "#ifndef BUGGY_INTEL_GEN4_GLSL \n"
129 " float f = sqrt(r*r - e*e); \n"
131 " float f = sqrt_hack(r*r - e*e); \n"
133 " float dist = t - f; \n"
136 " // inside the sphere \n"
140 " if(dist < EPSILON) \n"
143 " if(dist > isec.t) \n"
147 " isec.idx = idx; \n"
149 " isec.hit = ray.orig + ray.dir * isec.t; \n"
150 " isec.n = (isec.hit - c) / r; \n"
154 "intersect(const in Ray ray, \n"
155 " const in float max_t /*= INF*/) \n"
158 " nearest.t = max_t; \n"
159 " nearest.idx = -1; \n"
161 " intersect(ray, spheres0, 0, nearest); \n"
162 " intersect(ray, spheres1, 1, nearest); \n"
163 " intersect(ray, spheres2, 2, nearest); \n"
164 " intersect(ray, spheres3, 3, nearest); \n"
166 " return nearest; \n"
170 "idx2color(const in int idx) \n"
174 " diff = vec4(1.0, 0.0, 0.0, 0.0); \n"
175 " else if(idx == 1) \n"
176 " diff = vec4(0.0, 1.0, 0.0, 0.0); \n"
177 " else if(idx == 2) \n"
178 " diff = vec4(0.0, 0.0, 1.0, 0.0); \n"
179 " else if(idx == 3) \n"
180 " diff = vec4(1.0, 1.0, 0.0, 0.0); \n"
185 "trace0(const in Ray ray) \n"
187 " Isec isec = intersect(ray, INF); \n"
189 " if(isec.idx == -1) \n"
191 " return backgroundColor; \n"
194 " vec4 diff = idx2color(isec.idx); \n"
196 " vec3 N = isec.n; \n"
197 " vec3 L = normalize(lightPos-isec.hit); \n"
198 " vec3 camera_dir = normalize(ray.orig - isec.hit); \n"
199 " return dot(N,L)*diff + pow( \n"
200 " clamp(dot(reflect(-L,N),camera_dir),0.0,1.0),16.0); \n"
204 "trace1(const in Ray ray) \n"
206 " Isec isec = intersect(ray, INF); \n"
208 " if(isec.idx == -1) \n"
210 " return backgroundColor; \n"
213 " Ray reflRay = Ray(isec.hit, reflect(ray.dir, isec.n)); \n"
215 " vec4 reflCol = trace0(reflRay); \n"
217 " vec4 diff = idx2color(isec.idx) + reflCol; \n"
219 " vec3 N = isec.n; \n"
220 " vec3 L = normalize(lightPos-isec.hit); \n"
221 " vec3 camera_dir = normalize(ray.orig - isec.hit); \n"
222 " return dot(N,L)*diff + pow( \n"
223 " clamp(dot(reflect(-L,N),camera_dir),0.0,1.0),16.0); \n"
228 " const float z = -0.5; \n"
229 " const vec3 cameraPos = vec3(0,0,3); \n"
230 " Ray r = Ray(cameraPos, normalize(vec3(rayDir, z) * rot)); \n"
231 " gl_FragColor = trace1(r); \n"
238 GLint location
= glGetUniformLocation(program
, "rot");
239 static const float m
= -10.F
;
240 static const float p
= 10.F
;
241 static const float d
= -0.5F
;
244 glUseProgram(program
);
245 glUniformMatrix3fv(location
, 1, 0, rot
);
249 glTexCoord2f(0.0F
, 0.0F
); glVertex3f(m
, m
, d
);
250 glTexCoord2f(1.0F
, 0.0F
); glVertex3f(p
, m
, d
);
251 glTexCoord2f(1.0F
, 1.0F
); glVertex3f(p
, p
, d
);
252 glTexCoord2f(0.0F
, 1.0F
); glVertex3f(m
, p
, d
);
257 /* the pre-computed image is 256x256 */
258 assert(piglit_height
== 256);
259 assert(piglit_width
== 256);
261 float *pixels
= malloc(piglit_width
* piglit_height
* 3 * sizeof(float));
262 glReadPixels(0, 0, piglit_width
, piglit_height
, GL_RGB
, GL_FLOAT
, pixels
);
264 for (y
= 0; y
< piglit_height
; y
++)
266 for (x
= 0; x
< piglit_width
; x
++)
270 color
[0] = (float)pixel_data
[(y
*256+x
)*3 +0] / 256.0F
;
271 color
[1] = (float)pixel_data
[(y
*256+x
)*3 +1] / 256.0F
;
272 color
[2] = (float)pixel_data
[(y
*256+x
)*3 +2] / 256.0F
;
274 if (piglit_compare_pixels(x
, 255-y
, color
, pixels
+ ((255-y
) * piglit_width
+ x
) * 3, piglit_tolerance
, 3))
281 piglit_present_results();
283 return ((float)passed_cnt
> (1.0F
-failing_pixel_percentage
)
284 *piglit_width
*piglit_height
)
285 ? PIGLIT_PASS
: PIGLIT_FAIL
;
289 piglit_init(int argc
, char **argv
)
293 glDisable(GL_DEPTH_TEST
);
295 piglit_require_gl_version(20);
297 glViewport(0, 0, piglit_width
, piglit_height
);
298 glMatrixMode(GL_PROJECTION
);
300 glOrtho(-10, 10, -10, 10, -1, 1);
301 glMatrixMode(GL_MODELVIEW
);
304 vs
= piglit_compile_shader_text(GL_VERTEX_SHADER
, vsSource
);
305 fs
= piglit_compile_shader_text(GL_FRAGMENT_SHADER
, fsSource
);
307 assert(glIsShader(vs
));
308 assert(glIsShader(fs
));
310 program
= piglit_link_simple_program(vs
, fs
);
312 assert(glIsProgram(program
));
314 if (!piglit_link_check_status(program
))
315 piglit_report_result(PIGLIT_FAIL
);