diff --git a/librashader-runtime-d3d12/src/d3d12_primitives.rs b/librashader-runtime-d3d12/src/d3d12_primitives.rs index 0fee970..789a6bc 100644 --- a/librashader-runtime-d3d12/src/d3d12_primitives.rs +++ b/librashader-runtime-d3d12/src/d3d12_primitives.rs @@ -1,7 +1,12 @@ -use std::ptr::NonNull; +use std::sync::Arc; use windows::Win32::Graphics::Direct3D12::{D3D12_CPU_DESCRIPTOR_HANDLE, D3D12_DESCRIPTOR_HEAP_DESC, D3D12_GPU_DESCRIPTOR_HANDLE, ID3D12DescriptorHeap, ID3D12Device}; use crate::error; +pub struct D3D12DescriptorHeapSlot { + cpu_handle: D3D12_CPU_DESCRIPTOR_HANDLE, + heap: Arc +} + pub struct D3D12DescriptorHeap { heap: ID3D12DescriptorHeap, desc: D3D12_DESCRIPTOR_HEAP_DESC, @@ -13,7 +18,7 @@ pub struct D3D12DescriptorHeap { } impl D3D12DescriptorHeap { - pub fn new(device: &ID3D12Device, desc: D3D12_DESCRIPTOR_HEAP_DESC) -> error::Result { + pub fn new(device: &ID3D12Device, desc: D3D12_DESCRIPTOR_HEAP_DESC) -> error::Result> { unsafe { let heap: ID3D12DescriptorHeap = device.CreateDescriptorHeap(&desc)?; let cpu_handle = heap.GetCPUDescriptorHandleForHeapStart(); @@ -22,7 +27,7 @@ impl D3D12DescriptorHeap { let mut map = Vec::new(); map.resize(desc.NumDescriptors as usize, false); - Ok(D3D12DescriptorHeap { + Ok(Arc::new(D3D12DescriptorHeap { heap, desc, cpu_handle, @@ -30,11 +35,29 @@ impl D3D12DescriptorHeap { alignment, map: Box::new([]), start: 0, - }) + })) } } - pub fn allocate_slot(&mut self) -> error::Result { + pub fn allocate_slot(self: &Arc) -> error::Result { + let mut handle = D3D12_CPU_DESCRIPTOR_HANDLE { ptr: 0 }; + + for i in self.start..self.desc.NumDescriptors as usize { + if !self.map[i] { + self.map[i] = true; + handle.ptr = self.cpu_handle.ptr + (i * self.alignment) as u64; + self.start = i + 1; + return Ok(D3D12DescriptorHeapSlot { + cpu_handle: handle, + heap: Arc::clone(self), + }); + } + } + + todo!("error need to fail"); + } + + pub fn free_slot(&mut self) -> error::Result { let mut handle = D3D12_CPU_DESCRIPTOR_HANDLE { ptr: 0 }; for i in self.start..self.desc.NumDescriptors as usize { @@ -49,4 +72,6 @@ impl D3D12DescriptorHeap { todo!("error need to fail"); } -} \ No newline at end of file +} + +impl Drop \ No newline at end of file diff --git a/librashader-runtime-d3d12/src/samplers.rs b/librashader-runtime-d3d12/src/samplers.rs index 6d387af..7074394 100644 --- a/librashader-runtime-d3d12/src/samplers.rs +++ b/librashader-runtime-d3d12/src/samplers.rs @@ -4,5 +4,4 @@ use librashader_common::{FilterMode, WrapMode}; pub struct SamplerSet { samplers: FxHashMap<(WrapMode, FilterMode), D3D12_GPU_DESCRIPTOR_HANDLE>, - heap: D3D12Descriptor_heap }