rev |
line source |
nuclear@2
|
1 struct RendInfo {
|
nuclear@2
|
2 int xsz, ysz;
|
nuclear@3
|
3 int num_sph, num_lights;
|
nuclear@2
|
4 int max_iter;
|
nuclear@2
|
5 };
|
nuclear@2
|
6
|
nuclear@2
|
7 struct Sphere {
|
nuclear@2
|
8 float4 pos;
|
nuclear@5
|
9 float4 kd, ks;
|
nuclear@2
|
10 float radius;
|
nuclear@4
|
11 float spow, kr, kt;
|
nuclear@2
|
12 };
|
nuclear@2
|
13
|
nuclear@3
|
14 struct Light {
|
nuclear@3
|
15 float4 pos, color;
|
nuclear@3
|
16 };
|
nuclear@3
|
17
|
nuclear@2
|
18 struct Ray {
|
nuclear@2
|
19 float4 origin, dir;
|
nuclear@2
|
20 };
|
nuclear@2
|
21
|
nuclear@2
|
22 struct SurfPoint {
|
nuclear@2
|
23 float t;
|
nuclear@8
|
24 float4 pos, norm;
|
nuclear@4
|
25 global const struct Sphere *obj;
|
nuclear@2
|
26 };
|
nuclear@2
|
27
|
nuclear@2
|
28 #define EPSILON 1e-6
|
nuclear@2
|
29
|
nuclear@5
|
30 float4 shade(struct Ray ray, struct SurfPoint sp,
|
nuclear@5
|
31 global const struct Light *lights, int num_lights);
|
nuclear@4
|
32 bool intersect(struct Ray ray, global const struct Sphere *sph, struct SurfPoint *sp);
|
nuclear@8
|
33 float4 reflect(float4 v, float4 n);
|
nuclear@8
|
34 float4 transform(float4 v, global const float *xform);
|
nuclear@8
|
35 struct Ray transform_ray(global const struct Ray *ray, global const float *xform);
|
nuclear@2
|
36
|
nuclear@4
|
37
|
nuclear@4
|
38 kernel void render(global float4 *fb,
|
nuclear@4
|
39 global const struct RendInfo *rinf,
|
nuclear@4
|
40 global const struct Sphere *sphlist,
|
nuclear@4
|
41 global const struct Light *lights,
|
nuclear@7
|
42 global const struct Ray *primrays,
|
nuclear@8
|
43 global const float *xform)
|
nuclear@2
|
44 {
|
nuclear@2
|
45 int idx = get_global_id(0);
|
nuclear@2
|
46
|
nuclear@8
|
47 struct Ray ray = transform_ray(primrays + idx, xform);
|
nuclear@8
|
48
|
nuclear@4
|
49 struct SurfPoint sp, sp0;
|
nuclear@4
|
50 sp0.t = FLT_MAX;
|
nuclear@5
|
51 sp0.obj = 0;
|
nuclear@4
|
52
|
nuclear@4
|
53 for(int i=0; i<rinf->num_sph; i++) {
|
nuclear@5
|
54 if(intersect(ray, sphlist + i, &sp) && sp.t < sp0.t) {
|
nuclear@4
|
55 sp0 = sp;
|
nuclear@4
|
56 }
|
nuclear@2
|
57 }
|
nuclear@3
|
58
|
nuclear@5
|
59 if(sp0.obj) {
|
nuclear@5
|
60 fb[idx] = shade(ray, sp0, lights, rinf->num_lights);
|
nuclear@5
|
61 } else {
|
nuclear@5
|
62 fb[idx] = (float4)(0, 0, 0, 0);
|
nuclear@5
|
63 }
|
nuclear@4
|
64 }
|
nuclear@4
|
65
|
nuclear@5
|
66 float4 shade(struct Ray ray, struct SurfPoint sp,
|
nuclear@5
|
67 global const struct Light *lights, int num_lights)
|
nuclear@4
|
68 {
|
nuclear@8
|
69 float4 dcol = (float4)(0, 0, 0, 0);
|
nuclear@8
|
70 float4 scol = (float4)(0, 0, 0, 0);
|
nuclear@5
|
71
|
nuclear@5
|
72 for(int i=0; i<num_lights; i++) {
|
nuclear@8
|
73 float4 ldir = normalize(lights[i].pos - sp.pos);
|
nuclear@8
|
74 float4 vdir = -normalize(ray.dir);
|
nuclear@8
|
75 float4 vref = reflect(vdir, sp.norm);
|
nuclear@5
|
76
|
nuclear@5
|
77 float diff = fmax(dot(ldir, sp.norm), 0.0f);
|
nuclear@5
|
78 float spec = powr(fmax(dot(ldir, vref), 0.0f), sp.obj->spow);
|
nuclear@5
|
79
|
nuclear@8
|
80 dcol += sp.obj->kd * diff * lights[i].color;
|
nuclear@8
|
81 scol += sp.obj->ks * spec * lights[i].color;
|
nuclear@5
|
82 }
|
nuclear@5
|
83
|
nuclear@8
|
84 return dcol + scol;
|
nuclear@2
|
85 }
|
nuclear@2
|
86
|
nuclear@2
|
87 bool intersect(struct Ray ray,
|
nuclear@4
|
88 global const struct Sphere *sph,
|
nuclear@2
|
89 struct SurfPoint *sp)
|
nuclear@2
|
90 {
|
nuclear@8
|
91 float4 dir = ray.dir;
|
nuclear@8
|
92 float4 orig = ray.origin;
|
nuclear@8
|
93 float4 spos = sph->pos;
|
nuclear@2
|
94
|
nuclear@2
|
95 float a = dot(dir, dir);
|
nuclear@2
|
96 float b = 2.0 * dir.x * (orig.x - spos.x) +
|
nuclear@2
|
97 2.0 * dir.y * (orig.y - spos.y) +
|
nuclear@2
|
98 2.0 * dir.z * (orig.z - spos.z);
|
nuclear@2
|
99 float c = dot(spos, spos) + dot(orig, orig) +
|
nuclear@2
|
100 2.0 * dot(-spos, orig) - sph->radius * sph->radius;
|
nuclear@2
|
101
|
nuclear@2
|
102 float d = b * b - 4.0 * a * c;
|
nuclear@2
|
103 if(d < 0.0) return false;
|
nuclear@2
|
104
|
nuclear@2
|
105 float sqrt_d = sqrt(d);
|
nuclear@2
|
106 float t1 = (-b + sqrt_d) / (2.0 * a);
|
nuclear@2
|
107 float t2 = (-b - sqrt_d) / (2.0 * a);
|
nuclear@2
|
108
|
nuclear@2
|
109 if(t1 < EPSILON) t1 = t2;
|
nuclear@2
|
110 if(t2 < EPSILON) t2 = t1;
|
nuclear@2
|
111 float t = t1 < t2 ? t1 : t2;
|
nuclear@2
|
112
|
nuclear@2
|
113 if(t < EPSILON || t > 1.0) {
|
nuclear@2
|
114 return false;
|
nuclear@2
|
115 }
|
nuclear@2
|
116
|
nuclear@2
|
117 sp->t = t;
|
nuclear@2
|
118 sp->pos = orig + dir * sp->t;
|
nuclear@2
|
119 sp->norm = (sp->pos - spos) / sph->radius;
|
nuclear@5
|
120 sp->obj = sph;
|
nuclear@2
|
121 return true;
|
nuclear@2
|
122 }
|
nuclear@5
|
123
|
nuclear@8
|
124 float4 reflect(float4 v, float4 n)
|
nuclear@5
|
125 {
|
nuclear@5
|
126 return 2.0f * dot(v, n) * n - v;
|
nuclear@5
|
127 }
|
nuclear@8
|
128
|
nuclear@8
|
129 float4 transform(float4 v, global const float *xform)
|
nuclear@8
|
130 {
|
nuclear@8
|
131 float4 res;
|
nuclear@8
|
132 res.x = v.x * xform[0] + v.y * xform[4] + v.z * xform[8] + xform[12];
|
nuclear@8
|
133 res.y = v.x * xform[1] + v.y * xform[5] + v.z * xform[9] + xform[13];
|
nuclear@8
|
134 res.z = v.x * xform[2] + v.y * xform[6] + v.z * xform[10] + xform[14];
|
nuclear@8
|
135 res.w = 1.0;
|
nuclear@8
|
136 return res;
|
nuclear@8
|
137 }
|
nuclear@8
|
138
|
nuclear@8
|
139 struct Ray transform_ray(global const struct Ray *ray, global const float *xform)
|
nuclear@8
|
140 {
|
nuclear@8
|
141 struct Ray res;
|
nuclear@8
|
142 float rot[16];
|
nuclear@8
|
143
|
nuclear@8
|
144 for(int i=0; i<16; i++) {
|
nuclear@8
|
145 rot[i] = xform[i];
|
nuclear@8
|
146 }
|
nuclear@8
|
147 rot[3] = rot[7] = rot[11] = rot[12] = rot[13] = rot[14] = 0.0f;
|
nuclear@8
|
148 rot[15] = 1.0f;
|
nuclear@8
|
149
|
nuclear@8
|
150 res.origin = transform(ray->origin, xform);
|
nuclear@8
|
151 res.dir = transform(ray->dir, xform);
|
nuclear@8
|
152 return res;
|
nuclear@8
|
153 }
|