diff --git a/wgpu-core/src/command/mod.rs b/wgpu-core/src/command/mod.rs index 4f6502a2e..1d80d1a98 100644 --- a/wgpu-core/src/command/mod.rs +++ b/wgpu-core/src/command/mod.rs @@ -313,6 +313,26 @@ impl CommandEncoder { Ok(()) } + /// Finish the current command buffer and insert it at the beginning + /// of [`self.list`][l]. + /// + /// On return, the underlying hal encoder is closed. + /// + /// # Panics + /// + /// - If the encoder is not open. + /// + /// [l]: CommandEncoder::list + pub(crate) fn close_and_push_front(&mut self, device: &Device) -> 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))?; + self.list.insert(0, new); + + Ok(()) + } + /// Finish the current command buffer, if any, and add it to the /// end of [`self.list`][l]. /// @@ -323,7 +343,7 @@ impl CommandEncoder { /// On return, the underlying hal encoder is closed. /// /// [l]: CommandEncoder::list - fn close(&mut self, device: &Device) -> Result<(), DeviceError> { + pub(crate) fn close(&mut self, device: &Device) -> Result<(), DeviceError> { if self.is_open { self.is_open = false; let cmd_buf = @@ -355,7 +375,11 @@ impl CommandEncoder { /// its own label. /// /// The underlying hal encoder is put in the "recording" state. - fn open_pass(&mut self, hal_label: Option<&str>, device: &Device) -> Result<(), DeviceError> { + pub(crate) fn open_pass( + &mut self, + hal_label: Option<&str>, + device: &Device, + ) -> Result<(), DeviceError> { self.is_open = true; unsafe { self.raw.begin_encoding(hal_label) }.map_err(|e| device.handle_hal_error(e))?; diff --git a/wgpu-core/src/device/queue.rs b/wgpu-core/src/device/queue.rs index a44b3241a..e61e3d672 100644 --- a/wgpu-core/src/device/queue.rs +++ b/wgpu-core/src/device/queue.rs @@ -1165,14 +1165,10 @@ impl Queue { }; // execute resource transitions - if let Err(e) = unsafe { - baked.encoder.raw.begin_encoding(hal_label( - Some("(wgpu internal) Transit"), - self.device.instance_flags, - )) - } - .map_err(|e| self.device.handle_hal_error(e)) - { + if let Err(e) = baked.encoder.open_pass( + hal_label(Some("(wgpu internal) Transit"), self.device.instance_flags), + &self.device, + ) { break 'error Err(e.into()); } @@ -1199,21 +1195,21 @@ impl Queue { &snatch_guard, ); - let transit = unsafe { baked.encoder.raw.end_encoding().unwrap() }; - baked.encoder.list.insert(0, transit); + if let Err(e) = baked.encoder.close_and_push_front(&self.device) { + break 'error Err(e.into()); + } // Transition surface textures into `Present` state. // 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) = unsafe { - baked.encoder.raw.begin_encoding(hal_label( + if let Err(e) = baked.encoder.open_pass( + hal_label( Some("(wgpu internal) Present"), self.device.instance_flags, - )) - } - .map_err(|e| self.device.handle_hal_error(e)) - { + ), + &self.device, + ) { break 'error Err(e.into()); } let texture_barriers = trackers @@ -1223,11 +1219,12 @@ impl Queue { &snatch_guard, ) .collect::>(); - let present = unsafe { + unsafe { baked.encoder.raw.transition_textures(&texture_barriers); - baked.encoder.raw.end_encoding().unwrap() }; - baked.encoder.list.push(present); + if let Err(e) = baked.encoder.close(&self.device) { + break 'error Err(e.into()); + } used_surface_textures = track::TextureUsageScope::default(); }