gfx/
shade.rs

1// Copyright 2014 The Gfx-rs Developers.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Shader parameter handling.
16
17#[cfg(feature = "mint")]
18use mint;
19
20use std::error::Error;
21use std::fmt;
22pub use core::shade::{self as core, ConstFormat, Formatted, Usage};
23
24#[allow(missing_docs)]
25pub trait ToUniform: Copy {
26    fn convert(self) -> core::UniformValue;
27}
28
29macro_rules! impl_uniforms {
30    ( $( $ty_src:ty = $ty_dst:ident ,)* ) => {
31        $(
32            impl ToUniform for $ty_src {
33                fn convert(self) -> core::UniformValue {
34                    core::UniformValue::$ty_dst(self.into())
35                }
36            }
37        )*
38    }
39}
40
41impl_uniforms! {
42    i32 = I32,
43    u32 = U32,
44    f32 = F32,
45    [i32; 2] = I32Vector2,
46    [i32; 3] = I32Vector3,
47    [i32; 4] = I32Vector4,
48    [u32; 2] = U32Vector2,
49    [u32; 3] = U32Vector3,
50    [u32; 4] = U32Vector4,
51    [f32; 2] = F32Vector2,
52    [f32; 3] = F32Vector3,
53    [f32; 4] = F32Vector4,
54    [[f32; 2]; 2] = F32Matrix2,
55    [[f32; 3]; 3] = F32Matrix3,
56    [[f32; 4]; 4] = F32Matrix4,
57}
58
59#[cfg(feature = "mint")]
60impl_uniforms! {
61    mint::Point2<f32> = F32Vector2,
62    mint::Point3<f32> = F32Vector3,
63    mint::Vector2<f32> = F32Vector2,
64    mint::Vector3<f32> = F32Vector3,
65    mint::Vector4<f32> = F32Vector4,
66    mint::ColumnMatrix2<f32> = F32Matrix2,
67    mint::ColumnMatrix3<f32> = F32Matrix3,
68    mint::ColumnMatrix4<f32> = F32Matrix4,
69}
70
71/// Program linking error
72#[derive(Clone, Debug, PartialEq)]
73pub enum ProgramError {
74    /// Unable to compile the vertex shader
75    Vertex(core::CreateShaderError),
76    /// Unable to compile the hull shader
77    Hull(core::CreateShaderError),
78    /// Unable to compile the domain shader
79    Domain(core::CreateShaderError),
80    /// Unable to compile the geometry shader
81    Geometry(core::CreateShaderError),
82    /// Unable to compile the pixel shader
83    Pixel(core::CreateShaderError),
84    /// Unable to link
85    Link(core::CreateProgramError),
86}
87
88impl fmt::Display for ProgramError {
89    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
90        match *self {
91            ProgramError::Vertex(ref e) => write!(f, "{}: {}", self.description(), e),
92            ProgramError::Hull(ref e) => write!(f, "{}: {}", self.description(), e),
93            ProgramError::Domain(ref e) => write!(f, "{}: {}", self.description(), e),
94            ProgramError::Geometry(ref e) => write!(f, "{}: {}", self.description(), e),
95            ProgramError::Pixel(ref e) => write!(f, "{}: {}", self.description(), e),
96            ProgramError::Link(ref e) => write!(f, "{}: {}", self.description(), e),
97        }
98    }
99}
100
101impl Error for ProgramError {
102    fn description(&self) -> &str {
103        match *self {
104            ProgramError::Vertex(_) => "Unable to compile the vertex shader",
105            ProgramError::Hull(_) => "Unable to compile the hull shader",
106            ProgramError::Domain(_) => "Unable to compile the domain shader",
107            ProgramError::Geometry(_) => "Unable to compile the geometry shader",
108            ProgramError::Pixel(_) => "Unable to compile the pixel shader",
109            ProgramError::Link(_) => "Unable to link",
110        }
111    }
112
113    fn cause(&self) -> Option<&dyn Error> {
114        match *self {
115            ProgramError::Vertex(ref e) => Some(e),
116            ProgramError::Hull(ref e) => Some(e),
117            ProgramError::Domain(ref e) => Some(e),
118            ProgramError::Geometry(ref e) => Some(e),
119            ProgramError::Pixel(ref e) => Some(e),
120            ProgramError::Link(ref e) => Some(e),
121        }
122    }
123}