Skip to content

Copies investigation (+ proposals) #69

Closed

Description

@Kangz

Copies between buffers

This one is fairly easy.

D3D12

It goes through the CopyBufferRegion call that doesn't seem to have particular alignment constraints.

// On ID3D12GraphicsCommandList
void CopyBufferRegion(
  ID3D12Resource *pDstBuffer,
  UINT64         DstOffset,
  ID3D12Resource *pSrcBuffer,
  UINT64         SrcOffset,
  UINT64         NumBytes
);

Metal

// On MTLBlitCommandEncoder
func copy(
    from: MTLBuffer,
    sourceOffset: Int,
    to: MTLBuffer,
    destinationOffset: Int,
    size: Int)

On MacOS offsets and the size must be aligned to 4.

Vulkan

void vkCmdCopyBuffer(
    VkCommandBuffer                             commandBuffer,
    VkBuffer                                    srcBuffer,
    VkBuffer                                    dstBuffer,
    uint32_t                                    regionCount,
    const VkBufferCopy*                         pRegions);

typedef struct VkBufferCopy {
    VkDeviceSize    srcOffset;
    VkDeviceSize    dstOffset;
    VkDeviceSize    size;
} VkBufferCopy;

The spec doesn't have constraints alignement constraints. VkPhysicalDeviceLimits::optimalBufferCopyOffsetAlignment isn't for vkCmdCopyBuffer.

Proposal

Because of Metal we need the alignement of 4 constraint. All APIs are the same except that Vulkan can take multiple copies in one function. What happens if multiple copies write to the same location is unclear in Vulkan, so we should probably have single copies in WebGPU. The sketch.webidl looks like this which seems ok (up to the u32 that might need to be replaced with a WebGPUDeviceSize):

    void copyBufferToBuffer(WebGPUBuffer src,
                            u32 srcOffset,
                            WebGPUBuffer dst,
                            u32 dstOffset,
                            u32 size);

Validation is that:

  • src must have the transferSrc usage and dst must have the transferDst usage
  • The source and destination regions are in bounds of the resources
  • The source and destination regions don't overlap

These validation rules are also required for all other copies below.

Copies between buffers and textures

This talks about copies from buffers to textures and assumes copies from textures to buffers are symmetric.

D3D12

// This is a "view" of a resource as a texture copy location.
//  - Textures use SubresourceIndex to select a slice.
//  - Buffers use PlacedFootprint to declare how to interpret their content as a linear texture.
typedef struct D3D12_TEXTURE_COPY_LOCATION {
  ID3D12Resource          *pResource;
  D3D12_TEXTURE_COPY_TYPE Type;
  union {
    D3D12_PLACED_SUBRESOURCE_FOOTPRINT PlacedFootprint;
    UINT                               SubresourceIndex;
  };
};

typedef struct D3D12_PLACED_SUBRESOURCE_FOOTPRINT {
  UINT64                      Offset;
  D3D12_SUBRESOURCE_FOOTPRINT Footprint;
};

typedef struct D3D12_SUBRESOURCE_FOOTPRINT {
  DXGI_FORMAT Format;
  UINT        Width;
  UINT        Height;
  UINT        Depth;
  UINT        RowPitch;
};

// On ID3D12GraphicsCommandList
// Does a copy between two textures, some of which can be buffer memory viewed as a linear texture.
void CopyTextureRegion(
  const D3D12_TEXTURE_COPY_LOCATION *pDst,
  UINT                              DstX,
  UINT                              DstY,
  UINT                              DstZ,
  const D3D12_TEXTURE_COPY_LOCATION *pSrc,
  const D3D12_BOX                   *pSrcBox
);

RowPitch must be aligned to 256 (and must be able to contain a full row) and our experimentation show that Offset must be aligned to 256 too. Texture formats of the "textures" must be compatible (same number of channels, same size). Depth-stencil or multisample resources must have whole subresource copied.

Metal

// On MTLBlitCommandEncoder
func copy(
    from: MTLBuffer,
    sourceOffset: Int,
    sourceBytesPerRow: Int,
    sourceBytesPerImage: Int,
    sourceSize: MTLSize,
    to: MTLTexture,
    destinationSlice: Int,
    destinationLevel: Int,
    destinationOrigin: MTLOrigin)

Constraints are that sourceOffset, sourceBytesPerRow and sourceBytesPerImage must be aligned to the pixel format size in bytes. sourceBytesPerRow must also be less than 32767 times the pixel size in bytes (wat).

