clray

annotate src/rt.cc @ 35:7d77ded5f890

stopped using a heap to flatten the kdtree. added explicit left/right indices
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 26 Aug 2010 20:24:07 +0100
parents 4cf4919c3812
children 980bc07be868
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@13 56 bool init_renderer(int xsz, int ysz, Scene *scn)
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@12 97 prog->set_arg_buffer(KARG_FRAMEBUFFER, ARG_WR, xsz * ysz * 4 * sizeof(float));
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@32 128 long tm0 = get_msec();
nuclear@32 129
nuclear@3 130 if(!prog->run(1, global_size)) {
nuclear@3 131 return false;
nuclear@0 132 }
John@15 133
nuclear@32 134 long tm_run = get_msec() - tm0;
nuclear@32 135
nuclear@13 136 CLMemBuffer *mbuf = prog->get_arg_buffer(KARG_FRAMEBUFFER);
nuclear@12 137 void *fb = map_mem_buffer(mbuf, MAP_RD);
nuclear@13 138 if(!fb) {
nuclear@13 139 fprintf(stderr, "FAILED\n");
nuclear@13 140 return false;
nuclear@13 141 }
nuclear@13 142
nuclear@12 143 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rinf.xsz, rinf.ysz, GL_RGBA, GL_FLOAT, fb);
nuclear@2 144 unmap_mem_buffer(mbuf);
nuclear@32 145
nuclear@32 146 long tm_upd = get_msec() - tm0 - tm_run;
nuclear@32 147
nuclear@32 148 printf("render %ld msec (%ld run, %ld upd)\n", tm_run + tm_upd, tm_run, tm_upd);
nuclear@3 149 return true;
nuclear@0 150 }
nuclear@2 151
nuclear@27 152 #define MIN(a, b) ((a) < (b) ? (a) : (b))
nuclear@21 153 static void dbg_set_gl_material(Material *mat)
nuclear@21 154 {
nuclear@21 155 static Material def_mat = {{0.7, 0.7, 0.7, 1}, {0, 0, 0, 0}, 0, 0, 0};
nuclear@21 156
nuclear@21 157 if(!mat) mat = &def_mat;
nuclear@21 158
nuclear@21 159 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, mat->kd);
nuclear@21 160 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat->ks);
nuclear@27 161 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, MIN(mat->spow, 128.0f));
nuclear@21 162 }
nuclear@21 163
nuclear@27 164 void dbg_render_gl(Scene *scn, bool show_tree, bool show_obj)
nuclear@8 165 {
nuclear@22 166 glPushAttrib(GL_ENABLE_BIT | GL_TRANSFORM_BIT | GL_LIGHTING_BIT);
nuclear@8 167
nuclear@21 168 for(int i=0; i<rinf.num_lights; i++) {
nuclear@21 169 float lpos[4];
nuclear@21 170
nuclear@21 171 memcpy(lpos, lightlist[i].pos, sizeof lpos);
nuclear@21 172 lpos[3] = 1.0;
nuclear@21 173
nuclear@21 174 glLightfv(GL_LIGHT0 + i, GL_POSITION, lpos);
nuclear@21 175 glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, lightlist[i].color);
nuclear@22 176 glEnable(GL_LIGHT0 + i);
nuclear@21 177 }
nuclear@21 178
nuclear@12 179 glDisable(GL_TEXTURE_2D);
nuclear@12 180 glEnable(GL_DEPTH_TEST);
John@15 181 glEnable(GL_LIGHTING);
nuclear@12 182
nuclear@12 183 glMatrixMode(GL_PROJECTION);
nuclear@12 184 glPushMatrix();
nuclear@12 185 glLoadIdentity();
nuclear@12 186 gluPerspective(45.0, (float)rinf.xsz / (float)rinf.ysz, 0.5, 1000.0);
nuclear@12 187
nuclear@27 188 if(show_obj) {
nuclear@27 189 Material *materials = scn->get_materials();
John@14 190
nuclear@27 191 int num_faces = scn->get_num_faces();
nuclear@27 192 int cur_mat = -1;
nuclear@21 193
nuclear@27 194 for(int i=0; i<num_faces; i++) {
nuclear@27 195 if(faces[i].matid != cur_mat) {
nuclear@27 196 if(cur_mat != -1) {
nuclear@27 197 glEnd();
nuclear@27 198 }
nuclear@27 199 dbg_set_gl_material(materials ? materials + faces[i].matid : 0);
nuclear@27 200 cur_mat = faces[i].matid;
nuclear@27 201 glBegin(GL_TRIANGLES);
nuclear@21 202 }
nuclear@27 203
nuclear@27 204 for(int j=0; j<3; j++) {
nuclear@27 205 glNormal3fv(faces[i].v[j].normal);
nuclear@27 206 glVertex3fv(faces[i].v[j].pos);
nuclear@27 207 }
John@14 208 }
nuclear@27 209 glEnd();
nuclear@27 210 }
nuclear@12 211
nuclear@27 212 if(show_tree) {
nuclear@27 213 scn->draw_kdtree();
nuclear@12 214 }
nuclear@12 215
nuclear@12 216 glPopMatrix();
nuclear@12 217 glPopAttrib();
nuclear@22 218
nuclear@22 219 assert(glGetError() == GL_NO_ERROR);
nuclear@12 220 }
nuclear@12 221
nuclear@12 222 void set_xform(float *matrix, float *invtrans)
nuclear@12 223 {
nuclear@12 224 CLMemBuffer *mbuf_xform = prog->get_arg_buffer(KARG_XFORM);
nuclear@12 225 CLMemBuffer *mbuf_invtrans = prog->get_arg_buffer(KARG_INVTRANS_XFORM);
nuclear@12 226 assert(mbuf_xform && mbuf_invtrans);
nuclear@12 227
nuclear@12 228 float *mem = (float*)map_mem_buffer(mbuf_xform, MAP_WR);
nuclear@12 229 memcpy(mem, matrix, 16 * sizeof *mem);
nuclear@12 230 unmap_mem_buffer(mbuf_xform);
nuclear@12 231
nuclear@12 232 mem = (float*)map_mem_buffer(mbuf_invtrans, MAP_WR);
nuclear@12 233 memcpy(mem, invtrans, 16 * sizeof *mem);
nuclear@12 234 unmap_mem_buffer(mbuf_invtrans);
nuclear@8 235 }
nuclear@8 236
nuclear@3 237 static Ray get_primary_ray(int x, int y, int w, int h, float vfov_deg)
nuclear@2 238 {
nuclear@2 239 float vfov = M_PI * vfov_deg / 180.0;
nuclear@2 240 float aspect = (float)w / (float)h;
nuclear@2 241
nuclear@2 242 float ysz = 2.0;
nuclear@2 243 float xsz = aspect * ysz;
nuclear@2 244
nuclear@2 245 float px = ((float)x / (float)w) * xsz - xsz / 2.0;
nuclear@2 246 float py = 1.0 - ((float)y / (float)h) * ysz;
nuclear@2 247 float pz = 1.0 / tan(0.5 * vfov);
nuclear@2 248
nuclear@4 249 px *= 100.0;
nuclear@4 250 py *= 100.0;
nuclear@4 251 pz *= 100.0;
nuclear@2 252
nuclear@18 253 Ray ray = {{0, 0, 0, 1}, {px, py, -pz, 1}};
nuclear@2 254 return ray;
nuclear@2 255 }