VIC composition on the GPU

VIC composes up to eight NV12 or NV21 slots onto an RGBA, NV12 or NV21 surface. src/nx/gpu/video/vic.c reads the config and resolves the slots. Once the renderer is up, src/nx/gpu/vk/vic.c runs the composition as a compute shader and the CPU path (compose_cpu) only composes what the shader declines. The shader is src/nx/gpu/video/vic.comp, which declares the buffers, and vic_compose.glsl, the body, written in the part of GLSL that is also C++ so the tests run it on the CPU. The parameters are declared once, in vic_params.glsl, for GLSL, C and C++. Both are ported from Eden's VIC (GPL-3.0-or-later, with yuzu's GPL-2.0-or-later parts).

The shader

A workgroup is 16x4 invocations and composes a 64x8 tile, which is one GOB of 8 bit luma. Each invocation owns a block of 4x2 pixels: two luma words and one chroma word of NV12, or eight RGBA words. Every output word has one owner, so the shader has no shared memory and no barrier. Each input word a pixel needs is loaded once, straight from its plane, and the neighbors' loads of the same lines coalesce. The dispatch covers the target rectangle only, since nothing outside it is written.

The previous version staged each tile's input window in shared memory behind two barriers. It composed every slot that covered a pixel, overwriting the last one, and ran the colour matrix in full for every pixel. The rewrite removes repeated work in these ways:

vic_gpu_params refuses a job with a slot outside the target rectangle, which slot_resolve never produces, because the shader writes the target alone.

The shader is built twice from the same source (cmake/glsl.cmake). The default build needs every plane's rows to keep word aligned columns word aligned (block linear planes, or pitch linear ones with a pitch that is a multiple of 4), which vic_gpu_aligned checks. The -DVIC_UNALIGNED build funnels its loads and inserts its stores with atomics, since invocations can then share a word.

Speed

Measured on lavapipe (LLVM 19, on the CPU) with the real renderer, host copies included, median of 25 jobs. The old shader is shown as it shipped and recompiled with glslang's -O, which made it slower:

JobOldOld with -ONew
1920x1080 NV12 block linear to A8B8G8R8, BT.709 matrix21.4 ms30.3 ms7.6 ms
1920x1080 NV12 block linear to A8B8G8R8, no matrix32.0 ms28.7 ms6.3 ms
1920x1080 NV12 block linear to NV12 block linear19.8 ms31.1 ms5.4 ms
1280x720 NV12 pitch linear to A8B8G8R8, BT.709 matrix14.2 ms19.9 ms3.8 ms

Old and new wrote the same bytes in every job. Lavapipe counts operations and shows nothing of coalescing or divergence, which the new layout also improves on a GPU.

On an RX 6700 XT (RADV), the dispatch alone from GPU timestamps around it, median of 100 jobs per run, runs of both builds taken in turn:

JobOldNew
1920x1080 NV12 block linear to A8B8G8R8, BT.709 matrix90 us49 us
1920x1080 NV12 block linear to NV12 block linear108-160 us16 us
1920x1080 NV12 block linear to NV12 block linear, BT.709 matrix120-167 us41-53 us
1918x1080 NV12 pitch linear to NV21 pitch linear (the unaligned build)155-166 us24-25 us

The idle card switches between two power states, and both builds slow down together in the slow one, so these are compared within the fast one. The whole vic_compose call is mostly the host copies there (1.4 ms for 1080p to A8B8G8R8): it gains about 3% with A8B8G8R8 out and 13-15% with NV12 or NV21 out.

Tests


Made from docs/layers/media/vic.md at 69237645142c. GPL-3.0-or-later.