mirror of
https://github.com/LGUG2Z/komorebi.git
synced 2026-11-20 15:41:38 +01:00
On a host whose GPU can encode H.264 (a Windows driver through d3d12 on WSL 2 does), KRDP in
the demo guest encoded on the guest's CPUs. The host side was fine (vainfo on the host lists
H.264 and HEVC EncSlice), and virgl can pass a guest's VA-API video to it, but a chain of seven
problems stood between the two. Each is a patch, and the guest takes them all:
QEMU (nix/dev/qemu-virgl-video.patch)
QEMU never asks virglrenderer for video: virglrenderer turns it on only for an embedder that
passes VIRGL_RENDERER_USE_VIDEO and a get_drm_fd callback to open the host's video device on.
The flag is defined only with VIRGL_RENDERER_UNSTABLE_APIS, which the build sets.
virglrenderer 1.3.0, four patches in this order
virglrenderer-chroma-format.patch
the guest's Mesa and virglrenderer number the chroma format of a codec differently: 4:2:0
was read as 4:2:2, and the host's encoder refused every codec it was asked for
virglrenderer-video-memory-upload.patch
the d3d12 driver has no dma-bufs: vaExportSurfaceHandle fails, nothing was uploaded and
the encoder returned an empty bitstream. The planes of the guest's picture are read back
(glReadPixels) and written to the surface through a derived image (vaDeriveImage, with
vaCreateImage and vaPutImage as a fallback)
virglrenderer-h264-packed-sps.patch
Mesa's VA-API takes the level of the stream from a packed SPS, which virglrenderer never
sent: level 0 reached UNREACHABLE in the d3d12 encoder and crashed QEMU
virglrenderer-ubo0-constants.patch
a constant buffer 0 bound as a buffer was never read by the shader, so the colour matrix
of an RGB to YUV conversion was zero and the picture was a solid green
Mesa 26.2.5 in the guest, two patches (hardware.graphics.package)
mesa-virgl-rgb-yuv-csc.patch
the graphics compositor never loaded an RGB to YUV matrix, and multiplied by the YUV to
RGB one of its initial state
mesa-virgl-fence-leak.patch
a surface kept the out-fence (a sync_file) of its last flush and the next flush stored a
new one over it: one file descriptor leaked per converted frame, 44 a second under motion,
and KRDP froze when it reached the limit of 1024, about 28 seconds in
KRDP (krdp.nix) asks for H264Baseline and the guest's driver lists Main and High, so ffmpeg's
h264_vaapi found no profile it could use: it is built with H264Main.
What was checked: a luma ramp and a colour-bar pattern encoded through the guest to the 4090
decode to the right values; the real desktop shows over RDP in Windows' own client with the
right colours; under es2gears motion the descriptors of krdpserver stay flat (3 sync_file, 78
of 1024) where they were 949 of 1024 after 28 seconds.
run-kde sets what the host needs for Mesa and for virglrenderer's libva to find the Windows
driver, on WSL 2 with the GPU on and unless it is set already: GALLIUM_DRIVER=d3d12,
LD_LIBRARY_PATH with /usr/lib/wsl/lib first, LIBVA_DRIVER_NAME=d3d12 and LIBVA_DRIVERS_PATH
(/run/opengl-driver/lib/dri, for the d3d12 video driver, which it looks for). --doctor checks
for the libraries and for that driver, and the tests have a scenario for each.
Known: the software decoder warns "illegal short term buffer state detected" on the stream; the
d3d12 encoder writes Constrained Baseline whatever profile is asked for. There is no binary
cache: the first run builds QEMU and Mesa, which takes about 20 minutes. QEMU, virglrenderer and
Mesa are the runner's and the guest's only, and the VM suites build the Mesa as well.
Not measured yet: whether it is smoother than the CPU encoder under the same load.
57 lines
3.1 KiB
Diff
57 lines
3.1 KiB
Diff
mesa: let go of the fence a surface is flushed or processed again over
|
|
|
|
vlVaSurfaceFlush() (the upload of an image, the clear of a new surface) and the end of a
|
|
video-processing pass (vl_compositor_proc: the scale_vaapi of FFmpeg, the blit of
|
|
vaPutImage) flush into surf->pipe_fence, which gets the new fence of the flush and loses
|
|
the old one. The only places that let go of it are vaSyncSurface() and the destruction of
|
|
the surface, so a surface that is flushed again before a sync, which one of a pool is,
|
|
leaves the fence it had behind.
|
|
|
|
With virgl a fence owns a file descriptor: the sync_file that the kernel returns for the
|
|
out-fence of the execbuffer (VIRTGPU_EXECBUF_FENCE_FD_OUT), closed when the fence is freed.
|
|
The guest's KRDP kept one open for every frame it captured until it had 1024 of them,
|
|
after which every ioctl failed with EMFILE ("got error from kernel - expect bad rendering
|
|
24") and the picture froze.
|
|
|
|
Release the fence a flush is about to replace. The newer one covers it: both are fences of
|
|
the same context.
|
|
|
|
--- a/src/gallium/frontends/va/surface.c 2026-10-28 17:58:18.769410786 -0700
|
|
+++ b/src/gallium/frontends/va/surface.c 2026-10-28 17:58:30.469583700 -0700
|
|
@@ -812,6 +812,14 @@
|
|
void
|
|
vlVaSurfaceFlush(vlVaDriver *drv, vlVaSurface *surf)
|
|
{
|
|
+ /* A flush makes a new fence, which replaces the one in the surface: the one of the last flush
|
|
+ * is what vaSyncSurface waits for, and it is only let go of there and when the surface is
|
|
+ * destroyed. Replaced without a release, its fence is never freed, and with a driver whose
|
|
+ * fences hold a file descriptor (virgl: a sync_file) a surface that is flushed again and
|
|
+ * again, as the ones of a pool are, leaves one open a time until there are no more. */
|
|
+ if (surf->pipe_fence)
|
|
+ drv->pipe->screen->fence_reference(drv->pipe->screen, &surf->pipe_fence, NULL);
|
|
+
|
|
drv->pipe->flush(drv->pipe, &surf->pipe_fence,
|
|
drv->has_external_handles ? 0 : PIPE_FLUSH_ASYNC);
|
|
}
|
|
--- a/src/gallium/auxiliary/vl/vl_compositor_proc.c 2026-10-28 17:58:18.771011962 -0700
|
|
+++ b/src/gallium/auxiliary/vl/vl_compositor_proc.c 2026-10-28 17:58:30.814280800 -0700
|
|
@@ -185,8 +185,17 @@
|
|
struct pipe_picture_desc *picture)
|
|
{
|
|
struct vl_compositor_proc *proc = (struct vl_compositor_proc *)codec;
|
|
+ struct pipe_context *context = proc->b.context;
|
|
|
|
- proc->b.context->flush(proc->b.context, picture->out_pipe_fence, picture->flush_flags);
|
|
+ /* out_pipe_fence is the pipe_fence of the target surface (vlVaEndPicture, vlVaPutImage), and
|
|
+ * the flush makes a new fence for it. The last one is what a sync waits for, so the one it
|
|
+ * replaces is let go of here: left, it is never freed, and with virgl its fence is a
|
|
+ * sync_file, one more of them open for every frame of a pooled surface that is processed
|
|
+ * again before it is synced. */
|
|
+ if (picture->out_pipe_fence && *picture->out_pipe_fence)
|
|
+ context->screen->fence_reference(context->screen, picture->out_pipe_fence, NULL);
|
|
+
|
|
+ context->flush(context, picture->out_pipe_fence, picture->flush_flags);
|
|
|
|
return 0;
|
|
}
|