Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix(render): stop recording the unused Hi-Z pyramid
draw_into built six compute passes per frame for a pyramid no pass
samples. That was the allocation jump past the #269 floor. The pyramid
stays off until sample_hiz is set. Lavapipe samples are back in
100-120. The passes that still record keep the wgpu-core command lists.

Co-authored-by: Andrew Brown <lazyxeon@users.noreply.github.com>
  • Loading branch information
cursoragent and lazyxeon committed Oct 20, 2026
commit 862331002edef1a0d4f415c81bff2aebe03754c9
17 changes: 13 additions & 4 deletions astraweave-render/src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1047,8 +1047,12 @@ pub struct Renderer {
mesh_external: Option<Mesh>,
/// Named models for multi-model rendering (terrain, trees, rocks, etc.)
models: std::collections::HashMap<String, RenderModel>,
/// One Hi-Z pyramid. SSR, SSGI, and occlusion sample its R32Float copy.
/// One Hi-Z pyramid. Built only when [`Self::sample_hiz`] is set, because
/// each mip is its own wgpu compute pass (#269).
hiz: Option<crate::hiz_pyramid::HizPyramid>,
/// A recorded pass will sample [`crate::hiz_pyramid::HizPyramid::view`].
/// SSGI reads scene depth directly, so this stays false until that wiring exists.
sample_hiz: bool,
/// GL rejects `textureLoad` on `texture_depth_2d`, so the pyramid stays off.
hiz_depth_load: bool,
/// Presentation-only particle sim. `None` until an emitter is enabled.
Expand Down Expand Up @@ -3755,6 +3759,7 @@ fn vs(input: VSIn) -> VSOut {
mesh_external: None,
models: std::collections::HashMap::new(),
hiz: None,
sample_hiz: false,
hiz_depth_load,
particle_sim: None,
particle_draw: None,
Expand Down Expand Up @@ -8089,9 +8094,7 @@ fn vs(input: VSIn) -> VSOut {
self.open_timing_gap(enc, "after_post");

self.close_timing_gap(enc);
let hiz_stamp = self.begin_named_stamp(enc, "hiz");
self.record_hiz(enc, depth_view);
self.end_named_stamp(enc, hiz_stamp);
self.open_timing_gap(enc, "after_hiz");
self.close_timing_gap(enc);
let particle_stamp = self.begin_named_stamp(enc, "particles");
Expand Down Expand Up @@ -8326,7 +8329,11 @@ fn vs(input: VSIn) -> VSOut {
enc: &mut wgpu::CommandEncoder,
depth_view: Option<&wgpu::TextureView>,
) {
if !self.hiz_depth_load {
if !self.hiz_depth_load || !self.sample_hiz {
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
// Nothing in draw_into reads HizPyramid::view. SSGI samples the
// scene depth. Building the chain anyway is one wgpu command
// list per mip (#269): six passes at 64×64, about +108 allocs,
// and 0.275 ms on the 1660 Ti heavy scene.
return;
}
let width = self.config.width.max(1);
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

width.max(1) and height.max(1) turn a zero extent into 1×1, and Renderer::resize already accepts 1. HizPyramid::new then does (width.max(height) as f32).log2().ceil() as u32. log2(1) is 0, so this frame requests mip_level_count 0.

wgpu-core 25.0.2 rejects a zero mip count (InvalidMipLevelCount). If creation returned, the copy bind group indexes mip_views[0] on an empty range. hiz_rebind is 64×64 and sample_hiz defaults false, so the lavapipe gates do not reach it.

The phase plan's follow-up is to call set_sample_hiz(true) when a pass starts sampling. That call on a 1×1 target faults. Clamp the mip count with .max(1) in HizPyramid::new. ceil(log2(1024)) stays 10, which hiz_mip_count_power_of_two already asserts, and 1×1 becomes one copy pass. #400 lists this next to the uncapped sync reads; the fault is on the enable path this PR adds.

Expand All @@ -8345,9 +8352,11 @@ fn vs(input: VSIn) -> VSOut {
&source,
));
}
let stamp = self.begin_named_stamp(enc, "hiz");
if let Some(pyramid) = self.hiz.as_ref() {
pyramid.build(enc);
}
self.end_named_stamp(enc, stamp);
}

pub fn surface_size(&self) -> (u32, u32) {
Expand Down
22 changes: 10 additions & 12 deletions astraweave-render/tests/frame_alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,19 +111,17 @@ fn draw_into_allocates_nothing_after_warmup() {
eprintln!("draw_into alloc samples: {samples:?}");
let min = *samples.iter().min().unwrap();
let max = *samples.iter().max().unwrap();
// #269 measured this scene at 110-120 before the Hi-Z pyramid
// (samples [113, 111, 111, 110, 111, 110]). `BasePass::new` in
// wgpu-core 25 allocates a command Vec and a label String for every
// render or compute pass. The pyramid at 64×64 has
// ceil(log2(64)) = 6 passes (one depth copy plus one downsample per
// coarser mip). wgpu barriers only at pass boundaries, so those mips
// cannot share a pass. 6 passes × ~18 allocations is about +108, which
// is the jump to [219, 217, 217, 216, 217, 216]. The band is that
// sum, not a silent widen of the old 100-120 gate. A new pass moves
// the band. Engine scratch stays at zero (`update_instances` above).
// Measured after the unused Hi-Z chain stopped recording:
// [119, 116, 115, 116, 115, 115]. `draw_into` does not build the
// pyramid unless a pass samples `HizPyramid::view` (`sample_hiz`).
// Nothing does. The six mip passes were the jump to ~217. What
// remains is wgpu-core 25 `BasePass::new` for the passes that still
// record (shadows, sky, cloud shadow, main, post). Upstream still
// allocates that Vec per pass. Engine scratch stays at zero
// (`update_instances` above). A new recorded pass moves the band.
assert!(
max <= 230 && min >= 200 && max - min <= 4,
"draw_into allocs left the explained Hi-Z floor: {samples:?}"
max <= 120 && min >= 100 && max - min <= 6,
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
"draw_into allocs left the wgpu pass floor: {samples:?}"
);
});
}
4 changes: 2 additions & 2 deletions astraweave-render/tests/stream_fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ fn fallback_frame_does_not_allocate_before_the_texture_is_resident() {
let min = *samples.iter().min().unwrap();
let max = *samples.iter().max().unwrap();
assert!(
max <= 230 && min >= 200 && max - min <= 4,
"fallback frame left the wgpu recording floor: {samples:?}"
max <= 120 && min >= 100 && max - min <= 6,
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
"fallback frame left the wgpu pass floor: {samples:?}"
);

go_tx.send(()).expect("release writer");
Expand Down
Loading
Loading