pub struct MemoryAlloc { /* private fields */ }
Expand description

Memory allocations are portions of memory that are reserved for a specific resource or purpose.

There’s a few ways you can obtain a MemoryAlloc in Vulkano. Most commonly you will probably want to use a memory allocator. If you already have a DeviceMemory block on hand that you would like to turn into an allocation, you can use the constructor. Lastly, you can use a suballocator if you want to create multiple smaller allocations out of a bigger one.

Implementations§

source§

impl MemoryAlloc

source

pub fn new(device_memory: DeviceMemory) -> Result<Self, AllocationCreationError>

Creates a new MemoryAlloc.

The memory is mapped automatically if it’s host-visible.

source

pub fn offset(&self) -> DeviceSize

Returns the offset of the allocation within the DeviceMemory block.

source

pub fn size(&self) -> DeviceSize

Returns the size of the allocation.

source

pub fn allocation_type(&self) -> AllocationType

Returns the type of resources that can be bound to this allocation.

source

pub fn mapped_ptr(&self) -> Option<NonNull<c_void>>

Returns the mapped pointer to the start of the allocation if the memory is host-visible, otherwise returns None.

source

pub unsafe fn mapped_slice(&self) -> Option<&[u8]>

Returns a mapped slice to the data within the allocation if the memory is host-visible, otherwise returns None.

Safety
  • While the returned slice exists, there must be no operations pending or executing in a GPU queue that write to the same memory.
source

pub unsafe fn mapped_slice_mut(&mut self) -> Option<&mut [u8]>

Returns a mapped mutable slice to the data within the allocation if the memory is host-visible, otherwise returns None.

Safety
  • While the returned slice exists, there must be no operations pending or executing in a GPU queue that access the same memory.
source

pub unsafe fn invalidate_range( &self, range: Range<DeviceSize> ) -> Result<(), VulkanError>

Invalidates the host (CPU) cache for a range of the allocation.

You must call this method before the memory is read by the host, if the device previously wrote to the memory. It has no effect if the memory is not mapped or if the memory is host-coherent.

range is specified in bytes relative to the start of the allocation. The start and end of range must be a multiple of the non_coherent_atom_size device property, but range.end can also equal to self.size().

Safety
  • If there are memory writes by the GPU that have not been propagated into the CPU cache, then there must not be any references in Rust code to the specified range of the memory.
Panics
  • Panics if range is empty.
  • Panics if range.end exceeds self.size.
  • Panics if range.start or range.end are not a multiple of the non_coherent_atom_size.
source

pub unsafe fn flush_range( &self, range: Range<DeviceSize> ) -> Result<(), VulkanError>

Flushes the host (CPU) cache for a range of the allocation.

You must call this method after writing to the memory from the host, if the device is going to read the memory. It has no effect if the memory is not mapped or if the memory is host-coherent.

range is specified in bytes relative to the start of the allocation. The start and end of range must be a multiple of the non_coherent_atom_size device property, but range.end can also equal to self.size().

Safety
  • There must be no operations pending or executing in a GPU queue that access the specified range of the memory.
Panics
  • Panics if range is empty.
  • Panics if range.end exceeds self.size.
  • Panics if range.start or range.end are not a multiple of the non_coherent_atom_size.
source

pub fn device_memory(&self) -> &DeviceMemory

Returns the underlying block of DeviceMemory.

source

pub fn parent_allocation(&self) -> Option<&Self>

Returns the parent allocation if this allocation is a suballocation, otherwise returns None.

source

pub fn is_root(&self) -> bool

Returns true if this allocation is the root of the memory hierarchy.

source

pub fn is_dedicated(&self) -> bool

Returns true if this allocation is a dedicated allocation.

source

pub fn try_unwrap(self) -> Result<DeviceMemory, Self>

Returns the underlying block of DeviceMemory if this allocation is the root allocation and is not aliased, otherwise returns the allocation back wrapped in Err.

source

pub unsafe fn alias(&self) -> Option<Self>

Duplicates the allocation, creating aliased memory. Returns None if the allocation is a dedicated allocation.

You might consider using this method if you want to optimize memory usage by aliasing render targets for example, in which case you will have to double and triple check that the memory is not used concurrently unless it only involves reading. You are highly discouraged from doing this unless you have a reason to.

Safety
  • You must ensure memory accesses are synchronized yourself.
source

pub fn shift(&mut self, amount: DeviceSize)

Increases the offset of the allocation by the specified amount and shrinks its size by the same amount.

Panics
  • Panics if the amount exceeds the size of the allocation.
source

pub fn shrink(&mut self, new_size: DeviceSize)

Shrinks the size of the allocation to the specified new_size.

Panics
  • Panics if the new_size exceeds the current size of the allocation.
source

pub unsafe fn set_offset(&mut self, new_offset: DeviceSize)

Sets the offset of the allocation without checking for memory aliasing.

See also shift, which moves the offset safely.

Safety
  • You must ensure that the allocation doesn’t alias any other allocations within the DeviceMemory block, and if it does, then you must ensure memory accesses are synchronized yourself.
  • You must ensure the allocation still fits inside the DeviceMemory block.
source

pub unsafe fn set_size(&mut self, new_size: DeviceSize)

Sets the size of the allocation without checking for memory aliasing.

See also shrink, which sets the size safely.

Safety
  • You must ensure that the allocation doesn’t alias any other allocations within the DeviceMemory block, and if it does, then you must ensure memory accesses are synchronized yourself.
  • You must ensure the allocation still fits inside the DeviceMemory block.
source

pub unsafe fn set_allocation_type(&mut self, new_type: AllocationType)

Sets the allocation type.

This might cause memory aliasing due to buffer-image granularity conflicts if the allocation type is Linear or NonLinear and is changed to a different one.

Safety
  • You must ensure that the allocation doesn’t alias any other allocations within the DeviceMemory block, and if it does, then you must ensure memory accesses are synchronized yourself.

Trait Implementations§

source§

impl Debug for MemoryAlloc

source§

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

Formats the value using the given formatter. Read more
source§

impl DeviceOwned for MemoryAlloc

source§

fn device(&self) -> &Arc<Device>

Returns the device that owns Self.
source§

impl Drop for MemoryAlloc

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl Send for MemoryAlloc

source§

impl Sync for MemoryAlloc

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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 Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.