clray

view src/rt.cc @ 18:4b1604f9798a

debugging...
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 09 Aug 2010 05:38:51 +0100
parents 9e4a28063394
children 8baea9b66b50
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 "mesh.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_OUTFACES, /* DBG */
21 NUM_KERNEL_ARGS
22 };
24 struct RendInfo {
25 int xsz, ysz;
26 int num_faces, num_lights;
27 int max_iter;
28 float ambient[4];
29 int dbg;
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);
41 static Face *create_face_buffer(Mesh **meshes, int num_meshes);
43 static Face *faces;
44 static Ray *prim_rays;
45 static CLProgram *prog;
46 static int global_size;
48 static Light lightlist[] = {
49 {{-10, 13, -20, 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.075;
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.dbg = 8;
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 /*Face **/faces = create_face_buffer(&scn->meshes[0], scn->meshes.size());
85 if(!faces) {
86 fprintf(stderr, "failed to create face buffer\n");
87 return false;
88 }
90 /* setup argument buffers */
91 prog->set_arg_buffer(KARG_FRAMEBUFFER, ARG_WR, xsz * ysz * 4 * sizeof(float));
92 prog->set_arg_buffer(KARG_RENDER_INFO, ARG_RD, sizeof rinf, &rinf);
93 prog->set_arg_buffer(KARG_FACES, ARG_RD, rinf.num_faces * sizeof(Face), faces);
94 prog->set_arg_buffer(KARG_MATLIB, ARG_RD, scn->get_num_materials() * sizeof(Material), scn->get_materials());
95 prog->set_arg_buffer(KARG_LIGHTS, ARG_RD, sizeof lightlist, lightlist);
96 prog->set_arg_buffer(KARG_PRIM_RAYS, ARG_RD, xsz * ysz * sizeof *prim_rays, prim_rays);
97 prog->set_arg_buffer(KARG_XFORM, ARG_RD, 16 * sizeof(float));
98 prog->set_arg_buffer(KARG_INVTRANS_XFORM, ARG_RD, 16 * sizeof(float));
99 prog->set_arg_buffer(KARG_OUTFACES, ARG_WR, rinf.num_faces * sizeof(Face));
101 if(prog->get_num_args() < NUM_KERNEL_ARGS) {
102 return false;
103 }
105 if(!prog->build()) {
106 return false;
107 }
109 delete [] prim_rays;
111 global_size = xsz * ysz;
112 return true;
113 }
115 void destroy_renderer()
116 {
117 delete prog;
118 }
120 bool render()
121 {
122 printf("Running kernel...");
123 fflush(stdout);
124 if(!prog->run(1, global_size)) {
125 return false;
126 }
127 printf("done\n");
129 /* DEBUG */
130 CLMemBuffer *dbgbuf = prog->get_arg_buffer(KARG_OUTFACES);
131 Face *outfaces = (Face*)map_mem_buffer(dbgbuf, MAP_RD);
132 for(int i=0; i<rinf.num_faces; i++) {
133 if(!(faces[i] == outfaces[i])) {
134 fprintf(stderr, "SKATA %d\n", i);
135 return false;
136 }
137 faces[i] = outfaces[i];
138 }
139 printf("equality test passed\n");
140 unmap_mem_buffer(dbgbuf);
143 CLMemBuffer *mbuf = prog->get_arg_buffer(KARG_FRAMEBUFFER);
144 void *fb = map_mem_buffer(mbuf, MAP_RD);
145 if(!fb) {
146 fprintf(stderr, "FAILED\n");
147 return false;
148 }
150 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rinf.xsz, rinf.ysz, GL_RGBA, GL_FLOAT, fb);
151 unmap_mem_buffer(mbuf);
152 return true;
153 }
155 void dbg_set_dbg(int dbg)
156 {
157 printf("setting dbg: %d\n", dbg);
159 CLMemBuffer *mbuf = prog->get_arg_buffer(KARG_RENDER_INFO);
160 RendInfo *rinf = (RendInfo*)map_mem_buffer(mbuf, MAP_WR);
161 rinf->dbg = dbg;
162 unmap_mem_buffer(mbuf);
163 }
165 void dbg_render_gl(Scene *scn)
166 {
167 float lpos[] = {-1, 1, 10, 0};
168 glPushAttrib(GL_ENABLE_BIT | GL_TRANSFORM_BIT);
170 glDisable(GL_TEXTURE_2D);
171 glEnable(GL_DEPTH_TEST);
172 glEnable(GL_LIGHTING);
173 glEnable(GL_LIGHT0);
174 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
175 glEnable(GL_COLOR_MATERIAL);
177 glMatrixMode(GL_PROJECTION);
178 glPushMatrix();
179 glLoadIdentity();
180 gluPerspective(45.0, (float)rinf.xsz / (float)rinf.ysz, 0.5, 1000.0);
182 Material *materials = scn->get_materials();
184 glBegin(GL_TRIANGLES);
185 int num_faces = scn->get_num_faces();
186 for(int i=0; i<num_faces; i++) {
187 Material *mat = materials ? materials + faces[i].matid : 0;
189 if(mat) {
190 glColor3f(mat->kd[0], mat->kd[1], mat->kd[2]);
191 } else {
192 glColor3f(1, 1, 1);
193 }
195 for(int j=0; j<3; j++) {
196 float *pos = faces[i].v[j].pos;
197 float *norm = faces[i].normal;
198 glNormal3fv(norm);
199 glVertex3fv(pos);
200 }
201 }
203 /*for(size_t i=0; i<scn->meshes.size(); i++) {
204 Material *mat = &scn->matlib[scn->meshes[i]->matid];
206 glColor3f(mat->kd[0], mat->kd[1], mat->kd[2]);
207 for(size_t j=0; j<scn->meshes[i]->faces.size(); j++) {
208 for(int k=0; k<3; k++) {
209 float *pos = scn->meshes[i]->faces[j].v[k].pos;
210 glVertex3f(pos[0], pos[1], pos[2]);
211 }
212 }
213 }*/
214 glEnd();
216 glPopMatrix();
217 glPopAttrib();
218 }
220 void set_xform(float *matrix, float *invtrans)
221 {
222 CLMemBuffer *mbuf_xform = prog->get_arg_buffer(KARG_XFORM);
223 CLMemBuffer *mbuf_invtrans = prog->get_arg_buffer(KARG_INVTRANS_XFORM);
224 assert(mbuf_xform && mbuf_invtrans);
226 float *mem = (float*)map_mem_buffer(mbuf_xform, MAP_WR);
227 memcpy(mem, matrix, 16 * sizeof *mem);
228 /*printf("-- xform:\n");
229 for(int i=0; i<16; i++) {
230 printf("%2.3f\t", mem[i]);
231 if(i % 4 == 3) putchar('\n');
232 }*/
233 unmap_mem_buffer(mbuf_xform);
235 mem = (float*)map_mem_buffer(mbuf_invtrans, MAP_WR);
236 memcpy(mem, invtrans, 16 * sizeof *mem);
237 /*printf("-- inverse-transpose:\n");
238 for(int i=0; i<16; i++) {
239 printf("%2.3f\t", mem[i]);
240 if(i % 4 == 3) putchar('\n');
241 }*/
242 unmap_mem_buffer(mbuf_invtrans);
243 }
245 static Ray get_primary_ray(int x, int y, int w, int h, float vfov_deg)
246 {
247 float vfov = M_PI * vfov_deg / 180.0;
248 float aspect = (float)w / (float)h;
250 float ysz = 2.0;
251 float xsz = aspect * ysz;
253 float px = ((float)x / (float)w) * xsz - xsz / 2.0;
254 float py = 1.0 - ((float)y / (float)h) * ysz;
255 float pz = 1.0 / tan(0.5 * vfov);
257 px *= 100.0;
258 py *= 100.0;
259 pz *= 100.0;
261 Ray ray = {{0, 0, 0, 1}, {px, py, -pz, 1}};
262 return ray;
263 }
265 static Face *create_face_buffer(Mesh **meshes, int num_meshes)
266 {
267 int num_faces = 0;
268 for(int i=0; i<num_meshes; i++) {
269 num_faces += meshes[i]->faces.size();
270 }
271 printf("constructing face buffer with %d faces (out of %d meshes)\n", num_faces, num_meshes);
273 Face *faces = new Face[num_faces];
274 memset(faces, 0, num_faces * sizeof *faces);
275 Face *fptr = faces;
277 for(int i=0; i<num_meshes; i++) {
278 for(size_t j=0; j<meshes[i]->faces.size(); j++) {
279 *fptr++ = meshes[i]->faces[j];
280 }
281 }
282 return faces;
283 }