clray

annotate src/rt.cc @ 29:353d80127627

doh ... it doesn't work
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 21 Aug 2010 20:51:57 +0100
parents 97cfd9675310
children 4cf4919c3812
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@0 8
nuclear@12 9 // kernel arguments
nuclear@12 10 enum {
nuclear@12 11 KARG_FRAMEBUFFER,
nuclear@12 12 KARG_RENDER_INFO,
nuclear@12 13 KARG_FACES,
nuclear@12 14 KARG_MATLIB,
nuclear@12 15 KARG_LIGHTS,
nuclear@12 16 KARG_PRIM_RAYS,
nuclear@12 17 KARG_XFORM,
John@14 18 KARG_INVTRANS_XFORM,
nuclear@28 19 KARG_KDTREE,
John@14 20
John@14 21 NUM_KERNEL_ARGS
nuclear@12 22 };
John@11 23
nuclear@2 24 struct RendInfo {
nuclear@22 25 float ambient[4];
nuclear@2 26 int xsz, ysz;
nuclear@9 27 int num_faces, num_lights;
nuclear@2 28 int max_iter;
nuclear@28 29 int kd_depth;
nuclear@12 30 };
nuclear@2 31
nuclear@1 32 struct Ray {
nuclear@8 33 float origin[4], dir[4];
nuclear@12 34 };
nuclear@1 35
nuclear@3 36 struct Light {
nuclear@8 37 float pos[4], color[4];
nuclear@12 38 };
nuclear@1 39
nuclear@3 40 static Ray get_primary_ray(int x, int y, int w, int h, float vfov_deg);
nuclear@3 41
nuclear@13 42 static Face *faces;
nuclear@3 43 static Ray *prim_rays;
nuclear@3 44 static CLProgram *prog;
nuclear@3 45 static int global_size;
nuclear@3 46
nuclear@4 47 static Light lightlist[] = {
nuclear@22 48 {{-8, 15, 18, 0}, {1, 1, 1, 1}}
nuclear@4 49 };
nuclear@4 50
nuclear@7 51
nuclear@4 52 static RendInfo rinf;
nuclear@4 53
nuclear@4 54
nuclear@13 55 bool init_renderer(int xsz, int ysz, Scene *scn)
nuclear@0 56 {
nuclear@4 57 // render info
nuclear@22 58 rinf.ambient[0] = rinf.ambient[1] = rinf.ambient[2] = 0.0;
nuclear@16 59 rinf.ambient[3] = 0.0;
nuclear@16 60
nuclear@4 61 rinf.xsz = xsz;
nuclear@4 62 rinf.ysz = ysz;
nuclear@13 63 rinf.num_faces = scn->get_num_faces();
nuclear@4 64 rinf.num_lights = sizeof lightlist / sizeof *lightlist;
nuclear@4 65 rinf.max_iter = 6;
nuclear@28 66 rinf.kd_depth = kdtree_depth(scn->kdtree);
nuclear@4 67
nuclear@3 68 /* calculate primary rays */
nuclear@3 69 prim_rays = new Ray[xsz * ysz];
nuclear@2 70
nuclear@2 71 for(int i=0; i<ysz; i++) {
nuclear@2 72 for(int j=0; j<xsz; j++) {
nuclear@2 73 prim_rays[i * xsz + j] = get_primary_ray(j, i, xsz, ysz, 45.0);
nuclear@2 74 }
nuclear@0 75 }
nuclear@0 76
nuclear@2 77 /* setup opencl */
nuclear@3 78 prog = new CLProgram("render");
nuclear@3 79 if(!prog->load("rt.cl")) {
nuclear@8 80 return false;
nuclear@0 81 }
nuclear@0 82
nuclear@24 83 if(!(faces = (Face*)scn->get_face_buffer())) {
nuclear@13 84 fprintf(stderr, "failed to create face buffer\n");
nuclear@13 85 return false;
nuclear@13 86 }
nuclear@13 87
nuclear@28 88 const KDNodeGPU *kdbuf = scn->get_kdtree_buffer();
nuclear@28 89 if(!kdbuf) {
nuclear@28 90 fprintf(stderr, "failed to create kdtree buffer\n");
nuclear@28 91 return false;
nuclear@28 92 }
nuclear@28 93
nuclear@3 94 /* setup argument buffers */
nuclear@12 95 prog->set_arg_buffer(KARG_FRAMEBUFFER, ARG_WR, xsz * ysz * 4 * sizeof(float));
nuclear@12 96 prog->set_arg_buffer(KARG_RENDER_INFO, ARG_RD, sizeof rinf, &rinf);
John@14 97 prog->set_arg_buffer(KARG_FACES, ARG_RD, rinf.num_faces * sizeof(Face), faces);
John@14 98 prog->set_arg_buffer(KARG_MATLIB, ARG_RD, scn->get_num_materials() * sizeof(Material), scn->get_materials());
nuclear@12 99 prog->set_arg_buffer(KARG_LIGHTS, ARG_RD, sizeof lightlist, lightlist);
nuclear@12 100 prog->set_arg_buffer(KARG_PRIM_RAYS, ARG_RD, xsz * ysz * sizeof *prim_rays, prim_rays);
nuclear@12 101 prog->set_arg_buffer(KARG_XFORM, ARG_RD, 16 * sizeof(float));
nuclear@12 102 prog->set_arg_buffer(KARG_INVTRANS_XFORM, ARG_RD, 16 * sizeof(float));
nuclear@29 103 prog->set_arg_buffer(KARG_KDTREE, ARG_RD, scn->get_kdtree_buffer_size(), kdbuf);
nuclear@12 104
John@14 105 if(prog->get_num_args() < NUM_KERNEL_ARGS) {
John@14 106 return false;
John@14 107 }
John@14 108
nuclear@16 109 if(!prog->build()) {
nuclear@16 110 return false;
nuclear@16 111 }
nuclear@16 112
nuclear@12 113 delete [] prim_rays;
nuclear@2 114
nuclear@3 115 global_size = xsz * ysz;
nuclear@3 116 return true;
nuclear@3 117 }
nuclear@3 118
nuclear@3 119 void destroy_renderer()
nuclear@3 120 {
nuclear@3 121 delete prog;
nuclear@3 122 }
nuclear@3 123
nuclear@3 124 bool render()
nuclear@3 125 {
nuclear@3 126 if(!prog->run(1, global_size)) {
nuclear@3 127 return false;
nuclear@0 128 }
John@15 129
nuclear@13 130 CLMemBuffer *mbuf = prog->get_arg_buffer(KARG_FRAMEBUFFER);
nuclear@12 131 void *fb = map_mem_buffer(mbuf, MAP_RD);
nuclear@13 132 if(!fb) {
nuclear@13 133 fprintf(stderr, "FAILED\n");
nuclear@13 134 return false;
nuclear@13 135 }
nuclear@13 136
nuclear@22 137 static int foo = 0;
nuclear@22 138 if(!foo++) {
nuclear@22 139 bool write_ppm(const char *fname, float *fb, int xsz, int ysz);
nuclear@22 140 write_ppm("foo.ppm", (float*)fb, rinf.xsz, rinf.ysz);
nuclear@22 141 }
nuclear@22 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@3 145 return true;
nuclear@0 146 }
nuclear@2 147
nuclear@27 148 #define MIN(a, b) ((a) < (b) ? (a) : (b))
nuclear@21 149 static void dbg_set_gl_material(Material *mat)
nuclear@21 150 {
nuclear@21 151 static Material def_mat = {{0.7, 0.7, 0.7, 1}, {0, 0, 0, 0}, 0, 0, 0};
nuclear@21 152
nuclear@21 153 if(!mat) mat = &def_mat;
nuclear@21 154
nuclear@21 155 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, mat->kd);
nuclear@21 156 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat->ks);
nuclear@27 157 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, MIN(mat->spow, 128.0f));
nuclear@21 158 }
nuclear@21 159
nuclear@27 160 void dbg_render_gl(Scene *scn, bool show_tree, bool show_obj)
nuclear@8 161 {
nuclear@22 162 glPushAttrib(GL_ENABLE_BIT | GL_TRANSFORM_BIT | GL_LIGHTING_BIT);
nuclear@8 163
nuclear@21 164 for(int i=0; i<rinf.num_lights; i++) {
nuclear@21 165 float lpos[4];
nuclear@21 166
nuclear@21 167 memcpy(lpos, lightlist[i].pos, sizeof lpos);
nuclear@21 168 lpos[3] = 1.0;
nuclear@21 169
nuclear@21 170 glLightfv(GL_LIGHT0 + i, GL_POSITION, lpos);
nuclear@21 171 glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, lightlist[i].color);
nuclear@22 172 glEnable(GL_LIGHT0 + i);
nuclear@21 173 }
nuclear@21 174
nuclear@12 175 glDisable(GL_TEXTURE_2D);
nuclear@12 176 glEnable(GL_DEPTH_TEST);
John@15 177 glEnable(GL_LIGHTING);
nuclear@12 178
nuclear@12 179 glMatrixMode(GL_PROJECTION);
nuclear@12 180 glPushMatrix();
nuclear@12 181 glLoadIdentity();
nuclear@12 182 gluPerspective(45.0, (float)rinf.xsz / (float)rinf.ysz, 0.5, 1000.0);
nuclear@12 183
nuclear@27 184 if(show_obj) {
nuclear@27 185 Material *materials = scn->get_materials();
John@14 186
nuclear@27 187 int num_faces = scn->get_num_faces();
nuclear@27 188 int cur_mat = -1;
nuclear@21 189
nuclear@27 190 for(int i=0; i<num_faces; i++) {
nuclear@27 191 if(faces[i].matid != cur_mat) {
nuclear@27 192 if(cur_mat != -1) {
nuclear@27 193 glEnd();
nuclear@27 194 }
nuclear@27 195 dbg_set_gl_material(materials ? materials + faces[i].matid : 0);
nuclear@27 196 cur_mat = faces[i].matid;
nuclear@27 197 glBegin(GL_TRIANGLES);
nuclear@21 198 }
nuclear@27 199
nuclear@27 200 for(int j=0; j<3; j++) {
nuclear@27 201 glNormal3fv(faces[i].v[j].normal);
nuclear@27 202 glVertex3fv(faces[i].v[j].pos);
nuclear@27 203 }
John@14 204 }
nuclear@27 205 glEnd();
nuclear@27 206 }
nuclear@12 207
nuclear@27 208 if(show_tree) {
nuclear@27 209 scn->draw_kdtree();
nuclear@12 210 }
nuclear@12 211
nuclear@12 212 glPopMatrix();
nuclear@12 213 glPopAttrib();
nuclear@22 214
nuclear@22 215 assert(glGetError() == GL_NO_ERROR);
nuclear@12 216 }
nuclear@12 217
nuclear@12 218 void set_xform(float *matrix, float *invtrans)
nuclear@12 219 {
nuclear@12 220 CLMemBuffer *mbuf_xform = prog->get_arg_buffer(KARG_XFORM);
nuclear@12 221 CLMemBuffer *mbuf_invtrans = prog->get_arg_buffer(KARG_INVTRANS_XFORM);
nuclear@12 222 assert(mbuf_xform && mbuf_invtrans);
nuclear@12 223
nuclear@12 224 float *mem = (float*)map_mem_buffer(mbuf_xform, MAP_WR);
nuclear@12 225 memcpy(mem, matrix, 16 * sizeof *mem);
nuclear@12 226 unmap_mem_buffer(mbuf_xform);
nuclear@12 227
nuclear@12 228 mem = (float*)map_mem_buffer(mbuf_invtrans, MAP_WR);
nuclear@12 229 memcpy(mem, invtrans, 16 * sizeof *mem);
nuclear@12 230 unmap_mem_buffer(mbuf_invtrans);
nuclear@8 231 }
nuclear@8 232
nuclear@3 233 static Ray get_primary_ray(int x, int y, int w, int h, float vfov_deg)
nuclear@2 234 {
nuclear@2 235 float vfov = M_PI * vfov_deg / 180.0;
nuclear@2 236 float aspect = (float)w / (float)h;
nuclear@2 237
nuclear@2 238 float ysz = 2.0;
nuclear@2 239 float xsz = aspect * ysz;
nuclear@2 240
nuclear@2 241 float px = ((float)x / (float)w) * xsz - xsz / 2.0;
nuclear@2 242 float py = 1.0 - ((float)y / (float)h) * ysz;
nuclear@2 243 float pz = 1.0 / tan(0.5 * vfov);
nuclear@2 244
nuclear@4 245 px *= 100.0;
nuclear@4 246 py *= 100.0;
nuclear@4 247 pz *= 100.0;
nuclear@2 248
nuclear@18 249 Ray ray = {{0, 0, 0, 1}, {px, py, -pz, 1}};
nuclear@2 250 return ray;
nuclear@2 251 }