Debian Codegraph @

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

raw
1 #[compute]
2
3 #version 450
4
5 #VERSION_DEFINES
6
7 #ifdef USE_VULKAN_MEMORY_MODEL
8 #pragma use_vulkan_memory_model
9 #endif
10
11 #ifdef MODE_DENSITY
12 layout(local_size_x = 4, local_size_y = 4, local_size_z = 4) in;
13 #else
14 layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
15 #endif
16
17 #include "../cluster_data_inc.glsl"
18 #include "../light_data_inc.glsl"
19 #include "../oct_inc.glsl"
20
21 #define M_PI 3.14159265359
22
23 #define DENSITY_SCALE 1024.0
24
25 layout(set = 0, binding = 1) uniform texture2D shadow_atlas;
26 layout(set = 0, binding = 2) uniform texture2D directional_shadow_atlas;
27
28 layout(set = 0, binding = 3, std430) restrict readonly buffer OmniLights {
29 LightData data[];
30 }
31 omni_lights;
32
33 layout(set = 0, binding = 4, std430) restrict readonly buffer SpotLights {
34 LightData data[];
35 }
36 spot_lights;
37
38 layout(set = 0, binding = 5, std140) uniform DirectionalLights {
39 DirectionalLightData data[MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS];
40 }
41 directional_lights;
42
43 layout(set = 0, binding = 6, std430) buffer restrict readonly ClusterBuffer {
44 uint data[];
45 }
46 cluster_buffer;
47
48 layout(set = 0, binding = 7) uniform sampler linear_sampler;
49
50 #ifdef MODE_DENSITY
51 layout(rgba16f, set = 0, binding = 8) uniform restrict writeonly image3D density_map;
52 #endif
53
54 #ifdef MODE_FOG
55 layout(rgba16f, set = 0, binding = 8) uniform restrict readonly image3D density_map;
56 layout(rgba16f, set = 0, binding = 9) uniform restrict writeonly image3D fog_map;
57 #endif
58
59 #ifdef MODE_COPY
60 layout(rgba16f, set = 0, binding = 8) uniform restrict readonly image3D source_map;
61 layout(rgba16f, set = 0, binding = 9) uniform restrict writeonly image3D dest_map;
62 #endif
63
64 #ifdef MODE_FILTER
65 layout(rgba16f, set = 0, binding = 8) uniform restrict readonly image3D source_map;
66 layout(rgba16f, set = 0, binding = 9) uniform restrict writeonly image3D dest_map;
67 #endif
68
69 layout(set = 0, binding = 10) uniform sampler shadow_sampler;
70
71 #define MAX_VOXEL_GI_INSTANCES 8
72
73 struct VoxelGIData {
74 mat4 xform; // 64 - 64
75
76 vec3 bounds; // 12 - 76
77 float dynamic_range; // 4 - 80
78
79 float bias; // 4 - 84
80 float normal_bias; // 4 - 88
81 bool blend_ambient; // 4 - 92
82 uint mipmaps; // 4 - 96
83
84 vec3 pad; // 12 - 108
85 float exposure_normalization; // 4 - 112
86 };
87
88 layout(set = 0, binding = 11, std140) uniform VoxelGIs {
89 VoxelGIData data[MAX_VOXEL_GI_INSTANCES];
90 }
91 voxel_gi_instances;
92
93 layout(set = 0, binding = 12) uniform texture3D voxel_gi_textures[MAX_VOXEL_GI_INSTANCES];
94
95 layout(set = 0, binding = 13) uniform sampler linear_sampler_with_mipmaps;
96
97 #ifdef ENABLE_SDFGI
98
99 // SDFGI Integration on set 1
100 #define SDFGI_MAX_CASCADES 8
101
102 struct SDFVoxelGICascadeData {
103 vec3 position;
104 float to_probe;
105 ivec3 probe_world_offset;
106 float to_cell; // 1/bounds * grid_size
107 vec3 pad;
108 float exposure_normalization;
109 };
110
111 layout(set = 1, binding = 0, std140) uniform SDFGI {
112 vec3 grid_size;
113 uint max_cascades;
114
115 bool use_occlusion;
116 int probe_axis_size;
117 float probe_to_uvw;
118 float normal_bias;
119
120 vec3 lightprobe_tex_pixel_size;
121 float energy;
122
123 vec3 lightprobe_uv_offset;
124 float y_mult;
125
126 vec3 occlusion_clamp;
127 uint pad3;
128
129 vec3 occlusion_renormalize;
130 uint pad4;
131
132 vec3 cascade_probe_size;
133 uint pad5;
134
135 SDFVoxelGICascadeData cascades[SDFGI_MAX_CASCADES];
136 }
137 sdfgi;
138
139 layout(set = 1, binding = 1) uniform texture2DArray sdfgi_ambient_texture;
140
141 layout(set = 1, binding = 2) uniform texture3D sdfgi_occlusion_texture;
142
143 #endif //SDFGI
144
145 layout(set = 0, binding = 14, std140) uniform Params {
146 vec2 fog_frustum_size_begin;
147 vec2 fog_frustum_size_end;
148
149 float fog_frustum_end;
150 float ambient_inject;
151 float z_far;
152 int filter_axis;
153
154 vec3 ambient_color;
155 float sky_contribution;
156
157 ivec3 fog_volume_size;
158 uint directional_light_count;
159
160 vec3 base_emission;
161 float base_density;
162
163 vec3 base_scattering;
164 float phase_g;
165
166 float detail_spread;
167 float gi_inject;
168 uint max_voxel_gi_instances;
169 uint cluster_type_size;
170
171 vec2 screen_size;
172 uint cluster_shift;
173 uint cluster_width;
174
175 uint max_cluster_element_count_div_32;
176 bool use_temporal_reprojection;
177 uint temporal_frame;
178 float temporal_blend;
179
180 vec2 sky_border_size;
181 vec2 pad;
182
183 mat3x4 cam_rotation;
184 mat4 to_prev_view;
185
186 mat3 radiance_inverse_xform;
187 }
188 params;
189 #ifndef MODE_COPY
190 layout(set = 0, binding = 15) uniform texture3D prev_density_texture;
191
192 #ifdef NO_IMAGE_ATOMICS
193 layout(set = 0, binding = 16) buffer density_only_map_buffer {
194 uint density_only_map[];
195 };
196 layout(set = 0, binding = 17) buffer light_only_map_buffer {
197 uint light_only_map[];
198 };
199 layout(set = 0, binding = 18) buffer emissive_only_map_buffer {
200 uint emissive_only_map[];
201 };
202 #else
203 layout(r32ui, set = 0, binding = 16) uniform uimage3D density_only_map;
204 layout(r32ui, set = 0, binding = 17) uniform uimage3D light_only_map;
205 layout(r32ui, set = 0, binding = 18) uniform uimage3D emissive_only_map;
206 #endif
207
208 #ifdef USE_RADIANCE_OCTMAP_ARRAY
209 layout(set = 0, binding = 19) uniform texture2DArray sky_texture;
210 #else
211 layout(set = 0, binding = 19) uniform texture2D sky_texture;
212 #endif
213 #endif // MODE_COPY
214
215 float get_depth_at_pos(float cell_depth_size, int z) {
216 float d = float(z) * cell_depth_size + cell_depth_size * 0.5; //center of voxels
217 d = pow(d, params.detail_spread);
218 return params.fog_frustum_end * d;
219 }
220
221 vec3 hash3f(uvec3 x) {
222 x = ((x >> 16) ^ x) * 0x45d9f3b;
223 x = ((x >> 16) ^ x) * 0x45d9f3b;
224 x = (x >> 16) ^ x;
225 return vec3(x & 0xFFFFF) / vec3(float(0xFFFFF));
226 }
227
228 float get_omni_attenuation(float dist, float inv_range, float decay) {
229 float nd = dist * inv_range;
230 nd *= nd;
231 nd *= nd; // nd^4
232 nd = max(1.0 - nd, 0.0);
233 nd *= nd; // nd^2
234 return nd * pow(max(dist, 0.0001), -decay);
235 }
236
237 void cluster_get_item_range(uint p_offset, out uint item_min, out uint item_max, out uint item_from, out uint item_to) {
238 uint item_min_max = cluster_buffer.data[p_offset];
239 item_min = item_min_max & 0xFFFF;
240 item_max = item_min_max >> 16;
241
242 item_from = item_min >> 5;
243 item_to = (item_max == 0) ? 0 : ((item_max - 1) >> 5) + 1; //side effect of how it is stored, as item_max 0 means no elements
244 }
245
246 uint cluster_get_range_clip_mask(uint i, uint z_min, uint z_max) {
247 int local_min = clamp(int(z_min) - int(i) * 32, 0, 31);
248 int mask_width = min(int(z_max) - int(z_min), 32 - local_min);
249 return bitfieldInsert(uint(0), uint(0xFFFFFFFF), local_min, mask_width);
250 }
251
252 float henyey_greenstein(float cos_theta, float g) {
253 const float k = 0.0795774715459; // 1 / (4 * PI)
254 return k * (1.0 - g * g) / (pow(1.0 + g * g - 2.0 * g * cos_theta, 1.5));
255 }
256
257 #define TEMPORAL_FRAMES 16
258
259 const vec3 halton_map[TEMPORAL_FRAMES] = vec3[](
260 vec3(0.5, 0.33333333, 0.2),
261 vec3(0.25, 0.66666667, 0.4),
262 vec3(0.75, 0.11111111, 0.6),
263 vec3(0.125, 0.44444444, 0.8),
264 vec3(0.625, 0.77777778, 0.04),
265 vec3(0.375, 0.22222222, 0.24),
266 vec3(0.875, 0.55555556, 0.44),
267 vec3(0.0625, 0.88888889, 0.64),
268 vec3(0.5625, 0.03703704, 0.84),
269 vec3(0.3125, 0.37037037, 0.08),
270 vec3(0.8125, 0.7037037, 0.28),
271 vec3(0.1875, 0.14814815, 0.48),
272 vec3(0.6875, 0.48148148, 0.68),
273 vec3(0.4375, 0.81481481, 0.88),
274 vec3(0.9375, 0.25925926, 0.12),
275 vec3(0.03125, 0.59259259, 0.32));
276
277 // Higher values will make light in volumetric fog fade out sooner when it's occluded by shadow.
278 const float INV_FOG_FADE = 10.0;
279
280 void main() {
281 vec3 fog_cell_size = 1.0 / vec3(params.fog_volume_size);
282
283 #ifdef MODE_DENSITY
284
285 ivec3 pos = ivec3(gl_GlobalInvocationID.xyz);
286 if (any(greaterThanEqual(pos, params.fog_volume_size))) {
287 return; //do not compute
288 }
289 #ifdef NO_IMAGE_ATOMICS
290 uint lpos = pos.z * params.fog_volume_size.x * params.fog_volume_size.y + pos.y * params.fog_volume_size.x + pos.x;
291 #endif
292
293 vec3 posf = vec3(pos);
294
295 //posf += mix(vec3(0.0),vec3(1.0),0.3) * hash3f(uvec3(pos)) * 2.0 - 1.0;
296
297 vec3 fog_unit_pos = posf * fog_cell_size + fog_cell_size * 0.5; //center of voxels
298
299 uvec2 screen_pos = uvec2(fog_unit_pos.xy * params.screen_size);
300 uvec2 cluster_pos = screen_pos >> params.cluster_shift;
301 uint cluster_offset = (params.cluster_width * cluster_pos.y + cluster_pos.x) * (params.max_cluster_element_count_div_32 + 32);
302 //positions in screen are too spread apart, no hopes for optimizing with subgroups
303
304 fog_unit_pos.z = pow(fog_unit_pos.z, params.detail_spread);
305
306 vec3 view_pos;
307 view_pos.xy = (fog_unit_pos.xy * 2.0 - 1.0) * mix(params.fog_frustum_size_begin, params.fog_frustum_size_end, vec2(fog_unit_pos.z));
308 view_pos.z = -params.fog_frustum_end * fog_unit_pos.z;
309 view_pos.y = -view_pos.y;
310
311 vec4 reprojected_density = vec4(0.0);
312 float reproject_amount = 0.0;
313
314 if (params.use_temporal_reprojection) {
315 vec3 prev_view = (params.to_prev_view * vec4(view_pos, 1.0)).xyz;
316 //undo transform into prev view
317 prev_view.y = -prev_view.y;
318 //z back to unit size
319 prev_view.z /= -params.fog_frustum_end;
320 //xy back to unit size
321 prev_view.xy /= mix(params.fog_frustum_size_begin, params.fog_frustum_size_end, vec2(prev_view.z));
322 prev_view.xy = prev_view.xy * 0.5 + 0.5;
323 //z back to unspread value
324 prev_view.z = pow(prev_view.z, 1.0 / params.detail_spread);
325
326 if (all(greaterThan(prev_view, vec3(0.0))) && all(lessThan(prev_view, vec3(1.0)))) {
327 //reprojectinon fits
328
329 reprojected_density = textureLod(sampler3D(prev_density_texture, linear_sampler), prev_view, 0.0);
330 reproject_amount = params.temporal_blend;
331
332 // Since we can reproject, now we must jitter the current view pos.
333 // This is done here because cells that can't reproject should not jitter.
334
335 fog_unit_pos = posf * fog_cell_size + fog_cell_size * halton_map[params.temporal_frame]; //center of voxels, offset by halton table
336
337 screen_pos = uvec2(fog_unit_pos.xy * params.screen_size);
338 cluster_pos = screen_pos >> params.cluster_shift;
339 cluster_offset = (params.cluster_width * cluster_pos.y + cluster_pos.x) * (params.max_cluster_element_count_div_32 + 32);
340 //positions in screen are too spread apart, no hopes for optimizing with subgroups
341
342 fog_unit_pos.z = pow(fog_unit_pos.z, params.detail_spread);
343
344 view_pos.xy = (fog_unit_pos.xy * 2.0 - 1.0) * mix(params.fog_frustum_size_begin, params.fog_frustum_size_end, vec2(fog_unit_pos.z));
345 view_pos.z = -params.fog_frustum_end * fog_unit_pos.z;
346 view_pos.y = -view_pos.y;
347 }
348 }
349
350 uint cluster_z = uint(clamp((abs(view_pos.z) / params.z_far) * 32.0, 0.0, 31.0));
351
352 vec3 total_light = vec3(0.0);
353
354 float total_density = params.base_density;
355 #ifdef NO_IMAGE_ATOMICS
356 uint local_density = density_only_map[lpos];
357 #else
358 uint local_density = imageLoad(density_only_map, pos).x;
359 #endif
360
361 total_density += float(int(local_density)) / DENSITY_SCALE;
362 total_density = max(0.0, total_density);
363
364 #ifdef NO_IMAGE_ATOMICS
365 uint scattering_u = light_only_map[lpos];
366 #else
367 uint scattering_u = imageLoad(light_only_map, pos).x;
368 #endif
369 vec3 scattering = vec3(scattering_u >> 21, (scattering_u << 11) >> 21, scattering_u % 1024) / vec3(2047.0, 2047.0, 1023.0);
370 scattering += params.base_scattering * params.base_density;
371
372 #ifdef NO_IMAGE_ATOMICS
373 uint emission_u = emissive_only_map[lpos];
374 #else
375 uint emission_u = imageLoad(emissive_only_map, pos).x;
376 #endif
377 vec3 emission = vec3(emission_u >> 21, (emission_u << 11) >> 21, emission_u % 1024) / vec3(511.0, 511.0, 255.0);
378 emission += params.base_emission * params.base_density;
379
380 float cell_depth_size = abs(view_pos.z - get_depth_at_pos(fog_cell_size.z, pos.z + 1));
381 //compute directional lights
382
383 if (total_density > 0.00005) {
384 for (uint i = 0; i < params.directional_light_count; i++) {
385 if (directional_lights.data[i].volumetric_fog_energy > 0.001) {
386 vec3 shadow_attenuation = vec3(1.0);
387
388 if (directional_lights.data[i].shadow_opacity > 0.001) {
389 float depth_z = -view_pos.z;
390
391 vec4 pssm_coord;
392 vec3 light_dir = directional_lights.data[i].direction;
393 vec4 v = vec4(view_pos, 1.0);
394 float z_range;
395
396 if (depth_z < directional_lights.data[i].shadow_split_offsets.x) {
397 pssm_coord = (directional_lights.data[i].shadow_matrix1 * v);
398 pssm_coord /= pssm_coord.w;
399 z_range = directional_lights.data[i].shadow_z_range.x;
400
401 } else if (depth_z < directional_lights.data[i].shadow_split_offsets.y) {
402 pssm_coord = (directional_lights.data[i].shadow_matrix2 * v);
403 pssm_coord /= pssm_coord.w;
404 z_range = directional_lights.data[i].shadow_z_range.y;
405
406 } else if (depth_z < directional_lights.data[i].shadow_split_offsets.z) {
407 pssm_coord = (directional_lights.data[i].shadow_matrix3 * v);
408 pssm_coord /= pssm_coord.w;
409 z_range = directional_lights.data[i].shadow_z_range.z;
410
411 } else {
412 pssm_coord = (directional_lights.data[i].shadow_matrix4 * v);
413 pssm_coord /= pssm_coord.w;
414 z_range = directional_lights.data[i].shadow_z_range.w;
415 }
416
417 float depth = texture(sampler2D(directional_shadow_atlas, linear_sampler), pssm_coord.xy).r;
418 float shadow = exp(min(0.0, (pssm_coord.z - depth)) * z_range * INV_FOG_FADE);
419
420 shadow = mix(shadow, 1.0, smoothstep(directional_lights.data[i].fade_from, directional_lights.data[i].fade_to, view_pos.z)); //done with negative values for performance
421
422 shadow_attenuation = mix(vec3(1.0 - directional_lights.data[i].shadow_opacity), vec3(1.0), shadow);
423 }
424
425 total_light += shadow_attenuation * directional_lights.data[i].color * directional_lights.data[i].energy * henyey_greenstein(dot(normalize(view_pos), normalize(directional_lights.data[i].direction)), params.phase_g) * directional_lights.data[i].volumetric_fog_energy;
426 }
427 }
428
429 // Compute light from sky
430 if (params.ambient_inject > 0.0) {
431 vec3 isotropic = vec3(0.0);
432 vec3 anisotropic = vec3(0.0);
433 if (params.sky_contribution > 0.0) {
434 float mip_bias = 2.0 + total_density * (MAX_SKY_LOD - 2.0); // Not physically based, but looks nice
435 vec3 scatter_direction = (params.radiance_inverse_xform * normalize(view_pos)) * sign(params.phase_g);
436 #ifdef USE_RADIANCE_OCTMAP_ARRAY
437 isotropic = texture(sampler2DArray(sky_texture, linear_sampler_with_mipmaps), vec3(vec3_to_oct_with_border(vec3(0.0, 1.0, 0.0), params.sky_border_size), mip_bias)).rgb;
438 anisotropic = texture(sampler2DArray(sky_texture, linear_sampler_with_mipmaps), vec3(vec3_to_oct_with_border(scatter_direction, params.sky_border_size), mip_bias)).rgb;
439 #else
440 isotropic = textureLod(sampler2D(sky_texture, linear_sampler_with_mipmaps), vec3_to_oct_with_border(vec3(0.0, 1.0, 0.0), params.sky_border_size), mip_bias).rgb;
441 anisotropic = textureLod(sampler2D(sky_texture, linear_sampler_with_mipmaps), vec3_to_oct_with_border(scatter_direction, params.sky_border_size), mip_bias).rgb;
442 #endif //USE_RADIANCE_OCTMAP_ARRAY
443 }
444
445 total_light += mix(params.ambient_color, mix(isotropic, anisotropic, abs(params.phase_g)), params.sky_contribution) * params.ambient_inject;
446 }
447
448 //compute lights from cluster
449
450 { //omni lights
451
452 uint cluster_omni_offset = cluster_offset;
453
454 uint item_min;
455 uint item_max;
456 uint item_from;
457 uint item_to;
458
459 cluster_get_item_range(cluster_omni_offset + params.max_cluster_element_count_div_32 + cluster_z, item_min, item_max, item_from, item_to);
460
461 for (uint i = item_from; i < item_to; i++) {
462 uint mask = cluster_buffer.data[cluster_omni_offset + i];
463 mask &= cluster_get_range_clip_mask(i, item_min, item_max);
464 uint merged_mask = mask;
465
466 while (merged_mask != 0) {
467 uint bit = findMSB(merged_mask);
468 merged_mask &= ~(1 << bit);
469
470 uint light_index = 32 * i + bit;
471
472 //if (!bool(omni_omni_lights.data[light_index].mask & draw_call.layer_mask)) {
473 // continue; //not masked
474 //}
475
476 vec3 light_pos = omni_lights.data[light_index].position;
477 float d = distance(omni_lights.data[light_index].position, view_pos);
478 float shadow_attenuation = 1.0;
479
480 if (omni_lights.data[light_index].volumetric_fog_energy > 0.001 && d * omni_lights.data[light_index].inv_radius < 1.0) {
481 float attenuation = get_omni_attenuation(d, omni_lights.data[light_index].inv_radius, omni_lights.data[light_index].attenuation);
482
483 vec3 light = omni_lights.data[light_index].color;
484
485 if (omni_lights.data[light_index].shadow_opacity > 0.001) {
486 //has shadow
487 vec4 uv_rect = omni_lights.data[light_index].atlas_rect;
488 vec2 flip_offset = omni_lights.data[light_index].direction.xy;
489
490 vec3 local_vert = (omni_lights.data[light_index].shadow_matrix * vec4(view_pos, 1.0)).xyz;
491
492 float shadow_len = length(local_vert); //need to remember shadow len from here
493 vec3 shadow_sample = normalize(local_vert);
494
495 if (shadow_sample.z >= 0.0) {
496 uv_rect.xy += flip_offset;
497 }
498
499 shadow_sample.z = 1.0 + abs(shadow_sample.z);
500 vec3 pos = vec3(shadow_sample.xy / shadow_sample.z, shadow_len - omni_lights.data[light_index].shadow_bias);
501 pos.z *= omni_lights.data[light_index].inv_radius;
502 pos.z = 1.0 - pos.z;
503
504 pos.xy = pos.xy * 0.5 + 0.5;
505 pos.xy = uv_rect.xy + pos.xy * uv_rect.zw;
506
507 float depth = texture(sampler2D(shadow_atlas, linear_sampler), pos.xy).r;
508
509 shadow_attenuation = mix(1.0 - omni_lights.data[light_index].shadow_opacity, 1.0, exp(min(0.0, (pos.z - depth)) / omni_lights.data[light_index].inv_radius * INV_FOG_FADE));
510 }
511 total_light += light * attenuation * shadow_attenuation * henyey_greenstein(dot(normalize(light_pos - view_pos), normalize(view_pos)), params.phase_g) * omni_lights.data[light_index].volumetric_fog_energy;
512 }
513 }
514 }
515 }
516
517 { //spot lights
518
519 uint cluster_spot_offset = cluster_offset + params.cluster_type_size;
520
521 uint item_min;
522 uint item_max;
523 uint item_from;
524 uint item_to;
525
526 cluster_get_item_range(cluster_spot_offset + params.max_cluster_element_count_div_32 + cluster_z, item_min, item_max, item_from, item_to);
527
528 for (uint i = item_from; i < item_to; i++) {
529 uint mask = cluster_buffer.data[cluster_spot_offset + i];
530 mask &= cluster_get_range_clip_mask(i, item_min, item_max);
531 uint merged_mask = mask;
532
533 while (merged_mask != 0) {
534 uint bit = findMSB(merged_mask);
535 merged_mask &= ~(1 << bit);
536
537 //if (!bool(omni_lights.data[light_index].mask & draw_call.layer_mask)) {
538 // continue; //not masked
539 //}
540
541 uint light_index = 32 * i + bit;
542
543 vec3 light_pos = spot_lights.data[light_index].position;
544 vec3 light_rel_vec = spot_lights.data[light_index].position - view_pos;
545 float d = length(light_rel_vec);
546 float shadow_attenuation = 1.0;
547
548 if (spot_lights.data[light_index].volumetric_fog_energy > 0.001 && d * spot_lights.data[light_index].inv_radius < 1.0) {
549 float attenuation = get_omni_attenuation(d, spot_lights.data[light_index].inv_radius, spot_lights.data[light_index].attenuation);
550
551 vec3 spot_dir = spot_lights.data[light_index].direction;
552 float cone_angle = spot_lights.data[light_index].cone_angle;
553 float scos = max(dot(-normalize(light_rel_vec), spot_dir), cone_angle);
554 float spot_rim = max(0.0001, (1.0 - scos) / (1.0 - cone_angle));
555 attenuation *= 1.0 - pow(spot_rim, spot_lights.data[light_index].cone_attenuation);
556
557 vec3 light = spot_lights.data[light_index].color;
558
559 if (spot_lights.data[light_index].shadow_opacity > 0.001) {
560 //has shadow
561 vec4 uv_rect = spot_lights.data[light_index].atlas_rect;
562
563 vec4 v = vec4(view_pos, 1.0);
564
565 vec4 splane = (spot_lights.data[light_index].shadow_matrix * v);
566 splane.z -= spot_lights.data[light_index].shadow_bias;
567 splane /= splane.w;
568
569 vec3 pos = vec3(splane.xy * spot_lights.data[light_index].atlas_rect.zw + spot_lights.data[light_index].atlas_rect.xy, splane.z);
570
571 float depth = texture(sampler2D(shadow_atlas, linear_sampler), pos.xy).r;
572
573 shadow_attenuation = mix(1.0 - spot_lights.data[light_index].shadow_opacity, 1.0, exp(min(0.0, (pos.z - depth)) / spot_lights.data[light_index].inv_radius * INV_FOG_FADE));
574 }
575 total_light += light * attenuation * shadow_attenuation * henyey_greenstein(dot(normalize(light_rel_vec), normalize(view_pos)), params.phase_g) * spot_lights.data[light_index].volumetric_fog_energy;
576 }
577 }
578 }
579 }
580
581 vec3 world_pos = mat3(params.cam_rotation) * view_pos;
582
583 for (uint i = 0; i < params.max_voxel_gi_instances; i++) {
584 vec3 position = (voxel_gi_instances.data[i].xform * vec4(world_pos, 1.0)).xyz;
585
586 //this causes corrupted pixels, i have no idea why..
587 if (all(bvec2(all(greaterThanEqual(position, vec3(0.0))), all(lessThan(position, voxel_gi_instances.data[i].bounds))))) {
588 position /= voxel_gi_instances.data[i].bounds;
589
590 vec4 light = vec4(0.0);
591 for (uint j = 0; j < voxel_gi_instances.data[i].mipmaps; j++) {
592 vec4 slight = textureLod(sampler3D(voxel_gi_textures[i], linear_sampler_with_mipmaps), position, float(j));
593 float a = (1.0 - light.a);
594 light += a * slight;
595 }
596
597 light.rgb *= voxel_gi_instances.data[i].dynamic_range * params.gi_inject * voxel_gi_instances.data[i].exposure_normalization;
598
599 total_light += light.rgb;
600 }
601 }
602
603 //sdfgi
604 #ifdef ENABLE_SDFGI
605
606 {
607 float blend = -1.0;
608 vec3 ambient_total = vec3(0.0);
609
610 for (uint i = 0; i < sdfgi.max_cascades; i++) {
611 vec3 cascade_pos = (world_pos - sdfgi.cascades[i].position) * sdfgi.cascades[i].to_probe;
612
613 if (any(lessThan(cascade_pos, vec3(0.0))) || any(greaterThanEqual(cascade_pos, sdfgi.cascade_probe_size))) {
614 continue; //skip cascade
615 }
616
617 vec3 base_pos = floor(cascade_pos);
618 ivec3 probe_base_pos = ivec3(base_pos);
619
620 vec4 ambient_accum = vec4(0.0);
621
622 ivec3 tex_pos = ivec3(probe_base_pos.xy, int(i));
623 tex_pos.x += probe_base_pos.z * sdfgi.probe_axis_size;
624
625 for (uint j = 0; j < 8; j++) {
626 ivec3 offset = (ivec3(j) >> ivec3(0, 1, 2)) & ivec3(1, 1, 1);
627 ivec3 probe_posi = probe_base_pos;
628 probe_posi += offset;
629
630 // Compute weight
631
632 vec3 probe_pos = vec3(probe_posi);
633 vec3 probe_to_pos = cascade_pos - probe_pos;
634
635 vec3 trilinear = vec3(1.0) - abs(probe_to_pos);
636 float weight = trilinear.x * trilinear.y * trilinear.z;
637
638 // Compute lightprobe occlusion
639
640 if (sdfgi.use_occlusion) {
641 ivec3 occ_indexv = abs((sdfgi.cascades[i].probe_world_offset + probe_posi) & ivec3(1, 1, 1)) * ivec3(1, 2, 4);
642 vec4 occ_mask = mix(vec4(0.0), vec4(1.0), equal(ivec4(occ_indexv.x | occ_indexv.y), ivec4(0, 1, 2, 3)));
643
644 vec3 occ_pos = clamp(cascade_pos, probe_pos - sdfgi.occlusion_clamp, probe_pos + sdfgi.occlusion_clamp) * sdfgi.probe_to_uvw;
645 occ_pos.z += float(i);
646 if (occ_indexv.z != 0) { //z bit is on, means index is >=4, so make it switch to the other half of textures
647 occ_pos.x += 1.0;
648 }
649
650 occ_pos *= sdfgi.occlusion_renormalize;
651 float occlusion = dot(textureLod(sampler3D(sdfgi_occlusion_texture, linear_sampler), occ_pos, 0.0), occ_mask);
652
653 weight *= max(occlusion, 0.01);
654 }
655
656 // Compute ambient texture position
657
658 ivec3 uvw = tex_pos;
659 uvw.xy += offset.xy;
660 uvw.x += offset.z * sdfgi.probe_axis_size;
661
662 vec3 ambient = texelFetch(sampler2DArray(sdfgi_ambient_texture, linear_sampler), uvw, 0).rgb;
663
664 ambient_accum.rgb += ambient * weight * sdfgi.cascades[i].exposure_normalization;
665 ambient_accum.a += weight;
666 }
667
668 if (ambient_accum.a > 0) {
669 ambient_accum.rgb /= ambient_accum.a;
670 }
671 ambient_total = ambient_accum.rgb;
672 break;
673 }
674
675 total_light += ambient_total * params.gi_inject;
676 }
677
678 #endif
679 }
680
681 vec4 final_density = vec4(total_light * scattering + emission, total_density);
682
683 final_density = mix(final_density, reprojected_density, reproject_amount);
684
685 imageStore(density_map, pos, final_density);
686 #ifdef NO_IMAGE_ATOMICS
687 density_only_map[lpos] = 0;
688 light_only_map[lpos] = 0;
689 emissive_only_map[lpos] = 0;
690 #else
691 imageStore(density_only_map, pos, uvec4(0));
692 imageStore(light_only_map, pos, uvec4(0));
693 imageStore(emissive_only_map, pos, uvec4(0));
694 #endif
695 #endif
696
697 #ifdef MODE_FOG
698
699 ivec3 pos = ivec3(gl_GlobalInvocationID.xy, 0);
700
701 if (any(greaterThanEqual(pos, params.fog_volume_size))) {
702 return; //do not compute
703 }
704
705 vec4 fog_accum = vec4(0.0, 0.0, 0.0, 1.0);
706 float prev_z = 0.0;
707
708 for (int i = 0; i < params.fog_volume_size.z; i++) {
709 //compute fog position
710 ivec3 fog_pos = pos + ivec3(0, 0, i);
711 //get fog value
712 vec4 fog = imageLoad(density_map, fog_pos);
713
714 //get depth at cell pos
715 float z = get_depth_at_pos(fog_cell_size.z, i);
716 //get distance from previous pos
717 float d = abs(prev_z - z);
718 //compute transmittance using beer's law
719 float transmittance = exp(-d * fog.a);
720
721 fog_accum.rgb += ((fog.rgb - fog.rgb * transmittance) / max(fog.a, 0.00001)) * fog_accum.a;
722 fog_accum.a *= transmittance;
723
724 prev_z = z;
725
726 imageStore(fog_map, fog_pos, vec4(fog_accum.rgb, fog_accum.a));
727 }
728
729 #endif
730
731 #ifdef MODE_FILTER
732
733 ivec3 pos = ivec3(gl_GlobalInvocationID.xyz);
734
735 const float gauss[7] = float[](0.071303, 0.131514, 0.189879, 0.214607, 0.189879, 0.131514, 0.071303);
736
737 const ivec3 filter_dir[3] = ivec3[](ivec3(1, 0, 0), ivec3(0, 1, 0), ivec3(0, 0, 1));
738 ivec3 offset = filter_dir[params.filter_axis];
739
740 vec4 accum = vec4(0.0);
741 for (int i = -3; i <= 3; i++) {
742 accum += imageLoad(source_map, clamp(pos + offset * i, ivec3(0), params.fog_volume_size - ivec3(1))) * gauss[i + 3];
743 }
744
745 imageStore(dest_map, pos, accum);
746
747 #endif
748 #ifdef MODE_COPY
749 ivec3 pos = ivec3(gl_GlobalInvocationID.xyz);
750 if (any(greaterThanEqual(pos, params.fog_volume_size))) {
751 return; //do not compute
752 }
753
754 imageStore(dest_map, pos, imageLoad(source_map, pos));
755
756 #endif
757 }