diff --git a/wgpu-core/src/command/allocator.rs b/wgpu-core/src/command/allocator.rs index 4a021c9ab0..4d8cb376c4 100644 --- a/wgpu-core/src/command/allocator.rs +++ b/wgpu-core/src/command/allocator.rs @@ -98,26 +98,32 @@ impl CommandAllocator { let mut inner = self.inner.lock(); use std::collections::hash_map::Entry; - if let Entry::Vacant(e) = inner.pools.entry(thread_id) { - tracing::info!("Starting on thread {:?}", thread_id); - let raw = unsafe { - device - .create_command_pool( - self.queue_family, - hal::pool::CommandPoolCreateFlags::RESET_INDIVIDUAL, - ) - .or(Err(DeviceError::OutOfMemory))? - }; - e.insert(CommandPool { - raw, - total: 0, - available: Vec::new(), - pending: Vec::new(), - }); - } + let pool = match inner.pools.entry(thread_id) { + Entry::Vacant(e) => { + tracing::info!("Starting on thread {:?}", thread_id); + let raw = unsafe { + device + .create_command_pool( + self.queue_family, + hal::pool::CommandPoolCreateFlags::RESET_INDIVIDUAL, + ) + .or(Err(DeviceError::OutOfMemory))? + }; + e.insert(CommandPool { + raw, + total: 0, + available: Vec::new(), + pending: Vec::new(), + }) + } + Entry::Occupied(e) => e.into_mut(), + }; + + //Note: we have to allocate the first buffer right here, or otherwise + // the pool may be cleaned up by maintenance called from another thread. Ok(CommandBuffer { - raw: Vec::new(), + raw: vec![pool.allocate()], is_recording: true, recorded_thread_id: thread_id, device_id, diff --git a/wgpu-core/src/device/mod.rs b/wgpu-core/src/device/mod.rs index 107535b30f..769d4d8b11 100644 --- a/wgpu-core/src/device/mod.rs +++ b/wgpu-core/src/device/mod.rs @@ -3606,14 +3606,13 @@ impl Global { Err(e) => break e, }; - let mut raw = device.cmd_allocator.extend(&command_buffer); + let mut raw = command_buffer.raw.first_mut().unwrap(); unsafe { if let Some(ref label) = desc.label { device.raw.set_command_buffer_name(&mut raw, label); } raw.begin_primary(hal::command::CommandBufferFlags::ONE_TIME_SUBMIT); } - command_buffer.raw.push(raw); let id = hub .command_buffers