diff --git a/deno_webgpu/error.rs b/deno_webgpu/error.rs index cdb427908..a2a6d2947 100644 --- a/deno_webgpu/error.rs +++ b/deno_webgpu/error.rs @@ -17,7 +17,6 @@ use wgpu_core::binding_model::GetBindGroupLayoutError; use wgpu_core::command::ClearError; use wgpu_core::command::CommandEncoderError; use wgpu_core::command::ComputePassError; -use wgpu_core::command::CopyError; use wgpu_core::command::CreateRenderBundleError; use wgpu_core::command::EncoderStateError; use wgpu_core::command::QueryError; @@ -273,12 +272,6 @@ impl From for GPUError { } } -impl From for GPUError { - fn from(err: CopyError) -> Self { - GPUError::Validation(fmt_err(&err)) - } -} - impl From for GPUError { fn from(err: CommandEncoderError) -> Self { GPUError::Validation(fmt_err(&err)) diff --git a/tests/tests/wgpu-gpu/device.rs b/tests/tests/wgpu-gpu/device.rs index 7411aa35a..e9e42992b 100644 --- a/tests/tests/wgpu-gpu/device.rs +++ b/tests/tests/wgpu-gpu/device.rs @@ -380,70 +380,66 @@ static DEVICE_DESTROY_THEN_MORE: GpuTestConfiguration = GpuTestConfiguration::ne ); // Copying a buffer to a buffer should fail. + encoder_for_buffer_buffer_copy.copy_buffer_to_buffer( + &buffer_source, + 0, + &buffer_dest, + 0, + 256, + ); fail( &ctx.device, - || { - encoder_for_buffer_buffer_copy.copy_buffer_to_buffer( - &buffer_source, - 0, - &buffer_dest, - 0, - 256, - ); - }, + || encoder_for_buffer_buffer_copy.finish(), Some("device with '' label is invalid"), ); // Copying a buffer to a texture should fail. + encoder_for_buffer_texture_copy.copy_buffer_to_texture( + wgpu::TexelCopyBufferInfo { + buffer: &buffer_source, + layout: wgpu::TexelCopyBufferLayout { + offset: 0, + bytes_per_row: Some(4), + rows_per_image: None, + }, + }, + texture_for_write.as_image_copy(), + texture_extent, + ); fail( &ctx.device, - || { - encoder_for_buffer_texture_copy.copy_buffer_to_texture( - wgpu::TexelCopyBufferInfo { - buffer: &buffer_source, - layout: wgpu::TexelCopyBufferLayout { - offset: 0, - bytes_per_row: Some(4), - rows_per_image: None, - }, - }, - texture_for_write.as_image_copy(), - texture_extent, - ); - }, + || encoder_for_buffer_texture_copy.finish(), Some("device with '' label is invalid"), ); // Copying a texture to a buffer should fail. + encoder_for_texture_buffer_copy.copy_texture_to_buffer( + texture_for_read.as_image_copy(), + wgpu::TexelCopyBufferInfo { + buffer: &buffer_source, + layout: wgpu::TexelCopyBufferLayout { + offset: 0, + bytes_per_row: Some(4), + rows_per_image: None, + }, + }, + texture_extent, + ); fail( &ctx.device, - || { - encoder_for_texture_buffer_copy.copy_texture_to_buffer( - texture_for_read.as_image_copy(), - wgpu::TexelCopyBufferInfo { - buffer: &buffer_source, - layout: wgpu::TexelCopyBufferLayout { - offset: 0, - bytes_per_row: Some(4), - rows_per_image: None, - }, - }, - texture_extent, - ); - }, + || encoder_for_texture_buffer_copy.finish(), Some("device with '' label is invalid"), ); // Copying a texture to a texture should fail. + encoder_for_texture_texture_copy.copy_texture_to_texture( + texture_for_read.as_image_copy(), + texture_for_write.as_image_copy(), + texture_extent, + ); fail( &ctx.device, - || { - encoder_for_texture_texture_copy.copy_texture_to_texture( - texture_for_read.as_image_copy(), - texture_for_write.as_image_copy(), - texture_extent, - ); - }, + || encoder_for_texture_texture_copy.finish(), Some("device with '' label is invalid"), ); diff --git a/wgpu-core/src/command/mod.rs b/wgpu-core/src/command/mod.rs index 3b2572372..2c860d5bc 100644 --- a/wgpu-core/src/command/mod.rs +++ b/wgpu-core/src/command/mod.rs @@ -30,6 +30,7 @@ pub use timestamp_writes::PassTimestampWrites; use self::memory_init::CommandBufferTextureMemoryActions; +use crate::command::transition_resources::TransitionResourcesError; use crate::device::queue::TempResource; use crate::device::{Device, DeviceError, MissingFeatures}; use crate::lock::{rank, Mutex}; @@ -37,9 +38,11 @@ use crate::snatch::SnatchGuard; use crate::init_tracker::BufferInitTrackerAction; use crate::ray_tracing::AsAction; -use crate::resource::{Fallible, InvalidResourceError, Labeled, ParentDevice as _, QuerySet}; +use crate::resource::{ + DestroyedResourceError, Fallible, InvalidResourceError, Labeled, ParentDevice as _, QuerySet, +}; use crate::storage::Storage; -use crate::track::{DeviceTracker, Tracker, UsageScope}; +use crate::track::{DeviceTracker, ResourceUsageCompatibilityError, Tracker, UsageScope}; use crate::{api_log, global::Global, id, resource_log, Label}; use crate::{hal_label, LabelHelpers}; @@ -886,7 +889,17 @@ pub enum CommandEncoderError { #[error(transparent)] InvalidResource(#[from] InvalidResourceError), #[error(transparent)] + DestroyedResource(#[from] DestroyedResourceError), + #[error(transparent)] + ResourceUsage(#[from] ResourceUsageCompatibilityError), + #[error(transparent)] MissingFeatures(#[from] MissingFeatures), + #[error(transparent)] + Transfer(#[from] TransferError), + #[error(transparent)] + Clear(#[from] ClearError), + #[error(transparent)] + TransitionResources(#[from] TransitionResourcesError), #[error( "begin and end indices of pass timestamp writes are both set to {idx}, which is not allowed" )] diff --git a/wgpu-core/src/command/transfer.rs b/wgpu-core/src/command/transfer.rs index ac6bfff7c..d1b0689b5 100644 --- a/wgpu-core/src/command/transfer.rs +++ b/wgpu-core/src/command/transfer.rs @@ -8,9 +8,9 @@ use wgt::{BufferAddress, BufferUsages, Extent3d, TextureSelector, TextureUsages} use crate::device::trace::Command as TraceCommand; use crate::{ api_log, - command::{clear_texture, EncoderStateError}, + command::{clear_texture, CommandEncoderError, EncoderStateError}, conv, - device::{Device, DeviceError, MissingDownlevelFlags}, + device::{Device, MissingDownlevelFlags}, global::Global, id::{BufferId, CommandEncoderId, TextureId}, init_tracker::{ @@ -18,8 +18,8 @@ use crate::{ TextureInitTrackerAction, }, resource::{ - DestroyedResourceError, InvalidResourceError, MissingBufferUsageError, - MissingTextureUsageError, ParentDevice, Texture, TextureErrorDimension, + MissingBufferUsageError, MissingTextureUsageError, ParentDevice, Texture, + TextureErrorDimension, }, snatch::SnatchGuard, }; @@ -138,22 +138,6 @@ pub enum TransferError { InvalidMipLevel { requested: u32, count: u32 }, } -/// Error encountered while attempting to do a copy on a command encoder. -#[derive(Clone, Debug, Error)] -#[non_exhaustive] -pub enum CopyError { - #[error(transparent)] - EncoderState(#[from] EncoderStateError), - #[error(transparent)] - Device(#[from] DeviceError), - #[error("Copy error")] - Transfer(#[from] TransferError), - #[error(transparent)] - DestroyedResource(#[from] DestroyedResourceError), - #[error(transparent)] - InvalidResource(#[from] InvalidResourceError), -} - pub(crate) fn extract_texture_selector( copy_texture: &wgt::TexelCopyTextureInfo, copy_size: &Extent3d, @@ -511,165 +495,165 @@ impl Global { destination: BufferId, destination_offset: BufferAddress, size: Option, - ) -> Result<(), CopyError> { + ) -> Result<(), EncoderStateError> { profiling::scope!("CommandEncoder::copy_buffer_to_buffer"); api_log!( "CommandEncoder::copy_buffer_to_buffer {source:?} -> {destination:?} {size:?}bytes" ); - if source == destination { - return Err(TransferError::SameSourceDestinationBuffer.into()); - } let hub = &self.hub; let cmd_buf = hub .command_buffers .get(command_encoder_id.into_command_buffer_id()); let mut cmd_buf_data = cmd_buf.data.lock(); - let mut cmd_buf_data_guard = cmd_buf_data.record()?; - let cmd_buf_data = &mut *cmd_buf_data_guard; + cmd_buf_data.record_with(|cmd_buf_data| -> Result<(), CommandEncoderError> { + let device = &cmd_buf.device; + device.check_is_valid()?; - let device = &cmd_buf.device; - device.check_is_valid()?; + if source == destination { + return Err(TransferError::SameSourceDestinationBuffer.into()); + } - #[cfg(feature = "trace")] - if let Some(ref mut list) = cmd_buf_data.commands { - list.push(TraceCommand::CopyBufferToBuffer { - src: source, - src_offset: source_offset, - dst: destination, - dst_offset: destination_offset, - size, - }); - } + #[cfg(feature = "trace")] + if let Some(ref mut list) = cmd_buf_data.commands { + list.push(TraceCommand::CopyBufferToBuffer { + src: source, + src_offset: source_offset, + dst: destination, + dst_offset: destination_offset, + size, + }); + } - let snatch_guard = device.snatchable_lock.read(); + let snatch_guard = device.snatchable_lock.read(); - let src_buffer = hub.buffers.get(source).get()?; + let src_buffer = hub.buffers.get(source).get()?; - src_buffer.same_device_as(cmd_buf.as_ref())?; + src_buffer.same_device_as(cmd_buf.as_ref())?; - let src_pending = cmd_buf_data - .trackers - .buffers - .set_single(&src_buffer, wgt::BufferUses::COPY_SRC); + let src_pending = cmd_buf_data + .trackers + .buffers + .set_single(&src_buffer, wgt::BufferUses::COPY_SRC); - let src_raw = src_buffer.try_raw(&snatch_guard)?; - src_buffer - .check_usage(BufferUsages::COPY_SRC) - .map_err(TransferError::MissingBufferUsage)?; - // expecting only a single barrier - let src_barrier = src_pending.map(|pending| pending.into_hal(&src_buffer, &snatch_guard)); + let src_raw = src_buffer.try_raw(&snatch_guard)?; + src_buffer + .check_usage(BufferUsages::COPY_SRC) + .map_err(TransferError::MissingBufferUsage)?; + // expecting only a single barrier + let src_barrier = + src_pending.map(|pending| pending.into_hal(&src_buffer, &snatch_guard)); - let dst_buffer = hub.buffers.get(destination).get()?; + let dst_buffer = hub.buffers.get(destination).get()?; - dst_buffer.same_device_as(cmd_buf.as_ref())?; + dst_buffer.same_device_as(cmd_buf.as_ref())?; - let dst_pending = cmd_buf_data - .trackers - .buffers - .set_single(&dst_buffer, wgt::BufferUses::COPY_DST); + let dst_pending = cmd_buf_data + .trackers + .buffers + .set_single(&dst_buffer, wgt::BufferUses::COPY_DST); - let dst_raw = dst_buffer.try_raw(&snatch_guard)?; - dst_buffer - .check_usage(BufferUsages::COPY_DST) - .map_err(TransferError::MissingBufferUsage)?; - let dst_barrier = dst_pending.map(|pending| pending.into_hal(&dst_buffer, &snatch_guard)); + let dst_raw = dst_buffer.try_raw(&snatch_guard)?; + dst_buffer + .check_usage(BufferUsages::COPY_DST) + .map_err(TransferError::MissingBufferUsage)?; + let dst_barrier = + dst_pending.map(|pending| pending.into_hal(&dst_buffer, &snatch_guard)); - let (size, source_end_offset) = match size { - Some(size) => (size, source_offset + size), - None => (src_buffer.size - source_offset, src_buffer.size), - }; + let (size, source_end_offset) = match size { + Some(size) => (size, source_offset + size), + None => (src_buffer.size - source_offset, src_buffer.size), + }; - if size % wgt::COPY_BUFFER_ALIGNMENT != 0 { - return Err(TransferError::UnalignedCopySize(size).into()); - } - if source_offset % wgt::COPY_BUFFER_ALIGNMENT != 0 { - return Err(TransferError::UnalignedBufferOffset(source_offset).into()); - } - if destination_offset % wgt::COPY_BUFFER_ALIGNMENT != 0 { - return Err(TransferError::UnalignedBufferOffset(destination_offset).into()); - } - if !device - .downlevel - .flags - .contains(wgt::DownlevelFlags::UNRESTRICTED_INDEX_BUFFER) - && (src_buffer.usage.contains(BufferUsages::INDEX) - || dst_buffer.usage.contains(BufferUsages::INDEX)) - { - let forbidden_usages = BufferUsages::VERTEX - | BufferUsages::UNIFORM - | BufferUsages::INDIRECT - | BufferUsages::STORAGE; - if src_buffer.usage.intersects(forbidden_usages) - || dst_buffer.usage.intersects(forbidden_usages) + if size % wgt::COPY_BUFFER_ALIGNMENT != 0 { + return Err(TransferError::UnalignedCopySize(size).into()); + } + if source_offset % wgt::COPY_BUFFER_ALIGNMENT != 0 { + return Err(TransferError::UnalignedBufferOffset(source_offset).into()); + } + if destination_offset % wgt::COPY_BUFFER_ALIGNMENT != 0 { + return Err(TransferError::UnalignedBufferOffset(destination_offset).into()); + } + if !device + .downlevel + .flags + .contains(wgt::DownlevelFlags::UNRESTRICTED_INDEX_BUFFER) + && (src_buffer.usage.contains(BufferUsages::INDEX) + || dst_buffer.usage.contains(BufferUsages::INDEX)) { - return Err(TransferError::MissingDownlevelFlags(MissingDownlevelFlags( - wgt::DownlevelFlags::UNRESTRICTED_INDEX_BUFFER, - )) + let forbidden_usages = BufferUsages::VERTEX + | BufferUsages::UNIFORM + | BufferUsages::INDIRECT + | BufferUsages::STORAGE; + if src_buffer.usage.intersects(forbidden_usages) + || dst_buffer.usage.intersects(forbidden_usages) + { + return Err(TransferError::MissingDownlevelFlags(MissingDownlevelFlags( + wgt::DownlevelFlags::UNRESTRICTED_INDEX_BUFFER, + )) + .into()); + } + } + + let destination_end_offset = destination_offset + size; + if source_end_offset > src_buffer.size { + return Err(TransferError::BufferOverrun { + start_offset: source_offset, + end_offset: source_end_offset, + buffer_size: src_buffer.size, + side: CopySide::Source, + } .into()); } - } - - let destination_end_offset = destination_offset + size; - if source_end_offset > src_buffer.size { - return Err(TransferError::BufferOverrun { - start_offset: source_offset, - end_offset: source_end_offset, - buffer_size: src_buffer.size, - side: CopySide::Source, + if destination_end_offset > dst_buffer.size { + return Err(TransferError::BufferOverrun { + start_offset: destination_offset, + end_offset: destination_end_offset, + buffer_size: dst_buffer.size, + side: CopySide::Destination, + } + .into()); } - .into()); - } - if destination_end_offset > dst_buffer.size { - return Err(TransferError::BufferOverrun { - start_offset: destination_offset, - end_offset: destination_end_offset, - buffer_size: dst_buffer.size, - side: CopySide::Destination, + + if size == 0 { + log::trace!("Ignoring copy_buffer_to_buffer of size 0"); + return Ok(()); } - .into()); - } - if size == 0 { - log::trace!("Ignoring copy_buffer_to_buffer of size 0"); - cmd_buf_data_guard.mark_successful(); - return Ok(()); - } + // Make sure source is initialized memory and mark dest as initialized. + cmd_buf_data.buffer_memory_init_actions.extend( + dst_buffer.initialization_status.read().create_action( + &dst_buffer, + destination_offset..(destination_offset + size), + MemoryInitKind::ImplicitlyInitialized, + ), + ); + cmd_buf_data.buffer_memory_init_actions.extend( + src_buffer.initialization_status.read().create_action( + &src_buffer, + source_offset..(source_offset + size), + MemoryInitKind::NeedsInitializedMemory, + ), + ); - // Make sure source is initialized memory and mark dest as initialized. - cmd_buf_data.buffer_memory_init_actions.extend( - dst_buffer.initialization_status.read().create_action( - &dst_buffer, - destination_offset..(destination_offset + size), - MemoryInitKind::ImplicitlyInitialized, - ), - ); - cmd_buf_data.buffer_memory_init_actions.extend( - src_buffer.initialization_status.read().create_action( - &src_buffer, - source_offset..(source_offset + size), - MemoryInitKind::NeedsInitializedMemory, - ), - ); + let region = hal::BufferCopy { + src_offset: source_offset, + dst_offset: destination_offset, + size: wgt::BufferSize::new(size).unwrap(), + }; + let cmd_buf_raw = cmd_buf_data.encoder.open()?; + let barriers = src_barrier + .into_iter() + .chain(dst_barrier) + .collect::>(); + unsafe { + cmd_buf_raw.transition_buffers(&barriers); + cmd_buf_raw.copy_buffer_to_buffer(src_raw, dst_raw, &[region]); + } - let region = hal::BufferCopy { - src_offset: source_offset, - dst_offset: destination_offset, - size: wgt::BufferSize::new(size).unwrap(), - }; - let cmd_buf_raw = cmd_buf_data.encoder.open()?; - let barriers = src_barrier - .into_iter() - .chain(dst_barrier) - .collect::>(); - unsafe { - cmd_buf_raw.transition_buffers(&barriers); - cmd_buf_raw.copy_buffer_to_buffer(src_raw, dst_raw, &[region]); - } - - cmd_buf_data_guard.mark_successful(); - Ok(()) + Ok(()) + }) } pub fn command_encoder_copy_buffer_to_texture( @@ -678,7 +662,7 @@ impl Global { source: &TexelCopyBufferInfo, destination: &TexelCopyTextureInfo, copy_size: &Extent3d, - ) -> Result<(), CopyError> { + ) -> Result<(), EncoderStateError> { profiling::scope!("CommandEncoder::copy_buffer_to_texture"); api_log!( "CommandEncoder::copy_buffer_to_texture {:?} -> {:?} {copy_size:?}", @@ -692,141 +676,143 @@ impl Global { .command_buffers .get(command_encoder_id.into_command_buffer_id()); let mut cmd_buf_data = cmd_buf.data.lock(); - let mut cmd_buf_data_guard = cmd_buf_data.record()?; - let cmd_buf_data = &mut *cmd_buf_data_guard; + cmd_buf_data.record_with(|cmd_buf_data| -> Result<(), CommandEncoderError> { + let device = &cmd_buf.device; + device.check_is_valid()?; - let device = &cmd_buf.device; - device.check_is_valid()?; - - #[cfg(feature = "trace")] - if let Some(ref mut list) = cmd_buf_data.commands { - list.push(TraceCommand::CopyBufferToTexture { - src: *source, - dst: *destination, - size: *copy_size, - }); - } - - if copy_size.width == 0 || copy_size.height == 0 || copy_size.depth_or_array_layers == 0 { - log::trace!("Ignoring copy_buffer_to_texture of size 0"); - cmd_buf_data_guard.mark_successful(); - return Ok(()); - } - - let dst_texture = hub.textures.get(destination.texture).get()?; - - dst_texture.same_device_as(cmd_buf.as_ref())?; - - let (hal_copy_size, array_layer_count) = validate_texture_copy_range( - destination, - &dst_texture.desc, - CopySide::Destination, - copy_size, - )?; - - let (dst_range, dst_base) = extract_texture_selector(destination, copy_size, &dst_texture)?; - - let snatch_guard = device.snatchable_lock.read(); - - // Handle texture init *before* dealing with barrier transitions so we - // have an easier time inserting "immediate-inits" that may be required - // by prior discards in rare cases. - handle_dst_texture_init( - cmd_buf_data, - device, - destination, - copy_size, - &dst_texture, - &snatch_guard, - )?; - - let src_buffer = hub.buffers.get(source.buffer).get()?; - - src_buffer.same_device_as(cmd_buf.as_ref())?; - - let src_pending = cmd_buf_data - .trackers - .buffers - .set_single(&src_buffer, wgt::BufferUses::COPY_SRC); - - let src_raw = src_buffer.try_raw(&snatch_guard)?; - src_buffer - .check_usage(BufferUsages::COPY_SRC) - .map_err(TransferError::MissingBufferUsage)?; - let src_barrier = src_pending.map(|pending| pending.into_hal(&src_buffer, &snatch_guard)); - - let dst_pending = cmd_buf_data.trackers.textures.set_single( - &dst_texture, - dst_range, - wgt::TextureUses::COPY_DST, - ); - let dst_raw = dst_texture.try_raw(&snatch_guard)?; - dst_texture - .check_usage(TextureUsages::COPY_DST) - .map_err(TransferError::MissingTextureUsage)?; - let dst_barrier = dst_pending - .map(|pending| pending.into_hal(dst_raw)) - .collect::>(); - - if !dst_base.aspect.is_one() { - return Err(TransferError::CopyAspectNotOne.into()); - } - - if !conv::is_valid_copy_dst_texture_format(dst_texture.desc.format, destination.aspect) { - return Err(TransferError::CopyToForbiddenTextureFormat { - format: dst_texture.desc.format, - aspect: destination.aspect, + #[cfg(feature = "trace")] + if let Some(ref mut list) = cmd_buf_data.commands { + list.push(TraceCommand::CopyBufferToTexture { + src: *source, + dst: *destination, + size: *copy_size, + }); } - .into()); - } - let (required_buffer_bytes_in_copy, bytes_per_array_layer) = validate_linear_texture_data( - &source.layout, - dst_texture.desc.format, - destination.aspect, - src_buffer.size, - CopySide::Source, - copy_size, - true, - )?; + if copy_size.width == 0 || copy_size.height == 0 || copy_size.depth_or_array_layers == 0 + { + log::trace!("Ignoring copy_buffer_to_texture of size 0"); + return Ok(()); + } - if dst_texture.desc.format.is_depth_stencil_format() { - device - .require_downlevel_flags(wgt::DownlevelFlags::DEPTH_TEXTURE_AND_BUFFER_COPIES) - .map_err(TransferError::from)?; - } + let dst_texture = hub.textures.get(destination.texture).get()?; - cmd_buf_data.buffer_memory_init_actions.extend( - src_buffer.initialization_status.read().create_action( - &src_buffer, - source.layout.offset..(source.layout.offset + required_buffer_bytes_in_copy), - MemoryInitKind::NeedsInitializedMemory, - ), - ); + dst_texture.same_device_as(cmd_buf.as_ref())?; - let regions = (0..array_layer_count) - .map(|rel_array_layer| { - let mut texture_base = dst_base.clone(); - texture_base.array_layer += rel_array_layer; - let mut buffer_layout = source.layout; - buffer_layout.offset += rel_array_layer as u64 * bytes_per_array_layer; - hal::BufferTextureCopy { - buffer_layout, - texture_base, - size: hal_copy_size, + let (hal_copy_size, array_layer_count) = validate_texture_copy_range( + destination, + &dst_texture.desc, + CopySide::Destination, + copy_size, + )?; + + let (dst_range, dst_base) = + extract_texture_selector(destination, copy_size, &dst_texture)?; + + let snatch_guard = device.snatchable_lock.read(); + + // Handle texture init *before* dealing with barrier transitions so we + // have an easier time inserting "immediate-inits" that may be required + // by prior discards in rare cases. + handle_dst_texture_init( + cmd_buf_data, + device, + destination, + copy_size, + &dst_texture, + &snatch_guard, + )?; + + let src_buffer = hub.buffers.get(source.buffer).get()?; + + src_buffer.same_device_as(cmd_buf.as_ref())?; + + let src_pending = cmd_buf_data + .trackers + .buffers + .set_single(&src_buffer, wgt::BufferUses::COPY_SRC); + + let src_raw = src_buffer.try_raw(&snatch_guard)?; + src_buffer + .check_usage(BufferUsages::COPY_SRC) + .map_err(TransferError::MissingBufferUsage)?; + let src_barrier = + src_pending.map(|pending| pending.into_hal(&src_buffer, &snatch_guard)); + + let dst_pending = cmd_buf_data.trackers.textures.set_single( + &dst_texture, + dst_range, + wgt::TextureUses::COPY_DST, + ); + let dst_raw = dst_texture.try_raw(&snatch_guard)?; + dst_texture + .check_usage(TextureUsages::COPY_DST) + .map_err(TransferError::MissingTextureUsage)?; + let dst_barrier = dst_pending + .map(|pending| pending.into_hal(dst_raw)) + .collect::>(); + + if !dst_base.aspect.is_one() { + return Err(TransferError::CopyAspectNotOne.into()); + } + + if !conv::is_valid_copy_dst_texture_format(dst_texture.desc.format, destination.aspect) + { + return Err(TransferError::CopyToForbiddenTextureFormat { + format: dst_texture.desc.format, + aspect: destination.aspect, } - }) - .collect::>(); + .into()); + } - let cmd_buf_raw = cmd_buf_data.encoder.open()?; - unsafe { - cmd_buf_raw.transition_textures(&dst_barrier); - cmd_buf_raw.transition_buffers(src_barrier.as_slice()); - cmd_buf_raw.copy_buffer_to_texture(src_raw, dst_raw, ®ions); - } + let (required_buffer_bytes_in_copy, bytes_per_array_layer) = + validate_linear_texture_data( + &source.layout, + dst_texture.desc.format, + destination.aspect, + src_buffer.size, + CopySide::Source, + copy_size, + true, + )?; - cmd_buf_data_guard.mark_successful(); - Ok(()) + if dst_texture.desc.format.is_depth_stencil_format() { + device + .require_downlevel_flags(wgt::DownlevelFlags::DEPTH_TEXTURE_AND_BUFFER_COPIES) + .map_err(TransferError::from)?; + } + + cmd_buf_data.buffer_memory_init_actions.extend( + src_buffer.initialization_status.read().create_action( + &src_buffer, + source.layout.offset..(source.layout.offset + required_buffer_bytes_in_copy), + MemoryInitKind::NeedsInitializedMemory, + ), + ); + + let regions = (0..array_layer_count) + .map(|rel_array_layer| { + let mut texture_base = dst_base.clone(); + texture_base.array_layer += rel_array_layer; + let mut buffer_layout = source.layout; + buffer_layout.offset += rel_array_layer as u64 * bytes_per_array_layer; + hal::BufferTextureCopy { + buffer_layout, + texture_base, + size: hal_copy_size, + } + }) + .collect::>(); + + let cmd_buf_raw = cmd_buf_data.encoder.open()?; + unsafe { + cmd_buf_raw.transition_textures(&dst_barrier); + cmd_buf_raw.transition_buffers(src_barrier.as_slice()); + cmd_buf_raw.copy_buffer_to_texture(src_raw, dst_raw, ®ions); + } + + Ok(()) + }) } pub fn command_encoder_copy_texture_to_buffer( @@ -835,7 +821,7 @@ impl Global { source: &TexelCopyTextureInfo, destination: &TexelCopyBufferInfo, copy_size: &Extent3d, - ) -> Result<(), CopyError> { + ) -> Result<(), EncoderStateError> { profiling::scope!("CommandEncoder::copy_texture_to_buffer"); api_log!( "CommandEncoder::copy_texture_to_buffer {:?} -> {:?} {copy_size:?}", @@ -849,155 +835,159 @@ impl Global { .command_buffers .get(command_encoder_id.into_command_buffer_id()); let mut cmd_buf_data = cmd_buf.data.lock(); - let mut cmd_buf_data_guard = cmd_buf_data.record()?; - let cmd_buf_data = &mut *cmd_buf_data_guard; + cmd_buf_data.record_with(|cmd_buf_data| -> Result<(), CommandEncoderError> { + let device = &cmd_buf.device; + device.check_is_valid()?; - let device = &cmd_buf.device; - device.check_is_valid()?; - - #[cfg(feature = "trace")] - if let Some(list) = cmd_buf_data.commands.as_mut() { - list.push(TraceCommand::CopyTextureToBuffer { - src: *source, - dst: *destination, - size: *copy_size, - }); - } - - if copy_size.width == 0 || copy_size.height == 0 || copy_size.depth_or_array_layers == 0 { - log::trace!("Ignoring copy_texture_to_buffer of size 0"); - cmd_buf_data_guard.mark_successful(); - return Ok(()); - } - - let src_texture = hub.textures.get(source.texture).get()?; - - src_texture.same_device_as(cmd_buf.as_ref())?; - - let (hal_copy_size, array_layer_count) = - validate_texture_copy_range(source, &src_texture.desc, CopySide::Source, copy_size)?; - - let (src_range, src_base) = extract_texture_selector(source, copy_size, &src_texture)?; - - let snatch_guard = device.snatchable_lock.read(); - - // Handle texture init *before* dealing with barrier transitions so we - // have an easier time inserting "immediate-inits" that may be required - // by prior discards in rare cases. - handle_src_texture_init( - cmd_buf_data, - device, - source, - copy_size, - &src_texture, - &snatch_guard, - )?; - - let src_pending = cmd_buf_data.trackers.textures.set_single( - &src_texture, - src_range, - wgt::TextureUses::COPY_SRC, - ); - let src_raw = src_texture.try_raw(&snatch_guard)?; - src_texture - .check_usage(TextureUsages::COPY_SRC) - .map_err(TransferError::MissingTextureUsage)?; - if src_texture.desc.sample_count != 1 { - return Err(TransferError::InvalidSampleCount { - sample_count: src_texture.desc.sample_count, + #[cfg(feature = "trace")] + if let Some(list) = cmd_buf_data.commands.as_mut() { + list.push(TraceCommand::CopyTextureToBuffer { + src: *source, + dst: *destination, + size: *copy_size, + }); } - .into()); - } - if source.mip_level >= src_texture.desc.mip_level_count { - return Err(TransferError::InvalidMipLevel { - requested: source.mip_level, - count: src_texture.desc.mip_level_count, + + if copy_size.width == 0 || copy_size.height == 0 || copy_size.depth_or_array_layers == 0 + { + log::trace!("Ignoring copy_texture_to_buffer of size 0"); + return Ok(()); } - .into()); - } - let src_barrier = src_pending - .map(|pending| pending.into_hal(src_raw)) - .collect::>(); - let dst_buffer = hub.buffers.get(destination.buffer).get()?; + let src_texture = hub.textures.get(source.texture).get()?; - dst_buffer.same_device_as(cmd_buf.as_ref())?; + src_texture.same_device_as(cmd_buf.as_ref())?; - let dst_pending = cmd_buf_data - .trackers - .buffers - .set_single(&dst_buffer, wgt::BufferUses::COPY_DST); + let (hal_copy_size, array_layer_count) = validate_texture_copy_range( + source, + &src_texture.desc, + CopySide::Source, + copy_size, + )?; - let dst_raw = dst_buffer.try_raw(&snatch_guard)?; - dst_buffer - .check_usage(BufferUsages::COPY_DST) - .map_err(TransferError::MissingBufferUsage)?; - let dst_barrier = dst_pending.map(|pending| pending.into_hal(&dst_buffer, &snatch_guard)); + let (src_range, src_base) = extract_texture_selector(source, copy_size, &src_texture)?; - if !src_base.aspect.is_one() { - return Err(TransferError::CopyAspectNotOne.into()); - } + let snatch_guard = device.snatchable_lock.read(); - if !conv::is_valid_copy_src_texture_format(src_texture.desc.format, source.aspect) { - return Err(TransferError::CopyFromForbiddenTextureFormat { - format: src_texture.desc.format, - aspect: source.aspect, - } - .into()); - } + // Handle texture init *before* dealing with barrier transitions so we + // have an easier time inserting "immediate-inits" that may be required + // by prior discards in rare cases. + handle_src_texture_init( + cmd_buf_data, + device, + source, + copy_size, + &src_texture, + &snatch_guard, + )?; - let (required_buffer_bytes_in_copy, bytes_per_array_layer) = validate_linear_texture_data( - &destination.layout, - src_texture.desc.format, - source.aspect, - dst_buffer.size, - CopySide::Destination, - copy_size, - true, - )?; - - if src_texture.desc.format.is_depth_stencil_format() { - device - .require_downlevel_flags(wgt::DownlevelFlags::DEPTH_TEXTURE_AND_BUFFER_COPIES) - .map_err(TransferError::from)?; - } - - cmd_buf_data.buffer_memory_init_actions.extend( - dst_buffer.initialization_status.read().create_action( - &dst_buffer, - destination.layout.offset - ..(destination.layout.offset + required_buffer_bytes_in_copy), - MemoryInitKind::ImplicitlyInitialized, - ), - ); - - let regions = (0..array_layer_count) - .map(|rel_array_layer| { - let mut texture_base = src_base.clone(); - texture_base.array_layer += rel_array_layer; - let mut buffer_layout = destination.layout; - buffer_layout.offset += rel_array_layer as u64 * bytes_per_array_layer; - hal::BufferTextureCopy { - buffer_layout, - texture_base, - size: hal_copy_size, - } - }) - .collect::>(); - let cmd_buf_raw = cmd_buf_data.encoder.open()?; - unsafe { - cmd_buf_raw.transition_buffers(dst_barrier.as_slice()); - cmd_buf_raw.transition_textures(&src_barrier); - cmd_buf_raw.copy_texture_to_buffer( - src_raw, + let src_pending = cmd_buf_data.trackers.textures.set_single( + &src_texture, + src_range, wgt::TextureUses::COPY_SRC, - dst_raw, - ®ions, ); - } + let src_raw = src_texture.try_raw(&snatch_guard)?; + src_texture + .check_usage(TextureUsages::COPY_SRC) + .map_err(TransferError::MissingTextureUsage)?; + if src_texture.desc.sample_count != 1 { + return Err(TransferError::InvalidSampleCount { + sample_count: src_texture.desc.sample_count, + } + .into()); + } + if source.mip_level >= src_texture.desc.mip_level_count { + return Err(TransferError::InvalidMipLevel { + requested: source.mip_level, + count: src_texture.desc.mip_level_count, + } + .into()); + } + let src_barrier = src_pending + .map(|pending| pending.into_hal(src_raw)) + .collect::>(); - cmd_buf_data_guard.mark_successful(); - Ok(()) + let dst_buffer = hub.buffers.get(destination.buffer).get()?; + + dst_buffer.same_device_as(cmd_buf.as_ref())?; + + let dst_pending = cmd_buf_data + .trackers + .buffers + .set_single(&dst_buffer, wgt::BufferUses::COPY_DST); + + let dst_raw = dst_buffer.try_raw(&snatch_guard)?; + dst_buffer + .check_usage(BufferUsages::COPY_DST) + .map_err(TransferError::MissingBufferUsage)?; + let dst_barrier = + dst_pending.map(|pending| pending.into_hal(&dst_buffer, &snatch_guard)); + + if !src_base.aspect.is_one() { + return Err(TransferError::CopyAspectNotOne.into()); + } + + if !conv::is_valid_copy_src_texture_format(src_texture.desc.format, source.aspect) { + return Err(TransferError::CopyFromForbiddenTextureFormat { + format: src_texture.desc.format, + aspect: source.aspect, + } + .into()); + } + + let (required_buffer_bytes_in_copy, bytes_per_array_layer) = + validate_linear_texture_data( + &destination.layout, + src_texture.desc.format, + source.aspect, + dst_buffer.size, + CopySide::Destination, + copy_size, + true, + )?; + + if src_texture.desc.format.is_depth_stencil_format() { + device + .require_downlevel_flags(wgt::DownlevelFlags::DEPTH_TEXTURE_AND_BUFFER_COPIES) + .map_err(TransferError::from)?; + } + + cmd_buf_data.buffer_memory_init_actions.extend( + dst_buffer.initialization_status.read().create_action( + &dst_buffer, + destination.layout.offset + ..(destination.layout.offset + required_buffer_bytes_in_copy), + MemoryInitKind::ImplicitlyInitialized, + ), + ); + + let regions = (0..array_layer_count) + .map(|rel_array_layer| { + let mut texture_base = src_base.clone(); + texture_base.array_layer += rel_array_layer; + let mut buffer_layout = destination.layout; + buffer_layout.offset += rel_array_layer as u64 * bytes_per_array_layer; + hal::BufferTextureCopy { + buffer_layout, + texture_base, + size: hal_copy_size, + } + }) + .collect::>(); + let cmd_buf_raw = cmd_buf_data.encoder.open()?; + unsafe { + cmd_buf_raw.transition_buffers(dst_barrier.as_slice()); + cmd_buf_raw.transition_textures(&src_barrier); + cmd_buf_raw.copy_texture_to_buffer( + src_raw, + wgt::TextureUses::COPY_SRC, + dst_raw, + ®ions, + ); + } + + Ok(()) + }) } pub fn command_encoder_copy_texture_to_texture( @@ -1006,7 +996,7 @@ impl Global { source: &TexelCopyTextureInfo, destination: &TexelCopyTextureInfo, copy_size: &Extent3d, - ) -> Result<(), CopyError> { + ) -> Result<(), EncoderStateError> { profiling::scope!("CommandEncoder::copy_texture_to_texture"); api_log!( "CommandEncoder::copy_texture_to_texture {:?} -> {:?} {copy_size:?}", @@ -1020,154 +1010,157 @@ impl Global { .command_buffers .get(command_encoder_id.into_command_buffer_id()); let mut cmd_buf_data = cmd_buf.data.lock(); - let mut cmd_buf_data_guard = cmd_buf_data.record()?; - let cmd_buf_data = &mut *cmd_buf_data_guard; + cmd_buf_data.record_with(|cmd_buf_data| -> Result<(), CommandEncoderError> { + let device = &cmd_buf.device; + device.check_is_valid()?; - let device = &cmd_buf.device; - device.check_is_valid()?; + let snatch_guard = device.snatchable_lock.read(); - let snatch_guard = device.snatchable_lock.read(); - - #[cfg(feature = "trace")] - if let Some(ref mut list) = cmd_buf_data.commands { - list.push(TraceCommand::CopyTextureToTexture { - src: *source, - dst: *destination, - size: *copy_size, - }); - } - - if copy_size.width == 0 || copy_size.height == 0 || copy_size.depth_or_array_layers == 0 { - log::trace!("Ignoring copy_texture_to_texture of size 0"); - cmd_buf_data_guard.mark_successful(); - return Ok(()); - } - - let src_texture = hub.textures.get(source.texture).get()?; - let dst_texture = hub.textures.get(destination.texture).get()?; - - src_texture.same_device_as(cmd_buf.as_ref())?; - dst_texture.same_device_as(cmd_buf.as_ref())?; - - // src and dst texture format must be copy-compatible - // https://gpuweb.github.io/gpuweb/#copy-compatible - if src_texture.desc.format.remove_srgb_suffix() - != dst_texture.desc.format.remove_srgb_suffix() - { - return Err(TransferError::TextureFormatsNotCopyCompatible { - src_format: src_texture.desc.format, - dst_format: dst_texture.desc.format, + #[cfg(feature = "trace")] + if let Some(ref mut list) = cmd_buf_data.commands { + list.push(TraceCommand::CopyTextureToTexture { + src: *source, + dst: *destination, + size: *copy_size, + }); } - .into()); - } - let (src_copy_size, array_layer_count) = - validate_texture_copy_range(source, &src_texture.desc, CopySide::Source, copy_size)?; - let (dst_copy_size, _) = validate_texture_copy_range( - destination, - &dst_texture.desc, - CopySide::Destination, - copy_size, - )?; - - let (src_range, src_tex_base) = extract_texture_selector(source, copy_size, &src_texture)?; - let (dst_range, dst_tex_base) = - extract_texture_selector(destination, copy_size, &dst_texture)?; - let src_texture_aspects = hal::FormatAspects::from(src_texture.desc.format); - let dst_texture_aspects = hal::FormatAspects::from(dst_texture.desc.format); - if src_tex_base.aspect != src_texture_aspects { - return Err(TransferError::CopySrcMissingAspects.into()); - } - if dst_tex_base.aspect != dst_texture_aspects { - return Err(TransferError::CopyDstMissingAspects.into()); - } - - if src_texture.desc.sample_count != dst_texture.desc.sample_count { - return Err(TransferError::SampleCountNotEqual { - src_sample_count: src_texture.desc.sample_count, - dst_sample_count: dst_texture.desc.sample_count, + if copy_size.width == 0 || copy_size.height == 0 || copy_size.depth_or_array_layers == 0 + { + log::trace!("Ignoring copy_texture_to_texture of size 0"); + return Ok(()); } - .into()); - } - // Handle texture init *before* dealing with barrier transitions so we - // have an easier time inserting "immediate-inits" that may be required - // by prior discards in rare cases. - handle_src_texture_init( - cmd_buf_data, - device, - source, - copy_size, - &src_texture, - &snatch_guard, - )?; - handle_dst_texture_init( - cmd_buf_data, - device, - destination, - copy_size, - &dst_texture, - &snatch_guard, - )?; + let src_texture = hub.textures.get(source.texture).get()?; + let dst_texture = hub.textures.get(destination.texture).get()?; - let src_pending = cmd_buf_data.trackers.textures.set_single( - &src_texture, - src_range, - wgt::TextureUses::COPY_SRC, - ); - let src_raw = src_texture.try_raw(&snatch_guard)?; - src_texture - .check_usage(TextureUsages::COPY_SRC) - .map_err(TransferError::MissingTextureUsage)?; + src_texture.same_device_as(cmd_buf.as_ref())?; + dst_texture.same_device_as(cmd_buf.as_ref())?; - //TODO: try to avoid this the collection. It's needed because both - // `src_pending` and `dst_pending` try to hold `trackers.textures` mutably. - let mut barriers: ArrayVec<_, 2> = src_pending - .map(|pending| pending.into_hal(src_raw)) - .collect(); - - let dst_pending = cmd_buf_data.trackers.textures.set_single( - &dst_texture, - dst_range, - wgt::TextureUses::COPY_DST, - ); - let dst_raw = dst_texture.try_raw(&snatch_guard)?; - dst_texture - .check_usage(TextureUsages::COPY_DST) - .map_err(TransferError::MissingTextureUsage)?; - - barriers.extend(dst_pending.map(|pending| pending.into_hal(dst_raw))); - - let hal_copy_size = hal::CopyExtent { - width: src_copy_size.width.min(dst_copy_size.width), - height: src_copy_size.height.min(dst_copy_size.height), - depth: src_copy_size.depth.min(dst_copy_size.depth), - }; - let regions = (0..array_layer_count) - .map(|rel_array_layer| { - let mut src_base = src_tex_base.clone(); - let mut dst_base = dst_tex_base.clone(); - src_base.array_layer += rel_array_layer; - dst_base.array_layer += rel_array_layer; - hal::TextureCopy { - src_base, - dst_base, - size: hal_copy_size, + // src and dst texture format must be copy-compatible + // https://gpuweb.github.io/gpuweb/#copy-compatible + if src_texture.desc.format.remove_srgb_suffix() + != dst_texture.desc.format.remove_srgb_suffix() + { + return Err(TransferError::TextureFormatsNotCopyCompatible { + src_format: src_texture.desc.format, + dst_format: dst_texture.desc.format, } - }) - .collect::>(); - let cmd_buf_raw = cmd_buf_data.encoder.open()?; - unsafe { - cmd_buf_raw.transition_textures(&barriers); - cmd_buf_raw.copy_texture_to_texture( - src_raw, - wgt::TextureUses::COPY_SRC, - dst_raw, - ®ions, - ); - } + .into()); + } - cmd_buf_data_guard.mark_successful(); - Ok(()) + let (src_copy_size, array_layer_count) = validate_texture_copy_range( + source, + &src_texture.desc, + CopySide::Source, + copy_size, + )?; + let (dst_copy_size, _) = validate_texture_copy_range( + destination, + &dst_texture.desc, + CopySide::Destination, + copy_size, + )?; + + let (src_range, src_tex_base) = + extract_texture_selector(source, copy_size, &src_texture)?; + let (dst_range, dst_tex_base) = + extract_texture_selector(destination, copy_size, &dst_texture)?; + let src_texture_aspects = hal::FormatAspects::from(src_texture.desc.format); + let dst_texture_aspects = hal::FormatAspects::from(dst_texture.desc.format); + if src_tex_base.aspect != src_texture_aspects { + return Err(TransferError::CopySrcMissingAspects.into()); + } + if dst_tex_base.aspect != dst_texture_aspects { + return Err(TransferError::CopyDstMissingAspects.into()); + } + + if src_texture.desc.sample_count != dst_texture.desc.sample_count { + return Err(TransferError::SampleCountNotEqual { + src_sample_count: src_texture.desc.sample_count, + dst_sample_count: dst_texture.desc.sample_count, + } + .into()); + } + + // Handle texture init *before* dealing with barrier transitions so we + // have an easier time inserting "immediate-inits" that may be required + // by prior discards in rare cases. + handle_src_texture_init( + cmd_buf_data, + device, + source, + copy_size, + &src_texture, + &snatch_guard, + )?; + handle_dst_texture_init( + cmd_buf_data, + device, + destination, + copy_size, + &dst_texture, + &snatch_guard, + )?; + + let src_pending = cmd_buf_data.trackers.textures.set_single( + &src_texture, + src_range, + wgt::TextureUses::COPY_SRC, + ); + let src_raw = src_texture.try_raw(&snatch_guard)?; + src_texture + .check_usage(TextureUsages::COPY_SRC) + .map_err(TransferError::MissingTextureUsage)?; + + //TODO: try to avoid this the collection. It's needed because both + // `src_pending` and `dst_pending` try to hold `trackers.textures` mutably. + let mut barriers: ArrayVec<_, 2> = src_pending + .map(|pending| pending.into_hal(src_raw)) + .collect(); + + let dst_pending = cmd_buf_data.trackers.textures.set_single( + &dst_texture, + dst_range, + wgt::TextureUses::COPY_DST, + ); + let dst_raw = dst_texture.try_raw(&snatch_guard)?; + dst_texture + .check_usage(TextureUsages::COPY_DST) + .map_err(TransferError::MissingTextureUsage)?; + + barriers.extend(dst_pending.map(|pending| pending.into_hal(dst_raw))); + + let hal_copy_size = hal::CopyExtent { + width: src_copy_size.width.min(dst_copy_size.width), + height: src_copy_size.height.min(dst_copy_size.height), + depth: src_copy_size.depth.min(dst_copy_size.depth), + }; + let regions = (0..array_layer_count) + .map(|rel_array_layer| { + let mut src_base = src_tex_base.clone(); + let mut dst_base = dst_tex_base.clone(); + src_base.array_layer += rel_array_layer; + dst_base.array_layer += rel_array_layer; + hal::TextureCopy { + src_base, + dst_base, + size: hal_copy_size, + } + }) + .collect::>(); + let cmd_buf_raw = cmd_buf_data.encoder.open()?; + unsafe { + cmd_buf_raw.transition_textures(&barriers); + cmd_buf_raw.copy_texture_to_texture( + src_raw, + wgt::TextureUses::COPY_SRC, + dst_raw, + ®ions, + ); + } + + Ok(()) + }) } }