Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the validated bind_memory functions in Buffer and Image #2259

Merged
merged 2 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 9 additions & 15 deletions vulkano/src/buffer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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 {
Expand Down
14 changes: 5 additions & 9 deletions vulkano/src/image/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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))
}
Expand Down
16 changes: 16 additions & 0 deletions vulkano/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,22 @@ pub enum Validated<E> {
}

impl<E> Validated<E> {
/// Maps the inner `Error` value using the provided function, or does nothing if the value is
/// `ValidationError`.
pub fn map<F>(self, f: impl FnOnce(E) -> F) -> Validated<F> {
match self {
Self::Error(err) => Validated::Error(f(err)),
Self::ValidationError(err) => Validated::ValidationError(err),
}
}

fn map_validation(self, f: impl FnOnce(Box<ValidationError>) -> Box<ValidationError>) -> 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 {
Expand Down