From c5b16e1843a2389e98fbbb0ef9420791ec38211f Mon Sep 17 00:00:00 2001 From: sagudev <16504129+sagudev@users.noreply.github.com> Date: Tue, 7 Jul 2026 07:25:21 +0200 Subject: [PATCH] [core] Make `Global` methods thin in transfer Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com> --- wgpu-core/src/command/transfer.rs | 179 +++++++++++++++++++++--------- 1 file changed, 126 insertions(+), 53 deletions(-) diff --git a/wgpu-core/src/command/transfer.rs b/wgpu-core/src/command/transfer.rs index 8a63b6365..8a5e2bdfd 100644 --- a/wgpu-core/src/command/transfer.rs +++ b/wgpu-core/src/command/transfer.rs @@ -825,63 +825,56 @@ fn handle_buffer_init( } } -impl Global { - pub fn command_encoder_copy_buffer_to_buffer( - &self, - command_encoder_id: CommandEncoderId, - source: BufferId, +impl super::CommandEncoder { + pub fn copy_buffer_to_buffer( + self: &Arc, + source: Arc, source_offset: BufferAddress, - destination: BufferId, + destination: Arc, destination_offset: BufferAddress, size: Option, ) -> Result<(), EncoderStateError> { profiling::scope!("CommandEncoder::copy_buffer_to_buffer"); api_log!( - "CommandEncoder::copy_buffer_to_buffer {source:?} -> {destination:?} {size:?}bytes" + "CommandEncoder::copy_buffer_to_buffer {:?} -> {:?} {size:?}bytes", + Arc::as_ptr(&source), + Arc::as_ptr(&destination) ); - let hub = &self.hub; - - let cmd_enc = hub.command_encoders.get(command_encoder_id); - let mut cmd_buf_data = cmd_enc.data.lock(); + let mut cmd_buf_data = self.data.lock(); cmd_buf_data.push_with(|| -> Result<_, CommandEncoderError> { - let source_buffer = self.resolve_buffer_id(source); - source_buffer.check_is_valid()?; - let destination_buffer = self.resolve_buffer_id(destination); - destination_buffer.check_is_valid()?; + source.check_is_valid()?; + destination.check_is_valid()?; Ok(ArcCommand::CopyBufferToBuffer { - src: source_buffer, + src: source, src_offset: source_offset, - dst: destination_buffer, + dst: destination, dst_offset: destination_offset, size, }) }) } - pub fn command_encoder_copy_buffer_to_texture( - &self, - command_encoder_id: CommandEncoderId, - source: &TexelCopyBufferInfo, - destination: &wgt::TexelCopyTextureInfo, + pub fn copy_buffer_to_texture( + self: &Arc, + source: &wgt::TexelCopyBufferInfo>, + destination: &wgt::TexelCopyTextureInfo>, copy_size: &Extent3d, ) -> Result<(), EncoderStateError> { profiling::scope!("CommandEncoder::copy_buffer_to_texture"); api_log!( "CommandEncoder::copy_buffer_to_texture {:?} -> {:?} {copy_size:?}", - source.buffer, - destination.texture + Arc::as_ptr(&source.buffer), + Arc::as_ptr(&destination.texture) ); - let cmd_enc = self.hub.command_encoders.get(command_encoder_id); - - let mut cmd_buf_data = cmd_enc.data.lock(); + let mut cmd_buf_data = self.data.lock(); cmd_buf_data.push_with(|| -> Result<_, CommandEncoderError> { - let texture = self.resolve_texture_id(destination.texture); + let texture = destination.texture.clone(); texture.check_valid()?; - let source_buffer = self.resolve_buffer_id(source.buffer); + let source_buffer = source.buffer.clone(); source_buffer.check_is_valid()?; Ok(ArcCommand::CopyBufferToTexture { src: wgt::TexelCopyBufferInfo::> { @@ -899,28 +892,25 @@ impl Global { }) } - pub fn command_encoder_copy_texture_to_buffer( - &self, - command_encoder_id: CommandEncoderId, - source: &wgt::TexelCopyTextureInfo, - destination: &TexelCopyBufferInfo, + pub fn copy_texture_to_buffer( + self: &Arc, + source: &wgt::TexelCopyTextureInfo>, + destination: &wgt::TexelCopyBufferInfo>, copy_size: &Extent3d, ) -> Result<(), EncoderStateError> { profiling::scope!("CommandEncoder::copy_texture_to_buffer"); api_log!( "CommandEncoder::copy_texture_to_buffer {:?} -> {:?} {copy_size:?}", - source.texture, - destination.buffer + Arc::as_ptr(&source.texture), + Arc::as_ptr(&destination.buffer) ); - let cmd_enc = self.hub.command_encoders.get(command_encoder_id); - - let mut cmd_buf_data = cmd_enc.data.lock(); + let mut cmd_buf_data = self.data.lock(); cmd_buf_data.push_with(|| -> Result<_, CommandEncoderError> { - let texture = self.resolve_texture_id(source.texture); + let texture = source.texture.clone(); texture.check_valid()?; - let destination_buffer = self.resolve_buffer_id(destination.buffer); + let destination_buffer = destination.buffer.clone(); destination_buffer.check_is_valid()?; Ok(ArcCommand::CopyTextureToBuffer { src: wgt::TexelCopyTextureInfo::> { @@ -938,27 +928,24 @@ impl Global { }) } - pub fn command_encoder_copy_texture_to_texture( - &self, - command_encoder_id: CommandEncoderId, - source: &wgt::TexelCopyTextureInfo, - destination: &wgt::TexelCopyTextureInfo, + pub fn copy_texture_to_texture( + self: &Arc, + source: &wgt::TexelCopyTextureInfo>, + destination: &wgt::TexelCopyTextureInfo>, copy_size: &Extent3d, ) -> Result<(), EncoderStateError> { profiling::scope!("CommandEncoder::copy_texture_to_texture"); api_log!( "CommandEncoder::copy_texture_to_texture {:?} -> {:?} {copy_size:?}", - source.texture, - destination.texture + Arc::as_ptr(&source.texture), + Arc::as_ptr(&destination.texture) ); - let cmd_enc = self.hub.command_encoders.get(command_encoder_id); - - let mut cmd_buf_data = cmd_enc.data.lock(); + let mut cmd_buf_data = self.data.lock(); cmd_buf_data.push_with(|| -> Result<_, CommandEncoderError> { - let src_texture = self.resolve_texture_id(source.texture); - let dst_texture = self.resolve_texture_id(destination.texture); + let src_texture = source.texture.clone(); + let dst_texture = destination.texture.clone(); src_texture.check_valid()?; dst_texture.check_valid()?; Ok(ArcCommand::CopyTextureToTexture { @@ -980,6 +967,92 @@ impl Global { } } +impl Global { + pub fn command_encoder_copy_buffer_to_buffer( + &self, + command_encoder_id: CommandEncoderId, + source: BufferId, + source_offset: BufferAddress, + destination: BufferId, + destination_offset: BufferAddress, + size: Option, + ) -> Result<(), EncoderStateError> { + let hub = &self.hub; + + let cmd_enc = hub.command_encoders.get(command_encoder_id); + let source = self.resolve_buffer_id(source); + let destination = self.resolve_buffer_id(destination); + cmd_enc.copy_buffer_to_buffer(source, source_offset, destination, destination_offset, size) + } + + pub fn command_encoder_copy_buffer_to_texture( + &self, + command_encoder_id: CommandEncoderId, + source: &TexelCopyBufferInfo, + destination: &wgt::TexelCopyTextureInfo, + copy_size: &Extent3d, + ) -> Result<(), EncoderStateError> { + let cmd_enc = self.hub.command_encoders.get(command_encoder_id); + let source = wgt::TexelCopyBufferInfo { + buffer: self.resolve_buffer_id(source.buffer), + layout: source.layout, + }; + let destination = wgt::TexelCopyTextureInfo { + texture: self.resolve_texture_id(destination.texture), + mip_level: destination.mip_level, + origin: destination.origin, + aspect: destination.aspect, + }; + cmd_enc.copy_buffer_to_texture(&source, &destination, copy_size) + } + + pub fn command_encoder_copy_texture_to_buffer( + &self, + command_encoder_id: CommandEncoderId, + source: &wgt::TexelCopyTextureInfo, + destination: &TexelCopyBufferInfo, + copy_size: &Extent3d, + ) -> Result<(), EncoderStateError> { + let cmd_enc = self.hub.command_encoders.get(command_encoder_id); + + let source = wgt::TexelCopyTextureInfo { + texture: self.resolve_texture_id(source.texture), + mip_level: source.mip_level, + origin: source.origin, + aspect: source.aspect, + }; + let destination = wgt::TexelCopyBufferInfo { + buffer: self.resolve_buffer_id(destination.buffer), + layout: destination.layout, + }; + cmd_enc.copy_texture_to_buffer(&source, &destination, copy_size) + } + + pub fn command_encoder_copy_texture_to_texture( + &self, + command_encoder_id: CommandEncoderId, + source: &wgt::TexelCopyTextureInfo, + destination: &wgt::TexelCopyTextureInfo, + copy_size: &Extent3d, + ) -> Result<(), EncoderStateError> { + let cmd_enc = self.hub.command_encoders.get(command_encoder_id); + + let source = wgt::TexelCopyTextureInfo { + texture: self.resolve_texture_id(source.texture), + mip_level: source.mip_level, + origin: source.origin, + aspect: source.aspect, + }; + let destination = wgt::TexelCopyTextureInfo { + texture: self.resolve_texture_id(destination.texture), + mip_level: destination.mip_level, + origin: destination.origin, + aspect: destination.aspect, + }; + cmd_enc.copy_texture_to_texture(&source, &destination, copy_size) + } +} + pub(super) fn copy_buffer_to_buffer( state: &mut EncodingState, src_buffer: &Arc,