Struct vulkano::memory::MappedDeviceMemory
source · pub struct MappedDeviceMemory { /* private fields */ }
Expand description
Represents memory that has been allocated and mapped in CPU accessible space.
Can be obtained with DeviceMemory::alloc_and_map
. The function will panic if the memory type
is not host-accessible.
In order to access the content of the allocated memory, you can use the read_write
method.
This method returns a guard object that derefs to the content.
Example
use vulkano::memory::DeviceMemory;
// The memory type must be mappable.
let mem_ty = device.physical_device().memory_types()
.filter(|t| t.is_host_visible())
.next().unwrap(); // Vk specs guarantee that this can't fail
// Allocates 1KB of memory.
let memory = DeviceMemory::alloc_and_map(device.clone(), mem_ty, 1024).unwrap();
// Get access to the content. Note that this is very unsafe for two reasons: 1) the content is
// uninitialized, and 2) the access is unsynchronized.
unsafe {
let mut content = memory.read_write::<[u8]>(0 .. 1024);
content[12] = 54; // `content` derefs to a `&[u8]` or a `&mut [u8]`
}
Implementations
sourceimpl MappedDeviceMemory
impl MappedDeviceMemory
sourcepub fn unmap(self) -> DeviceMemory
pub fn unmap(self) -> DeviceMemory
Unmaps the memory. It will no longer be accessible from the CPU.
sourcepub unsafe fn read_write<T: ?Sized>(
&self,
range: Range<usize>
) -> CpuAccess<'_, T>where
T: Content,
pub unsafe fn read_write<T: ?Sized>(
&self,
range: Range<usize>
) -> CpuAccess<'_, T>where
T: Content,
Gives access to the content of the memory.
This function takes care of calling vkInvalidateMappedMemoryRanges
and
vkFlushMappedMemoryRanges
on the given range. You are therefore encouraged to use the
smallest range as possible, and to not call this function multiple times in a row for
several small changes.
Safety
- Type safety is not checked. You must ensure that
T
corresponds to the content of the buffer. - Accesses are not synchronized. Synchronization must be handled outside of
the
MappedDeviceMemory
.
Trait Implementations
sourceimpl AsMut<DeviceMemory> for MappedDeviceMemory
impl AsMut<DeviceMemory> for MappedDeviceMemory
sourcefn as_mut(&mut self) -> &mut DeviceMemory
fn as_mut(&mut self) -> &mut DeviceMemory
Converts this type into a mutable reference of the (usually inferred) input type.
sourceimpl AsRef<DeviceMemory> for MappedDeviceMemory
impl AsRef<DeviceMemory> for MappedDeviceMemory
sourcefn as_ref(&self) -> &DeviceMemory
fn as_ref(&self) -> &DeviceMemory
Converts this type into a shared reference of the (usually inferred) input type.
sourceimpl Debug for MappedDeviceMemory
impl Debug for MappedDeviceMemory
sourceimpl DeviceOwned for MappedDeviceMemory
impl DeviceOwned for MappedDeviceMemory
impl Send for MappedDeviceMemory
impl Sync for MappedDeviceMemory
Auto Trait Implementations
impl RefUnwindSafe for MappedDeviceMemory
impl Unpin for MappedDeviceMemory
impl UnwindSafe for MappedDeviceMemory
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Content for T
impl<T> Content for T
sourcefn ref_from_ptr<'a>(ptr: *mut c_void, size: usize) -> Option<*mut T>
fn ref_from_ptr<'a>(ptr: *mut c_void, size: usize) -> Option<*mut T>
Builds a pointer to this type from a raw pointer.
sourcefn is_size_suitable(size: usize) -> bool
fn is_size_suitable(size: usize) -> bool
Returns true if the size is suitable to store a type like this.
sourcefn indiv_size() -> usize
fn indiv_size() -> usize
Returns the size of an individual element.