Skip to main content

dream_soft_render/
error.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3use std::fmt;
4
5use crate::TextureId;
6
7/// Why a renderer call was rejected. Invalid input fails loudly rather than being clipped
8/// or skipped.
9#[derive(Clone, Debug, PartialEq, Eq)]
10#[non_exhaustive]
11pub enum Error {
12    /// The surface size overflows, exceeds [`MAX_SURFACE_PIXELS`](crate::MAX_SURFACE_PIXELS), or
13    /// is longer than 65535 pixels on a side.
14    SurfaceSize {
15        /// Requested width.
16        width: usize,
17        /// Requested height.
18        height: usize,
19    },
20    /// A texture dimension is zero or longer than 65536 texels, or the texture's byte size
21    /// overflows.
22    TextureSize {
23        /// Requested width.
24        width: usize,
25        /// Requested height.
26        height: usize,
27    },
28    /// Pixel data is not exactly `width * height * 4` bytes.
29    PixelDataLength {
30        /// Bytes the dimensions call for.
31        expected: usize,
32        /// Bytes supplied.
33        actual: usize,
34    },
35    /// Storing the texture would exceed [`MAX_TEXTURE_BYTES`](crate::MAX_TEXTURE_BYTES).
36    TextureBudget {
37        /// Total texture bytes the call would need.
38        requested: usize,
39        /// The budget.
40        budget: usize,
41    },
42    /// The texture handle was never created by this renderer, or has been freed.
43    UnknownTexture(TextureId),
44    /// The handle space is exhausted: every handle from 0 through `u64::MAX - 1` has been
45    /// issued. Handles are never reused, so no more textures can be created in this process.
46    TextureIdsExhausted,
47    /// A texture update region does not fit inside the texture.
48    TextureUpdateOutOfBounds(TextureId),
49    /// A rectangle coordinate is NaN or infinite.
50    NonFiniteRect,
51    /// A mesh vertex has a NaN or infinite position or texture coordinate.
52    NonFiniteVertex {
53        /// Index of the vertex in [`Mesh::vertices`](crate::Mesh::vertices).
54        index: usize,
55        /// Which of its coordinates is not finite.
56        field: VertexField,
57    },
58    /// A mesh's index count is not a multiple of three.
59    IndexCount(usize),
60    /// A mesh index points past the end of its vertices.
61    IndexOutOfRange {
62        /// The offending index.
63        index: u32,
64        /// How many vertices the mesh has.
65        vertex_count: usize,
66    },
67    /// The rasterizer rejected the draw; the message says why.
68    Raster(String),
69}
70
71impl fmt::Display for Error {
72    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
73        match self {
74            Self::SurfaceSize { width, height } => {
75                write!(
76                    f,
77                    "surface size {width}x{height} overflows, exceeds the pixel budget, or is \
78                     longer than 65535 pixels on a side"
79                )
80            }
81            Self::TextureSize { width, height } => {
82                write!(
83                    f,
84                    "texture size {width}x{height} is empty, overflows, or is longer than 65536 \
85                     texels on a side"
86                )
87            }
88            Self::PixelDataLength { expected, actual } => {
89                write!(f, "pixel data is {actual} bytes, expected {expected}")
90            }
91            Self::TextureBudget { requested, budget } => {
92                write!(
93                    f,
94                    "texture storage would need {requested} bytes, over the {budget} byte budget"
95                )
96            }
97            Self::UnknownTexture(texture) => write!(f, "unknown or freed texture {texture}"),
98            Self::TextureIdsExhausted => f.write_str("every texture handle has been issued"),
99            Self::TextureUpdateOutOfBounds(texture) => {
100                write!(f, "update region exceeds the bounds of texture {texture}")
101            }
102            Self::NonFiniteRect => f.write_str("rectangle has a non-finite coordinate"),
103            Self::NonFiniteVertex { index, field } => {
104                write!(f, "mesh vertex {index} has a non-finite {field}")
105            }
106            Self::IndexCount(count) => {
107                write!(f, "mesh has {count} indices, not a multiple of three")
108            }
109            Self::IndexOutOfRange {
110                index,
111                vertex_count,
112            } => write!(
113                f,
114                "mesh index {index} is out of range for {vertex_count} vertices"
115            ),
116            Self::Raster(message) => write!(f, "rasterizer error: {message}"),
117        }
118    }
119}
120
121impl std::error::Error for Error {}
122
123/// The part of a [`Vertex`](crate::Vertex) an [`Error::NonFiniteVertex`] refers to.
124#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
125pub enum VertexField {
126    /// [`Vertex::pos`](crate::Vertex::pos).
127    Position,
128    /// [`Vertex::uv`](crate::Vertex::uv).
129    Uv,
130}
131
132impl fmt::Display for VertexField {
133    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
134        f.write_str(match self {
135            Self::Position => "position",
136            Self::Uv => "texture coordinate",
137        })
138    }
139}
140
141impl From<std::io::Error> for Error {
142    fn from(error: std::io::Error) -> Self {
143        Self::Raster(error.to_string())
144    }
145}