Skip to content

Commit

Permalink
Single type for all validation errors (#2194)
Browse files Browse the repository at this point in the history
* Single type for all validation errors

* Documentation

* Small improvement

* Rename VulkanError to RuntimeError

* Simplify the error type by using an intermediate struct

* Update vulkano/src/lib.rs

Co-authored-by: marc0246 <40955683+marc0246@users.noreply.github.com>

* Update vulkano/src/lib.rs

Co-authored-by: marc0246 <40955683+marc0246@users.noreply.github.com>

* Update vulkano/src/lib.rs

Co-authored-by: marc0246 <40955683+marc0246@users.noreply.github.com>

* Better solution

* Revert to original state

---------

Co-authored-by: marc0246 <40955683+marc0246@users.noreply.github.com>
  • Loading branch information
Rua and marc0246 authored May 5, 2023
1 parent d680019 commit 59e0df3
Show file tree
Hide file tree
Showing 40 changed files with 704 additions and 614 deletions.
6 changes: 3 additions & 3 deletions vulkano/autogen/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ fn errors_output(members: &[ErrorsMember]) -> TokenStream {
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[repr(i32)]
#[non_exhaustive]
pub enum VulkanError {
pub enum RuntimeError {
#(#enum_items)*
Unnamed(ash::vk::Result),
}

impl From<ash::vk::Result> for VulkanError {
fn from(val: ash::vk::Result) -> VulkanError {
impl From<ash::vk::Result> for RuntimeError {
fn from(val: ash::vk::Result) -> RuntimeError {
match val {
#(#try_from_items)*
x => Self::Unnamed(x),
Expand Down
14 changes: 7 additions & 7 deletions vulkano/src/buffer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ use crate::{
},
range_map::RangeMap,
sync::{future::AccessError, CurrentAccess, Sharing},
DeviceSize, NonZeroDeviceSize, RequirementNotMet, RequiresOneOf, Version, VulkanError,
DeviceSize, NonZeroDeviceSize, RequirementNotMet, RequiresOneOf, RuntimeError, Version,
VulkanObject,
};
use parking_lot::{Mutex, MutexGuard};
Expand Down Expand Up @@ -780,7 +780,7 @@ struct BufferRangeState {
/// Error that can happen in buffer functions.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum BufferError {
VulkanError(VulkanError),
RuntimeError(RuntimeError),

/// Allocating memory failed.
AllocError(AllocationCreationError),
Expand Down Expand Up @@ -870,7 +870,7 @@ pub enum BufferError {
impl Error for BufferError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
Self::VulkanError(err) => Some(err),
Self::RuntimeError(err) => Some(err),
Self::AllocError(err) => Some(err),
_ => None,
}
Expand All @@ -880,7 +880,7 @@ impl Error for BufferError {
impl Display for BufferError {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> {
match self {
Self::VulkanError(_) => write!(f, "a runtime error occurred"),
Self::RuntimeError(_) => write!(f, "a runtime error occurred"),
Self::AllocError(_) => write!(f, "allocating memory failed"),
Self::RequirementNotMet {
required_for,
Expand Down Expand Up @@ -992,9 +992,9 @@ impl Display for BufferError {
}
}

impl From<VulkanError> for BufferError {
fn from(err: VulkanError) -> Self {
Self::VulkanError(err)
impl From<RuntimeError> for BufferError {
fn from(err: RuntimeError) -> Self {
Self::RuntimeError(err)
}
}

Expand Down
10 changes: 5 additions & 5 deletions vulkano/src/buffer/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::{
MemoryPropertyFlags, MemoryRequirements,
},
sync::Sharing,
DeviceSize, RequiresOneOf, Version, VulkanError, VulkanObject,
DeviceSize, RequiresOneOf, RuntimeError, Version, VulkanObject,
};
use smallvec::SmallVec;
use std::{mem::MaybeUninit, num::NonZeroU64, ptr, sync::Arc};
Expand Down Expand Up @@ -200,7 +200,7 @@ impl RawBuffer {
pub unsafe fn new_unchecked(
device: Arc<Device>,
create_info: BufferCreateInfo,
) -> Result<Self, VulkanError> {
) -> Result<Self, RuntimeError> {
let &BufferCreateInfo {
flags,
ref sharing,
Expand Down Expand Up @@ -252,7 +252,7 @@ impl RawBuffer {
output.as_mut_ptr(),
)
.result()
.map_err(VulkanError::from)?;
.map_err(RuntimeError::from)?;
output.assume_init()
};

Expand Down Expand Up @@ -521,7 +521,7 @@ impl RawBuffer {
pub unsafe fn bind_memory_unchecked(
self,
allocation: MemoryAlloc,
) -> Result<Buffer, (VulkanError, RawBuffer, MemoryAlloc)> {
) -> Result<Buffer, (RuntimeError, RawBuffer, MemoryAlloc)> {
let memory = allocation.device_memory();
let memory_offset = allocation.offset();

Expand Down Expand Up @@ -561,7 +561,7 @@ impl RawBuffer {
.result();

if let Err(err) = result {
return Err((VulkanError::from(err), self, allocation));
return Err((RuntimeError::from(err), self, allocation));
}

Ok(Buffer::from_raw(self, BufferMemory::Normal(allocation)))
Expand Down
8 changes: 4 additions & 4 deletions vulkano/src/buffer/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ use crate::{
format::{Format, FormatFeatures},
macros::impl_id_counter,
memory::{is_aligned, DeviceAlignment},
DeviceSize, OomError, RequirementNotMet, RequiresOneOf, Version, VulkanError, VulkanObject,
DeviceSize, OomError, RequirementNotMet, RequiresOneOf, RuntimeError, Version, VulkanObject,
};
use std::{
error::Error,
Expand Down Expand Up @@ -235,7 +235,7 @@ impl BufferView {
output.as_mut_ptr(),
)
.result()
.map_err(VulkanError::from)?;
.map_err(RuntimeError::from)?;
output.assume_init()
};

Expand Down Expand Up @@ -411,8 +411,8 @@ impl From<OomError> for BufferViewCreationError {
}
}

impl From<VulkanError> for BufferViewCreationError {
fn from(err: VulkanError) -> Self {
impl From<RuntimeError> for BufferViewCreationError {
fn from(err: RuntimeError) -> Self {
OomError::from(err).into()
}
}
Expand Down
18 changes: 9 additions & 9 deletions vulkano/src/command_buffer/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::{
command_buffer::CommandBufferLevel,
device::{Device, DeviceOwned},
macros::impl_id_counter,
OomError, RequiresOneOf, Version, VulkanError, VulkanObject,
OomError, RequiresOneOf, RuntimeError, Version, VulkanObject,
};
use smallvec::SmallVec;
use std::{
Expand Down Expand Up @@ -169,7 +169,7 @@ impl CommandPool {
output.as_mut_ptr(),
)
.result()
.map_err(VulkanError::from)?;
.map_err(RuntimeError::from)?;
output.assume_init()
};

Expand All @@ -195,7 +195,7 @@ impl CommandPool {
let fns = self.device.fns();
(fns.v1_0.reset_command_pool)(self.device.handle(), self.handle, flags)
.result()
.map_err(VulkanError::from)?;
.map_err(RuntimeError::from)?;

Ok(())
}
Expand Down Expand Up @@ -232,7 +232,7 @@ impl CommandPool {
out.as_mut_ptr(),
)
.result()
.map_err(VulkanError::from)?;
.map_err(RuntimeError::from)?;
out.set_len(command_buffer_count as usize);
out
}
Expand Down Expand Up @@ -390,10 +390,10 @@ impl Display for CommandPoolCreationError {
}
}

impl From<VulkanError> for CommandPoolCreationError {
fn from(err: VulkanError) -> Self {
impl From<RuntimeError> for CommandPoolCreationError {
fn from(err: RuntimeError) -> Self {
match err {
err @ VulkanError::OutOfHostMemory => Self::OomError(OomError::from(err)),
err @ RuntimeError::OutOfHostMemory => Self::OomError(OomError::from(err)),
_ => panic!("unexpected error: {:?}", err),
}
}
Expand Down Expand Up @@ -522,8 +522,8 @@ impl Display for CommandPoolTrimError {
}
}

impl From<VulkanError> for CommandPoolTrimError {
fn from(err: VulkanError) -> CommandPoolTrimError {
impl From<RuntimeError> for CommandPoolTrimError {
fn from(err: RuntimeError) -> CommandPoolTrimError {
panic!("unexpected error: {:?}", err)
}
}
Expand Down
8 changes: 4 additions & 4 deletions vulkano/src/command_buffer/standard/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ use crate::{
BufferMemoryBarrier, DependencyInfo, ImageMemoryBarrier, PipelineStage,
PipelineStageAccess, PipelineStageAccessSet, PipelineStages,
},
DeviceSize, OomError, RequiresOneOf, VulkanError, VulkanObject,
DeviceSize, OomError, RequiresOneOf, RuntimeError, VulkanObject,
};
use ahash::HashMap;
use parking_lot::Mutex;
Expand Down Expand Up @@ -548,7 +548,7 @@ where

(fns.v1_0.begin_command_buffer)(builder_alloc.inner().handle(), &begin_info_vk)
.result()
.map_err(VulkanError::from)?;
.map_err(RuntimeError::from)?;
}

let mut builder_state: CommandBufferBuilderState = Default::default();
Expand Down Expand Up @@ -608,7 +608,7 @@ where
let fns = self.device().fns();
(fns.v1_0.end_command_buffer)(self.builder_alloc.inner().handle())
.result()
.map_err(VulkanError::from)?;
.map_err(RuntimeError::from)?;

Ok(PrimaryCommandBuffer {
alloc: self.builder_alloc.into_alloc(),
Expand Down Expand Up @@ -638,7 +638,7 @@ where
let fns = self.device().fns();
(fns.v1_0.end_command_buffer)(self.builder_alloc.inner().handle())
.result()
.map_err(VulkanError::from)?;
.map_err(RuntimeError::from)?;

let submit_state = match self.usage {
CommandBufferUsage::MultipleSubmit => SubmitState::ExclusiveUse {
Expand Down
6 changes: 3 additions & 3 deletions vulkano/src/command_buffer/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::{
},
device::{Device, DeviceOwned},
query::QueryControlFlags,
OomError, VulkanError, VulkanObject,
OomError, RuntimeError, VulkanObject,
};
use smallvec::SmallVec;
use std::{ptr, sync::Arc};
Expand Down Expand Up @@ -179,7 +179,7 @@ impl UnsafeCommandBufferBuilder {

(fns.v1_0.begin_command_buffer)(pool_alloc.handle(), &begin_info_vk)
.result()
.map_err(VulkanError::from)?;
.map_err(RuntimeError::from)?;
}

Ok(UnsafeCommandBufferBuilder {
Expand All @@ -196,7 +196,7 @@ impl UnsafeCommandBufferBuilder {
let fns = self.device.fns();
(fns.v1_0.end_command_buffer)(self.handle)
.result()
.map_err(VulkanError::from)?;
.map_err(RuntimeError::from)?;

Ok(UnsafeCommandBuffer {
command_buffer: self.handle,
Expand Down
28 changes: 14 additions & 14 deletions vulkano/src/deferred.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use crate::{
device::{Device, DeviceOwned},
RequiresOneOf, VulkanError, VulkanObject,
RequiresOneOf, RuntimeError, VulkanObject,
};
use std::{
error::Error,
Expand Down Expand Up @@ -66,7 +66,7 @@ impl DeferredOperation {
}

#[cfg_attr(not(feature = "document_unchecked"), doc(hidden))]
pub unsafe fn new_unchecked(device: Arc<Device>) -> Result<Arc<Self>, VulkanError> {
pub unsafe fn new_unchecked(device: Arc<Device>) -> Result<Arc<Self>, RuntimeError> {
let handle = {
let fns = device.fns();
let mut output = MaybeUninit::uninit();
Expand All @@ -75,7 +75,7 @@ impl DeferredOperation {
device.handle(), ptr::null(), output.as_mut_ptr()
)
.result()
.map_err(VulkanError::from)?;
.map_err(RuntimeError::from)?;
output.assume_init()
};

Expand All @@ -96,7 +96,7 @@ impl DeferredOperation {
}

/// Executes a portion of the operation on the current thread.
pub fn join(&self) -> Result<DeferredOperationJoinStatus, VulkanError> {
pub fn join(&self) -> Result<DeferredOperationJoinStatus, RuntimeError> {
let result = unsafe {
let fns = self.device.fns();
(fns.khr_deferred_host_operations.deferred_operation_join_khr)(
Expand All @@ -109,12 +109,12 @@ impl DeferredOperation {
ash::vk::Result::SUCCESS => Ok(DeferredOperationJoinStatus::Complete),
ash::vk::Result::THREAD_DONE_KHR => Ok(DeferredOperationJoinStatus::ThreadDone),
ash::vk::Result::THREAD_IDLE_KHR => Ok(DeferredOperationJoinStatus::ThreadIdle),
err => Err(VulkanError::from(err)),
err => Err(RuntimeError::from(err)),
}
}

/// Returns the result of the operation, or `None` if the operation is not yet complete.
pub fn result(&self) -> Option<Result<(), VulkanError>> {
pub fn result(&self) -> Option<Result<(), RuntimeError>> {
let result = unsafe {
let fns = self.device.fns();
(fns.khr_deferred_host_operations
Expand All @@ -124,12 +124,12 @@ impl DeferredOperation {
match result {
ash::vk::Result::NOT_READY => None,
ash::vk::Result::SUCCESS => Some(Ok(())),
err => Some(Err(VulkanError::from(err))),
err => Some(Err(RuntimeError::from(err))),
}
}

/// Waits for the operation to complete, then returns its result.
pub fn wait(&self) -> Result<Result<(), VulkanError>, VulkanError> {
pub fn wait(&self) -> Result<Result<(), RuntimeError>, RuntimeError> {
// Based on example code on the extension's spec page.

// Call `join` until we get `Complete` or `ThreadDone`.
Expand Down Expand Up @@ -207,7 +207,7 @@ unsafe impl DeviceOwned for DeferredOperation {
/// Error that can happen when creating a `DeferredOperation`.
#[derive(Clone, Debug)]
pub enum DeferredOperationCreateError {
VulkanError(VulkanError),
RuntimeError(RuntimeError),

RequirementNotMet {
required_for: &'static str,
Expand All @@ -218,7 +218,7 @@ pub enum DeferredOperationCreateError {
impl Display for DeferredOperationCreateError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Self::VulkanError(_) => write!(f, "a runtime error occurred"),
Self::RuntimeError(_) => write!(f, "a runtime error occurred"),
Self::RequirementNotMet {
required_for,
requires_one_of,
Expand All @@ -234,15 +234,15 @@ impl Display for DeferredOperationCreateError {
impl Error for DeferredOperationCreateError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
Self::VulkanError(err) => Some(err),
Self::RuntimeError(err) => Some(err),
_ => None,
}
}
}

impl From<VulkanError> for DeferredOperationCreateError {
fn from(err: VulkanError) -> Self {
Self::VulkanError(err)
impl From<RuntimeError> for DeferredOperationCreateError {
fn from(err: RuntimeError) -> Self {
Self::RuntimeError(err)
}
}

Expand Down
Loading

0 comments on commit 59e0df3

Please sign in to comment.