LT21: the sun's shadows fade out at their distance instead of ending in a line; the extract-order line out again

Bevy draws everything past the last shadow cascade fully lit, so a city's shadows stopped in a straight line across
the street (on the 540p fly over Berlin, at 300 m). raea-sys-lod swaps Bevy's shadows.wesl (its hash checked) for
one whose last cascade fades to lit over the cascades' overlap, the last 30% of the distance, and reloads it: an
embedded shader is read on the IO threads as soon as PbrPlugin registers it, so a swap made later draws Bevy's.
The fly's frames change on about 2% of their pixels, all lighter.

Reaching 900 m, where the day's fog begins, costs too much as it is (live at 2560x1440, median of 3, work p50):
walk 45.9 ms at 300 m against 53.3, fly 42.5 against 54.3. Far shadows wait on far cascades cast by coarse levels
and redrawn every few frames.

The ordering of mesh extraction after visibility ranges comes out: Bevy 0.20 already orders them (bevy_pbr
mesh.rs:200), and it changed no pixel. The frame 35 and 226 snaps are still open.

LT14, LT1 on raster before (main 19:53) and after:
  floor3-room  mean 1.379 / 1.379 stops, bias -0.801 / -0.801, worst 3.370 / 3.370, flicker 0.101 / 0.101,
               live 15.5 / 14.8 ms (work p50 15.47 / 15.14)
  glade        mean 1.758 / 1.758, bias 1.542 / 1.542, worst 3.000 / 3.000, flicker 0.817 / 0.817
  gambit-walk  live 47.2 / 44.1 ms (work p50 44.76 / 42.48); no reference yet
  kitchen      live 61.0 / 59.9 ms (work p50 58.80 / 58.58); no reference yet
The live differences are run to run; the fade costs nothing measurable.
This commit is contained in:
shinyobjectz committed 2026-11-22 09:15:58 -08:00
1 parent 0dfab1dbfd
commit e35a7769c0
5 files changed
+84 -11

No files matched your search

