diff --git a/vulkano/src/buffer/mod.rs b/vulkano/src/buffer/mod.rs index 8fca0554a0..32a1c7b40f 100644 --- a/vulkano/src/buffer/mod.rs +++ b/vulkano/src/buffer/mod.rs @@ -111,7 +111,7 @@ use crate::{ AllocationCreateInfo, AllocationType, DeviceLayout, MemoryAlloc, MemoryAllocator, MemoryAllocatorError, }, - is_aligned, DedicatedAllocation, ExternalMemoryHandleType, ExternalMemoryHandleTypes, + DedicatedAllocation, ExternalMemoryHandleType, ExternalMemoryHandleTypes, ExternalMemoryProperties, MemoryRequirements, }, range_map::RangeMap, @@ -429,7 +429,7 @@ impl Buffer { let mut requirements = *raw_buffer.memory_requirements(); requirements.layout = requirements.layout.align_to(layout.alignment()).unwrap(); - let mut allocation = unsafe { + let allocation = unsafe { allocator .allocate_unchecked( requirements, @@ -439,19 +439,13 @@ impl Buffer { ) .map_err(BufferAllocateError::AllocateMemory)? }; - debug_assert!(is_aligned( - allocation.offset(), - requirements.layout.alignment(), - )); - debug_assert!(allocation.size() == requirements.layout.size()); - - // The implementation might require a larger size than we wanted. With this it is easier to - // invalidate and flush the whole buffer. It does not affect the allocation in any way. - allocation.shrink(layout.size()); - - unsafe { raw_buffer.bind_memory_unchecked(allocation) } - .map(Arc::new) - .map_err(|(err, _, _)| BufferAllocateError::BindMemory(err).into()) + + let buffer = raw_buffer.bind_memory(allocation).map_err(|(err, _, _)| { + err.map(BufferAllocateError::BindMemory) + .map_validation(|err| err.add_context("RawBuffer::bind_memory")) + })?; + + Ok(Arc::new(buffer)) } fn from_raw(inner: RawBuffer, memory: BufferMemory) -> Self { diff --git a/vulkano/src/image/mod.rs b/vulkano/src/image/mod.rs index 8027746b7c..d6946b7138 100644 --- a/vulkano/src/image/mod.rs +++ b/vulkano/src/image/mod.rs @@ -69,7 +69,7 @@ use crate::{ macros::{vulkan_bitflags, vulkan_bitflags_enum, vulkan_enum}, memory::{ allocator::{AllocationCreateInfo, MemoryAlloc, MemoryAllocator, MemoryAllocatorError}, - is_aligned, DedicatedAllocation, ExternalMemoryHandleType, ExternalMemoryHandleTypes, + DedicatedAllocation, ExternalMemoryHandleType, ExternalMemoryHandleTypes, ExternalMemoryProperties, MemoryRequirements, }, range_map::RangeMap, @@ -171,14 +171,10 @@ impl Image { .map_err(ImageAllocateError::AllocateMemory)? }; - debug_assert!(is_aligned( - allocation.offset(), - requirements.layout.alignment(), - )); - debug_assert!(allocation.size() == requirements.layout.size()); - - let image = unsafe { raw_image.bind_memory_unchecked([allocation]) } - .map_err(|(err, _, _)| ImageAllocateError::BindMemory(err))?; + let image = raw_image.bind_memory([allocation]).map_err(|(err, _, _)| { + err.map(ImageAllocateError::BindMemory) + .map_validation(|err| err.add_context("RawImage::bind_memory")) + })?; Ok(Arc::new(image)) } diff --git a/vulkano/src/lib.rs b/vulkano/src/lib.rs index 33dcbe4851..7aa5206312 100644 --- a/vulkano/src/lib.rs +++ b/vulkano/src/lib.rs @@ -411,6 +411,22 @@ pub enum Validated { } impl Validated { + /// Maps the inner `Error` value using the provided function, or does nothing if the value is + /// `ValidationError`. + pub fn map(self, f: impl FnOnce(E) -> F) -> Validated { + match self { + Self::Error(err) => Validated::Error(f(err)), + Self::ValidationError(err) => Validated::ValidationError(err), + } + } + + fn map_validation(self, f: impl FnOnce(Box) -> Box) -> Self { + match self { + Self::Error(err) => Self::Error(err), + Self::ValidationError(err) => Self::ValidationError(f(err)), + } + } + /// Returns the inner `Error` value, or panics if it contains `ValidationError`. pub fn unwrap(self) -> E { match self {