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