clray

view src/rt.cc @ 14:29f9330cfa4b

trying to debug the bloody thing
author John Tsiombikas
date Sat, 07 Aug 2010 03:36:36 +0100
parents 407935b73af3
children 754faf15ba36
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,
20 NUM_KERNEL_ARGS
21 };
23 struct RendInfo {
24 int xsz, ysz;
25 int num_faces, num_lights;
26 int max_iter;
27 };
29 struct Ray {
30 float origin[4], dir[4];
31 };
33 struct Light {
34 float pos[4], color[4];
35 };
37 static Ray get_primary_ray(int x, int y, int w, int h, float vfov_deg);
38 static Face *create_face_buffer(Mesh **meshes, int num_meshes);
40 static Face *faces;
41 static Ray *prim_rays;
42 static CLProgram *prog;
43 static int global_size;
45 static Light lightlist[] = {
46 {{-10, 10, -20, 0}, {1, 1, 1, 1}}
47 };
50 static RendInfo rinf;
53 bool init_renderer(int xsz, int ysz, Scene *scn)
54 {
55 // render info
56 rinf.xsz = xsz;
57 rinf.ysz = ysz;
58 rinf.num_faces = scn->get_num_faces();
59 rinf.num_lights = sizeof lightlist / sizeof *lightlist;
60 rinf.max_iter = 6;
62 /* calculate primary rays */
63 prim_rays = new Ray[xsz * ysz];
65 for(int i=0; i<ysz; i++) {
66 for(int j=0; j<xsz; j++) {
67 prim_rays[i * xsz + j] = get_primary_ray(j, i, xsz, ysz, 45.0);
68 }
69 }
71 /* setup opencl */
72 prog = new CLProgram("render");
73 if(!prog->load("rt.cl")) {
74 return false;
75 }
77 /*Face **/faces = create_face_buffer(&scn->meshes[0], scn->meshes.size());
78 if(!faces) {
79 fprintf(stderr, "failed to create face buffer\n");
80 return false;
81 }
83 /* setup argument buffers */
84 prog->set_arg_buffer(KARG_FRAMEBUFFER, ARG_WR, xsz * ysz * 4 * sizeof(float));
85 prog->set_arg_buffer(KARG_RENDER_INFO, ARG_RD, sizeof rinf, &rinf);
86 prog->set_arg_buffer(KARG_FACES, ARG_RD, rinf.num_faces * sizeof(Face), faces);
87 prog->set_arg_buffer(KARG_MATLIB, ARG_RD, scn->get_num_materials() * sizeof(Material), scn->get_materials());
88 prog->set_arg_buffer(KARG_LIGHTS, ARG_RD, sizeof lightlist, lightlist);
89 prog->set_arg_buffer(KARG_PRIM_RAYS, ARG_RD, xsz * ysz * sizeof *prim_rays, prim_rays);
90 prog->set_arg_buffer(KARG_XFORM, ARG_RD, 16 * sizeof(float));
91 prog->set_arg_buffer(KARG_INVTRANS_XFORM, ARG_RD, 16 * sizeof(float));
93 if(prog->get_num_args() < NUM_KERNEL_ARGS) {
94 return false;
95 }
97 delete [] prim_rays;
99 global_size = xsz * ysz;
100 return true;
101 }
103 void destroy_renderer()
104 {
105 delete prog;
106 }
108 bool render()
109 {
110 printf("Running kernel...");
111 fflush(stdout);
112 if(!prog->run(1, global_size)) {
113 return false;
114 }
115 printf("done\n");
117 CLMemBuffer *mbuf = prog->get_arg_buffer(KARG_FRAMEBUFFER);
118 void *fb = map_mem_buffer(mbuf, MAP_RD);
119 if(!fb) {
120 fprintf(stderr, "FAILED\n");
121 return false;
122 }
124 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rinf.xsz, rinf.ysz, GL_RGBA, GL_FLOAT, fb);
125 unmap_mem_buffer(mbuf);
126 return true;
127 }
129 void dbg_render_gl(Scene *scn)
130 {
131 glPushAttrib(GL_ENABLE_BIT | GL_TRANSFORM_BIT);
133 glDisable(GL_TEXTURE_2D);
134 glEnable(GL_DEPTH_TEST);
136 glMatrixMode(GL_PROJECTION);
137 glPushMatrix();
138 glLoadIdentity();
139 gluPerspective(45.0, (float)rinf.xsz / (float)rinf.ysz, 0.5, 1000.0);
141 Material *materials = scn->get_materials();
143 glBegin(GL_TRIANGLES);
144 int num_faces = scn->get_num_faces();
145 for(int i=0; i<num_faces; i++) {
146 Material *mat = materials ? materials + faces[i].matid : 0;
148 if(mat) {
149 glColor3f(mat->kd[0], mat->kd[1], mat->kd[2]);
150 } else {
151 glColor3f(1, 1, 1);
152 }
154 for(int j=0; j<3; j++) {
155 float *pos = faces[i].v[j].pos;
156 glVertex3f(pos[0], pos[1], pos[2]);
157 }
158 }
160 /*for(size_t i=0; i<scn->meshes.size(); i++) {
161 Material *mat = &scn->matlib[scn->meshes[i]->matid];
163 glColor3f(mat->kd[0], mat->kd[1], mat->kd[2]);
164 for(size_t j=0; j<scn->meshes[i]->faces.size(); j++) {
165 for(int k=0; k<3; k++) {
166 float *pos = scn->meshes[i]->faces[j].v[k].pos;
167 glVertex3f(pos[0], pos[1], pos[2]);
168 }
169 }
170 }*/
171 glEnd();
173 glPopMatrix();
174 glPopAttrib();
175 }
177 void set_xform(float *matrix, float *invtrans)
178 {
179 CLMemBuffer *mbuf_xform = prog->get_arg_buffer(KARG_XFORM);
180 CLMemBuffer *mbuf_invtrans = prog->get_arg_buffer(KARG_INVTRANS_XFORM);
181 assert(mbuf_xform && mbuf_invtrans);
183 float *mem = (float*)map_mem_buffer(mbuf_xform, MAP_WR);
184 memcpy(mem, matrix, 16 * sizeof *mem);
185 /*printf("-- xform:\n");
186 for(int i=0; i<16; i++) {
187 printf("%2.3f\t", mem[i]);
188 if(i % 4 == 3) putchar('\n');
189 }*/
190 unmap_mem_buffer(mbuf_xform);
192 mem = (float*)map_mem_buffer(mbuf_invtrans, MAP_WR);
193 memcpy(mem, invtrans, 16 * sizeof *mem);
194 /*printf("-- inverse-transpose:\n");
195 for(int i=0; i<16; i++) {
196 printf("%2.3f\t", mem[i]);
197 if(i % 4 == 3) putchar('\n');
198 }*/
199 unmap_mem_buffer(mbuf_invtrans);
200 }
202 static Ray get_primary_ray(int x, int y, int w, int h, float vfov_deg)
203 {
204 float vfov = M_PI * vfov_deg / 180.0;
205 float aspect = (float)w / (float)h;
207 float ysz = 2.0;
208 float xsz = aspect * ysz;
210 float px = ((float)x / (float)w) * xsz - xsz / 2.0;
211 float py = 1.0 - ((float)y / (float)h) * ysz;
212 float pz = 1.0 / tan(0.5 * vfov);
214 px *= 100.0;
215 py *= 100.0;
216 pz *= 100.0;
218 Ray ray = {{0, 0, 0, 1}, {px, py, -pz, 1}};
219 return ray;
220 }
222 static Face *create_face_buffer(Mesh **meshes, int num_meshes)
223 {
224 int num_faces = 0;
225 for(int i=0; i<num_meshes; i++) {
226 num_faces += meshes[i]->faces.size();
227 }
228 printf("constructing face buffer with %d faces (out of %d meshes)\n", num_faces, num_meshes);
230 Face *faces = new Face[num_faces];
231 memset(faces, 0, num_faces * sizeof *faces);
232 Face *fptr = faces;
234 for(int i=0; i<num_meshes; i++) {
235 for(size_t j=0; j<meshes[i]->faces.size(); j++) {
236 *fptr++ = meshes[i]->faces[j];
237 }
238 }
239 return faces;
240 }