Skip to content

Investigation: Sampling Depth Textures #659

Closed

Description

@Jiawei-Shao

Sampling a depth texture is useful for some post-processing effects with depth textures. In this report we will talk about how to support sampling depth textures in WebGPU.

Depth Texture Formats

The following table summarizes the supports of sampling on all depth texture formats.

Note that on D3D12, it is allowed to create a texture in TYPELESS formats, on which we can create a shader resource view (SRV) with the format in the same group of the TYPELESS format. For example, considering texture in R32_TYPELESS format, and we can create both an SRV in R32_FLOAT format and a depth stencil view (DSV) in D32_FLOAT format.

On Metal, the reinterpretation of image data between pixel formats is supported within the groups listed in this Metal document, where none of the depth formats are supported. So we can conclude that it is not allowed to create a Metal texture view in a color format over an existing Metal texture in a depth format.

Vulkan SPEC requires that "each depth/stencil format is only compatible with itself" (Vulkan SPEC Chapter 39.1.6), so it is not allowed to create a Vulkan image view in color formats over an image in depth formats.

Another restriction in Vulkan is that none of the depth formats are required to support VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT, while in D3D12 and Metal there is no such limitation.

Depth Texture Formats Supported in WebGPU Support sampling in D3D12 ("Shader sample (any filter)") Support filtering in Metal (Metal-Feature-Set-Tables, Page 9) Support sampling in Vulkan (Vulkan SPEC Table 58)
depth32float Yes Yes (R32_FLOAT) macOS 10.11+, no iOS Yes
depth24unorm-stencil8 Yes (depth24plus-stencil8) Yes (R24_UNORM_X8_TYPELESS) macOS 10.11+ with depth24Stencil8PixelFormatSupported, no iOS No
depth32float-stencil8 Yes (depth24plus-stencil8) Yes (R32_FLOAT_X8X24_TYPELESS) macOS 10.11+, no iOS No
depth16unorm No Yes (R16_UNORM) macOS 10.12+, iOS 13.0+ Yes

Proposal

According to the table in the last chapter, we can conclude that there is no depth format that can be sampled on all WebGPU back-ends because of the limitations on Metal. As it is still useful to support sampling a depth texture, we suggest introducing the following two extensions:

  • Support depth16unorm as a new texture format, which can be exposed as a depth format that supports sampling on D3D12, Vulkan, macOS 10.12+ and iOS 13.0.
  • Support sampling with depth textures on depth32float, which can be exposed on D3D12, Vulkan and macOS 10.11+.
  • Only allow nearest filter on depth formats because of the limitation on Vulkan.

There are bigger limitations on the support of sampling with depth24unorm-stencil8 and depth32float-stencil8 so currently we don't consider them here.

