clray

view src/rt.cc @ 11:d9a1bab1c3f5

ported to windows
author John Tsiombikas
date Sat, 31 Jul 2010 22:23:57 +0100
parents a09622aaa043
children 85fd61f374d9
line source
1 #include <stdio.h>
2 #include <string.h>
3 #include <math.h>
4 #include <assert.h>
5 #include "ocl.h"
6 #include "mesh.h"
8 #ifdef __GNUC__
9 #define PACKED __attribute__((packed))
10 #else
11 #define PACKED
12 #endif
14 #ifdef _MSC_VER
15 #pragma push(pack, 1)
16 #endif
18 struct RendInfo {
19 int xsz, ysz;
20 int num_faces, num_lights;
21 int max_iter;
22 } PACKED;
24 struct Ray {
25 float origin[4], dir[4];
26 } PACKED;
28 struct Light {
29 float pos[4], color[4];
30 } PACKED;
32 #ifdef _MSC_VER
33 #pragma pop(pack)
34 #endif
36 static Ray get_primary_ray(int x, int y, int w, int h, float vfov_deg);
38 static Ray *prim_rays;
39 static CLProgram *prog;
40 static int global_size;
42 static Face faces[] = {
43 {/* face0 */
44 {
45 {{-1, 0, 0, 1}, {0, 0, -1, 1}, {0, 0}},
46 {{0, 1, 0, 1}, {0, 0, -1, 1}, {0, 0}},
47 {{1, 0, 0, 1}, {0, 0, -1, 1}, {0, 0}}
48 },
49 {0, 0, -1, 1}, 0
50 },
51 {/* face1 */
52 {
53 {{-5, 0, -3, 1}, {0, 0, -1, 1}, {0, 0}},
54 {{0, 0, 3, 1}, {0, 0, -1, 1}, {0, 0}},
55 {{5, 0, -3, 1}, {0, 0, -1, 1}, {0, 0}}
56 },
57 {0, 0, -1, 1}, 1
58 }
59 };
61 static Material matlib[] = {
62 {{1, 0, 0, 1}, {1, 1, 1, 1}, 0, 0, 60.0},
63 {{0.2, 0.8, 0.3, 1}, {0, 0, 0, 0}, 0, 0, 0}
64 };
66 static Light lightlist[] = {
67 {{-10, 10, -20, 1}, {1, 1, 1, 1}}
68 };
70 static float xform[16] = {
71 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1
72 };
74 static RendInfo rinf;
77 bool init_renderer(int xsz, int ysz, float *fb)
78 {
79 // render info
80 rinf.xsz = xsz;
81 rinf.ysz = ysz;
82 rinf.num_faces = sizeof faces / sizeof *faces;
83 rinf.num_lights = sizeof lightlist / sizeof *lightlist;
84 rinf.max_iter = 6;
86 /* calculate primary rays */
87 prim_rays = new Ray[xsz * ysz];
89 for(int i=0; i<ysz; i++) {
90 for(int j=0; j<xsz; j++) {
91 prim_rays[i * xsz + j] = get_primary_ray(j, i, xsz, ysz, 45.0);
92 }
93 }
95 /* setup opencl */
96 prog = new CLProgram("render");
97 if(!prog->load("rt.cl")) {
98 return false;
99 }
101 /* setup argument buffers */
102 prog->set_arg_buffer(0, ARG_WR, xsz * ysz * 4 * sizeof(float), fb);
103 prog->set_arg_buffer(1, ARG_RD, sizeof rinf, &rinf);
104 prog->set_arg_buffer(2, ARG_RD, sizeof faces, faces);
105 prog->set_arg_buffer(3, ARG_RD, sizeof matlib, matlib);
106 prog->set_arg_buffer(4, ARG_RD, sizeof lightlist, lightlist);
107 prog->set_arg_buffer(5, ARG_RD, xsz * ysz * sizeof *prim_rays, prim_rays);
108 prog->set_arg_buffer(6, ARG_RD, sizeof xform, &xform);
110 global_size = xsz * ysz;
111 return true;
112 }
114 void destroy_renderer()
115 {
116 delete [] prim_rays;
117 delete prog;
118 }
120 bool render()
121 {
122 if(!prog->run(1, global_size)) {
123 return false;
124 }
126 CLMemBuffer *mbuf = prog->get_arg_buffer(0);
127 map_mem_buffer(mbuf, MAP_RD);
128 unmap_mem_buffer(mbuf);
129 return true;
130 }
132 void set_xform(float *matrix)
133 {
134 CLMemBuffer *mbuf = prog->get_arg_buffer(6);
135 assert(mbuf);
137 assert(map_mem_buffer(mbuf, MAP_WR) == xform);
138 memcpy(xform, matrix, sizeof xform);
139 unmap_mem_buffer(mbuf);
140 }
142 static Ray get_primary_ray(int x, int y, int w, int h, float vfov_deg)
143 {
144 float vfov = M_PI * vfov_deg / 180.0;
145 float aspect = (float)w / (float)h;
147 float ysz = 2.0;
148 float xsz = aspect * ysz;
150 float px = ((float)x / (float)w) * xsz - xsz / 2.0;
151 float py = 1.0 - ((float)y / (float)h) * ysz;
152 float pz = 1.0 / tan(0.5 * vfov);
154 px *= 100.0;
155 py *= 100.0;
156 pz *= 100.0;
158 Ray ray = {{0, 0, 0, 1}, {px, py, pz, 1}};
159 return ray;
160 }