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// 3x3 bilaterial filter (edge-preserving blur)
2// https://people.csail.mit.edu/sparis/bf_course/course_notes.pdf
4// Note: Does not use the Gaussian kernel part of a typical bilateral blur
5// From the paper: "use the information gathered on a neighborhood of 4 × 4 using a bilateral filter for
6// reconstruction, using _uniform_ convolution weights"
8// Note: The paper does a 4x4 (not quite centered) filter, offset by +/- 1 pixel every other frame
9// XeGTAO does a 3x3 filter, on two pixels at a time per compute thread, applied twice
10// We do a 3x3 filter, on 1 pixel per compute thread, applied once
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#ifdef USE_R16FLOAT
17@group(0) @binding(2) var ambient_occlusion: texture_storage_2d<r16float, write>;
18#else
19@group(0) @binding(2) var ambient_occlusion: texture_storage_2d<r32float, write>;
20#endif
21@group(1) @binding(0) var point_clamp_sampler: sampler;
22@group(1) @binding(1) var linear_clamp_sampler: sampler;
23@group(1) @binding(2) var<uniform> view: View;
25@compute
26@workgroup_size(8, 8, 1)
27fn spatial_denoise(@builtin(global_invocation_id) global_id: vec3<u32>) {
28 let pixel_coordinates = vec2<i32>(global_id.xy);
29 let uv = vec2<f32>(pixel_coordinates) / view.viewport.zw;
31 let edges0 = textureGather(0, depth_differences, point_clamp_sampler, uv);
32 let edges1 = textureGather(0, depth_differences, point_clamp_sampler, uv, vec2<i32>(2i, 0i));
33 let edges2 = textureGather(0, depth_differences, point_clamp_sampler, uv, vec2<i32>(1i, 2i));
34 let visibility0 = textureGather(0, ambient_occlusion_noisy, point_clamp_sampler, uv);
35 let visibility1 = textureGather(0, ambient_occlusion_noisy, point_clamp_sampler, uv, vec2<i32>(2i, 0i));
36 let visibility2 = textureGather(0, ambient_occlusion_noisy, point_clamp_sampler, uv, vec2<i32>(0i, 2i));
37 let visibility3 = textureGather(0, ambient_occlusion_noisy, point_clamp_sampler, uv, vec2<i32>(2i, 2i));
39 let left_edges = unpack4x8unorm(edges0.x);
40 let right_edges = unpack4x8unorm(edges1.x);
41 let top_edges = unpack4x8unorm(edges0.z);
42 let bottom_edges = unpack4x8unorm(edges2.w);
43 var center_edges = unpack4x8unorm(edges0.y);
44 center_edges *= vec4<f32>(left_edges.y, right_edges.x, top_edges.w, bottom_edges.z);
46 let center_weight = 1.2;
47 let left_weight = center_edges.x;
48 let right_weight = center_edges.y;
49 let top_weight = center_edges.z;
50 let bottom_weight = center_edges.w;
51 let top_left_weight = 0.425 * (top_weight * top_edges.x + left_weight * left_edges.z);
52 let top_right_weight = 0.425 * (top_weight * top_edges.y + right_weight * right_edges.z);
53 let bottom_left_weight = 0.425 * (bottom_weight * bottom_edges.x + left_weight * left_edges.w);
54 let bottom_right_weight = 0.425 * (bottom_weight * bottom_edges.y + right_weight * right_edges.w);
56 let center_visibility = visibility0.y;
57 let left_visibility = visibility0.x;
58 let right_visibility = visibility1.x;
59 let top_visibility = visibility0.z;
60 let bottom_visibility = visibility2.z;
61 let top_left_visibility = visibility0.w;
62 let top_right_visibility = visibility1.w;
63 let bottom_left_visibility = visibility2.w;
64 let bottom_right_visibility = visibility3.w;
66 var sum = center_visibility * center_weight;
67 sum += left_visibility * left_weight;
68 sum += right_visibility * right_weight;
69 sum += top_visibility * top_weight;
70 sum += bottom_visibility * bottom_weight;
71 sum += top_left_visibility * top_left_weight;
72 sum += top_right_visibility * top_right_weight;
73 sum += bottom_left_visibility * bottom_left_weight;
74 sum += bottom_right_visibility * bottom_right_weight;
76 var sum_weight = center_weight;
77 sum_weight += left_weight;
78 sum_weight += right_weight;
79 sum_weight += top_weight;
80 sum_weight += bottom_weight;
81 sum_weight += top_left_weight;
82 sum_weight += top_right_weight;
83 sum_weight += bottom_left_weight;
84 sum_weight += bottom_right_weight;
86 let denoised_visibility = sum / sum_weight;
88 textureStore(ambient_occlusion, pixel_coordinates, vec4<f32>(denoised_visibility, 0.0, 0.0, 0.0));
Contents