blob: 287e7e4ccce15a238c70f14d1b8f1b7f13de582d [file]
// Copyright 2026 The Dawn & Tint Authors
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef SRC_DAWN_NATIVE_BUFFERTEXTURECOPYOVERSIZEDROW_H_
#define SRC_DAWN_NATIVE_BUFFERTEXTURECOPYOVERSIZEDROW_H_
#include "absl/functional/function_ref.h"
#include "src/dawn/native/BlockInfo.h"
#include "src/dawn/native/Commands.h"
#include "src/dawn/native/Error.h"
namespace dawn::native {
class CommandAllocator;
class CommandEncoder;
// Determines whether a buffer<->texture copy needs to be split for oversized rows. This happens
// when `bufferCopy`'s bytes per row, texels per row, or multi-slice bytes per image overflows the
// limited-width hardware register field or 32-bit driver stride calculation used across multiple
// GPU vendors. See crbug.com/481934465, crbug.com/569161611, crbug.com/529867799,
// crbug.com/380487895, crbug.com/583083283, and crbug.com/584005138.
bool NeedsBufferTextureCopySplitForOversizedRow(const BufferCopy& bufferCopy,
const BlockExtent3D& copySize,
const TypedTexelBlockInfo& blockInfo);
// Same threshold check for a `TexelCopyBufferLayout` where `bytesPerRow` is not required to be a
// multiple of `blockInfo.byteSize` (e.g. `Queue::WriteTexture`).
bool NeedsBufferTextureCopySplitForOversizedRow(const TexelCopyBufferLayout& layout,
const BlockExtent3D& copySize,
const TypedTexelBlockInfo& blockInfo);
// Returns true when an oversized buffer<->texture copy cannot be split directly per row (because
// it is a multi-row depth/stencil copy whose single-slice per-row strides exceed limits, and
// depth/stencil copies must cover whole subresources) and must instead go through a compact
// temporary buffer.
bool NeedsTempBufferForOversizedRowCopy(const BufferCopy& bufferCopy,
const TextureCopy& textureCopy,
const BlockExtent3D& copySize,
const TypedTexelBlockInfo& blockInfo);
// Splits {bufferCopy, textureCopy, copySize} as needed so each sub-copy stays within
// kMaxTexelsPerRow, kMaxBytesPerRow, and kMaxBytesPerImage, invoking `func` for each one in order:
// a single copy unchanged if no split is needed, one sub-copy per slice if only multi-slice
// kMaxBytesPerImage is exceeded, otherwise one full-width sub-copy per unpadded row (every single
// row fits within the inclusive per-row limits since maxTextureDimension1D/2D <= 16384 and
// block.byteSize <= 16).
MaybeError ForEachSplitBufferTextureCopyForOversizedRow(
const BufferCopy& bufferCopy,
const TextureCopy& textureCopy,
const BlockExtent3D& copySize,
const TypedTexelBlockInfo& blockInfo,
absl::FunctionRef<MaybeError(const BufferCopy&, const TextureCopy&, const BlockExtent3D&)>
func);
// Allocates CopyTextureToBufferCmd(s) into `allocator`, splitting
// {bufferCopy, textureCopy, copySize} as ForEachSplitBufferTextureCopyForOversizedRow does and
// emitting a single command when no split is needed.
void SplitCopyTextureToBufferForOversizedRow(CommandAllocator* allocator,
const BufferCopy& bufferCopy,
const TextureCopy& textureCopy,
const BlockExtent3D& copySize,
const TypedTexelBlockInfo& blockInfo);
// Same as SplitCopyTextureToBufferForOversizedRow for the buffer-to-texture direction: allocates
// CopyBufferToTextureCmd(s) into `allocator` (a single command when no split is needed).
void SplitCopyBufferToTextureForOversizedRow(CommandAllocator* allocator,
const BufferCopy& bufferCopy,
const TextureCopy& textureCopy,
const BlockExtent3D& copySize,
const TypedTexelBlockInfo& blockInfo);
// Copies each slice of an oversized multi-row buffer-to-texture copy from `bufferCopy.buffer` into
// a compact temporary buffer, then allocates one full-slice CopyBufferToTextureCmd per slice into
// `allocator`. Requires the device guard to be held, `NeedsTempBufferForOversizedRowCopy` to be
// true, and 4-byte-aligned `bufferCopy.offset` and row byte stride.
MaybeError CopyBufferToTextureViaTempBufferForOversizedRow(CommandEncoder* commandEncoder,
CommandAllocator* allocator,
const BufferCopy& bufferCopy,
const TextureCopy& textureCopy,
const BlockExtent3D& copySize,
const TypedTexelBlockInfo& blockInfo);
// Copies each slice of an oversized multi-row texture-to-buffer copy into a compact temporary
// buffer by allocating one full-slice CopyTextureToBufferCmd per slice into `allocator`, then
// copies each row into `bufferCopy.buffer`. Requires the device guard to be held,
// `NeedsTempBufferForOversizedRowCopy` to be true, and 4-byte-aligned `bufferCopy.offset` and row
// byte stride.
MaybeError CopyTextureToBufferViaTempBufferForOversizedRow(CommandEncoder* commandEncoder,
CommandAllocator* allocator,
const TextureCopy& textureCopy,
const BufferCopy& bufferCopy,
const BlockExtent3D& copySize,
const TypedTexelBlockInfo& blockInfo);
} // namespace dawn::native
#endif // SRC_DAWN_NATIVE_BUFFERTEXTURECOPYOVERSIZEDROW_H_