Skip to content

fix(render): drop freed and hidden image atlas GPU textures in give_back - #2126

Merged
Hona merged 1 commit into
devfrom
fix/2102-give-back-image-textures
Nov 25, 2026
Merged

Hona merged 1 commit into
devfrom
fix/2102-give-back-image-textures

Conversation

@Hona

@Hona Hona commented Nov 25, 2026

Copy link
Copy Markdown
Member

Fixes #2102

Root cause

  • Host::give_back (crates/rune/src/window/host/lifecycle.rs) and OverlayCore::give_back (crates/rune/src/overlay/lifetime.rs) called rt.images.rest() (freeing CPU ImagePage::data and truncating trailing empty pages) and then renderer.give_back(all, ...) (crates/rune/src/render/layers.rs).
  • Renderer::give_back never updated self.images or cleared layer target t.images bind groups referencing those textures; image GPU textures were only dropped on the next upload_images draw call (or Wayland/Xwayland overlay hide / warmup). Because resting and hidden windows draw no frames, freed pages at idle rest and all image pages while hidden stayed resident in VRAM.

Changes

  • crates/rune/src/render/upload.rs: add Renderer::prune_images(&mut self, images: &ImageAtlas) to truncate self.images to images.pages.len() and drop any slot whose page has page.data.is_empty() or mismatched dimensions, clearing layer image bind groups (self.layers.clear_images()) and bumping image_generation (self.images_changed()) whenever anything drops so no layer target retains a reference to a freed texture.
  • crates/rune/src/render/layers.rs: pass images: &ImageAtlas into Renderer::give_back; call self.clear_images() when all == true (hidden/covered) and self.prune_images(images) when all == false (idle rest).
  • crates/rune/src/window/host/lifecycle.rs and crates/rune/src/overlay/lifetime.rs: pass &rt.images to renderer.give_back, right after rt.images.rest() and before GPU retirement/polling.
  • crates/rune/src/render/upload/tests.rs and crates/rune/src/overlay/rest_tests.rs: add headless real-GPU regression tests with test-gpu-counters covering idle rest (interior emptied page and trailing truncated page), hidden/covered GiveBackAll, layer image bind group release, ImageCache retention, and pixel-exact first-frame wake.
  • changelog/unreleased/2026-11-25-give-back-image-textures.md: changelog entry.

Before / after (2-page scene: 1024×128 shelf page + 1025×64 standalone page, small image hidden)

State Before (textures / bytes) After (textures / bytes)
Active before rest 2 / 786,688 B 2 / 786,688 B
Idle rest (GiveBackUnused, page 0 emptied) 2 / 786,688 B 1 / 262,400 B
Hidden / covered (GiveBackAll) 2 / 786,688 B 0 / 0 B
First frame after wake 1 / 262,400 B 1 / 262,400 B (pixel-exact)

Verification

  • Windows DX12 (test-gpu-counters): cargo test -p rune -p rune-mermaid -p rune-kit --features rune-kit/markdown,rune-kit/mermaid,rune/test-gpu-counters,rune-gpu/test-gpu-counters on render::upload::tests (13 passed), overlay::rest_tests (4 passed), and render::image_tests (13 passed); cargo clippy -p rune --lib --bins --tests --features rune/test-gpu-counters,rune-gpu/test-gpu-counters clean; cargo check-wasm clean.
  • macOS Metal (test-gpu-counters): cargo test -p rune --features rune/test-gpu-counters,rune-gpu/test-gpu-counters give_back (3 passed).
  • Linux aarch64: cargo check -p rune -p rune-kit --tests clean (compile check only; headless container has no GPU).
  • scripts/file-size.sh: 0 errors.

@Hona
Hona merged commit a46ddbc into dev Nov 25, 2026
1 check passed
@Hona
Hona deleted the fix/2102-give-back-image-textures branch November 25, 2026 05:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Host::give_back() and OverlayCore::give_back() free CPU ImageAtlas pages without dropping GPU Renderer::images textures

1 participant