#heap-allocator #d3d12 #allocator

d3d12-descriptor-heap

Descriptor heap allocator for Direct3D 12

5 releases

0.2.1 Oct 3, 2025
0.2.0 Sep 28, 2024
0.1.2 Aug 28, 2024
0.1.1 Aug 28, 2024
0.1.0 Aug 28, 2024

#130 in Graphics APIs

Download history 160/week @ 2025-12-07 153/week @ 2025-12-14 147/week @ 2025-12-21 147/week @ 2025-12-28 264/week @ 2026-01-04 232/week @ 2026-01-11 461/week @ 2026-01-18 884/week @ 2026-01-25 730/week @ 2026-02-01 464/week @ 2026-02-08 455/week @ 2026-02-15 750/week @ 2026-02-22 1155/week @ 2026-03-01 939/week @ 2026-03-08 1044/week @ 2026-03-15 464/week @ 2026-03-22

3,748 downloads per month
Used in 5 crates (2 directly)

MIT/Apache

20KB
295 lines

d3d12-descriptor-heap

A simple to use descriptor heap for Direct3D 12, using the windows crate.

Usage

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>.

Dependencies

~1–30MB
~500K SLoC