Skip to content

Images: every atlas page keeps a CPU copy beside its GPU texture #163

Open

Description

@godismyjudge95

What happens

ImageAtlas copies each shown image into a page buffer (ImagePage::data, 4 MiB for a 1024x1024 page, or the image's own size for a larger one) and uploads the dirty rows to the page texture. The buffer stays for as long as the page lives. So every image a window shows costs three copies: the app's Pixels, the page buffer, and the texture. The glyph atlas does the same with its 2048x2048 pages.

Where

At dev de2b16de:

Measured

macOS 26.6 VM, Rune 7ead7d63 (these files are the same on dev), our screenshot app with its main window showing a grid of thumbnails.

  • With malloc stack logging, the large resident anonymous regions at idle are three 4 MiB regions from ImageAtlas::place (page buffers) and one 4 MiB region from text::atlas::Atlas::new (an overlay's glyph atlas). Together 16 of the 34 MB of untagged memory.
  • Over 10 hide and show cycles of the main window, the untagged memory and the private texture memory move together, in 4 MB steps (38 and 36 MB, then 42 and 40 MB, and back). One page buffer and one page texture per step.

So the CPU side of the image atlas is about as large as its GPU side. On a GPU with its own memory, the CPU copy is the part that counts as process memory.

Ask

Do not keep the page buffer after its rows are uploaded. Options, from the cheapest:

  1. Copy each new image's rows straight into the staging upload, and keep no page buffer.
  2. On a lost device or a new texture, upload again from the Pixels that the view still shows (the slots know them), not from a page copy.
  3. If a CPU copy must stay for some backend, make it opt-in.

Done when: a window that shows N MB of images in 1024 atlas pages holds no CPU page buffers after the frame that uploaded them, a lost-device test still draws the images after recovery, and the glyph atlas follows the same rule or the issue says why not.

Related: #148 (the app keeps its own copy because Pixels cannot be read back), #80 (memory policy).


Posted via Claude

Activity

  1. heto-m commented on Nov 8, 2026

    @heto-m

    Another data point: I show scanned A4 pages (2480×3508, ~35 MB RGBA each). With El::pixels the Pixels copy is also kept for as long as the image is in the view, so each page stays in RAM twice. Three pages on screen cost about 100 MB RSS more than uploading the same pages to my own wgpu::Texture from a Compositor and dropping the bytes.

    So for this case the Pixels copy matters as much as the page buffer. Something like an upload-once image that only keeps GPU memory (and asks the app for the bytes again after a lost device) would let me drop the compositor.

  2. hlimouni commented on Nov 13, 2026

    @hlimouni

    Another data point, from a document viewer at dev 8e1ae9ae on macOS (M2 Pro, release, headless at 2×), with a view showing PDF pages:

    • Whole pages as one Pixels each (about 1190×1684 px, 8 MB): each visible page is held as the Pixels copy, the standalone page's ImagePage::data and the texture, so 2–3 visible pages cost about 61 MB resident. Pages scrolling in and out also create and free a standalone texture each time.
    • 512 px tiles instead, so they share 1024² atlas pages: no more per-page textures, but the copies stay. Holding the tiles' Pixels without showing them costs little (peak 140 MB resident); showing them during a fast scroll took the peak to 444–499 MB. Most of the difference looked like upload staging: Staging keeps its chunks for reuse at the size of the busiest burst, and a fast scroll uploads several MB of new tiles a frame. A headless run isn't paced, so a window may peak lower; not measured.

    What the app could do itself was hold tiles back during a fling. That brought the same run to 229 MB. Anything from the ask above would cut the rest. One addition: a way to trim Staging after a burst (when the window rests, or app-callable; give_back already frees it) would bound the peak.

  3. kaeawc commented on Nov 24, 2026

    @kaeawc

    Another data point, for a streamed image rather than a static one: an Android emulator screen (1080×2400 RGBA, 10 MB a frame) shown through El::pixels and replaced with Pixels::set on every new frame (kaeawc/simee, desktop/src/android.rs).

    An image that size gets a standalone page the size of the image (ImagePage::new, dev pixels.rs#L452-L454), so the page buffer is a byte-for-byte copy of the Pixels buffer. Each frame then pays for it: the producer's buffer is copied into the page (place_at, #L344-L353), and the page's rows are copied into staging (render/upload.rs#L141-L168). With the producer's own copy that is about three full-frame copies and a fresh 10 MB Vec per frame, which we estimate at roughly 6 ms of CPU per frame (an estimate, not a profile).

    Option 1 here (stage rows straight from the image, keep no page buffer) would remove one of those copies per frame for this case as well as the retained memory. For a standalone page in particular there is nothing to pack, so its rows could be staged from the Pixels buffer directly.

  4. opencodejr commented on Nov 25, 2026

    @opencodejr
    Contributor

    Thanks for the detailed write-up and the measurements. This still holds on current dev, though the code has moved. ImagePage is now in crates/rune-image/src/shelf.rs and ImageAtlas in crates/rune-image/src/pixels.rs. Since your report, shelf pages start small (#701), and #2103/#2102 dealt with empty or rested pages. A page that still shows an image keeps its full data buffer.

    A few things that read data today and would need another source if the buffer goes away:

    • Renderer::upload_images (crates/rune/src/render/upload.rs) uploads whole pages from it again for a new texture or after a lost device.
    • ImagePage::shrink_to_fit_rows truncates it, then marks the whole page dirty for upload again.
    • The CPU renderer (crates/rune/src/render/cpu_boot.rs, Scene::new) samples ImagePage::data and the glyph atlas's page_data directly. That's the backend your option 3 would cover.

    Your option 1 (stage the rows from the Pixels frame, with the slots re-uploading from their Pixels after a loss) fits the follow-up comments too. In particular, a standalone page is a byte-for-byte copy of its Pixels. We'll look at making the change against the "done when" criteria you listed.

  5. opencodejr commented on Nov 25, 2026

    @opencodejr
    Contributor

    I looked into making this change and haven't opened a PR. It needs a few design decisions before the work can be split up, so here's what I found on current dev:

    • Uploads only take full rows. Staging::write_texture_rows (and the Metal DirectTexture hook) writes whole page-width rows. Several images share those rows on a 1024 shelf page, so uploading without the page buffer would zero out the other images. Option 1 first needs rectangle writes in rune-staging, including its Metal path.
    • Full re-uploads use the buffer. Renderer::upload_images uses it after a lost device or for a new texture. When a shelf page grows (ImagePage::allocate) or shrinks (shrink_to_fit_rows), the whole page is also marked dirty. Option 2 can rebuild from the slots, but a slot only holds a Weak reference to its Pixels. We'd need to decide what a slot whose Pixels was dropped draws after recovery, and how animated or mip-level slots rebuild their bytes.
    • The CPU first-frame painter (render/cpu_boot.rs, Scene::new) samples ImagePage::data, and the GPU's first frame uploads those same bytes at the hand-over. That's the CPU copy option 3 would make opt-in.
    • The glyph atlas keeps its CPU copy for masks filled on the CPU (FilledOnCpu and refill_gpu_masks in rune-text/src/atlas/fill.rs), so it can't follow the same rule without a different path for those masks.

    A smaller step would be for a standalone (larger than 1024) page to hold a reference to the Pixels frame instead of copying it. That removes the extra copy for whole-page images and streamed frames like the emulator case, but it doesn't help 1024 shelf pages. I'm leaving this open with these notes so the approach can be agreed first.

  6. godismyjudge95 commented on Nov 27, 2026

    @godismyjudge95
    Author

    A current number from dev dacf5d89 (macOS 26.6 VM, native Metal backend, our screenshot app, no patches). With the main window hidden in the tray, malloc stack logging finds 10.5 MB of ImagePage buffers (rune-image/src/shelf.rs#L176) in 4 regions (ImagePage::allocate 8.0 MB, ImagePage::new 2.5 MB). After a region capture and a full-screen capture it is 12.0 MB in 4 regions. The GPU textures of these pages are gone at hide (#161), so only the CPU copies stay.

    In our case most of these pages stay because the last view of the hidden window still holds the images (#2675). If Rune drops those, the pages can go at hide before the larger change that this issue describes.


    Posted via Claude

  7. Hona commented on Nov 28, 2026

    @Hona
    Member

    Thank you @godismyjudge95 for the fresh Mac malloc stack measurement on dacf5d89!

    Merged #2688 (2173df2f) on dev to eliminate retained ImagePage::data CPU shadow buffers on GPU windows and overlays:

    • Direct sub-rectangle GPU uploads: added Staging::write_texture_rect (plus DirectTextureRect and Metal replace_texture_rect / write_texture_rect_in_place for idle Apple Silicon shared textures) so individual slots upload straight from Pixels / decoded mips without needing a full-page CPU row buffer.
    • Zero CPU page buffers in GPU mode: added ImageAtlas::set_backing(Backing::Gpu) / release_cpu_pages. GPU-first windows and overlays switch to Backing::Gpu before frame 0 (so ImagePage::new and ImagePage::allocate allocate 0 bytes for page.data), and Renderer::upload_images drops any retained CPU page buffers after uploading.
    • Safe resize, hide/wake, device-loss recovery, and CPU painting: shelf growth, rest() shrinking, give_back(true) hide/wake, invalidate(), and staging.lost() re-upload live slots directly from their Pixels handles (Slot::live) plus any in-frame pending snapshots. cpu_boot::Scene still borrows ImagePage::data with zero allocation during CPU-boot (and Windows handover keeps it until SurfaceMode::Handed commits) and reconstructs pages on demand via ImageAtlas::read_page_rgba if called after GPU upload.
    • Note on glyph atlas: the glyph atlas still retains its CPU copy for CPU-filled Windfoil masks (FilledOnCpu / refill_gpu_masks in rune-text/src/atlas/fill.rs), so this PR specifically removes the ImagePage CPU buffers.

    Verified locally (Windows DX12/wgpu + wasm32 check):

    • rune-staging: sub-rectangle uploads with unaligned widths preserve same-row neighbors.
    • rune-image: Backing::Gpu retains 0 bytes in ImagePage::data across placement, growth, incremental updates, rest() shrinking, invalidate() rebuilds, and bound enforcement.
    • rune: GPU upload drops CPU page buffers to 0 bytes while preserving same-row neighbors, shelf growth, hide/wake, lost-device recovery, and on-demand cpu_boot::Scene painting; wasm32-unknown-unknown compiles cleanly.

    Not run locally: macOS / native Metal hardware (replaceRegion sub-rect direct path and Mac malloc stack logging).

    Leaving this issue open pending your Mac malloc rerun on 2173df2f (or later dev) to confirm the 10.5–12.0 MB of ImagePage allocations are gone!

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