diff --git a/docs/far-shadows.md b/docs/far-shadows.md new file mode 100644 index 00000000..81181364 --- /dev/null +++ b/docs/far-shadows.md @@ -0,0 +1,23 @@ +# Static far sun shadows + +`setFarShadow(centerX, centerZ, halfX, halfZ, yMin, yMax, refreshFrames)` gives a large, mostly static world sun shadows +to the horizon (a city seen from a hill or from the air), which stretching the camera cascades over kilometres cannot do +cheaply: they blur, and they redraw the world whenever the camera or any caster moves. + +With it on, the third cascade stops following the camera. It is one fixed orthographic map over the given rectangle, +drawn from the static casters only, redrawn at most once every `refreshFrames` frames when the sun, the box or the static +casters changed; between redraws receivers sample it through the projection it was drawn with. The first two cascades +split `setShadowDistance` between them, and moving casters keep their shadows there. The receiver shaders already take +cascade 2 beyond the second split, so no shader changed; the third split carries the far map's fit radius, scaled by +`FAR_RECEIVER_OFFSET` (half the usual normal offset: a far texel is metres wide, and the usual 1.5 texels lifted the ground +under a house above its roof). + +The far map has its own per-node uniform buffer, so it is not limited to a cascade's 1,792 static slots. + +Measured on the Nineveh city (`city-view --farshadow 1`, about 2,000 to 6,000 casters in the map, M1 Max at 1600 x 900, a +loaded machine): steady state within noise of off (main pass 1.96 ms against 1.94 to 1.99); one redraw about 8 ms of GPU, +so at 30 frames it averages to nothing measurable in a fly-through, and redrawing every frame costs that 8 ms each frame. +Sharpness is the cascade map size over the rectangle as the sun sees it (`setShadowMapSize`). + +Code: `native/shared/src/far_shadow.rs`, the cascade split in `shadows.rs::compute_cascade_vps`, the redraw gate and the +uniform buffer in `renderer/shadow_pass.rs`. diff --git a/native/shared/src/far_shadow.rs b/native/shared/src/far_shadow.rs new file mode 100644 index 00000000..3dd32b7c --- /dev/null +++ b/native/shared/src/far_shadow.rs @@ -0,0 +1,244 @@ +//! Static far sun-shadow layer. +//! +//! A large world (a city seen from a hill or from the air) wants sun shadows to +//! the horizon, but stretching the camera-fitted cascades over kilometres both +//! blurs the near shadows and redraws the whole world whenever any caster or the +//! camera moves. With the far layer on, the third cascade stops following the +//! camera: it is one fixed orthographic map over a box the game names, drawn +//! from the static casters only, and redrawn at most once per refresh interval +//! (the sun, the box or the static caster set having changed). The first two +//! cascades split the ordinary shadow distance between them; receivers beyond +//! it sample the far map. Every receiver shader already selects cascade 2 +//! beyond the second split and reads its fit radius from the third split, so +//! no shader changes: the third split carries the far map's half-extent, +//! scaled by `FAR_RECEIVER_OFFSET`. + +/// The receivers push themselves 1.5 shadow texels off their surface (and bias +/// their depth by one), sized from the cascade's fit radius. A far texel is +/// metres wide: 1.5 of them lifted the ground under a 7 m house above its roof +/// and the far shadows all but vanished. Half of it keeps sun-facing walls free +/// of acne in the Nineveh test views and lets the shadows of houses land. +pub const FAR_RECEIVER_OFFSET: f32 = 0.5; + +/// The far layer as the game set it (`Renderer::set_far_shadow`). +#[derive(Copy, Clone, Debug, PartialEq)] +pub struct FarShadow { + /// Centre of the covered box on the ground plane (x, z), metres. + pub center: [f32; 2], + /// Half the covered rectangle's extent along x and along z, metres. + pub half: [f32; 2], + /// Lowest and highest caster height to cover, metres. + pub y_range: [f32; 2], + /// Minimum frames between two redraws of the map. The first draw, and a + /// draw after the box itself changes, happen at once. + pub refresh_frames: u32, +} + +/// Bookkeeping of what the far map holds. +#[derive(Default)] +pub struct FarShadowState { + /// The projection the far map was last drawn with; receivers keep + /// sampling through it until the next redraw. + pub rendered_vp: Option<[[f32; 4]; 4]>, + /// Frames since the last redraw. + pub frames_since: u32, + /// Per-node shadow uniforms for the far map, which holds far more casters + /// than a cascade's region of the shared buffer (`SHADOW_MAX_NODES`). + /// Grown on demand; (buffer, bind group, capacity in nodes). + pub uniforms: Option<(wgpu::Buffer, wgpu::BindGroup, usize)>, + /// How many casters the last redraw drew (statistics). + pub last_casters: usize, + /// How many redraws so far (statistics). + pub redraws: u64, +} + +impl FarShadowState { + /// A uniform buffer and bind group holding at least `nodes` per-node slots. + pub fn uniforms_for( + &mut self, + device: &wgpu::Device, + layout: &wgpu::BindGroupLayout, + nodes: usize, + ) -> (&wgpu::Buffer, &wgpu::BindGroup) { + let need = nodes.max(1); + let grow = match &self.uniforms { + Some((_, _, cap)) => *cap < need, + None => true, + }; + if grow { + let cap = need.next_power_of_two().max(1024); + let buffer = device.create_buffer(&wgpu::BufferDescriptor { + label: Some("far_shadow_uniform_buf"), + size: (crate::shadows::SHADOW_UNIFORM_STRIDE as usize * cap) as u64, + usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, + mapped_at_creation: false, + }); + let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("far_shadow_uniform_bg"), + layout, + entries: &[wgpu::BindGroupEntry { + binding: 0, + resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding { + buffer: &buffer, + offset: 0, + size: std::num::NonZeroU64::new(std::mem::size_of::< + crate::shadows::ShadowUniforms, + >() as u64), + }), + }], + }); + self.uniforms = Some((buffer, bind_group, cap)); + } + let (buffer, bind_group, _) = self.uniforms.as_ref().expect("far uniforms just made"); + (buffer, bind_group) + } +} + +/// The far map's view-projection for a normalised light direction `d` +/// (surface toward light) and the light-plane basis `right`, `up` the cascades +/// use. Returns the VP and the larger half-extent of the ortho window (the +/// value the receiver shaders take as the cascade's fit radius). +pub fn far_vp( + far: &FarShadow, + d: [f32; 3], + right: [f32; 3], + up: [f32; 3], + up_hint: [f32; 3], +) -> ([[f32; 4]; 4], f32) { + let y_mid = 0.5 * (far.y_range[0] + far.y_range[1]); + let y_half = 0.5 * (far.y_range[1] - far.y_range[0]).abs(); + let center = [far.center[0], y_mid, far.center[1]]; + // Half-extents of the box (half, y_half, half) projected on each axis. + let extent = + |a: [f32; 3]| a[0].abs() * far.half[0] + a[1].abs() * y_half + a[2].abs() * far.half[1]; + // Whole metres, so float noise in the light direction cannot move the map. + let hx = extent(right).ceil(); + let hy = extent(up).ceil(); + let hz = extent(d).ceil() + 1.0; + let eye = [ + center[0] + d[0] * hz, + center[1] + d[1] * hz, + center[2] + d[2] * hz, + ]; + let view = crate::renderer::mat4_look_at(eye, center, up_hint); + let proj = crate::renderer::mat4_ortho(-hx, hx, -hy, hy, 0.0, 2.0 * hz); + (crate::renderer::mat4_multiply(proj, view), hx.max(hy)) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn apply(m: [[f32; 4]; 4], p: [f32; 3]) -> [f32; 3] { + let v = [p[0], p[1], p[2], 1.0]; + let mut o = [0.0f32; 4]; + for (r, out) in o.iter_mut().enumerate() { + *out = (0..4).map(|c| m[c][r] * v[c]).sum(); + } + [o[0] / o[3], o[1] / o[3], o[2] / o[3]] + } + + #[test] + fn far_vp_covers_the_box() { + let far = FarShadow { + center: [100.0, -200.0], + half: [3000.0, 3000.0], + y_range: [-20.0, 120.0], + refresh_frames: 30, + }; + let d = { + let v = [0.62f32, 0.55, 0.56]; + let l = (v[0] * v[0] + v[1] * v[1] + v[2] * v[2]).sqrt(); + [v[0] / l, v[1] / l, v[2] / l] + }; + let up_hint = [0.0, 1.0, 0.0]; + let right = { + let r = [ + up_hint[1] * d[2] - up_hint[2] * d[1], + up_hint[2] * d[0] - up_hint[0] * d[2], + up_hint[0] * d[1] - up_hint[1] * d[0], + ]; + let l = (r[0] * r[0] + r[1] * r[1] + r[2] * r[2]).sqrt(); + [r[0] / l, r[1] / l, r[2] / l] + }; + let up = [ + d[1] * right[2] - d[2] * right[1], + d[2] * right[0] - d[0] * right[2], + d[0] * right[1] - d[1] * right[0], + ]; + let (vp, fit) = far_vp(&far, d, right, up, up_hint); + assert!(fit >= 3000.0); + for &x in &[-2900.0f32, 3100.0] { + for &z in &[-3200.0f32, 2800.0] { + for &y in &[-20.0f32, 120.0] { + let n = apply(vp, [x, y, z]); + assert!( + n[0].abs() <= 1.0 && n[1].abs() <= 1.0, + "{:?} -> {:?}", + (x, y, z), + n + ); + assert!( + (0.0..=1.0).contains(&n[2]) || (-1.0..=1.0).contains(&n[2]), + "depth {:?}", + n + ); + } + } + } + } +} + +impl crate::renderer::Renderer { + /// Turn the static far shadow layer on over the rectangle of half-extents + /// `half_x`, `half_z` metres centred on (`center_x`, `center_z`), covering + /// casters between `y_min` and `y_max`, redrawn at most every + /// `refresh_frames` frames; or off with a half-extent `<= 0`. See + /// `crate::far_shadow`. + #[allow(clippy::too_many_arguments)] + pub fn set_far_shadow( + &mut self, + center_x: f32, + center_z: f32, + half_x: f32, + half_z: f32, + y_min: f32, + y_max: f32, + refresh_frames: u32, + ) { + let next = if half_x.is_finite() && half_z.is_finite() && half_x > 0.0 && half_z > 0.0 { + Some(FarShadow { + center: [center_x, center_z], + half: [half_x, half_z], + y_range: [y_min.min(y_max), y_min.max(y_max)], + refresh_frames: refresh_frames.max(1), + }) + } else { + None + }; + let box_changed = match (self.shadow_map.far, next) { + (Some(a), Some(b)) => { + a.center != b.center || a.half != b.half || a.y_range != b.y_range + } + (None, None) => false, + _ => true, + }; + self.shadow_map.far = next; + if box_changed { + // The new box is drawn at once; the cascades re-split. + self.shadow_map.far_state.rendered_vp = None; + self.shadow_map.invalidate(); + } + } + + /// Far-layer statistics: 0 redraws so far, 1 casters in the last redraw, + /// 2 frames since it. + pub fn far_shadow_stat(&self, which: u32) -> f64 { + let s = &self.shadow_map.far_state; + match which { + 0 => s.redraws as f64, + 1 => s.last_casters as f64, + _ => s.frames_since as f64, + } + } +} diff --git a/native/shared/src/ffi_core/visual.rs b/native/shared/src/ffi_core/visual.rs index 5c981304..38d10a58 100644 --- a/native/shared/src/ffi_core/visual.rs +++ b/native/shared/src/ffi_core/visual.rs @@ -361,6 +361,36 @@ macro_rules! __bloom_ffi_visual { }) } + // bloom_set_far_shadow + #[no_mangle] + pub extern "C" fn bloom_set_far_shadow( + center_x: f64, + center_z: f64, + half_x: f64, + half_z: f64, + y_min: f64, + y_max: f64, + refresh_frames: f64, + ) -> f64 { + $crate::ffi::guard_applied("bloom_set_far_shadow", move || { + engine().renderer.set_far_shadow( + center_x as f32, + center_z as f32, + half_x as f32, + half_z as f32, + y_min as f32, + y_max as f32, + refresh_frames.max(1.0) as u32, + ); + }) + } + + // bloom_far_shadow_stat + #[no_mangle] + pub extern "C" fn bloom_far_shadow_stat(which: f64) -> f64 { + engine().renderer.far_shadow_stat(which as u32) + } + // bloom_set_shadow_cascade_stagger #[no_mangle] pub extern "C" fn bloom_set_shadow_cascade_stagger(on: f64) -> f64 { diff --git a/native/shared/src/lib.rs b/native/shared/src/lib.rs index 06ebaaf7..5233777c 100644 --- a/native/shared/src/lib.rs +++ b/native/shared/src/lib.rs @@ -35,6 +35,7 @@ pub mod anim_morph; pub mod anim_rig; pub mod custom_shaders; pub mod decals; +pub mod far_shadow; pub mod frame_callbacks; pub mod geometry; pub mod models; diff --git a/native/shared/src/renderer/shadow_pass.rs b/native/shared/src/renderer/shadow_pass.rs index b51b083b..9d8e6537 100644 --- a/native/shared/src/renderer/shadow_pass.rs +++ b/native/shared/src/renderer/shadow_pass.rs @@ -103,6 +103,24 @@ impl Renderer { scene_bounds, ); + // The static far layer (`crate::far_shadow`): its map is redrawn at + // most once per refresh interval. Between redraws receivers keep + // sampling it through the projection it was drawn with. + let far_cascade = crate::shadows::NUM_CASCADES - 1; + let far_on = self.shadow_map.far.is_some(); + let mut far_due = false; + if let Some(far_layer) = self.shadow_map.far { + let state = &mut self.shadow_map.far_state; + state.frames_since = state.frames_since.saturating_add(1); + far_due = + state.rendered_vp.is_none() || state.frames_since >= far_layer.refresh_frames; + if !far_due { + if let Some(vp) = state.rendered_vp { + self.shadow_map.light_vps[far_cascade] = vp; + } + } + } + // Staggered cascades (`set_shadow_cascade_stagger`): on its off // frames a cascade beyond the first keeps the projection it was // last drawn with, so receivers sample the depth it still holds, @@ -118,6 +136,9 @@ impl Renderer { { if let Some(rendered) = self.shadow_map.rendered_light_vps { for cascade in 1..crate::shadows::NUM_CASCADES { + if far_on && cascade == far_cascade { + continue; + } if (stagger_frame + cascade as u32) % 2 == 1 { self.shadow_map.light_vps[cascade] = rendered[cascade]; stagger_skip[cascade] = true; @@ -732,6 +753,12 @@ impl Renderer { for (i, entry) in shadow_nodes.iter().enumerate() { let has_bounds = entry.wmin[0] <= entry.wmax[0]; for c in 0..crate::shadows::NUM_CASCADES { + // Moving casters stay out of the far map: they are near the + // camera, in the camera cascades, and drawing them would + // refresh the far map every frame. + if far_on && c == far_cascade && entry.dynamic { + continue; + } if has_bounds && crate::scene::aabb_outside_frustum( &cascade_planes[c], @@ -876,9 +903,11 @@ impl Renderer { .rendered_light_vps .map(|vps| vps[cascade] != self.shadow_map.light_vps[cascade]) .unwrap_or(true); - let static_stale = force_all + let is_far = far_on && cascade == far_cascade; + let static_stale = (force_all || vp_changed - || self.shadow_map.rendered_cascade_sig[cascade] != cascade_sigs[cascade]; + || self.shadow_map.rendered_cascade_sig[cascade] != cascade_sigs[cascade]) + && (!is_far || far_due); let dyn_now = entries.iter().any(|&ei| shadow_nodes[ei].dynamic); if !static_stale && !dyn_now && !self.shadow_map.had_dynamic[cascade] { // Live texture already holds exactly this content. @@ -889,12 +918,32 @@ impl Renderer { } if static_stale { + // The far map has its own uniform buffer, sized to its casters. + let cap = if is_far { usize::MAX } else { max_static }; let static_entries: Vec = entries .iter() .copied() .filter(|&ei| !shadow_nodes[ei].dynamic) - .take(max_static) + .take(cap) .collect(); + if is_far { + self.shadow_map.far_state.uniforms_for( + &self.device, + &self.shadow_map.uniform_layout, + static_entries.len(), + ); + } + let (static_buffer, static_bind_group, static_base) = + match (is_far, self.shadow_map.far_state.uniforms.as_ref()) { + (true, Some(far_uniforms)) => { + (&far_uniforms.0, &far_uniforms.1, 0usize) + } + _ => ( + &self.shadow_map.uniform_buffer, + &self.shadow_map.uniform_bind_group, + cascade_base, + ), + }; let mut uniform_data: Vec = vec![0u8; stride * static_entries.len().max(1)]; for (slot, &ei) in static_entries.iter().enumerate() { let uniforms = crate::shadows::ShadowUniforms { @@ -920,8 +969,8 @@ impl Renderer { // matrices (the no-near-shadows bug). if !static_entries.is_empty() { self.queue.write_buffer( - &self.shadow_map.uniform_buffer, - cascade_base as u64, + static_buffer, + static_base as u64, &uniform_data[..static_entries.len() * stride], ); } @@ -952,7 +1001,7 @@ impl Renderer { shadow_pass.set_pipeline(&self.shadow_map.pipeline); for (slot, &ei) in static_entries.iter().enumerate() { let entry = &shadow_nodes[ei]; - let offset = (cascade_base + slot * stride) as u32; + let offset = (static_base + slot * stride) as u32; let kind: u8 = if entry.skinned { 2 } else if entry.cutout_idx >= 0 { @@ -968,11 +1017,7 @@ impl Renderer { }); cur_kind = kind; } - shadow_pass.set_bind_group( - 0, - &self.shadow_map.uniform_bind_group, - &[offset], - ); + shadow_pass.set_bind_group(0, static_bind_group, &[offset]); if kind == 1 { shadow_pass.set_bind_group( 1, @@ -1001,6 +1046,13 @@ impl Renderer { } } self.shadow_map.rendered_cascade_sig[cascade] = cascade_sigs[cascade]; + if is_far { + let state = &mut self.shadow_map.far_state; + state.rendered_vp = Some(cascade_vp); + state.frames_since = 0; + state.last_casters = static_entries.len(); + state.redraws += 1; + } } // Refresh the live texture from the static cache, then draw diff --git a/native/shared/src/shadows.rs b/native/shared/src/shadows.rs index c9f1ec4f..ed6984da 100644 --- a/native/shared/src/shadows.rs +++ b/native/shared/src/shadows.rs @@ -438,6 +438,11 @@ pub struct ShadowMap { /// Shadow cubes of a few local lights for material-path receivers /// (`renderer/local_light_shadows.rs`). Inert unless a game turns it on. pub(crate) local_lights: crate::renderer::local_light_shadows::LocalLightShadows, + /// The static far layer (`crate::far_shadow`): with it set, cascade 2 is a + /// fixed map over the game's box and cascades 0 and 1 split `distance`. + pub far: Option, + /// What the far map holds and when it was drawn. + pub far_state: crate::far_shadow::FarShadowState, } /// Accepted (slack-inflated) ortho fit for one cascade. `ls_x`/`ls_y` @@ -868,6 +873,8 @@ impl ShadowMap { accepted_light_dir: None, virtual_map, local_lights, + far: None, + far_state: crate::far_shadow::FarShadowState::default(), } } @@ -924,15 +931,24 @@ impl ShadowMap { // uniform split for stability. let lambda = 0.5f32; // blend factor (0 = uniform, 1 = logarithmic) let ratio = far / near; + // With the far layer the last cascade is the fixed far map; the camera + // cascades share the ordinary shadow distance between them. + let camera_cascades = if self.far.is_some() { + NUM_CASCADES - 1 + } else { + NUM_CASCADES + }; let mut splits = [0.0f32; NUM_CASCADES + 1]; splits[0] = near; - for i in 1..NUM_CASCADES { - let p = i as f32 / NUM_CASCADES as f32; + for i in 1..camera_cascades { + let p = i as f32 / camera_cascades as f32; let log_split = near * ratio.powf(p); let uniform_split = near + (far - near) * p; splits[i] = lambda * log_split + (1.0 - lambda) * uniform_split; } - splits[NUM_CASCADES] = far; + for split in splits.iter_mut().skip(camera_cascades) { + *split = far; + } // Store view-space Z split distances for shader cascade selection. // cascade_splits[i] = far edge of cascade i. @@ -964,7 +980,12 @@ impl ShadowMap { d[0] * right[1] - d[1] * right[0], ]; - for c in 0..NUM_CASCADES { + if let Some(far_layer) = self.far { + let (vp, fit) = crate::far_shadow::far_vp(&far_layer, d, right, ortho_up, up_hint); + self.light_vps[NUM_CASCADES - 1] = vp; + self.cascade_splits[NUM_CASCADES - 1] = fit * crate::far_shadow::FAR_RECEIVER_OFFSET; + } + for c in 0..camera_cascades { let c_near = splits[c]; let c_far = splits[c + 1]; diff --git a/native/watchos/src/ffi_stubs.rs b/native/watchos/src/ffi_stubs.rs index f4275a28..3d7616de 100644 --- a/native/watchos/src/ffi_stubs.rs +++ b/native/watchos/src/ffi_stubs.rs @@ -405,6 +405,12 @@ #[no_mangle] pub extern "C" fn bloom_set_shadow_cascade_stagger(_p0: f64) -> f64 { 0.0 } +#[no_mangle] pub extern "C" fn bloom_set_far_shadow(_p0: f64, _p1: f64, _p2: f64, _p3: f64, _p4: f64, _p5: f64, _p6: f64) -> f64 { + 0.0 +} +#[no_mangle] pub extern "C" fn bloom_far_shadow_stat(_p0: f64) -> f64 { + 0.0 +} #[no_mangle] pub extern "C" fn bloom_set_draw_shadow_casting(_p0: f64) -> f64 { 0.0 } diff --git a/native/web/src/render_settings.rs b/native/web/src/render_settings.rs index 2a9467aa..bce2acf1 100644 --- a/native/web/src/render_settings.rs +++ b/native/web/src/render_settings.rs @@ -105,6 +105,24 @@ pub fn bloom_set_shadow_filter_4tap(on: f64) -> f64 { 1.0 } #[wasm_bindgen] +#[allow(clippy::too_many_arguments)] +pub fn bloom_set_far_shadow(cx: f64, cz: f64, half_x: f64, half_z: f64, y_min: f64, y_max: f64, refresh: f64) -> f64 { + engine().renderer.set_far_shadow( + cx as f32, + cz as f32, + half_x as f32, + half_z as f32, + y_min as f32, + y_max as f32, + refresh.max(1.0) as u32, + ); + 1.0 +} +#[wasm_bindgen] +pub fn bloom_far_shadow_stat(which: f64) -> f64 { + engine().renderer.far_shadow_stat(which as u32) +} +#[wasm_bindgen] pub fn bloom_set_shadow_cascade_stagger(on: f64) -> f64 { engine().renderer.set_shadow_cascade_stagger(on != 0.0); 1.0 diff --git a/package.json b/package.json index e9fae9b4..f513e9f7 100644 --- a/package.json +++ b/package.json @@ -2094,6 +2094,26 @@ ], "returns": "f64" }, + { + "name": "bloom_set_far_shadow", + "params": [ + "f64", + "f64", + "f64", + "f64", + "f64", + "f64", + "f64" + ], + "returns": "f64" + }, + { + "name": "bloom_far_shadow_stat", + "params": [ + "f64" + ], + "returns": "f64" + }, { "name": "bloom_set_shadow_cascade_stagger", "params": [ diff --git a/src/core/index.ts b/src/core/index.ts index a9f8bcc7..2d3722a2 100644 --- a/src/core/index.ts +++ b/src/core/index.ts @@ -41,6 +41,8 @@ declare function bloom_set_sharpen_strength(strength: number): number; declare function bloom_set_ssgi_probe_spacing(pixels: number): number; declare function bloom_set_shadow_distance(metres: number): number; declare function bloom_set_shadow_cascade_stagger(on: number): number; +declare function bloom_set_far_shadow(cx: number, cz: number, halfX: number, halfZ: number, yMin: number, yMax: number, refreshFrames: number): number; +declare function bloom_far_shadow_stat(which: number): number; declare function bloom_set_texture_streaming(budgetMb: number): number; declare function bloom_set_shadow_filter_4tap(on: number): number; declare function bloom_set_shadow_map_size(texels: number): number; @@ -862,6 +864,32 @@ export function setShadowCascadeStagger(on: boolean): boolean { return bloom_set_shadow_cascade_stagger(on ? 1 : 0) !== 0; } +/** + * Static far sun shadows: sun shadows to the horizon over a large static world + * (a city seen from a hill or from the air) at almost no cost per frame. With + * it on, the third shadow cascade stops following the camera and becomes one + * fixed map over the rectangle reaching `halfX` and `halfZ` metres either side + * of (`centerX`, `centerZ`), holding the casters between `yMin` and `yMax`. It is + * drawn from the static casters only (moving characters keep their shadows in + * the two camera cascades, which now split `setShadowDistance` between them), + * and redrawn at most once every `refreshFrames` frames when the sun or the + * static casters changed; between redraws it costs one texture lookup per + * pixel beyond the shadow distance. Its sharpness is the cascade map size + * (`setShadowMapSize`) over the rectangle as the sun sees it: 4096 texels over + * 6 km is 1.5 m a texel. A half-extent `<= 0` turns it off. + */ +export function setFarShadow( + centerX: number, centerZ: number, halfX: number, halfZ: number, + yMin: number = -50, yMax: number = 300, refreshFrames: number = 30, +): boolean { + return bloom_set_far_shadow(centerX, centerZ, halfX, halfZ, yMin, yMax, refreshFrames) !== 0; +} + +/** Far-shadow statistics: 0 redraws so far, 1 casters in the last redraw, 2 frames since it. */ +export function farShadowStat(which: number): number { + return bloom_far_shadow_stat(which); +} + /** * Texture streaming: keep only the mip levels each texture needs at its * present distance, inside a budget of `budgetMb` megabytes (0 = off, the diff --git a/tools/validate-ffi.js b/tools/validate-ffi.js index aea23c4c..3da362f8 100644 --- a/tools/validate-ffi.js +++ b/tools/validate-ffi.js @@ -72,6 +72,8 @@ const RENDERER_STATUS_FUNCTIONS = new Set([ 'bloom_set_ssao_downscale', 'bloom_set_shadow_distance', 'bloom_set_shadow_cascade_stagger', + 'bloom_set_far_shadow', + 'bloom_far_shadow_stat', 'bloom_set_draw_shadow_casting', 'bloom_set_texture_streaming', 'bloom_set_shadow_filter_4tap',