rev |
line source |
nuclear@0
|
1 #include <stdio.h>
|
nuclear@8
|
2 #include <string.h>
|
nuclear@2
|
3 #include <math.h>
|
nuclear@0
|
4 #include <assert.h>
|
John@14
|
5 #include "ogl.h"
|
nuclear@0
|
6 #include "ocl.h"
|
nuclear@22
|
7 #include "scene.h"
|
nuclear@32
|
8 #include "timer.h"
|
nuclear@45
|
9 #include "common.h"
|
nuclear@0
|
10
|
nuclear@12
|
11 // kernel arguments
|
nuclear@12
|
12 enum {
|
nuclear@12
|
13 KARG_FRAMEBUFFER,
|
nuclear@12
|
14 KARG_RENDER_INFO,
|
nuclear@12
|
15 KARG_FACES,
|
nuclear@12
|
16 KARG_MATLIB,
|
nuclear@12
|
17 KARG_LIGHTS,
|
nuclear@12
|
18 KARG_PRIM_RAYS,
|
nuclear@12
|
19 KARG_XFORM,
|
John@14
|
20 KARG_INVTRANS_XFORM,
|
nuclear@28
|
21 KARG_KDTREE,
|
John@14
|
22
|
John@14
|
23 NUM_KERNEL_ARGS
|
nuclear@12
|
24 };
|
John@11
|
25
|
nuclear@2
|
26 struct RendInfo {
|
nuclear@22
|
27 float ambient[4];
|
nuclear@2
|
28 int xsz, ysz;
|
nuclear@9
|
29 int num_faces, num_lights;
|
nuclear@2
|
30 int max_iter;
|
nuclear@28
|
31 int kd_depth;
|
nuclear@12
|
32 };
|
nuclear@2
|
33
|
nuclear@1
|
34 struct Ray {
|
nuclear@8
|
35 float origin[4], dir[4];
|
nuclear@12
|
36 };
|
nuclear@1
|
37
|
nuclear@3
|
38 struct Light {
|
nuclear@8
|
39 float pos[4], color[4];
|
nuclear@12
|
40 };
|
nuclear@1
|
41
|
nuclear@3
|
42 static Ray get_primary_ray(int x, int y, int w, int h, float vfov_deg);
|
nuclear@43
|
43 static float *create_kdimage(const KDNodeGPU *kdtree, int num_nodes, int *xsz_ret, int *ysz_ret);
|
nuclear@3
|
44
|
nuclear@13
|
45 static Face *faces;
|
nuclear@3
|
46 static Ray *prim_rays;
|
nuclear@3
|
47 static CLProgram *prog;
|
nuclear@3
|
48 static int global_size;
|
nuclear@3
|
49
|
nuclear@4
|
50 static Light lightlist[] = {
|
nuclear@22
|
51 {{-8, 15, 18, 0}, {1, 1, 1, 1}}
|
nuclear@4
|
52 };
|
nuclear@4
|
53
|
nuclear@7
|
54
|
nuclear@4
|
55 static RendInfo rinf;
|
nuclear@4
|
56
|
nuclear@43
|
57 static long timing_sample_sum;
|
nuclear@43
|
58 static long num_timing_samples;
|
nuclear@43
|
59
|
nuclear@4
|
60
|
nuclear@39
|
61 bool init_renderer(int xsz, int ysz, Scene *scn, unsigned int tex)
|
nuclear@0
|
62 {
|
nuclear@4
|
63 // render info
|
nuclear@22
|
64 rinf.ambient[0] = rinf.ambient[1] = rinf.ambient[2] = 0.0;
|
nuclear@16
|
65 rinf.ambient[3] = 0.0;
|
nuclear@16
|
66
|
nuclear@4
|
67 rinf.xsz = xsz;
|
nuclear@4
|
68 rinf.ysz = ysz;
|
nuclear@13
|
69 rinf.num_faces = scn->get_num_faces();
|
nuclear@4
|
70 rinf.num_lights = sizeof lightlist / sizeof *lightlist;
|
nuclear@4
|
71 rinf.max_iter = 6;
|
nuclear@28
|
72 rinf.kd_depth = kdtree_depth(scn->kdtree);
|
nuclear@4
|
73
|
nuclear@3
|
74 /* calculate primary rays */
|
nuclear@3
|
75 prim_rays = new Ray[xsz * ysz];
|
nuclear@2
|
76
|
nuclear@2
|
77 for(int i=0; i<ysz; i++) {
|
nuclear@2
|
78 for(int j=0; j<xsz; j++) {
|
nuclear@2
|
79 prim_rays[i * xsz + j] = get_primary_ray(j, i, xsz, ysz, 45.0);
|
nuclear@2
|
80 }
|
nuclear@0
|
81 }
|
nuclear@0
|
82
|
nuclear@2
|
83 /* setup opencl */
|
nuclear@3
|
84 prog = new CLProgram("render");
|
nuclear@3
|
85 if(!prog->load("rt.cl")) {
|
nuclear@8
|
86 return false;
|
nuclear@0
|
87 }
|
nuclear@0
|
88
|
nuclear@24
|
89 if(!(faces = (Face*)scn->get_face_buffer())) {
|
nuclear@13
|
90 fprintf(stderr, "failed to create face buffer\n");
|
nuclear@13
|
91 return false;
|
nuclear@13
|
92 }
|
nuclear@13
|
93
|
nuclear@28
|
94 const KDNodeGPU *kdbuf = scn->get_kdtree_buffer();
|
nuclear@28
|
95 if(!kdbuf) {
|
nuclear@28
|
96 fprintf(stderr, "failed to create kdtree buffer\n");
|
nuclear@28
|
97 return false;
|
nuclear@28
|
98 }
|
nuclear@43
|
99
|
nuclear@43
|
100 int kdimg_xsz, kdimg_ysz;
|
nuclear@43
|
101 float *kdimg_pixels = create_kdimage(kdbuf, scn->get_num_kdnodes(), &kdimg_xsz, &kdimg_ysz);
|
nuclear@28
|
102
|
nuclear@3
|
103 /* setup argument buffers */
|
nuclear@41
|
104 #ifdef CLGL_INTEROP
|
nuclear@39
|
105 prog->set_arg_texture(KARG_FRAMEBUFFER, ARG_WR, tex);
|
nuclear@41
|
106 #else
|
nuclear@41
|
107 prog->set_arg_image(KARG_FRAMEBUFFER, ARG_WR, xsz, ysz);
|
nuclear@41
|
108 #endif
|
nuclear@12
|
109 prog->set_arg_buffer(KARG_RENDER_INFO, ARG_RD, sizeof rinf, &rinf);
|
John@14
|
110 prog->set_arg_buffer(KARG_FACES, ARG_RD, rinf.num_faces * sizeof(Face), faces);
|
John@14
|
111 prog->set_arg_buffer(KARG_MATLIB, ARG_RD, scn->get_num_materials() * sizeof(Material), scn->get_materials());
|
nuclear@12
|
112 prog->set_arg_buffer(KARG_LIGHTS, ARG_RD, sizeof lightlist, lightlist);
|
nuclear@12
|
113 prog->set_arg_buffer(KARG_PRIM_RAYS, ARG_RD, xsz * ysz * sizeof *prim_rays, prim_rays);
|
nuclear@12
|
114 prog->set_arg_buffer(KARG_XFORM, ARG_RD, 16 * sizeof(float));
|
nuclear@12
|
115 prog->set_arg_buffer(KARG_INVTRANS_XFORM, ARG_RD, 16 * sizeof(float));
|
nuclear@43
|
116 //prog->set_arg_buffer(KARG_KDTREE, ARG_RD, scn->get_num_kdnodes() * sizeof *kdbuf, kdbuf);
|
nuclear@43
|
117 prog->set_arg_image(KARG_KDTREE, ARG_RD, kdimg_xsz, kdimg_ysz, kdimg_pixels);
|
nuclear@43
|
118
|
nuclear@43
|
119 delete [] kdimg_pixels;
|
nuclear@43
|
120
|
nuclear@12
|
121
|
John@14
|
122 if(prog->get_num_args() < NUM_KERNEL_ARGS) {
|
John@14
|
123 return false;
|
John@14
|
124 }
|
John@14
|
125
|
nuclear@45
|
126 const char *opt = "-Isrc -cl-mad-enable -cl-single-precision-constant -cl-fast-relaxed-math";
|
nuclear@45
|
127 if(!prog->build(opt)) {
|
nuclear@16
|
128 return false;
|
nuclear@16
|
129 }
|
nuclear@16
|
130
|
nuclear@12
|
131 delete [] prim_rays;
|
nuclear@2
|
132
|
nuclear@3
|
133 global_size = xsz * ysz;
|
nuclear@3
|
134 return true;
|
nuclear@3
|
135 }
|
nuclear@3
|
136
|
nuclear@3
|
137 void destroy_renderer()
|
nuclear@3
|
138 {
|
nuclear@3
|
139 delete prog;
|
nuclear@43
|
140
|
nuclear@43
|
141 printf("rendertime mean: %ld msec\n", timing_sample_sum / num_timing_samples);
|
nuclear@3
|
142 }
|
nuclear@3
|
143
|
nuclear@3
|
144 bool render()
|
nuclear@3
|
145 {
|
nuclear@39
|
146 // XXX do we need to call glFinish ?
|
nuclear@39
|
147
|
nuclear@32
|
148 long tm0 = get_msec();
|
nuclear@32
|
149
|
nuclear@40
|
150 #ifdef CLGL_INTEROP
|
nuclear@39
|
151 cl_event ev;
|
nuclear@39
|
152 CLMemBuffer *texbuf = prog->get_arg_buffer(KARG_FRAMEBUFFER);
|
nuclear@39
|
153
|
nuclear@39
|
154 if(!acquire_gl_object(texbuf, &ev)) {
|
nuclear@39
|
155 return false;
|
nuclear@39
|
156 }
|
nuclear@39
|
157
|
nuclear@39
|
158 // make sure that we will wait for the acquire to finish before running
|
nuclear@39
|
159 prog->set_wait_event(ev);
|
nuclear@40
|
160 #endif
|
nuclear@39
|
161
|
nuclear@3
|
162 if(!prog->run(1, global_size)) {
|
nuclear@3
|
163 return false;
|
nuclear@0
|
164 }
|
John@15
|
165
|
nuclear@40
|
166 #ifdef CLGL_INTEROP
|
nuclear@39
|
167 if(!release_gl_object(texbuf, &ev)) {
|
nuclear@39
|
168 return false;
|
nuclear@39
|
169 }
|
nuclear@39
|
170 clWaitForEvents(1, &ev);
|
nuclear@40
|
171 #endif
|
nuclear@39
|
172
|
nuclear@40
|
173 #ifndef CLGL_INTEROP
|
nuclear@40
|
174 /* if we don't compile in CL/GL interoperability support, we need
|
nuclear@40
|
175 * to copy the output buffer to the OpenGL texture used to displaying
|
nuclear@40
|
176 * the image.
|
nuclear@40
|
177 */
|
nuclear@13
|
178 CLMemBuffer *mbuf = prog->get_arg_buffer(KARG_FRAMEBUFFER);
|
nuclear@12
|
179 void *fb = map_mem_buffer(mbuf, MAP_RD);
|
nuclear@13
|
180 if(!fb) {
|
nuclear@13
|
181 fprintf(stderr, "FAILED\n");
|
nuclear@13
|
182 return false;
|
nuclear@13
|
183 }
|
nuclear@13
|
184
|
nuclear@12
|
185 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rinf.xsz, rinf.ysz, GL_RGBA, GL_FLOAT, fb);
|
nuclear@2
|
186 unmap_mem_buffer(mbuf);
|
nuclear@40
|
187 #endif
|
nuclear@32
|
188
|
nuclear@43
|
189 long msec = get_msec() - tm0;
|
nuclear@43
|
190 timing_sample_sum += msec;
|
nuclear@43
|
191 num_timing_samples++;
|
nuclear@43
|
192
|
nuclear@43
|
193 printf("rendered in %ld msec\n", msec);
|
nuclear@3
|
194 return true;
|
nuclear@0
|
195 }
|
nuclear@2
|
196
|
nuclear@27
|
197 #define MIN(a, b) ((a) < (b) ? (a) : (b))
|
nuclear@21
|
198 static void dbg_set_gl_material(Material *mat)
|
nuclear@21
|
199 {
|
nuclear@21
|
200 static Material def_mat = {{0.7, 0.7, 0.7, 1}, {0, 0, 0, 0}, 0, 0, 0};
|
nuclear@21
|
201
|
nuclear@21
|
202 if(!mat) mat = &def_mat;
|
nuclear@21
|
203
|
nuclear@21
|
204 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, mat->kd);
|
nuclear@21
|
205 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat->ks);
|
nuclear@27
|
206 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, MIN(mat->spow, 128.0f));
|
nuclear@21
|
207 }
|
nuclear@21
|
208
|
nuclear@27
|
209 void dbg_render_gl(Scene *scn, bool show_tree, bool show_obj)
|
nuclear@8
|
210 {
|
nuclear@22
|
211 glPushAttrib(GL_ENABLE_BIT | GL_TRANSFORM_BIT | GL_LIGHTING_BIT);
|
nuclear@8
|
212
|
nuclear@21
|
213 for(int i=0; i<rinf.num_lights; i++) {
|
nuclear@21
|
214 float lpos[4];
|
nuclear@21
|
215
|
nuclear@21
|
216 memcpy(lpos, lightlist[i].pos, sizeof lpos);
|
nuclear@21
|
217 lpos[3] = 1.0;
|
nuclear@21
|
218
|
nuclear@21
|
219 glLightfv(GL_LIGHT0 + i, GL_POSITION, lpos);
|
nuclear@21
|
220 glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, lightlist[i].color);
|
nuclear@22
|
221 glEnable(GL_LIGHT0 + i);
|
nuclear@21
|
222 }
|
nuclear@21
|
223
|
nuclear@12
|
224 glDisable(GL_TEXTURE_2D);
|
nuclear@12
|
225 glEnable(GL_DEPTH_TEST);
|
John@15
|
226 glEnable(GL_LIGHTING);
|
nuclear@12
|
227
|
nuclear@12
|
228 glMatrixMode(GL_PROJECTION);
|
nuclear@12
|
229 glPushMatrix();
|
nuclear@12
|
230 glLoadIdentity();
|
nuclear@12
|
231 gluPerspective(45.0, (float)rinf.xsz / (float)rinf.ysz, 0.5, 1000.0);
|
nuclear@12
|
232
|
nuclear@27
|
233 if(show_obj) {
|
nuclear@27
|
234 Material *materials = scn->get_materials();
|
John@14
|
235
|
nuclear@27
|
236 int num_faces = scn->get_num_faces();
|
nuclear@27
|
237 int cur_mat = -1;
|
nuclear@21
|
238
|
nuclear@27
|
239 for(int i=0; i<num_faces; i++) {
|
nuclear@27
|
240 if(faces[i].matid != cur_mat) {
|
nuclear@27
|
241 if(cur_mat != -1) {
|
nuclear@27
|
242 glEnd();
|
nuclear@27
|
243 }
|
nuclear@27
|
244 dbg_set_gl_material(materials ? materials + faces[i].matid : 0);
|
nuclear@27
|
245 cur_mat = faces[i].matid;
|
nuclear@27
|
246 glBegin(GL_TRIANGLES);
|
nuclear@21
|
247 }
|
nuclear@27
|
248
|
nuclear@27
|
249 for(int j=0; j<3; j++) {
|
nuclear@27
|
250 glNormal3fv(faces[i].v[j].normal);
|
nuclear@27
|
251 glVertex3fv(faces[i].v[j].pos);
|
nuclear@27
|
252 }
|
John@14
|
253 }
|
nuclear@27
|
254 glEnd();
|
nuclear@27
|
255 }
|
nuclear@12
|
256
|
nuclear@27
|
257 if(show_tree) {
|
nuclear@27
|
258 scn->draw_kdtree();
|
nuclear@12
|
259 }
|
nuclear@12
|
260
|
nuclear@12
|
261 glPopMatrix();
|
nuclear@12
|
262 glPopAttrib();
|
nuclear@22
|
263
|
nuclear@22
|
264 assert(glGetError() == GL_NO_ERROR);
|
nuclear@12
|
265 }
|
nuclear@12
|
266
|
nuclear@12
|
267 void set_xform(float *matrix, float *invtrans)
|
nuclear@12
|
268 {
|
nuclear@12
|
269 CLMemBuffer *mbuf_xform = prog->get_arg_buffer(KARG_XFORM);
|
nuclear@12
|
270 CLMemBuffer *mbuf_invtrans = prog->get_arg_buffer(KARG_INVTRANS_XFORM);
|
nuclear@12
|
271 assert(mbuf_xform && mbuf_invtrans);
|
nuclear@12
|
272
|
nuclear@12
|
273 float *mem = (float*)map_mem_buffer(mbuf_xform, MAP_WR);
|
nuclear@12
|
274 memcpy(mem, matrix, 16 * sizeof *mem);
|
nuclear@12
|
275 unmap_mem_buffer(mbuf_xform);
|
nuclear@12
|
276
|
nuclear@12
|
277 mem = (float*)map_mem_buffer(mbuf_invtrans, MAP_WR);
|
nuclear@12
|
278 memcpy(mem, invtrans, 16 * sizeof *mem);
|
nuclear@12
|
279 unmap_mem_buffer(mbuf_invtrans);
|
nuclear@8
|
280 }
|
nuclear@8
|
281
|
nuclear@3
|
282 static Ray get_primary_ray(int x, int y, int w, int h, float vfov_deg)
|
nuclear@2
|
283 {
|
nuclear@2
|
284 float vfov = M_PI * vfov_deg / 180.0;
|
nuclear@2
|
285 float aspect = (float)w / (float)h;
|
nuclear@2
|
286
|
nuclear@2
|
287 float ysz = 2.0;
|
nuclear@2
|
288 float xsz = aspect * ysz;
|
nuclear@2
|
289
|
nuclear@2
|
290 float px = ((float)x / (float)w) * xsz - xsz / 2.0;
|
nuclear@2
|
291 float py = 1.0 - ((float)y / (float)h) * ysz;
|
nuclear@2
|
292 float pz = 1.0 / tan(0.5 * vfov);
|
nuclear@2
|
293
|
nuclear@43
|
294 float mag = sqrt(px * px + py * py + pz * pz);
|
nuclear@43
|
295
|
nuclear@45
|
296 px = px * RAY_MAG / mag;
|
nuclear@45
|
297 py = py * RAY_MAG / mag;
|
nuclear@45
|
298 pz = pz * RAY_MAG / mag;
|
nuclear@2
|
299
|
nuclear@18
|
300 Ray ray = {{0, 0, 0, 1}, {px, py, -pz, 1}};
|
nuclear@2
|
301 return ray;
|
nuclear@2
|
302 }
|
nuclear@43
|
303
|
nuclear@43
|
304 static float *create_kdimage(const KDNodeGPU *kdtree, int num_nodes, int *xsz_ret, int *ysz_ret)
|
nuclear@43
|
305 {
|
nuclear@45
|
306 int ysz = MIN(num_nodes, KDIMG_MAX_HEIGHT);
|
nuclear@45
|
307 int columns = (num_nodes - 1) / KDIMG_MAX_HEIGHT + 1;
|
nuclear@45
|
308 int xsz = KDIMG_NODE_WIDTH * columns;
|
nuclear@43
|
309
|
nuclear@43
|
310 printf("creating kdtree image %dx%d (%d nodes)\n", xsz, ysz, num_nodes);
|
nuclear@43
|
311
|
nuclear@43
|
312 float *img = new float[4 * xsz * ysz];
|
nuclear@43
|
313 memset(img, 0, 4 * xsz * ysz * sizeof *img);
|
nuclear@43
|
314
|
nuclear@43
|
315 for(int i=0; i<num_nodes; i++) {
|
nuclear@45
|
316 int x = KDIMG_NODE_WIDTH * (i / KDIMG_MAX_HEIGHT);
|
nuclear@45
|
317 int y = i % KDIMG_MAX_HEIGHT;
|
nuclear@45
|
318
|
nuclear@45
|
319 float *ptr = img + (y * xsz + x) * 4;
|
nuclear@43
|
320
|
nuclear@43
|
321 *ptr++ = kdtree[i].aabb.min[0];
|
nuclear@43
|
322 *ptr++ = kdtree[i].aabb.min[1];
|
nuclear@43
|
323 *ptr++ = kdtree[i].aabb.min[2];
|
nuclear@43
|
324 *ptr++ = 0.0;
|
nuclear@43
|
325
|
nuclear@43
|
326 *ptr++ = kdtree[i].aabb.max[0];
|
nuclear@43
|
327 *ptr++ = kdtree[i].aabb.max[1];
|
nuclear@43
|
328 *ptr++ = kdtree[i].aabb.max[2];
|
nuclear@43
|
329 *ptr++ = 0.0;
|
nuclear@43
|
330
|
nuclear@43
|
331 for(int j=0; j<MAX_NODE_FACES; j++) {
|
nuclear@43
|
332 *ptr++ = j < kdtree[i].num_faces ? (float)kdtree[i].face_idx[j] : 0.0f;
|
nuclear@43
|
333 }
|
nuclear@43
|
334
|
nuclear@43
|
335 *ptr++ = (float)kdtree[i].num_faces;
|
nuclear@43
|
336 *ptr++ = (float)kdtree[i].left;
|
nuclear@43
|
337 *ptr++ = (float)kdtree[i].right;
|
nuclear@43
|
338 *ptr++ = 0.0;
|
nuclear@43
|
339 }
|
nuclear@43
|
340
|
nuclear@43
|
341 if(xsz_ret) *xsz_ret = xsz;
|
nuclear@43
|
342 if(ysz_ret) *ysz_ret = ysz;
|
nuclear@43
|
343 return img;
|
nuclear@43
|
344 }
|