From d89376fd8ac048fb60861633d81595811f7b05ec Mon Sep 17 00:00:00 2001 From: marc0246 <40955683+marc0246@users.noreply.github.com> Date: Sun, 9 Jul 2023 22:13:03 +0200 Subject: [PATCH 1/2] Use the validated `bind_memory` functions in `Buffer` and `Image` --- vulkano/src/buffer/mod.rs | 16 +++++++--------- vulkano/src/image/mod.rs | 14 +++++--------- vulkano/src/lib.rs | 16 ++++++++++++++++ 3 files changed, 28 insertions(+), 18 deletions(-) diff --git a/vulkano/src/buffer/mod.rs b/vulkano/src/buffer/mod.rs index 8fca0554a0..5f9601575a 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, @@ -439,19 +439,17 @@ 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 { From 49b993f11a7eaddd43f60ee29e0f8abc3912676e Mon Sep 17 00:00:00 2001 From: marc0246 <40955683+marc0246@users.noreply.github.com> Date: Mon, 10 Jul 2023 11:36:30 +0200 Subject: [PATCH 2/2] Remove allocation shrinking --- vulkano/src/buffer/mod.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/vulkano/src/buffer/mod.rs b/vulkano/src/buffer/mod.rs index 5f9601575a..32a1c7b40f 100644 --- a/vulkano/src/buffer/mod.rs +++ b/vulkano/src/buffer/mod.rs @@ -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, @@ -440,10 +440,6 @@ impl Buffer { .map_err(BufferAllocateError::AllocateMemory)? }; - // 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()); - let buffer = raw_buffer.bind_memory(allocation).map_err(|(err, _, _)| { err.map(BufferAllocateError::BindMemory) .map_validation(|err| err.add_context("RawBuffer::bind_memory"))