From b4e413cba1ff61050cb94348687ba500abd9ad1c Mon Sep 17 00:00:00 2001 From: Sam <16504129+sagudev@users.noreply.github.com> Date: Tue, 7 Jul 2026 10:09:43 +0200 Subject: [PATCH] [core] Make `Global` methods thin in clear (#9822) Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com> --- wgpu-core/src/command/clear.rs | 67 ++++++++++++++++++++-------------- 1 file changed, 40 insertions(+), 27 deletions(-) diff --git a/wgpu-core/src/command/clear.rs b/wgpu-core/src/command/clear.rs index 5f5f7a4a7..3f4109575 100644 --- a/wgpu-core/src/command/clear.rs +++ b/wgpu-core/src/command/clear.rs @@ -110,6 +110,44 @@ impl WebGpuError for ClearError { } } +impl super::CommandEncoder { + pub fn clear_buffer( + self: &Arc, + dst: Arc, + offset: BufferAddress, + size: Option, + ) -> Result<(), EncoderStateError> { + profiling::scope!("CommandEncoder::clear_buffer"); + api_log!("CommandEncoder::clear_buffer {:?}", Arc::as_ptr(&dst)); + + let mut cmd_buf_data = self.data.lock(); + + cmd_buf_data.push_with(|| -> Result<_, ClearError> { + dst.check_is_valid()?; + Ok(ArcCommand::ClearBuffer { dst, offset, size }) + }) + } + + pub fn clear_texture( + self: &Arc, + dst: Arc, + subresource_range: &ImageSubresourceRange, + ) -> Result<(), EncoderStateError> { + profiling::scope!("CommandEncoder::clear_texture"); + api_log!("CommandEncoder::clear_texture {:?}", Arc::as_ptr(&dst)); + + let mut cmd_buf_data = self.data.lock(); + + cmd_buf_data.push_with(|| -> Result<_, ClearError> { + dst.check_valid()?; + Ok(ArcCommand::ClearTexture { + dst, + subresource_range: *subresource_range, + }) + }) + } +} + impl Global { pub fn command_encoder_clear_buffer( &self, @@ -118,23 +156,10 @@ impl Global { offset: BufferAddress, size: Option, ) -> Result<(), EncoderStateError> { - profiling::scope!("CommandEncoder::clear_buffer"); - api_log!("CommandEncoder::clear_buffer {dst:?}"); - let hub = &self.hub; let cmd_enc = hub.command_encoders.get(command_encoder_id); - let mut cmd_buf_data = cmd_enc.data.lock(); - - cmd_buf_data.push_with(|| -> Result<_, ClearError> { - let buffer = self.resolve_buffer_id(dst); - buffer.check_is_valid()?; - Ok(ArcCommand::ClearBuffer { - dst: buffer, - offset, - size, - }) - }) + cmd_enc.clear_buffer(hub.buffers.get(dst), offset, size) } pub fn command_encoder_clear_texture( @@ -143,23 +168,11 @@ impl Global { dst: TextureId, subresource_range: &ImageSubresourceRange, ) -> Result<(), EncoderStateError> { - profiling::scope!("CommandEncoder::clear_texture"); - api_log!("CommandEncoder::clear_texture {dst:?}"); - let hub = &self.hub; let cmd_enc = hub.command_encoders.get(command_encoder_id); - let mut cmd_buf_data = cmd_enc.data.lock(); - - cmd_buf_data.push_with(|| -> Result<_, ClearError> { - let texture = self.resolve_texture_id(dst); - texture.check_valid()?; - Ok(ArcCommand::ClearTexture { - dst: texture, - subresource_range: *subresource_range, - }) - }) + cmd_enc.clear_texture(hub.textures.get(dst), subresource_range) } }