build(nix): hardware H.264 encode for the demo guest's RDP

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.
This commit is contained in:
LGUG2Z committed 2026-10-28 18:43:06 -07:00
1 parent 1ee2405e44
commit 1915d6ac26
17 files changed
+911 -4

No files matched your search

Executable → Regular
+41
View File
@@ -59,6 +59,8 @@ osrelease=${RUN_KDE_OSRELEASE:-/proc/sys/kernel/osrelease}
sys_module_dir=${RUN_KDE_SYS_MODULE_DIR:-/sys/module}
store_dir=${RUN_KDE_STORE_DIR:-/nix}
wslconfig=${RUN_KDE_WSLCONFIG:-}
wsl_lib_dir=${RUN_KDE_WSL_LIB_DIR:-/usr/lib/wsl/lib}
va_drivers_dir=${RUN_KDE_VA_DRIVERS_DIR:-/run/opengl-driver/lib/dri}
mode=run
display=auto
@@ -276,6 +278,26 @@ find_virtiofsd() {
fi
}
# What the host's GPU needs of the process that uses it, on WSL 2. Mesa has to be told to take the
# d3d12 driver of Windows, whose libraries are in a directory of their own, and the libva that
# virglrenderer takes the host's video encoder from has to be told which driver that is. Without
# the first OpenGL is llvmpipe, and without the second the guest's VA-API lists no encoder, so KRDP
# encodes H.264 on the guest's CPUs. What a person has set is left as it is.
host_env=()
if [ "$wsl" = 1 ] && [ "$gpu_on" = 1 ]; then
host_env+=("GALLIUM_DRIVER=${GALLIUM_DRIVER:-d3d12}")
if [ -d "$wsl_lib_dir" ]; then
case ":${LD_LIBRARY_PATH:-}:" in
*":$wsl_lib_dir:"*) ;;
*) host_env+=("LD_LIBRARY_PATH=$wsl_lib_dir${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}") ;;
esac
fi
if [ -e "$va_drivers_dir/d3d12_drv_video.so" ]; then
host_env+=("LIBVA_DRIVER_NAME=${LIBVA_DRIVER_NAME:-d3d12}")
host_env+=("LIBVA_DRIVERS_PATH=${LIBVA_DRIVERS_PATH:-$va_drivers_dir}")
fi
fi
# ---------------------------------------------------------------------------------------------
# the checks
@@ -474,6 +496,20 @@ check_everything() {
*) ok "GPU: the host's OpenGL renderer is $renderer" ;;
esac
fi
if [ "$wsl" = 1 ] && [ -n "$gpu_device" ]; then
if [ -d "$wsl_lib_dir" ]; then
ok "GPU: the Windows driver is in $wsl_lib_dir"
else
warn "GPU: there is no $wsl_lib_dir, so Mesa cannot take the Windows driver (d3d12)" \
"run \`wsl --update\` in PowerShell, and install the Windows GPU driver with WSL support"
fi
if [ -e "$va_drivers_dir/d3d12_drv_video.so" ]; then
ok "GPU: the video driver of Mesa for it (d3d12) is in $va_drivers_dir"
else
warn "GPU: there is no d3d12 video driver in $va_drivers_dir, so KRDP encodes H.264 on the guest's CPUs" \
"on NixOS-WSL, wsl.useWindowsDriver = true; with a Mesa that is built with video codecs"
fi
fi
if [ "$gpu_on" = 1 ]; then
ok "the guest will use the GPU: $gpu_why"
else
@@ -564,6 +600,7 @@ if [ "$mode" = dry-run ]; then
echo " RUN_KDE_MEMORY=$guest_memory"
echo " QEMU_NET_OPTS=$net_opts"
echo " QEMU_OPTS=${qemu_opts[*]}"
for var in "${host_env[@]}"; do echo " $var"; done
echo " command: ${runner_bin:-<the runner, built from $repo>} ${runner_args[*]}"
exit 0
fi
@@ -633,6 +670,10 @@ export RUN_KDE_STATE=$state_dir
export RUN_KDE_MEMORY=$guest_memory
export QEMU_NET_OPTS=$net_opts
export QEMU_OPTS="${qemu_opts[*]}${RUN_KDE_QEMU_OPTS:+ $RUN_KDE_QEMU_OPTS}"
for var in "${host_env[@]}"; do
# shellcheck disable=SC2163 # the variable named by what `var` holds, as NAME=value
export "$var"
done
# virtiofsd says a great deal about being run by a user and not by root, none of it a problem
export RUST_LOG=${RUST_LOG:-error}