Struct gpu_alloc::GpuAllocator

source ·
pub struct GpuAllocator<M> { /* private fields */ }
Expand description

Memory allocator for Vulkan-like APIs.

Implementations§

source§

impl<M> GpuAllocator<M>
where M: MemoryBounds + 'static,

source

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.

source

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
  • device must be one with DeviceProperties that were provided to create this GpuAllocator instance.
  • Same device instance must be used for all interactions with one GpuAllocator instance and memory blocks allocated from it.
source

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
  • device must be one with DeviceProperties that were provided to create this GpuAllocator instance.
  • Same device instance must be used for all interactions with one GpuAllocator instance and memory blocks allocated from it.
source

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 memory must be allocated with the same device that was provided to create this GpuAllocator instance.
  • The memory must be valid.
  • The props, offset and size must 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 MemoryBlock with GpuAllocator::dealloc.
source

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 GpuAllocator instance
  • device must be one with DeviceProperties that were provided to create this GpuAllocator instance
  • Same device instance must be used for all interactions with one GpuAllocator instance and memory blocks allocated from it
source

pub fn max_allocation_size(&self) -> u64

Returns the maximum allocation size supported.

source

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

source

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.

source

pub unsafe fn cleanup(&mut self, device: &impl MemoryDevice<M>)

Deallocates leftover memory objects. Should be used before dropping.

§Safety
  • device must be one with DeviceProperties that were provided to create this GpuAllocator instance
  • Same device instance must be used for all interactions with one GpuAllocator instance and memory blocks allocated from it

Trait Implementations§

source§

impl<M: Debug> Debug for GpuAllocator<M>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<M> Freeze for GpuAllocator<M>

§

impl<M> RefUnwindSafe for GpuAllocator<M>
where M: RefUnwindSafe,

§

impl<M> Send for GpuAllocator<M>
where M: Send,

§

impl<M> Sync for GpuAllocator<M>
where M: Sync,

§

impl<M> Unpin for GpuAllocator<M>

§

impl<M> UnwindSafe for GpuAllocator<M>
where M: RefUnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.