Skip to content

Loupe: hand GPU renders to egui without a CPU readback (share the wgpu device) #1228

Open

Description

@mattsivak

Problem

When the GPU renders the loupe, every frame makes a round trip through the CPU before it reaches the screen:

  1. lightcraft_gpu makes its own wgpu instance and device (crates/gpu/src/ctx.rs, around lines 286-313).
  2. Each render is read back by finish_and_read (ctx.rs, around line 535). It copies into a staging buffer, calls map_async and waits with poll(Wait). The result becomes an Rgba8 buffer.
  3. On the UI thread, color_image() (crates/ui-egui/src/render.rs, around line 93) converts it pixel by pixel. Then tex.set() / ctx.load_texture (render.rs, around lines 729-739) uploads it to egui. egui runs on eframe's own wgpu device, a different one (eframe 0.36; apps/lightcraft/src/window_gpu.rs picks Wgpu, with Glow as the fallback).

At 1:1 on a large raw this costs more than the render does. Measured on PR #1009 (benchmark comment) on an Apple M3 Max, with a 61 MP Sony raw at 1:1 in a 2800x1700 Retina viewport:

  • The 1:1 window is 3840x2816, about 43 MB of RGBA per tick.
  • The whole-image GPU jobs take 7-9 ms (median).
  • Complete texture updates take 18.3-30 ms (median). 0 of 234 met the 16 ms slider budget.

Most of the difference is the readback, the conversion and the upload.

Proposal

When the window renders with wgpu, give lightcraft_gpu eframe's device and queue (from egui_wgpu::RenderState) instead of creating its own, so both use the same adapter. Render the loupe into a wgpu texture that stays on the GPU, and pass it to egui with egui_wgpu::Renderer::register_native_texture (or update_egui_texture_from_wgpu_texture when the size has not changed). That removes the readback, the per-pixel conversion and the upload.

Considerations

  • Device sharing: only possible when the window uses wgpu. The GPU context then has to be built from eframe's RenderState, after the window exists (related: Show a window before the GPU device is created, so a driver that blocks there doesn't leave a black "not responding" window #410). The adapter, features and limits that lightcraft_gpu needs must fit what eframe requested, or eframe has to request them.
  • The Glow window renderer and wasm keep the current readback path unchanged.
  • Histogram: it is computed from the CPU pixels today. It needs a GPU reduction, or a small readback such as a downscaled image or the bins.
  • Pixels still needed: keep_pixels, ui.screenshot, headless snapshots and export need CPU pixels. They keep the readback, on demand.
  • Never crash: device loss or a failed texture registration falls back to the CPU readback path with an error that can be logged. No panics, and texture ids are freed when the loupe image changes.
  • Offload path: renders that are not on the local GPU (CPU fallback, offload) keep using load_texture.
  • Threading: renders run off the UI thread. The texture handoff and registration must be synchronized with egui's frame, for example by registering on the UI thread from a handle the worker sends back.

Done when

  • A 1:1 slider drag on a 61 MP raw in a Retina viewport on Apple Silicon delivers complete texture updates within 16 ms (median), measured the same way as on LR-VIEW-ZOOM: slider drags stay sharp where the GPU keeps up (#330) #1009.
  • The Glow renderer and wasm behave as before.
  • Tests cover the native-texture path and the fallback to readback (device lost, Glow), and screenshots / headless snapshots still produce pixels.

Activity

  1. mattsivak commented on Oct 12, 2026

    @mattsivak
    ContributorAuthor

    I'll look into this later. If anyone has time before then, feel free to take it. Please post a comment here first saying you're on it, so we don't both work on it.

  2. mattsivak commented on Oct 12, 2026

    @mattsivak
    ContributorAuthor

    Two PRs for this:

    Measurement details are in both PR descriptions.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions