Replies: 2 comments 2 replies
|
I like this feature, it's exactly what I need! How do you plan to implement the same functionality on Mac and Windows? |
|
I liked this proposal and tried to build on top of it. With AI assistance, I added experimental macOS and Windows implementations for the external wgpu compositor path in my fork. Roughly what this adds:
I have tested the demo locally on both macOS and Windows, and it is usable in my current experiments. This is still experimental code, and there are probably rough edges around broader GPU/driver coverage, more complex multi-slot cases, and long-running stress tests. But if anyone is interested in this direction or has a similar need, feel free to try the branch or use it as a reference: https://github.com/MSIsunny/zed/tree/feat/external-compositor check: |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Problem
GPUI applications that embed a domain renderer — a CAD viewport, a scientific/medical volume viewer, a map engine — currently have no zero-copy path to composite externally rendered GPU content into a GPUI window on the wgpu backends:
surface()/Window::paint_surfaceaccepts aCVPixelBufferand is macOS-only.ImageSource::Custom,img(),canvas()+paint_image) goes through the sprite atlas as CPU BGRA bytes, which means a GPU→CPU readback per frame for GPU-rendered content. For an interactive 3D viewport (e.g. 512³ volume raycasting at 60 fps) that readback and the extra frame of latency are the difference between viable and not.wgpu::Surfaceon the same window is not an option (swapchain exclusivity on Vulkan WSI), andGpuContext/WgpuContextare not exposed publicly, so an embedder can't even render on the same device.Material PR: #60573
Proposal
A backend-neutral external compositor API in
gpui, with an implementation ingpui_wgpu:ExternalCompositorRegistry(per window): registers slots described by anExternalSlotDescriptor(format, alpha mode, size in texels, sample count, graphics-context generation). Handles are generational (ABA-safe). The compositor object itself is stored as an opaqueBox<dyn Any>— gpui core carries no callback trait and no wgpu types; each backend defines its own trait and double-boxes it.ExternalCompositorElement+Window::paint_external_compositor: inserts a neutralExternalCompositorPrimitiveinto theScene, ordered like any other primitive (same shape as the macOSSurfacepath). An optional.background(color)paints behind the composited texture (visible while not ready, or on backends without support).gpui_wgpu: aWgpuExternalCompositortrait —compose(&mut self, slot, ctx: &mut WgpuCompositorBackendCtx<'_>) -> ExternalComposeOutputwith real lifetimes, zero unsafe — is called once per slot per frame at a controlled point (a dedicated encoder submitted before the main pass), and the returnedArc<wgpu::TextureView>(created on the shared device) is drawn by a small RGBA pipeline that follows the existing storage-instance idiom. GPUI keeps ownership of the onequeue.submitper frame, so ordering is correct by construction.SharedGpuContext), bumped on real device recreation and propagated to every registered compositor across all windows sharing the context; stale slots are skipped and apps re-register.Status / proof
Working implementation on a fork branch:
0xCarbon/zed#feat/external-compositor(+2186/−33 across 19 files, no newunsafe,cargo test -p gpui --lib216 green, checks clean on gpui/gpui_wgpu/gpui_linux/gpui_macos/gpui_windows). Includescrates/gpui/examples/external_compositor.rs— a live-updating procedural texture composited zero-copy — validated on Wayland + RADV (RX 7900 XTX), and honorsEXTERNAL_COMPOSITOR_EXIT_AFTERfor headless smoke runs.We (0xCarbon) are building CAD/geoscience workstations on GPUI and will maintain this feature on our fork regardless; we'd much rather land it upstream where the whole embedder ecosystem can use it. Draft PR with the full diff to follow — happy to reshape the API to fit your plans for gpui (naming, feature-gating the wgpu surface, splitting the PR, or aligning with any internal design you already have for external/video surfaces on the wgpu backends).
All reactions