clray

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