Frames de vídeo vêm como YUV (NV12, I420, P010). A abordagem
direta é conversão dentro do shader — amostrar os planos Y/U/V separadamente,
fazer a matriz BT.709 manualmente. Funciona, mas o Vulkan tem um caminho dedicado:
VK_KHR_sampler_ycbcr_conversion faz o próprio sampler fazer a
conversão YUV→RGB — uma chamada texture() retorna RGB, usando o
layout multi-plane do formato e os padrões de transmissão (BT.601/709/
2020, limited/full range) como estado do sampler.
O que ela faz por você
compare.txt
manual (grid shader):
sample Y plane, sample UV plane → vec2, do matrix math, scale
— full control, but every frame is shader work
ycbcr sampler:
texture(ycbcrSampler, uv) → vec4 RGB — sampler hardware does the
matrix; the "image" is a multi-planar VkImage the sampler reads
as oneConfigurando
ycbcr.cpp
// 1. the conversion object — the matrix lives here:
vk::SamplerYcbcrConversionCreateInfo yci{};
yci.format = vk::Format::eG8B8R82Plane420Unorm; // NV12 as a format
yci.ycbcrModel = vk::SamplerYcbcrModelConversion::eYcbcr709;
yci.ycbcrRange = vk::SamplerYcbcrRange::eItuNarrow; // limited range
yci.components = {}; // identity swizzle
yci.xChromaOffset = vk::ChromaLocation::eMidpoint;
yci.yChromaOffset = vk::ChromaLocation::eMidpoint;
auto conv = dev.createSamplerYcbcrConversion(yci);
// 2. a sampler bound to the conversion:
vk::SamplerYcbcrConversionInfo convInfo{conv};
vk::SamplerCreateInfo sci{};
sci.pNext = &convInfo;
auto samp = dev.createSampler(sci);
// 3. a multi-plane VkImage with the ycbcr format, bound to the DMA-Buf
// or uploaded planes — the *image* holds the planes, the *sampler*
// does the math
frag.frag
// the shader just samples:
layout(set=0,binding=0) uniform sampler2D tex; // ycbcr sampler
vec4 rgb = texture(tex, uv); // returns RGBAOnde cada caminho se encaixa
choose.txt
ycbcr sampler: standard video formats (BT.601/709/2020, limited/
full, common chroma sitings) — the driver knows the
spec, uses native HW blocks when they exist
manual shader: non-standard formats (P010→RGB with custom scaling),
per-frame color-matrix changes, filters that need
per-plane control (rgb_gain/hue/sat-style filters),
or when the format isn't in the ycbcr format listRegras práticas
- O formato deve ser um formato ycbcr
_2Plane/_3Plane(eG8B8R82Plane420Unorm,eG10X6B10X6R10X62Plane420Unorm3Pack16, …) — formatos YUYV empacotados precisam do estiloeG8B8G8R8422Unorm. ycbcrRangeé limited-vs-full — o clássico bug de “vídeo desbotado” é divergênciaeItuNarrowvseItuFullcom ocolor_rangedo stream.xChromaOffset/yChromaOffset(eMidpoint/eCositedEven) deve bater com o chroma siting do stream — MPEG-2 é co-sited, H.264 é midpoint; siting errado = deslocamento de croma de meio pixel.- O objeto de conversão é imutável — embuta parâmetros por stream
(matriz, range, siting) nele; um stream diferente precisa de um
VkSamplerYcbcrConversiondiferente. - Mantenha o caminho manual de shader mesmo assim — o sampler ycbcr cobre os
casos comuns; os filtros
cutout/hue/saturation/rgb_gainde um app de câmera precisam de controle por pixel que o sampler não dá.
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow