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-vs-raytrace-bug26691.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 const float failing_pixel_percentage
= 0.15F
;
40 static GLuint program
;
41 static float rot
[9] = {1,0,0, 0,1,0, 0,0,1};
43 static const char* vsSource
=
44 "const float INF = 9999.9; \n"
45 "const float EPSILON = 0.00001; \n"
46 "const vec3 lightPos = vec3(0.0, 8.0, 1.0); \n"
47 "const vec4 backgroundColor = vec4(0.2,0.3,0.4,1); \n"
49 "uniform mat3 rot; \n"
72 "Sphere spheres0 = Sphere( vec3(0.0,0.0,-1.0), 0.5 ); \n"
73 "Sphere spheres1 = Sphere( vec3(-3.0,0.0,-1.0), 1.5 ); \n"
74 "Sphere spheres2 = Sphere( vec3(0.0,3.0,-1.0), 0.5 ); \n"
75 "Sphere spheres3 = Sphere( vec3(2.0,0.0,-1.0), 1.0 ); \n"
77 "const Sphere spheres0 = Sphere( vec3(0.0,0.0,-1.0), 0.5 ); \n"
78 "const Sphere spheres1 = Sphere( vec3(-3.0,0.0,-1.0), 1.5 ); \n"
79 "const Sphere spheres2 = Sphere( vec3(0.0,3.0,-1.0), 0.5 ); \n"
80 "const Sphere spheres3 = Sphere( vec3(2.0,0.0,-1.0), 1.0 ); \n"
83 "// Mesa intel gen4 generates \"unsupported IR in fragment shader 13\" for\n"
84 "// sqrt, let's work around. \n"
86 "sqrt_hack(float f2) \n"
88 " vec3 v = vec3(f2,0.0,0.0); \n"
89 " return length(v); \n"
93 "intersect(const in Ray ray, \n"
94 " const in Sphere sph, \n"
95 " const in int idx, \n"
96 " inout Isec isec) \n"
98 " // Project both o and the sphere to the plane perpendicular to d \n"
99 " // and containing c. Let x be the point where the ray intersects \n"
100 " // the plane. If |x-c| < r, the ray intersects the sphere. \n"
101 " vec3 o = ray.orig; \n"
102 " vec3 d = ray.dir; \n"
104 " vec3 c = sph.c; \n"
105 " float r = sph.r; \n"
106 " float t = dot(c-o,n)/dot(n,d); \n"
107 " vec3 x = o+d*t; \n"
108 " float e = length(x-c); \n"
111 " // no intersection \n"
115 " // Apply Pythagorean theorem on the (intersection,x,c) triangle \n"
116 " // to get the distance between c and the intersection. \n"
117 "//#define BUGGY_INTEL_GEN4_GLSL 1 \n"
118 "#ifndef BUGGY_INTEL_GEN4_GLSL \n"
119 " float f = sqrt(r*r - e*e); \n"
121 " float f = sqrt_hack(r*r - e*e); \n"
123 " float dist = t - f; \n"
126 " // inside the sphere \n"
130 " if(dist < EPSILON) \n"
133 " if(dist > isec.t) \n"
137 " isec.idx = idx; \n"
139 " isec.hit = ray.orig + ray.dir * isec.t; \n"
140 " isec.n = (isec.hit - c) / r; \n"
144 "intersect(const in Ray ray, \n"
145 " const in float max_t /*= INF*/) \n"
148 " nearest.t = max_t; \n"
149 " nearest.idx = -1; \n"
151 " intersect(ray, spheres0, 0, nearest); \n"
152 " intersect(ray, spheres1, 1, nearest); \n"
153 " intersect(ray, spheres2, 2, nearest); \n"
154 " intersect(ray, spheres3, 3, nearest); \n"
156 " return nearest; \n"
160 "idx2color(const in int idx) \n"
164 " diff = vec4(1.0, 0.0, 0.0, 0.0); \n"
165 " else if(idx == 1) \n"
166 " diff = vec4(0.0, 1.0, 0.0, 0.0); \n"
167 " else if(idx == 2) \n"
168 " diff = vec4(0.0, 0.0, 1.0, 0.0); \n"
169 " else if(idx == 3) \n"
170 " diff = vec4(1.0, 1.0, 0.0, 0.0); \n"
175 "trace0(const in Ray ray) \n"
177 " Isec isec = intersect(ray, INF); \n"
179 " if(isec.idx == -1) \n"
181 " return backgroundColor; \n"
184 " vec4 diff = idx2color(isec.idx); \n"
186 " vec3 N = isec.n; \n"
187 " vec3 L = normalize(lightPos-isec.hit); \n"
188 " vec3 camera_dir = normalize(ray.orig - isec.hit); \n"
189 " return dot(N,L)*diff + pow( \n"
190 " clamp(dot(reflect(-L,N),camera_dir),0.0,1.0),16.0); \n"
194 "trace1(const in Ray ray) \n"
196 " Isec isec = intersect(ray, INF); \n"
198 " if(isec.idx == -1) \n"
200 " return backgroundColor; \n"
203 " Ray reflRay = Ray(isec.hit, reflect(ray.dir, isec.n)); \n"
205 " vec4 reflCol = trace0(reflRay); \n"
207 " vec4 diff = idx2color(isec.idx) + reflCol; \n"
209 " vec3 N = isec.n; \n"
210 " vec3 L = normalize(lightPos-isec.hit); \n"
211 " vec3 camera_dir = normalize(ray.orig - isec.hit); \n"
212 " return dot(N,L)*diff + pow( \n"
213 " clamp(dot(reflect(-L,N),camera_dir),0.0,1.0),16.0); \n"
218 " const vec3 cameraPos = vec3(0,0,3); \n"
219 " vec3 rayDir = normalize(vec3(gl_Vertex.x, gl_Vertex.y, -1.0) * rot);\n"
220 " Ray ray = Ray(cameraPos, rayDir); \n"
221 " gl_Position = gl_Vertex; \n"
222 " gl_FrontColor = trace1(ray); \n"
229 const float w
= 0.5F
* piglit_width
;
230 const float h
= 0.5F
* piglit_height
;
232 GLint location
= glGetUniformLocation(program
, "rot");
234 glUseProgram(program
);
235 glUniformMatrix3fv(location
, 1, 0, rot
);
237 for(y
= 0; y
< piglit_height
; y
++)
239 for(x
= 0; x
< piglit_width
; x
++)
241 const float posx
= x
/ w
- 1.0F
;
242 const float posy
= y
/ h
- 1.0F
;
243 glVertex2f(posx
, posy
);
249 /* the pre-computed image is 256x256 */
250 assert(piglit_height
== 256);
251 assert(piglit_width
== 256);
253 float *pixels
= malloc(piglit_width
* piglit_height
* 3 * sizeof(float));
254 glReadPixels(0, 0, piglit_width
, piglit_height
, GL_RGB
, GL_FLOAT
, pixels
);
256 for (y
= 0; y
< piglit_height
; y
++)
258 for (x
= 0; x
< piglit_width
; x
++)
262 color
[0] = (float)pixel_data
[(y
*256+x
)*3 +0] / 256.0F
;
263 color
[1] = (float)pixel_data
[(y
*256+x
)*3 +1] / 256.0F
;
264 color
[2] = (float)pixel_data
[(y
*256+x
)*3 +2] / 256.0F
;
266 if (piglit_compare_pixels(x
, 255-y
, color
, pixels
+ ((255-y
) * piglit_width
+ x
) * 3, piglit_tolerance
, 3))
273 piglit_present_results();
275 return ((float)passed_cnt
> (1.0F
-failing_pixel_percentage
)
276 *piglit_width
*piglit_height
)
277 ? PIGLIT_PASS
: PIGLIT_FAIL
;
281 piglit_init(int argc
, char **argv
)
285 glDisable(GL_DEPTH_TEST
);
287 piglit_require_gl_version(20);
289 glViewport(0, 0, piglit_width
, piglit_height
);
290 glMatrixMode(GL_PROJECTION
);
292 glMatrixMode(GL_MODELVIEW
);
295 vs
= piglit_compile_shader_text(GL_VERTEX_SHADER
, vsSource
);
297 assert(glIsShader(vs
));
299 program
= piglit_link_simple_program(vs
, 0);
301 assert(glIsProgram(program
));
302 if (!piglit_link_check_status(program
))
303 piglit_report_result(PIGLIT_FAIL
);