d3d12: try to do desc heaps

This commit is contained in:
chyyran 2023-01-21 15:47:27 -05:00 committed by Ronny Chan
parent 92c9836985
commit 4dc34fceb2
2 changed files with 31 additions and 7 deletions

View file

@ -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 windows::Win32::Graphics::Direct3D12::{D3D12_CPU_DESCRIPTOR_HANDLE, D3D12_DESCRIPTOR_HEAP_DESC, D3D12_GPU_DESCRIPTOR_HANDLE, ID3D12DescriptorHeap, ID3D12Device};
use crate::error; use crate::error;
pub struct D3D12DescriptorHeapSlot {
cpu_handle: D3D12_CPU_DESCRIPTOR_HANDLE,
heap: Arc<D3D12DescriptorHeap>
}
pub struct D3D12DescriptorHeap { pub struct D3D12DescriptorHeap {
heap: ID3D12DescriptorHeap, heap: ID3D12DescriptorHeap,
desc: D3D12_DESCRIPTOR_HEAP_DESC, desc: D3D12_DESCRIPTOR_HEAP_DESC,
@ -13,7 +18,7 @@ pub struct D3D12DescriptorHeap {
} }
impl D3D12DescriptorHeap { impl D3D12DescriptorHeap {
pub fn new(device: &ID3D12Device, desc: D3D12_DESCRIPTOR_HEAP_DESC) -> error::Result<D3D12DescriptorHeap> { pub fn new(device: &ID3D12Device, desc: D3D12_DESCRIPTOR_HEAP_DESC) -> error::Result<Arc<D3D12DescriptorHeap>> {
unsafe { unsafe {
let heap: ID3D12DescriptorHeap = device.CreateDescriptorHeap(&desc)?; let heap: ID3D12DescriptorHeap = device.CreateDescriptorHeap(&desc)?;
let cpu_handle = heap.GetCPUDescriptorHandleForHeapStart(); let cpu_handle = heap.GetCPUDescriptorHandleForHeapStart();
@ -22,7 +27,7 @@ impl D3D12DescriptorHeap {
let mut map = Vec::new(); let mut map = Vec::new();
map.resize(desc.NumDescriptors as usize, false); map.resize(desc.NumDescriptors as usize, false);
Ok(D3D12DescriptorHeap { Ok(Arc::new(D3D12DescriptorHeap {
heap, heap,
desc, desc,
cpu_handle, cpu_handle,
@ -30,11 +35,29 @@ impl D3D12DescriptorHeap {
alignment, alignment,
map: Box::new([]), map: Box::new([]),
start: 0, start: 0,
}) }))
} }
} }
pub fn allocate_slot(&mut self) -> error::Result<D3D12_CPU_DESCRIPTOR_HANDLE> { pub fn allocate_slot(self: &Arc<D3D12DescriptorHeap>) -> error::Result<D3D12DescriptorHeapSlot> {
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<D3D12_CPU_DESCRIPTOR_HANDLE> {
let mut handle = D3D12_CPU_DESCRIPTOR_HANDLE { ptr: 0 }; let mut handle = D3D12_CPU_DESCRIPTOR_HANDLE { ptr: 0 };
for i in self.start..self.desc.NumDescriptors as usize { for i in self.start..self.desc.NumDescriptors as usize {
@ -50,3 +73,5 @@ impl D3D12DescriptorHeap {
} }
} }
impl Drop

View file

@ -4,5 +4,4 @@ use librashader_common::{FilterMode, WrapMode};
pub struct SamplerSet { pub struct SamplerSet {
samplers: FxHashMap<(WrapMode, FilterMode), D3D12_GPU_DESCRIPTOR_HANDLE>, samplers: FxHashMap<(WrapMode, FilterMode), D3D12_GPU_DESCRIPTOR_HANDLE>,
heap: D3D12Descriptor_heap
} }