Repository navigation
Expand file tree
/
Copy pathraytracing.glsl
More file actions
289 lines (221 loc) · 6.73 KB
/
Copy pathraytracing.glsl
File metadata and controls
289 lines (221 loc) · 6.73 KB
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
layout(local_size_x = 16, local_size_y = 16) in;
layout(binding = 0, rgba32f) uniform image2D output_texture;
layout(binding = 1, rgba32f) uniform image2D output_accum_texture;
layout(binding = 3, rgba32f) uniform image2D normal_texture;
layout(binding = 4, rgba32f) uniform image2D position_texture;
layout(binding = 5, rgba32f) uniform image2D light_texture;
layout(binding = 6, rgba32f) uniform image2D light_accum_texture;
layout(binding = 7, rgba32f) uniform image2D color_texture;
struct GPUObject {
mat4 rotation;
vec3 position; // 12 + 4
vec3 normal; // 12 + 4
vec3 vertex1; // 12 + 4
vec3 vertex2; // 12 + 4
float radius; // 4
int mat_index; // 4
int type; // 4
};
struct GPUTriangle
{
vec3 position;
vec3 vertex1;
vec3 vertex2;
vec3 normal_vertex1;
vec3 normal_vertex2;
vec3 normal_vertex3;
vec2 texture_vertex1;
vec2 texture_vertex2;
vec2 texture_vertex3;
int mat_index;
};
struct GPUMaterial
{
vec3 color; // 12 + 4
float emission; // 4
float roughness; // 4
float metallic; // 4
float refraction; // 4
int type; // 4
int texture_index; // 4
int emission_texture_index; // 4
};
struct GPUCamera
{
mat4 view_matrix;
vec3 position;
float aperture_size;
float focus_distance;
float fov;
int bounce;
};
struct GPUVolume
{
vec3 sigma_a; // absorption coefficient
vec3 sigma_s; // scattering coefficient
vec3 sigma_t; // extinction coefficient
float g; // phase function parameter
int enabled;
};
struct GPUBvhData
{
mat4 transform;
mat4 inv_transform;
vec3 offset;
float scale;
int bvh_start_index;
int triangle_start_index;
};
struct GPUBvh
{
vec3 min;
vec3 max;
int index;
int primitive_count;
};
layout(std430, binding = 1) buffer ObjectBuffer
{
GPUObject objects[];
};
layout(std430, binding = 2) buffer TriangleBuffer
{
GPUTriangle triangles[];
};
layout(std430, binding = 3) buffer BvhDataBuffer
{
GPUBvhData BvhData[];
};
layout(std430, binding = 4) buffer BvhBuffer
{
GPUBvh Bvh[];
};
layout(std430, binding = 5) buffer MaterialBuffer
{
GPUMaterial materials[];
};
layout(std430, binding = 6) buffer LightsBuffer
{
int lightsIndex[];
};
layout(std140, binding = 0) uniform CameraData
{
GPUCamera camera;
};
layout(std140, binding = 1) uniform VolumeData
{
GPUVolume volume;
};
uniform int u_objectsNum;
uniform int u_bvhNum;
uniform int u_lightsNum;
uniform vec2 u_resolution;
uniform int u_pixelisation;
uniform int u_frameCount;
uniform float u_time;
struct Ray
{
vec3 origin;
vec3 direction;
vec3 inv_direction;
};
struct hitInfo
{
float t;
float last_t;
vec3 normal;
vec3 position;
int obj_index;
int mat_index;
int obj_type;
float u;
float v;
};
#include "shaders/random.glsl"
#include "shaders/intersect.glsl"
#include "shaders/scatter.glsl"
#include "shaders/light.glsl"
#include "shaders/volumetric.glsl"
#include "shaders/trace.glsl"
#define accumulate(texture, new_color, color) imageLoad(texture, ivec2(gl_GlobalInvocationID.xy)); new_color.rgb = mix(new_color.rgb, color, 1.0 / float(u_frameCount + 1)); new_color.a = 1.0; imageStore(texture, ivec2(gl_GlobalInvocationID.xy), new_color);
vec3[2] pathtrace(Ray ray, inout uint rng_state)
{
vec3 color = vec3(1.0);
vec3 light = vec3(0.0);
vec3 transmittance = vec3(1.0);
for (int i = 0; i < camera.bounce; i++)
{
hitInfo hit = traceRay(ray);
#if SHADER_FOG
float t_scatter = 0.0;
bool scatter_valid = bool(volume.enabled != 0 && atmosScatter(hit, t_scatter, rng_state));
if (scatter_valid)
{
calculateVolumetricLight(t_scatter, ray, color, light, transmittance, rng_state);
continue ;
}
#endif
if (hit.obj_index == -1)
{
light += transmittance * GetEnvironmentLight(ray);
break;
}
if (i == 0)
{
// imageStore(normal_texture, ivec2(gl_GlobalInvocationID.xy), vec4(normalize(hit.normal), 1.0));
// imageStore(position_texture, ivec2(gl_GlobalInvocationID.xy), vec4(normalize(hit.position), 1.0));
vec4 accum_normal = accumulate(normal_texture, accum_normal, normalize(hit.normal));
vec4 accum_position = accumulate(position_texture, accum_position, normalize(hit.position));
}
float p = max(color.r, max(color.g, color.b));
if (randomValue(rng_state) >= p) break;
color /= max(p, 0.001);
GPUMaterial mat = materials[hit.mat_index];
calculateLightColor(mat, hit, color, light, rng_state);
if (mat.emission > 0.0 && mat.emission_texture_index == -1)
break;
ray = newRay(hit, ray, rng_state);
ray.inv_direction = 1.0 / ray.direction;
}
vec3[2] result = {color, light};
return result;
}
Ray initRay(vec2 uv, inout uint rng_state)
{
float focal_length = 1.0 / tan(radians(camera.fov) / 2.0);
vec3 origin = camera.position;
vec3 view_space_ray = normalize(vec3(uv.x, uv.y, -focal_length));
vec3 ray_direction = normalize((inverse(camera.view_matrix) * vec4(view_space_ray, 0.0)).xyz);
vec3 right = vec3(camera.view_matrix[0][0], camera.view_matrix[1][0], camera.view_matrix[2][0]);
vec3 up = vec3(camera.view_matrix[0][1], camera.view_matrix[1][1], camera.view_matrix[2][1]);
vec3 focal_point = origin + ray_direction * camera.focus_distance;
float r = sqrt(randomValue(rng_state));
float theta = 2.0 * M_PI * randomValue(rng_state);
vec2 lens_point = camera.aperture_size * r * vec2(cos(theta), sin(theta));
origin += right * lens_point.x + up * lens_point.y;
ray_direction = normalize(focal_point - origin);
return (Ray(origin, ray_direction, 1.0 / ray_direction));
}
void main()
{
ivec2 pixel_coords = ivec2(gl_GlobalInvocationID.xy);
if (pixel_coords.x >= int(u_resolution.x) || pixel_coords.y >= int(u_resolution.y))
return;
if (u_pixelisation != 1 && (uint(pixel_coords.x) % u_pixelisation != 0 || uint(pixel_coords.y) % u_pixelisation != 0))
return;
uint rng_state = uint(u_resolution.x) * uint(pixel_coords.y) + uint(pixel_coords.x);
rng_state = rng_state + u_frameCount * 719393;
vec2 jitter = randomPointInCircle(rng_state) * 1;
vec2 uv = ((vec2(pixel_coords) + jitter) / u_resolution) * 2.0 - 1.0;
uv.x *= u_resolution.x / u_resolution.y;
Ray ray = initRay(uv, rng_state);
vec3[2] color_light = pathtrace(ray, rng_state);
vec3 color = color_light[0] * color_light[1];
vec4 accum_color = accumulate(color_texture, accum_color, (color_light[0]));
vec4 accum_light = accumulate(light_accum_texture, accum_light, color_light[1]);
imageStore(light_texture, pixel_coords, accum_light);
vec4 accum = accumulate(output_accum_texture, accum, color);
vec4 final_output_color = sqrt(accum);
for (int y = 0; y < u_pixelisation; y++)
for (int x = 0; x < u_pixelisation; x++)
imageStore(output_texture, pixel_coords + ivec2(x, y), final_output_color);
}