clray

view src/rt.cc @ 7:575383f3a239

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 23 Jul 2010 01:22:03 +0100
parents 9f0ddb701882
children deaf85acf6af
line source
1 #include <stdio.h>
2 #include <math.h>
3 #include <assert.h>
4 #include "ocl.h"
6 struct RendInfo {
7 int xsz, ysz;
8 int num_sph, num_lights;
9 int max_iter;
10 } __attribute__((packed));
12 struct Sphere {
13 cl_float4 pos;
14 cl_float4 kd, ks;
15 cl_float radius;
16 cl_float spow;
17 cl_float kr, kt;
18 } __attribute__((packed));
20 struct Ray {
21 cl_float4 origin, dir;
22 } __attribute__((packed));
24 struct Light {
25 cl_float4 pos, color;
26 } __attribute__((packed));
28 struct Matrix4x4 {
29 cl_float m[16];
30 };
32 static Ray get_primary_ray(int x, int y, int w, int h, float vfov_deg);
34 static Ray *prim_rays;
35 static CLProgram *prog;
36 static int global_size;
38 static Sphere sphlist[] = {
39 {{0, 0, 8, 1}, {0.7, 0.2, 0.15, 1}, {1, 1, 1, 1}, 1.0, 60, 0, 0},
40 {{-0.2, 0.4, 5, 1}, {0.2, 0.9, 0.3, 1}, {1, 1, 1, 1}, 0.25, 40, 0, 0}
41 };
43 static Light lightlist[] = {
44 {{-10, 10, -20, 1}, {1, 1, 1, 1}}
45 };
47 static Matrix4x4 xform = {
48 {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}
49 };
51 static RendInfo rinf;
54 bool init_renderer(int xsz, int ysz, float *fb)
55 {
56 // render info
57 rinf.xsz = xsz;
58 rinf.ysz = ysz;
59 rinf.num_sph = sizeof sphlist / sizeof *sphlist;
60 rinf.num_lights = sizeof lightlist / sizeof *lightlist;
61 rinf.max_iter = 6;
63 /* calculate primary rays */
64 prim_rays = new Ray[xsz * ysz];
66 for(int i=0; i<ysz; i++) {
67 for(int j=0; j<xsz; j++) {
68 prim_rays[i * xsz + j] = get_primary_ray(j, i, xsz, ysz, 45.0);
69 }
70 }
72 /* setup opencl */
73 prog = new CLProgram("render");
74 if(!prog->load("rt.cl")) {
75 return 1;
76 }
78 /* setup argument buffers */
79 prog->set_arg_buffer(0, ARG_WR, xsz * ysz * 4 * sizeof(float), fb);
80 prog->set_arg_buffer(1, ARG_RD, sizeof rinf, &rinf);
81 prog->set_arg_buffer(2, ARG_RD, sizeof sphlist, sphlist);
82 prog->set_arg_buffer(3, ARG_RD, sizeof lightlist, lightlist);
83 prog->set_arg_buffer(4, ARG_RD, xsz * ysz * sizeof *prim_rays, prim_rays);
84 prog->set_arg_buffer(5, ARG_RD, sizeof xform, &xform);
86 global_size = xsz * ysz;
87 return true;
88 }
90 void destroy_renderer()
91 {
92 delete [] prim_rays;
93 delete prog;
94 }
96 bool render()
97 {
98 if(!prog->run(1, global_size)) {
99 return false;
100 }
102 CLMemBuffer *mbuf = prog->get_arg_buffer(0);
103 map_mem_buffer(mbuf, MAP_RD);
104 /*if(!write_ppm("out.ppm", fb, xsz, ysz)) {
105 return 1;
106 }*/
107 unmap_mem_buffer(mbuf);
108 return true;
109 }
111 static Ray get_primary_ray(int x, int y, int w, int h, float vfov_deg)
112 {
113 float vfov = M_PI * vfov_deg / 180.0;
114 float aspect = (float)w / (float)h;
116 float ysz = 2.0;
117 float xsz = aspect * ysz;
119 float px = ((float)x / (float)w) * xsz - xsz / 2.0;
120 float py = 1.0 - ((float)y / (float)h) * ysz;
121 float pz = 1.0 / tan(0.5 * vfov);
123 px *= 100.0;
124 py *= 100.0;
125 pz *= 100.0;
127 Ray ray = {{0, 0, 0, 1}, {px, py, pz, 1}};
128 return ray;
129 }