From 0b7a5b054bf06c68ba188cd2ba5d7423925214e8 Mon Sep 17 00:00:00 2001 From: teoxoy <28601907+teoxoy@users.noreply.github.com> Date: Wed, 19 Jun 2024 11:19:34 +0200 Subject: [PATCH] move command encoder/buffer creation in a new device method --- wgpu-core/src/device/global.rs | 23 ++++------------------- wgpu-core/src/device/resource.rs | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/wgpu-core/src/device/global.rs b/wgpu-core/src/device/global.rs index d3d1c5be5d..827de6af24 100644 --- a/wgpu-core/src/device/global.rs +++ b/wgpu-core/src/device/global.rs @@ -1313,26 +1313,11 @@ impl Global { Ok(device) => device, Err(_) => break 'error DeviceError::InvalidDeviceId, }; - if !device.is_valid() { - break 'error DeviceError::Lost; - } - let Some(queue) = device.get_queue() else { - break 'error DeviceError::InvalidQueueId; + + let command_buffer = match device.create_command_encoder(&desc.label) { + Ok(command_buffer) => command_buffer, + Err(e) => break 'error e, }; - let encoder = match device - .command_allocator - .acquire_encoder(device.raw(), queue.raw.as_ref().unwrap()) - { - Ok(raw) => raw, - Err(_) => break 'error DeviceError::OutOfMemory, - }; - let command_buffer = command::CommandBuffer::new( - encoder, - &device, - #[cfg(feature = "trace")] - device.trace.lock().is_some(), - desc.label.to_hal(device.instance_flags).map(str::to_owned), - ); let (id, _) = fid.assign(Arc::new(command_buffer)); api_log!("Device::create_command_encoder -> {id:?}"); diff --git a/wgpu-core/src/device/resource.rs b/wgpu-core/src/device/resource.rs index 83fe695d04..2ab9063a9b 100644 --- a/wgpu-core/src/device/resource.rs +++ b/wgpu-core/src/device/resource.rs @@ -1596,6 +1596,27 @@ impl Device { }) } + pub(crate) fn create_command_encoder( + self: &Arc, + label: &crate::Label, + ) -> Result, DeviceError> { + self.check_is_valid()?; + + let queue = self.get_queue().unwrap(); + + let encoder = self + .command_allocator + .acquire_encoder(self.raw(), queue.raw.as_ref().unwrap())?; + + Ok(command::CommandBuffer::new( + encoder, + self, + #[cfg(feature = "trace")] + self.trace.lock().is_some(), + label.to_hal(self.instance_flags).map(str::to_owned), + )) + } + /// Generate information about late-validated buffer bindings for pipelines. //TODO: should this be combined with `get_introspection_bind_group_layouts` in some way? pub(crate) fn make_late_sized_buffer_groups(