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

ValidationError-ify memory and memory allocators #2237

Merged
merged 8 commits into from
Jun 27, 2023
30 changes: 9 additions & 21 deletions vulkano/src/buffer/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{
device::{Device, DeviceOwned},
memory::{
allocator::{
align_up, AllocationCreateInfo, AllocationCreationError, DeviceLayout, MemoryAllocator,
align_up, AllocationCreateInfo, DeviceLayout, MemoryAllocator, MemoryAllocatorError,
MemoryUsage, StandardMemoryAllocator,
},
DeviceAlignment,
Expand Down Expand Up @@ -188,7 +188,7 @@ where
/// If `size` is greater than the current arena size, then a new arena will be allocated with
/// the new size, and all subsequently allocated arenas will also share the new size. Otherwise
/// this has no effect.
pub fn reserve(&self, size: DeviceSize) -> Result<(), AllocationCreationError> {
pub fn reserve(&self, size: DeviceSize) -> Result<(), MemoryAllocatorError> {
if size > self.arena_size() {
let state = unsafe { &mut *self.state.get() };
state.arena_size = size;
Expand All @@ -200,7 +200,7 @@ where
}

/// Allocates a subbuffer for sized data.
pub fn allocate_sized<T>(&self) -> Result<Subbuffer<T>, AllocationCreationError>
pub fn allocate_sized<T>(&self) -> Result<Subbuffer<T>, MemoryAllocatorError>
where
T: BufferContents,
{
Expand All @@ -216,10 +216,7 @@ where
/// # Panics
///
/// - Panics if `len` is zero.
pub fn allocate_slice<T>(
&self,
len: DeviceSize,
) -> Result<Subbuffer<[T]>, AllocationCreationError>
pub fn allocate_slice<T>(&self, len: DeviceSize) -> Result<Subbuffer<[T]>, MemoryAllocatorError>
where
T: BufferContents,
{
Expand All @@ -231,10 +228,7 @@ where
/// # Panics
///
/// - Panics if `len` is zero.
pub fn allocate_unsized<T>(
&self,
len: DeviceSize,
) -> Result<Subbuffer<T>, AllocationCreationError>
pub fn allocate_unsized<T>(&self, len: DeviceSize) -> Result<Subbuffer<T>, MemoryAllocatorError>
where
T: BufferContents + ?Sized,
{
Expand All @@ -251,10 +245,7 @@ where
/// # Panics
///
/// - Panics if `layout.alignment()` exceeds `64`.
pub fn allocate(
&self,
layout: DeviceLayout,
) -> Result<Subbuffer<[u8]>, AllocationCreationError> {
pub fn allocate(&self, layout: DeviceLayout) -> Result<Subbuffer<[u8]>, MemoryAllocatorError> {
assert!(layout.alignment().as_devicesize() <= 64);

unsafe { &mut *self.state.get() }.allocate(layout)
Expand Down Expand Up @@ -291,10 +282,7 @@ impl<A> SubbufferAllocatorState<A>
where
A: MemoryAllocator,
{
fn allocate(
&mut self,
layout: DeviceLayout,
) -> Result<Subbuffer<[u8]>, AllocationCreationError> {
fn allocate(&mut self, layout: DeviceLayout) -> Result<Subbuffer<[u8]>, MemoryAllocatorError> {
let size = layout.size();
let alignment = cmp::max(layout.alignment(), self.buffer_alignment);

Expand Down Expand Up @@ -335,7 +323,7 @@ where
}
}

fn next_arena(&mut self) -> Result<Arc<Arena>, AllocationCreationError> {
fn next_arena(&mut self) -> Result<Arc<Arena>, MemoryAllocatorError> {
if self.reserve.is_none() {
self.reserve = Some(Arc::new(ArrayQueue::new(MAX_ARENAS)));
}
Expand All @@ -353,7 +341,7 @@ where
})
}

fn create_arena(&self) -> Result<Arc<Buffer>, AllocationCreationError> {
fn create_arena(&self) -> Result<Arc<Buffer>, MemoryAllocatorError> {
Buffer::new(
&self.memory_allocator,
BufferCreateInfo {
Expand Down
10 changes: 5 additions & 5 deletions vulkano/src/buffer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ use crate::{
macros::{vulkan_bitflags, vulkan_enum},
memory::{
allocator::{
AllocationCreateInfo, AllocationCreationError, AllocationType, DeviceLayout,
MemoryAlloc, MemoryAllocator,
AllocationCreateInfo, AllocationType, DeviceLayout, MemoryAlloc, MemoryAllocator,
MemoryAllocatorError,
},
is_aligned, DedicatedAllocation, DeviceAlignment, ExternalMemoryHandleType,
ExternalMemoryHandleTypes, ExternalMemoryProperties, MemoryRequirements,
Expand Down Expand Up @@ -782,7 +782,7 @@ pub enum BufferError {
RuntimeError(RuntimeError),

/// Allocating memory failed.
AllocError(AllocationCreationError),
AllocError(MemoryAllocatorError),

RequirementNotMet {
required_for: &'static str,
Expand Down Expand Up @@ -997,8 +997,8 @@ impl From<RuntimeError> for BufferError {
}
}

impl From<AllocationCreationError> for BufferError {
fn from(err: AllocationCreationError) -> Self {
impl From<MemoryAllocatorError> for BufferError {
fn from(err: MemoryAllocatorError) -> Self {
Self::AllocError(err)
}
}
Expand Down
7 changes: 3 additions & 4 deletions vulkano/src/image/attachment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ use crate::{
AllocationCreateInfo, AllocationType, MemoryAllocatePreference, MemoryAllocator,
MemoryUsage,
},
is_aligned, DedicatedAllocation, DeviceMemoryError, ExternalMemoryHandleType,
ExternalMemoryHandleTypes,
is_aligned, DedicatedAllocation, ExternalMemoryHandleType, ExternalMemoryHandleTypes,
},
DeviceSize,
DeviceSize, VulkanError,
};
use std::{
fs::File,
Expand Down Expand Up @@ -563,7 +562,7 @@ impl AttachmentImage {
/// Exports posix file descriptor for the allocated memory.
/// Requires `khr_external_memory_fd` and `khr_external_memory` extensions to be loaded.
#[inline]
pub fn export_posix_fd(&self) -> Result<File, DeviceMemoryError> {
pub fn export_posix_fd(&self) -> Result<File, VulkanError> {
let allocation = match self.inner.memory() {
ImageMemory::Normal(a) => &a[0],
_ => unreachable!(),
Expand Down
10 changes: 5 additions & 5 deletions vulkano/src/image/immutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ use crate::{
image::sys::ImageCreateInfo,
memory::{
allocator::{
AllocationCreateInfo, AllocationCreationError, AllocationType,
MemoryAllocatePreference, MemoryAllocator, MemoryUsage,
AllocationCreateInfo, AllocationType, MemoryAllocatePreference, MemoryAllocator,
MemoryAllocatorError, MemoryUsage,
},
is_aligned, DedicatedAllocation,
},
Expand Down Expand Up @@ -402,7 +402,7 @@ impl Hash for ImmutableImageInitialization {
#[derive(Clone, Debug)]
pub enum ImmutableImageCreationError {
ImageCreationError(ImageError),
AllocError(AllocationCreationError),
AllocError(MemoryAllocatorError),
CommandBufferBeginError(CommandBufferBeginError),

/// The size of the provided source data is less than the required size for an image with the
Expand Down Expand Up @@ -449,8 +449,8 @@ impl From<ImageError> for ImmutableImageCreationError {
}
}

impl From<AllocationCreationError> for ImmutableImageCreationError {
fn from(err: AllocationCreationError) -> Self {
impl From<MemoryAllocatorError> for ImmutableImageCreationError {
fn from(err: MemoryAllocatorError) -> Self {
Self::AllocError(err)
}
}
Expand Down
7 changes: 3 additions & 4 deletions vulkano/src/image/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@ use crate::{
AllocationCreateInfo, AllocationType, MemoryAllocatePreference, MemoryAllocator,
MemoryUsage,
},
is_aligned, DedicatedAllocation, DeviceMemoryError, ExternalMemoryHandleType,
ExternalMemoryHandleTypes,
is_aligned, DedicatedAllocation, ExternalMemoryHandleType, ExternalMemoryHandleTypes,
},
sync::Sharing,
DeviceSize,
DeviceSize, VulkanError,
};
use smallvec::SmallVec;

Expand Down Expand Up @@ -422,7 +421,7 @@ impl StorageImage {
/// Exports posix file descriptor for the allocated memory.
/// Requires `khr_external_memory_fd` and `khr_external_memory` extensions to be loaded.
#[inline]
pub fn export_posix_fd(&self) -> Result<File, DeviceMemoryError> {
pub fn export_posix_fd(&self) -> Result<File, VulkanError> {
let allocation = match self.inner.memory() {
ImageMemory::Normal(a) => &a[0],
_ => unreachable!(),
Expand Down
8 changes: 4 additions & 4 deletions vulkano/src/image/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use crate::{
},
macros::impl_id_counter,
memory::{
allocator::{AllocationCreationError, AllocationType, DeviceLayout, MemoryAlloc},
allocator::{AllocationType, DeviceLayout, MemoryAlloc, MemoryAllocatorError},
is_aligned, DedicatedTo, DeviceAlignment, ExternalMemoryHandleType,
ExternalMemoryHandleTypes, MemoryPropertyFlags, MemoryRequirements,
},
Expand Down Expand Up @@ -2790,7 +2790,7 @@ pub enum ImageError {
RuntimeError(RuntimeError),

/// Allocating memory failed.
AllocError(AllocationCreationError),
AllocError(MemoryAllocatorError),

RequirementNotMet {
required_for: &'static str,
Expand Down Expand Up @@ -3264,8 +3264,8 @@ impl From<RuntimeError> for ImageError {
}
}

impl From<AllocationCreationError> for ImageError {
fn from(err: AllocationCreationError) -> Self {
impl From<MemoryAllocatorError> for ImageError {
fn from(err: MemoryAllocatorError) -> Self {
Self::AllocError(err)
}
}
Expand Down
Loading