From 660407fa58d03fa6e41fe2edc7d4ae3658841233 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 23 Oct 2026 13:22:10 +0200 Subject: [PATCH] Metal: cull only frames with 4,096 section draws or more On Apple's GPUs the passes the test adds cost a fixed share of the frame (it is stored and reloaded at each), which the hidden sections repay only at high render distances: at 12 culling cost an M4 Pro 0.05 ms of GPU time a frame, at 32 it saved 0.25 to 1.6 ms. Other backends keep culling every frame; HORIZON_GPU_CULL=all culls every frame on Metal too. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/gfx.rs | 14 ++++++++++---- crates/horizon-client/src/gfx/cull.rs | 10 ++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index 91da4f616..022d14dcd 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -2146,8 +2146,8 @@ impl Renderer { .features() .contains(wgpu::Features::INDIRECT_FIRST_INSTANCE); // HORIZON_GPU_CULL=0 turns occlusion culling off, `all` culls every - // frame however little it hides, `shader` tests in the shader as on - // Metal (A/B checks). + // frame however few its draws and little it hides, `shader` tests in + // the shader as on Metal (A/B checks). let cull_env = std::env::var("HORIZON_GPU_CULL"); let cull = cull::Cull::supported(&adapter, &device) .filter(|_| cull_env.as_deref() != Ok("0")) @@ -2159,6 +2159,9 @@ impl Renderer { let mut cull = cull::Cull::new(&device, config.format.remove_srgb_suffix(), depth_test); cull.always = cull_env.as_deref() == Ok("all"); + if !cull.always && adapter.get_info().backend == wgpu::Backend::Metal { + cull.min_draws = cull::TILED_MIN_DRAWS; + } cull }); let depth = Self::depth(&device, &config, cull.as_ref()); @@ -3148,11 +3151,14 @@ impl Renderer { .inverse() .transform_point3(Vec3::ZERO); // Occlusion culling leaves the sections its last result did not see - // to phase 2, after the others, with instance counts of 0. + // to phase 2, after the others, with instance counts of 0. Whether + // the frame has draws enough goes by the last one's. let mut cull = self .cull .as_mut() - .filter(|_| self.cull_on && !frame.title && self.multi_draw) + .filter(|c| { + self.cull_on && !frame.title && self.multi_draw && self.section_draws >= c.min_draws + }) .and_then(|cull| cull.begin(&self.device).then_some(cull)); self.phase2_draws.clear(); self.phase2_pages.clear(); diff --git a/crates/horizon-client/src/gfx/cull.rs b/crates/horizon-client/src/gfx/cull.rs index 77574e850..57049eb4d 100644 --- a/crates/horizon-client/src/gfx/cull.rs +++ b/crates/horizon-client/src/gfx/cull.rs @@ -46,6 +46,13 @@ const RETEST: u32 = 8; /// more than they save. Else one frame in `PROBE` tests every section. const MIN_SHARE: f32 = 0.1; const PROBE: u32 = 64; +/// On Metal, section draws from which a frame culls. Apple's GPUs store and +/// reload the whole frame at each of the passes the test adds, a fixed cost +/// that the sections it hides repay only at high render distances: on an M4 +/// Pro at 1080p and render distance 12 (1,000–1,600 draws) culling added +/// about 0.05 ms of GPU time while hiding 15–30% of the solid indices; at +/// 32 (drawn flat, over this many draws) it saved 0.25–1.6 ms. +pub const TILED_MIN_DRAWS: usize = 4096; /// Boxes over more than this share of the screen count as seen untested. const BIG: f32 = 0.25; /// Results read back at once, some frames late so the CPU never waits. @@ -141,6 +148,8 @@ pub struct Cull { pub hidden: ([usize; 2], f32), /// Cull every frame, whatever the hidden share (A/B checks). pub always: bool, + /// Frames with fewer section draws than this do not cull. + pub min_draws: usize, } fn buffer( @@ -337,6 +346,7 @@ impl Cull { stamp: 0, hidden: ([0; 2], 1.0), always: false, + min_draws: 0, } }