Repository navigation
Description
Introduction
Explicit APIs all provide mechanisms to specify if we can create a texture view with different format when creating a texture to optimize the access of textures when the texture is never interpreted in a different format. This proposal intends to add a new usage bit in WebGPUTextureDescriptor to make full use of this type of optimization provided by all explicit APIs.
Native APIs
D3D12
In D3D, there are two ways to specify the layout (or memory footprint) of a resource:
- Typed - fully specify the type when the resource is created.
- Typeless - fully specify the type when the resource is bound to the pipeline.
D3D document also mentions creating a fully-typed resource enables the runtime to optimize access, especially if the resource is created with flags indicating that it cannot be mapped by the application.
However fully-typed resources cannot be reinterpreted using the view mechanism unless the resource was created with the D3D10_DDI_BIND_PRESENT flag.
On a typeless resource, the data type is unknown when the resource is first created. The exact data format (whether the memory will be interpreted as integers, floating point values, unsigned integers etc.) will not be determined until the resource is bound to the pipeline with a resource view. The format specified must be from the same family as the typeless format used when creating the resource. For example, a resource created with the R8G8B8A8_TYPELESS format cannot be viewed as a R32_FLOAT resource even though both formats may be the same size in memory.
Metal
In Metal, a texture must be created with MTLTextureUsagePixelFormatView usage if you want to call makeTextureView() on this texture.
Vulkan
Vulkan defines VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT for the member flag in VkImageViewCreateInfo struct. VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT specifies that the image can be used to create a VkImageView with a different format from the image.
If VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT is not set, the drivers will be able to do optimizations on the storage of the image. For example, according to the implementation in Mesa, for the images in some formats and created without this flag, their storage will be compressed inside the driver and the performance of all clearing, texturing and rendering operations on these images will be able to get improved.
Proposal
Here is our proposal on WebGPU texture view in IDL.
interface WebGPUTextureUsage {
const u32 NONE = 0;
const u32 TRANSFER_SRC = 1;
const u32 TRANSFER_DST = 2;
const u32 SAMPLED = 4;
const u32 STORAGE = 8;
const u32 OUTPUT_ATTACHMENT = 16;
const u32 PRESENT = 32;
const u32 VIEW_IN_DIFFERENT_FORMAT = 64;
};As D3D12, Metal and Vulkan all provide mechanisms to restrict the format of the texture view must be the same as the original texture, it is necessary to expose this ability to WebGPU.
In our proposal a new usage bit VIEW_IN_DIFFERENT_FORMAT is added to WebGPUTextureUsage to specify if we can create a texture view with a different format when creating the WebGPU texture. This usage bit can be implemented as follows on each backends:
- On D3D12, when the usage bit is set, the we create the D3D12 texture in a typeless format, otherwise we will use a typed format.
- On Metal, when the usage bit is set and the texture will be a 2D texture with only one mipmap level and one array layer, we can avoid setting the
MTLTextureUsagePixelFormatViewflag when creating Metal textures. - On Vulkan, when the usage bit is set, we can avoid setting
VK_IMAGE_CREATE_MUTABLE_FORMAT_BITwhen creating Vulkan images.
Activity
bbernhar commentedon Jan 17, 2019
GPU could support casting between fully typed resources; if so, typeless is not required. In D3D, this is done by querying
D3D12_FEATURE_DATA_D3D12_OPTIONSwhere the conversions are done in H/W transparently.kvark commentedon Jan 17, 2019
Thank you for investigation @Jiawei-Shao !
In general, I like the idea of specifying whether the user intends to create views of a texture with different formats up-front. The particular mechanism to do so is concerning: we have a clear definition of "usage", and we shouldn't add more flags there that don't fit this definition. Perhaps, we could introduce a new flag type:
interface WebGPUTextureCapabilities { const u32 NONE = 0; const u32 VIEW_AS_DIFFERENT_FORMAT = 1; const u32 VIEW_AS_CUBE = 2; };Compatibility spec
Major things missing from the investigation are:
Correction-1: D3D12
The link you provided for D3D12 API is really about D3D11 programming guide. The D3D12 docs on
CreateShaderResourceViewstate the following:Digging deeper, the rules for casting seem to be very complex: https://docs.microsoft.com/en-us/windows/desktop/direct3ddxgi/hardware-support-for-direct3d-12-0-formats
Look at the "PCS" versus "FCS" versus "FNS" there... ouch
Correction-2: D3D12
IIRC, this only applies to RTV and SRV, where the number of bits for each component must match. For UAVs D3D12 allows RGBA8 to be treated as Uint32, for example. I don't know if that's just specified on a case-by-case basis, or simply requires the total number of bits to match. Would be great to get some clarity from @RafaelCintron .
Correction-3: Metal
I don't think we can do this. We'd still need
MTLTextureUsagePixelFormatViewin order to make views of this texture in different formats, regardless of 2D/mipmap/layers.litherum commentedon Jan 23, 2019
https://developer.apple.com/documentation/metal/mtltexture/1515598-maketextureview
kdashg commentedon Jan 23, 2019
I prefer if we aim for the subset of "compatible classes" of formats between the underlying APIs.
Vulkan has largely the same classes as Metal, (Vulkan spec "Format Compatibility Classes") though Vulkan is more compatible between compressed textures, but does restrict some formats that are otherwise of the same bit-per-block count. (R10X6G10X6B10X6A10X6 is not compatible with R16G16B16A16)
@RafaelCintron will confirm what the compatibility landscape is for D3D12.
RafaelCintron commentedon Feb 9, 2019
Here is the breakdown of the D3D's texture families.
R32G32B32A32_UINT
R32G32B32A32_SINT
R32G32B32_UINT
R32G32B32_SINT
R16G16B16A16_UNORM
R16G16B16A16_UINT
R16G16B16A16_SNORM
R16G16B16A16_SINT
R32G32_UINT
R32G32_SINT
R32_FLOAT_X8X24_TYPELESS
X32_TYPELESS_G8X24_UINT
R10G10B10A2_UINT
R10G10B10_XR_BIAS_A2_UNORM
R8G8B8A8_UNORM_SRGB
R8G8B8A8_UINT
R8G8B8A8_SNORM
R8G8B8A8_SINT
R16G16_UNORM
R16G16_UINT
R16G16_SNORM
R16G16_SINT
R32_FLOAT
R32_UINT
R32_SINT
R24_UNORM_X8_TYPELESS
X24_TYPELESS_G8_UINT
R8G8_UINT
R8G8_SNORM
R8G8_SINT
D16_UNORM
R16_UNORM
R16_UINT
R16_SNORM
R16_SINT
R8_UINT
R8_SNORM
R8_SINT
R9G9B9E5_SHAREDEXP
R8G8_B8G8_UNORM
G8R8_G8B8_UNORM
BC1_UNORM_SRGB
BC2_UNORM_SRGB
BC3_UNORM_SRGB
BC4_SNORM
BC5_SNORM
B5G5R5A1_UNORM
B8G8R8A8_UNORM_SRGB
B8G8R8X8_UNORM_SRGB
BC6H_SF16
BC7_UNORM_SRGB
Y410
Y416
NV12
P010
P016
420_OPAQUE
YUY2
Y210
Y216
NV11
AI44
IA44
P8
A8P8
B4G4R4A4_UNORM
kvark commentedon Feb 9, 2019
@RafaelCintron documentation for SRV creation has an explicit section casting from typeless to typed formats, and the families you provided are applicable there. However, documentation for either RTV, DSV, or UAV don't mention any compatibility requirements with the original resource format. Please clarify if this is an oversight in documentation, or the rules for SRV are indeed different?
RafaelCintron commentedon Feb 20, 2019
@kvark , this information is missing from the documentation. The rules are the same for RTV and DSV as they are for SRV.
kvark commentedon Feb 20, 2019
@RafaelCintron I don't think this is true, or at least there appears to be something missing here.
Just tried to do a quick experiment: given a texture of format
R8G8B8A8_TYPELESSI tried creating SRV and UAV of typeR32_UINT. The SRV creation failed with the following explanation:The UAV however succeeded, even though the target type is outside of the source type family. There is also a relevant documentation piece about casting to
R32_UINT, although I'm not aware of a place that would fully specify these rules.RafaelCintron commentedon Feb 26, 2019
Thank you for keeping me honest, @kvark
kainino0x commentedon Aug 18, 2020
Hopefully comprehensive spreadsheet that we can reference when adding a "reinterpretable" flag: #744 (comment)
litherum commentedon Oct 25, 2021
What's the rationale for this being in V1?
Kangz commentedon Oct 25, 2021
RGB vs. SRGB seems pretty important but I don't know for sure. Other reinterpretations don't seem that useful for V1 though.
23 remaining items
fintelia commentedon Dec 3, 2021
Is this intended to capture the semantics of Vulkan's
VK_IMAGE_CREATE_BLOCK_TEXEL_VIEW_COMPATIBLE_BIT(which is slighly more constrained than "any two formats with the same byte size")? If not, I'd definitely encourage adding a family that does provide that functionality.kvark commentedon Dec 3, 2021
@kainino0x
That's a difficult enterprise. We'd have to understand all the open-source driver logic and anticipate what happens in closed-source drivers. We can't get it perfect.
There is a different approach we could take. We could define the set of formats that can be viewed with, portably, in all APIs. And then ask the user to specify exactly the ones they want:
I believe this is more future-proof, since we don't have to expose the nasty details of the drivers.
I also believe it's more straightforward for the users. If a user needs to cast between Srgb and non-Srgb, they'd check with the spec if it's possible, and pass in the cast format in this list. They wouldn't need to know which exact sub-class of castable formats this belongs to.
kainino0x commentedon Dec 3, 2021
Exactly my point, we can't get it perfect especially for future hardware. We can only get closer to perfect, with better granularity.
I think this is the same as #811. If we allowed a list of formats we should definitely always enforce strictly specified compatibility rules, not vary by platform.
Ah yeah, that was what I had in mind, I just forgot what the name in Vulkan was. (I just didn't think about it too hard because I think it's impossible in D3D12 anyway so we wouldn't be able to expose it.)
shaoboyan commentedon Jan 21, 2022
We got inputs from the mesa driver team about creating *-srgb texture views on non-srgb texture.
With descriptor indexing they just don't have a way to tell when an image is going to be used in SRGB/UNORM.
So we(Intel) support Kai's proposal.
kdashg commentedon Jan 22, 2022
WGSL meeting minutes 2022-01-19
ccs_ebased on whether srgb views exist of it. This I have no idea about. If the driver doesn't have to disableccs_eonR8G8B8A8_UNORMuntil you actually create a view with formatR8G8B8A8_UNORM_SRGB, then it would be significantly less of a problem. (edited)Add GPUTextureDescriptor viewFormats list
Add GPUTextureDescriptor viewFormats list
Add GPUTextureDescriptor viewFormats list (#2540)
litherum commentedon Jan 29, 2022
I think there's more to discuss here:
Kangz commentedon Jan 31, 2022
Each format should have a list of compatible formats (that is portable to all APIs of course) that are supposed to all be compatible with each other. Using a
viewFormatthat's not in theGPUTexture's format compatibility list produces an error.That wouldn't be allowed, the list of compatible formats should be portable (except for optional features of course).
I'm not sure I understand. On HD630 you get a slowdown. On other systems you don't. That's the point of having a creation argument that says whether there's reinterpretation.
Kangz commentedon Jan 31, 2022
Note that #2540 correctly restricts the set of formats that can be reinterpreted:
Kangz commentedon Mar 21, 2022
This landed in the spec, so tentatively closing this issue.
Fix texture binding usage test (gpuweb#168)