-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathraycastkernel.c
274 lines (237 loc) · 9.16 KB
/
raycastkernel.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#define MAX_DEPTH 16
#define MARCH_DIST //%MARCH_DIST.0f
#define MARCH_REPS //%MARCH_REPS
#define MAT_LEN //%MAT_LENGTH
#define TEXTURE_T __read_only image2d_array_t
#define SCENE_T(S, T) SceneInfo* S , TEXTURE_T T
#define DFLT2(V) ((V).x), ((V).y)
#define DFLT3(V) ((V).x), ((V).y), ((V).z)
#define DFLT4(V) ((V).x), ((V).y), ((V).z), ((V).w)
#define writeln(X) if (Is_Debug()){printf(X "\n"); }
#define writeint(X) if (Is_Debug()){printf(#X " %d\n", (X));}
#define writefloat(X) if (Is_Debug()){printf(#X " %f\n", (X));}
#define writefloat2(X) if (Is_Debug()){printf(#X " %f, %f\n", DFLT2(X));}
#define writefloat3(X) if (Is_Debug()){printf(#X " %f, %f, %f\n", DFLT3(X));}
#define writefloat4(X) if (Is_Debug()){printf(#X " %f, %f, %f, %f\n",DFLT4(X));}
bool Is_Debug ( ) {
return get_global_id(0) == get_global_size(0)/2 &&
get_global_id(1) == get_global_size(1)/2;
}
__constant float MARCH_ACC = //%MARCH_ACC.0f/1000.0f;
// -----------------------------------------------------------------------------
// --------------- GPU-CPU STRUCTS ---------------------------------------------
typedef struct T_Camera {
float3 position, lookat, up;
int2 dim;
float fov, focal, radius;
int flags;
} Camera;
typedef struct T_Material {
// colour [set to (-1.0, -1.0, -1.0) to have map override it]
float3 albedo;
// sampling strategy
float diffuse, specular, glossy, glossy_lobe;
float transmittive, ior;
// PBR material
float roughness, metallic, fresnel, subsurface, anisotropic;
} Material;
typedef struct T_Emitter {
float3 origin, emission;
float radius;
int index;
} Emitter;
typedef struct T_SharedInfo {
unsigned char clear_img;
unsigned long finished_samples;
unsigned char spp;
uint2 rng_state;
} SharedInfo;
typedef struct T_SceneInfo {
float time;
// __read_only image2d_array_t textures; IMAGES CANT BE USED AS FIELD TYPES:-(
__constant Material* materials;
float3 debug_values;
uint2 rng_state;
} SceneInfo;
SceneInfo New_SceneInfo(float time, __constant Material* materials,
float3 debug_values, uint2 rng){
SceneInfo si;
si.time = time;
si.materials = materials;
si.debug_values = debug_values;
si.rng_state = rng;
return si;
}
__constant float PI = 3.141592654f;
__constant float TAU = 6.283185307f;
// -----------------------------------------------------------------------------
// --------------- GENERAL STRUCTS ---------------------------------------------
typedef struct T_Ray {
float3 origin, dir;
} Ray;
typedef struct T_SampledPt {
float3 colour, origin, dir;
float dist;
int material_index;
} SampledPt;
// -----------------------------------------------------------------------------
// --------------- GENERAL FUNCTIONS --------------------------------------
float Rand ( SceneInfo* si ) {
enum { A=4294883355U };
uint2 r = (*si).rng_state;
uint res = r.x^r.y;
uint hi = mul_hi(r.x, A);
r.x = r.x*A + r.y;
r.y = hi + (r.x<r.y);
(*si).rng_state = r;
return res/(float)(UINT_MAX);
}
float Sample_Uniform ( SceneInfo* si ) {
return Rand(si);
}
float2 Sample_Uniform2 ( SceneInfo* si ) {
return (float2)(Sample_Uniform(si), Sample_Uniform(si));
}
// -1 .. 1
float2 Sample_Uniform2_2 ( SceneInfo* si ) {
return ((float2)(Sample_Uniform(si), Sample_Uniform(si))-(float2)(0.5f))*2.0f;
}
float3 Sample_Uniform3 ( SceneInfo* si ) {
return (float3)(Sample_Uniform(si), Sample_Uniform2(si));
}
// -----------------------------------------------------------------------------
// --------------- MAP GEOMETRY FUNCTIONS --------------------------------------
//---MAP GEOMETRY INSERTION POINT---
//%MAPFUNCDECLARATIONS
//----------------------------------
//%MAPFUNCDEFINITIONS
//----------------------------------
void MapUnionG( int avoid, SampledPt* d1, float d, int mi, float3 c ) {
if ( mi != avoid && d1->dist > d ) {
d1->colour = c;
d1->dist = d;
d1->material_index = mi;
}
}
// -----------------------------------------------------------------------------
// --------------- SCENE -------------------------------------------------------
//%SCENEINSERT
//------------------------
// -----------------------------------------------------------------------------
// --------------- MAP ---------------------------------------------------------
SampledPt Map ( int a, float3 origin, SCENE_T(si, Tx) ) {
SampledPt res;
res.dist = FLT_MAX;
//---MAP INSERTION POINT---
//%MAPINSERT
//-------------------------
// approx lighting with emissions
for ( int i = 0; i != EMITTER_AMT; ++ i ) {
Emitter e = REmission(i, si->debug_values, si->time);
float dist = sdSphere(origin - e.origin, e.radius);
MapUnionG(a, &res, dist, -100 - i, e.emission);
}
return res;
}
// -----------------------------------------------------------------------------
// --------------- RAYTRACING/MARCH --------------------------------------------
SampledPt March ( int avoid, Ray ray, SCENE_T(si, Tx) ) {
float distance = 0.0f;
SampledPt t_info;
for ( int i = 0; i < MARCH_REPS; ++ i ) {
t_info = Map(avoid, ray.origin + ray.dir*distance, si, Tx);
if ( t_info.dist <= 0.0001f || t_info.dist > MARCH_DIST ) break;
distance += t_info.dist;
}
if ( t_info.dist > MARCH_DIST || t_info.dist < 0.0f ) {
t_info.dist = -1.0f;
return t_info;
}
t_info.dist = distance;
return t_info;
}
float Distance(float3 u, float3 v) {
float x = u.x-v.x, y = u.y-v.y, z = u.z-v.z;
return sqrt(x*x + y*y + z*z);
}
float3 RColour ( float3 pt_colour, __constant Material* m ) {
return (pt_colour.x >= 0.0f) ? pt_colour : m->albedo;
}
//----POSTPROCESS---
//%POSTPROCESS
// -----------------------------------------------------------------------------
// --------------- CAMERA ------------------------------------------------------
Ray Camera_Ray(Camera* camera, SceneInfo* si) {
float2 coord = (float2)((float)get_global_id(0), (float)get_global_id(1));
float2 resolution = (float2)((float)camera->dim.x, (float)camera->dim.y);
resolution.y *= 16.0f/9.0f;
float fov_r = (180.0f - camera->fov)*PI/180.0f;
float2 mouse_pos = camera->lookat.xy;
float2 puv = -1.0f + 2.0f * (coord/resolution);
float input_angle = PI - 2.0f*PI*mouse_pos.x;
float3 cam_pos = camera->position;
float3 cam_target = cam_pos + (float3)(sin(input_angle),
(3.0f * mouse_pos.y) - 1.0f,
cos(input_angle));
float3 cam_front = normalize(cam_target - cam_pos);
float3 cam_right = normalize ( cross(cam_front, (float3)(0.0f, 1.0f, 0.0f)));
float3 cam_up = normalize(cross(cam_right, cam_front));
Ray ray;
/* // ------ DOF & antialiasing ----- */
float3 ray_dir = puv.x*cam_right + puv.y*cam_up + fov_r*cam_front;
return (Ray){cam_pos, normalize(ray_dir)};
}
float3 Normal ( float3 p, SCENE_T(si, Tx) ) {
float2 e = (float2)(1.0f, -1.0f)*0.5773f*0.0005f;
return normalize(
e.xyy*Map(-1, p + e.xyy, si, Tx).dist +
e.yyx*Map(-1, p + e.yyx, si, Tx).dist +
e.yxy*Map(-1, p + e.yxy, si, Tx).dist +
e.xxx*Map(-1, p + e.xxx, si, Tx).dist);
}
// -----------------------------------------------------------------------------
// --------------- RAYTRACE KERNEL ---------------------------------------------
__kernel void DTOADQ_Kernel (
__global unsigned char* img, // R G B ITER
__write_only image2d_t output_img,
__global SharedInfo* sinfo,
__global Camera* camera_ptr,
__global float* time_ptr,
__read_only image2d_array_t textures,
__constant Material* materials,
__global float* debug_val_ptr,
__global uint2* rng_states
) {
int2 out = (int2)(get_global_id(0), get_global_id(1));
Camera camera = *camera_ptr;
float3 dval = (float3)(debug_val_ptr[0], debug_val_ptr[1], debug_val_ptr[2]);
float time = *time_ptr;
// -- get old pixel, check if there are samples to be done
// (counter is stored in alpha channel)
Update_Camera(&camera, time);
SceneInfo scene_info = New_SceneInfo(time, materials, dval,
rng_states[out.y*camera.dim.x + out.x]);
Ray ray = Camera_Ray(&camera, &scene_info);
SampledPt pt = March(-1, ray, &scene_info, textures);
float3 col = (float3)(0.0f);
if ( pt.dist >= 0 ) {
float3 O = ray.origin + pt.dist*ray.dir;
for ( int i = 0; i != EMITTER_AMT; ++ i ) {
Emitter emit = REmission(i, scene_info.debug_values, scene_info.time);
if ( pt.material_index >= 0 && pt.material_index < MAT_LEN ) {
float3 albedo = RColour(pt.colour, materials+pt.material_index);
// diffusive component i made up, no normal needed
float3 lo = emit.origin - O;
float3 V = -ray.dir, L = normalize(lo);
col += albedo*0.6f + dot(V, L)*0.2f;
/* col += normalize(lo)*0.1f; */
/* col += Normal(O, &scene_info, textures)*0.2f; */
} else if ( pt.material_index < -10 ) {
col = (float3)(1.0f, 1.0f, 1.0f)*ray.dir;
}
}
col /= (float)(EMITTER_AMT);
}
write_imagef(output_img, out, (float4)(col, 1.0f));
rng_states[out.y*camera.dim.x + out.x] = scene_info.rng_state;
}