Skip to content

Extract texture <-> buffer copy logic to wgpu-types #7553

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 49 additions & 57 deletions wgpu-core/src/command/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,96 +235,88 @@ pub(crate) fn validate_linear_texture_data(
copy_size: &Extent3d,
need_copy_aligned_rows: bool,
) -> Result<(BufferAddress, BufferAddress), TransferError> {
// Convert all inputs to BufferAddress (u64) to avoid some of the overflow issues
// Note: u64 is not always enough to prevent overflow, especially when multiplying
// something with a potentially large depth value, so it is preferable to validate
// the copy size before calling this function (for example via `validate_texture_copy_range`).
let copy_width = copy_size.width as BufferAddress;
let copy_height = copy_size.height as BufferAddress;
let depth_or_array_layers = copy_size.depth_or_array_layers as BufferAddress;

let offset = layout.offset;

let block_size = format.block_copy_size(Some(aspect)).unwrap() as BufferAddress;
let (block_width, block_height) = format.block_dimensions();
let block_width = block_width as BufferAddress;
let block_height = block_height as BufferAddress;

if copy_width % block_width != 0 {
let wgt::BufferTextureCopyInfo {
copy_width,
copy_height,
depth_or_array_layers,

offset,

block_size_bytes,
block_width_texels,
block_height_texels,

width_blocks: _,
height_blocks,

row_bytes_dense,
row_stride_bytes,

image_stride_rows: _,
image_stride_bytes,

image_rows_dense: _,
image_bytes_dense: _,

bytes_in_copy,
} = layout.get_buffer_texture_copy_info(format, aspect, copy_size);

if copy_width % block_width_texels != 0 {
return Err(TransferError::UnalignedCopyWidth);
}
if copy_height % block_height != 0 {
if copy_height % block_height_texels != 0 {
return Err(TransferError::UnalignedCopyHeight);
}

let width_in_blocks = copy_width / block_width;
let height_in_blocks = copy_height / block_height;

let bytes_in_last_row = width_in_blocks * block_size;
let requires_multiple_rows = depth_or_array_layers > 1 || height_blocks > 1;
let requires_multiple_images = depth_or_array_layers > 1;

let bytes_per_row = if let Some(bytes_per_row) = layout.bytes_per_row {
let bytes_per_row = bytes_per_row as BufferAddress;
if bytes_per_row < bytes_in_last_row {
if let Some(raw_bytes_per_row) = layout.bytes_per_row {
let raw_bytes_per_row = raw_bytes_per_row as BufferAddress;
if raw_bytes_per_row < row_bytes_dense {
return Err(TransferError::InvalidBytesPerRow);
}
bytes_per_row
} else {
if depth_or_array_layers > 1 || height_in_blocks > 1 {
return Err(TransferError::UnspecifiedBytesPerRow);
}
0
};
let rows_per_image = if let Some(rows_per_image) = layout.rows_per_image {
let rows_per_image = rows_per_image as BufferAddress;
if rows_per_image < height_in_blocks {
} else if requires_multiple_rows {
return Err(TransferError::UnspecifiedBytesPerRow);
}

if let Some(raw_rows_per_image) = layout.rows_per_image {
let raw_rows_per_image = raw_rows_per_image as BufferAddress;
if raw_rows_per_image < height_blocks {
return Err(TransferError::InvalidRowsPerImage);
}
rows_per_image
} else {
if depth_or_array_layers > 1 {
return Err(TransferError::UnspecifiedRowsPerImage);
}
0
} else if requires_multiple_images {
return Err(TransferError::UnspecifiedRowsPerImage);
};

if need_copy_aligned_rows {
let bytes_per_row_alignment = wgt::COPY_BYTES_PER_ROW_ALIGNMENT as BufferAddress;

let mut offset_alignment = block_size;
let mut offset_alignment = block_size_bytes;
if format.is_depth_stencil_format() {
offset_alignment = 4
}
if offset % offset_alignment != 0 {
return Err(TransferError::UnalignedBufferOffset(offset));
}

if bytes_per_row % bytes_per_row_alignment != 0 {
// The alignment of row_stride_bytes is only required if there are
// multiple rows
if requires_multiple_rows && row_stride_bytes % bytes_per_row_alignment != 0 {
return Err(TransferError::UnalignedBytesPerRow);
}
}

let bytes_per_image = bytes_per_row * rows_per_image;

let required_bytes_in_copy = if depth_or_array_layers == 0 {
0
} else {
let mut required_bytes_in_copy = bytes_per_image * (depth_or_array_layers - 1);
if height_in_blocks > 0 {
required_bytes_in_copy += bytes_per_row * (height_in_blocks - 1) + bytes_in_last_row;
}
required_bytes_in_copy
};

if offset + required_bytes_in_copy > buffer_size {
if offset + bytes_in_copy > buffer_size {
return Err(TransferError::BufferOverrun {
start_offset: offset,
end_offset: offset + required_bytes_in_copy,
end_offset: offset + bytes_in_copy,
buffer_size,
side: buffer_side,
});
}

Ok((required_bytes_in_copy, bytes_per_image))
Ok((bytes_in_copy, image_stride_bytes))
}

/// WebGPU's [validating texture copy range][vtcr] algorithm.
Expand Down
2 changes: 2 additions & 0 deletions wgpu-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ mod env;
mod features;
pub mod instance;
pub mod math;
mod transfers;

pub use counters::*;
pub use features::*;
pub use instance::*;
pub use transfers::*;

/// Integral type used for [`Buffer`] offsets and sizes.
///
Expand Down
Loading