Debian Codegraph @

servers/rendering/renderer_rd/shaders/environment/sdfgi_direct_light.glsl

raw
1 #[compute]
2
3 #version 450
4
5 #VERSION_DEFINES
6
7 layout(local_size_x = 64, local_size_y = 1, local_size_z = 1) in;
8
9 #define MAX_CASCADES 8
10
11 layout(set = 0, binding = 1) uniform texture3D sdf_cascades[MAX_CASCADES];
12 layout(set = 0, binding = 2) uniform sampler linear_sampler;
13
14 layout(set = 0, binding = 3, std430) restrict readonly buffer DispatchData {
15 uint x;
16 uint y;
17 uint z;
18 uint total_count;
19 }
20 dispatch_data;
21
22 struct ProcessVoxel {
23 uint position; // xyz 7 bit packed, extra 11 bits for neighbors.
24 uint albedo; // rgb bits 0-15 albedo, bits 16-21 are normal bits (set if geometry exists toward that side), extra 11 bits for neighbors.
25 uint light; // rgbe8985 encoded total saved light, extra 2 bits for neighbors.
26 uint light_aniso; // 55555 light anisotropy, extra 2 bits for neighbors.
27 //total neighbors: 26
28 };
29
30 #ifdef MODE_PROCESS_STATIC
31 layout(set = 0, binding = 4, std430) restrict buffer ProcessVoxels {
32 #else
33 layout(set = 0, binding = 4, std430) restrict buffer readonly ProcessVoxels {
34 #endif
35 ProcessVoxel data[];
36 }
37 process_voxels;
38
39 layout(r32ui, set = 0, binding = 5) uniform restrict uimage3D dst_light;
40 layout(rgba8, set = 0, binding = 6) uniform restrict image3D dst_aniso0;
41 layout(rg8, set = 0, binding = 7) uniform restrict image3D dst_aniso1;
42
43 struct CascadeData {
44 vec3 offset; //offset of (0,0,0) in world coordinates
45 float to_cell; // 1/bounds * grid_size
46 ivec3 probe_world_offset;
47 uint pad;
48 vec4 pad2;
49 };
50
51 layout(set = 0, binding = 8, std140) uniform Cascades {
52 CascadeData data[MAX_CASCADES];
53 }
54 cascades;
55
56 #define LIGHT_TYPE_DIRECTIONAL 0
57 #define LIGHT_TYPE_OMNI 1
58 #define LIGHT_TYPE_SPOT 2
59
60 struct Light {
61 vec3 color;
62 float energy;
63
64 vec3 direction;
65 bool has_shadow;
66
67 vec3 position;
68 float attenuation;
69
70 uint type;
71 float cos_spot_angle;
72 float inv_spot_attenuation;
73 float radius;
74 };
75
76 layout(set = 0, binding = 9, std140) buffer restrict readonly Lights {
77 Light data[];
78 }
79 lights;
80
81 layout(set = 0, binding = 10) uniform texture2DArray lightprobe_texture;
82 layout(set = 0, binding = 11) uniform texture3D occlusion_texture;
83
84 layout(push_constant, std430) uniform Params {
85 vec3 grid_size;
86 uint max_cascades;
87
88 uint cascade;
89 uint light_count;
90 uint process_offset;
91 uint process_increment;
92
93 int probe_axis_size;
94 float bounce_feedback;
95 float y_mult;
96 bool use_occlusion;
97 }
98 params;
99
100 vec2 octahedron_wrap(vec2 v) {
101 vec2 signVal;
102 signVal.x = v.x >= 0.0 ? 1.0 : -1.0;
103 signVal.y = v.y >= 0.0 ? 1.0 : -1.0;
104 return (1.0 - abs(v.yx)) * signVal;
105 }
106
107 vec2 octahedron_encode(vec3 n) {
108 // https://twitter.com/Stubbesaurus/status/937994790553227264
109 n /= (abs(n.x) + abs(n.y) + abs(n.z));
110 n.xy = n.z >= 0.0 ? n.xy : octahedron_wrap(n.xy);
111 n.xy = n.xy * 0.5 + 0.5;
112 return n.xy;
113 }
114
115 float get_omni_attenuation(float distance, float inv_range, float decay) {
116 float nd = distance * inv_range;
117 nd *= nd;
118 nd *= nd; // nd^4
119 nd = max(1.0 - nd, 0.0);
120 nd *= nd; // nd^2
121 return nd * pow(max(distance, 0.0001), -decay);
122 }
123
124 void main() {
125 uint voxel_index = uint(gl_GlobalInvocationID.x);
126
127 //used for skipping voxels every N frames
128 if (params.process_increment > 1) {
129 voxel_index *= params.process_increment;
130 voxel_index += params.process_offset;
131 }
132
133 if (voxel_index >= dispatch_data.total_count) {
134 return;
135 }
136
137 uint voxel_position = process_voxels.data[voxel_index].position;
138
139 //keep for storing to texture
140 ivec3 positioni = ivec3((uvec3(voxel_position, voxel_position, voxel_position) >> uvec3(0, 7, 14)) & uvec3(0x7F));
141
142 vec3 position = vec3(positioni) + vec3(0.5);
143 position /= cascades.data[params.cascade].to_cell;
144 position += cascades.data[params.cascade].offset;
145
146 uint voxel_albedo = process_voxels.data[voxel_index].albedo;
147
148 vec3 albedo = vec3(uvec3(voxel_albedo >> 10, voxel_albedo >> 5, voxel_albedo) & uvec3(0x1F)) / float(0x1F);
149 vec3 light_accum[6] = vec3[](vec3(0.0), vec3(0.0), vec3(0.0), vec3(0.0), vec3(0.0), vec3(0.0));
150 uint valid_aniso = (voxel_albedo >> 15) & 0x3F;
151
152 const vec3 aniso_dir[6] = vec3[](
153 vec3(1, 0, 0),
154 vec3(0, 1, 0),
155 vec3(0, 0, 1),
156 vec3(-1, 0, 0),
157 vec3(0, -1, 0),
158 vec3(0, 0, -1));
159
160 // Add indirect light first, in order to save computation resources
161 #ifdef MODE_PROCESS_DYNAMIC
162 if (params.bounce_feedback > 0.001) {
163 vec3 feedback = (params.bounce_feedback < 1.0) ? (albedo * params.bounce_feedback) : mix(albedo, vec3(1.0), params.bounce_feedback - 1.0);
164 vec3 pos = (vec3(positioni) + vec3(0.5)) * float(params.probe_axis_size - 1) / params.grid_size;
165 ivec3 probe_base_pos = ivec3(pos);
166
167 float weight_accum[6] = float[](0, 0, 0, 0, 0, 0);
168
169 ivec3 tex_pos = ivec3(probe_base_pos.xy, int(params.cascade));
170 tex_pos.x += probe_base_pos.z * int(params.probe_axis_size);
171
172 tex_pos.xy = tex_pos.xy * (OCT_SIZE + 2) + ivec2(1);
173
174 vec3 base_tex_posf = vec3(tex_pos);
175 vec2 tex_pixel_size = 1.0 / vec2(ivec2((OCT_SIZE + 2) * params.probe_axis_size * params.probe_axis_size, (OCT_SIZE + 2) * params.probe_axis_size));
176 vec3 probe_uv_offset = vec3(ivec3(OCT_SIZE + 2, OCT_SIZE + 2, (OCT_SIZE + 2) * params.probe_axis_size)) * tex_pixel_size.xyx;
177
178 for (uint j = 0; j < 8; j++) {
179 ivec3 offset = (ivec3(j) >> ivec3(0, 1, 2)) & ivec3(1, 1, 1);
180 ivec3 probe_posi = probe_base_pos;
181 probe_posi += offset;
182
183 // Compute weight
184
185 vec3 probe_pos = vec3(probe_posi);
186 vec3 probe_to_pos = pos - probe_pos;
187 vec3 probe_dir = normalize(-probe_to_pos);
188
189 // Compute lightprobe texture position
190
191 vec3 trilinear = vec3(1.0) - abs(probe_to_pos);
192
193 for (uint k = 0; k < 6; k++) {
194 if (bool(valid_aniso & (1 << k))) {
195 vec3 n = aniso_dir[k];
196 float weight = trilinear.x * trilinear.y * trilinear.z * max(0, dot(n, probe_dir));
197
198 if (weight > 0.0 && params.use_occlusion) {
199 ivec3 occ_indexv = abs((cascades.data[params.cascade].probe_world_offset + probe_posi) & ivec3(1, 1, 1)) * ivec3(1, 2, 4);
200 vec4 occ_mask = mix(vec4(0.0), vec4(1.0), equal(ivec4(occ_indexv.x | occ_indexv.y), ivec4(0, 1, 2, 3)));
201
202 vec3 occ_pos = (vec3(positioni) + aniso_dir[k] + vec3(0.5)) / params.grid_size;
203 occ_pos.z += float(params.cascade);
204 if (occ_indexv.z != 0) { //z bit is on, means index is >=4, so make it switch to the other half of textures
205 occ_pos.x += 1.0;
206 }
207 occ_pos *= vec3(0.5, 1.0, 1.0 / float(params.max_cascades)); //renormalize
208 float occlusion = dot(textureLod(sampler3D(occlusion_texture, linear_sampler), occ_pos, 0.0), occ_mask);
209
210 weight *= occlusion;
211 }
212
213 if (weight > 0.0) {
214 vec3 tex_posf = base_tex_posf + vec3(octahedron_encode(n) * float(OCT_SIZE), 0.0);
215 tex_posf.xy *= tex_pixel_size;
216
217 vec3 pos_uvw = tex_posf;
218 pos_uvw.xy += vec2(offset.xy) * probe_uv_offset.xy;
219 pos_uvw.x += float(offset.z) * probe_uv_offset.z;
220 vec3 indirect_light = textureLod(sampler2DArray(lightprobe_texture, linear_sampler), pos_uvw, 0.0).rgb;
221
222 light_accum[k] += indirect_light * weight;
223 weight_accum[k] += weight;
224 }
225 }
226 }
227 }
228
229 for (uint k = 0; k < 6; k++) {
230 if (weight_accum[k] > 0.0) {
231 light_accum[k] /= weight_accum[k];
232 light_accum[k] *= feedback;
233 }
234 }
235 }
236
237 #endif
238
239 {
240 uint rgbe = process_voxels.data[voxel_index].light;
241
242 //read rgbe8985
243 float r = float((rgbe & 0xff) << 1);
244 float g = float((rgbe >> 8) & 0x1ff);
245 float b = float(((rgbe >> 17) & 0xff) << 1);
246 float e = float((rgbe >> 25) & 0x1F);
247 float m = pow(2.0, e - 15.0 - 9.0);
248
249 vec3 l = vec3(r, g, b) * m;
250
251 uint aniso = process_voxels.data[voxel_index].light_aniso;
252 for (uint i = 0; i < 6; i++) {
253 float strength = ((aniso >> (i * 5)) & 0x1F) / float(0x1F);
254 light_accum[i] += l * strength;
255 }
256 }
257
258 // Raytrace light
259
260 vec3 pos_to_uvw = 1.0 / params.grid_size;
261 vec3 uvw_ofs = pos_to_uvw * 0.5;
262
263 for (uint i = 0; i < params.light_count; i++) {
264 float attenuation = 1.0;
265 vec3 direction;
266 float light_distance = 1e20;
267
268 switch (lights.data[i].type) {
269 case LIGHT_TYPE_DIRECTIONAL: {
270 direction = -lights.data[i].direction;
271 } break;
272 case LIGHT_TYPE_OMNI: {
273 vec3 rel_vec = lights.data[i].position - position;
274 direction = normalize(rel_vec);
275 light_distance = length(rel_vec);
276 rel_vec.y /= params.y_mult;
277 attenuation = get_omni_attenuation(light_distance, 1.0 / lights.data[i].radius, lights.data[i].attenuation);
278
279 } break;
280 case LIGHT_TYPE_SPOT: {
281 vec3 rel_vec = lights.data[i].position - position;
282 direction = normalize(rel_vec);
283 light_distance = length(rel_vec);
284 rel_vec.y /= params.y_mult;
285 attenuation = get_omni_attenuation(light_distance, 1.0 / lights.data[i].radius, lights.data[i].attenuation);
286
287 float cos_spot_angle = lights.data[i].cos_spot_angle;
288 float cos_angle = dot(-direction, lights.data[i].direction);
289
290 if (cos_angle < cos_spot_angle) {
291 continue;
292 }
293
294 float scos = max(cos_angle, cos_spot_angle);
295 float spot_rim = max(0.0001, (1.0 - scos) / (1.0 - cos_spot_angle));
296 attenuation *= 1.0 - pow(spot_rim, lights.data[i].inv_spot_attenuation);
297 } break;
298 }
299
300 if (attenuation < 0.001) {
301 continue;
302 }
303
304 bool hit = false;
305
306 vec3 ray_pos = position;
307 vec3 ray_dir = direction;
308 vec3 inv_dir = 1.0 / ray_dir;
309
310 //this is how to properly bias outgoing rays
311 float cell_size = 1.0 / cascades.data[params.cascade].to_cell;
312 ray_pos += sign(direction) * cell_size * 0.48; // go almost to the box edge but remain inside
313 ray_pos += ray_dir * 0.4 * cell_size; //apply a small bias from there
314
315 for (uint j = params.cascade; j < params.max_cascades; j++) {
316 //convert to local bounds
317 vec3 pos = ray_pos - cascades.data[j].offset;
318 pos *= cascades.data[j].to_cell;
319 float local_distance = light_distance * cascades.data[j].to_cell;
320
321 if (any(lessThan(pos, vec3(0.0))) || any(greaterThanEqual(pos, params.grid_size))) {
322 continue; //already past bounds for this cascade, goto next
323 }
324
325 //find maximum advance distance (until reaching bounds)
326 vec3 t0 = -pos * inv_dir;
327 vec3 t1 = (params.grid_size - pos) * inv_dir;
328 vec3 tmax = max(t0, t1);
329 float max_advance = min(tmax.x, min(tmax.y, tmax.z));
330
331 max_advance = min(local_distance, max_advance);
332
333 float advance = 0.0;
334 float occlusion = 1.0;
335
336 while (advance < max_advance) {
337 //read how much to advance from SDF
338 vec3 uvw = (pos + ray_dir * advance) * pos_to_uvw;
339
340 float distance = texture(sampler3D(sdf_cascades[j], linear_sampler), uvw).r * 255.0 - 1.0;
341 if (distance < 0.001) {
342 //consider hit
343 hit = true;
344 break;
345 }
346
347 occlusion = min(occlusion, distance);
348
349 advance += distance;
350 }
351
352 if (hit) {
353 attenuation *= occlusion;
354 break;
355 }
356
357 if (advance >= local_distance) {
358 break; //past light distance, abandon search
359 }
360 //change ray origin to collision with bounds
361 pos += ray_dir * max_advance;
362 pos /= cascades.data[j].to_cell;
363 pos += cascades.data[j].offset;
364 light_distance -= max_advance / cascades.data[j].to_cell;
365 ray_pos = pos;
366 }
367
368 if (!hit) {
369 vec3 light = albedo * lights.data[i].color.rgb * lights.data[i].energy * attenuation;
370
371 for (int j = 0; j < 6; j++) {
372 if (bool(valid_aniso & (1 << j))) {
373 light_accum[j] += max(0.0, dot(aniso_dir[j], direction)) * light;
374 }
375 }
376 }
377 }
378
379 // Store the light in the light texture
380
381 float lumas[6];
382 vec3 light_total = vec3(0);
383
384 for (int i = 0; i < 6; i++) {
385 light_total += light_accum[i];
386 lumas[i] = max(light_accum[i].r, max(light_accum[i].g, light_accum[i].b));
387 }
388
389 float luma_total = max(light_total.r, max(light_total.g, light_total.b));
390
391 uint light_total_rgbe;
392
393 {
394 //compress to RGBE9995 to save space
395
396 const float pow2to9 = 512.0f;
397 const float B = 15.0f;
398 const float N = 9.0f;
399 const float LN2 = 0.6931471805599453094172321215;
400
401 float cRed = clamp(light_total.r, 0.0, 65408.0);
402 float cGreen = clamp(light_total.g, 0.0, 65408.0);
403 float cBlue = clamp(light_total.b, 0.0, 65408.0);
404
405 float cMax = max(cRed, max(cGreen, cBlue));
406
407 float expp = max(-B - 1.0f, floor(log(cMax) / LN2)) + 1.0f + B;
408
409 float sMax = floor((cMax / pow(2.0f, expp - B - N)) + 0.5f);
410
411 float exps = expp + 1.0f;
412
413 if (0.0 <= sMax && sMax < pow2to9) {
414 exps = expp;
415 }
416
417 float sRed = floor((cRed / pow(2.0f, exps - B - N)) + 0.5f);
418 float sGreen = floor((cGreen / pow(2.0f, exps - B - N)) + 0.5f);
419 float sBlue = floor((cBlue / pow(2.0f, exps - B - N)) + 0.5f);
420 #ifdef MODE_PROCESS_STATIC
421 //since its self-save, use RGBE8985
422 light_total_rgbe = ((uint(sRed) & 0x1FF) >> 1) | ((uint(sGreen) & 0x1FF) << 8) | (((uint(sBlue) & 0x1FF) >> 1) << 17) | ((uint(exps) & 0x1F) << 25);
423
424 #else
425 light_total_rgbe = (uint(sRed) & 0x1FF) | ((uint(sGreen) & 0x1FF) << 9) | ((uint(sBlue) & 0x1FF) << 18) | ((uint(exps) & 0x1F) << 27);
426 #endif
427 }
428
429 #ifdef MODE_PROCESS_DYNAMIC
430
431 vec4 aniso0;
432 aniso0.r = lumas[0] / luma_total;
433 aniso0.g = lumas[1] / luma_total;
434 aniso0.b = lumas[2] / luma_total;
435 aniso0.a = lumas[3] / luma_total;
436
437 vec2 aniso1;
438 aniso1.r = lumas[4] / luma_total;
439 aniso1.g = lumas[5] / luma_total;
440
441 //save to 3D textures
442 imageStore(dst_aniso0, positioni, aniso0);
443 imageStore(dst_aniso1, positioni, vec4(aniso1, 0.0, 0.0));
444 imageStore(dst_light, positioni, uvec4(light_total_rgbe));
445
446 //also fill neighbors, so light interpolation during the indirect pass works
447
448 //recover the neighbor list from the leftover bits
449 uint neighbors = (voxel_albedo >> 21) | ((voxel_position >> 21) << 11) | ((process_voxels.data[voxel_index].light >> 30) << 22) | ((process_voxels.data[voxel_index].light_aniso >> 30) << 24);
450
451 const uint max_neighbours = 26;
452 const ivec3 neighbour_positions[max_neighbours] = ivec3[](
453 ivec3(-1, -1, -1),
454 ivec3(-1, -1, 0),
455 ivec3(-1, -1, 1),
456 ivec3(-1, 0, -1),
457 ivec3(-1, 0, 0),
458 ivec3(-1, 0, 1),
459 ivec3(-1, 1, -1),
460 ivec3(-1, 1, 0),
461 ivec3(-1, 1, 1),
462 ivec3(0, -1, -1),
463 ivec3(0, -1, 0),
464 ivec3(0, -1, 1),
465 ivec3(0, 0, -1),
466 ivec3(0, 0, 1),
467 ivec3(0, 1, -1),
468 ivec3(0, 1, 0),
469 ivec3(0, 1, 1),
470 ivec3(1, -1, -1),
471 ivec3(1, -1, 0),
472 ivec3(1, -1, 1),
473 ivec3(1, 0, -1),
474 ivec3(1, 0, 0),
475 ivec3(1, 0, 1),
476 ivec3(1, 1, -1),
477 ivec3(1, 1, 0),
478 ivec3(1, 1, 1));
479
480 for (uint i = 0; i < max_neighbours; i++) {
481 if (bool(neighbors & (1 << i))) {
482 ivec3 neighbour_pos = positioni + neighbour_positions[i];
483 imageStore(dst_light, neighbour_pos, uvec4(light_total_rgbe));
484 imageStore(dst_aniso0, neighbour_pos, aniso0);
485 imageStore(dst_aniso1, neighbour_pos, vec4(aniso1, 0.0, 0.0));
486 }
487 }
488
489 #endif
490
491 #ifdef MODE_PROCESS_STATIC
492
493 //save back the anisotropic
494
495 uint light = process_voxels.data[voxel_index].light & (3 << 30);
496 light |= light_total_rgbe;
497 process_voxels.data[voxel_index].light = light; //replace
498
499 uint light_aniso = process_voxels.data[voxel_index].light_aniso & (3 << 30);
500 for (int i = 0; i < 6; i++) {
501 light_aniso |= min(31, uint((lumas[i] / luma_total) * 31.0)) << (i * 5);
502 }
503
504 process_voxels.data[voxel_index].light_aniso = light_aniso;
505
506 #endif
507 }