rev |
line source |
nuclear@0
|
1 #include <stdio.h>
|
nuclear@8
|
2 #include <string.h>
|
nuclear@2
|
3 #include <math.h>
|
nuclear@0
|
4 #include <assert.h>
|
John@14
|
5 #include "ogl.h"
|
nuclear@0
|
6 #include "ocl.h"
|
nuclear@22
|
7 #include "scene.h"
|
nuclear@32
|
8 #include "timer.h"
|
nuclear@0
|
9
|
nuclear@12
|
10 // kernel arguments
|
nuclear@12
|
11 enum {
|
nuclear@12
|
12 KARG_FRAMEBUFFER,
|
nuclear@12
|
13 KARG_RENDER_INFO,
|
nuclear@12
|
14 KARG_FACES,
|
nuclear@12
|
15 KARG_MATLIB,
|
nuclear@12
|
16 KARG_LIGHTS,
|
nuclear@12
|
17 KARG_PRIM_RAYS,
|
nuclear@12
|
18 KARG_XFORM,
|
John@14
|
19 KARG_INVTRANS_XFORM,
|
nuclear@28
|
20 KARG_KDTREE,
|
John@14
|
21
|
John@14
|
22 NUM_KERNEL_ARGS
|
nuclear@12
|
23 };
|
John@11
|
24
|
nuclear@2
|
25 struct RendInfo {
|
nuclear@22
|
26 float ambient[4];
|
nuclear@2
|
27 int xsz, ysz;
|
nuclear@9
|
28 int num_faces, num_lights;
|
nuclear@2
|
29 int max_iter;
|
nuclear@28
|
30 int kd_depth;
|
nuclear@12
|
31 };
|
nuclear@2
|
32
|
nuclear@1
|
33 struct Ray {
|
nuclear@8
|
34 float origin[4], dir[4];
|
nuclear@12
|
35 };
|
nuclear@1
|
36
|
nuclear@3
|
37 struct Light {
|
nuclear@8
|
38 float pos[4], color[4];
|
nuclear@12
|
39 };
|
nuclear@1
|
40
|
nuclear@3
|
41 static Ray get_primary_ray(int x, int y, int w, int h, float vfov_deg);
|
nuclear@3
|
42
|
nuclear@13
|
43 static Face *faces;
|
nuclear@3
|
44 static Ray *prim_rays;
|
nuclear@3
|
45 static CLProgram *prog;
|
nuclear@3
|
46 static int global_size;
|
nuclear@3
|
47
|
nuclear@4
|
48 static Light lightlist[] = {
|
nuclear@22
|
49 {{-8, 15, 18, 0}, {1, 1, 1, 1}}
|
nuclear@4
|
50 };
|
nuclear@4
|
51
|
nuclear@7
|
52
|
nuclear@4
|
53 static RendInfo rinf;
|
nuclear@4
|
54
|
nuclear@4
|
55
|
nuclear@39
|
56 bool init_renderer(int xsz, int ysz, Scene *scn, unsigned int tex)
|
nuclear@0
|
57 {
|
nuclear@4
|
58 // render info
|
nuclear@22
|
59 rinf.ambient[0] = rinf.ambient[1] = rinf.ambient[2] = 0.0;
|
nuclear@16
|
60 rinf.ambient[3] = 0.0;
|
nuclear@16
|
61
|
nuclear@4
|
62 rinf.xsz = xsz;
|
nuclear@4
|
63 rinf.ysz = ysz;
|
nuclear@13
|
64 rinf.num_faces = scn->get_num_faces();
|
nuclear@4
|
65 rinf.num_lights = sizeof lightlist / sizeof *lightlist;
|
nuclear@4
|
66 rinf.max_iter = 6;
|
nuclear@28
|
67 rinf.kd_depth = kdtree_depth(scn->kdtree);
|
nuclear@4
|
68
|
nuclear@3
|
69 /* calculate primary rays */
|
nuclear@3
|
70 prim_rays = new Ray[xsz * ysz];
|
nuclear@2
|
71
|
nuclear@2
|
72 for(int i=0; i<ysz; i++) {
|
nuclear@2
|
73 for(int j=0; j<xsz; j++) {
|
nuclear@2
|
74 prim_rays[i * xsz + j] = get_primary_ray(j, i, xsz, ysz, 45.0);
|
nuclear@2
|
75 }
|
nuclear@0
|
76 }
|
nuclear@0
|
77
|
nuclear@2
|
78 /* setup opencl */
|
nuclear@3
|
79 prog = new CLProgram("render");
|
nuclear@3
|
80 if(!prog->load("rt.cl")) {
|
nuclear@8
|
81 return false;
|
nuclear@0
|
82 }
|
nuclear@0
|
83
|
nuclear@24
|
84 if(!(faces = (Face*)scn->get_face_buffer())) {
|
nuclear@13
|
85 fprintf(stderr, "failed to create face buffer\n");
|
nuclear@13
|
86 return false;
|
nuclear@13
|
87 }
|
nuclear@13
|
88
|
nuclear@28
|
89 const KDNodeGPU *kdbuf = scn->get_kdtree_buffer();
|
nuclear@28
|
90 if(!kdbuf) {
|
nuclear@28
|
91 fprintf(stderr, "failed to create kdtree buffer\n");
|
nuclear@28
|
92 return false;
|
nuclear@28
|
93 }
|
nuclear@32
|
94 // XXX now we can actually destroy the original kdtree and keep only the GPU version
|
nuclear@28
|
95
|
nuclear@3
|
96 /* setup argument buffers */
|
nuclear@41
|
97 #ifdef CLGL_INTEROP
|
nuclear@39
|
98 prog->set_arg_texture(KARG_FRAMEBUFFER, ARG_WR, tex);
|
nuclear@41
|
99 #else
|
nuclear@41
|
100 prog->set_arg_image(KARG_FRAMEBUFFER, ARG_WR, xsz, ysz);
|
nuclear@41
|
101 #endif
|
nuclear@12
|
102 prog->set_arg_buffer(KARG_RENDER_INFO, ARG_RD, sizeof rinf, &rinf);
|
John@14
|
103 prog->set_arg_buffer(KARG_FACES, ARG_RD, rinf.num_faces * sizeof(Face), faces);
|
John@14
|
104 prog->set_arg_buffer(KARG_MATLIB, ARG_RD, scn->get_num_materials() * sizeof(Material), scn->get_materials());
|
nuclear@12
|
105 prog->set_arg_buffer(KARG_LIGHTS, ARG_RD, sizeof lightlist, lightlist);
|
nuclear@12
|
106 prog->set_arg_buffer(KARG_PRIM_RAYS, ARG_RD, xsz * ysz * sizeof *prim_rays, prim_rays);
|
nuclear@12
|
107 prog->set_arg_buffer(KARG_XFORM, ARG_RD, 16 * sizeof(float));
|
nuclear@12
|
108 prog->set_arg_buffer(KARG_INVTRANS_XFORM, ARG_RD, 16 * sizeof(float));
|
nuclear@35
|
109 prog->set_arg_buffer(KARG_KDTREE, ARG_RD, scn->get_num_kdnodes() * sizeof *kdbuf, kdbuf);
|
nuclear@12
|
110
|
John@14
|
111 if(prog->get_num_args() < NUM_KERNEL_ARGS) {
|
John@14
|
112 return false;
|
John@14
|
113 }
|
John@14
|
114
|
nuclear@16
|
115 if(!prog->build()) {
|
nuclear@16
|
116 return false;
|
nuclear@16
|
117 }
|
nuclear@16
|
118
|
nuclear@12
|
119 delete [] prim_rays;
|
nuclear@2
|
120
|
nuclear@3
|
121 global_size = xsz * ysz;
|
nuclear@3
|
122 return true;
|
nuclear@3
|
123 }
|
nuclear@3
|
124
|
nuclear@3
|
125 void destroy_renderer()
|
nuclear@3
|
126 {
|
nuclear@3
|
127 delete prog;
|
nuclear@3
|
128 }
|
nuclear@3
|
129
|
nuclear@3
|
130 bool render()
|
nuclear@3
|
131 {
|
nuclear@39
|
132 // XXX do we need to call glFinish ?
|
nuclear@39
|
133
|
nuclear@32
|
134 long tm0 = get_msec();
|
nuclear@32
|
135
|
nuclear@40
|
136 #ifdef CLGL_INTEROP
|
nuclear@39
|
137 cl_event ev;
|
nuclear@39
|
138 CLMemBuffer *texbuf = prog->get_arg_buffer(KARG_FRAMEBUFFER);
|
nuclear@39
|
139
|
nuclear@39
|
140 if(!acquire_gl_object(texbuf, &ev)) {
|
nuclear@39
|
141 return false;
|
nuclear@39
|
142 }
|
nuclear@39
|
143
|
nuclear@39
|
144 // make sure that we will wait for the acquire to finish before running
|
nuclear@39
|
145 prog->set_wait_event(ev);
|
nuclear@40
|
146 #endif
|
nuclear@39
|
147
|
nuclear@3
|
148 if(!prog->run(1, global_size)) {
|
nuclear@3
|
149 return false;
|
nuclear@0
|
150 }
|
John@15
|
151
|
nuclear@40
|
152 #ifdef CLGL_INTEROP
|
nuclear@39
|
153 if(!release_gl_object(texbuf, &ev)) {
|
nuclear@39
|
154 return false;
|
nuclear@39
|
155 }
|
nuclear@39
|
156 clWaitForEvents(1, &ev);
|
nuclear@40
|
157 #endif
|
nuclear@39
|
158
|
nuclear@40
|
159 #ifndef CLGL_INTEROP
|
nuclear@40
|
160 /* if we don't compile in CL/GL interoperability support, we need
|
nuclear@40
|
161 * to copy the output buffer to the OpenGL texture used to displaying
|
nuclear@40
|
162 * the image.
|
nuclear@40
|
163 */
|
nuclear@13
|
164 CLMemBuffer *mbuf = prog->get_arg_buffer(KARG_FRAMEBUFFER);
|
nuclear@12
|
165 void *fb = map_mem_buffer(mbuf, MAP_RD);
|
nuclear@13
|
166 if(!fb) {
|
nuclear@13
|
167 fprintf(stderr, "FAILED\n");
|
nuclear@13
|
168 return false;
|
nuclear@13
|
169 }
|
nuclear@13
|
170
|
nuclear@12
|
171 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rinf.xsz, rinf.ysz, GL_RGBA, GL_FLOAT, fb);
|
nuclear@2
|
172 unmap_mem_buffer(mbuf);
|
nuclear@40
|
173 #endif
|
nuclear@32
|
174
|
nuclear@40
|
175 printf("rendered in %ld msec\n", get_msec() - tm0);
|
nuclear@3
|
176 return true;
|
nuclear@0
|
177 }
|
nuclear@2
|
178
|
nuclear@27
|
179 #define MIN(a, b) ((a) < (b) ? (a) : (b))
|
nuclear@21
|
180 static void dbg_set_gl_material(Material *mat)
|
nuclear@21
|
181 {
|
nuclear@21
|
182 static Material def_mat = {{0.7, 0.7, 0.7, 1}, {0, 0, 0, 0}, 0, 0, 0};
|
nuclear@21
|
183
|
nuclear@21
|
184 if(!mat) mat = &def_mat;
|
nuclear@21
|
185
|
nuclear@21
|
186 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, mat->kd);
|
nuclear@21
|
187 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat->ks);
|
nuclear@27
|
188 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, MIN(mat->spow, 128.0f));
|
nuclear@21
|
189 }
|
nuclear@21
|
190
|
nuclear@27
|
191 void dbg_render_gl(Scene *scn, bool show_tree, bool show_obj)
|
nuclear@8
|
192 {
|
nuclear@22
|
193 glPushAttrib(GL_ENABLE_BIT | GL_TRANSFORM_BIT | GL_LIGHTING_BIT);
|
nuclear@8
|
194
|
nuclear@21
|
195 for(int i=0; i<rinf.num_lights; i++) {
|
nuclear@21
|
196 float lpos[4];
|
nuclear@21
|
197
|
nuclear@21
|
198 memcpy(lpos, lightlist[i].pos, sizeof lpos);
|
nuclear@21
|
199 lpos[3] = 1.0;
|
nuclear@21
|
200
|
nuclear@21
|
201 glLightfv(GL_LIGHT0 + i, GL_POSITION, lpos);
|
nuclear@21
|
202 glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, lightlist[i].color);
|
nuclear@22
|
203 glEnable(GL_LIGHT0 + i);
|
nuclear@21
|
204 }
|
nuclear@21
|
205
|
nuclear@12
|
206 glDisable(GL_TEXTURE_2D);
|
nuclear@12
|
207 glEnable(GL_DEPTH_TEST);
|
John@15
|
208 glEnable(GL_LIGHTING);
|
nuclear@12
|
209
|
nuclear@12
|
210 glMatrixMode(GL_PROJECTION);
|
nuclear@12
|
211 glPushMatrix();
|
nuclear@12
|
212 glLoadIdentity();
|
nuclear@12
|
213 gluPerspective(45.0, (float)rinf.xsz / (float)rinf.ysz, 0.5, 1000.0);
|
nuclear@12
|
214
|
nuclear@27
|
215 if(show_obj) {
|
nuclear@27
|
216 Material *materials = scn->get_materials();
|
John@14
|
217
|
nuclear@27
|
218 int num_faces = scn->get_num_faces();
|
nuclear@27
|
219 int cur_mat = -1;
|
nuclear@21
|
220
|
nuclear@27
|
221 for(int i=0; i<num_faces; i++) {
|
nuclear@27
|
222 if(faces[i].matid != cur_mat) {
|
nuclear@27
|
223 if(cur_mat != -1) {
|
nuclear@27
|
224 glEnd();
|
nuclear@27
|
225 }
|
nuclear@27
|
226 dbg_set_gl_material(materials ? materials + faces[i].matid : 0);
|
nuclear@27
|
227 cur_mat = faces[i].matid;
|
nuclear@27
|
228 glBegin(GL_TRIANGLES);
|
nuclear@21
|
229 }
|
nuclear@27
|
230
|
nuclear@27
|
231 for(int j=0; j<3; j++) {
|
nuclear@27
|
232 glNormal3fv(faces[i].v[j].normal);
|
nuclear@27
|
233 glVertex3fv(faces[i].v[j].pos);
|
nuclear@27
|
234 }
|
John@14
|
235 }
|
nuclear@27
|
236 glEnd();
|
nuclear@27
|
237 }
|
nuclear@12
|
238
|
nuclear@27
|
239 if(show_tree) {
|
nuclear@27
|
240 scn->draw_kdtree();
|
nuclear@12
|
241 }
|
nuclear@12
|
242
|
nuclear@12
|
243 glPopMatrix();
|
nuclear@12
|
244 glPopAttrib();
|
nuclear@22
|
245
|
nuclear@22
|
246 assert(glGetError() == GL_NO_ERROR);
|
nuclear@12
|
247 }
|
nuclear@12
|
248
|
nuclear@12
|
249 void set_xform(float *matrix, float *invtrans)
|
nuclear@12
|
250 {
|
nuclear@12
|
251 CLMemBuffer *mbuf_xform = prog->get_arg_buffer(KARG_XFORM);
|
nuclear@12
|
252 CLMemBuffer *mbuf_invtrans = prog->get_arg_buffer(KARG_INVTRANS_XFORM);
|
nuclear@12
|
253 assert(mbuf_xform && mbuf_invtrans);
|
nuclear@12
|
254
|
nuclear@12
|
255 float *mem = (float*)map_mem_buffer(mbuf_xform, MAP_WR);
|
nuclear@12
|
256 memcpy(mem, matrix, 16 * sizeof *mem);
|
nuclear@12
|
257 unmap_mem_buffer(mbuf_xform);
|
nuclear@12
|
258
|
nuclear@12
|
259 mem = (float*)map_mem_buffer(mbuf_invtrans, MAP_WR);
|
nuclear@12
|
260 memcpy(mem, invtrans, 16 * sizeof *mem);
|
nuclear@12
|
261 unmap_mem_buffer(mbuf_invtrans);
|
nuclear@8
|
262 }
|
nuclear@8
|
263
|
nuclear@3
|
264 static Ray get_primary_ray(int x, int y, int w, int h, float vfov_deg)
|
nuclear@2
|
265 {
|
nuclear@2
|
266 float vfov = M_PI * vfov_deg / 180.0;
|
nuclear@2
|
267 float aspect = (float)w / (float)h;
|
nuclear@2
|
268
|
nuclear@2
|
269 float ysz = 2.0;
|
nuclear@2
|
270 float xsz = aspect * ysz;
|
nuclear@2
|
271
|
nuclear@2
|
272 float px = ((float)x / (float)w) * xsz - xsz / 2.0;
|
nuclear@2
|
273 float py = 1.0 - ((float)y / (float)h) * ysz;
|
nuclear@2
|
274 float pz = 1.0 / tan(0.5 * vfov);
|
nuclear@2
|
275
|
nuclear@4
|
276 px *= 100.0;
|
nuclear@4
|
277 py *= 100.0;
|
nuclear@4
|
278 pz *= 100.0;
|
nuclear@2
|
279
|
nuclear@18
|
280 Ray ray = {{0, 0, 0, 1}, {px, py, -pz, 1}};
|
nuclear@2
|
281 return ray;
|
nuclear@2
|
282 }
|