There is a second version of the function with an extra argument to copy just the stencil, or just the depth of a packed depth-stencil format.

Vulkan

typedef struct VkImageSubresourceLayers {
    VkImageAspectFlags    aspectMask;
    uint32_t              mipLevel;
    uint32_t              baseArrayLayer;
    uint32_t              layerCount;
} VkImageSubresourceLayers;

typedef struct VkBufferImageCopy {
    VkDeviceSize                bufferOffset;
    uint32_t                    bufferRowLength;
    uint32_t                    bufferImageHeight;
    VkImageSubresourceLayers    imageSubresource;
    VkOffset3D                  imageOffset;
    VkExtent3D                  imageExtent;
} VkBufferImageCopy;

void vkCmdCopyBufferToImage(
    VkCommandBuffer                             commandBuffer,
    VkBuffer                                    srcBuffer,
    VkImage                                     dstImage,
    VkImageLayout                               dstImageLayout,
    uint32_t                                    regionCount,
    const VkBufferImageCopy*                    pRegions);

The only difference with Metal is that multiple copies can be done at the same time, and that multiple array slices can be done in one copy. It also supports multi-planar formats contrary to other APIs. Multisampled textures cannot be copied.

On transfer-only queues (no-compute, no-render), there is a minImageTransferGranularity requirement that can be arbitrary.

bufferOffset must be aligned to 4. width/height must be multiple of the block size for compressed formats. aspectMask must contain a single bit (no depth-stencil copies).

Copies between textures

D3D12

Same as buffer to texture.

Metal

// On MTLBlitCommandEncoder
func copy(
    from: MTLTexture,
    sourceSlice: Int,
    sourceLevel: Int,
    sourceOrigin: MTLOrigin,
    sourceSize: MTLSize,
    to: MTLTexture,
    destinationSlice: Int,
    destinationLevel: Int,
    destinationOrigin: MTLOrigin)

Vulkan

typedef struct VkImageCopy {
    VkImageSubresourceLayers    srcSubresource;
    VkOffset3D                  srcOffset;
    VkImageSubresourceLayers    dstSubresource;
    VkOffset3D                  dstOffset;
    VkExtent3D                  extent;
} VkImageCopy;

void vkCmdCopyImage(
    VkCommandBuffer                             commandBuffer,
    VkImage                                     srcImage,
    VkImageLayout                               srcImageLayout,
    VkImage                                     dstImage,
    VkImageLayout                               dstImageLayout,
    uint32_t                                    regionCount,
    const VkImageCopy*                          pRegions);

Proposal from copies involving textures

We can see that all APIs do copies between to view of GPU memory as textures, either from a texture, or a buffer viewed as a linear texture. This means we could phrase WebGPU this way too:

dictionnary WebGPUOrigin3D {
    u32 x;
    u32 y;
    u32 z;
};

dictionnary WebGPUExtent3D {
    u32 width;
    u32 height;
    u32 depth;
};

dictionary WebGPUBufferCopyView {
    WebGPUBuffer buffer;
    u32 offset;
    u32 rowPitch;
    u32 imageHeight;
}

dictionary WebGPUTextureCopyView {
    WebGPUTexture texture;
    u32 level;
    u32 slice;
    WebGPUOrigin3D origin;
    WebGPUTextureAspect aspect;
}

partial interface WebGPUCopyCommandRecordingThingy {
    void copyBufferToTexture(
        WebGPUBufferCopyView source,
        WebGPUTextureCopyView destination,
        WebGPUExtent3D copySize);

    void copyTextureToBuffer(
        WebGPUTextureCopyView source,
        WebGPUBufferCopyView destination,
        WebGPUExtent3D copySize);

    void copyTextureToTexture(
        WebGPUTextureCopyView source,
        WebGPUTextureCopyView destination,
        WebGPUExtent3D copySize);
}

Validation rules would include stuff like alignement for compressed formats, alignement for the buffer copy view members (for pixel format size, and 256 rowPitch for D3D12), aspects matching if needed, etc.

