Skip to content

armgit / raea

Public Imported from GitHub
0

Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.

A glossy reflection no longer trails what it shows: the denoiser takes its reflection's depth

shinyobjectza691968e575f
diff --git a/engine/render/raytrace/src/metalfx.rs b/engine/render/raytrace/src/metalfx.rs
index 2f5049c0..b3d06624 100644
--- a/engine/render/raytrace/src/metalfx.rs
+++ b/engine/render/raytrace/src/metalfx.rs
@@ -64,7 +64,7 @@ pub(crate) fn add(app: &mut App) {
// Solari's own guides (primary surface replacement: a mirror's pixel takes what it reflects), unless
// RAEA_SOLARI_PSR=0, which guides from the G-buffer as before
if std::env::var("RAEA_SOLARI_PSR").as_deref() != Ok("0") {
- r.world_mut().insert_resource(bevy::solari::realtime::SolariGuideBuffers);
+ r.world_mut().insert_resource(bevy::solari::realtime::SolariGuideBuffers { reflection_depth: psr_depth() });
}
}
@@ -98,11 +98,14 @@ fn pre_exposure() -> f32 {
const DECODE_WGSL: &str = include_str!("metalfx_guides.wgsl");
-/// A view's denoiser and its textures, made again when its input's or output's size changes.
+/// A view's denoiser and its textures, made again when its input's or output's size changes, or its depth's kind.
struct View {
/// the main pass's size (the guides'), and the denoised frame's
size: UVec2,
out_size: UVec2,
+ /// whether its depth is Solari's guide depth (R32Float: what a mirror's pixel reflects, as far as the reflection
+ /// goes) rather than the prepass's (Depth32Float, the glass's own)
+ guide_depth: bool,
scaler: Retained<ProtocolObject<dyn MTLFXTemporalDenoisedScaler>>,
diffuse: wgpu::Texture,
specular: wgpu::Texture,
@@ -147,7 +150,7 @@ fn mtl(t: &wgpu::Texture) -> Option<Retained<ProtocolObject<dyn MTLTexture>>> {
}
impl View {
- fn new(dev: &wgpu::Device, size: UVec2, out_size: UVec2) -> Result<View, String> {
+ fn new(dev: &wgpu::Device, size: UVec2, out_size: UVec2, guide_depth: bool) -> Result<View, String> {
let hal = unsafe { dev.as_hal::<Metal>() }.ok_or("the GPU isn't on Metal")?;
let device = hal.raw_device();
unsafe {
@@ -156,7 +159,7 @@ impl View {
}
let d = MTLFXTemporalDenoisedScalerDescriptor::new();
d.setColorTextureFormat(MTLPixelFormat::RGBA16Float);
- d.setDepthTextureFormat(MTLPixelFormat::Depth32Float);
+ d.setDepthTextureFormat(if guide_depth { MTLPixelFormat::R32Float } else { MTLPixelFormat::Depth32Float });
d.setMotionTextureFormat(MTLPixelFormat::RG16Float);
d.setDiffuseAlbedoTextureFormat(MTLPixelFormat::RGBA16Float);
d.setSpecularAlbedoTextureFormat(MTLPixelFormat::RGBA16Float);
@@ -177,6 +180,7 @@ impl View {
Ok(View {
size,
out_size,
+ guide_depth,
scaler,
diffuse: texture(dev, size, wgpu::TextureFormat::Rgba16Float, guide, "metalfx diffuse albedo"),
specular: texture(dev, size, wgpu::TextureFormat::Rgba16Float, guide, "metalfx specular albedo"),
@@ -279,6 +283,15 @@ fn denoise_up(view: ViewQuery<Viewed>, device: Res<RenderDevice>, mut kit: Local
run(entity, item, &device, &mut kit, &mut ctx, true);
}
+/// The denoiser's depth is Solari's guide depth, where a glossy pixel is as far off as what it reflects (docs/raytracing.feature
+/// RT34): an orb's reflection in a monitor's glass no longer trails it (+0.71 -> +0.08 stops). RAEA_SOLARI_PSR_DEPTH=0
+/// gives it the prepass's depth, as before. A glossy pixel whose reflection leaves the scene must have its depth too: left
+/// unwritten (0, the sky's) MetalFX darkened window.tsx's shelves, far from those pixels, by 8%
+fn psr_depth() -> bool {
+ static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
+ *ON.get_or_init(|| std::env::var("RAEA_SOLARI_PSR_DEPTH").as_deref() != Ok("0"))
+}
+
type Item<'a> = (&'a ViewTarget, &'a ViewPrepassTextures, &'a ExtractedView, &'a ExtractedCamera, &'a SolariLighting, Option<&'a MainPassResolutionOverride>, Option<&'a TemporalJitter>, bool, Option<&'a SolariGuideTextures>);
/// The guides from the G-buffer and the emissive out, at the main pass's size; MetalFX's denoiser, out to the whole
@@ -298,8 +311,11 @@ fn run(entity: Entity, item: Item, device: &RenderDevice, kit: &mut Kit, ctx: &m
let out_size = if up { full } else { size };
let jitter = jitter.map_or(Vec2::ZERO, |j| -j.offset * super::upscale::jitter_gain(true, true));
let dev = device.wgpu_device().clone();
- if kit.views.get(&entity).map(|v| (v.size, v.out_size)) != Some((size, out_size)) {
- match View::new(&dev, size, out_size) {
+ // with Solari's guides, the denoiser's depth is theirs: a mirror's pixel as far off as what it reflects, so when what
+ // it reflects moves off (an orb in a monitor's glass) the wall now there is a disocclusion and its history goes
+ let guide_depth = guides.is_some() && psr_depth();
+ if kit.views.get(&entity).map(|v| (v.size, v.out_size, v.guide_depth)) != Some((size, out_size, guide_depth)) {
+ match View::new(&dev, size, out_size, guide_depth) {
Ok(v) => {
kit.views.insert(entity, v);
}
@@ -363,7 +379,7 @@ fn run(entity: Entity, item: Item, device: &RenderDevice, kit: &mut Kit, ctx: &m
let mut raw = dev.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("metalfx denoise") });
let done = unsafe {
raw.as_hal_mut::<Metal, _, _>(|e| match e.and_then(|e| e.raw_command_buffer()) {
- Some(cb) => v.encode(cb, color, &depth.texture.texture, guide_motion, ext, jitter, solari.reset),
+ Some(cb) => v.encode(cb, color, match guides { Some(g) if v.guide_depth => &g.depth.texture, _ => &depth.texture.texture }, guide_motion, ext, jitter, solari.reset),
None => Err("wgpu gave no Metal command buffer"),
})
};
diff --git a/vendor/bevy_solari/src/realtime/guides.rs b/vendor/bevy_solari/src/realtime/guides.rs
index 9e8cb60d..5ee0b664 100644
--- a/vendor/bevy_solari/src/realtime/guides.rs
+++ b/vendor/bevy_solari/src/realtime/guides.rs
@@ -21,7 +21,11 @@ use bevy_render::{
/// In the render world: every Solari view writes the guides (primary surface replacement included) to its
/// [`SolariGuideTextures`]. Inserted by the denoiser that reads them, before the render app starts.
#[derive(Resource, Clone, Copy, Default)]
-pub struct SolariGuideBuffers;
+pub struct SolariGuideBuffers {
+ /// a glossy pixel that moves as its reflection does (perceptual roughness up to 0.25) and mostly reflects is as far
+ /// off as its reflection in the guide depth, as a mirror's is (resolve_dlss_rr_textures.wesl PSR_DEPTH_FOLLOWS_MOTION)
+ pub reflection_depth: bool,
+}
/// A Solari view's guides, at its main pass's size, written each frame by its lighting pass, as DLSS-RR's are:
/// diffuse and specular albedo (Rgba8Unorm, linear), world normal and perceptual roughness (Rgba16Float), depth
diff --git a/vendor/bevy_solari/src/realtime/initial_path.wesl b/vendor/bevy_solari/src/realtime/initial_path.wesl
index c7aa1199..4448976c 100644
--- a/vendor/bevy_solari/src/realtime/initial_path.wesl
+++ b/vendor/bevy_solari/src/realtime/initial_path.wesl
@@ -120,7 +120,7 @@ fn generate_initial_path(world_position: vec3<f32>, world_normal: vec3<f32>, mat
if ray.kind == RAY_QUERY_INTERSECTION_NONE {
@if(DLSS_RR_GUIDE_BUFFERS)
if !psr.finished && !next_bounce.diffuse_selected {
- psr_environment_miss(pixel_id, wo, psr.replace_fully);
+ psr_environment_miss(pixel_id, wo, psr.replace_fully, material, world_normal);
}
// raea: the sun only off a mirror's specular lobe, which light sampling can't reach; elsewhere it finds the sun
diff --git a/vendor/bevy_solari/src/realtime/node.rs b/vendor/bevy_solari/src/realtime/node.rs
index a67497a0..3715cca5 100644
--- a/vendor/bevy_solari/src/realtime/node.rs
+++ b/vendor/bevy_solari/src/realtime/node.rs
@@ -443,6 +443,7 @@ pub fn init_solari_lighting_pipelines(
) {
// raea: the pipelines that write the guides, for DLSS-RR or for guides.rs's
let psr = guides.is_some() || cfg!(all(feature = "dlss", not(feature = "force_disable_dlss")));
+ let guides_rs = guides.is_some_and(|g| g.reflection_depth);
let motion_vectors_storage_read_write = render_adapter
.get_texture_format_features(MOTION_VECTOR_PREPASS_FORMAT)
.flags
@@ -553,6 +554,11 @@ pub fn init_solari_lighting_pipelines(
}
ExtraBindGroup::DlssRrGuideBuffers => {
shader_defs.push("DLSS_RR_GUIDE_BUFFERS".into());
+ // raea: guides.rs's guides (MetalFX's) take one depth a pixel, so a pixel moving as its reflection does
+ // is as far off as it (resolve_dlss_rr_textures.wesl); DLSS-RR has a specular motion of its own
+ if guides_rs {
+ shader_defs.push("PSR_DEPTH_FOLLOWS_MOTION".into());
+ }
}
}
shader_defs.extend_from_slice(&extra_shader_defs);
diff --git a/vendor/bevy_solari/src/realtime/resolve_dlss_rr_textures.wesl b/vendor/bevy_solari/src/realtime/resolve_dlss_rr_textures.wesl
index 77bf3051..79947db2 100644
--- a/vendor/bevy_solari/src/realtime/resolve_dlss_rr_textures.wesl
+++ b/vendor/bevy_solari/src/realtime/resolve_dlss_rr_textures.wesl
@@ -32,9 +32,8 @@ fn resolve_dlss_rr_textures(@builtin(global_invocation_id) global_id: vec3<u32>)
let depth = textureLoad(depth_buffer, global_id.xy, 0);
let surface_motion_vector = load_motion_vector(pixel_id);
- textureStore(dlss_rr_depth, pixel_id, vec4(depth));
-
if depth == 0.0 {
+ textureStore(dlss_rr_depth, pixel_id, vec4(depth));
textureStore(specular_motion_vectors, pixel_id, vec4(surface_motion_vector, vec2(0.0)));
textureStore(diffuse_albedo, pixel_id, vec4(0.0));
textureStore(specular_albedo, pixel_id, vec4(0.5));
@@ -46,6 +45,17 @@ fn resolve_dlss_rr_textures(@builtin(global_invocation_id) global_id: vec3<u32>)
let F0 = calculate_F0(surface.material.base_color, surface.material.metallic, vec3(surface.material.reflectance));
let wo = normalize(view.world_position - surface.world_position);
+ // raea: with PSR_DEPTH_FOLLOWS_MOTION (guides.rs's, for MetalFX: one motion and one depth a pixel), a glossy pixel
+ // mostly reflecting takes its reflection's depth with its motion (replace_primary_surface), and keeps it on the
+ // frames it draws its diffuse lobe, as its motion is kept; flipping back to the glass's would read as a disocclusion
+ var depth_is_the_surfaces = true;
+ @if(PSR_DEPTH_FOLLOWS_MOTION) {
+ depth_is_the_surfaces = !depth_follows_reflection(surface.material, max(dot(surface.world_normal, wo), 0.0001));
+ }
+ if depth_is_the_surfaces {
+ textureStore(dlss_rr_depth, pixel_id, vec4(depth));
+ }
+
textureStore(diffuse_albedo, pixel_id, vec4(calculate_diffuse_color(surface.material.base_color, surface.material.metallic, 0.0, 0.0), 0.0));
textureStore(specular_albedo, pixel_id, vec4(env_brdf_approx2(F0, surface.material.roughness, surface.world_normal, wo), 0.0));
textureStore(normal_roughness, pixel_id, vec4(surface.world_normal, surface.material.perceptual_roughness));
@@ -62,6 +72,13 @@ fn needs_specular_motion_vector(roughness: f32) -> bool {
return roughness <= 0.0625;
}
+// raea: a pixel whose depth is its reflection's under PSR_DEPTH_FOLLOWS_MOTION: one whose motion is (a near-mirror) and
+// that mostly reflects (a dark monitor's glass, 0.88 of its light specular): when what it reflects moves off (an orb), the
+// denoiser sees the wall behind as a disocclusion and drops the orb's history, where at the glass's depth it smeared it
+fn depth_follows_reflection(material: ResolvedMaterial, NdotV: f32) -> bool {
+ return needs_specular_motion_vector(material.roughness) && specular_fraction(material, F_AB(material.perceptual_roughness, NdotV)) >= 0.5;
+}
+
/// Carried across every bounce of the path walk, so anything derivable from a value the walk already
/// holds is derived at the end instead of stored. What is left is only what the walk itself produces.
struct PsrState {
@@ -108,10 +125,18 @@ fn psr_chain_mirror(psr: PsrState, plane_normal: vec3<f32>) -> PsrState {
return chained;
}
-fn psr_environment_miss(pixel_id: vec2<u32>, primary_wo: vec3<f32>, replace_fully: bool) {
+fn psr_environment_miss(pixel_id: vec2<u32>, primary_wo: vec3<f32>, replace_fully: bool, primary_material: ResolvedMaterial, primary_normal: vec3<f32>) {
let far_position = view.world_position - (primary_wo * ENVIRONMENT_VIRTUAL_DISTANCE);
let motion = calculate_motion_vector(far_position, far_position);
textureStore(specular_motion_vectors, pixel_id, vec4(motion, vec2(0.0)));
+ // raea: a glossy pixel whose depth follows its reflection's motion, reflecting the sky: as far off as the point its
+ // motion is of
+ @if(PSR_DEPTH_FOLLOWS_MOTION) {
+ if !replace_fully && depth_follows_reflection(primary_material, max(dot(primary_normal, primary_wo), 0.0001)) {
+ let far_clip = view.unjittered_clip_from_world * vec4(far_position, 1.0);
+ textureStore(dlss_rr_depth, pixel_id, vec4(far_clip.z / far_clip.w));
+ }
+ }
// raea: a mirror that shows the sky is guided as the sky's own pixels are: infinitely far, nothing to denoise.
// Its diffuse albedo's alpha 1 says so to a denoiser other than DLSS-RR (guides.rs); left as the glass's, the
// denoiser smoothed the clouds a mirror shows away
@@ -145,6 +170,8 @@ fn replace_primary_surface(
// Too curved to track the reflection; fall back to the surface's own motion so the guide is not stale
if psr.replace_fully && 2.0 * reflector_curvature(pixel_id, primary_normal) * psr.reflection_length > MAX_VIRTUAL_DEPTH_JITTER * path_length {
textureStore(specular_motion_vectors, pixel_id, vec4(load_motion_vector(pixel_id), vec2(0.0)));
+ @if(PSR_DEPTH_FOLLOWS_MOTION)
+ textureStore(dlss_rr_depth, pixel_id, vec4(textureLoad(depth_buffer, pixel_id, 0)));
return;
}
@@ -156,6 +183,11 @@ fn replace_primary_surface(
textureStore(specular_motion_vectors, pixel_id, vec4(specular_motion_vector, vec2(0.0)));
if !psr.replace_fully {
+ @if(PSR_DEPTH_FOLLOWS_MOTION) {
+ if depth_follows_reflection(primary_material, max(dot(primary_normal, primary_wo), 0.0001)) {
+ textureStore(dlss_rr_depth, pixel_id, vec4(textureLoad(depth_buffer, pixel_id, 0) * primary_distance / path_length));
+ }
+ }
return;
}