Repository navigation
Description
This investigation has branched out of #1415, related to this google doc discussion.
Support
VK_KHR_sampler_ycbcr_conversion (included in Vulkan 1.1) introduced built-in functionality to bind YCbCr images and sample them as RGB in the shaders. It's gated by samplerYcbcrConversion feature, which is available on more than half of the reports (see windows vk1.1 and android ext).
The specification also defines interaction with Android hardware buffers:
Images that will be backed by an Android hardware buffer can use an external format by setting VkImageCreateInfo::format to VK_FORMAT_UNDEFINED and including a VkExternalFormatANDROID structure in the pNext chain. Images can be created with an external format even if the Android hardware buffer has a format which has an equivalent Vulkan format to enable consistent handling of images from sources that might use either category of format.
@critsec kindly pointed out that Android CTS actually requires the feature to be supported on Vulkan-1.1 devices, so we can expect the coverage to grow.
API
To create a sampler with Y′CBCR conversion enabled, add a VkSamplerYcbcrConversionInfo structure to the pNext chain of the VkSamplerCreateInfo structure. To create a sampler Y′CBCR conversion, the samplerYcbcrConversion feature must be enabled. Conversion must be fixed at pipeline creation time, through use of a combined image sampler with an immutable sampler in VkDescriptorSetLayoutBinding.
A VkSamplerYcbcrConversionInfo must be provided for samplers to be used with image views that access VK_IMAGE_ASPECT_COLOR_BIT if the format appears in Formats requiring sampler Y′CBCR conversion for VK_IMAGE_ASPECT_COLOR_BIT image views , or if the image view has an external format .
In other words, the steps are:
- create
VkSamplerYcbcrConversionobject by specifying the format and the color model of the multi-planar image - create
VkSamplerwith the specified conversion - create
VkDescriptorSetLayoutwith a binding that is a combined image with an immutable sampler. This immutable sampler (that we created in the previous step) is provided right here. - write to
VkDescriptorSet(of this layout) with aVkImagethat has a special way of binding multi-planar memory (at any point before rendering) - business as usual: pipeline creation, binding, etc
Proposal
I think, accepting an external texture API that is incompatible with this functionality (available on most Vulkan devices) would be a big missed opportunity - to get driver-specific optimizations and lower the power cost of playing and processing videos.
We should try to make the external texture API compatible with it, instead, so that a Vulkan implementation can opt into using the built-in support if it can.
Interestingly, it also gives an implementation the advantage if it does not want to use Vulkan YCbCr, or even not using Vulkan entirely. Knowing everything that is to know about the external texture at GPUBindGroupLayout type allow us to generate the optimal shader code for the pipeline (since it's visible to createRenderPipeline).
In order to support this, #1415 host side would need to be modified, so that it requires a bit more data:
const layout = device.createBindGroupLayout({
entries: [{
binding: 0,
visibility: GPUShaderStage.FRAGMENT,
// creates `VkSamplerYcbcrConversion` and `VkSampler` on Vulkan
videoTexture: {
format: "y8cb8cr8_422",
},
}]
});Alternatively, if we want to support videos that change the format mid-play, we could make it occupy the whole bind group:
// returns `GPUBindGroupLayout`
const layout = device.createExternalBindGroupLayout({
visibility: GPUShaderStage.FRAGMENT,
// specify all the formats that will need to be supported here.
// a Vulkan implementation may create a separate `VkDescriptorSetLayout` for each, internally.
formats: [
"y8cb8cr8_422",
"y16cb16cr16_422",
],
});If this seems too complex, we could postpone the idea of external textures entirely to avoid being rushed. We can ship some version of a copyXxToTexture in MVP to cover the basic needs, while keeping working on the external texture API.
Disclaimer: this is a rough proposal, may be missing important details. I'm not an expert in video playback or color spaces. Any edits are welcome!
cc @chadversary @Kangz
Activity
sfricke-samsung commentedon Mar 18, 2021
There are some other subtle things with regards to YCbCr in Vulkan (disjoint,
combinedImageSamplerDescriptorCount, etc), there is a guide page on it:https://github.com/KhronosGroup/Vulkan-Guide/blob/master/chapters/extensions/VK_KHR_sampler_ycbcr_conversion.md
Also, I can confirm that over 90% of the YCbCr related VUs are in the validation layers to help catch edge cases, especially around the AHardwareBuffer spec text
shaoboyan commentedon Mar 19, 2021
@kvark It's a great investigation of
VK_KHR_sampler_ycbcr_conversionand I agree with that we should take advantage of this extension.But would it be possible that we always generate shaders take
VK_KHR_sampler_ycbcr_conversionas a possible solution to expandvideo_texturesampler? When browser extractsVideoFramefromHTMLVideoElementand the platform supports this extension, we set a flag (maybe through a uniform buffer) to use the extension to sample the binding resource.And for
BindGroupLayout, would it be possible to reserve several binding point(consider different conversion logic) forVK_KHR_sampler_ycbcr_conversionin vulkan backend implementation?My concern is that let developer provide specific
formatfromHTMLVideoElementis not easy. It is more like a hint because browser takes charge of decoding and not expose the info to developers.Kangz commentedon Mar 19, 2021
The concern I have with relying on Vulkan YCbCr samplers is that while they might sometimes be more performant, they come with a lot of constraints and inflexibility which wouldn't play well with how dynamic the Web platform is.
Like @kvark described WebGPU would need to require additional information at
GPUBindGroupLayoutcreation:This means that the application needs to know the format of the external texture when creating the pipeline. The problem is that for videos the format can be extremely dynamic: all of regular videos, livestreams and WebRTC videos can switch formats from frame to frame, for example when switching to a different resolution. This means that 1) the format would need to be exposed somehow, which is a big change to the Web platform (WebCodec anyone?) and 2) that the application needs to be designed to handle recompiling pipelines on the fly if needed. What's more even if the application controls its videos (which it might now always be able to), different browser (or the same on different systems) can choose a different format than what it expects. For example falling back to
rgba8unormfor software decoded videos.On the other hand the browsers know the set of all the formats they can support GPU-accelerated and produce the correct switch statement to inject in the shader. What's more videos can do more things than YUV and color conversion. Looking at the WebCodec VideoFrame the frames can also be cropped, and the browser could also take care of this with
GPUExternalTexture. Overall the browser does more for the application for not much more implementation complexity, and gives strong portability guarantees. There might be a small performance cost on some devices.We discussed this a bit with @kvark and I think the conclusion based on all of what's above is that we should do
GPUExternalTexturewithout using YCbCr sampling in Vulkan, and see later (after v1) if we need an extension.kvark commentedon Mar 19, 2021
I think this will work well with Web Codec, where the site has to know the formats anyway to create the codec instances for them, explicitly. They would just create
GPURenderPipelinealongside each codec. So we should keep this on our radar for the follow-up.Implement api,validation,capability_checks,features,texture_formats:c…