librashader_reflect/
error.rs

1use crate::reflect::semantics::UniformMemberBlock;
2use thiserror::Error;
3
4/// Error type for shader compilation.
5#[non_exhaustive]
6#[derive(Error, Debug)]
7pub enum ShaderCompileError {
8    /// Compile error from naga.
9    #[cfg(feature = "unstable-naga-in")]
10    #[error("shader")]
11    NagaCompileError(Vec<naga::front::glsl::Error>),
12
13    /// Compilation error from glslang.
14    #[error("error when compiling with glslang: {0}")]
15    GlslangError(#[from] glslang::error::GlslangError),
16
17    /// Error when initializing the glslang compiler.
18    #[error("error when initializing glslang")]
19    CompilerInitError,
20
21    /// Error when transpiling from spirv-cross.
22    #[error("spirv-cross error: {0:?}")]
23    SpirvCrossCompileError(#[from] spirv_cross2::SpirvCrossError),
24
25    /// Error when transpiling from spirv-to-dxil
26    #[cfg(all(target_os = "windows", feature = "dxil"))]
27    #[error("spirv-to-dxil error: {0:?}")]
28    SpirvToDxilCompileError(#[from] spirv_to_dxil::SpirvToDxilError),
29
30    /// Error when transpiling from naga
31    #[cfg(all(feature = "wgsl", feature = "naga"))]
32    #[error("naga error when compiling wgsl: {0:?}")]
33    NagaWgslError(#[from] naga::back::wgsl::Error),
34
35    /// Error when transpiling from naga
36    #[cfg(feature = "naga")]
37    #[error("naga error when compiling spirv: {0:?}")]
38    NagaSpvError(#[from] naga::back::spv::Error),
39
40    /// Error when transpiling from naga
41    #[cfg(all(feature = "naga", feature = "msl"))]
42    #[error("naga error when compiling msl: {0:?}")]
43    NagaMslError(#[from] naga::back::msl::Error),
44
45    /// Error when transpiling from naga
46    #[cfg(any(feature = "naga", feature = "wgsl"))]
47    #[error("naga validation error: {0}")]
48    NagaValidationError(#[from] naga::WithSpan<naga::valid::ValidationError>),
49}
50
51/// The error kind encountered when reflecting shader semantics.
52#[derive(Debug)]
53pub enum SemanticsErrorKind {
54    /// The number of uniform buffers was invalid. Only one UBO is permitted.
55    InvalidUniformBufferCount(usize),
56    /// The number of push constant blocks was invalid. Only one push constant block is permitted.
57    InvalidPushBufferCount(usize),
58    /// The size of the push constant block was invalid.
59    InvalidPushBufferSize(u32),
60    /// The location of a varying was invalid.
61    InvalidLocation(u32),
62    /// The requested descriptor set was invalid. Only descriptor set 0 is available.
63    InvalidDescriptorSet(u32),
64    /// The number of inputs to the shader was invalid.
65    InvalidInputCount(usize),
66    /// The number of outputs declared was invalid.
67    InvalidOutputCount(usize),
68    /// Expected a binding but there was none.
69    MissingBinding,
70    /// The declared binding point was invalid.
71    InvalidBinding(u32),
72    /// The declared resource type was invalid.
73    InvalidResourceType,
74    /// The range of a struct member was invalid.
75    InvalidRange(u32),
76    /// The number of entry points in the shader was invalid.
77    InvalidEntryPointCount(usize),
78    /// The requested uniform or texture name was not provided semantics.
79    UnknownSemantics(String),
80    /// The type of the requested uniform was not compatible with the provided semantics.
81    InvalidTypeForSemantic(String),
82}
83
84/// Error type for shader reflection.
85#[non_exhaustive]
86#[derive(Error, Debug)]
87pub enum ShaderReflectError {
88    /// Reflection error from spirv-cross.
89    #[error("spirv cross error: {0}")]
90    SpirvCrossError(#[from] spirv_cross2::SpirvCrossError),
91    /// Error when validating vertex shader semantics.
92    #[error("error when verifying vertex semantics: {0:?}")]
93    VertexSemanticError(SemanticsErrorKind),
94    /// Error when validating fragment shader semantics.
95    #[error("error when verifying texture semantics {0:?}")]
96    FragmentSemanticError(SemanticsErrorKind),
97    /// The vertex and fragment shader must have the same UBO binding location.
98    #[error("vertex and fragment shader must have same UBO binding. declared {vertex} in vertex, got {fragment} in fragment")]
99    MismatchedUniformBuffer { vertex: u32, fragment: u32 },
100    /// The filter chain was found to be non causal. A pass tried to access the target output
101    /// in the future.
102    #[error("filter chain is non causal: tried to access target {target} in pass {pass}")]
103    NonCausalFilterChain { pass: usize, target: usize },
104    /// The offset of the given uniform did not match up in both the vertex and fragment shader.
105    #[error("the offset of {semantic} was declared as {expected} but found as {received} in pass {pass}")]
106    MismatchedOffset {
107        semantic: String,
108        expected: usize,
109        received: usize,
110        ty: UniformMemberBlock,
111        pass: usize,
112    },
113    /// The size of the given uniform did not match up in both the vertex and fragment shader.
114    #[error("the size of {semantic} was found declared as {vertex} in vertex shader but found as {fragment} in fragment in pass {pass}")]
115    MismatchedSize {
116        semantic: String,
117        vertex: u32,
118        fragment: u32,
119        pass: usize,
120    },
121    /// The binding number is already in use.
122    #[error("binding {0} is already in use")]
123    BindingInUse(u32),
124    /// Error when transpiling from naga
125    #[cfg(feature = "naga")]
126    #[error("naga spirv error: {0}")]
127    NagaInputError(#[from] naga::front::spv::Error),
128    /// Error when transpiling from naga
129    #[cfg(feature = "naga")]
130    #[error("naga validation error: {0}")]
131    NagaReflectError(#[from] naga::WithSpan<naga::valid::ValidationError>),
132}
133
134#[cfg(feature = "unstable-naga-in")]
135impl From<Vec<naga::front::glsl::Error>> for ShaderCompileError {
136    fn from(err: Vec<naga::front::glsl::Error>) -> Self {
137        ShaderCompileError::NagaCompileError(err)
138    }
139}