clray

view src/rt.cc @ 42:1169f3d04135

added CL/GL interop support for macosx (compiles) and windows (untested)
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 28 Aug 2010 02:01:16 +0100
parents 1bcbb53b3505
children f9eec11e5acc
line source
1 #include <stdio.h>
2 #include <string.h>
3 #include <math.h>
4 #include <assert.h>
5 #include "ogl.h"
6 #include "ocl.h"
7 #include "scene.h"
8 #include "timer.h"
10 // kernel arguments
11 enum {
12 KARG_FRAMEBUFFER,
13 KARG_RENDER_INFO,
14 KARG_FACES,
15 KARG_MATLIB,
16 KARG_LIGHTS,
17 KARG_PRIM_RAYS,
18 KARG_XFORM,
19 KARG_INVTRANS_XFORM,
20 KARG_KDTREE,
22 NUM_KERNEL_ARGS
23 };
25 struct RendInfo {
26 float ambient[4];
27 int xsz, ysz;
28 int num_faces, num_lights;
29 int max_iter;
30 int kd_depth;
31 };
33 struct Ray {
34 float origin[4], dir[4];
35 };
37 struct Light {
38 float pos[4], color[4];
39 };
41 static Ray get_primary_ray(int x, int y, int w, int h, float vfov_deg);
43 static Face *faces;
44 static Ray *prim_rays;
45 static CLProgram *prog;
46 static int global_size;
48 static Light lightlist[] = {
49 {{-8, 15, 18, 0}, {1, 1, 1, 1}}
50 };
53 static RendInfo rinf;
56 bool init_renderer(int xsz, int ysz, Scene *scn, unsigned int tex)
57 {
58 // render info
59 rinf.ambient[0] = rinf.ambient[1] = rinf.ambient[2] = 0.0;
60 rinf.ambient[3] = 0.0;
62 rinf.xsz = xsz;
63 rinf.ysz = ysz;
64 rinf.num_faces = scn->get_num_faces();
65 rinf.num_lights = sizeof lightlist / sizeof *lightlist;
66 rinf.max_iter = 6;
67 rinf.kd_depth = kdtree_depth(scn->kdtree);
69 /* calculate primary rays */
70 prim_rays = new Ray[xsz * ysz];
72 for(int i=0; i<ysz; i++) {
73 for(int j=0; j<xsz; j++) {
74 prim_rays[i * xsz + j] = get_primary_ray(j, i, xsz, ysz, 45.0);
75 }
76 }
78 /* setup opencl */
79 prog = new CLProgram("render");
80 if(!prog->load("rt.cl")) {
81 return false;
82 }
84 if(!(faces = (Face*)scn->get_face_buffer())) {
85 fprintf(stderr, "failed to create face buffer\n");
86 return false;
87 }
89 const KDNodeGPU *kdbuf = scn->get_kdtree_buffer();
90 if(!kdbuf) {
91 fprintf(stderr, "failed to create kdtree buffer\n");
92 return false;
93 }
94 // XXX now we can actually destroy the original kdtree and keep only the GPU version
96 /* setup argument buffers */
97 #ifdef CLGL_INTEROP
98 prog->set_arg_texture(KARG_FRAMEBUFFER, ARG_WR, tex);
99 #else
100 prog->set_arg_image(KARG_FRAMEBUFFER, ARG_WR, xsz, ysz);
101 #endif
102 prog->set_arg_buffer(KARG_RENDER_INFO, ARG_RD, sizeof rinf, &rinf);
103 prog->set_arg_buffer(KARG_FACES, ARG_RD, rinf.num_faces * sizeof(Face), faces);
104 prog->set_arg_buffer(KARG_MATLIB, ARG_RD, scn->get_num_materials() * sizeof(Material), scn->get_materials());
105 prog->set_arg_buffer(KARG_LIGHTS, ARG_RD, sizeof lightlist, lightlist);
106 prog->set_arg_buffer(KARG_PRIM_RAYS, ARG_RD, xsz * ysz * sizeof *prim_rays, prim_rays);
107 prog->set_arg_buffer(KARG_XFORM, ARG_RD, 16 * sizeof(float));
108 prog->set_arg_buffer(KARG_INVTRANS_XFORM, ARG_RD, 16 * sizeof(float));
109 prog->set_arg_buffer(KARG_KDTREE, ARG_RD, scn->get_num_kdnodes() * sizeof *kdbuf, kdbuf);
111 if(prog->get_num_args() < NUM_KERNEL_ARGS) {
112 return false;
113 }
115 if(!prog->build()) {
116 return false;
117 }
119 delete [] prim_rays;
121 global_size = xsz * ysz;
122 return true;
123 }
125 void destroy_renderer()
126 {
127 delete prog;
128 }
130 bool render()
131 {
132 // XXX do we need to call glFinish ?
134 long tm0 = get_msec();
136 #ifdef CLGL_INTEROP
137 cl_event ev;
138 CLMemBuffer *texbuf = prog->get_arg_buffer(KARG_FRAMEBUFFER);
140 if(!acquire_gl_object(texbuf, &ev)) {
141 return false;
142 }
144 // make sure that we will wait for the acquire to finish before running
145 prog->set_wait_event(ev);
146 #endif
148 if(!prog->run(1, global_size)) {
149 return false;
150 }
152 #ifdef CLGL_INTEROP
153 if(!release_gl_object(texbuf, &ev)) {
154 return false;
155 }
156 clWaitForEvents(1, &ev);
157 #endif
159 #ifndef CLGL_INTEROP
160 /* if we don't compile in CL/GL interoperability support, we need
161 * to copy the output buffer to the OpenGL texture used to displaying
162 * the image.
163 */
164 CLMemBuffer *mbuf = prog->get_arg_buffer(KARG_FRAMEBUFFER);
165 void *fb = map_mem_buffer(mbuf, MAP_RD);
166 if(!fb) {
167 fprintf(stderr, "FAILED\n");
168 return false;
169 }
171 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rinf.xsz, rinf.ysz, GL_RGBA, GL_FLOAT, fb);
172 unmap_mem_buffer(mbuf);
173 #endif
175 printf("rendered in %ld msec\n", get_msec() - tm0);
176 return true;
177 }
179 #define MIN(a, b) ((a) < (b) ? (a) : (b))
180 static void dbg_set_gl_material(Material *mat)
181 {
182 static Material def_mat = {{0.7, 0.7, 0.7, 1}, {0, 0, 0, 0}, 0, 0, 0};
184 if(!mat) mat = &def_mat;
186 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, mat->kd);
187 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat->ks);
188 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, MIN(mat->spow, 128.0f));
189 }
191 void dbg_render_gl(Scene *scn, bool show_tree, bool show_obj)
192 {
193 glPushAttrib(GL_ENABLE_BIT | GL_TRANSFORM_BIT | GL_LIGHTING_BIT);
195 for(int i=0; i<rinf.num_lights; i++) {
196 float lpos[4];
198 memcpy(lpos, lightlist[i].pos, sizeof lpos);
199 lpos[3] = 1.0;
201 glLightfv(GL_LIGHT0 + i, GL_POSITION, lpos);
202 glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, lightlist[i].color);
203 glEnable(GL_LIGHT0 + i);
204 }
206 glDisable(GL_TEXTURE_2D);
207 glEnable(GL_DEPTH_TEST);
208 glEnable(GL_LIGHTING);
210 glMatrixMode(GL_PROJECTION);
211 glPushMatrix();
212 glLoadIdentity();
213 gluPerspective(45.0, (float)rinf.xsz / (float)rinf.ysz, 0.5, 1000.0);
215 if(show_obj) {
216 Material *materials = scn->get_materials();
218 int num_faces = scn->get_num_faces();
219 int cur_mat = -1;
221 for(int i=0; i<num_faces; i++) {
222 if(faces[i].matid != cur_mat) {
223 if(cur_mat != -1) {
224 glEnd();
225 }
226 dbg_set_gl_material(materials ? materials + faces[i].matid : 0);
227 cur_mat = faces[i].matid;
228 glBegin(GL_TRIANGLES);
229 }
231 for(int j=0; j<3; j++) {
232 glNormal3fv(faces[i].v[j].normal);
233 glVertex3fv(faces[i].v[j].pos);
234 }
235 }
236 glEnd();
237 }
239 if(show_tree) {
240 scn->draw_kdtree();
241 }
243 glPopMatrix();
244 glPopAttrib();
246 assert(glGetError() == GL_NO_ERROR);
247 }
249 void set_xform(float *matrix, float *invtrans)
250 {
251 CLMemBuffer *mbuf_xform = prog->get_arg_buffer(KARG_XFORM);
252 CLMemBuffer *mbuf_invtrans = prog->get_arg_buffer(KARG_INVTRANS_XFORM);
253 assert(mbuf_xform && mbuf_invtrans);
255 float *mem = (float*)map_mem_buffer(mbuf_xform, MAP_WR);
256 memcpy(mem, matrix, 16 * sizeof *mem);
257 unmap_mem_buffer(mbuf_xform);
259 mem = (float*)map_mem_buffer(mbuf_invtrans, MAP_WR);
260 memcpy(mem, invtrans, 16 * sizeof *mem);
261 unmap_mem_buffer(mbuf_invtrans);
262 }
264 static Ray get_primary_ray(int x, int y, int w, int h, float vfov_deg)
265 {
266 float vfov = M_PI * vfov_deg / 180.0;
267 float aspect = (float)w / (float)h;
269 float ysz = 2.0;
270 float xsz = aspect * ysz;
272 float px = ((float)x / (float)w) * xsz - xsz / 2.0;
273 float py = 1.0 - ((float)y / (float)h) * ysz;
274 float pz = 1.0 / tan(0.5 * vfov);
276 px *= 100.0;
277 py *= 100.0;
278 pz *= 100.0;
280 Ray ray = {{0, 0, 0, 1}, {px, py, -pz, 1}};
281 return ray;
282 }