clray

view src/rt.cc @ 40:1bcbb53b3505

segfault on exit?
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 27 Aug 2010 19:00:14 +0100
parents 980bc07be868
children 057b8575a1c1
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 prog->set_arg_texture(KARG_FRAMEBUFFER, ARG_WR, tex);
98 prog->set_arg_buffer(KARG_RENDER_INFO, ARG_RD, sizeof rinf, &rinf);
99 prog->set_arg_buffer(KARG_FACES, ARG_RD, rinf.num_faces * sizeof(Face), faces);
100 prog->set_arg_buffer(KARG_MATLIB, ARG_RD, scn->get_num_materials() * sizeof(Material), scn->get_materials());
101 prog->set_arg_buffer(KARG_LIGHTS, ARG_RD, sizeof lightlist, lightlist);
102 prog->set_arg_buffer(KARG_PRIM_RAYS, ARG_RD, xsz * ysz * sizeof *prim_rays, prim_rays);
103 prog->set_arg_buffer(KARG_XFORM, ARG_RD, 16 * sizeof(float));
104 prog->set_arg_buffer(KARG_INVTRANS_XFORM, ARG_RD, 16 * sizeof(float));
105 prog->set_arg_buffer(KARG_KDTREE, ARG_RD, scn->get_num_kdnodes() * sizeof *kdbuf, kdbuf);
107 if(prog->get_num_args() < NUM_KERNEL_ARGS) {
108 return false;
109 }
111 if(!prog->build()) {
112 return false;
113 }
115 delete [] prim_rays;
117 global_size = xsz * ysz;
118 return true;
119 }
121 void destroy_renderer()
122 {
123 delete prog;
124 }
126 bool render()
127 {
128 // XXX do we need to call glFinish ?
130 long tm0 = get_msec();
132 #ifdef CLGL_INTEROP
133 cl_event ev;
134 CLMemBuffer *texbuf = prog->get_arg_buffer(KARG_FRAMEBUFFER);
136 if(!acquire_gl_object(texbuf, &ev)) {
137 return false;
138 }
140 // make sure that we will wait for the acquire to finish before running
141 prog->set_wait_event(ev);
142 #endif
144 if(!prog->run(1, global_size)) {
145 return false;
146 }
148 #ifdef CLGL_INTEROP
149 if(!release_gl_object(texbuf, &ev)) {
150 return false;
151 }
152 clWaitForEvents(1, &ev);
153 #endif
155 #ifndef CLGL_INTEROP
156 /* if we don't compile in CL/GL interoperability support, we need
157 * to copy the output buffer to the OpenGL texture used to displaying
158 * the image.
159 */
160 CLMemBuffer *mbuf = prog->get_arg_buffer(KARG_FRAMEBUFFER);
161 void *fb = map_mem_buffer(mbuf, MAP_RD);
162 if(!fb) {
163 fprintf(stderr, "FAILED\n");
164 return false;
165 }
167 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rinf.xsz, rinf.ysz, GL_RGBA, GL_FLOAT, fb);
168 unmap_mem_buffer(mbuf);
169 #endif
171 printf("rendered in %ld msec\n", get_msec() - tm0);
172 return true;
173 }
175 #define MIN(a, b) ((a) < (b) ? (a) : (b))
176 static void dbg_set_gl_material(Material *mat)
177 {
178 static Material def_mat = {{0.7, 0.7, 0.7, 1}, {0, 0, 0, 0}, 0, 0, 0};
180 if(!mat) mat = &def_mat;
182 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, mat->kd);
183 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat->ks);
184 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, MIN(mat->spow, 128.0f));
185 }
187 void dbg_render_gl(Scene *scn, bool show_tree, bool show_obj)
188 {
189 glPushAttrib(GL_ENABLE_BIT | GL_TRANSFORM_BIT | GL_LIGHTING_BIT);
191 for(int i=0; i<rinf.num_lights; i++) {
192 float lpos[4];
194 memcpy(lpos, lightlist[i].pos, sizeof lpos);
195 lpos[3] = 1.0;
197 glLightfv(GL_LIGHT0 + i, GL_POSITION, lpos);
198 glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, lightlist[i].color);
199 glEnable(GL_LIGHT0 + i);
200 }
202 glDisable(GL_TEXTURE_2D);
203 glEnable(GL_DEPTH_TEST);
204 glEnable(GL_LIGHTING);
206 glMatrixMode(GL_PROJECTION);
207 glPushMatrix();
208 glLoadIdentity();
209 gluPerspective(45.0, (float)rinf.xsz / (float)rinf.ysz, 0.5, 1000.0);
211 if(show_obj) {
212 Material *materials = scn->get_materials();
214 int num_faces = scn->get_num_faces();
215 int cur_mat = -1;
217 for(int i=0; i<num_faces; i++) {
218 if(faces[i].matid != cur_mat) {
219 if(cur_mat != -1) {
220 glEnd();
221 }
222 dbg_set_gl_material(materials ? materials + faces[i].matid : 0);
223 cur_mat = faces[i].matid;
224 glBegin(GL_TRIANGLES);
225 }
227 for(int j=0; j<3; j++) {
228 glNormal3fv(faces[i].v[j].normal);
229 glVertex3fv(faces[i].v[j].pos);
230 }
231 }
232 glEnd();
233 }
235 if(show_tree) {
236 scn->draw_kdtree();
237 }
239 glPopMatrix();
240 glPopAttrib();
242 assert(glGetError() == GL_NO_ERROR);
243 }
245 void set_xform(float *matrix, float *invtrans)
246 {
247 CLMemBuffer *mbuf_xform = prog->get_arg_buffer(KARG_XFORM);
248 CLMemBuffer *mbuf_invtrans = prog->get_arg_buffer(KARG_INVTRANS_XFORM);
249 assert(mbuf_xform && mbuf_invtrans);
251 float *mem = (float*)map_mem_buffer(mbuf_xform, MAP_WR);
252 memcpy(mem, matrix, 16 * sizeof *mem);
253 unmap_mem_buffer(mbuf_xform);
255 mem = (float*)map_mem_buffer(mbuf_invtrans, MAP_WR);
256 memcpy(mem, invtrans, 16 * sizeof *mem);
257 unmap_mem_buffer(mbuf_invtrans);
258 }
260 static Ray get_primary_ray(int x, int y, int w, int h, float vfov_deg)
261 {
262 float vfov = M_PI * vfov_deg / 180.0;
263 float aspect = (float)w / (float)h;
265 float ysz = 2.0;
266 float xsz = aspect * ysz;
268 float px = ((float)x / (float)w) * xsz - xsz / 2.0;
269 float py = 1.0 - ((float)y / (float)h) * ysz;
270 float pz = 1.0 / tan(0.5 * vfov);
272 px *= 100.0;
273 py *= 100.0;
274 pz *= 100.0;
276 Ray ray = {{0, 0, 0, 1}, {px, py, -pz, 1}};
277 return ray;
278 }