clray

annotate src/rt.cc @ 39:980bc07be868

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