gfx_hal/pool.rs
1//! Command pools
2
3use crate::{command::Level, Backend, PseudoVec};
4
5use std::{any::Any, fmt};
6
7bitflags!(
8 /// Command pool creation flags.
9 #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
10 pub struct CommandPoolCreateFlags: u8 {
11 /// Indicates short-lived command buffers.
12 /// Memory optimization hint for implementations.
13 const TRANSIENT = 0x1;
14 /// Allow command buffers to be reset individually.
15 const RESET_INDIVIDUAL = 0x2;
16 }
17);
18
19/// The allocated command buffers are associated with the creating command queue.
20pub trait CommandPool<B: Backend>: fmt::Debug + Any + Send + Sync {
21 /// Reset the command pool and the corresponding command buffers.
22 ///
23 /// # Arguments
24 ///
25 /// * `release_resources` - if `true`, this command pool will recycle all the
26 /// resources it own and give them back to the system.
27 ///
28 /// # Synchronization
29 ///
30 /// You may _not_ free the pool if a command buffer allocated from it
31 /// is still in use (pool memory still in use).
32 unsafe fn reset(&mut self, release_resources: bool);
33
34 /// Allocate a single [command buffer][crate::command::CommandBuffer] from the pool.
35 ///
36 /// # Arguments
37 ///
38 /// * `level` - whether this command buffer is primary or secondary.
39 unsafe fn allocate_one(&mut self, level: Level) -> B::CommandBuffer {
40 let mut result = PseudoVec(None);
41 self.allocate(1, level, &mut result);
42 result.0.unwrap()
43 }
44
45 /// Allocate new [command buffers][crate::command::CommandBuffer] from the pool.
46 ///
47 /// # Arguments
48 ///
49 /// * `num` - how many buffers to return
50 /// * `level` - whether to allocate primary or secondary command buffers.
51 /// * `list` - an extendable list of command buffers into which to allocate.
52 unsafe fn allocate<E>(&mut self, num: usize, level: Level, list: &mut E)
53 where
54 E: Extend<B::CommandBuffer>,
55 {
56 list.extend((0..num).map(|_| self.allocate_one(level)));
57 }
58
59 /// Free [command buffers][crate::command::CommandBuffer] allocated from this pool.
60 unsafe fn free<I>(&mut self, buffers: I)
61 where
62 I: Iterator<Item = B::CommandBuffer>;
63}