Repository navigation
Description
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
depth16unormas 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
nearestfilter 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
kvark commentedon Apr 1, 2020
Thank you for the investigation!
So you are suggesting to add
depth16unormdespite 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).kainino0x commentedon Apr 1, 2020
No, they're proposed as extensions:
Jiawei-Shao commentedon Apr 7, 2020
Yes I think it is suitable to be an extension.
Added the investigation on the support of linear filter to the report. Vulkan does not require
VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BITbe supported on depth formats, so maybe we also have to disable using linear filters on depth formats.kvark commentedon Apr 8, 2020
@Jiawei-Shao is your conclusion that only Vulkan has optional filtering? Your table at the top reflects the inability of iOS to
Filtermost of the depth formats.Jiawei-Shao commentedon Apr 15, 2020
Hi @kvark, yes I think only Vulkan has optional linear filtering.
kvark commentedon Apr 15, 2020
@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:
kainino0x commentedon Apr 15, 2020
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.
kvark commentedon Apr 15, 2020
@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:
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.
kainino0x commentedon Apr 15, 2020
Gotcha, you're right, I think that's clear.
kainino0x commentedon Apr 17, 2020
Based on that, doesn't that mean we can also support depth16unorm in core? That would be really nice.
kainino0x commentedon Apr 17, 2020
#508 (comment)
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).
expenses commentedon Mar 7, 2021
I've stumbled apon an issue while using
wgpu-rsand 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 adepth32floattexture: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_BITshould be supported though:and the image view is 2D, so I have absolutely no clue what's going on here.
Kangz commentedon Mar 8, 2021
The checkboxes are for
VK_FORMAT_FEATURE_SAMPLED_IMAGE_BITandVK_FORMAT_FEATURE_BLIT_SRC_BIT, notVK_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.
expenses commentedon Mar 8, 2021
Oh, my god, I think I was just reading the table wrong 🤦🏻♀️
Kangz commentedon Sep 2, 2021
I think everything in this issue has been encoded in the spec but we need to double check. Putting v1 milesonte,
Jiawei-Shao commentedon Apr 21, 2022
Can we close this issue as we have already added the chapters (API, WGSL) about depth sampling in the SPEC?
Fix vertex_buffer_used_multiple_times_interleaved vertex OOB error (g…