perf/pixel-rate: new pixel throughput microbenchmark
[piglit.git] / tests / shaders / glsl-fs-raytrace-bug27060.c
blob008000558f10e518b1c366d64ebe6d1a151e49d6
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-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"
44 " \n"
45 "void main() \n"
46 "{ \n"
47 " rayDir = gl_MultiTexCoord0.xy - vec2(0.5,0.5); \n"
48 " gl_Position = gl_ProjectionMatrix * gl_Vertex; \n"
49 "}\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"
56 " \n"
57 "varying vec2 rayDir; \n"
58 " \n"
59 "uniform mat3 rot; \n"
60 " \n"
61 "struct Ray \n"
62 "{ \n"
63 "vec3 orig; \n"
64 "vec3 dir; \n"
65 "}; \n"
66 " \n"
67 "struct Sphere \n"
68 "{ \n"
69 " vec3 c; \n"
70 " float r; \n"
71 "}; \n"
72 " \n"
73 "struct Isec \n"
74 "{ \n"
75 " float t; \n"
76 " int idx; \n"
77 " vec3 hit; \n"
78 " vec3 n; \n"
79 "}; \n"
80 " \n"
81 #ifdef __APPLE__
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"
86 #else
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"
91 #endif
92 " \n"
93 "// Mesa intel gen4 generates \"unsupported IR in fragment shader 13\" for\n"
94 "// sqrt, let's work around. \n"
95 "float \n"
96 "sqrt_hack(float f2) \n"
97 "{ \n"
98 " vec3 v = vec3(f2,0.0,0.0); \n"
99 " return length(v); \n"
100 "} \n"
101 " \n"
102 "void \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"
107 "{ \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"
113 " vec3 n = -d; \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"
119 " if(e > r) \n"
120 " { \n"
121 " // no intersection \n"
122 " return; \n"
123 " } \n"
124 " \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"
130 "#else \n"
131 " float f = sqrt_hack(r*r - e*e); \n"
132 "#endif \n"
133 " float dist = t - f; \n"
134 " if(dist < 0.0) \n"
135 " { \n"
136 " // inside the sphere \n"
137 " return; \n"
138 " } \n"
139 " \n"
140 " if(dist < EPSILON) \n"
141 " return; \n"
142 " \n"
143 " if(dist > isec.t) \n"
144 " return; \n"
145 " \n"
146 " isec.t = dist; \n"
147 " isec.idx = idx; \n"
148 " \n"
149 " isec.hit = ray.orig + ray.dir * isec.t; \n"
150 " isec.n = (isec.hit - c) / r; \n"
151 "} \n"
152 " \n"
153 "Isec \n"
154 "intersect(const in Ray ray, \n"
155 " const in float max_t /*= INF*/) \n"
156 "{ \n"
157 " Isec nearest; \n"
158 " nearest.t = max_t; \n"
159 " nearest.idx = -1; \n"
160 " \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"
165 " \n"
166 " return nearest; \n"
167 "} \n"
168 " \n"
169 "vec4 \n"
170 "idx2color(const in int idx) \n"
171 "{ \n"
172 " vec4 diff; \n"
173 " if(idx == 0) \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"
181 " return diff; \n"
182 "} \n"
183 " \n"
184 "vec4 \n"
185 "trace0(const in Ray ray) \n"
186 "{ \n"
187 " Isec isec = intersect(ray, INF); \n"
188 " \n"
189 " if(isec.idx == -1) \n"
190 " { \n"
191 " return backgroundColor; \n"
192 " } \n"
193 " \n"
194 " vec4 diff = idx2color(isec.idx); \n"
195 " \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"
201 "} \n"
202 " \n"
203 "vec4 \n"
204 "trace1(const in Ray ray) \n"
205 "{ \n"
206 " Isec isec = intersect(ray, INF); \n"
207 " \n"
208 " if(isec.idx == -1) \n"
209 " { \n"
210 " return backgroundColor; \n"
211 " } \n"
212 " \n"
213 " Ray reflRay = Ray(isec.hit, reflect(ray.dir, isec.n)); \n"
214 " \n"
215 " vec4 reflCol = trace0(reflRay); \n"
216 " \n"
217 " vec4 diff = idx2color(isec.idx) + reflCol; \n"
218 " \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"
224 "} \n"
225 " \n"
226 "void main() \n"
227 "{ \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"
232 "}\n";
234 enum piglit_result
235 piglit_display(void)
237 int passed_cnt = 0;
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;
242 int x,y;
244 glUseProgram(program);
245 glUniformMatrix3fv(location, 1, 0, rot);
247 glBegin(GL_QUADS);
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);
254 glEnd();
255 glUseProgram(0);
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++)
268 float color[3];
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))
275 passed_cnt++;
279 free(pixels);
281 piglit_present_results();
283 return ((float)passed_cnt > (1.0F-failing_pixel_percentage)
284 *piglit_width*piglit_height)
285 ? PIGLIT_PASS : PIGLIT_FAIL;
288 void
289 piglit_init(int argc, char **argv)
291 GLint vs=-1, fs=-1;
293 glDisable(GL_DEPTH_TEST);
295 piglit_require_gl_version(20);
297 glViewport(0, 0, piglit_width, piglit_height);
298 glMatrixMode(GL_PROJECTION);
299 glLoadIdentity();
300 glOrtho(-10, 10, -10, 10, -1, 1);
301 glMatrixMode(GL_MODELVIEW);
302 glLoadIdentity();
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);