Crates.io | d3d12-descriptor-heap |
lib.rs | d3d12-descriptor-heap |
version | 0.2.0 |
source | src |
created_at | 2024-08-28 05:09:17.655707 |
updated_at | 2024-09-28 05:44:27.448406 |
description | Descriptor heap allocator for Direct3D 12 |
homepage | |
repository | https://github.com/SnowflakePowered/d3d12-descriptor-heap-rs |
max_upload_size | |
id | 1354142 |
size | 31,837 |
A simple to use descriptor heap for Direct3D 12, using the windows
crate.
Declare ZST structs for each heap type, and implement D3D12DescriptorHeapType
for it.
#[derive(Clone)]
pub struct CpuStagingHeap;
impl D3D12DescriptorHeapType for CpuStagingHeap {
fn create_desc(size: usize) -> D3D12_DESCRIPTOR_HEAP_DESC {
D3D12_DESCRIPTOR_HEAP_DESC {
Type: D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV,
NumDescriptors: size as u32,
Flags: D3D12_DESCRIPTOR_HEAP_FLAG_NONE,
NodeMask: 0,
}
}
}
Then create the heap and hand out ref-counted handles to slots in the heap. The slot will be freed on drop.
fn create_heap(device: &ID3D12Device) -> Result<(), D3D12DescriptorHeapError> {
let heap = D3D12DescriptorHeap::<CpuStagingHeap>::new(device, 10);
let slot = heap.allocate_descriptor()?;
}
Heap slots implement AsRef<D3D12_CPU_DESCRIPTOR_HANDLE>
so they can be passed directly into Direct3D 12 APIs that
take D3D12_CPU_DESCRIPTOR_HANDLE
. For shader-visible heaps, implement D3D12ShaderVisibleDescriptorHeapType
for the heap type.
#[derive(Clone)]
pub struct SamplerHeap;
impl D3D12DescriptorHeapType for SamplerHeap {
fn create_desc(size: usize) -> D3D12_DESCRIPTOR_HEAP_DESC {
D3D12_DESCRIPTOR_HEAP_DESC {
Type: D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER,
NumDescriptors: size as u32,
Flags: D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE,
NodeMask: 0,
}
}
}
unsafe impl D3D12ShaderVisibleDescriptorHeapType for SamplerHeap {}
Heap slots that are allocated for a heap type implementing D3D12ShaderVisibleDescriptorHeapType
also implement
AsRef<D3D12_GPU_DESCRIPTOR_HANDLE>
.