Generated
+1
View File
@@ -16124,6 +16124,7 @@ name = "raea-sys-lod"
version = "0.1.0"
dependencies = [
"bevy",
"raea-sys-frame",
]
[[package]]
+12 -3
View File
@@ -363,13 +363,22 @@ Feature: raea Light: raea's own real-time realism renderer, measured against rae
And so far (11-17, branch realism; not done): a Model's `detail` (near, far, max_px, min_px, edge, fade,
shadow) is raea-sys-lod's Detail. Bevy's dither now runs in raea's own fragments (ShaderExt, ShaderMaterial).
Ranges are measured from each mesh's origin, as Bevy's GPU measures them, and moved to the part's nearest
edge. Meshes are extracted after the ranges. The live diff keeps its fast path with Detail rows (it had run in
full every frame, +19 ms a step). gambit's City draws `levels="detail"`, its tiles' levels cross-faded by
edge. The live diff keeps its fast path with Detail rows (it had run in full every frame, +19 ms a step). gambit's City draws `levels="detail"`, its tiles' levels cross-faded by
distance, streamed or not
And measured on the 540p fly over Berlin (240 frames; a one-frame spike is an 8x8 block whose change is over
12/255 and 3 times the frames' either side): detail's level switches at frames 41, 117, 122 and 157 (45, 20,
19 and 17 blocks) are gone, and JS levels' at 58, 106, 112 and 148 (41, 15, 16, 13) are unchanged. Two left
at 35 and 226 (29 and 12 blocks) came from Bevy's extract order; their fix waits for a rebuild to be checked
at 35 and 226 (29 and 12 blocks) aren't Bevy's extract order: ordering meshes after the ranges changed no
pixel (Bevy 0.20 already orders them, bevy_pbr mesh.rs:200), and it came out again. At 35 a held tile's
arcaded row fades out half stippled for three frames with the sky showing through, no level behind it, then
drops in one frame: open
And the sun's shadows no longer end in a line (Shane, 11-17, on the fly: "the shadow having a clear cull
distance"): Bevy draws past its last cascade fully lit, so raea-sys-lod swaps its shadows.wesl (hash checked)
for one whose last cascade fades to lit over the cascades' overlap, the last 30% of the distance; the fly's
frames change on about 2% of their pixels, all lighter. Reaching 900 m, where DAY's fog begins, costs too
much as it is: live at 2560x1440, median of 3, work p50 walk 45.9 ms at 300 m against 53.3 at 900, fly
42.5 against 54.3. Far shadows wait on the far cascades cast by coarse levels only and redrawn every few
frames
And measured live at 2560x1440 on an M-series Mac, work p50 JS levels against detail: walk 51.7 against 48.9
ms (p99 144.8 against 78.2), fly 50.3 against 50.1 (four runs each, about 5 ms apart run to run), streamed
run 34.7 against 31.8 (p99 143.3 against 114.6). Detail adds 0.4 to 1.3 ms of step. Footprint: walk 6.97
+3 -1
View File
@@ -4,7 +4,9 @@ version = "0.1.0"
edition = "2024"
license = "FSL-1.1-ALv2"
publish = false
description = "Detail by distance and size on screen: where each part is drawn, its levels cross-faded, its shadow left to a coarser level, nothing drawn past its last (docs/realism.feature LT21)"
description = "Detail by distance and size on screen: where each part is drawn, its levels cross-faded, its shadow left to a coarser level, nothing drawn past its last, the sun's shadows faded out at their distance (docs/realism.feature LT21)"
[dependencies]
bevy = { version = "=0.20.0", default-features = false, features = ["std", "default_app", "3d_bevy_render"] }
# swapping Bevy's shadows.wesl for the one whose last cascade fades out (shadow.rs)
raea-sys-frame = { path = "../frame" }
+4 -7
View File
@@ -7,6 +7,9 @@
//! camera's vertical field of view and its target's height in pixels, so a level looks the same at any output size.
//! `shadow: false` keeps a level out of the shadow maps, for a coarser level to cast its shadow.
// the sun's shadows fade out at their distance rather than end in a line
mod shadow;
use bevy::{
camera::{
primitives::Aabb,
@@ -49,13 +52,7 @@ struct ShadowLeft;
pub fn install(app: &mut App) {
app.register_type::<Detail>().add_systems(PostUpdate, apply.after(VisibilitySystems::CalculateBounds).after(bevy::transform::TransformSystems::Propagate).before(VisibilitySystems::CheckVisibility));
// Bevy 0.20 extracts meshes and visibility ranges in no set order. A mesh re-extracted for its changed range before
// the ranges are read bakes the range's previous slot into its uniform and keeps it until its range changes again:
// the GPU dithers it by a range the CPU no longer culls by (on the 540p fly a tile's LOD1, in range at 14% faded,
// drew nothing for two frames, then snapped in). Meshes are extracted after the ranges.
if let Some(render) = app.get_sub_app_mut(bevy::render::RenderApp) {
render.configure_sets(bevy::render::ExtractSchedule, bevy::pbr::MeshExtractionSystems.after(bevy::render::view::extract_visibility_ranges));
}
shadow::install(app);
match std::env::var("RAEA_LOD_DEBUG").as_deref() {
Ok("time") => {
app.init_resource::<LodTime>();
+64
View File
@@ -0,0 +1,64 @@
//! The sun's shadows fade out where they end (docs/realism.feature LT21). Bevy blends each shadow cascade into the next
//! over their overlap, but draws everything past the last cascade's far bound fully lit, so a city's shadows stop in a
//! straight line across the street (Shane, 11-17, on the 540p fly: "the shadow having a clear cull distance"). Bevy's
//! shadows.wesl is swapped for one whose last cascade fades to lit over the same overlap (raea_sys_frame's
//! swap_embedded_shader, which checks Bevy's file is still the one this was written against).
use bevy::prelude::*;
/// FNV-1a of bevy_pbr 0.20's render/shadows.wesl, the file the fade was written against
const UPSTREAM: u64 = 0xcd13_e017_3c8e_a62c;
/// the end of fetch_directional_shadow's blend into the next cascade, where the last cascade's fade goes
const FROM: &str = " shadow = mix(shadow, next_shadow, (-view_z - next_near_bound) / (this_far_bound - next_near_bound));
}
}
return shadow;
}";
const TO: &str = " shadow = mix(shadow, next_shadow, (-view_z - next_near_bound) / (this_far_bound - next_near_bound));
}
} else {
// raea: the last cascade fades to lit over the cascades' overlap, so the shadows don't end in a line
let far_bound = (*light).cascades[cascade_index].far_bound;
let fade_from = (1.0 - (*light).cascades_overlap_proportion) * far_bound;
if (-view_z >= fade_from) {
shadow = mix(shadow, 1.0, (-view_z - fade_from) / max(far_bound - fade_from, 0.0001));
}
}
return shadow;
}";
/// Bevy's shadows.wesl with the last cascade's fade
fn faded(bevy: &str) -> String {
assert_eq!(bevy.matches(FROM).count(), 1, "raea: Bevy's shadows.wesl no longer ends its cascade blend where raea's fade goes (docs/bevy-upgrade.md)");
bevy.replace(FROM, TO)
}
/// Swaps Bevy's shadows.wesl for the faded one; after PbrPlugin, which embeds it. Without PBR there's nothing to fade.
pub fn install(app: &mut App) {
if !raea_sys_frame::bevy_api::swap_embedded_shader(app, "bevy_pbr/render/shadows.wesl", UPSTREAM, faded) {
debug!("raea-sys-lod: no bevy_pbr shadows.wesl to fade");
return;
}
// PbrPlugin started loading Bevy's file when it was built, and an embedded file is read on the IO threads at once,
// so by the time this plugin is built it already holds Bevy's bytes (on 11-17 the fly drew bit for bit as before
// the swap): read again, from the swapped file
if let Some(server) = app.world().get_resource::<AssetServer>() {
server.reload("embedded://bevy_pbr/render/shadows.wesl");
}
}
#[cfg(test)]
mod tests {
use super::*;
/// The fade replaces the end of the cascade blend once and adds the last cascade's branch.
#[test]
fn the_last_cascade_fades() {
let bevy = format!("fn fetch_directional_shadow() -> f32 {{\n if (next_cascade_index < n) {{\n if (x) {{\n{FROM}\n");
let ours = faded(&bevy);
assert_eq!(ours.matches("raea: the last cascade fades").count(), 1);
assert!(ours.contains("shadow = mix(shadow, 1.0,"));
}
}