From bb622ea4baa68667f06e0660c264c87381aec0b6 Mon Sep 17 00:00:00 2001 From: marc0246 <40955683+marc0246@users.noreply.github.com> Date: Fri, 19 Apr 2024 13:54:50 +0200 Subject: [PATCH] Add `ResourceMemory::from_device_memory_unchecked` (#2519) --- vulkano/src/memory/mod.rs | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/vulkano/src/memory/mod.rs b/vulkano/src/memory/mod.rs index b5564cd1..4c73cab0 100644 --- a/vulkano/src/memory/mod.rs +++ b/vulkano/src/memory/mod.rs @@ -146,11 +146,43 @@ impl ResourceMemory { unsafe { Self::new_dedicated_unchecked(Arc::new(device_memory)) } } - #[cfg_attr(not(feature = "document_unchecked"), doc(hidden))] + /// Same as [`new_dedicated`], except that this allows creating aliasing resources. + /// + /// # Safety + /// + /// - Two resources must not alias each other, and if they do, you must ensure correct + /// synchronization yourself. pub unsafe fn new_dedicated_unchecked(device_memory: Arc) -> Self { + let size = device_memory.allocation_size(); + + unsafe { Self::from_device_memory_unchecked(device_memory, 0, size) } + } + + /// Creates a new `ResourceMemory` from the given portion of the given device memory block. You + /// may use this when you need to portion an existing memory block in a specific way. Note that + /// when you don't have this requirement of placing resources at specific offsets, you should + /// use a memory allocator instead. + /// + /// # Safety + /// + /// - Two resources must not alias each other (as returned by [`Buffer::memory_requirements`] + /// or [`Image::memory_requirements`]), and if they do, you must ensure correct + /// synchronization yourself. + /// + /// # Panics + /// + /// - Panics if `offset + size` is greater than `device_memory.allocation_size()`. + pub unsafe fn from_device_memory_unchecked( + device_memory: Arc, + offset: DeviceSize, + size: DeviceSize, + ) -> Self { + assert!(offset <= device_memory.allocation_size()); + assert!(size <= device_memory.allocation_size() - offset); + ResourceMemory { - offset: 0, - size: device_memory.allocation_size(), + offset, + size, allocation_type: AllocationType::Unknown, allocation_handle: AllocationHandle::null(), suballocation_handle: None,