| 1 |
use alloc::{boxed::Box, vec::Vec}; |
| 2 |
|
| 3 |
use crate::lock::{rank, Mutex}; |
| 4 |
|
| 5 |
/// A pool of free [`wgpu_hal::CommandEncoder`]s, owned by a `Device`. |
| 6 |
/// |
| 7 |
/// Each encoder in this list is in the "closed" state. |
| 8 |
/// |
| 9 |
/// Since a raw [`CommandEncoder`][ce] is itself a pool for allocating |
| 10 |
/// raw [`CommandBuffer`][cb]s, this is a pool of pools. |
| 11 |
/// |
| 12 |
/// [`wgpu_hal::CommandEncoder`]: hal::CommandEncoder |
| 13 |
/// [ce]: hal::CommandEncoder |
| 14 |
/// [cb]: hal::Api::CommandBuffer |
| 15 |
pub(crate) struct CommandAllocator { |
| 16 |
free_encoders: Mutex<Vec<Box<dyn hal::DynCommandEncoder>>>, |
| 17 |
} |
| 18 |
|
| 19 |
impl CommandAllocator { |
| 20 |
pub(crate) fn new() -> Self { |
| 21 |
Self { |
| 22 |
free_encoders: Mutex::new(rank::COMMAND_ALLOCATOR_FREE_ENCODERS, Vec::new()), |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
/// Return a fresh [`wgpu_hal::CommandEncoder`] in the "closed" state. |
| 27 |
/// |
| 28 |
/// If we have free encoders in the pool, take one of those. Otherwise, |
| 29 |
/// create a new one on `device`. |
| 30 |
/// |
| 31 |
/// [`wgpu_hal::CommandEncoder`]: hal::CommandEncoder |
| 32 |
pub(crate) fn acquire_encoder( |
| 33 |
&self, |
| 34 |
device: &dyn hal::DynDevice, |
| 35 |
queue: &dyn hal::DynQueue, |
| 36 |
) -> Result<Box<dyn hal::DynCommandEncoder>, hal::DeviceError> { |
| 37 |
let mut free_encoders = self.free_encoders.lock(); |
| 38 |
match free_encoders.pop() { |
| 39 |
Some(encoder) => Ok(encoder), |
| 40 |
None => unsafe { |
| 41 |
let hal_desc = hal::CommandEncoderDescriptor { label: None, queue }; |
| 42 |
device.create_command_encoder(&hal_desc) |
| 43 |
}, |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
/// Add `encoder` back to the free pool. |
| 48 |
pub(crate) fn release_encoder(&self, encoder: Box<dyn hal::DynCommandEncoder>) { |
| 49 |
let mut free_encoders = self.free_encoders.lock(); |
| 50 |
free_encoders.push(encoder); |
| 51 |
} |
| 52 |
} |