clray
view src/rt.cc @ 16:9e4a28063394
cl compiler segfaults...
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 09 Aug 2010 04:18:21 +0100 |
parents | 754faf15ba36 |
children | 4b1604f9798a |
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 float energy;
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);
42 static Face *create_face_buffer(Mesh **meshes, int num_meshes);
44 static Face *faces;
45 static Ray *prim_rays;
46 static CLProgram *prog;
47 static int global_size;
49 static Light lightlist[] = {
50 {{-10, 13, -20, 0}, {1, 1, 1, 1}}
51 };
54 static RendInfo rinf;
57 bool init_renderer(int xsz, int ysz, Scene *scn)
58 {
59 // render info
60 rinf.ambient[0] = rinf.ambient[1] = rinf.ambient[2] = 0.075;
61 rinf.ambient[3] = 0.0;
63 rinf.xsz = xsz;
64 rinf.ysz = ysz;
65 rinf.num_faces = scn->get_num_faces();
66 rinf.num_lights = sizeof lightlist / sizeof *lightlist;
67 rinf.max_iter = 6;
68 rinf.dbg = 8;
70 /* calculate primary rays */
71 prim_rays = new Ray[xsz * ysz];
73 for(int i=0; i<ysz; i++) {
74 for(int j=0; j<xsz; j++) {
75 prim_rays[i * xsz + j] = get_primary_ray(j, i, xsz, ysz, 45.0);
76 }
77 }
79 /* setup opencl */
80 prog = new CLProgram("render");
81 if(!prog->load("rt.cl")) {
82 return false;
83 }
85 /*Face **/faces = create_face_buffer(&scn->meshes[0], scn->meshes.size());
86 if(!faces) {
87 fprintf(stderr, "failed to create face buffer\n");
88 return false;
89 }
91 /* setup argument buffers */
92 prog->set_arg_buffer(KARG_FRAMEBUFFER, ARG_WR, xsz * ysz * 4 * sizeof(float));
93 prog->set_arg_buffer(KARG_RENDER_INFO, ARG_RD, sizeof rinf, &rinf);
94 prog->set_arg_buffer(KARG_FACES, ARG_RD, rinf.num_faces * sizeof(Face), faces);
95 prog->set_arg_buffer(KARG_MATLIB, ARG_RD, scn->get_num_materials() * sizeof(Material), scn->get_materials());
96 prog->set_arg_buffer(KARG_LIGHTS, ARG_RD, sizeof lightlist, lightlist);
97 prog->set_arg_buffer(KARG_PRIM_RAYS, ARG_RD, xsz * ysz * sizeof *prim_rays, prim_rays);
98 prog->set_arg_buffer(KARG_XFORM, ARG_RD, 16 * sizeof(float));
99 prog->set_arg_buffer(KARG_INVTRANS_XFORM, ARG_RD, 16 * sizeof(float));
100 prog->set_arg_buffer(KARG_OUTFACES, ARG_WR, rinf.num_faces * sizeof(Face));
102 if(prog->get_num_args() < NUM_KERNEL_ARGS) {
103 return false;
104 }
106 if(!prog->build()) {
107 return false;
108 }
110 delete [] prim_rays;
112 global_size = xsz * ysz;
113 return true;
114 }
116 void destroy_renderer()
117 {
118 delete prog;
119 }
121 bool render()
122 {
123 printf("Running kernel...");
124 fflush(stdout);
125 if(!prog->run(1, global_size)) {
126 return false;
127 }
128 printf("done\n");
130 /* DEBUG */
131 CLMemBuffer *dbgbuf = prog->get_arg_buffer(KARG_OUTFACES);
132 Face *outfaces = (Face*)map_mem_buffer(dbgbuf, MAP_RD);
133 for(int i=0; i<rinf.num_faces; i++) {
134 if(!(faces[i] == outfaces[i])) {
135 fprintf(stderr, "SKATA %d\n", i);
136 return false;
137 }
138 faces[i] = outfaces[i];
139 }
140 printf("equality test passed\n");
141 unmap_mem_buffer(dbgbuf);
144 CLMemBuffer *mbuf = prog->get_arg_buffer(KARG_FRAMEBUFFER);
145 void *fb = map_mem_buffer(mbuf, MAP_RD);
146 if(!fb) {
147 fprintf(stderr, "FAILED\n");
148 return false;
149 }
151 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rinf.xsz, rinf.ysz, GL_RGBA, GL_FLOAT, fb);
152 unmap_mem_buffer(mbuf);
153 return true;
154 }
156 void dbg_set_dbg(int dbg)
157 {
158 printf("setting dbg: %d\n", dbg);
160 CLMemBuffer *mbuf = prog->get_arg_buffer(KARG_RENDER_INFO);
161 RendInfo *rinf = (RendInfo*)map_mem_buffer(mbuf, MAP_WR);
162 rinf->dbg = dbg;
163 unmap_mem_buffer(mbuf);
164 }
166 void dbg_render_gl(Scene *scn)
167 {
168 float lpos[] = {-1, 1, 10, 0};
169 glPushAttrib(GL_ENABLE_BIT | GL_TRANSFORM_BIT);
171 glDisable(GL_TEXTURE_2D);
172 glEnable(GL_DEPTH_TEST);
173 glEnable(GL_LIGHTING);
174 glEnable(GL_LIGHT0);
175 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
176 glEnable(GL_COLOR_MATERIAL);
178 glMatrixMode(GL_PROJECTION);
179 glPushMatrix();
180 glLoadIdentity();
181 gluPerspective(45.0, (float)rinf.xsz / (float)rinf.ysz, 0.5, 1000.0);
183 Material *materials = scn->get_materials();
185 glBegin(GL_TRIANGLES);
186 int num_faces = scn->get_num_faces();
187 for(int i=0; i<num_faces; i++) {
188 Material *mat = materials ? materials + faces[i].matid : 0;
190 if(mat) {
191 glColor3f(mat->kd[0], mat->kd[1], mat->kd[2]);
192 } else {
193 glColor3f(1, 1, 1);
194 }
196 for(int j=0; j<3; j++) {
197 float *pos = faces[i].v[j].pos;
198 float *norm = faces[i].normal;
199 glNormal3fv(norm);
200 glVertex3fv(pos);
201 }
202 }
204 /*for(size_t i=0; i<scn->meshes.size(); i++) {
205 Material *mat = &scn->matlib[scn->meshes[i]->matid];
207 glColor3f(mat->kd[0], mat->kd[1], mat->kd[2]);
208 for(size_t j=0; j<scn->meshes[i]->faces.size(); j++) {
209 for(int k=0; k<3; k++) {
210 float *pos = scn->meshes[i]->faces[j].v[k].pos;
211 glVertex3f(pos[0], pos[1], pos[2]);
212 }
213 }
214 }*/
215 glEnd();
217 glPopMatrix();
218 glPopAttrib();
219 }
221 void set_xform(float *matrix, float *invtrans)
222 {
223 CLMemBuffer *mbuf_xform = prog->get_arg_buffer(KARG_XFORM);
224 CLMemBuffer *mbuf_invtrans = prog->get_arg_buffer(KARG_INVTRANS_XFORM);
225 assert(mbuf_xform && mbuf_invtrans);
227 float *mem = (float*)map_mem_buffer(mbuf_xform, MAP_WR);
228 memcpy(mem, matrix, 16 * sizeof *mem);
229 /*printf("-- xform:\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_xform);
236 mem = (float*)map_mem_buffer(mbuf_invtrans, MAP_WR);
237 memcpy(mem, invtrans, 16 * sizeof *mem);
238 /*printf("-- inverse-transpose:\n");
239 for(int i=0; i<16; i++) {
240 printf("%2.3f\t", mem[i]);
241 if(i % 4 == 3) putchar('\n');
242 }*/
243 unmap_mem_buffer(mbuf_invtrans);
244 }
246 static Ray get_primary_ray(int x, int y, int w, int h, float vfov_deg)
247 {
248 float vfov = M_PI * vfov_deg / 180.0;
249 float aspect = (float)w / (float)h;
251 float ysz = 2.0;
252 float xsz = aspect * ysz;
254 float px = ((float)x / (float)w) * xsz - xsz / 2.0;
255 float py = 1.0 - ((float)y / (float)h) * ysz;
256 float pz = 1.0 / tan(0.5 * vfov);
258 px *= 100.0;
259 py *= 100.0;
260 pz *= 100.0;
262 Ray ray = {{0, 0, 0, 1}, {px, py, -pz, 1}, 1.0};
263 return ray;
264 }
266 static Face *create_face_buffer(Mesh **meshes, int num_meshes)
267 {
268 int num_faces = 0;
269 for(int i=0; i<num_meshes; i++) {
270 num_faces += meshes[i]->faces.size();
271 }
272 printf("constructing face buffer with %d faces (out of %d meshes)\n", num_faces, num_meshes);
274 Face *faces = new Face[num_faces];
275 memset(faces, 0, num_faces * sizeof *faces);
276 Face *fptr = faces;
278 for(int i=0; i<num_meshes; i++) {
279 for(size_t j=0; j<meshes[i]->faces.size(); j++) {
280 *fptr++ = meshes[i]->faces[j];
281 }
282 }
283 return faces;
284 }