Where the host's video decodes into device caches, a picture the driver's check refused to decode into its cache (its codec and format), or whose planes do not lie as a target's must, landed by the runtime device's GPU copy, which imports the decode surface as images by their DRM format modifiers, and else by the CPU through the session's staging. On an RX 550 both apply to HEVC Main 10: its radeonsi fails the check's second step for P010 (the first wrong byte, 0x40, is the low byte of the check's 0x5a5a scribble as a 10-bit sample, read from the reference's overwritten target), and its RADV lists no VK_EXT_image_drm_format_modifier, so every 10-bit picture was read back and copied by the CPU. A 26.7.2 guest's walk there (m3-max-21989) played a 2160p30 HEVC Main 10 clip at 22.2 fps with 109 frames dropped. The cache's memory is a dma-buf the arm already exports for its decodes (CacheTargets). The arm's first landing, the driver's video processing into a linear VA surface over a dma-buf of the planes' pages (Frame::write_to_guest), now also takes such a cache: CacheBytes hands the cache's memory as the device memory of the write, each range at its own byte of the one dma-buf, and the write is acquired as a decode into the cache is (Buffer::external_write_done). Its import goes as the cache does, by the cache's identity. Where the driver's import check refuses the format or the planes' alignment, the picture lands as before. What the guest sees: the same pictures in its outputs, landed in the cache by the driver's GPU work rather than by the CPU. The session counts such pictures "in a device cache, by the hardware". Tests: the ignored hardware test a_picture_written_into_a_dma_buf_cache_by_video_processing_is_the_cpus_copy, on a VA-API host with the conformance suite's streams.
13 KiB
The graphics device's video
The reims-graphics device (PCI 1234:5247) decodes and encodes the guest's H.264 and HEVC on the
host's hardware. The guest's video driver, in our Metal bundle, is AppleGVA's vendor driver
(docs/graphics-kext/design.md, "Decided", 16); it decodes, and
sends no encode op yet. This file is the host's side: what it answers the guest, and what a
decode and an encode do. The wire's records and their
fields are documented where they are defined: crates/reims-vgpu-graphics/src/wire.rs (the caps and
the session) and crates/reims-vgpu-graphics/src/ops/video.rs (the ops).
Which hardware
On Linux, the device's video runs on the first of the host's two video arms that takes the
runtime device (reims-vgpu's graphics/video_choice.rs):
- NVIDIA's, decoding on NVDEC and encoding on NVENC, where the device is an NVIDIA GPU that
CUDA opens (
graphics/nvidia.rs, nvidia.md). - VA-API, on the device's render node, whatever its vendor (
graphics/vaapi.rs, vaapi.md).
Each arm that declines says why in one line at info. A Mac has no video arm, on either platform. Where no arm takes the device, the host writes no video caps, and the guest is offered no codec.
The caps
At the device's init, the host writes an RgfxVideoCaps at RGFX_VIDEO_CAPS_OFFSET, after
the device info (GraphicsDevice::with_platform, in graphics.rs):
- The entries: one
RgfxVideoCodecCapsfor each codec and direction the arm takes: its profiles, its highest level, its smallest and largest coded pictures, its DPB and reference bounds, its output formats, and the alignment at which it reads an access unit in place. Each answer is the arm's video API's own (the arms' docs), but for VA-API's largest H.264 size, kept under the 65,535 macroblocks its drivers decode: radeonsi's 4096x4096 and iHD's 8192x8192 are answered as 4096x4080 (bugs.md, U28). - The flags:
RGFX_VIDEO_CAPS_OUTPUT_CACHEDwhere the kext makes device caches, of every surface it asks one for (RGFX_FEATURE_SURFACE_CACHE, a GPU of its own memory) or of those whose textures would not lie in place in their pages (RGFX_FEATURE_SURFACE_CACHE_REFUSED, a GPU that shares the CPU's memory), and the arm lands pictures in them: NVDEC by its own GPU work, where CUDA imports the device's exportable buffers (nvidia.md), the device's caches of surfaces of two planes (the outputs' NV12 and P010, as the layout a cached block'sSURFACE_PAGEScarries gives them) then made exportable as they are made, and every other cache made as any buffer; VA-API by the runtime device's copy of the decode surface, or the CPU's through the session's staging (vaapi.md). The guest's video driver then asks its output surfaces cached (guest/bundle/src/RgfxMetalDevice+Video.mm), with their layout, as for any other surface, and on a GPU that shares the CPU's memory the device keeps a cache of those it answers so; elsewhere it asks them uncached. A cache made for a surface whose first plane starts past its first byte, as CoreVideo's 32 bytes into their surface, has the platform place that byte where its textures over the cache's memory may start in place (on the Vulkan platform a multiple of 256 bytes,BufferDescriptor::with_aligned_byte), so textures over the planes lie in the cache rather than in the runtime's shadows (surface_cache::aligned_byte): from the layout the kext's ask gives, or on a GPU of its own memory the layout a cached or deferred block'sSURFACE_PAGEScarries, the exportable caches NVDEC lands its pictures in among them. That holds where the runtime binds images in the cache's memory: on the RTX 5080 metal2vulkan makes each exportable buffer its own dedicated allocation and binds no image there, so the planes' textures over NVDEC's caches stay its shadows wherever the byte lies (measured 2026-11-11). - The layouts: an
RgfxVideoOutputLayoutper entry, which says how a surface is laid out to take a picture's whole coded rows and columns in place. An arm that lands every picture by a copy of its crop answers zero, and every entry's is zero where the flag is set, since a picture lands in place only in a surface's pages. For an encode entry it would say how a source is laid out to be read in place; both arms read every source by a copy, and answer zero. - What an encode entry takes: an
RgfxVideoEncodeCapsper entry (encode): the rate controls (a bit each of CQP, CBR and VBR, as the arm's API lists them for the codec's profiles) and the most consecutive B pictures, 0 on both arms, whose encoders keep frames in display order. - What a decode entry bounds: an
RgfxVideoDecodeCapsper entry (decode): the narrowest HEVC tile column the arm decodes, in luma samples, 128 on both arms (reims_vgpu_video::codec::hevc::MIN_TILE_COLUMN_WIDTH); zero, no bound, in every other entry. An HEVC decode entry'smax_referencesis 8 on both arms: the most current references with which every picture decodes (vaapi.md, "What it refuses"; nvidia.md).
The guest's video driver checks an HEVC picture against these before it schedules it
(hevcTaken, guest/bundle/src/RgfxVideoDriver.cpp): a picture whose coded size is outside its
entry's, whose PPS has a tile column narrower than decode's, or whose first slice segment's
header names more current references than max_references fails its schedule, and VideoToolbox
reports kVTVideoDecoderMalfunctionErr for it and every picture after it. Sent, the host would
refuse it after it took it, and the schedule would fail only once the host's verdict came back.
AppleGVA's H.264 decoder delivers a picture whose schedule failed, so an H.264 decode is checked
at its context's create (sizeTaken, CreateContexts): a coded size outside its entry's fails
the create, and VideoToolbox decodes the stream in software. What the host refuses only from a
picture's parameter sets or slices still reaches the guest undecoded (bugs.md, D25).
Sessions
VIDEO_SESSION_CREATE names a codec and a direction. The arm makes a session where it has an
entry for them. Elsewhere the create is refused, with a warning that names the codec and the
direction. Each session has a shared event, which the host signals.
A decode
A decode is an op (DECODE_H264 0x4000, DECODE_H265 0x4001) in a video encoder of a
command buffer of the session's process (graphics/decode/video.rs):
- The op's objects are resolved as the command buffer is taken: the bitstream buffer, and the buffers the output surface's planes lie in. An op faults the command buffer where it is of another codec than its session's, where its parameter sets run past its end or its access unit past its buffer, and where it names no texture over a plane.
- The work starts at once, after the session's earlier work, on the host's thread that
takes the command buffer. It does not wait for the buffer's earlier encoders, since the
guest's video driver encodes none before it. It does wait, on the CPU, for the buffers of
other queues the command buffer runs after (its
SURFACE_ORDER), so a decode whose output another process still reads lands once that read is done. And it waits, on the CPU, for the host's own work on an output's device cache, its last fill and a copy of its last picture into the pages still in flight, as long as that takes, as a Mac's video work waits for the GPU's earlier work on its surface: a GPU slowed by other work slows the picture and fails nothing. Each ends as the platform sees its command buffer complete, failed where the device was lost; only a reset of the device ends the wait sooner, faulting the op. A wait past 2 s is named in the log. - The access unit is read in place, in the guest's buffer, where it starts and ends at the
entry's
bitstream_alignment; elsewhere the arm copies it. The parameter sets the op carries are handed to the decoder before the access unit. - The picture lands in the output surface's planes, cropped: in the surface's pages, or in its device cache where the kext keeps the surface in one. The arms' ways differ (below).
- The command buffer's later work, and its completion, wait for the session's event to reach the value the decode signals once its picture has landed. So a CPU lock of the output waits for the picture, as for any GPU write.
- A picture that fails to land still signals its value, and the command buffer completes
failed, with Metal's internal error. The guest's video driver answers that as a decode error:
an HEVC picture's schedule waits for its verdict and fails (
kVTVideoDecoderMalfunctionErr, design.md, "A decode the driver cannot complete is a decode error"); an H.264 picture reaches the guest as decoded, as AppleGVA's H.264 decoder reports no failure, so the driver refuses what it can tell before a picture, its size, at the create (bugs.md, D25). - A picture the stream's random access skips lands nothing, and its command buffer completes as any other.
An encode
An encode is an op (ENCODE_H264 0x4002, ENCODE_H265 0x4003) in a video encoder of a command
buffer of an encode session's process (reims-vgpu's graphics/video_host/encode.rs):
- The op is checked as the command buffer is taken: the settings it carries (a session's first encode carries them, and each change after), the source's size against them and the entry's sizes, and the buffers the access unit and its result go in. A refusal faults the command buffer.
- A change of settings that makes a new sequence (the size, format, profile, level,
entropy coder, B pictures, keyframe interval or colour) makes the arm a new encoder, whose
first access unit is a keyframe;
a change of the bitrates, QPs or frame rate goes to the encoder from the next frame.
min_qp,max_qpand a change ofqualityalone are not taken. - The frame is encoded on the session's encode thread (
reims-vgpu.graphics-video-encode), in the order the encodes were taken, once the command buffer's waits are done: its two planes are read as host bytes, in the surface's pages, or, for a plane in a device cache, in a staging buffer the GPU copies it into first. - The access unit is written in Annex-B form into the bitstream buffer, a keyframe's with
its parameter sets first, and the frame's
RgfxVideoEncodeResultafter it. One past the capacity is not written (RGFX_VIDEO_ENCODE_OVERFLOW, with the bytes it needed), and the next frame is made a keyframe; a frame the hardware fails getsRGFX_VIDEO_ENCODE_FAILED. Either way the command buffer completes as any other. - The command buffer's later work, and its completion, wait for the session's event to reach the value the encode signals once its result is written.
| arm | encoder | its access unit |
|---|---|---|
| NVENC | NvencEncoder (nvidia.md); quality picks the presets P1 to P7 at the high-quality tuning, and the keyframe interval is the GOP (graphics/nvidia/encode.rs) |
Annex B as NVENC writes it, its parameter sets before each IDR picture |
| VA-API | VideoEncoder (vaapi.md), on EncSlice, else EncSliceLP; quality maps onto the driver's quality levels (graphics/vaapi/encode.rs) |
the encoder's length-prefixed sample, written as Annex B, a keyframe's parameter sets first from its configuration record |
How each arm lands a picture
| arm | access unit in place | a picture lands | layout asked of the guest |
|---|---|---|---|
| NVDEC | at any place and length | by one CUDA copy a plane: into the output's device caches, through CUDA's import of their memory, where the flag is set; into the pages, where CUDA registers their view; else read back and copied by the CPU | none |
| VA-API | at any place and length | decoded into the output's device cache, where the driver's check of decodes into targets passed the codec and format and the cache's memory is a dma-buf; else by the driver's video processing into the pages, or into such a cache through its memory, where the import check passed for the format and the planes meet its alignment; else by the runtime device's GPU, a copy of each plane of the decode surface's export as dma-bufs into the pages or the device caches, where it imports them; else read back and copied by the CPU | granularity [2, 2], the import check's alignment |
Both run in reims-vgpu's host video session (graphics/video_host.rs, backends.md,
"The traits"): a plane in a device cache the arm does not write itself takes its picture staged
in host bytes, and the session copies it into the cache on a runtime queue. Each session counts
how its pictures landed, and logs the counts at its end (vaapi.md, "The
reims-graphics device").