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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
use std::error::Error;
use std::fmt;
use core::{buffer, format, handle, texture, state};
use core::{Primitive, Resources, ShaderSet};
use core::factory::Factory;
use core::pso::{CreationError, Descriptor};
use core::memory::{self, Bind, Pod};
use slice::{Slice, IndexBuffer, IntoIndexBuffer};
use pso;
use shade::ProgramError;
#[derive(Clone, PartialEq, Debug)]
pub enum PipelineStateError<S> {
Program(ProgramError),
DescriptorInit(pso::InitError<S>),
DeviceCreate(CreationError),
}
impl<'a> From<PipelineStateError<&'a str>> for PipelineStateError<String> {
fn from(pse: PipelineStateError<&'a str>) -> PipelineStateError<String> {
match pse {
PipelineStateError::Program(e) => PipelineStateError::Program(e),
PipelineStateError::DescriptorInit(e) => PipelineStateError::DescriptorInit(e.into()),
PipelineStateError::DeviceCreate(e) => PipelineStateError::DeviceCreate(e),
}
}
}
impl<S: fmt::Debug + fmt::Display> fmt::Display for PipelineStateError<S> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
PipelineStateError::Program(ref e) => write!(f, "{}: {}", self.description(), e),
PipelineStateError::DescriptorInit(ref e) => write!(f, "{}: {}", self.description(), e),
PipelineStateError::DeviceCreate(ref e) => write!(f, "{}: {}", self.description(), e),
}
}
}
impl<S: fmt::Debug + fmt::Display> Error for PipelineStateError<S> {
fn description(&self) -> &str {
match *self {
PipelineStateError::Program(_) => "Shader program failed to link",
PipelineStateError::DescriptorInit(_) =>
"Unable to create PSO descriptor due to mismatched formats",
PipelineStateError::DeviceCreate(_) => "Device failed to create the handle give the descriptor",
}
}
fn cause(&self) -> Option<&Error> {
match *self {
PipelineStateError::Program(ref program_error) => Some(program_error),
PipelineStateError::DescriptorInit(ref init_error) => Some(init_error),
PipelineStateError::DeviceCreate(ref creation_error) => Some(creation_error),
}
}
}
impl<S> From<ProgramError> for PipelineStateError<S> {
fn from(e: ProgramError) -> Self {
PipelineStateError::Program(e)
}
}
impl<S> From<pso::InitError<S>> for PipelineStateError<S> {
fn from(e: pso::InitError<S>) -> Self {
PipelineStateError::DescriptorInit(e)
}
}
impl<S> From<CreationError> for PipelineStateError<S> {
fn from(e: CreationError) -> Self {
PipelineStateError::DeviceCreate(e)
}
}
pub trait FactoryExt<R: Resources>: Factory<R> {
fn create_vertex_buffer<T>(&mut self, vertices: &[T])
-> handle::Buffer<R, T>
where T: Pod + pso::buffer::Structure<format::Format>
{
self.create_buffer_immutable(vertices, buffer::Role::Vertex, Bind::empty())
.unwrap()
}
fn create_vertex_buffer_with_slice<B, V>(&mut self, vertices: &[V], indices: B)
-> (handle::Buffer<R, V>, Slice<R>)
where V: Pod + pso::buffer::Structure<format::Format>,
B: IntoIndexBuffer<R>
{
let vertex_buffer = self.create_vertex_buffer(vertices);
let index_buffer = indices.into_index_buffer(self);
let buffer_length = match index_buffer {
IndexBuffer::Auto => vertex_buffer.len(),
IndexBuffer::Index16(ref ib) => ib.len(),
IndexBuffer::Index32(ref ib) => ib.len(),
};
(vertex_buffer, Slice {
start: 0,
end: buffer_length as u32,
base_vertex: 0,
instances: None,
buffer: index_buffer
})
}
fn create_constant_buffer<T>(&mut self, num: usize) -> handle::Buffer<R, T>
where T: Copy
{
self.create_buffer(num,
buffer::Role::Constant,
memory::Usage::Dynamic,
Bind::empty()).unwrap()
}
fn create_upload_buffer<T>(&mut self, num: usize)
-> Result<handle::Buffer<R, T>, buffer::CreationError>
{
self.create_buffer(num,
buffer::Role::Staging,
memory::Usage::Upload,
memory::TRANSFER_SRC)
}
fn create_download_buffer<T>(&mut self, num: usize)
-> Result<handle::Buffer<R, T>, buffer::CreationError>
{
self.create_buffer(num,
buffer::Role::Staging,
memory::Usage::Download,
memory::TRANSFER_DST)
}
fn create_shader_set(&mut self, vs_code: &[u8], ps_code: &[u8])
-> Result<ShaderSet<R>, ProgramError> {
let vs = match self.create_shader_vertex(vs_code) {
Ok(s) => s,
Err(e) => return Err(ProgramError::Vertex(e)),
};
let ps = match self.create_shader_pixel(ps_code) {
Ok(s) => s,
Err(e) => return Err(ProgramError::Pixel(e)),
};
Ok(ShaderSet::Simple(vs, ps))
}
fn create_shader_set_tessellation(&mut self, vs_code: &[u8], hs_code: &[u8], ds_code: &[u8], ps_code: &[u8])
-> Result<ShaderSet<R>, ProgramError> {
let vs = match self.create_shader_vertex(vs_code) {
Ok(s) => s,
Err(e) => return Err(ProgramError::Vertex(e)),
};
let hs = match self.create_shader_hull(hs_code) {
Ok(s) => s,
Err(e) => return Err(ProgramError::Hull(e)),
};
let ds = match self.create_shader_domain(ds_code) {
Ok(s) => s,
Err(e) => return Err(ProgramError::Domain(e)),
};
let ps = match self.create_shader_pixel(ps_code) {
Ok(s) => s,
Err(e) => return Err(ProgramError::Pixel(e)),
};
Ok(ShaderSet::Tessellated(vs, hs, ds, ps))
}
fn link_program(&mut self, vs_code: &[u8], ps_code: &[u8])
-> Result<handle::Program<R>, ProgramError> {
let set = try!(self.create_shader_set(vs_code, ps_code));
self.create_program(&set).map_err(|e| ProgramError::Link(e))
}
fn create_pipeline_state<I: pso::PipelineInit>(&mut self, shaders: &ShaderSet<R>,
primitive: Primitive, rasterizer: state::Rasterizer, init: I)
-> Result<pso::PipelineState<R, I::Meta>, PipelineStateError<String>>
{
let program = try!(self.create_program(shaders).map_err(|e| ProgramError::Link(e)));
self.create_pipeline_from_program(&program, primitive, rasterizer, init).map_err(|error| {
use self::PipelineStateError::*;
match error {
Program(e) => Program(e),
DescriptorInit(e) => DescriptorInit(e.into()),
DeviceCreate(e) => DeviceCreate(e),
}
})
}
fn create_pipeline_from_program<'a, I: pso::PipelineInit>(&mut self, program: &'a handle::Program<R>,
primitive: Primitive, rasterizer: state::Rasterizer, init: I)
-> Result<pso::PipelineState<R, I::Meta>, PipelineStateError<&'a str>>
{
let mut descriptor = Descriptor::new(primitive, rasterizer);
let meta = try!(init.link_to(&mut descriptor, program.get_info()));
let raw = try!(self.create_pipeline_state_raw(program, &descriptor));
Ok(pso::PipelineState::new(raw, primitive, meta))
}
fn create_pipeline_simple<I: pso::PipelineInit>(&mut self, vs: &[u8], ps: &[u8], init: I)
-> Result<pso::PipelineState<R, I::Meta>, PipelineStateError<String>>
{
let set = try!(self.create_shader_set(vs, ps));
self.create_pipeline_state(&set, Primitive::TriangleList, state::Rasterizer::new_fill(),
init)
}
fn create_sampler_linear(&mut self) -> handle::Sampler<R> {
self.create_sampler(texture::SamplerInfo::new(
texture::FilterMethod::Trilinear,
texture::WrapMode::Clamp,
))
}
}
impl<R: Resources, F: Factory<R>> FactoryExt<R> for F {}