clray
view rt.cl @ 35:7d77ded5f890
stopped using a heap to flatten the kdtree. added explicit left/right indices
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 26 Aug 2010 20:24:07 +0100 |
parents | 92786fc3317e |
children | 980bc07be868 |
line source
1 /* vim: set ft=opencl:ts=4:sw=4 */
3 struct RendInfo {
4 float4 ambient;
5 int xsz, ysz;
6 int num_faces, num_lights;
7 int max_iter;
8 int kd_depth;
9 };
11 struct Vertex {
12 float4 pos;
13 float4 normal;
14 float4 tex;
15 float4 padding;
16 };
18 struct Face {
19 struct Vertex v[3];
20 float4 normal;
21 int matid;
22 int padding[3];
23 };
25 struct Material {
26 float4 kd, ks;
27 float kr, kt;
28 float spow;
29 float padding;
30 };
32 struct Light {
33 float4 pos, color;
34 };
36 struct Ray {
37 float4 origin, dir;
38 };
40 struct SurfPoint {
41 float t;
42 float4 pos, norm, dbg;
43 global const struct Face *obj;
44 struct Material mat;
45 };
47 struct Scene {
48 float4 ambient;
49 global const struct Face *faces;
50 int num_faces;
51 global const struct Light *lights;
52 int num_lights;
53 global const struct Material *matlib;
54 global const struct KDNode *kdtree;
55 };
57 struct AABBox {
58 float4 min, max;
59 };
61 struct KDNode {
62 struct AABBox aabb;
63 int face_idx[32];
64 int num_faces;
65 int left, right;
66 int padding;
67 };
69 #define MIN_ENERGY 0.001
70 #define EPSILON 1e-5
72 float4 shade(struct Ray ray, struct Scene *scn, const struct SurfPoint *sp);
73 bool find_intersection(struct Ray ray, const struct Scene *scn, struct SurfPoint *sp);
74 bool intersect(struct Ray ray, global const struct Face *face, struct SurfPoint *sp);
75 bool intersect_aabb(struct Ray ray, struct AABBox aabb);
77 float4 reflect(float4 v, float4 n);
78 float4 transform(float4 v, global const float *xform);
79 void transform_ray(struct Ray *ray, global const float *xform, global const float *invtrans);
80 float4 calc_bary(float4 pt, global const struct Face *face, float4 norm);
81 float mean(float4 v);
83 kernel void render(global float4 *fb,
84 global const struct RendInfo *rinf,
85 global const struct Face *faces,
86 global const struct Material *matlib,
87 global const struct Light *lights,
88 global const struct Ray *primrays,
89 global const float *xform,
90 global const float *invtrans,
91 global const struct KDNode *kdtree)
92 {
93 int idx = get_global_id(0);
95 struct Scene scn;
96 scn.ambient = rinf->ambient;
97 scn.faces = faces;
98 scn.num_faces = rinf->num_faces;
99 scn.lights = lights;
100 scn.num_lights = rinf->num_lights;
101 scn.matlib = matlib;
102 scn.kdtree = kdtree;
104 struct Ray ray = primrays[idx];
105 transform_ray(&ray, xform, invtrans);
107 float4 pixel = (float4)(0, 0, 0, 0);
108 float4 energy = (float4)(1.0, 1.0, 1.0, 0.0);
109 int iter = 0;
111 while(iter++ < rinf->max_iter && mean(energy) > MIN_ENERGY) {
112 struct SurfPoint sp;
113 if(find_intersection(ray, &scn, &sp)) {
114 pixel += shade(ray, &scn, &sp) * energy;
116 float4 refl_col = sp.mat.ks * sp.mat.kr;
118 ray.origin = sp.pos;
119 ray.dir = reflect(-ray.dir, sp.norm);
121 energy *= refl_col;
122 } else {
123 break;
124 }
125 }
127 fb[idx] = pixel;
128 }
130 float4 shade(struct Ray ray, struct Scene *scn, const struct SurfPoint *sp)
131 {
132 float4 norm = sp->norm;
133 bool entering = true;
135 if(dot(ray.dir, norm) >= 0.0) {
136 norm = -norm;
137 entering = false;
138 }
140 float4 dcol = scn->ambient * sp->mat.kd;
141 float4 scol = (float4)(0, 0, 0, 0);
143 for(int i=0; i<scn->num_lights; i++) {
144 float4 ldir = scn->lights[i].pos - sp->pos;
146 struct Ray shadowray;
147 shadowray.origin = sp->pos;
148 shadowray.dir = ldir;
150 if(!find_intersection(shadowray, scn, 0)) {
151 ldir = normalize(ldir);
152 float4 vdir = -normalize(ray.dir);
153 float4 vref = reflect(vdir, norm);
155 float diff = fmax(dot(ldir, norm), 0.0f);
156 dcol += sp->mat.kd * scn->lights[i].color * diff;
158 float spec = powr(fmax(dot(ldir, vref), 0.0f), sp->mat.spow);
159 scol += sp->mat.ks * scn->lights[i].color * spec;
160 }
161 }
163 return dcol + scol;
164 }
166 #define STACK_SIZE 64
167 bool find_intersection(struct Ray ray, const struct Scene *scn, struct SurfPoint *spres)
168 {
169 struct SurfPoint sp0;
170 sp0.t = 1.0;
171 sp0.obj = 0;
173 int idxstack[STACK_SIZE];
174 int top = 0; // points after the topmost element of the stack
175 idxstack[top++] = 0; // root at tree[0]
177 while(top > 0) {
178 int idx = idxstack[--top]; // remove this index from the stack and process it
180 global const struct KDNode *node = scn->kdtree + idx;
182 /*if(get_global_id(0) == 0) {
183 for(int i=0; i<top+1; i++) {
184 printf(" ");
185 }
186 printf("(%d) idx: %d (%p) num_faces: %d\n", top+1, idx, node, node->num_faces);
187 }*/
189 if(intersect_aabb(ray, node->aabb)) {
190 if(node->left == -1) {
191 // leaf node... check each face in turn and update the nearest intersection as needed
192 for(int i=0; i<node->num_faces; i++) {
193 struct SurfPoint spt;
194 int fidx = node->face_idx[i];
196 if(intersect(ray, scn->faces + fidx, &spt) && spt.t < sp0.t) {
197 sp0 = spt;
198 }
199 }
200 } else {
201 // internal node... recurse to the children
202 /*if(get_global_id(0) == 0) {
203 printf("pushing %d's children %d and %d\n", idx, node->left, node->right);
204 }*/
205 idxstack[top++] = node->left;
206 idxstack[top++] = node->right;
207 }
208 }
209 }
211 if(!sp0.obj) {
212 return false;
213 }
215 if(spres) {
216 *spres = sp0;
217 spres->mat = scn->matlib[sp0.obj->matid];
218 }
219 return true;
220 }
222 /*bool find_intersection(struct Ray ray, const struct Scene *scn, struct SurfPoint *spres)
223 {
224 struct SurfPoint sp, sp0;
225 sp0.t = 1.0;
226 sp0.obj = 0;
228 for(int i=0; i<scn->num_faces; i++) {
229 if(intersect(ray, scn->faces + i, &sp) && sp.t < sp0.t) {
230 sp0 = sp;
231 }
232 }
234 if(!sp0.obj) {
235 return false;
236 }
238 if(spres) {
239 *spres = sp0;
240 spres->mat = scn->matlib[sp0.obj->matid];
241 }
242 return true;
243 }*/
245 bool intersect(struct Ray ray, global const struct Face *face, struct SurfPoint *sp)
246 {
247 float4 origin = ray.origin;
248 float4 dir = ray.dir;
249 float4 norm = face->normal;
251 float ndotdir = dot(dir, norm);
253 if(fabs(ndotdir) <= EPSILON) {
254 return false;
255 }
257 float4 pt = face->v[0].pos;
258 float4 vec = pt - origin;
260 float ndotvec = dot(norm, vec);
261 float t = ndotvec / ndotdir;
263 if(t < EPSILON || t > 1.0) {
264 return false;
265 }
266 pt = origin + dir * t;
269 float4 bc = calc_bary(pt, face, norm);
270 float bc_sum = bc.x + bc.y + bc.z;
272 if(bc_sum < 1.0 - EPSILON || bc_sum > 1.0 + EPSILON) {
273 return false;
274 bc *= 1.2;
275 }
277 sp->t = t;
278 sp->pos = pt;
279 sp->norm = normalize(face->v[0].normal * bc.x + face->v[1].normal * bc.y + face->v[2].normal * bc.z);
280 sp->obj = face;
281 sp->dbg = bc;
282 return true;
283 }
285 bool intersect_aabb(struct Ray ray, struct AABBox aabb)
286 {
287 if(ray.origin.x >= aabb.min.x && ray.origin.y >= aabb.min.y && ray.origin.z >= aabb.min.z &&
288 ray.origin.x < aabb.max.x && ray.origin.y < aabb.max.y && ray.origin.z < aabb.max.z) {
289 return true;
290 }
292 float4 bbox[2] = {
293 aabb.min.x, aabb.min.y, aabb.min.z, 0,
294 aabb.max.x, aabb.max.y, aabb.max.z, 0
295 };
297 int xsign = (int)(ray.dir.x < 0.0);
298 float invdirx = 1.0 / ray.dir.x;
299 float tmin = (bbox[xsign].x - ray.origin.x) * invdirx;
300 float tmax = (bbox[1 - xsign].x - ray.origin.x) * invdirx;
302 int ysign = (int)(ray.dir.y < 0.0);
303 float invdiry = 1.0 / ray.dir.y;
304 float tymin = (bbox[ysign].y - ray.origin.y) * invdiry;
305 float tymax = (bbox[1 - ysign].y - ray.origin.y) * invdiry;
307 if(tmin > tymax || tymin > tmax) {
308 return false;
309 }
311 if(tymin > tmin) tmin = tymin;
312 if(tymax < tmax) tmax = tymax;
314 int zsign = (int)(ray.dir.z < 0.0);
315 float invdirz = 1.0 / ray.dir.z;
316 float tzmin = (bbox[zsign].z - ray.origin.z) * invdirz;
317 float tzmax = (bbox[1 - zsign].z - ray.origin.z) * invdirz;
319 if(tmin > tzmax || tzmin > tmax) {
320 return false;
321 }
323 return tmin < 1.0 && tmax > 0.0;
324 }
326 float4 reflect(float4 v, float4 n)
327 {
328 return 2.0f * dot(v, n) * n - v;
329 }
331 float4 transform(float4 v, global const float *xform)
332 {
333 float4 res;
334 res.x = v.x * xform[0] + v.y * xform[4] + v.z * xform[8] + xform[12];
335 res.y = v.x * xform[1] + v.y * xform[5] + v.z * xform[9] + xform[13];
336 res.z = v.x * xform[2] + v.y * xform[6] + v.z * xform[10] + xform[14];
337 res.w = 0.0;
338 return res;
339 }
341 void transform_ray(struct Ray *ray, global const float *xform, global const float *invtrans)
342 {
343 ray->origin = transform(ray->origin, xform);
344 ray->dir = transform(ray->dir, invtrans);
345 }
347 float4 calc_bary(float4 pt, global const struct Face *face, float4 norm)
348 {
349 float4 bc = (float4)(0, 0, 0, 0);
351 // calculate area of the whole triangle
352 float4 v1 = face->v[1].pos - face->v[0].pos;
353 float4 v2 = face->v[2].pos - face->v[0].pos;
354 float4 xv1v2 = cross(v1, v2);
356 float area = fabs(dot(xv1v2, norm)) * 0.5;
357 if(area < EPSILON) {
358 return bc;
359 }
361 float4 pv0 = face->v[0].pos - pt;
362 float4 pv1 = face->v[1].pos - pt;
363 float4 pv2 = face->v[2].pos - pt;
365 // calculate the area of each sub-triangle
366 float4 x12 = cross(pv1, pv2);
367 float4 x20 = cross(pv2, pv0);
368 float4 x01 = cross(pv0, pv1);
370 float a0 = fabs(dot(x12, norm)) * 0.5;
371 float a1 = fabs(dot(x20, norm)) * 0.5;
372 float a2 = fabs(dot(x01, norm)) * 0.5;
374 bc.x = a0 / area;
375 bc.y = a1 / area;
376 bc.z = a2 / area;
377 return bc;
378 }
380 float mean(float4 v)
381 {
382 return native_divide(v.x + v.y + v.z, 3.0);
383 }