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    /// This build embeds no kernel library. Builds for macOS (15 or later, nuvec's floor)
10    /// always embed one, precompiled or as runtime sources, so they never report this.
11    NoKernels,
12    /// The kernel library could not be loaded or compiled.
13    LibraryLoad(String),
14    /// No kernel with this name exists in the library.
15    MissingKernel(String),
16    /// A compute pipeline could not be created.
17    Pipeline {
18        /// The kernel name.
19        name: String,
20        /// Metal's description.
21        message: String,
22    },
23    /// A dispatch argument is invalid (offset out of range, too many arguments, …).
24    InvalidArgument(String),
25    /// The buffer is used by submitted GPU work that has not been waited on.
26    BufferInUse,
27    /// Metal could not allocate a buffer or command object of this many bytes.
28    OutOfMemory(usize),
29    /// The command buffer failed on the GPU.
30    CommandBuffer(String),
31    /// A file could not be opened or mapped.
32    Io(String),
33    /// The buffer is a read-only mapped file.
34    ReadOnly,
35}
36
37/// `Result` with [`GpuError`].
38pub type Result<T> = std::result::Result<T, GpuError>;
39
40impl fmt::Display for GpuError {
41    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42        match self {
43            Self::NoKernels => f.write_str("the Metal kernel library is unavailable in this build"),
44            Self::LibraryLoad(msg) => write!(f, "cannot load the Metal kernel library: {msg}"),
45            Self::MissingKernel(name) => write!(f, "no Metal kernel named `{name}`"),
46            Self::Pipeline { name, message } => {
47                write!(f, "cannot create pipeline `{name}`: {message}")
48            }
49            Self::InvalidArgument(msg) => write!(f, "invalid dispatch argument: {msg}"),
50            Self::BufferInUse => {
51                f.write_str("buffer is in use by submitted GPU work; wait for it first")
52            }
53            Self::OutOfMemory(n) => write!(f, "Metal could not allocate {n} bytes"),
54            Self::CommandBuffer(msg) => write!(f, "GPU command buffer failed: {msg}"),
55            Self::Io(msg) => write!(f, "I/O error: {msg}"),
56            Self::ReadOnly => f.write_str("the buffer is a read-only mapped file"),
57        }
58    }
59}
60
61impl std::error::Error for GpuError {}