From a5c3be575edae1d461b8dfb008fcd32236f22cf5 Mon Sep 17 00:00:00 2001 From: teoxoy <28601907+teoxoy@users.noreply.github.com> Date: Fri, 20 Dec 2024 13:50:20 +0100 Subject: [PATCH] remove `device` arg from all `CommandEncoder` methods --- wgpu-core/src/command/clear.rs | 4 +-- wgpu-core/src/command/compute.rs | 14 +++------ wgpu-core/src/command/mod.rs | 47 ++++++++++++++-------------- wgpu-core/src/command/query.rs | 4 +-- wgpu-core/src/command/ray_tracing.rs | 4 +-- wgpu-core/src/command/render.rs | 14 +++------ wgpu-core/src/command/transfer.rs | 10 +++--- wgpu-core/src/device/queue.rs | 13 +++----- wgpu-core/src/resource.rs | 2 +- 9 files changed, 49 insertions(+), 63 deletions(-) diff --git a/wgpu-core/src/command/clear.rs b/wgpu-core/src/command/clear.rs index 9b7bc9726a..0811c2ac42 100644 --- a/wgpu-core/src/command/clear.rs +++ b/wgpu-core/src/command/clear.rs @@ -155,7 +155,7 @@ impl Global { // actual hal barrier & operation let dst_barrier = dst_pending.map(|pending| pending.into_hal(&dst_buffer, &snatch_guard)); - let cmd_buf_raw = cmd_buf_data.encoder.open(&cmd_buf.device)?; + let cmd_buf_raw = cmd_buf_data.encoder.open()?; unsafe { cmd_buf_raw.transition_buffers(dst_barrier.as_slice()); cmd_buf_raw.clear_buffer(dst_raw, offset..end_offset); @@ -235,7 +235,7 @@ impl Global { let device = &cmd_buf.device; device.check_is_valid()?; - let (encoder, tracker) = cmd_buf_data.open_encoder_and_tracker(&cmd_buf.device)?; + let (encoder, tracker) = cmd_buf_data.open_encoder_and_tracker()?; let snatch_guard = device.snatchable_lock.read(); clear_texture( diff --git a/wgpu-core/src/command/compute.rs b/wgpu-core/src/command/compute.rs index eaad9230a1..0fa6845d28 100644 --- a/wgpu-core/src/command/compute.rs +++ b/wgpu-core/src/command/compute.rs @@ -415,11 +415,9 @@ impl Global { // We automatically keep extending command buffers over time, and because // we want to insert a command buffer _before_ what we're about to record, // we need to make sure to close the previous one. - encoder - .close_if_open(&cmd_buf.device) - .map_pass_err(pass_scope)?; + encoder.close_if_open().map_pass_err(pass_scope)?; let raw_encoder = encoder - .open_pass(base.label.as_deref(), &cmd_buf.device) + .open_pass(base.label.as_deref()) .map_pass_err(pass_scope)?; let mut state = State { @@ -598,13 +596,13 @@ impl Global { } = state; // Stop the current command buffer. - encoder.close(&cmd_buf.device).map_pass_err(pass_scope)?; + encoder.close().map_pass_err(pass_scope)?; // Create a new command buffer, which we will insert _before_ the body of the compute pass. // // Use that buffer to insert barriers and clear discarded images. let transit = encoder - .open_pass(Some("(wgpu internal) Pre Pass"), &cmd_buf.device) + .open_pass(Some("(wgpu internal) Pre Pass")) .map_pass_err(pass_scope)?; fixup_discarded_surfaces( pending_discard_init_fixups.into_iter(), @@ -620,9 +618,7 @@ impl Global { &snatch_guard, ); // Close the command buffer, and swap it with the previous. - encoder - .close_and_swap(&cmd_buf.device) - .map_pass_err(pass_scope)?; + encoder.close_and_swap().map_pass_err(pass_scope)?; cmd_buf_data_guard.mark_successful(); Ok(()) diff --git a/wgpu-core/src/command/mod.rs b/wgpu-core/src/command/mod.rs index d3a7c9c366..88cf874d3a 100644 --- a/wgpu-core/src/command/mod.rs +++ b/wgpu-core/src/command/mod.rs @@ -150,10 +150,10 @@ impl CommandEncoderStatus { } } - fn finish(&mut self, device: &Device) -> Result<(), CommandEncoderError> { + fn finish(&mut self) -> Result<(), CommandEncoderError> { match mem::replace(self, Self::Error) { Self::Recording(mut inner) => { - if let Err(e) = inner.encoder.close_if_open(device) { + if let Err(e) = inner.encoder.close_if_open() { Err(e.into()) } else { *self = Self::Finished(inner); @@ -302,11 +302,12 @@ impl CommandEncoder { /// [l]: CommandEncoder::list /// [`transition_buffers`]: hal::CommandEncoder::transition_buffers /// [`transition_textures`]: hal::CommandEncoder::transition_textures - fn close_and_swap(&mut self, device: &Device) -> Result<(), DeviceError> { + fn close_and_swap(&mut self) -> Result<(), DeviceError> { assert!(self.is_open); self.is_open = false; - let new = unsafe { self.raw.end_encoding() }.map_err(|e| device.handle_hal_error(e))?; + let new = + unsafe { self.raw.end_encoding() }.map_err(|e| self.device.handle_hal_error(e))?; self.list.insert(self.list.len() - 1, new); Ok(()) @@ -322,11 +323,12 @@ impl CommandEncoder { /// - If the encoder is not open. /// /// [l]: CommandEncoder::list - pub(crate) fn close_and_push_front(&mut self, device: &Device) -> Result<(), DeviceError> { + pub(crate) fn close_and_push_front(&mut self) -> Result<(), DeviceError> { assert!(self.is_open); self.is_open = false; - let new = unsafe { self.raw.end_encoding() }.map_err(|e| device.handle_hal_error(e))?; + let new = + unsafe { self.raw.end_encoding() }.map_err(|e| self.device.handle_hal_error(e))?; self.list.insert(0, new); Ok(()) @@ -342,11 +344,12 @@ impl CommandEncoder { /// - If the encoder is not open. /// /// [l]: CommandEncoder::list - pub(crate) fn close(&mut self, device: &Device) -> Result<(), DeviceError> { + pub(crate) fn close(&mut self) -> Result<(), DeviceError> { assert!(self.is_open); self.is_open = false; - let cmd_buf = unsafe { self.raw.end_encoding() }.map_err(|e| device.handle_hal_error(e))?; + let cmd_buf = + unsafe { self.raw.end_encoding() }.map_err(|e| self.device.handle_hal_error(e))?; self.list.push(cmd_buf); Ok(()) @@ -362,11 +365,11 @@ impl CommandEncoder { /// On return, the underlying hal encoder is closed. /// /// [l]: CommandEncoder::list - fn close_if_open(&mut self, device: &Device) -> Result<(), DeviceError> { + fn close_if_open(&mut self) -> Result<(), DeviceError> { if self.is_open { self.is_open = false; let cmd_buf = - unsafe { self.raw.end_encoding() }.map_err(|e| device.handle_hal_error(e))?; + unsafe { self.raw.end_encoding() }.map_err(|e| self.device.handle_hal_error(e))?; self.list.push(cmd_buf); } @@ -376,15 +379,12 @@ impl CommandEncoder { /// Begin recording a new command buffer, if we haven't already. /// /// The underlying hal encoder is put in the "recording" state. - pub(crate) fn open( - &mut self, - device: &Device, - ) -> Result<&mut dyn hal::DynCommandEncoder, DeviceError> { + pub(crate) fn open(&mut self) -> Result<&mut dyn hal::DynCommandEncoder, DeviceError> { if !self.is_open { self.is_open = true; let hal_label = self.hal_label.as_deref(); unsafe { self.raw.begin_encoding(hal_label) } - .map_err(|e| device.handle_hal_error(e))?; + .map_err(|e| self.device.handle_hal_error(e))?; } Ok(self.raw.as_mut()) @@ -401,13 +401,13 @@ impl CommandEncoder { pub(crate) fn open_pass( &mut self, label: Option<&str>, - device: &Device, ) -> Result<&mut dyn hal::DynCommandEncoder, DeviceError> { assert!(!self.is_open); self.is_open = true; - let hal_label = hal_label(label, device.instance_flags); - unsafe { self.raw.begin_encoding(hal_label) }.map_err(|e| device.handle_hal_error(e))?; + let hal_label = hal_label(label, self.device.instance_flags); + unsafe { self.raw.begin_encoding(hal_label) } + .map_err(|e| self.device.handle_hal_error(e))?; Ok(self.raw.as_mut()) } @@ -468,9 +468,8 @@ pub struct CommandBufferMutable { impl CommandBufferMutable { pub(crate) fn open_encoder_and_tracker( &mut self, - device: &Device, ) -> Result<(&mut dyn hal::DynCommandEncoder, &mut Tracker), DeviceError> { - let encoder = self.encoder.open(device)?; + let encoder = self.encoder.open()?; let tracker = &mut self.trackers; Ok((encoder, tracker)) @@ -753,7 +752,7 @@ impl Global { let cmd_buf = hub.command_buffers.get(encoder_id.into_command_buffer_id()); - let error = match cmd_buf.data.lock().finish(&cmd_buf.device) { + let error = match cmd_buf.data.lock().finish() { Ok(_) => None, Err(e) => Some(e), }; @@ -781,7 +780,7 @@ impl Global { list.push(TraceCommand::PushDebugGroup(label.to_string())); } - let cmd_buf_raw = cmd_buf_data.encoder.open(&cmd_buf.device)?; + let cmd_buf_raw = cmd_buf_data.encoder.open()?; if !cmd_buf .device .instance_flags @@ -821,7 +820,7 @@ impl Global { .instance_flags .contains(wgt::InstanceFlags::DISCARD_HAL_LABELS) { - let cmd_buf_raw = cmd_buf_data.encoder.open(&cmd_buf.device)?; + let cmd_buf_raw = cmd_buf_data.encoder.open()?; unsafe { cmd_buf_raw.insert_debug_marker(label); } @@ -850,7 +849,7 @@ impl Global { list.push(TraceCommand::PopDebugGroup); } - let cmd_buf_raw = cmd_buf_data.encoder.open(&cmd_buf.device)?; + let cmd_buf_raw = cmd_buf_data.encoder.open()?; if !cmd_buf .device .instance_flags diff --git a/wgpu-core/src/command/query.rs b/wgpu-core/src/command/query.rs index 56ca3339de..c2444aa129 100644 --- a/wgpu-core/src/command/query.rs +++ b/wgpu-core/src/command/query.rs @@ -337,7 +337,7 @@ impl Global { }); } - let raw_encoder = cmd_buf_data.encoder.open(&cmd_buf.device)?; + let raw_encoder = cmd_buf_data.encoder.open()?; let query_set = hub.query_sets.get(query_set_id).get()?; @@ -447,7 +447,7 @@ impl Global { ); let raw_dst_buffer = dst_buffer.try_raw(&snatch_guard)?; - let raw_encoder = cmd_buf_data.encoder.open(&cmd_buf.device)?; + let raw_encoder = cmd_buf_data.encoder.open()?; unsafe { raw_encoder.transition_buffers(dst_barrier.as_slice()); raw_encoder.copy_query_results( diff --git a/wgpu-core/src/command/ray_tracing.rs b/wgpu-core/src/command/ray_tracing.rs index 01fe891575..65922524f9 100644 --- a/wgpu-core/src/command/ray_tracing.rs +++ b/wgpu-core/src/command/ray_tracing.rs @@ -322,7 +322,7 @@ impl Global { let blas_present = !blas_storage.is_empty(); let tlas_present = !tlas_storage.is_empty(); - let cmd_buf_raw = cmd_buf_data.encoder.open(device)?; + let cmd_buf_raw = cmd_buf_data.encoder.open()?; let mut descriptors = Vec::new(); @@ -674,7 +674,7 @@ impl Global { let blas_present = !blas_storage.is_empty(); let tlas_present = !tlas_storage.is_empty(); - let cmd_buf_raw = cmd_buf_data.encoder.open(device)?; + let cmd_buf_raw = cmd_buf_data.encoder.open()?; let mut descriptors = Vec::new(); diff --git a/wgpu-core/src/command/render.rs b/wgpu-core/src/command/render.rs index 6e2d5ab07a..2442ef3660 100644 --- a/wgpu-core/src/command/render.rs +++ b/wgpu-core/src/command/render.rs @@ -1663,11 +1663,9 @@ impl Global { // We automatically keep extending command buffers over time, and because // we want to insert a command buffer _before_ what we're about to record, // we need to make sure to close the previous one. + encoder.close_if_open().map_pass_err(pass_scope)?; encoder - .close_if_open(&cmd_buf.device) - .map_pass_err(pass_scope)?; - encoder - .open_pass(base.label.as_deref(), &cmd_buf.device) + .open_pass(base.label.as_deref()) .map_pass_err(pass_scope)?; let info = RenderPassInfo::start( @@ -1971,7 +1969,7 @@ impl Global { .finish(state.raw_encoder, state.snatch_guard) .map_pass_err(pass_scope)?; - encoder.close(&cmd_buf.device).map_pass_err(pass_scope)?; + encoder.close().map_pass_err(pass_scope)?; (trackers, pending_discard_init_fixups) }; @@ -1980,7 +1978,7 @@ impl Global { { let transit = encoder - .open_pass(Some("(wgpu internal) Pre Pass"), &cmd_buf.device) + .open_pass(Some("(wgpu internal) Pre Pass")) .map_pass_err(pass_scope)?; fixup_discarded_surfaces( @@ -1996,9 +1994,7 @@ impl Global { CommandBuffer::insert_barriers_from_scope(transit, tracker, &scope, snatch_guard); } - encoder - .close_and_swap(&cmd_buf.device) - .map_pass_err(pass_scope)?; + encoder.close_and_swap().map_pass_err(pass_scope)?; cmd_buf_data_guard.mark_successful(); Ok(()) diff --git a/wgpu-core/src/command/transfer.rs b/wgpu-core/src/command/transfer.rs index 3ecff5ca43..291c44bd2c 100644 --- a/wgpu-core/src/command/transfer.rs +++ b/wgpu-core/src/command/transfer.rs @@ -444,7 +444,7 @@ fn handle_texture_init( // In rare cases we may need to insert an init operation immediately onto the command buffer. if !immediate_inits.is_empty() { - let cmd_buf_raw = cmd_buf_data.encoder.open(device)?; + let cmd_buf_raw = cmd_buf_data.encoder.open()?; for init in immediate_inits { clear_texture( &init.texture, @@ -678,7 +678,7 @@ impl Global { dst_offset: destination_offset, size: wgt::BufferSize::new(size).unwrap(), }; - let cmd_buf_raw = cmd_buf_data.encoder.open(&cmd_buf.device)?; + let cmd_buf_raw = cmd_buf_data.encoder.open()?; let barriers = src_barrier .into_iter() .chain(dst_barrier) @@ -838,7 +838,7 @@ impl Global { }) .collect::>(); - let cmd_buf_raw = cmd_buf_data.encoder.open(&cmd_buf.device)?; + 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()); @@ -1004,7 +1004,7 @@ impl Global { } }) .collect::>(); - let cmd_buf_raw = cmd_buf_data.encoder.open(&cmd_buf.device)?; + 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); @@ -1168,7 +1168,7 @@ impl Global { } }) .collect::>(); - let cmd_buf_raw = cmd_buf_data.encoder.open(&cmd_buf.device)?; + let cmd_buf_raw = cmd_buf_data.encoder.open()?; unsafe { cmd_buf_raw.transition_textures(&barriers); cmd_buf_raw.copy_texture_to_texture( diff --git a/wgpu-core/src/device/queue.rs b/wgpu-core/src/device/queue.rs index 77b47665eb..cd6731ae04 100644 --- a/wgpu-core/src/device/queue.rs +++ b/wgpu-core/src/device/queue.rs @@ -1164,10 +1164,7 @@ impl Queue { }; // execute resource transitions - if let Err(e) = baked - .encoder - .open_pass(Some("(wgpu internal) Transit"), &self.device) - { + if let Err(e) = baked.encoder.open_pass(Some("(wgpu internal) Transit")) { break 'error Err(e.into()); } @@ -1194,7 +1191,7 @@ impl Queue { &snatch_guard, ); - if let Err(e) = baked.encoder.close_and_push_front(&self.device) { + if let Err(e) = baked.encoder.close_and_push_front() { break 'error Err(e.into()); } @@ -1202,9 +1199,7 @@ impl Queue { // Note: we could technically do it after all of the command buffers, // but here we have a command encoder by hand, so it's easier to use it. if !used_surface_textures.is_empty() { - if let Err(e) = baked - .encoder - .open_pass(Some("(wgpu internal) Present"), &self.device) + if let Err(e) = baked.encoder.open_pass(Some("(wgpu internal) Present")) { break 'error Err(e.into()); } @@ -1218,7 +1213,7 @@ impl Queue { unsafe { baked.encoder.raw.transition_textures(&texture_barriers); }; - if let Err(e) = baked.encoder.close(&self.device) { + if let Err(e) = baked.encoder.close() { break 'error Err(e.into()); } used_surface_textures = track::TextureUsageScope::default(); diff --git a/wgpu-core/src/resource.rs b/wgpu-core/src/resource.rs index 08c76c6d33..c29bd816ee 100644 --- a/wgpu-core/src/resource.rs +++ b/wgpu-core/src/resource.rs @@ -1381,7 +1381,7 @@ impl Global { if let Ok(mut cmd_buf_data_guard) = cmd_buf_data_guard { let cmd_buf_raw = cmd_buf_data_guard .encoder - .open(&cmd_buf.device) + .open() .ok() .and_then(|encoder| encoder.as_any_mut().downcast_mut()); let ret = hal_command_encoder_callback(cmd_buf_raw);