1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
use std::marker::PhantomData;
pub trait HasStage {
type AStage;
fn new_shader(shader_type: Type, src: &str) -> Result<Self::AStage, StageError>;
fn free_shader(shader: &mut Self::AStage);
}
pub trait ShaderTypeable {
fn shader_type() -> Type;
}
#[derive(Clone, Copy, Debug)]
pub enum Type {
TessellationControlShader,
TessellationEvaluationShader,
VertexShader,
GeometryShader,
FragmentShader
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct TessellationControlShader;
impl ShaderTypeable for TessellationControlShader {
fn shader_type() -> Type { Type::TessellationControlShader }
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct TessellationEvaluationShader;
impl ShaderTypeable for TessellationEvaluationShader {
fn shader_type() -> Type { Type::TessellationEvaluationShader }
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct VertexShader;
impl ShaderTypeable for VertexShader {
fn shader_type() -> Type { Type::VertexShader }
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct GeometryShader;
impl ShaderTypeable for GeometryShader {
fn shader_type() -> Type { Type::GeometryShader }
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct FragmentShader;
impl ShaderTypeable for FragmentShader {
fn shader_type() -> Type { Type::FragmentShader }
}
#[derive(Debug)]
pub struct Stage<C, T> where C: HasStage {
pub repr: C::AStage,
_t: PhantomData<T>
}
impl<C, T> Drop for Stage<C, T> where C: HasStage {
fn drop(&mut self) {
C::free_shader(&mut self.repr)
}
}
impl<C, T> Stage<C, T> where C: HasStage, T: ShaderTypeable {
pub fn new(src: &str) -> Result<Self, StageError> {
let shader = C::new_shader(T::shader_type(), src);
shader.map(|shader| Stage {
repr: shader,
_t: PhantomData
})
}
}
#[derive(Clone, Debug)]
pub enum StageError {
CompilationFailed(Type, String),
UnsupportedType(Type)
}