Debian Codegraph @

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

raw
1 #[compute]
2
3 #version 450
4
5 #VERSION_DEFINES
6
7 #ifdef MODE_DYNAMIC
8 layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
9 #else
10 layout(local_size_x = 64, local_size_y = 1, local_size_z = 1) in;
11 #endif
12
13 #ifndef MODE_DYNAMIC
14
15 #define NO_CHILDREN 0xFFFFFFFF
16
17 struct CellChildren {
18 uint children[8];
19 };
20
21 layout(set = 0, binding = 1, std430) buffer CellChildrenBuffer {
22 CellChildren data[];
23 }
24 cell_children;
25
26 struct CellData {
27 uint position; // xyz 10 bits
28 uint albedo; //rgb albedo
29 uint emission; //rgb normalized with e as multiplier
30 uint normal; //RGB normal encoded
31 };
32
33 layout(set = 0, binding = 2, std430) buffer CellDataBuffer {
34 CellData data[];
35 }
36 cell_data;
37
38 #endif // MODE DYNAMIC
39
40 #define LIGHT_TYPE_DIRECTIONAL 0
41 #define LIGHT_TYPE_OMNI 1
42 #define LIGHT_TYPE_SPOT 2
43
44 #if defined(MODE_COMPUTE_LIGHT) || defined(MODE_DYNAMIC_LIGHTING)
45
46 struct Light {
47 uint type;
48 float energy;
49 float radius;
50 float attenuation;
51
52 vec3 color;
53 float cos_spot_angle;
54
55 vec3 position;
56 float inv_spot_attenuation;
57
58 vec3 direction;
59 bool has_shadow;
60 };
61
62 layout(set = 0, binding = 3, std140) uniform Lights {
63 Light data[MAX_LIGHTS];
64 }
65 lights;
66
67 #endif // MODE COMPUTE LIGHT
68
69 #ifdef MODE_SECOND_BOUNCE
70
71 layout(set = 0, binding = 5) uniform texture3D color_texture;
72
73 #endif // MODE_SECOND_BOUNCE
74
75 #ifndef MODE_DYNAMIC
76
77 layout(push_constant, std430) uniform Params {
78 ivec3 limits;
79 uint stack_size;
80
81 float emission_scale;
82 float propagation;
83 float dynamic_range;
84
85 uint light_count;
86 uint cell_offset;
87 uint cell_count;
88 float aniso_strength;
89 float cell_size;
90 }
91 params;
92
93 layout(set = 0, binding = 4, std430) buffer Outputs {
94 vec4 data[];
95 }
96 outputs;
97
98 #endif // MODE DYNAMIC
99
100 layout(set = 0, binding = 9) uniform texture3D texture_sdf;
101 layout(set = 0, binding = 10) uniform sampler texture_sampler;
102
103 #ifdef MODE_WRITE_TEXTURE
104
105 layout(rgba8, set = 0, binding = 5) uniform restrict writeonly image3D color_tex;
106
107 #endif
108
109 #ifdef MODE_DYNAMIC
110
111 layout(push_constant, std430) uniform Params {
112 ivec3 limits;
113 uint light_count; //when not lighting
114 ivec3 x_dir;
115 float z_base;
116 ivec3 y_dir;
117 float z_sign;
118 ivec3 z_dir;
119 float pos_multiplier;
120 ivec2 rect_pos;
121 ivec2 rect_size;
122 ivec2 prev_rect_ofs;
123 ivec2 prev_rect_size;
124 bool flip_x;
125 bool flip_y;
126 float dynamic_range;
127 bool on_mipmap;
128 float propagation;
129 float cell_size;
130 float pad[2];
131 }
132 params;
133
134 #ifdef MODE_DYNAMIC_LIGHTING
135
136 layout(rgba8, set = 0, binding = 5) uniform restrict readonly image2D source_albedo;
137 layout(rgba8, set = 0, binding = 6) uniform restrict readonly image2D source_normal;
138 layout(rgba8, set = 0, binding = 7) uniform restrict readonly image2D source_orm;
139 //layout (set=0,binding=8) uniform texture2D source_depth;
140 layout(rgba16f, set = 0, binding = 11) uniform restrict image2D emission;
141 layout(r32f, set = 0, binding = 12) uniform restrict image2D depth;
142
143 #endif
144
145 #ifdef MODE_DYNAMIC_SHRINK
146
147 layout(rgba16f, set = 0, binding = 5) uniform restrict readonly image2D source_light;
148 layout(r32f, set = 0, binding = 6) uniform restrict readonly image2D source_depth;
149
150 #ifdef MODE_DYNAMIC_SHRINK_WRITE
151
152 layout(rgba16f, set = 0, binding = 7) uniform restrict writeonly image2D light;
153 layout(r32f, set = 0, binding = 8) uniform restrict writeonly image2D depth;
154
155 #endif // MODE_DYNAMIC_SHRINK_WRITE
156
157 #ifdef MODE_DYNAMIC_SHRINK_PLOT
158
159 layout(rgba8, set = 0, binding = 11) uniform restrict image3D color_texture;
160
161 #endif //MODE_DYNAMIC_SHRINK_PLOT
162
163 #endif // MODE_DYNAMIC_SHRINK
164
165 //layout (rgba8,set=0,binding=5) uniform restrict writeonly image3D color_tex;
166
167 #endif // MODE DYNAMIC
168
169 #if defined(MODE_COMPUTE_LIGHT) || defined(MODE_DYNAMIC_LIGHTING)
170
171 float raymarch(float distance, float distance_adv, vec3 from, vec3 direction) {
172 vec3 cell_size = 1.0 / vec3(params.limits);
173 float occlusion = 1.0;
174 while (distance > 0.5) { //use this to avoid precision errors
175 float advance = texture(sampler3D(texture_sdf, texture_sampler), from * cell_size).r * 255.0 - 1.0;
176 if (advance < 0.0) {
177 occlusion = 0.0;
178 break;
179 }
180
181 occlusion = min(advance, occlusion);
182
183 advance = max(distance_adv, advance - mod(advance, distance_adv)); //should always advance in multiples of distance_adv
184
185 from += direction * advance;
186 distance -= advance;
187 }
188
189 return occlusion; //max(0.0,distance);
190 }
191
192 float get_omni_attenuation(float distance, float inv_range, float decay) {
193 float nd = distance * inv_range;
194 nd *= nd;
195 nd *= nd; // nd^4
196 nd = max(1.0 - nd, 0.0);
197 nd *= nd; // nd^2
198 return nd * pow(max(distance, 0.0001), -decay);
199 }
200
201 bool compute_light_vector(uint light, vec3 pos, out float attenuation, out vec3 light_pos) {
202 if (lights.data[light].type == LIGHT_TYPE_DIRECTIONAL) {
203 light_pos = pos - lights.data[light].direction * length(vec3(params.limits));
204 attenuation = 1.0;
205
206 } else {
207 light_pos = lights.data[light].position;
208 float distance = length(pos - light_pos);
209 if (distance >= lights.data[light].radius) {
210 return false;
211 }
212
213 attenuation = get_omni_attenuation(
214 distance * params.cell_size,
215 1.0 / (lights.data[light].radius * params.cell_size),
216 lights.data[light].attenuation);
217
218 if (lights.data[light].type == LIGHT_TYPE_SPOT) {
219 vec3 rel = normalize(pos - light_pos);
220 float cos_spot_angle = lights.data[light].cos_spot_angle;
221 float cos_angle = dot(rel, lights.data[light].direction);
222 if (cos_angle < cos_spot_angle) {
223 return false;
224 }
225
226 float scos = max(cos_angle, cos_spot_angle);
227 float spot_rim = max(0.0001, (1.0 - scos) / (1.0 - cos_spot_angle));
228 attenuation *= 1.0 - pow(spot_rim, lights.data[light].inv_spot_attenuation);
229 }
230 }
231
232 return true;
233 }
234
235 float get_normal_advance(vec3 p_normal) {
236 vec3 normal = p_normal;
237 vec3 unorm = abs(normal);
238
239 if ((unorm.x >= unorm.y) && (unorm.x >= unorm.z)) {
240 // x code
241 unorm = normal.x > 0.0 ? vec3(1.0, 0.0, 0.0) : vec3(-1.0, 0.0, 0.0);
242 } else if ((unorm.y > unorm.x) && (unorm.y >= unorm.z)) {
243 // y code
244 unorm = normal.y > 0.0 ? vec3(0.0, 1.0, 0.0) : vec3(0.0, -1.0, 0.0);
245 } else if ((unorm.z > unorm.x) && (unorm.z > unorm.y)) {
246 // z code
247 unorm = normal.z > 0.0 ? vec3(0.0, 0.0, 1.0) : vec3(0.0, 0.0, -1.0);
248 } else {
249 // oh-no we messed up code
250 // has to be
251 unorm = vec3(1.0, 0.0, 0.0);
252 }
253
254 return 1.0 / dot(normal, unorm);
255 }
256
257 void clip_segment(vec4 plane, vec3 begin, inout vec3 end) {
258 vec3 segment = begin - end;
259 float den = dot(plane.xyz, segment);
260
261 //printf("den is %i\n",den);
262 if (den < 0.0001) {
263 return;
264 }
265
266 float dist = (dot(plane.xyz, begin) - plane.w) / den;
267
268 if (dist < 0.0001 || dist > 1.0001) {
269 return;
270 }
271
272 end = begin + segment * -dist;
273 }
274
275 bool compute_light_at_pos(uint index, vec3 pos, vec3 normal, inout vec3 light, inout vec3 light_dir) {
276 float attenuation;
277 vec3 light_pos;
278
279 if (!compute_light_vector(index, pos, attenuation, light_pos)) {
280 return false;
281 }
282
283 light_dir = normalize(pos - light_pos);
284
285 if (attenuation < 0.01 || (length(normal) > 0.2 && dot(normal, light_dir) >= 0)) {
286 return false; //not facing the light, or attenuation is near zero
287 }
288
289 if (lights.data[index].has_shadow) {
290 float distance_adv = get_normal_advance(light_dir);
291
292 vec3 to = pos;
293 if (length(normal) > 0.2) {
294 to += normal * distance_adv * 0.51;
295 } else {
296 to -= sign(light_dir) * 0.45; //go near the edge towards the light direction to avoid self occlusion
297 }
298
299 //clip
300 clip_segment(mix(vec4(-1.0, 0.0, 0.0, 0.0), vec4(1.0, 0.0, 0.0, float(params.limits.x - 1)), bvec4(light_dir.x < 0.0)), to, light_pos);
301 clip_segment(mix(vec4(0.0, -1.0, 0.0, 0.0), vec4(0.0, 1.0, 0.0, float(params.limits.y - 1)), bvec4(light_dir.y < 0.0)), to, light_pos);
302 clip_segment(mix(vec4(0.0, 0.0, -1.0, 0.0), vec4(0.0, 0.0, 1.0, float(params.limits.z - 1)), bvec4(light_dir.z < 0.0)), to, light_pos);
303
304 float distance = length(to - light_pos);
305 if (distance < 0.1) {
306 return false; // hit
307 }
308
309 distance += distance_adv - mod(distance, distance_adv); //make it reach the center of the box always
310 light_pos = to - light_dir * distance;
311
312 //from -= sign(light_dir)*0.45; //go near the edge towards the light direction to avoid self occlusion
313
314 /*float dist = raymarch(distance,distance_adv,light_pos,light_dir);
315
316 if (dist > distance_adv) {
317 return false;
318 }
319
320 attenuation *= 1.0 - smoothstep(0.1*distance_adv,distance_adv,dist);
321 */
322
323 float occlusion = raymarch(distance, distance_adv, light_pos, light_dir);
324
325 if (occlusion == 0.0) {
326 return false;
327 }
328
329 attenuation *= occlusion; //1.0 - smoothstep(0.1*distance_adv,distance_adv,dist);
330 }
331
332 light = lights.data[index].color * attenuation * lights.data[index].energy;
333 return true;
334 }
335
336 #endif // MODE COMPUTE LIGHT
337
338 void main() {
339 #ifndef MODE_DYNAMIC
340
341 uint cell_index = gl_GlobalInvocationID.x;
342 if (cell_index >= params.cell_count) {
343 return;
344 }
345 cell_index += params.cell_offset;
346
347 uvec3 posu = uvec3(cell_data.data[cell_index].position & 0x7FF, (cell_data.data[cell_index].position >> 11) & 0x3FF, cell_data.data[cell_index].position >> 21);
348 vec4 albedo = unpackUnorm4x8(cell_data.data[cell_index].albedo);
349
350 #endif
351
352 /////////////////COMPUTE LIGHT///////////////////////////////
353
354 #ifdef MODE_COMPUTE_LIGHT
355
356 vec3 pos = vec3(posu) + vec3(0.5);
357
358 vec3 emission = vec3(uvec3(cell_data.data[cell_index].emission & 0x1ff, (cell_data.data[cell_index].emission >> 9) & 0x1ff, (cell_data.data[cell_index].emission >> 18) & 0x1ff)) * pow(2.0, float(cell_data.data[cell_index].emission >> 27) - 15.0 - 9.0);
359 vec3 normal = unpackSnorm4x8(cell_data.data[cell_index].normal).xyz;
360
361 vec3 accum = vec3(0.0);
362
363 for (uint i = 0; i < params.light_count; i++) {
364 vec3 light;
365 vec3 light_dir;
366 if (!compute_light_at_pos(i, pos, normal.xyz, light, light_dir)) {
367 continue;
368 }
369
370 light *= albedo.rgb;
371
372 if (length(normal) > 0.2) {
373 accum += max(0.0, dot(normal, -light_dir)) * light;
374 } else {
375 //all directions
376 accum += light;
377 }
378 }
379
380 outputs.data[cell_index] = vec4(accum + emission, 0.0);
381
382 #endif //MODE_COMPUTE_LIGHT
383
384 /////////////////SECOND BOUNCE///////////////////////////////
385
386 #ifdef MODE_SECOND_BOUNCE
387 vec3 pos = vec3(posu) + vec3(0.5);
388 ivec3 ipos = ivec3(posu);
389 vec4 normal = unpackSnorm4x8(cell_data.data[cell_index].normal);
390
391 vec3 accum = outputs.data[cell_index].rgb;
392
393 if (length(normal.xyz) > 0.2) {
394 vec3 v0 = abs(normal.z) < 0.999 ? vec3(0.0, 0.0, 1.0) : vec3(0.0, 1.0, 0.0);
395 vec3 tangent = normalize(cross(v0, normal.xyz));
396 vec3 bitangent = normalize(cross(tangent, normal.xyz));
397 mat3 normal_mat = mat3(tangent, bitangent, normal.xyz);
398
399 #define MAX_CONE_DIRS 6
400
401 vec3 cone_dirs[MAX_CONE_DIRS] = vec3[](
402 vec3(0.0, 0.0, 1.0),
403 vec3(0.866025, 0.0, 0.5),
404 vec3(0.267617, 0.823639, 0.5),
405 vec3(-0.700629, 0.509037, 0.5),
406 vec3(-0.700629, -0.509037, 0.5),
407 vec3(0.267617, -0.823639, 0.5));
408
409 float cone_weights[MAX_CONE_DIRS] = float[](0.25, 0.15, 0.15, 0.15, 0.15, 0.15);
410 float tan_half_angle = 0.577;
411
412 for (int i = 0; i < MAX_CONE_DIRS; i++) {
413 vec3 direction = normal_mat * cone_dirs[i];
414 vec4 color = vec4(0.0);
415 {
416 float dist = 1.5;
417 float max_distance = length(vec3(params.limits));
418 vec3 cell_size = 1.0 / vec3(params.limits);
419
420 while (dist < max_distance && color.a < 0.95) {
421 float diameter = max(1.0, 2.0 * tan_half_angle * dist);
422 vec3 uvw_pos = (pos + dist * direction) * cell_size;
423 float half_diameter = diameter * 0.5;
424 //check if outside, then break
425 //if ( any(greaterThan(abs(uvw_pos - 0.5),vec3(0.5f + half_diameter * cell_size)) ) ) {
426 // break;
427 //}
428
429 float log2_diameter = log2(diameter);
430 vec4 scolor = textureLod(sampler3D(color_texture, texture_sampler), uvw_pos, log2_diameter);
431 float a = (1.0 - color.a);
432 color += a * scolor;
433 dist += half_diameter;
434 }
435 }
436 color *= cone_weights[i] * vec4(albedo.rgb, 1.0) * params.dynamic_range; //restore range
437 accum += color.rgb;
438 }
439 }
440
441 outputs.data[cell_index] = vec4(accum, 0.0);
442
443 #endif // MODE_SECOND_BOUNCE
444
445 /////////////////UPDATE MIPMAPS///////////////////////////////
446
447 #ifdef MODE_UPDATE_MIPMAPS
448
449 {
450 vec3 light_accum = vec3(0.0);
451 float count = 0.0;
452 for (uint i = 0; i < 8; i++) {
453 uint child_index = cell_children.data[cell_index].children[i];
454 if (child_index == NO_CHILDREN) {
455 continue;
456 }
457 light_accum += outputs.data[child_index].rgb;
458
459 count += 1.0;
460 }
461
462 float divisor = mix(8.0, count, params.propagation);
463 outputs.data[cell_index] = vec4(light_accum / divisor, 0.0);
464 }
465 #endif
466
467 ///////////////////WRITE TEXTURE/////////////////////////////
468
469 #ifdef MODE_WRITE_TEXTURE
470 {
471 imageStore(color_tex, ivec3(posu), vec4(outputs.data[cell_index].rgb / params.dynamic_range, albedo.a));
472 }
473 #endif
474
475 ///////////////////DYNAMIC LIGHTING/////////////////////////////
476
477 #ifdef MODE_DYNAMIC
478
479 ivec2 pos_xy = ivec2(gl_GlobalInvocationID.xy);
480 if (any(greaterThanEqual(pos_xy, params.rect_size))) {
481 return; //out of bounds
482 }
483
484 ivec2 uv_xy = pos_xy;
485 if (params.flip_x) {
486 uv_xy.x = params.rect_size.x - pos_xy.x - 1;
487 }
488 if (params.flip_y) {
489 uv_xy.y = params.rect_size.y - pos_xy.y - 1;
490 }
491
492 #ifdef MODE_DYNAMIC_LIGHTING
493
494 {
495 float z = params.z_base + imageLoad(depth, uv_xy).x * params.z_sign;
496
497 ivec3 pos = params.x_dir * (params.rect_pos.x + pos_xy.x) + params.y_dir * (params.rect_pos.y + pos_xy.y) + abs(params.z_dir) * int(z);
498
499 vec3 normal = normalize(imageLoad(source_normal, uv_xy).xyz * 2.0 - 1.0);
500 normal = vec3(params.x_dir) * normal.x * mix(1.0, -1.0, params.flip_x) + vec3(params.y_dir) * normal.y * mix(1.0, -1.0, params.flip_y) - vec3(params.z_dir) * normal.z;
501
502 vec4 albedo = imageLoad(source_albedo, uv_xy);
503
504 //determine the position in space
505
506 vec3 accum = vec3(0.0);
507 for (uint i = 0; i < params.light_count; i++) {
508 vec3 light;
509 vec3 light_dir;
510 if (!compute_light_at_pos(i, vec3(pos) * params.pos_multiplier, normal, light, light_dir)) {
511 continue;
512 }
513
514 light *= albedo.rgb;
515
516 accum += max(0.0, dot(normal, -light_dir)) * light;
517 }
518
519 accum += imageLoad(emission, uv_xy).xyz;
520
521 imageStore(emission, uv_xy, vec4(accum, albedo.a));
522 imageStore(depth, uv_xy, vec4(z));
523 }
524
525 #endif // MODE DYNAMIC LIGHTING
526
527 #ifdef MODE_DYNAMIC_SHRINK
528
529 {
530 vec4 accum = vec4(0.0);
531 float accum_z = 0.0;
532 float count = 0.0;
533
534 for (int i = 0; i < 4; i++) {
535 ivec2 ofs = pos_xy * 2 + ivec2(i & 1, i >> 1) - params.prev_rect_ofs;
536 if (any(lessThan(ofs, ivec2(0))) || any(greaterThanEqual(ofs, params.prev_rect_size))) {
537 continue;
538 }
539 if (params.flip_x) {
540 ofs.x = params.prev_rect_size.x - ofs.x - 1;
541 }
542 if (params.flip_y) {
543 ofs.y = params.prev_rect_size.y - ofs.y - 1;
544 }
545
546 vec4 light = imageLoad(source_light, ofs);
547 if (light.a == 0.0) { //ignore empty
548 continue;
549 }
550 accum += light;
551 float z = imageLoad(source_depth, ofs).x;
552 accum_z += z * 0.5; //shrink half too
553 count += 1.0;
554 }
555
556 if (params.on_mipmap) {
557 accum.rgb /= mix(8.0, count, params.propagation);
558 accum.a /= 8.0;
559 } else {
560 accum /= 4.0;
561 }
562
563 if (count == 0.0) {
564 accum_z = 0.0; //avoid nan
565 } else {
566 accum_z /= count;
567 }
568
569 #ifdef MODE_DYNAMIC_SHRINK_WRITE
570
571 imageStore(light, uv_xy, accum);
572 imageStore(depth, uv_xy, vec4(accum_z));
573 #endif
574
575 #ifdef MODE_DYNAMIC_SHRINK_PLOT
576
577 if (accum.a < 0.001) {
578 return; //do not blit if alpha is too low
579 }
580
581 ivec3 pos = params.x_dir * (params.rect_pos.x + pos_xy.x) + params.y_dir * (params.rect_pos.y + pos_xy.y) + abs(params.z_dir) * int(accum_z);
582
583 float z_frac = fract(accum_z);
584
585 for (int i = 0; i < 2; i++) {
586 ivec3 pos3d = pos + abs(params.z_dir) * i;
587 if (any(lessThan(pos3d, ivec3(0))) || any(greaterThanEqual(pos3d, params.limits))) {
588 //skip if offlimits
589 continue;
590 }
591 vec4 color_blit = accum * (i == 0 ? 1.0 - z_frac : z_frac);
592 vec4 color = imageLoad(color_texture, pos3d);
593 color.rgb *= params.dynamic_range;
594
595 #if 0
596 color.rgb = mix(color.rgb,color_blit.rgb,color_blit.a);
597 color.a+=color_blit.a;
598 #else
599
600 float sa = 1.0 - color_blit.a;
601 vec4 result;
602 result.a = color.a * sa + color_blit.a;
603 if (result.a == 0.0) {
604 result = vec4(0.0);
605 } else {
606 result.rgb = (color.rgb * color.a * sa + color_blit.rgb * color_blit.a) / result.a;
607 color = result;
608 }
609
610 #endif
611 color.rgb /= params.dynamic_range;
612 imageStore(color_texture, pos3d, color);
613 //imageStore(color_texture,pos3d,vec4(1,1,1,1));
614 }
615 #endif // MODE_DYNAMIC_SHRINK_PLOT
616 }
617 #endif
618
619 #endif // MODE DYNAMIC
620 }