Activity

  1. kvark commented on Aug 17, 2018

    @kvark
    Contributor

    Thank you for the investigation! I agree with the proposed WebGPU API, I don't think there is much controversy about it.

    Note that batching multiple copies together allows our Vulkan portability implementation to reduce the number of render/compute passes done by the backend. However, I think for WebGPU we can constrain the problem space enough so that these workarounds (using compute/render for copy operations) aren't needed as much.

    RowPitch must be aligned to 256 (and must be able to contain a full row) and our experimentation show that Offset must be aligned to 256 too

    RowPitch must be aligned to D3D12_TEXTURE_DATA_PITCH_ALIGNMENT.
    Offset must be aligned to D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT, which is 512.

    Constraints are that sourceOffset, sourceBytesPerRow and sourceBytesPerImage must be aligned to the pixel format size in bytes.

    Doesn't sourceBytesPerRow have a minimum alignment of 4?

    Also, Metal feature tables define "Buffer alignment for copying an existing texture to a buffer" as the value between 16 and 256 bytes, depending on the platform.

  2. litherum commented on Sep 10, 2018

    @litherum
    Contributor

    We've discussed this internally, and we think the critical missing information is how big the performance cliff is. Depending on the size of the cliff, it will make either more ore less sense to offer a WebGL-style "you either get the fast path or the slow path, good luck!" or the native-API-style "Do it this way and if you don't you are bad and you should feel bad." Before making this decision, we should do more investigation.

  3. Kangz commented on Sep 11, 2018

    @Kangz
    ContributorAuthor

    In the meeting we agreed to go forward with this interface and discuss the restrictions more.

  4. devshgraphicsprogramming commented on Nov 14, 2018

    @devshgraphicsprogramming

    4 byte offset alignment restriction makes sense, some hardware may not be able to perform unaligned copies such as ARM (or will perform them much much slower).

    Moreover GPUs have dedicated DMA data transfer queues, which are asynchronous and simultaneous with Rasterization and Compute... you want to use these and the underlying Native API copy routines... not your own compute shader based workarounds.

  5. bjjones commented on Dec 4, 2018

    @bjjones
    Contributor

    I'd like to re-examine the inclusion of TextureAspectFlags within TextureCopyView

    This was merged to the IDL in #89.

    This parameter is not useful currently, as the texture's aspect can be determined from the texture's format.

    This could be useful if splitting depth and stencil components was a feature. This is available on Vulkan and Metal, but doesn't seem to be a feature for DX12.

    DX12

    Using CopyTextureRegion on DX12 requires both depth and stencil components to be copied. [MSDN]

    Note: If you use CopyTextureRegion with a depth-stencil buffer or a multisampled resource, you must copy the whole subresource.

    Metal

    Metal allows separate depth/stencil component copies through MTLBlitOption [Apple]

    // Blits the depth attachment portion of a combined depth/stencil resource.
    static var depthFromDepthStencil: MTLBlitOption
    
    // Blits the stencil attachment portion of a combined depth/stencil resource.
    static var stencilFromDepthStencil: MTLBlitOption
    

    Vulkan

    Vulkan actually requires depth and stencil components to be copied separately:

    Within VkImageSubresourceLayers [Khronos]:

    aspectMask is a combination of VkImageAspectFlagBits, selecting the color, depth and/or stencil aspects to be copied.

    And for valid usage within a VkBufferImageCopy structure [Khronos]:

    The aspectMask member of imageSubresource must only have a single bit set

    Proposal

    Since separate depth/stencil component copies isn't available on DX12, the feature should not be made available in WebGPU. Without this feature, there isn't a usage for TextureAspectFlags within TextureCopyView, and it should be removed.

  6. grovesNL commented on Dec 4, 2018

    @grovesNL
    Contributor

    @bjjones I think removing it from copy is consistent with the discussion in the linked PR (#89 (comment)), it seems like it just hasn't been removed yet.

  7. devshgraphicsprogramming commented on Dec 6, 2018

    @devshgraphicsprogramming

    This parameter is not useful currently, as the texture's aspect can be determined from the texture's format.

    Thats what the folks at The Forge assumed in their vulkan implementation, and then I came around trying to do stencil optimized SDF shadows and it turned out that you cannot fully determine aspect flags from texture format.

    I.e. for FBO attachment (render target) and texture copy you need both stencil+depth aspects for good performance and for it to work at all.

    However when you want a texture view to sample with in the Shaders, then you must create a texture view with only one aspect view.. so its only possible to sample either the depth value or the stencil value via a texture view from a depth-stencil image.

  8. Kangz commented on Sep 2, 2021

    @Kangz
    ContributorAuthor

    Closing this. The proposal has been accepted and later modified in various ways, including the addition of an aspect flag for texture source/destination subresources.

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

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions