cmake: move defaults into the per-platform section
[piglit.git] / tests / shaders / glsl-vs-raytrace-bug26691.c
blob6b5cb844b0bf31430f5c153d4e93e9fdf1fb8f17
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-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"
48 " \n"
49 "uniform mat3 rot; \n"
50 " \n"
51 "struct Ray \n"
52 "{ \n"
53 "vec3 orig; \n"
54 "vec3 dir; \n"
55 "}; \n"
56 " \n"
57 "struct Sphere \n"
58 "{ \n"
59 " vec3 c; \n"
60 " float r; \n"
61 "}; \n"
62 " \n"
63 "struct Isec \n"
64 "{ \n"
65 " float t; \n"
66 " int idx; \n"
67 " vec3 hit; \n"
68 " vec3 n; \n"
69 "}; \n"
70 " \n"
71 #ifdef __APPLE__
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"
76 #else
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"
81 #endif
82 " \n"
83 "// Mesa intel gen4 generates \"unsupported IR in fragment shader 13\" for\n"
84 "// sqrt, let's work around. \n"
85 "float \n"
86 "sqrt_hack(float f2) \n"
87 "{ \n"
88 " vec3 v = vec3(f2,0.0,0.0); \n"
89 " return length(v); \n"
90 "} \n"
91 " \n"
92 "void \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"
97 "{ \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"
103 " vec3 n = -d; \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"
109 " if(e > r) \n"
110 " { \n"
111 " // no intersection \n"
112 " return; \n"
113 " } \n"
114 " \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"
120 "#else \n"
121 " float f = sqrt_hack(r*r - e*e); \n"
122 "#endif \n"
123 " float dist = t - f; \n"
124 " if(dist < 0.0) \n"
125 " { \n"
126 " // inside the sphere \n"
127 " return; \n"
128 " } \n"
129 " \n"
130 " if(dist < EPSILON) \n"
131 " return; \n"
132 " \n"
133 " if(dist > isec.t) \n"
134 " return; \n"
135 " \n"
136 " isec.t = dist; \n"
137 " isec.idx = idx; \n"
138 " \n"
139 " isec.hit = ray.orig + ray.dir * isec.t; \n"
140 " isec.n = (isec.hit - c) / r; \n"
141 "} \n"
142 " \n"
143 "Isec \n"
144 "intersect(const in Ray ray, \n"
145 " const in float max_t /*= INF*/) \n"
146 "{ \n"
147 " Isec nearest; \n"
148 " nearest.t = max_t; \n"
149 " nearest.idx = -1; \n"
150 " \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"
155 " \n"
156 " return nearest; \n"
157 "} \n"
158 " \n"
159 "vec4 \n"
160 "idx2color(const in int idx) \n"
161 "{ \n"
162 " vec4 diff; \n"
163 " if(idx == 0) \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"
171 " return diff; \n"
172 "} \n"
173 " \n"
174 "vec4 \n"
175 "trace0(const in Ray ray) \n"
176 "{ \n"
177 " Isec isec = intersect(ray, INF); \n"
178 " \n"
179 " if(isec.idx == -1) \n"
180 " { \n"
181 " return backgroundColor; \n"
182 " } \n"
183 " \n"
184 " vec4 diff = idx2color(isec.idx); \n"
185 " \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"
191 "} \n"
192 " \n"
193 "vec4 \n"
194 "trace1(const in Ray ray) \n"
195 "{ \n"
196 " Isec isec = intersect(ray, INF); \n"
197 " \n"
198 " if(isec.idx == -1) \n"
199 " { \n"
200 " return backgroundColor; \n"
201 " } \n"
202 " \n"
203 " Ray reflRay = Ray(isec.hit, reflect(ray.dir, isec.n)); \n"
204 " \n"
205 " vec4 reflCol = trace0(reflRay); \n"
206 " \n"
207 " vec4 diff = idx2color(isec.idx) + reflCol; \n"
208 " \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"
214 "} \n"
215 " \n"
216 "void main() \n"
217 "{ \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"
223 "}\n";
225 enum piglit_result
226 piglit_display(void)
228 int passed_cnt = 0;
229 const float w = 0.5F * piglit_width;
230 const float h = 0.5F * piglit_height;
231 int x,y;
232 GLint location = glGetUniformLocation(program, "rot");
234 glUseProgram(program);
235 glUniformMatrix3fv(location, 1, 0, rot);
236 glBegin(GL_POINTS);
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);
246 glEnd();
247 glUseProgram(0);
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++)
260 float color[3];
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))
267 passed_cnt++;
271 free(pixels);
273 piglit_present_results();
275 return ((float)passed_cnt > (1.0F-failing_pixel_percentage)
276 *piglit_width*piglit_height)
277 ? PIGLIT_PASS : PIGLIT_FAIL;
280 void
281 piglit_init(int argc, char **argv)
283 GLint vs=-1;
285 glDisable(GL_DEPTH_TEST);
287 piglit_require_gl_version(20);
289 glViewport(0, 0, piglit_width, piglit_height);
290 glMatrixMode(GL_PROJECTION);
291 glLoadIdentity();
292 glMatrixMode(GL_MODELVIEW);
293 glLoadIdentity();
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);