pub struct GpuAllocator<M> { /* private fields */ }Expand description
Memory allocator for Vulkan-like APIs.
Implementations§
Source§impl<M> GpuAllocator<M>where
M: MemoryBounds + 'static,
impl<M> GpuAllocator<M>where
M: MemoryBounds + 'static,
Sourcepub fn new(config: Config, props: DeviceProperties<'_>) -> Self
pub fn new(config: Config, props: DeviceProperties<'_>) -> Self
Creates new instance of GpuAllocator.
Provided DeviceProperties should match properties of MemoryDevice that will be used
with created GpuAllocator instance.
Sourcepub unsafe fn alloc(
&mut self,
device: &impl MemoryDevice<M>,
request: Request,
) -> Result<MemoryBlock<M>, AllocationError>
pub unsafe fn alloc( &mut self, device: &impl MemoryDevice<M>, request: Request, ) -> Result<MemoryBlock<M>, AllocationError>
Allocates memory block from specified device according to the request.
§Safety
devicemust be one withDevicePropertiesthat were provided to create thisGpuAllocatorinstance.- Same
deviceinstance must be used for all interactions with oneGpuAllocatorinstance and memory blocks allocated from it.
Sourcepub unsafe fn alloc_with_dedicated(
&mut self,
device: &impl MemoryDevice<M>,
request: Request,
dedicated: Dedicated,
) -> Result<MemoryBlock<M>, AllocationError>
pub unsafe fn alloc_with_dedicated( &mut self, device: &impl MemoryDevice<M>, request: Request, dedicated: Dedicated, ) -> Result<MemoryBlock<M>, AllocationError>
Allocates memory block from specified device according to the request.
This function allows user to force specific allocation strategy.
Improper use can lead to suboptimal performance or too large overhead.
Prefer GpuAllocator::alloc if doubt.
§Safety
devicemust be one withDevicePropertiesthat were provided to create thisGpuAllocatorinstance.- Same
deviceinstance must be used for all interactions with oneGpuAllocatorinstance and memory blocks allocated from it.
Sourcepub unsafe fn import_memory(
&mut self,
memory: M,
memory_type: u32,
props: MemoryPropertyFlags,
offset: u64,
size: u64,
) -> MemoryBlock<M>
pub unsafe fn import_memory( &mut self, memory: M, memory_type: u32, props: MemoryPropertyFlags, offset: u64, size: u64, ) -> MemoryBlock<M>
Creates a memory block from an existing memory allocation, transferring ownership to the allocator.
This function allows the GpuAllocator to manage memory allocated outside of the typical
GpuAllocator::alloc family of functions.
§Usage
If you need to import external memory, such as a Win32 HANDLE or a Linux dmabuf, import the device
memory using the graphics api and platform dependent functions. Once that is done, call this function
to make the GpuAllocator take ownership of the imported memory.
When calling this function, you must ensure there are enough remaining allocations.
§Safety
- The
memorymust be allocated with the same device that was provided to create thisGpuAllocatorinstance. - The
memorymust be valid. - The
props,offsetandsizemust match the properties, offset and size of the memory allocation. - The memory must have been allocated with the specified
memory_type. - There must be enough remaining allocations.
- The memory allocation must not come from an existing memory block created by this allocator.
- The underlying memory object must be deallocated using the returned
MemoryBlockwithGpuAllocator::dealloc.
Sourcepub unsafe fn dealloc(
&mut self,
device: &impl MemoryDevice<M>,
block: MemoryBlock<M>,
)
pub unsafe fn dealloc( &mut self, device: &impl MemoryDevice<M>, block: MemoryBlock<M>, )
Deallocates memory block previously allocated from this GpuAllocator instance.
§Safety
- Memory block must have been allocated by this
GpuAllocatorinstance devicemust be one withDevicePropertiesthat were provided to create thisGpuAllocatorinstance- Same
deviceinstance must be used for all interactions with oneGpuAllocatorinstance and memory blocks allocated from it
Sourcepub fn max_allocation_size(&self) -> u64
pub fn max_allocation_size(&self) -> u64
Returns the maximum allocation size supported.
Sourcepub fn remaining_allocations(&self) -> u32
pub fn remaining_allocations(&self) -> u32
Returns the number of remaining available allocations.
This may be useful if you need know if the allocator can allocate a number of allocations ahead of time. This function is also useful for ensuring you do not allocate too much memory outside allocator (such as external memory).
Sourcepub unsafe fn set_remaining_allocations(&mut self, remaining: u32)
pub unsafe fn set_remaining_allocations(&mut self, remaining: u32)
Sets the number of remaining available allocations.
§Safety
The caller is responsible for ensuring the number of remaining allocations does not exceed how many remaining allocations there actually are on the memory device.
Sourcepub unsafe fn cleanup(&mut self, device: &impl MemoryDevice<M>)
pub unsafe fn cleanup(&mut self, device: &impl MemoryDevice<M>)
Deallocates leftover memory objects. Should be used before dropping.
§Safety
devicemust be one withDevicePropertiesthat were provided to create thisGpuAllocatorinstance- Same
deviceinstance must be used for all interactions with oneGpuAllocatorinstance and memory blocks allocated from it