| 1 |
use alloc::{sync::Arc, vec::Vec}; |
| 2 |
|
| 3 |
use arrayvec::ArrayVec; |
| 4 |
use thiserror::Error; |
| 5 |
use wgt::{BufferAddress, BufferUsages, Extent3d, TextureSelector, TextureUsages}; |
| 6 |
|
| 7 |
#[cfg(feature = "trace")] |
| 8 |
use crate::device::trace::Command as TraceCommand; |
| 9 |
use crate::{ |
| 10 |
api_log, |
| 11 |
command::{clear_texture, CommandEncoderError}, |
| 12 |
conv, |
| 13 |
device::{Device, DeviceError, MissingDownlevelFlags}, |
| 14 |
global::Global, |
| 15 |
id::{BufferId, CommandEncoderId, TextureId}, |
| 16 |
init_tracker::{ |
| 17 |
has_copy_partial_init_tracker_coverage, MemoryInitKind, TextureInitRange, |
| 18 |
TextureInitTrackerAction, |
| 19 |
}, |
| 20 |
resource::{ |
| 21 |
DestroyedResourceError, InvalidResourceError, MissingBufferUsageError, |
| 22 |
MissingTextureUsageError, ParentDevice, Texture, TextureErrorDimension, |
| 23 |
}, |
| 24 |
snatch::SnatchGuard, |
| 25 |
}; |
| 26 |
|
| 27 |
use super::{ClearError, CommandBufferMutable}; |
| 28 |
|
| 29 |
pub type TexelCopyBufferInfo = wgt::TexelCopyBufferInfo<BufferId>; |
| 30 |
pub type TexelCopyTextureInfo = wgt::TexelCopyTextureInfo<TextureId>; |
| 31 |
pub type CopyExternalImageDestInfo = wgt::CopyExternalImageDestInfo<TextureId>; |
| 32 |
|
| 33 |
#[deprecated( |
| 34 |
since = "24.0.0", |
| 35 |
note = "This has been renamed to `TexelCopyBufferInfo`, and will be removed in 25.0.0." |
| 36 |
)] |
| 37 |
pub type ImageCopyBuffer = wgt::TexelCopyBufferInfo<BufferId>; |
| 38 |
|
| 39 |
#[deprecated( |
| 40 |
since = "24.0.0", |
| 41 |
note = "This has been renamed to `TexelCopyTextureInfo`, and will be removed in 25.0.0." |
| 42 |
)] |
| 43 |
pub type ImageCopyTexture = wgt::TexelCopyTextureInfo<TextureId>; |
| 44 |
|
| 45 |
#[deprecated( |
| 46 |
since = "24.0.0", |
| 47 |
note = "This has been renamed to `TexelCopyTextureSourceInfo`, and will be removed in 25.0.0." |
| 48 |
)] |
| 49 |
pub type ImageCopyTextureTagged = wgt::CopyExternalImageDestInfo<TextureId>; |
| 50 |
|
| 51 |
#[derive(Clone, Copy, Debug)] |
| 52 |
pub enum CopySide { |
| 53 |
Source, |
| 54 |
Destination, |
| 55 |
} |
| 56 |
|
| 57 |
/// Error encountered while attempting a data transfer. |
| 58 |
#[derive(Clone, Debug, Error)] |
| 59 |
#[non_exhaustive] |
| 60 |
pub enum TransferError { |
| 61 |
#[error("Source and destination cannot be the same buffer")] |
| 62 |
SameSourceDestinationBuffer, |
| 63 |
#[error(transparent)] |
| 64 |
MissingBufferUsage(#[from] MissingBufferUsageError), |
| 65 |
#[error(transparent)] |
| 66 |
MissingTextureUsage(#[from] MissingTextureUsageError), |
| 67 |
#[error("Copy of {start_offset}..{end_offset} would end up overrunning the bounds of the {side:?} buffer of size {buffer_size}")] |
| 68 |
BufferOverrun { |
| 69 |
start_offset: BufferAddress, |
| 70 |
end_offset: BufferAddress, |
| 71 |
buffer_size: BufferAddress, |
| 72 |
side: CopySide, |
| 73 |
}, |
| 74 |
#[error("Copy of {dimension:?} {start_offset}..{end_offset} would end up overrunning the bounds of the {side:?} texture of {dimension:?} size {texture_size}")] |
| 75 |
TextureOverrun { |
| 76 |
start_offset: u32, |
| 77 |
end_offset: u32, |
| 78 |
texture_size: u32, |
| 79 |
dimension: TextureErrorDimension, |
| 80 |
side: CopySide, |
| 81 |
}, |
| 82 |
#[error("Unable to select texture aspect {aspect:?} from format {format:?}")] |
| 83 |
InvalidTextureAspect { |
| 84 |
format: wgt::TextureFormat, |
| 85 |
aspect: wgt::TextureAspect, |
| 86 |
}, |
| 87 |
#[error("Unable to select texture mip level {level} out of {total}")] |
| 88 |
InvalidTextureMipLevel { level: u32, total: u32 }, |
| 89 |
#[error("Texture dimension must be 2D when copying from an external texture")] |
| 90 |
InvalidDimensionExternal, |
| 91 |
#[error("Buffer offset {0} is not aligned to block size or `COPY_BUFFER_ALIGNMENT`")] |
| 92 |
UnalignedBufferOffset(BufferAddress), |
| 93 |
#[error("Copy size {0} does not respect `COPY_BUFFER_ALIGNMENT`")] |
| 94 |
UnalignedCopySize(BufferAddress), |
| 95 |
#[error("Copy width is not a multiple of block width")] |
| 96 |
UnalignedCopyWidth, |
| 97 |
#[error("Copy height is not a multiple of block height")] |
| 98 |
UnalignedCopyHeight, |
| 99 |
#[error("Copy origin's x component is not a multiple of block width")] |
| 100 |
UnalignedCopyOriginX, |
| 101 |
#[error("Copy origin's y component is not a multiple of block height")] |
| 102 |
UnalignedCopyOriginY, |
| 103 |
#[error("Bytes per row does not respect `COPY_BYTES_PER_ROW_ALIGNMENT`")] |
| 104 |
UnalignedBytesPerRow, |
| 105 |
#[error("Number of bytes per row needs to be specified since more than one row is copied")] |
| 106 |
UnspecifiedBytesPerRow, |
| 107 |
#[error("Number of rows per image needs to be specified since more than one image is copied")] |
| 108 |
UnspecifiedRowsPerImage, |
| 109 |
#[error("Number of bytes per row is less than the number of bytes in a complete row")] |
| 110 |
InvalidBytesPerRow, |
| 111 |
#[error("Number of rows per image is invalid")] |
| 112 |
InvalidRowsPerImage, |
| 113 |
#[error("Copy source aspects must refer to all aspects of the source texture format")] |
| 114 |
CopySrcMissingAspects, |
| 115 |
#[error( |
| 116 |
"Copy destination aspects must refer to all aspects of the destination texture format" |
| 117 |
)] |
| 118 |
CopyDstMissingAspects, |
| 119 |
#[error("Copy aspect must refer to a single aspect of texture format")] |
| 120 |
CopyAspectNotOne, |
| 121 |
#[error("Copying from textures with format {format:?} and aspect {aspect:?} is forbidden")] |
| 122 |
CopyFromForbiddenTextureFormat { |
| 123 |
format: wgt::TextureFormat, |
| 124 |
aspect: wgt::TextureAspect, |
| 125 |
}, |
| 126 |
#[error("Copying to textures with format {format:?} and aspect {aspect:?} is forbidden")] |
| 127 |
CopyToForbiddenTextureFormat { |
| 128 |
format: wgt::TextureFormat, |
| 129 |
aspect: wgt::TextureAspect, |
| 130 |
}, |
| 131 |
#[error( |
| 132 |
"Copying to textures with format {0:?} is forbidden when copying from external texture" |
| 133 |
)] |
| 134 |
ExternalCopyToForbiddenTextureFormat(wgt::TextureFormat), |
| 135 |
#[error( |
| 136 |
"Source format ({src_format:?}) and destination format ({dst_format:?}) are not copy-compatible (they may only differ in srgb-ness)" |
| 137 |
)] |
| 138 |
TextureFormatsNotCopyCompatible { |
| 139 |
src_format: wgt::TextureFormat, |
| 140 |
dst_format: wgt::TextureFormat, |
| 141 |
}, |
| 142 |
#[error(transparent)] |
| 143 |
MemoryInitFailure(#[from] ClearError), |
| 144 |
#[error("Cannot encode this copy because of a missing downelevel flag")] |
| 145 |
MissingDownlevelFlags(#[from] MissingDownlevelFlags), |
| 146 |
#[error("Source texture sample count must be 1, got {sample_count}")] |
| 147 |
InvalidSampleCount { sample_count: u32 }, |
| 148 |
#[error( |
| 149 |
"Source sample count ({src_sample_count:?}) and destination sample count ({dst_sample_count:?}) are not equal" |
| 150 |
)] |
| 151 |
SampleCountNotEqual { |
| 152 |
src_sample_count: u32, |
| 153 |
dst_sample_count: u32, |
| 154 |
}, |
| 155 |
#[error("Requested mip level {requested} does no exist (count: {count})")] |
| 156 |
InvalidMipLevel { requested: u32, count: u32 }, |
| 157 |
} |
| 158 |
|
| 159 |
/// Error encountered while attempting to do a copy on a command encoder. |
| 160 |
#[derive(Clone, Debug, Error)] |
| 161 |
#[non_exhaustive] |
| 162 |
pub enum CopyError { |
| 163 |
#[error(transparent)] |
| 164 |
Encoder(#[from] CommandEncoderError), |
| 165 |
#[error("Copy error")] |
| 166 |
Transfer(#[from] TransferError), |
| 167 |
#[error(transparent)] |
| 168 |
DestroyedResource(#[from] DestroyedResourceError), |
| 169 |
#[error(transparent)] |
| 170 |
InvalidResource(#[from] InvalidResourceError), |
| 171 |
} |
| 172 |
|
| 173 |
impl From<DeviceError> for CopyError { |
| 174 |
fn from(err: DeviceError) -> Self { |
| 175 |
CopyError::Encoder(CommandEncoderError::Device(err)) |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
pub(crate) fn extract_texture_selector<T>( |
| 180 |
copy_texture: &wgt::TexelCopyTextureInfo<T>, |
| 181 |
copy_size: &Extent3d, |
| 182 |
texture: &Texture, |
| 183 |
) -> Result<(TextureSelector, hal::TextureCopyBase), TransferError> { |
| 184 |
let format = texture.desc.format; |
| 185 |
let copy_aspect = hal::FormatAspects::new(format, copy_texture.aspect); |
| 186 |
if copy_aspect.is_empty() { |
| 187 |
return Err(TransferError::InvalidTextureAspect { |
| 188 |
format, |
| 189 |
aspect: copy_texture.aspect, |
| 190 |
}); |
| 191 |
} |
| 192 |
|
| 193 |
let (layers, origin_z) = match texture.desc.dimension { |
| 194 |
wgt::TextureDimension::D1 => (0..1, 0), |
| 195 |
wgt::TextureDimension::D2 => ( |
| 196 |
copy_texture.origin.z..copy_texture.origin.z + copy_size.depth_or_array_layers, |
| 197 |
0, |
| 198 |
), |
| 199 |
wgt::TextureDimension::D3 => (0..1, copy_texture.origin.z), |
| 200 |
}; |
| 201 |
let base = hal::TextureCopyBase { |
| 202 |
origin: wgt::Origin3d { |
| 203 |
x: copy_texture.origin.x, |
| 204 |
y: copy_texture.origin.y, |
| 205 |
z: origin_z, |
| 206 |
}, |
| 207 |
// this value will be incremented per copied layer |
| 208 |
array_layer: layers.start, |
| 209 |
mip_level: copy_texture.mip_level, |
| 210 |
aspect: copy_aspect, |
| 211 |
}; |
| 212 |
let selector = TextureSelector { |
| 213 |
mips: copy_texture.mip_level..copy_texture.mip_level + 1, |
| 214 |
layers, |
| 215 |
}; |
| 216 |
|
| 217 |
Ok((selector, base)) |
| 218 |
} |
| 219 |
|
| 220 |
/// WebGPU's [validating linear texture data][vltd] algorithm. |
| 221 |
/// |
| 222 |
/// Copied with some modifications from WebGPU standard. |
| 223 |
/// |
| 224 |
/// If successful, returns a pair `(bytes, stride)`, where: |
| 225 |
/// - `bytes` is the number of buffer bytes required for this copy, and |
| 226 |
/// - `stride` number of bytes between array layers. |
| 227 |
/// |
| 228 |
/// [vltd]: https://gpuweb.github.io/gpuweb/#abstract-opdef-validating-linear-texture-data |
| 229 |
pub(crate) fn validate_linear_texture_data( |
| 230 |
layout: &wgt::TexelCopyBufferLayout, |
| 231 |
format: wgt::TextureFormat, |
| 232 |
aspect: wgt::TextureAspect, |
| 233 |
buffer_size: BufferAddress, |
| 234 |
buffer_side: CopySide, |
| 235 |
copy_size: &Extent3d, |
| 236 |
need_copy_aligned_rows: bool, |
| 237 |
) -> Result<(BufferAddress, BufferAddress), TransferError> { |
| 238 |
let wgt::BufferTextureCopyInfo { |
| 239 |
copy_width, |
| 240 |
copy_height, |
| 241 |
depth_or_array_layers, |
| 242 |
|
| 243 |
offset, |
| 244 |
|
| 245 |
block_size_bytes, |
| 246 |
block_width_texels, |
| 247 |
block_height_texels, |
| 248 |
|
| 249 |
width_blocks: _, |
| 250 |
height_blocks, |
| 251 |
|
| 252 |
row_bytes_dense, |
| 253 |
row_stride_bytes, |
| 254 |
|
| 255 |
image_stride_rows: _, |
| 256 |
image_stride_bytes, |
| 257 |
|
| 258 |
image_rows_dense: _, |
| 259 |
image_bytes_dense: _, |
| 260 |
|
| 261 |
bytes_in_copy, |
| 262 |
} = layout.get_buffer_texture_copy_info(format, aspect, copy_size); |
| 263 |
|
| 264 |
if copy_width % block_width_texels != 0 { |
| 265 |
return Err(TransferError::UnalignedCopyWidth); |
| 266 |
} |
| 267 |
if copy_height % block_height_texels != 0 { |
| 268 |
return Err(TransferError::UnalignedCopyHeight); |
| 269 |
} |
| 270 |
|
| 271 |
let requires_multiple_rows = depth_or_array_layers > 1 || height_blocks > 1; |
| 272 |
let requires_multiple_images = depth_or_array_layers > 1; |
| 273 |
|
| 274 |
if let Some(raw_bytes_per_row) = layout.bytes_per_row { |
| 275 |
let raw_bytes_per_row = raw_bytes_per_row as BufferAddress; |
| 276 |
if raw_bytes_per_row < row_bytes_dense { |
| 277 |
return Err(TransferError::InvalidBytesPerRow); |
| 278 |
} |
| 279 |
} else if requires_multiple_rows { |
| 280 |
return Err(TransferError::UnspecifiedBytesPerRow); |
| 281 |
} |
| 282 |
|
| 283 |
if let Some(raw_rows_per_image) = layout.rows_per_image { |
| 284 |
let raw_rows_per_image = raw_rows_per_image as BufferAddress; |
| 285 |
if raw_rows_per_image < height_blocks { |
| 286 |
return Err(TransferError::InvalidRowsPerImage); |
| 287 |
} |
| 288 |
} else if requires_multiple_images { |
| 289 |
return Err(TransferError::UnspecifiedRowsPerImage); |
| 290 |
}; |
| 291 |
|
| 292 |
if need_copy_aligned_rows { |
| 293 |
let bytes_per_row_alignment = wgt::COPY_BYTES_PER_ROW_ALIGNMENT as BufferAddress; |
| 294 |
|
| 295 |
let mut offset_alignment = block_size_bytes; |
| 296 |
if format.is_depth_stencil_format() { |
| 297 |
offset_alignment = 4 |
| 298 |
} |
| 299 |
if offset % offset_alignment != 0 { |
| 300 |
return Err(TransferError::UnalignedBufferOffset(offset)); |
| 301 |
} |
| 302 |
|
| 303 |
// The alignment of row_stride_bytes is only required if there are |
| 304 |
// multiple rows |
| 305 |
if requires_multiple_rows && row_stride_bytes % bytes_per_row_alignment != 0 { |
| 306 |
return Err(TransferError::UnalignedBytesPerRow); |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
if offset + bytes_in_copy > buffer_size { |
| 311 |
return Err(TransferError::BufferOverrun { |
| 312 |
start_offset: offset, |
| 313 |
end_offset: offset + bytes_in_copy, |
| 314 |
buffer_size, |
| 315 |
side: buffer_side, |
| 316 |
}); |
| 317 |
} |
| 318 |
|
| 319 |
Ok((bytes_in_copy, image_stride_bytes)) |
| 320 |
} |
| 321 |
|
| 322 |
/// WebGPU's [validating texture copy range][vtcr] algorithm. |
| 323 |
/// |
| 324 |
/// Copied with minor modifications from WebGPU standard. |
| 325 |
/// |
| 326 |
/// Returns the HAL copy extent and the layer count. |
| 327 |
/// |
| 328 |
/// [vtcr]: https://gpuweb.github.io/gpuweb/#validating-texture-copy-range |
| 329 |
pub(crate) fn validate_texture_copy_range<T>( |
| 330 |
texture_copy_view: &wgt::TexelCopyTextureInfo<T>, |
| 331 |
desc: &wgt::TextureDescriptor<(), Vec<wgt::TextureFormat>>, |
| 332 |
texture_side: CopySide, |
| 333 |
copy_size: &Extent3d, |
| 334 |
) -> Result<(hal::CopyExtent, u32), TransferError> { |
| 335 |
let (block_width, block_height) = desc.format.block_dimensions(); |
| 336 |
|
| 337 |
let extent_virtual = desc.mip_level_size(texture_copy_view.mip_level).ok_or( |
| 338 |
TransferError::InvalidTextureMipLevel { |
| 339 |
level: texture_copy_view.mip_level, |
| 340 |
total: desc.mip_level_count, |
| 341 |
}, |
| 342 |
)?; |
| 343 |
// physical size can be larger than the virtual |
| 344 |
let extent = extent_virtual.physical_size(desc.format); |
| 345 |
|
| 346 |
/// Return `Ok` if a run `size` texels long starting at `start_offset` falls |
| 347 |
/// entirely within `texture_size`. Otherwise, return an appropriate a`Err`. |
| 348 |
fn check_dimension( |
| 349 |
dimension: TextureErrorDimension, |
| 350 |
side: CopySide, |
| 351 |
start_offset: u32, |
| 352 |
size: u32, |
| 353 |
texture_size: u32, |
| 354 |
) -> Result<(), TransferError> { |
| 355 |
// Avoid underflow in the subtraction by checking start_offset against |
| 356 |
// texture_size first. |
| 357 |
if start_offset <= texture_size && size <= texture_size - start_offset { |
| 358 |
Ok(()) |
| 359 |
} else { |
| 360 |
Err(TransferError::TextureOverrun { |
| 361 |
start_offset, |
| 362 |
end_offset: start_offset.wrapping_add(size), |
| 363 |
texture_size, |
| 364 |
dimension, |
| 365 |
side, |
| 366 |
}) |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
check_dimension( |
| 371 |
TextureErrorDimension::X, |
| 372 |
texture_side, |
| 373 |
texture_copy_view.origin.x, |
| 374 |
copy_size.width, |
| 375 |
extent.width, |
| 376 |
)?; |
| 377 |
check_dimension( |
| 378 |
TextureErrorDimension::Y, |
| 379 |
texture_side, |
| 380 |
texture_copy_view.origin.y, |
| 381 |
copy_size.height, |
| 382 |
extent.height, |
| 383 |
)?; |
| 384 |
check_dimension( |
| 385 |
TextureErrorDimension::Z, |
| 386 |
texture_side, |
| 387 |
texture_copy_view.origin.z, |
| 388 |
copy_size.depth_or_array_layers, |
| 389 |
extent.depth_or_array_layers, |
| 390 |
)?; |
| 391 |
|
| 392 |
if texture_copy_view.origin.x % block_width != 0 { |
| 393 |
return Err(TransferError::UnalignedCopyOriginX); |
| 394 |
} |
| 395 |
if texture_copy_view.origin.y % block_height != 0 { |
| 396 |
return Err(TransferError::UnalignedCopyOriginY); |
| 397 |
} |
| 398 |
if copy_size.width % block_width != 0 { |
| 399 |
return Err(TransferError::UnalignedCopyWidth); |
| 400 |
} |
| 401 |
if copy_size.height % block_height != 0 { |
| 402 |
return Err(TransferError::UnalignedCopyHeight); |
| 403 |
} |
| 404 |
|
| 405 |
let (depth, array_layer_count) = match desc.dimension { |
| 406 |
wgt::TextureDimension::D1 => (1, 1), |
| 407 |
wgt::TextureDimension::D2 => (1, copy_size.depth_or_array_layers), |
| 408 |
wgt::TextureDimension::D3 => (copy_size.depth_or_array_layers, 1), |
| 409 |
}; |
| 410 |
|
| 411 |
let copy_extent = hal::CopyExtent { |
| 412 |
width: copy_size.width, |
| 413 |
height: copy_size.height, |
| 414 |
depth, |
| 415 |
}; |
| 416 |
Ok((copy_extent, array_layer_count)) |
| 417 |
} |
| 418 |
|
| 419 |
fn handle_texture_init( |
| 420 |
init_kind: MemoryInitKind, |
| 421 |
cmd_buf_data: &mut CommandBufferMutable, |
| 422 |
device: &Device, |
| 423 |
copy_texture: &TexelCopyTextureInfo, |
| 424 |
copy_size: &Extent3d, |
| 425 |
texture: &Arc<Texture>, |
| 426 |
snatch_guard: &SnatchGuard<'_>, |
| 427 |
) -> Result<(), ClearError> { |
| 428 |
let init_action = TextureInitTrackerAction { |
| 429 |
texture: texture.clone(), |
| 430 |
range: TextureInitRange { |
| 431 |
mip_range: copy_texture.mip_level..copy_texture.mip_level + 1, |
| 432 |
layer_range: copy_texture.origin.z |
| 433 |
..(copy_texture.origin.z + copy_size.depth_or_array_layers), |
| 434 |
}, |
| 435 |
kind: init_kind, |
| 436 |
}; |
| 437 |
|
| 438 |
// Register the init action. |
| 439 |
let immediate_inits = cmd_buf_data |
| 440 |
.texture_memory_actions |
| 441 |
.register_init_action(&{ init_action }); |
| 442 |
|
| 443 |
// In rare cases we may need to insert an init operation immediately onto the command buffer. |
| 444 |
if !immediate_inits.is_empty() { |
| 445 |
let cmd_buf_raw = cmd_buf_data.encoder.open()?; |
| 446 |
for init in immediate_inits { |
| 447 |
clear_texture( |
| 448 |
&init.texture, |
| 449 |
TextureInitRange { |
| 450 |
mip_range: init.mip_level..(init.mip_level + 1), |
| 451 |
layer_range: init.layer..(init.layer + 1), |
| 452 |
}, |
| 453 |
cmd_buf_raw, |
| 454 |
&mut cmd_buf_data.trackers.textures, |
| 455 |
&device.alignments, |
| 456 |
device.zero_buffer.as_ref(), |
| 457 |
snatch_guard, |
| 458 |
)?; |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
Ok(()) |
| 463 |
} |
| 464 |
|
| 465 |
/// Prepare a transfer's source texture. |
| 466 |
/// |
| 467 |
/// Ensure the source texture of a transfer is in the right initialization |
| 468 |
/// state, and record the state for after the transfer operation. |
| 469 |
fn handle_src_texture_init( |
| 470 |
cmd_buf_data: &mut CommandBufferMutable, |
| 471 |
device: &Device, |
| 472 |
source: &TexelCopyTextureInfo, |
| 473 |
copy_size: &Extent3d, |
| 474 |
texture: &Arc<Texture>, |
| 475 |
snatch_guard: &SnatchGuard<'_>, |
| 476 |
) -> Result<(), TransferError> { |
| 477 |
handle_texture_init( |
| 478 |
MemoryInitKind::NeedsInitializedMemory, |
| 479 |
cmd_buf_data, |
| 480 |
device, |
| 481 |
source, |
| 482 |
copy_size, |
| 483 |
texture, |
| 484 |
snatch_guard, |
| 485 |
)?; |
| 486 |
Ok(()) |
| 487 |
} |
| 488 |
|
| 489 |
/// Prepare a transfer's destination texture. |
| 490 |
/// |
| 491 |
/// Ensure the destination texture of a transfer is in the right initialization |
| 492 |
/// state, and record the state for after the transfer operation. |
| 493 |
fn handle_dst_texture_init( |
| 494 |
cmd_buf_data: &mut CommandBufferMutable, |
| 495 |
device: &Device, |
| 496 |
destination: &TexelCopyTextureInfo, |
| 497 |
copy_size: &Extent3d, |
| 498 |
texture: &Arc<Texture>, |
| 499 |
snatch_guard: &SnatchGuard<'_>, |
| 500 |
) -> Result<(), TransferError> { |
| 501 |
// Attention: If we don't write full texture subresources, we need to a full |
| 502 |
// clear first since we don't track subrects. This means that in rare cases |
| 503 |
// even a *destination* texture of a transfer may need an immediate texture |
| 504 |
// init. |
| 505 |
let dst_init_kind = if has_copy_partial_init_tracker_coverage( |
| 506 |
copy_size, |
| 507 |
destination.mip_level, |
| 508 |
&texture.desc, |
| 509 |
) { |
| 510 |
MemoryInitKind::NeedsInitializedMemory |
| 511 |
} else { |
| 512 |
MemoryInitKind::ImplicitlyInitialized |
| 513 |
}; |
| 514 |
|
| 515 |
handle_texture_init( |
| 516 |
dst_init_kind, |
| 517 |
cmd_buf_data, |
| 518 |
device, |
| 519 |
destination, |
| 520 |
copy_size, |
| 521 |
texture, |
| 522 |
snatch_guard, |
| 523 |
)?; |
| 524 |
Ok(()) |
| 525 |
} |
| 526 |
|
| 527 |
impl Global { |
| 528 |
pub fn command_encoder_copy_buffer_to_buffer( |
| 529 |
&self, |
| 530 |
command_encoder_id: CommandEncoderId, |
| 531 |
source: BufferId, |
| 532 |
source_offset: BufferAddress, |
| 533 |
destination: BufferId, |
| 534 |
destination_offset: BufferAddress, |
| 535 |
size: BufferAddress, |
| 536 |
) -> Result<(), CopyError> { |
| 537 |
profiling::scope!("CommandEncoder::copy_buffer_to_buffer"); |
| 538 |
api_log!( |
| 539 |
"CommandEncoder::copy_buffer_to_buffer {source:?} -> {destination:?} {size:?}bytes" |
| 540 |
); |
| 541 |
|
| 542 |
if source == destination { |
| 543 |
return Err(TransferError::SameSourceDestinationBuffer.into()); |
| 544 |
} |
| 545 |
let hub = &self.hub; |
| 546 |
|
| 547 |
let cmd_buf = hub |
| 548 |
.command_buffers |
| 549 |
.get(command_encoder_id.into_command_buffer_id()); |
| 550 |
let mut cmd_buf_data = cmd_buf.data.lock(); |
| 551 |
let mut cmd_buf_data_guard = cmd_buf_data.record()?; |
| 552 |
let cmd_buf_data = &mut *cmd_buf_data_guard; |
| 553 |
|
| 554 |
let device = &cmd_buf.device; |
| 555 |
device.check_is_valid()?; |
| 556 |
|
| 557 |
#[cfg(feature = "trace")] |
| 558 |
if let Some(ref mut list) = cmd_buf_data.commands { |
| 559 |
list.push(TraceCommand::CopyBufferToBuffer { |
| 560 |
src: source, |
| 561 |
src_offset: source_offset, |
| 562 |
dst: destination, |
| 563 |
dst_offset: destination_offset, |
| 564 |
size, |
| 565 |
}); |
| 566 |
} |
| 567 |
|
| 568 |
let snatch_guard = device.snatchable_lock.read(); |
| 569 |
|
| 570 |
let src_buffer = hub.buffers.get(source).get()?; |
| 571 |
|
| 572 |
src_buffer.same_device_as(cmd_buf.as_ref())?; |
| 573 |
|
| 574 |
let src_pending = cmd_buf_data |
| 575 |
.trackers |
| 576 |
.buffers |
| 577 |
.set_single(&src_buffer, wgt::BufferUses::COPY_SRC); |
| 578 |
|
| 579 |
let src_raw = src_buffer.try_raw(&snatch_guard)?; |
| 580 |
src_buffer |
| 581 |
.check_usage(BufferUsages::COPY_SRC) |
| 582 |
.map_err(TransferError::MissingBufferUsage)?; |
| 583 |
// expecting only a single barrier |
| 584 |
let src_barrier = src_pending.map(|pending| pending.into_hal(&src_buffer, &snatch_guard)); |
| 585 |
|
| 586 |
let dst_buffer = hub.buffers.get(destination).get()?; |
| 587 |
|
| 588 |
dst_buffer.same_device_as(cmd_buf.as_ref())?; |
| 589 |
|
| 590 |
let dst_pending = cmd_buf_data |
| 591 |
.trackers |
| 592 |
.buffers |
| 593 |
.set_single(&dst_buffer, wgt::BufferUses::COPY_DST); |
| 594 |
|
| 595 |
let dst_raw = dst_buffer.try_raw(&snatch_guard)?; |
| 596 |
dst_buffer |
| 597 |
.check_usage(BufferUsages::COPY_DST) |
| 598 |
.map_err(TransferError::MissingBufferUsage)?; |
| 599 |
let dst_barrier = dst_pending.map(|pending| pending.into_hal(&dst_buffer, &snatch_guard)); |
| 600 |
|
| 601 |
if size % wgt::COPY_BUFFER_ALIGNMENT != 0 { |
| 602 |
return Err(TransferError::UnalignedCopySize(size).into()); |
| 603 |
} |
| 604 |
if source_offset % wgt::COPY_BUFFER_ALIGNMENT != 0 { |
| 605 |
return Err(TransferError::UnalignedBufferOffset(source_offset).into()); |
| 606 |
} |
| 607 |
if destination_offset % wgt::COPY_BUFFER_ALIGNMENT != 0 { |
| 608 |
return Err(TransferError::UnalignedBufferOffset(destination_offset).into()); |
| 609 |
} |
| 610 |
if !device |
| 611 |
.downlevel |
| 612 |
.flags |
| 613 |
.contains(wgt::DownlevelFlags::UNRESTRICTED_INDEX_BUFFER) |
| 614 |
&& (src_buffer.usage.contains(BufferUsages::INDEX) |
| 615 |
|| dst_buffer.usage.contains(BufferUsages::INDEX)) |
| 616 |
{ |
| 617 |
let forbidden_usages = BufferUsages::VERTEX |
| 618 |
| BufferUsages::UNIFORM |
| 619 |
| BufferUsages::INDIRECT |
| 620 |
| BufferUsages::STORAGE; |
| 621 |
if src_buffer.usage.intersects(forbidden_usages) |
| 622 |
|| dst_buffer.usage.intersects(forbidden_usages) |
| 623 |
{ |
| 624 |
return Err(TransferError::MissingDownlevelFlags(MissingDownlevelFlags( |
| 625 |
wgt::DownlevelFlags::UNRESTRICTED_INDEX_BUFFER, |
| 626 |
)) |
| 627 |
.into()); |
| 628 |
} |
| 629 |
} |
| 630 |
|
| 631 |
let source_end_offset = source_offset + size; |
| 632 |
let destination_end_offset = destination_offset + size; |
| 633 |
if source_end_offset > src_buffer.size { |
| 634 |
return Err(TransferError::BufferOverrun { |
| 635 |
start_offset: source_offset, |
| 636 |
end_offset: source_end_offset, |
| 637 |
buffer_size: src_buffer.size, |
| 638 |
side: CopySide::Source, |
| 639 |
} |
| 640 |
.into()); |
| 641 |
} |
| 642 |
if destination_end_offset > dst_buffer.size { |
| 643 |
return Err(TransferError::BufferOverrun { |
| 644 |
start_offset: destination_offset, |
| 645 |
end_offset: destination_end_offset, |
| 646 |
buffer_size: dst_buffer.size, |
| 647 |
side: CopySide::Destination, |
| 648 |
} |
| 649 |
.into()); |
| 650 |
} |
| 651 |
|
| 652 |
if size == 0 { |
| 653 |
log::trace!("Ignoring copy_buffer_to_buffer of size 0"); |
| 654 |
cmd_buf_data_guard.mark_successful(); |
| 655 |
return Ok(()); |
| 656 |
} |
| 657 |
|
| 658 |
// Make sure source is initialized memory and mark dest as initialized. |
| 659 |
cmd_buf_data.buffer_memory_init_actions.extend( |
| 660 |
dst_buffer.initialization_status.read().create_action( |
| 661 |
&dst_buffer, |
| 662 |
destination_offset..(destination_offset + size), |
| 663 |
MemoryInitKind::ImplicitlyInitialized, |
| 664 |
), |
| 665 |
); |
| 666 |
cmd_buf_data.buffer_memory_init_actions.extend( |
| 667 |
src_buffer.initialization_status.read().create_action( |
| 668 |
&src_buffer, |
| 669 |
source_offset..(source_offset + size), |
| 670 |
MemoryInitKind::NeedsInitializedMemory, |
| 671 |
), |
| 672 |
); |
| 673 |
|
| 674 |
let region = hal::BufferCopy { |
| 675 |
src_offset: source_offset, |
| 676 |
dst_offset: destination_offset, |
| 677 |
size: wgt::BufferSize::new(size).unwrap(), |
| 678 |
}; |
| 679 |
let cmd_buf_raw = cmd_buf_data.encoder.open()?; |
| 680 |
let barriers = src_barrier |
| 681 |
.into_iter() |
| 682 |
.chain(dst_barrier) |
| 683 |
.collect::<Vec<_>>(); |
| 684 |
unsafe { |
| 685 |
cmd_buf_raw.transition_buffers(&barriers); |
| 686 |
cmd_buf_raw.copy_buffer_to_buffer(src_raw, dst_raw, &[region]); |
| 687 |
} |
| 688 |
|
| 689 |
cmd_buf_data_guard.mark_successful(); |
| 690 |
Ok(()) |
| 691 |
} |
| 692 |
|
| 693 |
pub fn command_encoder_copy_buffer_to_texture( |
| 694 |
&self, |
| 695 |
command_encoder_id: CommandEncoderId, |
| 696 |
source: &TexelCopyBufferInfo, |
| 697 |
destination: &TexelCopyTextureInfo, |
| 698 |
copy_size: &Extent3d, |
| 699 |
) -> Result<(), CopyError> { |
| 700 |
profiling::scope!("CommandEncoder::copy_buffer_to_texture"); |
| 701 |
api_log!( |
| 702 |
"CommandEncoder::copy_buffer_to_texture {:?} -> {:?} {copy_size:?}", |
| 703 |
source.buffer, |
| 704 |
destination.texture |
| 705 |
); |
| 706 |
|
| 707 |
let hub = &self.hub; |
| 708 |
|
| 709 |
let cmd_buf = hub |
| 710 |
.command_buffers |
| 711 |
.get(command_encoder_id.into_command_buffer_id()); |
| 712 |
let mut cmd_buf_data = cmd_buf.data.lock(); |
| 713 |
let mut cmd_buf_data_guard = cmd_buf_data.record()?; |
| 714 |
let cmd_buf_data = &mut *cmd_buf_data_guard; |
| 715 |
|
| 716 |
let device = &cmd_buf.device; |
| 717 |
device.check_is_valid()?; |
| 718 |
|
| 719 |
#[cfg(feature = "trace")] |
| 720 |
if let Some(ref mut list) = cmd_buf_data.commands { |
| 721 |
list.push(TraceCommand::CopyBufferToTexture { |
| 722 |
src: *source, |
| 723 |
dst: *destination, |
| 724 |
size: *copy_size, |
| 725 |
}); |
| 726 |
} |
| 727 |
|
| 728 |
if copy_size.width == 0 || copy_size.height == 0 || copy_size.depth_or_array_layers == 0 { |
| 729 |
log::trace!("Ignoring copy_buffer_to_texture of size 0"); |
| 730 |
cmd_buf_data_guard.mark_successful(); |
| 731 |
return Ok(()); |
| 732 |
} |
| 733 |
|
| 734 |
let dst_texture = hub.textures.get(destination.texture).get()?; |
| 735 |
|
| 736 |
dst_texture.same_device_as(cmd_buf.as_ref())?; |
| 737 |
|
| 738 |
let (hal_copy_size, array_layer_count) = validate_texture_copy_range( |
| 739 |
destination, |
| 740 |
&dst_texture.desc, |
| 741 |
CopySide::Destination, |
| 742 |
copy_size, |
| 743 |
)?; |
| 744 |
|
| 745 |
let (dst_range, dst_base) = extract_texture_selector(destination, copy_size, &dst_texture)?; |
| 746 |
|
| 747 |
let snatch_guard = device.snatchable_lock.read(); |
| 748 |
|
| 749 |
// Handle texture init *before* dealing with barrier transitions so we |
| 750 |
// have an easier time inserting "immediate-inits" that may be required |
| 751 |
// by prior discards in rare cases. |
| 752 |
handle_dst_texture_init( |
| 753 |
cmd_buf_data, |
| 754 |
device, |
| 755 |
destination, |
| 756 |
copy_size, |
| 757 |
&dst_texture, |
| 758 |
&snatch_guard, |
| 759 |
)?; |
| 760 |
|
| 761 |
let src_buffer = hub.buffers.get(source.buffer).get()?; |
| 762 |
|
| 763 |
src_buffer.same_device_as(cmd_buf.as_ref())?; |
| 764 |
|
| 765 |
let src_pending = cmd_buf_data |
| 766 |
.trackers |
| 767 |
.buffers |
| 768 |
.set_single(&src_buffer, wgt::BufferUses::COPY_SRC); |
| 769 |
|
| 770 |
let src_raw = src_buffer.try_raw(&snatch_guard)?; |
| 771 |
src_buffer |
| 772 |
.check_usage(BufferUsages::COPY_SRC) |
| 773 |
.map_err(TransferError::MissingBufferUsage)?; |
| 774 |
let src_barrier = src_pending.map(|pending| pending.into_hal(&src_buffer, &snatch_guard)); |
| 775 |
|
| 776 |
let dst_pending = cmd_buf_data.trackers.textures.set_single( |
| 777 |
&dst_texture, |
| 778 |
dst_range, |
| 779 |
wgt::TextureUses::COPY_DST, |
| 780 |
); |
| 781 |
let dst_raw = dst_texture.try_raw(&snatch_guard)?; |
| 782 |
dst_texture |
| 783 |
.check_usage(TextureUsages::COPY_DST) |
| 784 |
.map_err(TransferError::MissingTextureUsage)?; |
| 785 |
let dst_barrier = dst_pending |
| 786 |
.map(|pending| pending.into_hal(dst_raw)) |
| 787 |
.collect::<Vec<_>>(); |
| 788 |
|
| 789 |
if !dst_base.aspect.is_one() { |
| 790 |
return Err(TransferError::CopyAspectNotOne.into()); |
| 791 |
} |
| 792 |
|
| 793 |
if !conv::is_valid_copy_dst_texture_format(dst_texture.desc.format, destination.aspect) { |
| 794 |
return Err(TransferError::CopyToForbiddenTextureFormat { |
| 795 |
format: dst_texture.desc.format, |
| 796 |
aspect: destination.aspect, |
| 797 |
} |
| 798 |
.into()); |
| 799 |
} |
| 800 |
|
| 801 |
let (required_buffer_bytes_in_copy, bytes_per_array_layer) = validate_linear_texture_data( |
| 802 |
&source.layout, |
| 803 |
dst_texture.desc.format, |
| 804 |
destination.aspect, |
| 805 |
src_buffer.size, |
| 806 |
CopySide::Source, |
| 807 |
copy_size, |
| 808 |
true, |
| 809 |
)?; |
| 810 |
|
| 811 |
if dst_texture.desc.format.is_depth_stencil_format() { |
| 812 |
device |
| 813 |
.require_downlevel_flags(wgt::DownlevelFlags::DEPTH_TEXTURE_AND_BUFFER_COPIES) |
| 814 |
.map_err(TransferError::from)?; |
| 815 |
} |
| 816 |
|
| 817 |
cmd_buf_data.buffer_memory_init_actions.extend( |
| 818 |
src_buffer.initialization_status.read().create_action( |
| 819 |
&src_buffer, |
| 820 |
source.layout.offset..(source.layout.offset + required_buffer_bytes_in_copy), |
| 821 |
MemoryInitKind::NeedsInitializedMemory, |
| 822 |
), |
| 823 |
); |
| 824 |
|
| 825 |
let regions = (0..array_layer_count) |
| 826 |
.map(|rel_array_layer| { |
| 827 |
let mut texture_base = dst_base.clone(); |
| 828 |
texture_base.array_layer += rel_array_layer; |
| 829 |
let mut buffer_layout = source.layout; |
| 830 |
buffer_layout.offset += rel_array_layer as u64 * bytes_per_array_layer; |
| 831 |
hal::BufferTextureCopy { |
| 832 |
buffer_layout, |
| 833 |
texture_base, |
| 834 |
size: hal_copy_size, |
| 835 |
} |
| 836 |
}) |
| 837 |
.collect::<Vec<_>>(); |
| 838 |
|
| 839 |
let cmd_buf_raw = cmd_buf_data.encoder.open()?; |
| 840 |
unsafe { |
| 841 |
cmd_buf_raw.transition_textures(&dst_barrier); |
| 842 |
cmd_buf_raw.transition_buffers(src_barrier.as_slice()); |
| 843 |
cmd_buf_raw.copy_buffer_to_texture(src_raw, dst_raw, ®ions); |
| 844 |
} |
| 845 |
|
| 846 |
cmd_buf_data_guard.mark_successful(); |
| 847 |
Ok(()) |
| 848 |
} |
| 849 |
|
| 850 |
pub fn command_encoder_copy_texture_to_buffer( |
| 851 |
&self, |
| 852 |
command_encoder_id: CommandEncoderId, |
| 853 |
source: &TexelCopyTextureInfo, |
| 854 |
destination: &TexelCopyBufferInfo, |
| 855 |
copy_size: &Extent3d, |
| 856 |
) -> Result<(), CopyError> { |
| 857 |
profiling::scope!("CommandEncoder::copy_texture_to_buffer"); |
| 858 |
api_log!( |
| 859 |
"CommandEncoder::copy_texture_to_buffer {:?} -> {:?} {copy_size:?}", |
| 860 |
source.texture, |
| 861 |
destination.buffer |
| 862 |
); |
| 863 |
|
| 864 |
let hub = &self.hub; |
| 865 |
|
| 866 |
let cmd_buf = hub |
| 867 |
.command_buffers |
| 868 |
.get(command_encoder_id.into_command_buffer_id()); |
| 869 |
let mut cmd_buf_data = cmd_buf.data.lock(); |
| 870 |
let mut cmd_buf_data_guard = cmd_buf_data.record()?; |
| 871 |
let cmd_buf_data = &mut *cmd_buf_data_guard; |
| 872 |
|
| 873 |
let device = &cmd_buf.device; |
| 874 |
device.check_is_valid()?; |
| 875 |
|
| 876 |
#[cfg(feature = "trace")] |
| 877 |
if let Some(list) = cmd_buf_data.commands.as_mut() { |
| 878 |
list.push(TraceCommand::CopyTextureToBuffer { |
| 879 |
src: *source, |
| 880 |
dst: *destination, |
| 881 |
size: *copy_size, |
| 882 |
}); |
| 883 |
} |
| 884 |
|
| 885 |
if copy_size.width == 0 || copy_size.height == 0 || copy_size.depth_or_array_layers == 0 { |
| 886 |
log::trace!("Ignoring copy_texture_to_buffer of size 0"); |
| 887 |
cmd_buf_data_guard.mark_successful(); |
| 888 |
return Ok(()); |
| 889 |
} |
| 890 |
|
| 891 |
let src_texture = hub.textures.get(source.texture).get()?; |
| 892 |
|
| 893 |
src_texture.same_device_as(cmd_buf.as_ref())?; |
| 894 |
|
| 895 |
let (hal_copy_size, array_layer_count) = |
| 896 |
validate_texture_copy_range(source, &src_texture.desc, CopySide::Source, copy_size)?; |
| 897 |
|
| 898 |
let (src_range, src_base) = extract_texture_selector(source, copy_size, &src_texture)?; |
| 899 |
|
| 900 |
let snatch_guard = device.snatchable_lock.read(); |
| 901 |
|
| 902 |
// Handle texture init *before* dealing with barrier transitions so we |
| 903 |
// have an easier time inserting "immediate-inits" that may be required |
| 904 |
// by prior discards in rare cases. |
| 905 |
handle_src_texture_init( |
| 906 |
cmd_buf_data, |
| 907 |
device, |
| 908 |
source, |
| 909 |
copy_size, |
| 910 |
&src_texture, |
| 911 |
&snatch_guard, |
| 912 |
)?; |
| 913 |
|
| 914 |
let src_pending = cmd_buf_data.trackers.textures.set_single( |
| 915 |
&src_texture, |
| 916 |
src_range, |
| 917 |
wgt::TextureUses::COPY_SRC, |
| 918 |
); |
| 919 |
let src_raw = src_texture.try_raw(&snatch_guard)?; |
| 920 |
src_texture |
| 921 |
.check_usage(TextureUsages::COPY_SRC) |
| 922 |
.map_err(TransferError::MissingTextureUsage)?; |
| 923 |
if src_texture.desc.sample_count != 1 { |
| 924 |
return Err(TransferError::InvalidSampleCount { |
| 925 |
sample_count: src_texture.desc.sample_count, |
| 926 |
} |
| 927 |
.into()); |
| 928 |
} |
| 929 |
if source.mip_level >= src_texture.desc.mip_level_count { |
| 930 |
return Err(TransferError::InvalidMipLevel { |
| 931 |
requested: source.mip_level, |
| 932 |
count: src_texture.desc.mip_level_count, |
| 933 |
} |
| 934 |
.into()); |
| 935 |
} |
| 936 |
let src_barrier = src_pending |
| 937 |
.map(|pending| pending.into_hal(src_raw)) |
| 938 |
.collect::<Vec<_>>(); |
| 939 |
|
| 940 |
let dst_buffer = hub.buffers.get(destination.buffer).get()?; |
| 941 |
|
| 942 |
dst_buffer.same_device_as(cmd_buf.as_ref())?; |
| 943 |
|
| 944 |
let dst_pending = cmd_buf_data |
| 945 |
.trackers |
| 946 |
.buffers |
| 947 |
.set_single(&dst_buffer, wgt::BufferUses::COPY_DST); |
| 948 |
|
| 949 |
let dst_raw = dst_buffer.try_raw(&snatch_guard)?; |
| 950 |
dst_buffer |
| 951 |
.check_usage(BufferUsages::COPY_DST) |
| 952 |
.map_err(TransferError::MissingBufferUsage)?; |
| 953 |
let dst_barrier = dst_pending.map(|pending| pending.into_hal(&dst_buffer, &snatch_guard)); |
| 954 |
|
| 955 |
if !src_base.aspect.is_one() { |
| 956 |
return Err(TransferError::CopyAspectNotOne.into()); |
| 957 |
} |
| 958 |
|
| 959 |
if !conv::is_valid_copy_src_texture_format(src_texture.desc.format, source.aspect) { |
| 960 |
return Err(TransferError::CopyFromForbiddenTextureFormat { |
| 961 |
format: src_texture.desc.format, |
| 962 |
aspect: source.aspect, |
| 963 |
} |
| 964 |
.into()); |
| 965 |
} |
| 966 |
|
| 967 |
let (required_buffer_bytes_in_copy, bytes_per_array_layer) = validate_linear_texture_data( |
| 968 |
&destination.layout, |
| 969 |
src_texture.desc.format, |
| 970 |
source.aspect, |
| 971 |
dst_buffer.size, |
| 972 |
CopySide::Destination, |
| 973 |
copy_size, |
| 974 |
true, |
| 975 |
)?; |
| 976 |
|
| 977 |
if src_texture.desc.format.is_depth_stencil_format() { |
| 978 |
device |
| 979 |
.require_downlevel_flags(wgt::DownlevelFlags::DEPTH_TEXTURE_AND_BUFFER_COPIES) |
| 980 |
.map_err(TransferError::from)?; |
| 981 |
} |
| 982 |
|
| 983 |
cmd_buf_data.buffer_memory_init_actions.extend( |
| 984 |
dst_buffer.initialization_status.read().create_action( |
| 985 |
&dst_buffer, |
| 986 |
destination.layout.offset |
| 987 |
..(destination.layout.offset + required_buffer_bytes_in_copy), |
| 988 |
MemoryInitKind::ImplicitlyInitialized, |
| 989 |
), |
| 990 |
); |
| 991 |
|
| 992 |
let regions = (0..array_layer_count) |
| 993 |
.map(|rel_array_layer| { |
| 994 |
let mut texture_base = src_base.clone(); |
| 995 |
texture_base.array_layer += rel_array_layer; |
| 996 |
let mut buffer_layout = destination.layout; |
| 997 |
buffer_layout.offset += rel_array_layer as u64 * bytes_per_array_layer; |
| 998 |
hal::BufferTextureCopy { |
| 999 |
buffer_layout, |
| 1000 |
texture_base, |
| 1001 |
size: hal_copy_size, |
| 1002 |
} |
| 1003 |
}) |
| 1004 |
.collect::<Vec<_>>(); |
| 1005 |
let cmd_buf_raw = cmd_buf_data.encoder.open()?; |
| 1006 |
unsafe { |
| 1007 |
cmd_buf_raw.transition_buffers(dst_barrier.as_slice()); |
| 1008 |
cmd_buf_raw.transition_textures(&src_barrier); |
| 1009 |
cmd_buf_raw.copy_texture_to_buffer( |
| 1010 |
src_raw, |
| 1011 |
wgt::TextureUses::COPY_SRC, |
| 1012 |
dst_raw, |
| 1013 |
®ions, |
| 1014 |
); |
| 1015 |
} |
| 1016 |
|
| 1017 |
cmd_buf_data_guard.mark_successful(); |
| 1018 |
Ok(()) |
| 1019 |
} |
| 1020 |
|
| 1021 |
pub fn command_encoder_copy_texture_to_texture( |
| 1022 |
&self, |
| 1023 |
command_encoder_id: CommandEncoderId, |
| 1024 |
source: &TexelCopyTextureInfo, |
| 1025 |
destination: &TexelCopyTextureInfo, |
| 1026 |
copy_size: &Extent3d, |
| 1027 |
) -> Result<(), CopyError> { |
| 1028 |
profiling::scope!("CommandEncoder::copy_texture_to_texture"); |
| 1029 |
api_log!( |
| 1030 |
"CommandEncoder::copy_texture_to_texture {:?} -> {:?} {copy_size:?}", |
| 1031 |
source.texture, |
| 1032 |
destination.texture |
| 1033 |
); |
| 1034 |
|
| 1035 |
let hub = &self.hub; |
| 1036 |
|
| 1037 |
let cmd_buf = hub |
| 1038 |
.command_buffers |
| 1039 |
.get(command_encoder_id.into_command_buffer_id()); |
| 1040 |
let mut cmd_buf_data = cmd_buf.data.lock(); |
| 1041 |
let mut cmd_buf_data_guard = cmd_buf_data.record()?; |
| 1042 |
let cmd_buf_data = &mut *cmd_buf_data_guard; |
| 1043 |
|
| 1044 |
let device = &cmd_buf.device; |
| 1045 |
device.check_is_valid()?; |
| 1046 |
|
| 1047 |
let snatch_guard = device.snatchable_lock.read(); |
| 1048 |
|
| 1049 |
#[cfg(feature = "trace")] |
| 1050 |
if let Some(ref mut list) = cmd_buf_data.commands { |
| 1051 |
list.push(TraceCommand::CopyTextureToTexture { |
| 1052 |
src: *source, |
| 1053 |
dst: *destination, |
| 1054 |
size: *copy_size, |
| 1055 |
}); |
| 1056 |
} |
| 1057 |
|
| 1058 |
if copy_size.width == 0 || copy_size.height == 0 || copy_size.depth_or_array_layers == 0 { |
| 1059 |
log::trace!("Ignoring copy_texture_to_texture of size 0"); |
| 1060 |
cmd_buf_data_guard.mark_successful(); |
| 1061 |
return Ok(()); |
| 1062 |
} |
| 1063 |
|
| 1064 |
let src_texture = hub.textures.get(source.texture).get()?; |
| 1065 |
let dst_texture = hub.textures.get(destination.texture).get()?; |
| 1066 |
|
| 1067 |
src_texture.same_device_as(cmd_buf.as_ref())?; |
| 1068 |
dst_texture.same_device_as(cmd_buf.as_ref())?; |
| 1069 |
|
| 1070 |
// src and dst texture format must be copy-compatible |
| 1071 |
// https://gpuweb.github.io/gpuweb/#copy-compatible |
| 1072 |
if src_texture.desc.format.remove_srgb_suffix() |
| 1073 |
!= dst_texture.desc.format.remove_srgb_suffix() |
| 1074 |
{ |
| 1075 |
return Err(TransferError::TextureFormatsNotCopyCompatible { |
| 1076 |
src_format: src_texture.desc.format, |
| 1077 |
dst_format: dst_texture.desc.format, |
| 1078 |
} |
| 1079 |
.into()); |
| 1080 |
} |
| 1081 |
|
| 1082 |
let (src_copy_size, array_layer_count) = |
| 1083 |
validate_texture_copy_range(source, &src_texture.desc, CopySide::Source, copy_size)?; |
| 1084 |
let (dst_copy_size, _) = validate_texture_copy_range( |
| 1085 |
destination, |
| 1086 |
&dst_texture.desc, |
| 1087 |
CopySide::Destination, |
| 1088 |
copy_size, |
| 1089 |
)?; |
| 1090 |
|
| 1091 |
let (src_range, src_tex_base) = extract_texture_selector(source, copy_size, &src_texture)?; |
| 1092 |
let (dst_range, dst_tex_base) = |
| 1093 |
extract_texture_selector(destination, copy_size, &dst_texture)?; |
| 1094 |
let src_texture_aspects = hal::FormatAspects::from(src_texture.desc.format); |
| 1095 |
let dst_texture_aspects = hal::FormatAspects::from(dst_texture.desc.format); |
| 1096 |
if src_tex_base.aspect != src_texture_aspects { |
| 1097 |
return Err(TransferError::CopySrcMissingAspects.into()); |
| 1098 |
} |
| 1099 |
if dst_tex_base.aspect != dst_texture_aspects { |
| 1100 |
return Err(TransferError::CopyDstMissingAspects.into()); |
| 1101 |
} |
| 1102 |
|
| 1103 |
if src_texture.desc.sample_count != dst_texture.desc.sample_count { |
| 1104 |
return Err(TransferError::SampleCountNotEqual { |
| 1105 |
src_sample_count: src_texture.desc.sample_count, |
| 1106 |
dst_sample_count: dst_texture.desc.sample_count, |
| 1107 |
} |
| 1108 |
.into()); |
| 1109 |
} |
| 1110 |
|
| 1111 |
// Handle texture init *before* dealing with barrier transitions so we |
| 1112 |
// have an easier time inserting "immediate-inits" that may be required |
| 1113 |
// by prior discards in rare cases. |
| 1114 |
handle_src_texture_init( |
| 1115 |
cmd_buf_data, |
| 1116 |
device, |
| 1117 |
source, |
| 1118 |
copy_size, |
| 1119 |
&src_texture, |
| 1120 |
&snatch_guard, |
| 1121 |
)?; |
| 1122 |
handle_dst_texture_init( |
| 1123 |
cmd_buf_data, |
| 1124 |
device, |
| 1125 |
destination, |
| 1126 |
copy_size, |
| 1127 |
&dst_texture, |
| 1128 |
&snatch_guard, |
| 1129 |
)?; |
| 1130 |
|
| 1131 |
let src_pending = cmd_buf_data.trackers.textures.set_single( |
| 1132 |
&src_texture, |
| 1133 |
src_range, |
| 1134 |
wgt::TextureUses::COPY_SRC, |
| 1135 |
); |
| 1136 |
let src_raw = src_texture.try_raw(&snatch_guard)?; |
| 1137 |
src_texture |
| 1138 |
.check_usage(TextureUsages::COPY_SRC) |
| 1139 |
.map_err(TransferError::MissingTextureUsage)?; |
| 1140 |
|
| 1141 |
//TODO: try to avoid this the collection. It's needed because both |
| 1142 |
// `src_pending` and `dst_pending` try to hold `trackers.textures` mutably. |
| 1143 |
let mut barriers: ArrayVec<_, 2> = src_pending |
| 1144 |
.map(|pending| pending.into_hal(src_raw)) |
| 1145 |
.collect(); |
| 1146 |
|
| 1147 |
let dst_pending = cmd_buf_data.trackers.textures.set_single( |
| 1148 |
&dst_texture, |
| 1149 |
dst_range, |
| 1150 |
wgt::TextureUses::COPY_DST, |
| 1151 |
); |
| 1152 |
let dst_raw = dst_texture.try_raw(&snatch_guard)?; |
| 1153 |
dst_texture |
| 1154 |
.check_usage(TextureUsages::COPY_DST) |
| 1155 |
.map_err(TransferError::MissingTextureUsage)?; |
| 1156 |
|
| 1157 |
barriers.extend(dst_pending.map(|pending| pending.into_hal(dst_raw))); |
| 1158 |
|
| 1159 |
let hal_copy_size = hal::CopyExtent { |
| 1160 |
width: src_copy_size.width.min(dst_copy_size.width), |
| 1161 |
height: src_copy_size.height.min(dst_copy_size.height), |
| 1162 |
depth: src_copy_size.depth.min(dst_copy_size.depth), |
| 1163 |
}; |
| 1164 |
let regions = (0..array_layer_count) |
| 1165 |
.map(|rel_array_layer| { |
| 1166 |
let mut src_base = src_tex_base.clone(); |
| 1167 |
let mut dst_base = dst_tex_base.clone(); |
| 1168 |
src_base.array_layer += rel_array_layer; |
| 1169 |
dst_base.array_layer += rel_array_layer; |
| 1170 |
hal::TextureCopy { |
| 1171 |
src_base, |
| 1172 |
dst_base, |
| 1173 |
size: hal_copy_size, |
| 1174 |
} |
| 1175 |
}) |
| 1176 |
.collect::<Vec<_>>(); |
| 1177 |
let cmd_buf_raw = cmd_buf_data.encoder.open()?; |
| 1178 |
unsafe { |
| 1179 |
cmd_buf_raw.transition_textures(&barriers); |
| 1180 |
cmd_buf_raw.copy_texture_to_texture( |
| 1181 |
src_raw, |
| 1182 |
wgt::TextureUses::COPY_SRC, |
| 1183 |
dst_raw, |
| 1184 |
®ions, |
| 1185 |
); |
| 1186 |
} |
| 1187 |
|
| 1188 |
cmd_buf_data_guard.mark_successful(); |
| 1189 |
Ok(()) |
| 1190 |
} |
| 1191 |
} |