这是indexloc提供的服务,不要输入任何密码
Skip to content

Proposal: Add a usage bit to allow creating a view with different format when creating a texture #168

@Jiawei-Shao

Description

@Jiawei-Shao

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 MTLTextureUsagePixelFormatView flag when creating Metal textures.
  • On Vulkan, when the usage bit is set, we can avoid setting VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT when creating Vulkan images.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions