Skip to main content

nuvec_gpu/
error.rs

1//! Errors of the Metal runtime.
2
3use std::fmt;
4
5/// Why a GPU operation failed.
6#[derive(Debug, Clone, PartialEq, Eq)]
7#[non_exhaustive]
8pub enum GpuError {
9    /// The kernel library is unavailable in this build (non-macOS target).
10    NoKernels,
11    /// The kernel library could not be loaded or compiled.
12    LibraryLoad(String),
13    /// No kernel with this name exists in the library.
14    MissingKernel(String),
15    /// A compute pipeline could not be created.
16    Pipeline {
17        /// The kernel name.
18        name: String,
19        /// Metal's description.
20        message: String,
21    },
22    /// A dispatch argument is invalid (offset out of range, too many arguments, …).
23    InvalidArgument(String),
24    /// The buffer is used by submitted GPU work that has not been waited on.
25    BufferInUse,
26    /// Metal could not allocate a buffer or command object of this many bytes.
27    OutOfMemory(usize),
28    /// The command buffer failed on the GPU.
29    CommandBuffer(String),
30}
31
32/// `Result` with [`GpuError`].
33pub type Result<T> = std::result::Result<T, GpuError>;
34
35impl fmt::Display for GpuError {
36    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37        match self {
38            Self::NoKernels => f.write_str("the Metal kernel library is unavailable in this build"),
39            Self::LibraryLoad(msg) => write!(f, "cannot load the Metal kernel library: {msg}"),
40            Self::MissingKernel(name) => write!(f, "no Metal kernel named `{name}`"),
41            Self::Pipeline { name, message } => {
42                write!(f, "cannot create pipeline `{name}`: {message}")
43            }
44            Self::InvalidArgument(msg) => write!(f, "invalid dispatch argument: {msg}"),
45            Self::BufferInUse => {
46                f.write_str("buffer is in use by submitted GPU work; wait for it first")
47            }
48            Self::OutOfMemory(n) => write!(f, "Metal could not allocate {n} bytes"),
49            Self::CommandBuffer(msg) => write!(f, "GPU command buffer failed: {msg}"),
50        }
51    }
52}
53
54impl std::error::Error for GpuError {}