clray

annotate src/rt.cc @ 58:3d13924b22e6

implementing polygon split
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 12 Sep 2010 00:19:04 +0100
parents df239a52a091
children 8c858e1a89e8
rev   line source
nuclear@0 1 #include <stdio.h>
nuclear@8 2 #include <string.h>
nuclear@2 3 #include <math.h>
nuclear@58 4 #include <limits.h>
nuclear@0 5 #include <assert.h>
nuclear@47 6 #include "rt.h"
John@14 7 #include "ogl.h"
nuclear@0 8 #include "ocl.h"
nuclear@22 9 #include "scene.h"
nuclear@32 10 #include "timer.h"
nuclear@45 11 #include "common.h"
nuclear@0 12
nuclear@12 13 // kernel arguments
nuclear@12 14 enum {
nuclear@12 15 KARG_FRAMEBUFFER,
nuclear@12 16 KARG_RENDER_INFO,
nuclear@12 17 KARG_FACES,
nuclear@12 18 KARG_MATLIB,
nuclear@12 19 KARG_LIGHTS,
nuclear@12 20 KARG_PRIM_RAYS,
nuclear@12 21 KARG_XFORM,
John@14 22 KARG_INVTRANS_XFORM,
nuclear@28 23 KARG_KDTREE,
John@14 24
John@14 25 NUM_KERNEL_ARGS
nuclear@12 26 };
John@11 27
nuclear@47 28 static void update_render_info();
nuclear@3 29 static Ray get_primary_ray(int x, int y, int w, int h, float vfov_deg);
nuclear@43 30 static float *create_kdimage(const KDNodeGPU *kdtree, int num_nodes, int *xsz_ret, int *ysz_ret);
nuclear@3 31
nuclear@13 32 static Face *faces;
nuclear@3 33 static Ray *prim_rays;
nuclear@3 34 static CLProgram *prog;
nuclear@3 35 static int global_size;
nuclear@3 36
nuclear@7 37
nuclear@4 38 static RendInfo rinf;
nuclear@55 39 static RenderStats rstat;
nuclear@47 40 static int saved_iter_val;
nuclear@4 41
nuclear@43 42 static long timing_sample_sum;
nuclear@43 43 static long num_timing_samples;
nuclear@43 44
nuclear@4 45
nuclear@39 46 bool init_renderer(int xsz, int ysz, Scene *scn, unsigned int tex)
nuclear@0 47 {
nuclear@4 48 // render info
nuclear@22 49 rinf.ambient[0] = rinf.ambient[1] = rinf.ambient[2] = 0.0;
nuclear@16 50 rinf.ambient[3] = 0.0;
nuclear@16 51
nuclear@4 52 rinf.xsz = xsz;
nuclear@4 53 rinf.ysz = ysz;
nuclear@13 54 rinf.num_faces = scn->get_num_faces();
nuclear@54 55 rinf.num_lights = scn->get_num_lights();
nuclear@47 56 rinf.max_iter = saved_iter_val = 6;
nuclear@47 57 rinf.cast_shadows = true;
nuclear@4 58
nuclear@3 59 /* calculate primary rays */
nuclear@3 60 prim_rays = new Ray[xsz * ysz];
nuclear@2 61
nuclear@2 62 for(int i=0; i<ysz; i++) {
nuclear@2 63 for(int j=0; j<xsz; j++) {
nuclear@2 64 prim_rays[i * xsz + j] = get_primary_ray(j, i, xsz, ysz, 45.0);
nuclear@2 65 }
nuclear@0 66 }
nuclear@54 67 dbg_set_primary_rays(prim_rays); // give them to the debug renderer
nuclear@0 68
nuclear@2 69 /* setup opencl */
nuclear@3 70 prog = new CLProgram("render");
nuclear@54 71 if(!prog->load("src/rt.cl")) {
nuclear@8 72 return false;
nuclear@0 73 }
nuclear@0 74
nuclear@24 75 if(!(faces = (Face*)scn->get_face_buffer())) {
nuclear@13 76 fprintf(stderr, "failed to create face buffer\n");
nuclear@13 77 return false;
nuclear@13 78 }
nuclear@13 79
nuclear@28 80 const KDNodeGPU *kdbuf = scn->get_kdtree_buffer();
nuclear@28 81 if(!kdbuf) {
nuclear@28 82 fprintf(stderr, "failed to create kdtree buffer\n");
nuclear@28 83 return false;
nuclear@28 84 }
nuclear@43 85
nuclear@43 86 int kdimg_xsz, kdimg_ysz;
nuclear@43 87 float *kdimg_pixels = create_kdimage(kdbuf, scn->get_num_kdnodes(), &kdimg_xsz, &kdimg_ysz);
nuclear@28 88
nuclear@3 89 /* setup argument buffers */
nuclear@41 90 #ifdef CLGL_INTEROP
nuclear@39 91 prog->set_arg_texture(KARG_FRAMEBUFFER, ARG_WR, tex);
nuclear@41 92 #else
nuclear@41 93 prog->set_arg_image(KARG_FRAMEBUFFER, ARG_WR, xsz, ysz);
nuclear@41 94 #endif
nuclear@12 95 prog->set_arg_buffer(KARG_RENDER_INFO, ARG_RD, sizeof rinf, &rinf);
John@14 96 prog->set_arg_buffer(KARG_FACES, ARG_RD, rinf.num_faces * sizeof(Face), faces);
John@14 97 prog->set_arg_buffer(KARG_MATLIB, ARG_RD, scn->get_num_materials() * sizeof(Material), scn->get_materials());
nuclear@54 98 prog->set_arg_buffer(KARG_LIGHTS, ARG_RD, scn->get_num_lights() * sizeof(Light), scn->get_lights());
nuclear@12 99 prog->set_arg_buffer(KARG_PRIM_RAYS, ARG_RD, xsz * ysz * sizeof *prim_rays, prim_rays);
nuclear@12 100 prog->set_arg_buffer(KARG_XFORM, ARG_RD, 16 * sizeof(float));
nuclear@12 101 prog->set_arg_buffer(KARG_INVTRANS_XFORM, ARG_RD, 16 * sizeof(float));
nuclear@43 102 //prog->set_arg_buffer(KARG_KDTREE, ARG_RD, scn->get_num_kdnodes() * sizeof *kdbuf, kdbuf);
nuclear@43 103 prog->set_arg_image(KARG_KDTREE, ARG_RD, kdimg_xsz, kdimg_ysz, kdimg_pixels);
nuclear@43 104
nuclear@43 105 delete [] kdimg_pixels;
nuclear@43 106
nuclear@12 107
John@14 108 if(prog->get_num_args() < NUM_KERNEL_ARGS) {
John@14 109 return false;
John@14 110 }
John@14 111
nuclear@45 112 const char *opt = "-Isrc -cl-mad-enable -cl-single-precision-constant -cl-fast-relaxed-math";
nuclear@45 113 if(!prog->build(opt)) {
nuclear@16 114 return false;
nuclear@16 115 }
nuclear@16 116
nuclear@54 117 //delete [] prim_rays; now dbg_renderer handles them
nuclear@2 118
nuclear@3 119 global_size = xsz * ysz;
nuclear@54 120
nuclear@54 121
nuclear@54 122 init_dbg_renderer(xsz, ysz, scn, tex);
nuclear@3 123 return true;
nuclear@3 124 }
nuclear@3 125
nuclear@3 126 void destroy_renderer()
nuclear@3 127 {
nuclear@3 128 delete prog;
nuclear@43 129
nuclear@54 130 destroy_dbg_renderer();
nuclear@54 131
nuclear@54 132 if(num_timing_samples) {
nuclear@54 133 printf("rendertime mean: %ld msec\n", timing_sample_sum / num_timing_samples);
nuclear@54 134 }
nuclear@3 135 }
nuclear@3 136
nuclear@3 137 bool render()
nuclear@3 138 {
nuclear@58 139 long tm0 = get_msec();
nuclear@39 140
nuclear@58 141 // initialize render-stats
nuclear@58 142 memset(&rstat, 0, sizeof rstat);
nuclear@58 143 rstat.min_aabb_tests = rstat.min_triangle_tests = INT_MAX;
nuclear@58 144 rstat.max_aabb_tests = rstat.max_triangle_tests = 0;
nuclear@32 145
nuclear@40 146 #ifdef CLGL_INTEROP
nuclear@39 147 cl_event ev;
nuclear@39 148 CLMemBuffer *texbuf = prog->get_arg_buffer(KARG_FRAMEBUFFER);
nuclear@39 149
nuclear@39 150 if(!acquire_gl_object(texbuf, &ev)) {
nuclear@39 151 return false;
nuclear@39 152 }
nuclear@39 153
nuclear@39 154 // make sure that we will wait for the acquire to finish before running
nuclear@39 155 prog->set_wait_event(ev);
nuclear@40 156 #endif
nuclear@39 157
nuclear@3 158 if(!prog->run(1, global_size)) {
nuclear@3 159 return false;
nuclear@0 160 }
John@15 161
nuclear@40 162 #ifdef CLGL_INTEROP
nuclear@39 163 if(!release_gl_object(texbuf, &ev)) {
nuclear@39 164 return false;
nuclear@39 165 }
nuclear@39 166 clWaitForEvents(1, &ev);
nuclear@40 167 #endif
nuclear@39 168
nuclear@40 169 #ifndef CLGL_INTEROP
nuclear@40 170 /* if we don't compile in CL/GL interoperability support, we need
nuclear@40 171 * to copy the output buffer to the OpenGL texture used to displaying
nuclear@40 172 * the image.
nuclear@40 173 */
nuclear@13 174 CLMemBuffer *mbuf = prog->get_arg_buffer(KARG_FRAMEBUFFER);
nuclear@12 175 void *fb = map_mem_buffer(mbuf, MAP_RD);
nuclear@13 176 if(!fb) {
nuclear@13 177 fprintf(stderr, "FAILED\n");
nuclear@13 178 return false;
nuclear@13 179 }
nuclear@13 180
nuclear@12 181 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rinf.xsz, rinf.ysz, GL_RGBA, GL_FLOAT, fb);
nuclear@2 182 unmap_mem_buffer(mbuf);
nuclear@40 183 #endif
nuclear@32 184
nuclear@58 185 rstat.render_time = get_msec() - tm0;
nuclear@58 186
nuclear@58 187 timing_sample_sum += rstat.render_time;
nuclear@43 188 num_timing_samples++;
nuclear@43 189
nuclear@3 190 return true;
nuclear@0 191 }
nuclear@2 192
nuclear@12 193
nuclear@12 194 void set_xform(float *matrix, float *invtrans)
nuclear@12 195 {
nuclear@12 196 CLMemBuffer *mbuf_xform = prog->get_arg_buffer(KARG_XFORM);
nuclear@12 197 CLMemBuffer *mbuf_invtrans = prog->get_arg_buffer(KARG_INVTRANS_XFORM);
nuclear@12 198 assert(mbuf_xform && mbuf_invtrans);
nuclear@12 199
nuclear@12 200 float *mem = (float*)map_mem_buffer(mbuf_xform, MAP_WR);
nuclear@12 201 memcpy(mem, matrix, 16 * sizeof *mem);
nuclear@12 202 unmap_mem_buffer(mbuf_xform);
nuclear@12 203
nuclear@12 204 mem = (float*)map_mem_buffer(mbuf_invtrans, MAP_WR);
nuclear@12 205 memcpy(mem, invtrans, 16 * sizeof *mem);
nuclear@12 206 unmap_mem_buffer(mbuf_invtrans);
nuclear@8 207 }
nuclear@8 208
nuclear@54 209
nuclear@54 210 const RendInfo *get_render_info()
nuclear@54 211 {
nuclear@54 212 return &rinf;
nuclear@54 213 }
nuclear@54 214
nuclear@55 215 const RenderStats *get_render_stats()
nuclear@55 216 {
nuclear@55 217 return &rstat;
nuclear@55 218 }
nuclear@55 219
nuclear@55 220 void print_render_stats(FILE *fp)
nuclear@55 221 {
nuclear@55 222 fprintf(fp, "-- render stats --\n");
nuclear@55 223 fprintf(fp, "> timing\n");
nuclear@55 224 fprintf(fp, " render time (msec): %lu\n", rstat.render_time);
nuclear@55 225 fprintf(fp, " tex update time (msec): %lu\n", rstat.tex_update_time);
nuclear@55 226 fprintf(fp, "> counters\n");
nuclear@55 227 fprintf(fp, " AABB tests: %d\n", rstat.aabb_tests);
nuclear@55 228 fprintf(fp, " AABB tests per ray (min/max/avg): %d/%d/%f\n",
nuclear@55 229 rstat.min_aabb_tests, rstat.max_aabb_tests, rstat.avg_aabb_tests);
nuclear@55 230 fprintf(fp, " triangle tests: %d\n", rstat.triangle_tests);
nuclear@55 231 fprintf(fp, " triangle tests per ray (min/max/avg): %d/%d/%f\n",
nuclear@55 232 rstat.min_triangle_tests, rstat.max_triangle_tests, rstat.avg_triangle_tests);
nuclear@55 233 fprintf(fp, " rays cast: %dp %dr %ds (sum: %d)\n", rstat.prim_rays,
nuclear@55 234 rstat.refl_rays, rstat.shadow_rays, rstat.rays_cast);
nuclear@55 235 fprintf(fp, " rays per second: %d\n", rstat.rays_per_sec);
nuclear@55 236 fprintf(fp, " BRDF evaluations: %d\n", rstat.brdf_evals);
nuclear@55 237 fputc('\n', fp);
nuclear@55 238 }
nuclear@55 239
nuclear@47 240 void set_render_option(int opt, bool val)
nuclear@47 241 {
nuclear@47 242 switch(opt) {
nuclear@47 243 case ROPT_ITER:
nuclear@47 244 case ROPT_REFL:
nuclear@47 245 rinf.max_iter = val ? saved_iter_val : 0;
nuclear@47 246 break;
nuclear@47 247
nuclear@47 248 case ROPT_SHAD:
nuclear@47 249 rinf.cast_shadows = val;
nuclear@47 250 break;
nuclear@47 251
nuclear@47 252 default:
nuclear@47 253 return;
nuclear@47 254 }
nuclear@47 255
nuclear@47 256 update_render_info();
nuclear@47 257 }
nuclear@47 258
nuclear@47 259 void set_render_option(int opt, int val)
nuclear@47 260 {
nuclear@47 261 switch(opt) {
nuclear@47 262 case ROPT_ITER:
nuclear@47 263 rinf.max_iter = saved_iter_val = val;
nuclear@47 264 break;
nuclear@47 265
nuclear@47 266 case ROPT_SHAD:
nuclear@47 267 rinf.cast_shadows = val;
nuclear@47 268 break;
nuclear@47 269
nuclear@47 270 case ROPT_REFL:
nuclear@47 271 rinf.max_iter = val ? saved_iter_val : 0;
nuclear@47 272 break;
nuclear@47 273
nuclear@47 274 default:
nuclear@47 275 return;
nuclear@47 276 }
nuclear@47 277
nuclear@47 278 update_render_info();
nuclear@47 279 }
nuclear@47 280
nuclear@47 281 void set_render_option(int opt, float val)
nuclear@47 282 {
nuclear@47 283 set_render_option(opt, (int)val);
nuclear@47 284 }
nuclear@47 285
nuclear@47 286 bool get_render_option_bool(int opt)
nuclear@47 287 {
nuclear@47 288 switch(opt) {
nuclear@47 289 case ROPT_ITER:
nuclear@47 290 return rinf.max_iter;
nuclear@47 291 case ROPT_SHAD:
nuclear@47 292 return rinf.cast_shadows;
nuclear@47 293 case ROPT_REFL:
nuclear@47 294 return rinf.max_iter == saved_iter_val;
nuclear@47 295 default:
nuclear@47 296 break;
nuclear@47 297 }
nuclear@47 298 return false;
nuclear@47 299 }
nuclear@47 300
nuclear@47 301 int get_render_option_int(int opt)
nuclear@47 302 {
nuclear@47 303 switch(opt) {
nuclear@47 304 case ROPT_ITER:
nuclear@47 305 return rinf.max_iter;
nuclear@47 306 case ROPT_SHAD:
nuclear@47 307 return rinf.cast_shadows ? 1 : 0;
nuclear@47 308 case ROPT_REFL:
nuclear@47 309 return rinf.max_iter == saved_iter_val ? 1 : 0;
nuclear@47 310 default:
nuclear@47 311 break;
nuclear@47 312 }
nuclear@47 313 return -1;
nuclear@47 314 }
nuclear@47 315
nuclear@47 316 float get_render_option_float(int opt)
nuclear@47 317 {
nuclear@47 318 return (float)get_render_option_int(opt);
nuclear@47 319 }
nuclear@47 320
nuclear@47 321 static void update_render_info()
nuclear@47 322 {
nuclear@47 323 if(!prog) {
nuclear@47 324 return;
nuclear@47 325 }
nuclear@47 326
nuclear@47 327 CLMemBuffer *mbuf = prog->get_arg_buffer(KARG_RENDER_INFO);
nuclear@47 328 assert(mbuf);
nuclear@47 329
nuclear@47 330 RendInfo *rinf_ptr = (RendInfo*)map_mem_buffer(mbuf, MAP_WR);
nuclear@47 331 *rinf_ptr = rinf;
nuclear@47 332 unmap_mem_buffer(mbuf);
nuclear@47 333 }
nuclear@47 334
nuclear@3 335 static Ray get_primary_ray(int x, int y, int w, int h, float vfov_deg)
nuclear@2 336 {
nuclear@2 337 float vfov = M_PI * vfov_deg / 180.0;
nuclear@2 338 float aspect = (float)w / (float)h;
nuclear@2 339
nuclear@2 340 float ysz = 2.0;
nuclear@2 341 float xsz = aspect * ysz;
nuclear@2 342
nuclear@2 343 float px = ((float)x / (float)w) * xsz - xsz / 2.0;
nuclear@2 344 float py = 1.0 - ((float)y / (float)h) * ysz;
nuclear@2 345 float pz = 1.0 / tan(0.5 * vfov);
nuclear@2 346
nuclear@43 347 float mag = sqrt(px * px + py * py + pz * pz);
nuclear@43 348
nuclear@45 349 px = px * RAY_MAG / mag;
nuclear@45 350 py = py * RAY_MAG / mag;
nuclear@45 351 pz = pz * RAY_MAG / mag;
nuclear@2 352
nuclear@18 353 Ray ray = {{0, 0, 0, 1}, {px, py, -pz, 1}};
nuclear@2 354 return ray;
nuclear@2 355 }
nuclear@43 356
nuclear@54 357 #define MIN(a, b) ((a) < (b) ? (a) : (b))
nuclear@54 358
nuclear@43 359 static float *create_kdimage(const KDNodeGPU *kdtree, int num_nodes, int *xsz_ret, int *ysz_ret)
nuclear@43 360 {
nuclear@45 361 int ysz = MIN(num_nodes, KDIMG_MAX_HEIGHT);
nuclear@45 362 int columns = (num_nodes - 1) / KDIMG_MAX_HEIGHT + 1;
nuclear@45 363 int xsz = KDIMG_NODE_WIDTH * columns;
nuclear@43 364
nuclear@43 365 printf("creating kdtree image %dx%d (%d nodes)\n", xsz, ysz, num_nodes);
nuclear@43 366
nuclear@43 367 float *img = new float[4 * xsz * ysz];
nuclear@43 368 memset(img, 0, 4 * xsz * ysz * sizeof *img);
nuclear@43 369
nuclear@43 370 for(int i=0; i<num_nodes; i++) {
nuclear@45 371 int x = KDIMG_NODE_WIDTH * (i / KDIMG_MAX_HEIGHT);
nuclear@45 372 int y = i % KDIMG_MAX_HEIGHT;
nuclear@45 373
nuclear@45 374 float *ptr = img + (y * xsz + x) * 4;
nuclear@43 375
nuclear@43 376 *ptr++ = kdtree[i].aabb.min[0];
nuclear@43 377 *ptr++ = kdtree[i].aabb.min[1];
nuclear@43 378 *ptr++ = kdtree[i].aabb.min[2];
nuclear@43 379 *ptr++ = 0.0;
nuclear@43 380
nuclear@43 381 *ptr++ = kdtree[i].aabb.max[0];
nuclear@43 382 *ptr++ = kdtree[i].aabb.max[1];
nuclear@43 383 *ptr++ = kdtree[i].aabb.max[2];
nuclear@43 384 *ptr++ = 0.0;
nuclear@43 385
nuclear@43 386 for(int j=0; j<MAX_NODE_FACES; j++) {
nuclear@43 387 *ptr++ = j < kdtree[i].num_faces ? (float)kdtree[i].face_idx[j] : 0.0f;
nuclear@43 388 }
nuclear@43 389
nuclear@43 390 *ptr++ = (float)kdtree[i].num_faces;
nuclear@43 391 *ptr++ = (float)kdtree[i].left;
nuclear@43 392 *ptr++ = (float)kdtree[i].right;
nuclear@43 393 *ptr++ = 0.0;
nuclear@43 394 }
nuclear@43 395
nuclear@43 396 if(xsz_ret) *xsz_ret = xsz;
nuclear@43 397 if(ysz_ret) *ysz_ret = ysz;
nuclear@43 398 return img;
nuclear@43 399 }