use bit_set::BitSet; use parking_lot::Mutex; use range_alloc::RangeAllocator; use windows::Win32::Graphics::Direct3D12;
usecrate::auxil::dxgi::result::HResult as _;
const HEAP_SIZE_FIXED: usize = 64;
#[derive(Copy, Clone)] pub(super) struct DualHandle {
cpu: Direct3D12::D3D12_CPU_DESCRIPTOR_HANDLE, pub gpu: Direct3D12::D3D12_GPU_DESCRIPTOR_HANDLE, /// How large the block allocated to this handle is.
count: u64,
}
pub(super) fn allocate_slice(&self, count: u64) -> Result<DescriptorIndex, crate::DeviceError> { let range = self.ranges.lock().allocate_range(count).map_err(|err| {
log::error!("Unable to allocate descriptors: {:?}", err); crate::DeviceError::OutOfMemory
})?;
Ok(range.start)
}
/// Free handles previously given out by this `DescriptorHeapSlice`. /// Do not use this with handles not given out by this `DescriptorHeapSlice`. pub(crate) fn free_slice(&self, handle: DualHandle) { let start = (handle.gpu.ptr - self.start.gpu.ptr) / self.handle_size; self.ranges.lock().free_range(start..start + handle.count);
}
}
/// Fixed-size free-list allocator for CPU descriptors. struct FixedSizeHeap {
_raw: Direct3D12::ID3D12DescriptorHeap, /// Bit flag representation of available handles in the heap. /// /// 0 - Occupied /// 1 - free
availability: u64,
handle_size: usize,
start: Direct3D12::D3D12_CPU_DESCRIPTOR_HANDLE,
}
fn alloc_handle(
&mutself,
) -> Result<Direct3D12::D3D12_CPU_DESCRIPTOR_HANDLE, crate::DeviceError> { // Find first free slot. let slot = self.availability.trailing_zeros() as usize; if slot >= HEAP_SIZE_FIXED {
log::error!("Failed to allocate a handle form a fixed size heap"); return Err(crate::DeviceError::OutOfMemory);
} // Set the slot as occupied. self.availability ^= 1 << slot;
// Allocate a new heap if heap_index == self.heaps.len() { self.heaps.push(FixedSizeHeap::new(&self.device, self.ty)?); self.available_heap_indices.insert(heap_index);
}
let heap = &mutself.heaps[heap_index]; let handle = Handle {
raw: heap.alloc_handle()?,
heap_index,
}; if heap.is_full() { self.available_heap_indices.remove(heap_index);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.