Skip to content

Taking advantage of Vulkan built-in YCbCr conversion #1537

Open

Description

@kvark

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:

  1. create VkSamplerYcbcrConversion object by specifying the format and the color model of the multi-planar image
  2. create VkSampler with the specified conversion
  3. create VkDescriptorSetLayout with 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.
  4. write to VkDescriptorSet (of this layout) with a VkImage that has a special way of binding multi-planar memory (at any point before rendering)
  5. 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

  1. sfricke-samsung commented on Mar 18, 2021

    @sfricke-samsung

    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

  2. shaoboyan commented on Mar 19, 2021

    @shaoboyan
    Contributor

    @kvark It's a great investigation of VK_KHR_sampler_ycbcr_conversion and 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_conversion as a possible solution to expand video_texture sampler? When browser extracts VideoFrame from HTMLVideoElement and 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) for VK_KHR_sampler_ycbcr_conversion in vulkan backend implementation?

    My concern is that let developer provide specific format from HTMLVideoElement is not easy. It is more like a hint because browser takes charge of decoding and not expose the info to developers.

  3. Kangz commented on Mar 19, 2021

    @Kangz
    Contributor

    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 GPUBindGroupLayout creation:

    • the format, and
    • the color space conversion, and
    • the sampling parameters

    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 rgba8unorm for 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 GPUExternalTexture without using YCbCr sampling in Vulkan, and see later (after v1) if we need an extension.

  4. kvark commented on Mar 19, 2021

    @kvark
    ContributorAuthor

    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 GPURenderPipeline alongside each codec. So we should keep this on our radar for the follow-up.

  5. added this to the post-MVP milestone on Aug 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions