clray
view src/rt.cc @ 21:bd6c2b25f6e7
fixed, now we need to start with optimizations
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Tue, 10 Aug 2010 07:24:18 +0100 |
parents | 63a6b46f58a0 |
children | 6c44e4b1726d |
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 float ambient[4];
28 };
30 struct Ray {
31 float origin[4], dir[4];
32 };
34 struct Light {
35 float pos[4], color[4];
36 };
38 static Ray get_primary_ray(int x, int y, int w, int h, float vfov_deg);
39 static Face *create_face_buffer(Mesh **meshes, int num_meshes);
41 static Face *faces;
42 static Ray *prim_rays;
43 static CLProgram *prog;
44 static int global_size;
46 static Light lightlist[] = {
47 {{-8, 15, -18, 0}, {1, 1, 1, 1}}
48 };
51 static RendInfo rinf;
54 bool init_renderer(int xsz, int ysz, Scene *scn)
55 {
56 // render info
57 rinf.ambient[0] = rinf.ambient[1] = rinf.ambient[2] = 0.075;
58 rinf.ambient[3] = 0.0;
60 rinf.xsz = xsz;
61 rinf.ysz = ysz;
62 rinf.num_faces = scn->get_num_faces();
63 rinf.num_lights = sizeof lightlist / sizeof *lightlist;
64 rinf.max_iter = 6;
66 /* calculate primary rays */
67 prim_rays = new Ray[xsz * ysz];
69 for(int i=0; i<ysz; i++) {
70 for(int j=0; j<xsz; j++) {
71 prim_rays[i * xsz + j] = get_primary_ray(j, i, xsz, ysz, 45.0);
72 }
73 }
75 /* setup opencl */
76 prog = new CLProgram("render");
77 if(!prog->load("rt.cl")) {
78 return false;
79 }
81 /*Face **/faces = create_face_buffer(&scn->meshes[0], scn->meshes.size());
82 if(!faces) {
83 fprintf(stderr, "failed to create face buffer\n");
84 return false;
85 }
87 /* setup argument buffers */
88 prog->set_arg_buffer(KARG_FRAMEBUFFER, ARG_WR, xsz * ysz * 4 * sizeof(float));
89 prog->set_arg_buffer(KARG_RENDER_INFO, ARG_RD, sizeof rinf, &rinf);
90 prog->set_arg_buffer(KARG_FACES, ARG_RD, rinf.num_faces * sizeof(Face), faces);
91 prog->set_arg_buffer(KARG_MATLIB, ARG_RD, scn->get_num_materials() * sizeof(Material), scn->get_materials());
92 prog->set_arg_buffer(KARG_LIGHTS, ARG_RD, sizeof lightlist, lightlist);
93 prog->set_arg_buffer(KARG_PRIM_RAYS, ARG_RD, xsz * ysz * sizeof *prim_rays, prim_rays);
94 prog->set_arg_buffer(KARG_XFORM, ARG_RD, 16 * sizeof(float));
95 prog->set_arg_buffer(KARG_INVTRANS_XFORM, ARG_RD, 16 * sizeof(float));
97 if(prog->get_num_args() < NUM_KERNEL_ARGS) {
98 return false;
99 }
101 if(!prog->build()) {
102 return false;
103 }
105 delete [] prim_rays;
107 global_size = xsz * ysz;
108 return true;
109 }
111 void destroy_renderer()
112 {
113 delete prog;
114 }
116 bool render()
117 {
118 printf("Running kernel... ");
119 fflush(stdout);
120 if(!prog->run(1, global_size)) {
121 return false;
122 }
123 printf("done\n");
126 CLMemBuffer *mbuf = prog->get_arg_buffer(KARG_FRAMEBUFFER);
127 void *fb = map_mem_buffer(mbuf, MAP_RD);
128 if(!fb) {
129 fprintf(stderr, "FAILED\n");
130 return false;
131 }
133 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rinf.xsz, rinf.ysz, GL_RGBA, GL_FLOAT, fb);
134 unmap_mem_buffer(mbuf);
135 return true;
136 }
138 static void dbg_set_gl_material(Material *mat)
139 {
140 static Material def_mat = {{0.7, 0.7, 0.7, 1}, {0, 0, 0, 0}, 0, 0, 0};
142 if(!mat) mat = &def_mat;
144 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, mat->kd);
145 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat->ks);
146 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, mat->spow);
147 }
149 void dbg_render_gl(Scene *scn)
150 {
151 glPushAttrib(GL_ENABLE_BIT | GL_TRANSFORM_BIT);
153 for(int i=0; i<rinf.num_lights; i++) {
154 float lpos[4];
156 memcpy(lpos, lightlist[i].pos, sizeof lpos);
157 lpos[3] = 1.0;
159 glLightfv(GL_LIGHT0 + i, GL_POSITION, lpos);
160 glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, lightlist[i].color);
161 }
163 glDisable(GL_TEXTURE_2D);
164 glEnable(GL_DEPTH_TEST);
165 glEnable(GL_LIGHTING);
166 glEnable(GL_LIGHT0);
168 glMatrixMode(GL_PROJECTION);
169 glPushMatrix();
170 glLoadIdentity();
171 gluPerspective(45.0, (float)rinf.xsz / (float)rinf.ysz, 0.5, 1000.0);
173 Material *materials = scn->get_materials();
175 int num_faces = scn->get_num_faces();
176 int cur_mat = -1;
178 for(int i=0; i<num_faces; i++) {
179 if(faces[i].matid != cur_mat) {
180 if(cur_mat != -1) {
181 glEnd();
182 }
183 dbg_set_gl_material(materials ? materials + faces[i].matid : 0);
184 cur_mat = faces[i].matid;
185 glBegin(GL_TRIANGLES);
186 }
188 for(int j=0; j<3; j++) {
189 glNormal3fv(faces[i].v[j].normal);
190 glVertex3fv(faces[i].v[j].pos);
191 }
192 }
193 glEnd();
195 glPopMatrix();
196 glPopAttrib();
197 }
199 void set_xform(float *matrix, float *invtrans)
200 {
201 CLMemBuffer *mbuf_xform = prog->get_arg_buffer(KARG_XFORM);
202 CLMemBuffer *mbuf_invtrans = prog->get_arg_buffer(KARG_INVTRANS_XFORM);
203 assert(mbuf_xform && mbuf_invtrans);
205 float *mem = (float*)map_mem_buffer(mbuf_xform, MAP_WR);
206 memcpy(mem, matrix, 16 * sizeof *mem);
207 /*printf("-- xform:\n");
208 for(int i=0; i<16; i++) {
209 printf("%2.3f\t", mem[i]);
210 if(i % 4 == 3) putchar('\n');
211 }*/
212 unmap_mem_buffer(mbuf_xform);
214 mem = (float*)map_mem_buffer(mbuf_invtrans, MAP_WR);
215 memcpy(mem, invtrans, 16 * sizeof *mem);
216 /*printf("-- inverse-transpose:\n");
217 for(int i=0; i<16; i++) {
218 printf("%2.3f\t", mem[i]);
219 if(i % 4 == 3) putchar('\n');
220 }*/
221 unmap_mem_buffer(mbuf_invtrans);
222 }
224 static Ray get_primary_ray(int x, int y, int w, int h, float vfov_deg)
225 {
226 float vfov = M_PI * vfov_deg / 180.0;
227 float aspect = (float)w / (float)h;
229 float ysz = 2.0;
230 float xsz = aspect * ysz;
232 float px = ((float)x / (float)w) * xsz - xsz / 2.0;
233 float py = 1.0 - ((float)y / (float)h) * ysz;
234 float pz = 1.0 / tan(0.5 * vfov);
236 px *= 100.0;
237 py *= 100.0;
238 pz *= 100.0;
240 Ray ray = {{0, 0, 0, 1}, {px, py, -pz, 1}};
241 return ray;
242 }
244 static Face *create_face_buffer(Mesh **meshes, int num_meshes)
245 {
246 int num_faces = 0;
247 for(int i=0; i<num_meshes; i++) {
248 num_faces += meshes[i]->faces.size();
249 }
250 printf("constructing face buffer with %d faces (out of %d meshes)\n", num_faces, num_meshes);
252 Face *faces = new Face[num_faces];
253 memset(faces, 0, num_faces * sizeof *faces);
254 Face *fptr = faces;
256 for(int i=0; i<num_meshes; i++) {
257 for(size_t j=0; j<meshes[i]->faces.size(); j++) {
258 *fptr++ = meshes[i]->faces[j];
259 }
260 }
261 return faces;
262 }