clray

view src/rt.cc @ 28:97cfd9675310

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