Search across packages, versions, files, declarations, and comments. Use multiple terms to narrow results (AND), and add @vertex, @fragment, or @compute to filter by shader stage.
1//! Inputs a depth texture and outputs a MIP-chain of depths.
2//!
3//! Because SSAO's performance is bound by texture reads, this increases
4//! performance over using the full resolution depth for every sample.
5//!
6//! Reference: https://research.nvidia.com/sites/default/files/pubs/2012-06_Scalable-Ambient-Obscurance/McGuire12SAO.pdf, section 2.2
8import bevy_render::view::View;
10@group(0) @binding(0) var input_depth: texture_depth_2d;
11@if(USE_R16FLOAT) {
12@group(0) @binding(1) var preprocessed_depth_mip0: texture_storage_2d<r16float, write>;
13@group(0) @binding(2) var preprocessed_depth_mip1: texture_storage_2d<r16float, write>;
14@group(0) @binding(3) var preprocessed_depth_mip2: texture_storage_2d<r16float, write>;
15@group(0) @binding(4) var preprocessed_depth_mip3: texture_storage_2d<r16float, write>;
16@group(0) @binding(5) var preprocessed_depth_mip4: texture_storage_2d<r16float, write>;
18@if(!USE_R16FLOAT) {
19@group(0) @binding(1) var preprocessed_depth_mip0: texture_storage_2d<r32float, write>;
20@group(0) @binding(2) var preprocessed_depth_mip1: texture_storage_2d<r32float, write>;
21@group(0) @binding(3) var preprocessed_depth_mip2: texture_storage_2d<r32float, write>;
22@group(0) @binding(4) var preprocessed_depth_mip3: texture_storage_2d<r32float, write>;
23@group(0) @binding(5) var preprocessed_depth_mip4: texture_storage_2d<r32float, write>;
25@group(1) @binding(0) var point_clamp_sampler: sampler;
26@group(1) @binding(1) var linear_clamp_sampler: sampler;
27@group(1) @binding(2) var<uniform> view: View;
29/// Using 4 depths from the previous MIP, compute a weighted average for the depth of the current MIP
30fn weighted_average(depth0: f32, depth1: f32, depth2: f32, depth3: f32) -> f32 {
31 let depth_range_scale_factor = 0.75;
32 let effect_radius = depth_range_scale_factor * 0.5 * 1.457;
33 let falloff_range = 0.615 * effect_radius;
34 let falloff_from = effect_radius * (1.0 - 0.615);
35 let falloff_mul = -1.0 / falloff_range;
36 let falloff_add = falloff_from / falloff_range + 1.0;
38 let min_depth = min(min(depth0, depth1), min(depth2, depth3));
39 let weight0 = saturate((depth0 - min_depth) * falloff_mul + falloff_add);
40 let weight1 = saturate((depth1 - min_depth) * falloff_mul + falloff_add);
41 let weight2 = saturate((depth2 - min_depth) * falloff_mul + falloff_add);
42 let weight3 = saturate((depth3 - min_depth) * falloff_mul + falloff_add);
43 let weight_total = weight0 + weight1 + weight2 + weight3;
45 return ((weight0 * depth0) + (weight1 * depth1) + (weight2 * depth2) + (weight3 * depth3)) / weight_total;
48/// Used to share the depths from the previous MIP level between all invocations in a workgroup
49var<workgroup> previous_mip_depth: array<array<f32, 8>, 8>;
51@compute
52@workgroup_size(8, 8, 1)
53fn preprocess_depth(@builtin(global_invocation_id) global_id: vec3<u32>, @builtin(local_invocation_id) local_id: vec3<u32>) {
54 let base_coordinates = vec2<i32>(global_id.xy);
56 // MIP 0 - Copy 4 texels from the input depth (per invocation, 8x8 invocations per workgroup)
57 let pixel_coordinates0 = base_coordinates * 2i;
58 let pixel_coordinates1 = pixel_coordinates0 + vec2<i32>(1i, 0i);
59 let pixel_coordinates2 = pixel_coordinates0 + vec2<i32>(0i, 1i);
60 let pixel_coordinates3 = pixel_coordinates0 + vec2<i32>(1i, 1i);
61 let input_depth_size = vec2<f32>(textureDimensions(input_depth));
62 let depths_uv = (vec2<f32>(pixel_coordinates0) + view.viewport.xy) / input_depth_size;
63 let depths = textureGather(input_depth, point_clamp_sampler, depths_uv, vec2<i32>(1i, 1i));
64 textureStore(preprocessed_depth_mip0, pixel_coordinates0, vec4<f32>(depths.w, 0.0, 0.0, 0.0));
65 textureStore(preprocessed_depth_mip0, pixel_coordinates1, vec4<f32>(depths.z, 0.0, 0.0, 0.0));
66 textureStore(preprocessed_depth_mip0, pixel_coordinates2, vec4<f32>(depths.x, 0.0, 0.0, 0.0));
67 textureStore(preprocessed_depth_mip0, pixel_coordinates3, vec4<f32>(depths.y, 0.0, 0.0, 0.0));
69 // MIP 1 - Weighted average of MIP 0's depth values (per invocation, 8x8 invocations per workgroup)
70 let depth_mip1 = weighted_average(depths.w, depths.z, depths.x, depths.y);
71 textureStore(preprocessed_depth_mip1, base_coordinates, vec4<f32>(depth_mip1, 0.0, 0.0, 0.0));
72 previous_mip_depth[local_id.x][local_id.y] = depth_mip1;
74 workgroupBarrier();
76 // MIP 2 - Weighted average of MIP 1's depth values (per invocation, 4x4 invocations per workgroup)
77 if all(local_id.xy % vec2<u32>(2u) == vec2<u32>(0u)) {
78 let depth0 = previous_mip_depth[local_id.x + 0u][local_id.y + 0u];
79 let depth1 = previous_mip_depth[local_id.x + 1u][local_id.y + 0u];
80 let depth2 = previous_mip_depth[local_id.x + 0u][local_id.y + 1u];
81 let depth3 = previous_mip_depth[local_id.x + 1u][local_id.y + 1u];
82 let depth_mip2 = weighted_average(depth0, depth1, depth2, depth3);
83 textureStore(preprocessed_depth_mip2, base_coordinates / 2i, vec4<f32>(depth_mip2, 0.0, 0.0, 0.0));
84 previous_mip_depth[local_id.x][local_id.y] = depth_mip2;
85 }
87 workgroupBarrier();
89 // MIP 3 - Weighted average of MIP 2's depth values (per invocation, 2x2 invocations per workgroup)
90 if all(local_id.xy % vec2<u32>(4u) == vec2<u32>(0u)) {
91 let depth0 = previous_mip_depth[local_id.x + 0u][local_id.y + 0u];
92 let depth1 = previous_mip_depth[local_id.x + 2u][local_id.y + 0u];
93 let depth2 = previous_mip_depth[local_id.x + 0u][local_id.y + 2u];
94 let depth3 = previous_mip_depth[local_id.x + 2u][local_id.y + 2u];
95 let depth_mip3 = weighted_average(depth0, depth1, depth2, depth3);
96 textureStore(preprocessed_depth_mip3, base_coordinates / 4i, vec4<f32>(depth_mip3, 0.0, 0.0, 0.0));
97 previous_mip_depth[local_id.x][local_id.y] = depth_mip3;
98 }
100 workgroupBarrier();
102 // MIP 4 - Weighted average of MIP 3's depth values (per invocation, 1 invocation per workgroup)
103 if all(local_id.xy % vec2<u32>(8u) == vec2<u32>(0u)) {
104 let depth0 = previous_mip_depth[local_id.x + 0u][local_id.y + 0u];
105 let depth1 = previous_mip_depth[local_id.x + 4u][local_id.y + 0u];
106 let depth2 = previous_mip_depth[local_id.x + 0u][local_id.y + 4u];
107 let depth3 = previous_mip_depth[local_id.x + 4u][local_id.y + 4u];
108 let depth_mip4 = weighted_average(depth0, depth1, depth2, depth3);
109 textureStore(preprocessed_depth_mip4, base_coordinates / 8i, vec4<f32>(depth_mip4, 0.0, 0.0, 0.0));
110 }
Contents