Galaxy wgpu renderer
WgpuRenderer, a Smithay 0.7.0 renderer on wgpu 30.0.1, phases 1 and 2 part A (with the bind through a copy), the custom programs and the offscreen passes of phase 3 of
openspec/changes/wgpu-renderer-plan/design.md: offscreen targets,
solid colors and memory textures with the semantics of GlesRenderer, and on
wgpu's Vulkan backend dma-buf textures (ImportDma, ImportDmaWl, so ImportAll
and the surface tree elements work), dma-buf render targets (Bind<Dmabuf>) and
an exportable SYNC_FD fence. The compositor links it in every build, as its only
renderer, since step 10 of openspec/changes/archive/2026-11-10-gles-removal-plan/design.md (2026-11-11); it is no feature. src/effects/ holds the shared WGSL cores of the compositor's
plain scene (glass, mask, warp, finish, outline, black hole), moved here on 2026-11-10 when
the effects bridge that once shared them with the GLES scene was removed
(openspec/changes/archive/2026-11-10-gles-removal-plan/design.md, step 2); the compositor include_str!s them.
The renderer takes any wgpu device (WgpuRenderer::new(device, queue)) or opens
one on an adapter (WgpuRenderer::from_adapter); those have no dma-buf path
(for_render_node and for_render_node_gl open the devices that have one). On
the GL backend (wgpu::hal::gles::Adapter::new_external) every call must run on
a thread where that GL context is current.
A frame only appends vertices and draws; Frame::finish (or a drop) records them
into one render pass of one command encoder and submits once.
Self-test change on Vulkan devices with modifiers (untested)
Since the GL path landed, self_test exports a check buffer as implicit (modifier
Invalid) only when the format under test is Invalid; a buffer of a real modifier keeps
the modifier GBM reports (selftest::export_as). Before, every buffer except the LINEAR one
of the implicit path was exported as implicit, so on a device with
VK_EXT_image_drm_format_modifier every check of a real modifier failed with "GBM gave
...". On the implicit path (the only one that has ever run, an RX 550 on RADV) nothing
changes. On a GPU with modifiers the Vulkan self-test behaves differently from before, and
neither version has run there.
Custom programs (phase 3)
The extension point for the custom elements of the compositor
(openspec/changes/wgpu-renderer-plan/design.md, Phase 3 step 1).
A program is WGSL compiled after PROGRAM_PRELUDE, which gives it the
renderer's vertex stage (vs, VsOut with uv and the per-draw params),
the texture of a textured draw at @group(0) (tex, tex_sampler) and
smithay_texel (the red-blue swap and the no-alpha flag of the default
program). The program declares @fragment fn fs(in: VsOut) and one uniform
block at @group(1) @binding(0).
let program = renderer.compile_program(ProgramSource {
label: "window finish",
wgsl: FINISH_WGSL,
uniform_size: 128, // a multiple of 16, at most MAX_UNIFORM_SIZE
textured: true,
second_texture: false, // true: a second texture at @group(2), see below
repeat: false, // true: `tex` samples with REPEAT instead of CLAMP_TO_EDGE
})?; // keep it, for example in renderer.user_data()
// In RenderElement::draw:
frame.override_texture_program(&program, &uniform_bytes)?; // like GlesFrame::override_default_tex_program
inner.draw(frame, src, dst, damage, &[])?; // its textures draw with `program`
frame.clear_texture_program_override();
frame.draw_program(&procedural, src, dst, size, damage, alpha, &bytes)?; // like render_pixel_shader_to
let [map_x, map_y] = frame.pixel_mapping(); // @builtin(position) to frame coordinates
Each draw still appends to the frame's one vertex buffer and one render pass;
its uniform block goes into one uniform buffer per frame at the device's offset
alignment, bound with a dynamic offset. compile_program runs in error scopes
and also builds the pipeline for Rgba8Unorm, so a source that does not
validate is an Err there; a pipeline per target format and blending is made at
the first draw and cached only when it was valid. A program of another
renderer, a texture program used as a procedural one (or the reverse) and a
uniform block of the wrong size are errors. As in GLES, a texture without alpha
under an override draws with blending.
Two options of a textured program serve the glass of phase 3 step 2:
second_texture: truedeclares a second texture at@group(2)(@binding(0)the texture,@binding(1)its sampler, LINEAR and CLAMP_TO_EDGE as unit 1 of the GLES program).WgpuFrame::override_texture_program_with(&program, &bytes, Some(&texture))binds it for the draws of the override; a program that declares one needs one, a program that does not refuses one, and the target of the frame cannot be it (a feedback loop).repeat: truesamplestexwith REPEAT, the wrap of an offscreen GLES texture that the morphology of the mask glass reads unclamped and the canvas lens samples at the edge of its flat box.
Either one needs textured. There is no draw that samples the framebuffer: the
glass renders the elements behind it into its own offscreen textures, as the
GLES scene does, so each glass pass is one more frame and submission on the
same queue before the output's frame.
Offscreen passes (phase 3 step 6)
A theme package of openspec/specs/themes/spec.md brings whole WGSL
modules with their own vertex stage and an explicit @group(0) layout (the
workspace transition ABI v1 and the F9 effect ABI v1), so they cannot run as a
custom program inside a frame. compile_pass and run_pass run them as
offscreen passes (openspec/changes/wgpu-renderer-plan/design.md,
Phase 3 step 6):
let pass = renderer.compile_pass(PassSource {
label: "workspace card",
wgsl: CARD_WGSL, // `@vertex fn vs` and `@fragment fn fs`, at most MAX_PASS_SOURCE
bindings: &[
PassBinding::Texture, // binding 0
PassBinding::Sampler { linear: true, repeat: false }, // binding 1
PassBinding::Uniform(64), // binding 2
], // at most MAX_PASS_BINDINGS
})?;
renderer.run_pass(
&pass,
&mut target, // an offscreen texture of this renderer
viewport, // the top-left part of `target` that the pass covers
&[PassResource::Texture(&input), PassResource::Sampler, PassResource::Uniform(&bytes)],
)?;
// Then draw `target` as an ordinary texture of the next frame.
compile_pass runs in error scopes and builds the pipeline for Rgba8Unorm, so
a source or layout that does not validate is an Err and nothing is cached; the
pipeline of another target format is made at its first run and cached only when
valid, and the pipelines of dropped passes go at the next compile. A pass has
the topology, the sample count and the blending of the theme ABIs: triangle
list, no vertex buffers, one sample, no blending. run_pass clears the target
to transparent, sets the viewport and scissor to viewport, draws three
vertices, and submits one encoder on the queue of the frames, so a later frame
samples the result. A pass of another renderer, a target or an input that is
not a plain offscreen texture (dma-buf, shadow copy, swizzle or red-blue swap),
an input that is the target (a feedback loop), a viewport outside the target
and resources that do not match the layout are errors, never panics.
dma-buf path
let mut renderer = WgpuRenderer::for_render_node(Path::new("/dev/dri/renderD128"))?;
if let DmaSupport::Unsupported(why) = renderer.dma_support() {
/* keep GLES */
}
// Explicit (VK_EXT_image_drm_format_modifier) and Implicit alike: nothing
// imports or binds until this passed. GbmDevice of the same GPU.
let report = renderer.self_test(&gbm_device)?;
let render_formats = renderer.dmabuf_render_formats(); // renderer_formats of DrmCompositor
for_render_node opens the Vulkan device of that node with external memory,
dma-buf import, SYNC_FD semaphores, the foreign queue family and, when the driver
has it, VK_EXT_image_drm_format_modifier. With the modifier extension buffers
import with the explicit modifiers the driver lists and implicit buffers
(Modifier::Invalid) never import. Without it (the RX 550 of this project)
implicit buffers import with OPTIMAL tiling, whose layout RADV takes from the BO
metadata, and LINEAR buffers import with LINEAR tiling to sample, only when
Vulkan's row pitch is the buffer's stride; an implicit buffer is never imported
as LINEAR. On both paths nothing imports until self_test returned Ok: it
draws GBM buffers written through GBM's map and draws into GBM scanout buffers
read back through GBM's map, for each format of DMA_FORMATS (and on the
explicit path each listed modifier, allocated with it), at 131x67 and 1031x617,
and Argb8888 and Xrgb8888 also at 1920x1080 and 3840x2160, because the layout can
depend on the size and on SCANOUT (0.95 to 1.15 s in release on the RX 550). The
render check draws a whole frame and then a damage-only frame into the same
buffer. It lists only what passed. It returns Ok when Argb8888 and Xrgb8888
sample; render targets are enabled per format and may be none, so read
dmabuf_render_formats(). A failure is an Err, never a panic, and leaves every import
refused; each run drops the cached imports first.
A client's plane is checked before Vulkan sees it: whole texels, a row at least
the width, the rows inside the buffer (fstat size), offset 0 on the implicit
OPTIMAL path; the explicit path then checks that the image took the client's
offset and row pitch. A buffer whose fd does not take the dma-buf sync-file
ioctls is refused at import.
Bind<Dmabuf> imports the target for transfers only (TRANSFER_SRC | TRANSFER_DST): RADV without the modifier extension wants CMASK room past the
end of a GBM scanout buffer of 1031x617 or more for a COLOR_ATTACHMENT image
(openspec/changes/wgpu-renderer-plan/design.md, Review round 1 of part A). A frame draws into a
wgpu texture of the same size and format kept with the cached import (one per
buffer, so a buffer age stays valid), which the first frame fills from the
buffer, and copies all of it into the buffer at the end of its one encoder.
The explicit path does the same. The copy costs about 0.9 ms per 1080p frame
and 1.6 ms per 4K frame on the RX 550, and each target holds a second texture
of its size.
Formats: Argb8888, Xrgb8888, Abgr8888, Xbgr8888, Abgr2101010, Xbgr2101010 (sample and render) and Argb2101010, Xrgb2101010 (sample only). Rgb565, NV12 and YUV420 do not import.
Each submission that uses imported images acquires them from the foreign queue
family and releases them back, in raw command buffers around wgpu's own in the
same vkQueueSubmit. It waits on the GPU for the buffers' implicit fences
(DMA_BUF_IOCTL_EXPORT_SYNC_FILE) and on fences staged by Renderer::wait, and
signals a SYNC_FD semaphore. That fd is the frame's SyncPoint::export() and is
also added to each buffer's reservation (DMA_BUF_IOCTL_IMPORT_SYNC_FILE), so KMS
and clients see it implicitly. A submission that fails before anything is queued
keeps the waits of Renderer::wait for the next one; once the work is queued, a
failed export or reservation import is logged and the frame stands (a failed
export leaves a fence that is not exportable). Renderer::wait on a foreign fence that does not
export waits on the CPU.
wgpu errors never panic: the renderer logs uncaptured errors, runs resource
creation, uploads, read backs and submissions inside error scopes, and refuses
zero sizes, sizes above max_texture_dimension_2d and read backs above
max_buffer_size with a WgpuError. Drawing the bound target into its own frame
returns WgpuError::FeedbackLoop.
dma-buf path on the GL backend
let mut renderer = WgpuRenderer::for_render_node_gl(Path::new("/dev/dri/renderD128"))?;
assert_eq!(renderer.gpu_api(), Some(GpuApi::Gl)); // Some(Vulkan) after for_render_node
let report = renderer.self_test(&gbm_device)?; // DmaSupport::Egl { verified }
for_render_node_gl is the same surface as for_render_node on wgpu's OpenGL backend, for a
machine without a usable Vulkan device. It opens a GBM device on the render node, an
EGLDisplay and a surfaceless OpenGL ES context with Smithay's backend_egl (no Smithay
GLES renderer: the crate does not enable renderer_gl or renderer_glow), and wgpu-hal's
Adapter::new_external on that context. gpu_api() is None for a renderer from new or
from_adapter, which has no dma-buf path.
- Import (
ImportDma,ImportDmaWl):eglCreateImageKHRon the dma-buf, a GL texture bound withglEGLImageTargetTexture2DOES, wrapped bytexture_from_raw. The drop callback of the wgpu texture frees the texture and the EGLImage. EGL maps the DRM channel order, so every format is stored asRgba8UnormorRgb10a2Unormwith no red-blue swap, and the 10-bitArgb2101010andXrgb2101010can be targets too. One-plane buffers only, as on Vulkan. - Targets (
Bind<Dmabuf>): the same texture as a color target, drawn into directly; there is no shadow texture, because the CMASK limit of RADV is a Vulkan one. - Candidates of
self_test: the formats and modifiers ofEGLDisplay::dmabuf_texture_formatsanddmabuf_render_formatsthat are inDMA_FORMATS;Modifier::Invalidwith an implicit GBM buffer, an explicit modifier with a buffer of that modifier, and, for a format whose display lists no explicit modifier, a LINEAR-described sample check as on the Vulkan implicit path. The pass rule is the Vulkan one, andArgb8888andXrgb8888must sample implicit buffers when the display lists them. The report'ssupportisDmaSupport::Egl { verified }; nothing imports or binds before it passed. - Fences: after each submission an
EGL_ANDROID_native_fence_syncfence is created and its fd is the frame's SYNC_FD (WgpuFence::is_exportable); without the extension the fence waits on the CPU.Renderer::waitof a foreign fence imports it as an EGL fence and queues the server-side wait in the context. Implicit synchronization with clients and KMS is the driver's, as with Smithay's GLES renderer: the sync-file ioctls of the Vulkan path are not used here. - The context: wgpu-hal never makes an external context current, so the renderer makes its
own current whenever it enters wgpu and it is not (
eglGetCurrentContextcomparison), also when another EGL context was current on the thread meanwhile. It belongs to one thread, and a texture must be dropped on that thread while the renderer lives; a texture that outlives the renderer keeps the context alive and frees its GL objects with it current.
Measured on the RX 550 (radeonsi, Mesa 26.0.8): the display lists Modifier::Invalid only,
every format of DMA_FORMATS samples, and Xbgr2101010 and Xrgb2101010 do not render
(radeonsi does not take an EGLImage of an X2 format as a color attachment: the frame writes
nothing), so the self-test leaves them out of dmabuf_render_formats().
Checks of the crate itself (it has its own workspace and lockfile, a subset of the compositor's):
PATH=/usr/lib/rust-1.93/bin:$PATH cargo fmt --manifest-path native/wgpu-renderer/Cargo.toml --all --check
PATH=/usr/lib/rust-1.93/bin:$PATH cargo test --locked --manifest-path native/wgpu-renderer/Cargo.toml
PATH=/usr/lib/rust-1.93/bin:$PATH cargo clippy --locked --manifest-path native/wgpu-renderer/Cargo.toml --all-targets -- -D warnings
The renderer-level tests live in the compositor (GPU, ignored; the parity tests with
GlesRenderer went with step 9 of openspec/changes/archive/2026-11-10-gles-removal-plan/design.md), and the GL path is compared with
the Vulkan renderer in native_wgpu_renderer_gl_ tests:
python3 scripts/prepare-project.py --root "$PWD" --cargo test --locked --features session native_wgpu_renderer_ -- --ignored --nocapture --test-threads=1