diff --git a/wgpu-core/src/command/transfer.rs b/wgpu-core/src/command/transfer.rs index 21b0108a8..b4b8b4027 100644 --- a/wgpu-core/src/command/transfer.rs +++ b/wgpu-core/src/command/transfer.rs @@ -84,6 +84,10 @@ pub enum TransferError { InvalidRowsPerImage, #[error("source and destination layers have different aspects")] MismatchedAspects, + #[error("copying from textures with format {0:?} is forbidden")] + CopyFromForbiddenTextureFormat(wgt::TextureFormat), + #[error("copying to textures with format {0:?} is forbidden")] + CopyToForbiddenTextureFormat(wgt::TextureFormat), } /// Error encountered while attempting to do a copy on a command encoder. @@ -480,6 +484,11 @@ impl Global { )?; let (block_width, _) = conv::texture_block_size(dst_texture.format); + if !conv::is_valid_copy_dst_texture_format(dst_texture.format) { + Err(TransferError::CopyToForbiddenTextureFormat( + dst_texture.format, + ))? + } let buffer_width = (source.layout.bytes_per_row / bytes_per_block) * block_width; let region = hal::command::BufferImageCopy { @@ -594,6 +603,11 @@ impl Global { )?; let (block_width, _) = conv::texture_block_size(src_texture.format); + if !conv::is_valid_copy_src_texture_format(src_texture.format) { + Err(TransferError::CopyFromForbiddenTextureFormat( + src_texture.format, + ))? + } let buffer_width = (destination.layout.bytes_per_row / bytes_per_block) * block_width; let region = hal::command::BufferImageCopy { diff --git a/wgpu-core/src/conv.rs b/wgpu-core/src/conv.rs index 9b0f5fda2..426927505 100644 --- a/wgpu-core/src/conv.rs +++ b/wgpu-core/src/conv.rs @@ -439,11 +439,10 @@ pub fn texture_block_size(format: wgt::TextureFormat) -> (u32, u32) { | Tf::Rgba16Float | Tf::Rgba32Uint | Tf::Rgba32Sint - | Tf::Rgba32Float => (1, 1), - - Tf::Depth32Float | Tf::Depth24Plus | Tf::Depth24PlusStencil8 => { - unreachable!("unexpected depth format") - } + | Tf::Rgba32Float + | Tf::Depth32Float + | Tf::Depth24Plus + | Tf::Depth24PlusStencil8 => (1, 1), Tf::Bc1RgbaUnorm | Tf::Bc1RgbaUnormSrgb @@ -562,6 +561,22 @@ pub fn is_power_of_two(val: u32) -> bool { val != 0 && (val & (val - 1)) == 0 } +pub fn is_valid_copy_src_texture_format(format: wgt::TextureFormat) -> bool { + use wgt::TextureFormat as Tf; + match format { + Tf::Depth24Plus | Tf::Depth24PlusStencil8 => false, + _ => true, + } +} + +pub fn is_valid_copy_dst_texture_format(format: wgt::TextureFormat) -> bool { + use wgt::TextureFormat as Tf; + match format { + Tf::Depth32Float | Tf::Depth24Plus | Tf::Depth24PlusStencil8 => false, + _ => true, + } +} + pub fn map_texture_dimension_size( dimension: wgt::TextureDimension, wgt::Extent3d { diff --git a/wgpu-core/src/device/queue.rs b/wgpu-core/src/device/queue.rs index eed943529..f8cb7068d 100644 --- a/wgpu-core/src/device/queue.rs +++ b/wgpu-core/src/device/queue.rs @@ -313,6 +313,9 @@ impl Global { size, )?; let (block_width, block_height) = conv::texture_block_size(texture_format); + if !conv::is_valid_copy_dst_texture_format(texture_format) { + Err(TransferError::CopyToForbiddenTextureFormat(texture_format))? + } let width_blocks = size.width / block_width; let height_blocks = size.height / block_width;