12#import bevy_render::view::View 14@group(0) @binding(0) var ambient_occlusion_noisy: texture_2d<f32>; 15@group(0) @binding(1) var depth_differences: texture_2d<u32>; 16@group(0) @binding(2) var ambient_occlusion: texture_storage_2d<r16float, write>; 17@group(1) @binding(0) var point_clamp_sampler: sampler; 18@group(1) @binding(1) var linear_clamp_sampler: sampler; 19@group(1) @binding(2) var<uniform> view: View; 22@workgroup_size(8, 8, 1) 23fn spatial_denoise(@builtin(global_invocation_id) global_id: vec3<u32>) { 24 let pixel_coordinates = vec2<i32>(global_id.xy); 25 let uv = vec2<f32>(pixel_coordinates) / view.viewport.zw; 27 let edges0 = textureGather(0, depth_differences, point_clamp_sampler, uv); 28 let edges1 = textureGather(0, depth_differences, point_clamp_sampler, uv, vec2<i32>(2i, 0i)); 29 let edges2 = textureGather(0, depth_differences, point_clamp_sampler, uv, vec2<i32>(1i, 2i)); 30 let visibility0 = textureGather(0, ambient_occlusion_noisy, point_clamp_sampler, uv); 31 let visibility1 = textureGather(0, ambient_occlusion_noisy, point_clamp_sampler, uv, vec2<i32>(2i, 0i)); 32 let visibility2 = textureGather(0, ambient_occlusion_noisy, point_clamp_sampler, uv, vec2<i32>(0i, 2i)); 33 let visibility3 = textureGather(0, ambient_occlusion_noisy, point_clamp_sampler, uv, vec2<i32>(2i, 2i)); 35 let left_edges = unpack4x8unorm(edges0.x); 36 let right_edges = unpack4x8unorm(edges1.x); 37 let top_edges = unpack4x8unorm(edges0.z); 38 let bottom_edges = unpack4x8unorm(edges2.w); 39 var center_edges = unpack4x8unorm(edges0.y); 40 center_edges *= vec4<f32>(left_edges.y, right_edges.x, top_edges.w, bottom_edges.z); 42 let center_weight = 1.2; 43 let left_weight = center_edges.x; 44 let right_weight = center_edges.y; 45 let top_weight = center_edges.z; 46 let bottom_weight = center_edges.w; 47 let top_left_weight = 0.425 * (top_weight * top_edges.x + left_weight * left_edges.z); 48 let top_right_weight = 0.425 * (top_weight * top_edges.y + right_weight * right_edges.z); 49 let bottom_left_weight = 0.425 * (bottom_weight * bottom_edges.x + left_weight * left_edges.w); 50 let bottom_right_weight = 0.425 * (bottom_weight * bottom_edges.y + right_weight * right_edges.w); 52 let center_visibility = visibility0.y; 53 let left_visibility = visibility0.x; 54 let right_visibility = visibility0.z; 55 let top_visibility = visibility1.x; 56 let bottom_visibility = visibility2.z; 57 let top_left_visibility = visibility0.w; 58 let top_right_visibility = visibility1.w; 59 let bottom_left_visibility = visibility2.w; 60 let bottom_right_visibility = visibility3.w; 62 var sum = center_visibility; 63 sum += left_visibility * left_weight; 64 sum += right_visibility * right_weight; 65 sum += top_visibility * top_weight; 66 sum += bottom_visibility * bottom_weight; 67 sum += top_left_visibility * top_left_weight; 68 sum += top_right_visibility * top_right_weight; 69 sum += bottom_left_visibility * bottom_left_weight; 70 sum += bottom_right_visibility * bottom_right_weight; 72 var sum_weight = center_weight; 73 sum_weight += left_weight; 74 sum_weight += right_weight; 75 sum_weight += top_weight; 76 sum_weight += bottom_weight; 77 sum_weight += top_left_weight; 78 sum_weight += top_right_weight; 79 sum_weight += bottom_left_weight; 80 sum_weight += bottom_right_weight; 82 let denoised_visibility = sum / sum_weight; 84 textureStore(ambient_occlusion, pixel_coordinates, vec4<f32>(denoised_visibility, 0.0, 0.0, 0.0));