clray

view rt.cl @ 16:9e4a28063394

cl compiler segfaults...
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 09 Aug 2010 04:18:21 +0100
parents 754faf15ba36
children 074a64b9d6bd
line source
1 /* vim: set ft=opencl:ts=4:sw=4 */
3 struct RendInfo {
4 int xsz, ysz;
5 int num_faces, num_lights;
6 int max_iter;
7 float4 ambient;
8 int dbg;
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 float energy, pad[3];
39 };
41 struct SurfPoint {
42 float t;
43 float4 pos, norm, dbg;
44 global const struct Face *obj;
45 global const 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 };
57 #define MIN_ENERGY 0.001
58 #define EPSILON 1e-6
60 float4 trace(struct Ray ray, struct Scene *scn);
61 float4 shade(struct Ray ray, struct Scene *scn, const struct SurfPoint *sp);
62 bool find_intersection(struct Ray ray, const struct Scene *scn, struct SurfPoint *sp);
63 bool intersect(struct Ray ray, global const struct Face *face, struct SurfPoint *sp);
65 float4 reflect(float4 v, float4 n);
66 float4 transform(float4 v, global const float *xform);
67 void transform_ray(struct Ray *ray, global const float *xform, global const float *invtrans);
68 float4 calc_bary(float4 pt, global const struct Face *face, float4 norm);
70 kernel void render(global float4 *fb,
71 global const struct RendInfo *rinf,
72 global const struct Face *faces,
73 global const struct Material *matlib,
74 global const struct Light *lights,
75 global const struct Ray *primrays,
76 global const float *xform,
77 global const float *invtrans,
78 global struct Face *outfaces)
79 {
80 int idx = get_global_id(0);
82 struct Scene scn;
83 scn.ambient = rinf->ambient;
84 scn.faces = faces;
85 scn.num_faces = rinf->num_faces;
86 scn.lights = lights;
87 scn.num_lights = rinf->num_lights;
88 scn.matlib = matlib;
90 struct Ray ray = primrays[idx];
91 transform_ray(&ray, xform, invtrans);
93 fb[idx] = trace(ray, &scn);
94 }
96 float4 trace(struct Ray ray, struct Scene *scn)
97 {
98 float4 color;
99 struct SurfPoint sp;
101 if(find_intersection(ray, scn, &sp)) {
102 color = shade(ray, scn, &sp);
103 } else {
104 color = (float4)(0, 0, 0, 0);
105 }
106 return color;
107 }
109 float4 shade(struct Ray ray, struct Scene *scn, const struct SurfPoint *sp)
110 {
111 float4 norm = sp->norm;
112 bool entering = true;
113 struct Material mat = *sp->mat;
115 if(dot(ray.dir, norm) >= 0.0) {
116 norm = -norm;
117 entering = false;
118 }
120 float4 dcol = scn->ambient * mat.kd;
121 float4 scol = (float4)(0, 0, 0, 0);
123 for(int i=0; i<scn->num_lights; i++) {
124 float4 ldir = scn->lights[i].pos - sp->pos;
126 struct Ray shadowray;
127 shadowray.origin = sp->pos;
128 shadowray.dir = ldir;
130 if(!find_intersection(shadowray, scn, 0)) {
131 ldir = normalize(ldir);
132 float4 vdir = -normalize(ray.dir);
133 float4 vref = reflect(vdir, norm);
135 float diff = fmax(dot(ldir, norm), 0.0f);
136 dcol += mat.kd * diff * scn->lights[i].color;
138 //float spec = powr(fmax(dot(ldir, vref), 0.0f), mat.spow);
139 //scol += mat.ks * spec * scn->lights[i].color;
140 }
141 }
143 float4 refl_col = mat.ks * mat.kr;
144 float refl_coeff = (refl_col.x + refl_col.y + refl_col.z) / 3.0;
146 if(refl_coeff > MIN_ENERGY) {
147 struct Ray refl_ray;
148 refl_ray.origin = sp->pos;
149 refl_ray.dir = reflect(-ray.dir, norm);
150 refl_ray.energy *= refl_coeff;
152 scol += trace(refl_ray, scn) * refl_col;
153 }
155 return dcol + scol;
156 }
159 bool find_intersection(struct Ray ray, const struct Scene *scn, struct SurfPoint *spres)
160 {
161 struct SurfPoint sp, sp0;
162 sp0.t = 1.0;
163 sp0.obj = 0;
165 for(int i=0; i<scn->num_faces; i++) {
166 if(intersect(ray, scn->faces + i, &sp) && sp.t < sp0.t) {
167 sp0 = sp;
168 }
169 }
171 if(!sp0.obj) {
172 return false;
173 }
175 if(spres) {
176 *spres = sp0;
177 spres->mat = scn->matlib + sp0.obj->matid;
178 }
179 return true;
180 }
182 bool intersect(struct Ray ray, global const struct Face *face, struct SurfPoint *sp)
183 {
184 float4 origin = ray.origin;
185 float4 dir = ray.dir;
186 float4 norm = face->normal;
188 float ndotdir = dot(dir, norm);
190 if(fabs(ndotdir) <= EPSILON) {
191 return false;
192 }
194 float4 pt = face->v[0].pos;
195 float4 vec = pt - origin;
197 float ndotvec = dot(norm, vec);
198 float t = ndotvec / ndotdir;
200 if(t < EPSILON || t > 1.0) {
201 return false;
202 }
203 pt = origin + dir * t;
206 float4 bc = calc_bary(pt, face, norm);
207 float bc_sum = bc.x + bc.y + bc.z;
209 if(bc_sum < 0.0 || bc_sum > 1.0 + EPSILON) {
210 return false;
211 bc *= 1.2;
212 }
214 sp->t = t;
215 sp->pos = pt;
216 sp->norm = norm;
217 sp->obj = face;
218 sp->dbg = bc;
219 return true;
220 }
222 float4 reflect(float4 v, float4 n)
223 {
224 float4 res = 2.0f * dot(v, n) * n - v;
225 return res;
226 }
228 float4 transform(float4 v, global const float *xform)
229 {
230 float4 res;
231 res.x = v.x * xform[0] + v.y * xform[4] + v.z * xform[8] + xform[12];
232 res.y = v.x * xform[1] + v.y * xform[5] + v.z * xform[9] + xform[13];
233 res.z = v.x * xform[2] + v.y * xform[6] + v.z * xform[10] + xform[14];
234 res.w = 0.0;
235 return res;
236 }
238 void transform_ray(struct Ray *ray, global const float *xform, global const float *invtrans)
239 {
240 ray->origin = transform(ray->origin, xform);
241 ray->dir = transform(ray->dir, invtrans);
242 }
244 float4 calc_bary(float4 pt, global const struct Face *face, float4 norm)
245 {
246 float4 bc = (float4)(0, 0, 0, 0);
248 // calculate area of the whole triangle
249 float4 v1 = face->v[1].pos - face->v[0].pos;
250 float4 v2 = face->v[2].pos - face->v[0].pos;
251 float4 xv1v2 = cross(v1, v2);
253 float area = fabs(dot(xv1v2, norm)) * 0.5;
254 if(area < EPSILON) {
255 return bc;
256 }
258 float4 pv0 = face->v[0].pos - pt;
259 float4 pv1 = face->v[1].pos - pt;
260 float4 pv2 = face->v[2].pos - pt;
262 // calculate the area of each sub-triangle
263 float4 x12 = cross(pv1, pv2);
264 float4 x20 = cross(pv2, pv0);
265 float4 x01 = cross(pv0, pv1);
267 float a0 = fabs(dot(x12, norm)) * 0.5;
268 float a1 = fabs(dot(x20, norm)) * 0.5;
269 float a2 = fabs(dot(x01, norm)) * 0.5;
271 bc.x = a0 / area;
272 bc.y = a1 / area;
273 bc.z = a2 / area;
274 return bc;
275 }