diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
commit | 2aa4a82499d4becd2284cdb482213d541b8804dd (patch) | |
tree | b80bf8bf13c3766139fbacc530efd0dd9d54394c /third_party/rust/gfx-backend-dx12/src/pool.rs | |
parent | Initial commit. (diff) | |
download | firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.tar.xz firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.zip |
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/gfx-backend-dx12/src/pool.rs')
-rw-r--r-- | third_party/rust/gfx-backend-dx12/src/pool.rs | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/third_party/rust/gfx-backend-dx12/src/pool.rs b/third_party/rust/gfx-backend-dx12/src/pool.rs new file mode 100644 index 0000000000..2299dd18da --- /dev/null +++ b/third_party/rust/gfx-backend-dx12/src/pool.rs @@ -0,0 +1,126 @@ +use std::{fmt, sync::Arc}; + +use parking_lot::Mutex; +use winapi::shared::winerror; + +use crate::{command::CommandBuffer, Backend, Shared}; +use hal::{command, pool}; + +pub struct PoolShared { + device: native::Device, + list_type: native::CmdListType, + allocators: Mutex<Vec<native::CommandAllocator>>, + lists: Mutex<Vec<native::GraphicsCommandList>>, +} + +impl fmt::Debug for PoolShared { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + // TODO: print out as struct + fmt.write_str("PoolShared") + } +} + +impl PoolShared { + pub fn acquire(&self) -> (native::CommandAllocator, native::GraphicsCommandList) { + let allocator = match self.allocators.lock().pop() { + Some(allocator) => allocator, + None => { + let (allocator, hr) = self.device.create_command_allocator(self.list_type); + assert_eq!( + winerror::S_OK, + hr, + "error on command allocator creation: {:x}", + hr + ); + allocator + } + }; + let list = match self.lists.lock().pop() { + Some(list) => { + list.reset(allocator, native::PipelineState::null()); + list + } + None => { + let (command_list, hr) = self.device.create_graphics_command_list( + self.list_type, + allocator, + native::PipelineState::null(), + 0, + ); + assert_eq!( + hr, + winerror::S_OK, + "error on command list creation: {:x}", + hr + ); + command_list + } + }; + (allocator, list) + } + + pub fn release_allocator(&self, allocator: native::CommandAllocator) { + self.allocators.lock().push(allocator); + } + + pub fn release_list(&self, list: native::GraphicsCommandList) { + //pre-condition: list must be closed + self.lists.lock().push(list); + } +} + +#[derive(Debug)] +pub struct CommandPool { + shared: Arc<Shared>, + pool_shared: Arc<PoolShared>, +} + +unsafe impl Send for CommandPool {} +unsafe impl Sync for CommandPool {} + +impl CommandPool { + pub(crate) fn new( + device: native::Device, + list_type: native::CmdListType, + shared: &Arc<Shared>, + _create_flags: pool::CommandPoolCreateFlags, + ) -> Self { + let pool_shared = Arc::new(PoolShared { + device, + list_type, + allocators: Mutex::default(), + lists: Mutex::default(), + }); + CommandPool { + shared: Arc::clone(shared), + pool_shared, + } + } +} + +impl pool::CommandPool<Backend> for CommandPool { + unsafe fn reset(&mut self, _release_resources: bool) { + //do nothing. The allocated command buffers would not know + // that this happened, but they should be ready to + // process `begin` as if they are in `Initial` state. + } + + unsafe fn allocate_one(&mut self, level: command::Level) -> CommandBuffer { + // TODO: Implement secondary buffers + assert_eq!(level, command::Level::Primary); + CommandBuffer::new(&self.shared, &self.pool_shared) + } + + unsafe fn free<I>(&mut self, cbufs: I) + where + I: IntoIterator<Item = CommandBuffer>, + { + let mut allocators = self.pool_shared.allocators.lock(); + let mut lists = self.pool_shared.lists.lock(); + for cbuf in cbufs { + let (allocator, list) = cbuf.destroy(); + allocators.extend(allocator); + lists.extend(list); + } + } +} |