Skip to content

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

Done

Description

@wmfeht

Tested on dev at 452fd21ae6faced35a6cb296bc0d7dc7b100450b.

Problem

When a window rests (idle) or becomes hidden/covered (all == true), Host::give_back(&mut self, all: bool) (crates/rune/src/window/host/lifecycle.rs:778-802) frees CPU image buffers via rt.images.rest() (:782) and then asks the renderer to reclaim GPU memory via renderer.give_back(all, ...) (:793) followed (when all == true) by an empty queue submit + poll(wait_indefinitely) + renderer.trim_uploads() (:796-800).

However, Renderer::give_back() (crates/rune/src/render/layers.rs:301-323) never prunes or clears self.images (Vec<Option<(wgpu::Texture, wgpu::BindGroup)>>):

  • self.images textures are only dropped on the next rendered frame inside Renderer::upload_images() (crates/rune/src/render/upload.rs:154-181), or via Renderer::clear_images() (crates/rune/src/render/upload.rs:187-196), which is currently only called on Wayland/Xwayland overlay hide (crates/rune/src/overlay/lifetime.rs:338, 349) and warmup (crates/rune/src/render/warmup.rs:185).
  • OverlayCore::give_back() (crates/rune/src/overlay/lifetime.rs:271-295) has the exact same omission (rt.images.rest() at :275 followed by renderer.give_back(...) at :286).
  • Because no frame is drawn while a window is resting or hidden/covered, large image/preview GPU textures (such as PDF pages or high-resolution photo previews) whose CPU ImagePage::data was already freed by rt.images.rest() stay resident in VRAM and GPU driver memory until the user returns and triggers a redraw.

Expected behavior

  • In Host::give_back and OverlayCore::give_back, right after rt.images.rest(), prune renderer.images to match rt.images.pages (truncate to pages.len() and drop self.images[i] = None for any page.data.is_empty(), calling self.layers.clear_images() and self.images_changed() when anything was dropped) — and when all == true (hidden/covered), drop all image textures via renderer.clear_images() (matching overlay hide) before submitting/polling so VRAM is actually reclaimed while idle or hidden.

Activity

  1. opencodejr commented on Nov 25, 2026

    @opencodejr
    Contributor

    Thanks for the detailed report. I reproduced this at the renderer level.

    No GPU here, so I used wgpu's no-op backend (--features rune/test-gpu-noop). I couldn't run Host::give_back / OverlayCore::give_back directly because both need a live window. Instead, a test in crates/rune/src/render/image_tests.rs makes the same renderer calls in the same order as Host::give_back(true):

    1. Draw a frame that shows a 4×4 El::pixels image, then upload_images. The renderer holds an image texture.
    2. Draw a frame without the image, then upload_images again. The 1024 atlas page keeps its CPU data, so the texture stays.
    3. Run rt.images.rest(), then renderer.give_back(true, ..) and renderer.trim_uploads().

    Result:

    after last frame: pages=1 gpu_images=true
    after rest + give_back(true): cpu pages=0 gpu_images=true
    

    ImageAtlas::rest frees the page, but Renderer::give_back leaves Renderer::images alone. The texture is only dropped by the next upload_images (a drawn frame) or by clear_images, which today only runs on Wayland/Xwayland overlay hide and in warmup. So a resting or hidden window keeps that VRAM until it draws again.

  2. opencodejr commented on Nov 25, 2026

    @opencodejr
    Contributor

    I opened a fix. When a window or overlay rests, a new Renderer::rest_images now runs right after rt.images.rest() in both Host::give_back and OverlayCore::give_back. It drops the GPU textures of image pages the atlas just freed, before the existing submit/poll, so that memory is released while the window is idle or hidden.

    One difference from your proposal: pages still on screen keep their textures, even on GiveBackAll. Clearing everything when covered would mean re-uploading every visible image on return, so I left that choice to the maintainers in the PR.

    Tested with a new image test on wgpu's no-op backend (no GPU available here, so real VRAM use wasn't measured), plus the rune lib test suite.

    #2108

  3. Hona commented on Nov 25, 2026

    @Hona
    Member

    Renderer::give_back previously left Renderer::images and layer image bind groups untouched after ImageAtlas::rest(), so GPU image textures for freed pages at idle rest and all image pages while hidden or covered stayed resident in VRAM until the next drawn frame. This is now fixed on dev in #2126 (�46ddbc12): in a two-page headless GPU test (1024×128 shelf page + 1025×64 standalone page, 786,688 B total), idle rest drops the emptied shelf page from 2 textures (786,688 B) to 1 texture (262,400 B), hiding drops all image textures to 0 textures (0 B), and the first frame after waking re-uploads and draws pixel-identically.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions