clray

view 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
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, unsigned int tex)
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_texture(KARG_FRAMEBUFFER, ARG_WR, tex);
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_num_kdnodes() * sizeof *kdbuf, 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 // XXX do we need to call glFinish ?
130 long tm0 = get_msec();
132 cl_event ev;
133 CLMemBuffer *texbuf = prog->get_arg_buffer(KARG_FRAMEBUFFER);
135 if(!acquire_gl_object(texbuf, &ev)) {
136 return false;
137 }
139 // make sure that we will wait for the acquire to finish before running
140 prog->set_wait_event(ev);
142 if(!prog->run(1, global_size)) {
143 return false;
144 }
146 if(!release_gl_object(texbuf, &ev)) {
147 return false;
148 }
149 clWaitForEvents(1, &ev);
151 printf("rendered in %ld msec\n", get_msec() - tm0);
153 /*long tm_run = get_msec() - tm0;
155 CLMemBuffer *mbuf = prog->get_arg_buffer(KARG_FRAMEBUFFER);
156 void *fb = map_mem_buffer(mbuf, MAP_RD);
157 if(!fb) {
158 fprintf(stderr, "FAILED\n");
159 return false;
160 }
162 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rinf.xsz, rinf.ysz, GL_RGBA, GL_FLOAT, fb);
163 unmap_mem_buffer(mbuf);
165 long tm_upd = get_msec() - tm0 - tm_run;
167 printf("render %ld msec (%ld run, %ld upd)\n", tm_run + tm_upd, tm_run, tm_upd);
168 */
169 return true;
170 }
172 #define MIN(a, b) ((a) < (b) ? (a) : (b))
173 static void dbg_set_gl_material(Material *mat)
174 {
175 static Material def_mat = {{0.7, 0.7, 0.7, 1}, {0, 0, 0, 0}, 0, 0, 0};
177 if(!mat) mat = &def_mat;
179 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, mat->kd);
180 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat->ks);
181 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, MIN(mat->spow, 128.0f));
182 }
184 void dbg_render_gl(Scene *scn, bool show_tree, bool show_obj)
185 {
186 glPushAttrib(GL_ENABLE_BIT | GL_TRANSFORM_BIT | GL_LIGHTING_BIT);
188 for(int i=0; i<rinf.num_lights; i++) {
189 float lpos[4];
191 memcpy(lpos, lightlist[i].pos, sizeof lpos);
192 lpos[3] = 1.0;
194 glLightfv(GL_LIGHT0 + i, GL_POSITION, lpos);
195 glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, lightlist[i].color);
196 glEnable(GL_LIGHT0 + i);
197 }
199 glDisable(GL_TEXTURE_2D);
200 glEnable(GL_DEPTH_TEST);
201 glEnable(GL_LIGHTING);
203 glMatrixMode(GL_PROJECTION);
204 glPushMatrix();
205 glLoadIdentity();
206 gluPerspective(45.0, (float)rinf.xsz / (float)rinf.ysz, 0.5, 1000.0);
208 if(show_obj) {
209 Material *materials = scn->get_materials();
211 int num_faces = scn->get_num_faces();
212 int cur_mat = -1;
214 for(int i=0; i<num_faces; i++) {
215 if(faces[i].matid != cur_mat) {
216 if(cur_mat != -1) {
217 glEnd();
218 }
219 dbg_set_gl_material(materials ? materials + faces[i].matid : 0);
220 cur_mat = faces[i].matid;
221 glBegin(GL_TRIANGLES);
222 }
224 for(int j=0; j<3; j++) {
225 glNormal3fv(faces[i].v[j].normal);
226 glVertex3fv(faces[i].v[j].pos);
227 }
228 }
229 glEnd();
230 }
232 if(show_tree) {
233 scn->draw_kdtree();
234 }
236 glPopMatrix();
237 glPopAttrib();
239 assert(glGetError() == GL_NO_ERROR);
240 }
242 void set_xform(float *matrix, float *invtrans)
243 {
244 CLMemBuffer *mbuf_xform = prog->get_arg_buffer(KARG_XFORM);
245 CLMemBuffer *mbuf_invtrans = prog->get_arg_buffer(KARG_INVTRANS_XFORM);
246 assert(mbuf_xform && mbuf_invtrans);
248 float *mem = (float*)map_mem_buffer(mbuf_xform, MAP_WR);
249 memcpy(mem, matrix, 16 * sizeof *mem);
250 unmap_mem_buffer(mbuf_xform);
252 mem = (float*)map_mem_buffer(mbuf_invtrans, MAP_WR);
253 memcpy(mem, invtrans, 16 * sizeof *mem);
254 unmap_mem_buffer(mbuf_invtrans);
255 }
257 static Ray get_primary_ray(int x, int y, int w, int h, float vfov_deg)
258 {
259 float vfov = M_PI * vfov_deg / 180.0;
260 float aspect = (float)w / (float)h;
262 float ysz = 2.0;
263 float xsz = aspect * ysz;
265 float px = ((float)x / (float)w) * xsz - xsz / 2.0;
266 float py = 1.0 - ((float)y / (float)h) * ysz;
267 float pz = 1.0 / tan(0.5 * vfov);
269 px *= 100.0;
270 py *= 100.0;
271 pz *= 100.0;
273 Ray ray = {{0, 0, 0, 1}, {px, py, -pz, 1}};
274 return ray;
275 }