clray

view src/rt.cl @ 62:d9520da6b801

minor readme fix
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 28 Dec 2015 10:31:58 +0200
parents 30bf84881553
children
line source
1 /* vim: set ft=opencl:ts=4:sw=4 */
2 #include "common.h"
4 struct RendInfo {
5 float4 ambient;
6 int xsz, ysz;
7 int num_faces, num_lights;
8 int max_iter;
9 int cast_shadows;
10 };
12 struct Vertex {
13 float4 pos;
14 float4 normal;
15 float4 tex;
16 float4 padding;
17 };
19 struct Face {
20 struct Vertex v[3];
21 float4 normal;
22 int matid;
23 int padding[3];
24 };
26 struct Material {
27 float4 kd, ks;
28 float kr, kt;
29 float spow;
30 float padding;
31 };
33 struct Light {
34 float4 pos, color;
35 };
37 struct Ray {
38 float4 origin, dir;
39 };
41 struct SurfPoint {
42 float t;
43 float4 pos, norm, dbg;
44 global const struct Face *obj;
45 struct Material mat;
46 };
48 struct Scene {
49 float4 ambient;
50 global const struct Face *faces;
51 int num_faces;
52 global const struct Light *lights;
53 int num_lights;
54 global const struct Material *matlib;
55 //global const struct KDNode *kdtree;
56 bool cast_shadows;
57 };
59 struct AABBox {
60 float4 min, max;
61 };
63 struct KDNode {
64 struct AABBox aabb;
65 int face_idx[MAX_NODE_FACES];
66 int num_faces;
67 int left, right;
68 int padding;
69 };
71 #define MIN_ENERGY 0.001
73 float4 shade(struct Ray ray, struct Scene *scn, const struct SurfPoint *sp, read_only image2d_t kdimg);
74 bool find_intersection(struct Ray ray, const struct Scene *scn, struct SurfPoint *sp, read_only image2d_t kdimg);
75 bool intersect(struct Ray ray, global const struct Face *face, struct SurfPoint *sp);
76 bool intersect_aabb(struct Ray ray, struct AABBox aabb);
78 float4 reflect(float4 v, float4 n);
79 float4 transform(float4 v, global const float *xform);
80 void transform_ray(struct Ray *ray, global const float *xform, global const float *invtrans);
81 float4 calc_bary(float4 pt, global const struct Face *face, float4 norm);
82 float mean(float4 v);
84 void read_kdnode(int idx, struct KDNode *node, read_only image2d_t kdimg);
87 kernel void render(write_only image2d_t fb,
88 global const struct RendInfo *rinf,
89 global const struct Face *faces,
90 global const struct Material *matlib,
91 global const struct Light *lights,
92 global const struct Ray *primrays,
93 global const float *xform,
94 global const float *invtrans,
95 //global const struct KDNode *kdtree
96 read_only image2d_t kdtree_img)
97 {
98 int idx = get_global_id(0);
100 struct Scene scn;
101 scn.ambient = rinf->ambient;
102 scn.faces = faces;
103 scn.num_faces = rinf->num_faces;
104 scn.lights = lights;
105 scn.num_lights = rinf->num_lights;
106 scn.matlib = matlib;
107 scn.cast_shadows = rinf->cast_shadows;
109 struct Ray ray = primrays[idx];
110 transform_ray(&ray, xform, invtrans);
112 float4 pixel = (float4)(0, 0, 0, 0);
113 float4 energy = (float4)(1.0, 1.0, 1.0, 0.0);
114 int iter = 0;
116 while(iter++ <= rinf->max_iter && mean(energy) > MIN_ENERGY) {
117 struct SurfPoint sp;
118 if(find_intersection(ray, &scn, &sp, kdtree_img)) {
119 pixel += shade(ray, &scn, &sp, kdtree_img) * energy;
121 float4 refl_col = sp.mat.ks * sp.mat.kr;
123 ray.origin = sp.pos;
124 ray.dir = reflect(-ray.dir, sp.norm);
126 energy *= refl_col;
127 } else {
128 energy = (float4)(0.0, 0.0, 0.0, 0.0);
129 }
130 }
132 int2 coord;
133 coord.x = idx % rinf->xsz;
134 coord.y = idx / rinf->xsz;
136 write_imagef(fb, coord, pixel);
137 }
139 float4 shade(struct Ray ray, struct Scene *scn, const struct SurfPoint *sp, read_only image2d_t kdimg)
140 {
141 float4 norm = sp->norm;
142 //bool entering = true;
144 if(dot(ray.dir, norm) >= 0.0) {
145 norm = -norm;
146 //entering = false;
147 }
149 float4 dcol = scn->ambient * sp->mat.kd;
150 float4 scol = (float4)(0, 0, 0, 0);
152 for(int i=0; i<scn->num_lights; i++) {
153 float4 ldir = scn->lights[i].pos - sp->pos;
155 struct Ray shadowray;
156 shadowray.origin = sp->pos;
157 shadowray.dir = ldir;
159 if(!scn->cast_shadows || !find_intersection(shadowray, scn, 0, kdimg)) {
160 ldir = normalize(ldir);
161 float4 vdir = -ray.dir;
162 vdir.x = native_divide(vdir.x, RAY_MAG);
163 vdir.y = native_divide(vdir.y, RAY_MAG);
164 vdir.z = native_divide(vdir.z, RAY_MAG);
165 float4 vref = reflect(vdir, norm);
167 float diff = fmax(dot(ldir, norm), 0.0f);
168 dcol += sp->mat.kd /* scn->lights[i].color*/ * diff;
170 float spec = native_powr(fmax(dot(ldir, vref), 0.0f), sp->mat.spow);
171 scol += sp->mat.ks /* scn->lights[i].color*/ * spec;
172 }
173 }
175 return dcol + scol;
176 }
178 #define STACK_SIZE MAX_TREE_DEPTH
179 bool find_intersection(struct Ray ray, const struct Scene *scn, struct SurfPoint *spres, read_only image2d_t kdimg)
180 {
181 struct SurfPoint sp0;
182 sp0.t = 1.0;
183 sp0.obj = 0;
185 int idxstack[STACK_SIZE];
186 int top = 0; // points after the topmost element of the stack
187 idxstack[top++] = 0; // root at tree[0]
189 while(top > 0) {
190 int idx = idxstack[--top]; // remove this index from the stack and process it
192 struct KDNode node;
193 read_kdnode(idx, &node, kdimg);
195 if(intersect_aabb(ray, node.aabb)) {
196 if(node.left == -1) {
197 // leaf node... check each face in turn and update the nearest intersection as needed
198 for(int i=0; i<node.num_faces; i++) {
199 struct SurfPoint spt;
200 int fidx = node.face_idx[i];
202 if(intersect(ray, scn->faces + fidx, &spt) && spt.t < sp0.t) {
203 sp0 = spt;
204 }
205 }
206 } else {
207 // internal node... recurse to the children
208 idxstack[top++] = node.left;
209 idxstack[top++] = node.right;
210 }
211 }
212 }
214 if(!sp0.obj) {
215 return false;
216 }
218 if(spres) {
219 *spres = sp0;
220 spres->mat = scn->matlib[sp0.obj->matid];
221 }
222 return true;
223 }
225 bool intersect(struct Ray ray, global const struct Face *face, struct SurfPoint *sp)
226 {
227 float4 origin = ray.origin;
228 float4 dir = ray.dir;
229 float4 norm = face->normal;
231 float ndotdir = dot(dir, norm);
233 if(fabs(ndotdir) <= EPSILON) {
234 return false;
235 }
237 float4 pt = face->v[0].pos;
238 float4 vec = pt - origin;
240 float ndotvec = dot(norm, vec);
241 float t = native_divide(ndotvec, ndotdir);
243 if(t < EPSILON || t > 1.0) {
244 return false;
245 }
246 pt = origin + dir * t;
249 float4 bc = calc_bary(pt, face, norm);
250 float bc_sum = bc.x + bc.y + bc.z;
252 if(bc_sum < 1.0 - EPSILON || bc_sum > 1.0 + EPSILON) {
253 return false;
254 }
256 sp->t = t;
257 sp->pos = pt;
258 sp->norm = normalize(face->v[0].normal * bc.x + face->v[1].normal * bc.y + face->v[2].normal * bc.z);
259 sp->obj = face;
260 sp->dbg = bc;
261 return true;
262 }
264 bool intersect_aabb(struct Ray ray, struct AABBox aabb)
265 {
266 if(ray.origin.x >= aabb.min.x && ray.origin.y >= aabb.min.y && ray.origin.z >= aabb.min.z &&
267 ray.origin.x < aabb.max.x && ray.origin.y < aabb.max.y && ray.origin.z < aabb.max.z) {
268 return true;
269 }
271 float4 bbox[2] = {
272 aabb.min.x, aabb.min.y, aabb.min.z, 0,
273 aabb.max.x, aabb.max.y, aabb.max.z, 0
274 };
276 int xsign = (int)(ray.dir.x < 0.0);
277 float invdirx = native_recip(ray.dir.x);
278 float tmin = (bbox[xsign].x - ray.origin.x) * invdirx;
279 float tmax = (bbox[1 - xsign].x - ray.origin.x) * invdirx;
281 int ysign = (int)(ray.dir.y < 0.0);
282 float invdiry = native_recip(ray.dir.y);
283 float tymin = (bbox[ysign].y - ray.origin.y) * invdiry;
284 float tymax = (bbox[1 - ysign].y - ray.origin.y) * invdiry;
286 if(tmin > tymax || tymin > tmax) {
287 return false;
288 }
290 if(tymin > tmin) tmin = tymin;
291 if(tymax < tmax) tmax = tymax;
293 int zsign = (int)(ray.dir.z < 0.0);
294 float invdirz = native_recip(ray.dir.z);
295 float tzmin = (bbox[zsign].z - ray.origin.z) * invdirz;
296 float tzmax = (bbox[1 - zsign].z - ray.origin.z) * invdirz;
298 if(tmin > tzmax || tzmin > tmax) {
299 return false;
300 }
302 return tmin < 1.0 && tmax > 0.0;
303 }
305 float4 reflect(float4 v, float4 n)
306 {
307 return 2.0f * dot(v, n) * n - v;
308 }
310 float4 transform(float4 v, global const float *xform)
311 {
312 float4 res;
313 res.x = v.x * xform[0] + v.y * xform[4] + v.z * xform[8] + xform[12];
314 res.y = v.x * xform[1] + v.y * xform[5] + v.z * xform[9] + xform[13];
315 res.z = v.x * xform[2] + v.y * xform[6] + v.z * xform[10] + xform[14];
316 res.w = 0.0;
317 return res;
318 }
320 void transform_ray(struct Ray *ray, global const float *xform, global const float *invtrans)
321 {
322 ray->origin = transform(ray->origin, xform);
323 ray->dir = transform(ray->dir, invtrans);
324 }
326 float4 calc_bary(float4 pt, global const struct Face *face, float4 norm)
327 {
328 float4 bc = (float4)(0, 0, 0, 0);
330 // calculate area of the whole triangle
331 float4 v1 = face->v[1].pos - face->v[0].pos;
332 float4 v2 = face->v[2].pos - face->v[0].pos;
333 float4 xv1v2 = cross(v1, v2);
335 float area = fabs(dot(xv1v2, norm)) * 0.5;
336 if(area < EPSILON) {
337 return bc;
338 }
340 float4 pv0 = face->v[0].pos - pt;
341 float4 pv1 = face->v[1].pos - pt;
342 float4 pv2 = face->v[2].pos - pt;
344 // calculate the area of each sub-triangle
345 float4 x12 = cross(pv1, pv2);
346 float4 x20 = cross(pv2, pv0);
347 float4 x01 = cross(pv0, pv1);
349 float a0 = fabs(dot(x12, norm)) * 0.5;
350 float a1 = fabs(dot(x20, norm)) * 0.5;
351 float a2 = fabs(dot(x01, norm)) * 0.5;
353 bc.x = native_divide(a0, area);
354 bc.y = native_divide(a1, area);
355 bc.z = native_divide(a2, area);
356 return bc;
357 }
359 float mean(float4 v)
360 {
361 return native_divide(v.x + v.y + v.z, 3.0);
362 }
365 const sampler_t kdsampler = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_NONE | CLK_FILTER_NEAREST;
367 // read a KD-tree node from a texture scanline
368 void read_kdnode(int idx, struct KDNode *node, read_only image2d_t kdimg)
369 {
370 int startx = KDIMG_NODE_WIDTH * (idx / KDIMG_MAX_HEIGHT);
372 int2 tc;
373 tc.x = startx;
374 tc.y = idx % KDIMG_MAX_HEIGHT;
376 node->aabb.min = read_imagef(kdimg, kdsampler, tc); tc.x++;
377 node->aabb.max = read_imagef(kdimg, kdsampler, tc);
379 tc.x = startx + 2 + MAX_NODE_FACES / 4;
380 float4 pix = read_imagef(kdimg, kdsampler, tc);
381 node->num_faces = (int)pix.x;
382 node->left = (int)pix.y;
383 node->right = (int)pix.z;
385 tc.x = startx + 2;
386 for(int i=0; i<node->num_faces; i+=4) {
387 float4 pix = read_imagef(kdimg, kdsampler, tc); tc.x++;
388 node->face_idx[i] = (int)pix.x;
389 node->face_idx[i + 1] = (int)pix.y;
390 node->face_idx[i + 2] = (int)pix.z;
391 node->face_idx[i + 3] = (int)pix.w;
392 }
393 }