clray
view src/rt.cc @ 15:754faf15ba36
burp
author | John Tsiombikas |
---|---|
date | Sun, 08 Aug 2010 09:51:45 +0100 |
parents | 29f9330cfa4b |
children | 9e4a28063394 |
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 int dbg;
29 };
31 struct Ray {
32 float origin[4], dir[4];
33 };
35 struct Light {
36 float pos[4], color[4];
37 };
39 static Ray get_primary_ray(int x, int y, int w, int h, float vfov_deg);
40 static Face *create_face_buffer(Mesh **meshes, int num_meshes);
42 static Face *faces;
43 static Ray *prim_rays;
44 static CLProgram *prog;
45 static int global_size;
47 static Light lightlist[] = {
48 {{-10, 10, -20, 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.xsz = xsz;
59 rinf.ysz = ysz;
60 rinf.num_faces = scn->get_num_faces();
61 rinf.num_lights = sizeof lightlist / sizeof *lightlist;
62 rinf.max_iter = 6;
63 rinf.dbg = 8;
65 /* calculate primary rays */
66 prim_rays = new Ray[xsz * ysz];
68 for(int i=0; i<ysz; i++) {
69 for(int j=0; j<xsz; j++) {
70 prim_rays[i * xsz + j] = get_primary_ray(j, i, xsz, ysz, 45.0);
71 }
72 }
74 /* setup opencl */
75 prog = new CLProgram("render");
76 if(!prog->load("rt.cl")) {
77 return false;
78 }
80 /*Face **/faces = create_face_buffer(&scn->meshes[0], scn->meshes.size());
81 if(!faces) {
82 fprintf(stderr, "failed to create face buffer\n");
83 return false;
84 }
86 /* setup argument buffers */
87 prog->set_arg_buffer(KARG_FRAMEBUFFER, ARG_WR, xsz * ysz * 4 * sizeof(float));
88 prog->set_arg_buffer(KARG_RENDER_INFO, ARG_RD, sizeof rinf, &rinf);
89 prog->set_arg_buffer(KARG_FACES, ARG_RD, rinf.num_faces * sizeof(Face), faces);
90 prog->set_arg_buffer(KARG_MATLIB, ARG_RD, scn->get_num_materials() * sizeof(Material), scn->get_materials());
91 prog->set_arg_buffer(KARG_LIGHTS, ARG_RD, sizeof lightlist, lightlist);
92 prog->set_arg_buffer(KARG_PRIM_RAYS, ARG_RD, xsz * ysz * sizeof *prim_rays, prim_rays);
93 prog->set_arg_buffer(KARG_XFORM, ARG_RD, 16 * sizeof(float));
94 prog->set_arg_buffer(KARG_INVTRANS_XFORM, ARG_RD, 16 * sizeof(float));
95 prog->set_arg_buffer(KARG_OUTFACES, ARG_WR, rinf.num_faces * sizeof(Face));
97 if(prog->get_num_args() < NUM_KERNEL_ARGS) {
98 return false;
99 }
101 delete [] prim_rays;
103 global_size = xsz * ysz;
104 return true;
105 }
107 void destroy_renderer()
108 {
109 delete prog;
110 }
112 bool render()
113 {
114 printf("Running kernel...");
115 fflush(stdout);
116 if(!prog->run(1, global_size)) {
117 return false;
118 }
119 printf("done\n");
121 /* DEBUG */
122 CLMemBuffer *dbgbuf = prog->get_arg_buffer(KARG_OUTFACES);
123 Face *outfaces = (Face*)map_mem_buffer(dbgbuf, MAP_RD);
124 for(int i=0; i<rinf.num_faces; i++) {
125 if(!(faces[i] == outfaces[i])) {
126 fprintf(stderr, "SKATA %d\n", i);
127 return false;
128 }
129 faces[i] = outfaces[i];
130 }
131 printf("equality test passed\n");
132 unmap_mem_buffer(dbgbuf);
135 CLMemBuffer *mbuf = prog->get_arg_buffer(KARG_FRAMEBUFFER);
136 void *fb = map_mem_buffer(mbuf, MAP_RD);
137 if(!fb) {
138 fprintf(stderr, "FAILED\n");
139 return false;
140 }
142 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rinf.xsz, rinf.ysz, GL_RGBA, GL_FLOAT, fb);
143 unmap_mem_buffer(mbuf);
144 return true;
145 }
147 void dbg_set_dbg(int dbg)
148 {
149 printf("setting dbg: %d\n", dbg);
151 CLMemBuffer *mbuf = prog->get_arg_buffer(KARG_RENDER_INFO);
152 RendInfo *rinf = (RendInfo*)map_mem_buffer(mbuf, MAP_WR);
153 rinf->dbg = dbg;
154 unmap_mem_buffer(mbuf);
155 }
157 void dbg_render_gl(Scene *scn)
158 {
159 float lpos[] = {-1, 1, 10, 0};
160 glPushAttrib(GL_ENABLE_BIT | GL_TRANSFORM_BIT);
162 glDisable(GL_TEXTURE_2D);
163 glEnable(GL_DEPTH_TEST);
164 glEnable(GL_LIGHTING);
165 glEnable(GL_LIGHT0);
166 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
167 glEnable(GL_COLOR_MATERIAL);
169 glMatrixMode(GL_PROJECTION);
170 glPushMatrix();
171 glLoadIdentity();
172 gluPerspective(45.0, (float)rinf.xsz / (float)rinf.ysz, 0.5, 1000.0);
174 Material *materials = scn->get_materials();
176 glBegin(GL_TRIANGLES);
177 int num_faces = scn->get_num_faces();
178 for(int i=0; i<num_faces; i++) {
179 Material *mat = materials ? materials + faces[i].matid : 0;
181 if(mat) {
182 glColor3f(mat->kd[0], mat->kd[1], mat->kd[2]);
183 } else {
184 glColor3f(1, 1, 1);
185 }
187 for(int j=0; j<3; j++) {
188 float *pos = faces[i].v[j].pos;
189 float *norm = faces[i].normal;
190 glNormal3fv(norm);
191 glVertex3fv(pos);
192 }
193 }
195 /*for(size_t i=0; i<scn->meshes.size(); i++) {
196 Material *mat = &scn->matlib[scn->meshes[i]->matid];
198 glColor3f(mat->kd[0], mat->kd[1], mat->kd[2]);
199 for(size_t j=0; j<scn->meshes[i]->faces.size(); j++) {
200 for(int k=0; k<3; k++) {
201 float *pos = scn->meshes[i]->faces[j].v[k].pos;
202 glVertex3f(pos[0], pos[1], pos[2]);
203 }
204 }
205 }*/
206 glEnd();
208 glPopMatrix();
209 glPopAttrib();
210 }
212 void set_xform(float *matrix, float *invtrans)
213 {
214 CLMemBuffer *mbuf_xform = prog->get_arg_buffer(KARG_XFORM);
215 CLMemBuffer *mbuf_invtrans = prog->get_arg_buffer(KARG_INVTRANS_XFORM);
216 assert(mbuf_xform && mbuf_invtrans);
218 float *mem = (float*)map_mem_buffer(mbuf_xform, MAP_WR);
219 memcpy(mem, matrix, 16 * sizeof *mem);
220 /*printf("-- xform:\n");
221 for(int i=0; i<16; i++) {
222 printf("%2.3f\t", mem[i]);
223 if(i % 4 == 3) putchar('\n');
224 }*/
225 unmap_mem_buffer(mbuf_xform);
227 mem = (float*)map_mem_buffer(mbuf_invtrans, MAP_WR);
228 memcpy(mem, invtrans, 16 * sizeof *mem);
229 /*printf("-- inverse-transpose:\n");
230 for(int i=0; i<16; i++) {
231 printf("%2.3f\t", mem[i]);
232 if(i % 4 == 3) putchar('\n');
233 }*/
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 }
257 static Face *create_face_buffer(Mesh **meshes, int num_meshes)
258 {
259 int num_faces = 0;
260 for(int i=0; i<num_meshes; i++) {
261 num_faces += meshes[i]->faces.size();
262 }
263 printf("constructing face buffer with %d faces (out of %d meshes)\n", num_faces, num_meshes);
265 Face *faces = new Face[num_faces];
266 memset(faces, 0, num_faces * sizeof *faces);
267 Face *fptr = faces;
269 for(int i=0; i<num_meshes; i++) {
270 for(size_t j=0; j<meshes[i]->faces.size(); j++) {
271 *fptr++ = meshes[i]->faces[j];
272 }
273 }
274 return faces;
275 }