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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use device;
use device::{handle, tex};
use device::shade::{Stage, CreateShaderError};
use render::mesh::{Mesh, VertexFormat};
use super::shade::*;
pub trait FactoryExt<R: device::Resources>: device::Factory<R> {
fn create_mesh<T: VertexFormat>(&mut self, data: &[T]) -> Mesh<R> {
let nv = data.len();
let buf = self.create_buffer_static(data);
Mesh::from_format(buf, nv as device::VertexCount)
}
fn link_program(&mut self, vs_code: &[u8], fs_code: &[u8])
-> Result<handle::Program<R>, ProgramError> {
let vs = match self.create_shader(Stage::Vertex, vs_code) {
Ok(s) => s,
Err(e) => return Err(ProgramError::Vertex(e)),
};
let fs = match self.create_shader(Stage::Fragment, fs_code) {
Ok(s) => s,
Err(e) => return Err(ProgramError::Fragment(e)),
};
self.create_program(&[vs, fs], None)
.map_err(|e| ProgramError::Link(e))
}
fn link_program_source(&mut self, vs_src: ShaderSource, fs_src: ShaderSource,
caps: &device::Capabilities)
-> Result<handle::Program<R>, ProgramError> {
let model = caps.shader_model;
let err_model = CreateShaderError::ModelNotSupported;
let vs = match vs_src.choose(model) {
Ok(code) => match self.create_shader(Stage::Vertex, code) {
Ok(s) => s,
Err(e) => return Err(ProgramError::Vertex(e)),
},
Err(_) => return Err(ProgramError::Vertex(err_model))
};
let fs = match fs_src.choose(model) {
Ok(code) => match self.create_shader(Stage::Fragment, code) {
Ok(s) => s,
Err(e) => return Err(ProgramError::Fragment(e)),
},
Err(_) => return Err(ProgramError::Fragment(err_model))
};
self.create_program(&[vs, fs], Some(fs_src.targets))
.map_err(|e| ProgramError::Link(e))
}
fn compile_shader_source(&mut self, stage: Stage, source: ShaderSource)
-> Result<handle::Shader<R>, CreateShaderError> {
match source.choose(self.get_capabilities().shader_model) {
Ok(code) => self.create_shader(stage, code),
Err(_) => Err(CreateShaderError::ModelNotSupported)
}
}
fn link_program_source2(&mut self, vs_src: ShaderSource, fs_src: ShaderSource)
-> Result<handle::Program<R>, ProgramError> {
let vs = match self.compile_shader_source(Stage::Vertex, vs_src) {
Ok(s) => s,
Err(e) => return Err(ProgramError::Vertex(e)),
};
let fs = match self.compile_shader_source(Stage::Fragment, fs_src) {
Ok(s) => s,
Err(e) => return Err(ProgramError::Fragment(e)),
};
self.create_program(&[vs, fs], Some(fs_src.targets))
.map_err(|e| ProgramError::Link(e))
}
fn create_texture_rgba8(&mut self, width: u16, height: u16)
-> Result<handle::Texture<R>, tex::TextureError> {
self.create_texture(tex::TextureInfo {
width: width,
height: height,
depth: 1,
levels: 1,
kind: tex::TextureKind::Texture2D,
format: tex::RGBA8,
})
}
fn create_texture_rgba8_static(&mut self, width: u16, height: u16, data: &[u32])
-> Result<handle::Texture<R>, tex::TextureError> {
let info = tex::TextureInfo {
width: width,
height: height,
depth: 1,
levels: 99,
kind: tex::TextureKind::Texture2D,
format: tex::RGBA8,
};
match self.create_texture_static(info, data) {
Ok(handle) => {
self.generate_mipmap(&handle);
Ok(handle)
},
Err(e) => Err(e),
}
}
fn create_texture_depth_stencil(&mut self, width: u16, height: u16)
-> Result<handle::Texture<R>, tex::TextureError> {
self.create_texture(tex::TextureInfo {
width: width,
height: height,
depth: 0,
levels: 1,
kind: tex::TextureKind::Texture2D,
format: tex::Format::DEPTH24_STENCIL8,
})
}
}
impl<R: device::Resources, F: device::Factory<R>> FactoryExt<R> for F {}