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