From 1269bbde2b348c32d54f70d1d5ceceee45eaca4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ralph=20K=C3=BCpper?= Date: Sat, 21 Nov 2026 18:17:39 +0100 Subject: [PATCH 1/3] perf(scene): do not submit GPU-driven draws the CPU frustum test rejected; BLOOM_DRAW_STATS=1 prints draw counts In the Nineveh bazaar view 16,686 draws were submitted per pass and ~1,500 were in view; the rest were zero-count indirect draws Metal still encodes and issues in the depth prepass and the main pass. Mac: bazaar 53.2/45.1 -> 46.9/41.4 ms, gate 31.5 -> 29.9, low air 31.4 -> 29.7 (noisy machine). --- native/shared/src/renderer/gpu_driven.rs | 18 ++++++++++++++++++ native/shared/src/scene/draw_lists.rs | 14 +++++++++----- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/native/shared/src/renderer/gpu_driven.rs b/native/shared/src/renderer/gpu_driven.rs index e75c02ad..eca876ab 100644 --- a/native/shared/src/renderer/gpu_driven.rs +++ b/native/shared/src/renderer/gpu_driven.rs @@ -508,6 +508,7 @@ impl GpuDrivenRenderer { frustum_visible_oracle, frustum_culled_oracle, }; + draw_stats_log(&self.stats); if !self.enabled || self.draw_scratch.is_empty() { return; } @@ -996,6 +997,23 @@ impl Renderer { } } +/// `BLOOM_DRAW_STATS=1`: one line every 120 frames with how many retained draws went GPU-driven and how many +/// took the compatibility path, to see where a scene's draw-call cost comes from. +fn draw_stats_log(stats: &SubmissionStats) { + static ON: OnceLock = OnceLock::new(); + static FRAME: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0); + if !*ON.get_or_init(|| std::env::var("BLOOM_DRAW_STATS").is_ok_and(|v| v != "0")) { + return; + } + let f = FRAME.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + if f % 120 == 0 { + println!( + "bloom draws: gpu-driven records {} (indirect calls {}), compatibility {}, frustum visible {} culled {}", + stats.submitted, stats.indirect_calls, stats.compatibility, stats.frustum_visible_oracle, stats.frustum_culled_oracle + ); + } +} + fn gpu_driven_forced_off() -> bool { static OFF: OnceLock = OnceLock::new(); *OFF.get_or_init(|| { diff --git a/native/shared/src/scene/draw_lists.rs b/native/shared/src/scene/draw_lists.rs index 40a6a0a4..332bd50c 100644 --- a/native/shared/src/scene/draw_lists.rs +++ b/native/shared/src/scene/draw_lists.rs @@ -466,6 +466,14 @@ impl SceneGraph { compatibility += u32::from(node.in_view_frustum); continue; }; + // The CPU frustum test above is conservative (world AABB against the planes), so a node it + // rejects cannot be visible. Submitting it anyway cost a whole draw call per pass on backends + // without an indirect count (Metal): the cull compute only zeroes its instance count. A city + // view of 16,600 retained nodes with 1,500 in view issued 33,000 empty draws a frame. + if !node.in_view_frustum { + culled += 1; + continue; + } let offset = slot as usize * NODE_UNIFORM_STRIDE as usize; let end = offset + std::mem::size_of::(); let Some(bytes) = self.scratch.get(offset..end) else { @@ -494,11 +502,7 @@ impl SceneGraph { node.gpu_material_id.raw(), ], }); - if node.in_view_frustum { - visible += 1; - } else { - culled += 1; - } + visible += 1; } [compatibility, visible, culled] } From 54d0a8c1e13f4d3698b0a252abb1909f5e211131 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ralph=20K=C3=BCpper?= Date: Sat, 21 Nov 2026 18:24:12 +0100 Subject: [PATCH 2/3] perf(shadows): scene nodes cast with their active level of detail (WIP: not yet built or measured) --- native/shared/src/renderer/shadow_pass.rs | 25 +++++++++++++++++------ native/shared/src/scene.rs | 22 ++++++++++++++++++-- 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/native/shared/src/renderer/shadow_pass.rs b/native/shared/src/renderer/shadow_pass.rs index 9d8e6537..345c973f 100644 --- a/native/shared/src/renderer/shadow_pass.rs +++ b/native/shared/src/renderer/shadow_pass.rs @@ -305,8 +305,17 @@ impl Renderer { { continue; } - let Some(vb) = &node.gpu_vb else { continue }; - let Some(ib) = &node.gpu_ib else { continue }; + // A node drawn at a simpler level of detail casts with that level too: the full + // mesh of a distant tree went into every cascade at its close-up triangle count. + let lod = node.active_lod_geometry(); + let (vb, ib, index_count, on_lod) = match lod { + Some((vb, ib, count)) => (vb, ib, count, true), + None => { + let Some(vb) = &node.gpu_vb else { continue }; + let Some(ib) = &node.gpu_ib else { continue }; + (vb, ib, node.gpu_index_count, false) + } + }; let vb_idx = shadow_vbs.len(); shadow_vbs.push(vb); shadow_ibs.push(ib); @@ -322,27 +331,28 @@ impl Renderer { }; let gpu_shared_draw = node .gpu_geometry + .filter(|_| !on_lod) .map(|slice| (slice.first_index, slice.base_vertex)); let world_transform = node.world_transform(); shadow_nodes.push(ShadowDrawEntry { vb_idx, ib_idx: vb_idx, index_start: 0, - index_count: node.gpu_index_count, + index_count, base_vertex: 0, transform: world_transform, wmin: node.world_bounds_min, wmax: node.world_bounds_max, cutout_idx, skinned: false, - sig: entry_sig(0, i as u64, node.gpu_index_count as u64, &world_transform), + sig: entry_sig(0, i as u64, index_count as u64, &world_transform), dynamic: false, joint_offset: 0.0, foliage: 0.0, gpu_opaque_shared: gpu_shared_draw.is_some() && cutout_idx < 0, gpu_index_start: gpu_shared_draw.map_or(0, |draw| draw.0), gpu_base_vertex: gpu_shared_draw.map_or(0, |draw| draw.1), - key: caster_id(0, i as u64, node.gpu_index_count as u64, &world_transform), + key: caster_id(0, i as u64, index_count as u64, &world_transform), }); } @@ -643,12 +653,15 @@ impl Renderer { { continue; } + let cast_count = node + .active_lod_geometry() + .map_or(node.gpu_index_count, |g| g.2); if node.cast_shadow && !prev_caster_ids.is_empty() && !prev_caster_ids.contains(&caster_id( 0, i as u64, - node.gpu_index_count as u64, + cast_count as u64, &node.world_transform(), )) { diff --git a/native/shared/src/scene.rs b/native/shared/src/scene.rs index 59692bdd..ce849cad 100644 --- a/native/shared/src/scene.rs +++ b/native/shared/src/scene.rs @@ -317,8 +317,9 @@ pub struct SceneNode { /// Reduced-detail geometry variants, ordered coarser and coarser /// (descending max_coverage). Selected per frame in prepare() by /// projected screen coverage; the base geometry above is "LOD 0". - /// Shadows, picking, BLAS, and SDF always use the base geometry — - /// LODs only affect the main camera rasterization. + /// The camera passes and the sun's shadow casters use the active level + /// (a distant tree cast its full close-up mesh into every cascade); + /// picking, BLAS and SDF always use the base geometry. pub lods: Vec, /// Active LOD this frame: -1 = base geometry, otherwise an index /// into `lods`. Driven by prepare(); render() binds accordingly. @@ -344,6 +345,23 @@ pub struct LodLevel { } impl SceneNode { + /// The level of detail drawn this frame: -1 for the base geometry, else an index into `lods`. + pub fn active_lod(&self) -> i32 { + self.active_lod + } + /// The active level's own GPU geometry (vertex buffer, index buffer, index count), or + /// None when the base geometry is drawn or the level is not uploaded. + pub fn active_lod_geometry(&self) -> Option<(&wgpu::Buffer, &wgpu::Buffer, u32)> { + if self.active_lod < 0 { + return None; + } + let lod = self.lods.get(self.active_lod as usize)?; + Some(( + lod.gpu_vb.as_ref()?, + lod.gpu_ib.as_ref()?, + lod.gpu_index_count, + )) + } /// Whether the depth prepass draws this opaque node, so that the main /// pass shades it with the depth-equal pipelines. Not with a fractional /// opacity: it blends over what lies behind it, and its prepass depth From e88a68c06934af73b9d775184e76c4c3cc6f34d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ralph=20K=C3=BCpper?= Date: Sat, 21 Nov 2026 22:55:39 +0100 Subject: [PATCH 3/3] perf(scene): reduced levels of detail live in the shared static-geometry arena A node on a reduced level now stays on the GPU-driven main/depth path and in the batched shadow casters (each level is uploaded to the arena once, deduplicated by geometry key, released with the level or the node). A node out of view or occluded keeps the level it was last drawn at instead of falling back to the base geometry: it still casts, and the reset flipped the casters' keys with every Hi-Z result, redrawing every cascade each frame. Nineveh Explorer, M1 Max, 1600x900, against the frustum-skip commit alone: bazaar 32.1 -> 29.8 ms, gate 23.0 -> 21.1 ms, low air 21.6 -> 21.4 ms. Pictures match (PSNR 42 to 54 dB; differences are distant casters' shadows). Co-Authored-By: Claude --- native/shared/src/renderer/shadow_pass.rs | 13 ++-- native/shared/src/scene.rs | 92 ++++++++++++++++++++++- native/shared/src/scene/draw_lists.rs | 4 +- native/shared/src/scene/helpers.rs | 4 +- 4 files changed, 100 insertions(+), 13 deletions(-) diff --git a/native/shared/src/renderer/shadow_pass.rs b/native/shared/src/renderer/shadow_pass.rs index 345c973f..9a455b99 100644 --- a/native/shared/src/renderer/shadow_pass.rs +++ b/native/shared/src/renderer/shadow_pass.rs @@ -308,12 +308,12 @@ impl Renderer { // A node drawn at a simpler level of detail casts with that level too: the full // mesh of a distant tree went into every cascade at its close-up triangle count. let lod = node.active_lod_geometry(); - let (vb, ib, index_count, on_lod) = match lod { - Some((vb, ib, count)) => (vb, ib, count, true), + let (vb, ib, index_count) = match lod { + Some((vb, ib, count)) => (vb, ib, count), None => { let Some(vb) = &node.gpu_vb else { continue }; let Some(ib) = &node.gpu_ib else { continue }; - (vb, ib, node.gpu_index_count, false) + (vb, ib, node.gpu_index_count) } }; let vb_idx = shadow_vbs.len(); @@ -329,10 +329,11 @@ impl Renderer { } None => -1, }; + // The arena slice of the same geometry (active level or base) batches the caster. let gpu_shared_draw = node - .gpu_geometry - .filter(|_| !on_lod) - .map(|slice| (slice.first_index, slice.base_vertex)); + .active_gpu_slice() + .filter(|(_, count)| *count == index_count) + .map(|(slice, _)| (slice.first_index, slice.base_vertex)); let world_transform = node.world_transform(); shadow_nodes.push(ShadowDrawEntry { vb_idx, diff --git a/native/shared/src/scene.rs b/native/shared/src/scene.rs index ce849cad..675bf82b 100644 --- a/native/shared/src/scene.rs +++ b/native/shared/src/scene.rs @@ -341,6 +341,12 @@ pub struct LodLevel { gpu_secondary_uv_vb: Option, gpu_ib: Option, gpu_index_count: u32, + /// This level's slice of the shared static-geometry arena, so a node on a + /// reduced level stays on the GPU-driven path and in the batched shadow + /// casters (deduplicated by geometry key, like the base geometry). + gpu_geometry: Option, + gpu_geometry_key: Option, + gpu_geometry_refused: bool, dirty: bool, } @@ -362,6 +368,16 @@ impl SceneNode { lod.gpu_index_count, )) } + /// The arena slice and index count of the geometry drawn this frame (the + /// active level, or the base geometry), for the GPU-driven and batched + /// shadow paths. None when that geometry is not in the arena. + pub(crate) fn active_gpu_slice(&self) -> Option<(GeometrySlice, u32)> { + if self.active_lod < 0 { + return self.gpu_geometry.map(|slice| (slice, self.gpu_index_count)); + } + let lod = self.lods.get(self.active_lod as usize)?; + lod.gpu_geometry.map(|slice| (slice, lod.gpu_index_count)) + } /// Whether the depth prepass draws this opaque node, so that the main /// pass shades it with the depth-equal pipelines. Not with a fractional /// opacity: it blends over what lies behind it, and its prepass depth @@ -680,6 +696,7 @@ impl SceneGraph { pub fn destroy_node(&mut self, handle: f64) { let mut geometry_key = None; + let mut lod_geometry_keys: Vec = Vec::new(); let mut resource_key = None; let mut material_key = None; if let Some(node) = self.nodes.get(handle) { @@ -694,10 +711,15 @@ impl SceneGraph { self.free_card_blocks.push(first); } geometry_key = node.gpu_geometry_key; + lod_geometry_keys = node + .lods + .iter() + .filter_map(|lod| lod.gpu_geometry_key) + .collect(); resource_key = node.gpu_resource_key; material_key = node.gpu_material_key; } - if let Some(key) = geometry_key { + for key in geometry_key.into_iter().chain(lod_geometry_keys) { release_scene_geometry( &mut self.shared_gpu_geometry, &mut self.retired_gpu_geometry, @@ -1037,7 +1059,14 @@ impl SceneGraph { }; if vertices.is_empty() { if lod_index < node.lods.len() { - node.lods.remove(lod_index); + let removed = node.lods.remove(lod_index); + if let Some(key) = removed.gpu_geometry_key { + release_scene_geometry( + &mut self.shared_gpu_geometry, + &mut self.retired_gpu_geometry, + key, + ); + } } return; } @@ -1052,6 +1081,9 @@ impl SceneGraph { gpu_secondary_uv_vb: None, gpu_ib: None, gpu_index_count: 0, + gpu_geometry: None, + gpu_geometry_key: None, + gpu_geometry_refused: false, dirty: true, }); } @@ -1658,6 +1690,12 @@ impl SceneGraph { release_scene_geometry(shared_gpu_geometry, retired_gpu_geometry, old_key); } node.gpu_geometry = None; + for lod in &mut node.lods { + if let Some(old_key) = lod.gpu_geometry_key.take() { + release_scene_geometry(shared_gpu_geometry, retired_gpu_geometry, old_key); + } + lod.gpu_geometry = None; + } } if node.indices().is_empty() { if let Some(old_key) = node.gpu_geometry_key.take() { @@ -1760,9 +1798,12 @@ impl SceneGraph { } } node.active_lod = chosen; - } else { + } else if node.lods.is_empty() || node.active_lod >= node.lods.len() as i32 { node.active_lod = -1; } + // Out of view or occluded, a node keeps the level it was last drawn at: it still + // casts into the shadow cascades, and resetting it to the base geometry made + // the casters' keys flip with every Hi-Z result, redrawing every cascade each frame. } else { // Empty or uninitialized bounds — can't cull, play safe. node.in_view_frustum = true; @@ -1776,6 +1817,11 @@ impl SceneGraph { || node.material.layered_pbr.has_resolved_tex_coord(1); for lod in &mut node.lods { if lod.dirty { + if let Some(old_key) = lod.gpu_geometry_key.take() { + release_scene_geometry(shared_gpu_geometry, retired_gpu_geometry, old_key); + } + lod.gpu_geometry = None; + lod.gpu_geometry_refused = false; if let Some(mesh) = lod.shared.as_ref() { let (vertex, index) = shared_mesh_cache.lod_buffers(device, mesh); lod.gpu_vb = Some(vertex); @@ -1815,6 +1861,46 @@ impl SceneGraph { } else if !needs_secondary_uv { lod.gpu_secondary_uv_vb = None; } + // The level's slice of the shared arena (see `LodLevel::gpu_geometry`). + if gpu_driven.enabled() + && gpu_driven_scene_active + && lod.gpu_geometry.is_none() + && !lod.gpu_geometry_refused + { + let (vertices, indices) = match lod.shared.as_ref() { + Some(mesh) => (&mesh.vertices[..], &mesh.indices[..]), + None => (&lod.vertices[..], &lod.indices[..]), + }; + if !vertices.is_empty() && !indices.is_empty() { + let key = match lod.shared.as_ref() { + Some(mesh) => shared_mesh_cache.key(mesh), + None => scene_geometry_key(vertices, indices), + }; + let slice = if let Some(cached) = shared_gpu_geometry.get_mut(&key) { + cached.references = cached.references.saturating_add(1); + Some(cached.slice) + } else { + let slice = gpu_driven.upload_static(device, queue, vertices, indices); + if let Some(slice) = slice { + shared_gpu_geometry.insert( + key, + SharedSceneGeometry { + slice, + references: 1, + }, + ); + } + slice + }; + match slice { + Some(slice) => { + lod.gpu_geometry = Some(slice); + lod.gpu_geometry_key = Some(key); + } + None => lod.gpu_geometry_refused = true, + } + } + } } let geometry_changed = node.geo_dirty; diff --git a/native/shared/src/scene/draw_lists.rs b/native/shared/src/scene/draw_lists.rs index 332bd50c..f44d405c 100644 --- a/native/shared/src/scene/draw_lists.rs +++ b/native/shared/src/scene/draw_lists.rs @@ -458,7 +458,7 @@ impl SceneGraph { compatibility += u32::from(node.in_view_frustum); continue; } - let Some(slice) = node.gpu_geometry else { + let Some((slice, index_count)) = node.active_gpu_slice() else { compatibility += u32::from(node.in_view_frustum); continue; }; @@ -496,7 +496,7 @@ impl SceneGraph { 0.0, ], draw: [ - node.gpu_index_count, + index_count, slice.first_index, slice.base_vertex as u32, node.gpu_material_id.raw(), diff --git a/native/shared/src/scene/helpers.rs b/native/shared/src/scene/helpers.rs index 86328845..363a73e5 100644 --- a/native/shared/src/scene/helpers.rs +++ b/native/shared/src/scene/helpers.rs @@ -10,7 +10,6 @@ pub(super) fn scene_node_gpu_driven_ready( imported_refraction_enabled: bool, ) -> bool { node.render_layer == 0 - && node.active_lod < 0 // Retained MASK/transparent nodes historically blend in submission // order while depth-writing in the same pass. A depth prepass would // collapse overlapping translucent layers to the nearest surface, @@ -22,7 +21,8 @@ pub(super) fn scene_node_gpu_driven_ready( && !(imported_refraction_enabled && node.material.transmission.is_active()) && !node.material.layered_pbr.is_active() && (!node.material.opacity.is_finite() || node.material.opacity >= 1.0) - && node.gpu_geometry.is_some() + // The geometry drawn this frame (base or active level) must be in the arena. + && node.active_gpu_slice().is_some() && node.gpu_material_id != MaterialId::FALLBACK && node.uniform_slot.is_some() }