Repository navigation
Description
Problem
When the GPU renders the loupe, every frame makes a round trip through the CPU before it reaches the screen:
lightcraft_gpumakes its own wgpu instance and device (crates/gpu/src/ctx.rs, around lines 286-313).- Each render is read back by
finish_and_read(ctx.rs, around line 535). It copies into a staging buffer, callsmap_asyncand waits withpoll(Wait). The result becomes anRgba8buffer. - On the UI thread,
color_image()(crates/ui-egui/src/render.rs, around line 93) converts it pixel by pixel. Thentex.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.rspicks 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 thatlightcraft_gpuneeds 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
mattsivak commentedon Oct 12, 2026
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.
LR-VIEW-ZOOM: window textures are opt-in (LIGHTCRAFT_GPU_TEXTURES=1):…
mattsivak commentedon Oct 12, 2026
Two PRs for this:
LIGHTCRAFT_GPU_TEXTURES=1): a device has one queue, and the window's frames wait behind renders there. In 1:1 drags on a 61 MP raw, the shared device dropped 0–4 % of frames (worst 33–131 ms), where renders on their own device dropped none. The PR also handles a lost shared device: the app saves and offers a restart instead of crashing inside egui-wgpu.Measurement details are in both PR descriptions.
LR-VIEW-ZOOM: Help ▸ Debug ▸ Show Frame Rate, the display the storyto…
Revert "LR-VIEW-ZOOM: Help ▸ Debug ▸ Show Frame Rate, the display the s…
LR-VIEW-ZOOM: the lost-device dialog is translated in every language (s…
LR-VIEW-ZOOM: the lost-device dialog keeps the UI language (storytold…
LR-VIEW-ZOOM: renders on the window's device come back as textures, n…
LR-VIEW-ZOOM: zoom windows reach the screen as GPU textures, no readb…
LR-VIEW-ZOOM: window textures are opt-in (LIGHTCRAFT_GPU_TEXTURES=1):…
LR-VIEW-ZOOM: the lost-device dialog is translated in every language (s…
LR-VIEW-ZOOM: the lost-device dialog keeps the UI language (storytold…
LR-VIEW-ZOOM: merge main into storytold#1228 (conflicts resolved)
LR-VIEW-ZOOM: renders on the window's device come back as textures, n…
LR-VIEW-ZOOM: zoom windows reach the screen as GPU textures, no readb…
LR-VIEW-ZOOM: window textures are opt-in (LIGHTCRAFT_GPU_TEXTURES=1):…
LR-VIEW-ZOOM: the lost-device dialog is translated in every language (s…
LR-VIEW-ZOOM: the lost-device dialog keeps the UI language (storytold…