Activity

  1. kvark commented on Apr 1, 2020

    @kvark
    Contributor

    Thank you for the investigation!

    So you are suggesting to add depth16unorm despite the fact it's not available on macOS 10.11 and iOS 10.12-?

    It would be nice to extend this investigation to linear sampling capabilities. Looks like we have some harder restrictions on which depth formats can be linearly sampled (i.e. VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT).

  2. kainino0x commented on Apr 1, 2020

    @kainino0x
    Contributor

    No, they're proposed as extensions:

    As it is still useful to support sampling a depth texture, we suggest introducing the following two extensions:

  3. Jiawei-Shao commented on Apr 7, 2020

    @Jiawei-Shao
    ContributorAuthor

    Thank you for the investigation!

    So you are suggesting to add depth16unorm despite the fact it's not available on macOS 10.11 and iOS 10.12-?

    Yes I think it is suitable to be an extension.

    It would be nice to extend this investigation to linear sampling capabilities. Looks like we have some harder restrictions on which depth formats can be linearly sampled (i.e. VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT).

    Added the investigation on the support of linear filter to the report. Vulkan does not require VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT be supported on depth formats, so maybe we also have to disable using linear filters on depth formats.

  4. kvark commented on Apr 8, 2020

    @kvark
    Contributor

    @Jiawei-Shao is your conclusion that only Vulkan has optional filtering? Your table at the top reflects the inability of iOS to Filter most of the depth formats.

  5. Jiawei-Shao commented on Apr 15, 2020

    @Jiawei-Shao
    ContributorAuthor

    @Jiawei-Shao is your conclusion that only Vulkan has optional filtering? Your table at the top reflects the inability of iOS to Filter most of the depth formats.

    Hi @kvark, yes I think only Vulkan has optional linear filtering.

  6. kvark commented on Apr 15, 2020

    @kvark
    Contributor

    @Jiawei-Shao perhaps @litherum will correct me, but it looks to me that "filter" in Metal actually refers to the non-nearest sampling, since the regular sampling is supported on all formats:

    All graphics and compute functions can read or sample from any texture, regardless of its pixel format. Additional capabilities are defined as follows:

    • Filter—the texture can be filtered during sampling.
  7. kainino0x commented on Apr 15, 2020

    @kainino0x
    Contributor

    It's not clear to me whether sample-with-nearest is counted as "filtering" by Metal, or if you have to use the equivalent of texelFetch. Not extremely different though, AFAIK.

  8. kvark commented on Apr 15, 2020

    @kvark
    Contributor

    @kainino0x Actually, I think the documentation is fairly clear. The table says "can read or sample from any texture". If you go to MSL documentation, these two things are defined:

    sample - A graphics or kernel function can sample the texture object. sample implies the ability to read from a texture with or without a sampler.
    read - Without a sampler, a graphics or kernel function can only read the texture object.

    If "Filter" applied to nearest sampling, it would basically conflict with the "All graphics and compute functions can read or sample from any texture, regardless of its pixel format" statement.

  9. kainino0x commented on Apr 15, 2020

    @kainino0x
    Contributor

    Gotcha, you're right, I think that's clear.

  10. kainino0x commented on Apr 17, 2020

    @kainino0x
    Contributor

    Based on that, doesn't that mean we can also support depth16unorm in core? That would be really nice.

  11. kainino0x commented on Apr 17, 2020

    @kainino0x
    Contributor

    #508 (comment)

    Although depth16unorm is available on all Metal devices, it requires macOS 10.12 and iOS 13, so we cannot bring this format into core WebGPU SPEC.

    I forget again what min macOS version we wanted to support. Maybe it would be okay to bring this into core since I don't think the iOS version really matters (Safari doesn't expect to ship WebGPU on old iOS anyway).

  12. expenses commented on Mar 7, 2021

    @expenses

    I've stumbled apon an issue while using wgpu-rs and I haven't found a better place to post about it than here. I'm getting the following vulkan validation error while trying to linearly sample a depth32float texture:

    VALIDATION [VUID-vkCmdDrawIndexed-magFilter-04553 (-1670231826)] : Validation Error: [ VUID-vkCmdDrawIndexed-magFilter-04553 ] Object 0: handle = 0x120000000012, name = cascaded shadow map - rendering bind group, type = VK_OBJECT_TYPE_DESCRIPTOR_SET; Object 1: handle = 0x70000000007, type = VK_OBJECT_TYPE_SAMPLER; Object 2: handle = 0x100000000010, type = VK_OBJECT_TYPE_IMAGE_VIEW; | MessageID = 0x9c7248ee | sampler (VkSampler 0x70000000007[]) in descriptor set (VkDescriptorSet 0x120000000012[cascaded shadow map - rendering bind group]) is set to use VK_FILTER_LINEAR with compareEnable is set to VK_TRUE, but image view's (VkImageView 0x100000000010[]) format (VK_FORMAT_D32_SFLOAT) does not contain VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT in its format features. The Vulkan spec states: If a VkSampler created with magFilter or minFilter equal to VK_FILTER_LINEAR and compareEnable equal to VK_FALSE is used to sample a VkImageView as a result of this command, then the image view's format features must contain VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT (https://www.khronos.org/registry/vulkan/specs/1.2-extensions/html/vkspec.html#VUID-vkCmdDrawIndexed-magFilter-04553)

    The key part here being (VK_FORMAT_D32_SFLOAT) does not contain VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT in its format features.

    The Vulkan 1.0 spec clearly(ish) states that VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT should be supported though:

    1615142762

    and the image view is 2D, so I have absolutely no clue what's going on here.

  13. Kangz commented on Mar 8, 2021

    @Kangz
    Contributor

    The checkboxes are for VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT and VK_FORMAT_FEATURE_BLIT_SRC_BIT, not VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT.

    The WebGPU spec already says "none of the depth formats can be filtered" so we're good here.

  14. expenses commented on Mar 8, 2021

    @expenses

    Oh, my god, I think I was just reading the table wrong 🤦🏻‍♀️

  15. Kangz commented on Sep 2, 2021

    @Kangz
    Contributor

    I think everything in this issue has been encoded in the spec but we need to double check. Putting v1 milesonte,

  16. added this to the V1.0 milestone on Sep 2, 2021
  17. added
    copyediting
    on Dec 14, 2021
  18. Jiawei-Shao commented on Apr 21, 2022

    @Jiawei-Shao
    ContributorAuthor

    Can we close this issue as we have already added the chapters (API, WGSL) about depth sampling in the SPEC?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    copyediting

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions