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#define_import_path bevy_solari::gbuffer_utils
3#import bevy_pbr::pbr_deferred_types::unpack_24bit_normal
4#import bevy_pbr::rgb9e5::rgb9e5_to_vec3_
5#import bevy_pbr::utils::octahedral_decode
6#import bevy_render::view::{View, depth_ndc_to_view_z}
7#import bevy_solari::scene_bindings::ResolvedMaterial
9struct ResolvedGPixel {
10 world_position: vec3<f32>,
11 world_normal: vec3<f32>,
12 material: ResolvedMaterial,
15fn gpixel_resolve(gpixel: vec4<u32>, depth: f32, pixel_id: vec2<u32>, view_size: vec2<f32>, world_from_clip: mat4x4<f32>) -> ResolvedGPixel {
16 let world_position = reconstruct_world_position(pixel_id, depth, view_size, world_from_clip);
17 let world_normal = octahedral_decode(unpack_24bit_normal(gpixel.a));
19 let base_rough = unpack4x8unorm(gpixel.r);
20 let base_color = pow(base_rough.rgb, vec3(2.2));
21 // Clamp roughness to prevent NaNs
22 let perceptual_roughness = clamp(base_rough.a, 0.0316227766, 1.0); // Clamp roughness to 0.001
23 let roughness = perceptual_roughness * perceptual_roughness;
24 let props = unpack4x8unorm(gpixel.b);
25 let reflectance = vec3(props.r);
26 let metallic = saturate(props.g); // TODO: Not sure why saturate is needed here to prevent NaNs
27 let emissive = rgb9e5_to_vec3_(gpixel.g);
28 let material = ResolvedMaterial(base_color, emissive, reflectance, perceptual_roughness, roughness, metallic);
30 return ResolvedGPixel(world_position, world_normal, material);
33fn reconstruct_world_position(pixel_id: vec2<u32>, depth: f32, view_size: vec2<f32>, world_from_clip: mat4x4<f32>) -> vec3<f32> {
34 let uv = (vec2<f32>(pixel_id) + 0.5) / view_size;
35 let xy_ndc = (uv - vec2(0.5)) * vec2(2.0, -2.0);
36 let world_pos = world_from_clip * vec4(xy_ndc, depth, 1.0);
37 return world_pos.xyz / world_pos.w;
40// Reject if tangent plane differs more than 0.3% or angle between normals more than 25 degrees
41fn pixel_dissimilar(depth: f32, world_position: vec3<f32>, other_world_position: vec3<f32>, normal: vec3<f32>, other_normal: vec3<f32>, view: View) -> bool {
42 // https://developer.download.nvidia.com/video/gputechconf/gtc/2020/presentations/s22699-fast-denoising-with-self-stabilizing-recurrent-blurs.pdf#page=45
43 let tangent_plane_distance = abs(dot(normal, other_world_position - world_position));
44 let view_z = -depth_ndc_to_view_z(depth, view.clip_from_view, view.view_from_clip);
46 return tangent_plane_distance / view_z > 0.003 || dot(normal, other_normal) < 0.906;
49fn permute_pixel(pixel_id: vec2<u32>, frame_index: u32, view_size: vec2<f32>) -> vec2<u32> {
50 let r = frame_index;
51 let offset = vec2(r & 3u, (r >> 2u) & 3u);
52 var shifted_pixel_id = pixel_id + offset;
53 shifted_pixel_id ^= vec2(3u);
54 shifted_pixel_id -= offset;
55 return min(shifted_pixel_id, vec2<u32>(view_size - 1.0));
Contents