clray

view src/rt.cc @ 34:a218551293ad

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