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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
use {MAX_COLOR_TARGETS, MAX_VERTEX_ATTRIBUTES, MAX_CONSTANT_BUFFERS,
MAX_RESOURCE_VIEWS, MAX_UNORDERED_VIEWS, MAX_SAMPLERS};
use {ConstantBufferSlot, ColorSlot, ResourceViewSlot,
UnorderedViewSlot, SamplerSlot,
Primitive, Resources};
use {format, state as s, texture};
use shade::Usage;
use std::error::Error;
use std::fmt;
pub const MAX_VERTEX_BUFFERS: usize = 4;
pub type BufferOffset = usize;
#[derive(Clone, Debug, PartialEq)]
pub struct CreationError;
impl fmt::Display for CreationError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.description())
}
}
impl Error for CreationError {
fn description(&self) -> &str {
"Could not create PSO on device."
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
pub struct ColorInfo {
pub mask: s::ColorMask,
pub color: Option<s::BlendChannel>,
pub alpha: Option<s::BlendChannel>,
}
impl From<s::ColorMask> for ColorInfo {
fn from(mask: s::ColorMask) -> ColorInfo {
ColorInfo {
mask: mask,
color: None,
alpha: None,
}
}
}
impl From<s::Blend> for ColorInfo {
fn from(blend: s::Blend) -> ColorInfo {
ColorInfo {
mask: s::ColorMask::all(),
color: Some(blend.color),
alpha: Some(blend.alpha),
}
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
pub struct DepthStencilInfo {
pub depth: Option<s::Depth>,
pub front: Option<s::StencilSide>,
pub back: Option<s::StencilSide>,
}
impl From<s::Depth> for DepthStencilInfo {
fn from(depth: s::Depth) -> DepthStencilInfo {
DepthStencilInfo {
depth: Some(depth),
front: None,
back: None,
}
}
}
impl From<s::Stencil> for DepthStencilInfo {
fn from(stencil: s::Stencil) -> DepthStencilInfo {
DepthStencilInfo {
depth: None,
front: Some(stencil.front),
back: Some(stencil.back),
}
}
}
impl From<(s::Depth, s::Stencil)> for DepthStencilInfo {
fn from(ds: (s::Depth, s::Stencil)) -> DepthStencilInfo {
DepthStencilInfo {
depth: Some(ds.0),
front: Some(ds.1.front),
back: Some(ds.1.back),
}
}
}
pub type BufferIndex = u8;
pub type ElemOffset = u32;
pub type ElemStride = u8;
pub type InstanceRate = u8;
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
pub struct Element<F> {
pub format: F,
pub offset: ElemOffset,
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
pub struct VertexBufferDesc {
pub stride: ElemStride,
pub rate: InstanceRate,
}
pub type AttributeDesc = (BufferIndex, Element<format::Format>);
pub type ConstantBufferDesc = Usage;
pub type ResourceViewDesc = Usage;
pub type UnorderedViewDesc = Usage;
pub type SamplerDesc = Usage;
pub type ColorTargetDesc = (format::Format, ColorInfo);
pub type DepthStencilDesc = (format::Format, DepthStencilInfo);
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
pub struct Descriptor {
pub primitive: Primitive,
pub rasterizer: s::Rasterizer,
pub scissor: bool,
pub vertex_buffers: [Option<VertexBufferDesc>; MAX_VERTEX_BUFFERS],
pub attributes: [Option<AttributeDesc>; MAX_VERTEX_ATTRIBUTES],
pub constant_buffers: [Option<ConstantBufferDesc>; MAX_CONSTANT_BUFFERS],
pub resource_views: [Option<ResourceViewDesc>; MAX_RESOURCE_VIEWS],
pub unordered_views: [Option<UnorderedViewDesc>; MAX_UNORDERED_VIEWS],
pub samplers: [Option<SamplerDesc>; MAX_SAMPLERS],
pub color_targets: [Option<ColorTargetDesc>; MAX_COLOR_TARGETS],
pub depth_stencil: Option<DepthStencilDesc>,
}
impl Descriptor {
pub fn new(primitive: Primitive, rast: s::Rasterizer) -> Descriptor {
Descriptor {
primitive: primitive,
rasterizer: rast,
scissor: false,
vertex_buffers: [None; MAX_VERTEX_BUFFERS],
attributes: [None; MAX_VERTEX_ATTRIBUTES],
constant_buffers: [None; MAX_CONSTANT_BUFFERS],
resource_views: [None; MAX_RESOURCE_VIEWS],
unordered_views: [None; MAX_UNORDERED_VIEWS],
samplers: [None; MAX_SAMPLERS],
color_targets: [None; MAX_COLOR_TARGETS],
depth_stencil: None,
}
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct VertexBufferSet<R: Resources>(
pub [Option<(R::Buffer, BufferOffset)>; MAX_VERTEX_ATTRIBUTES]
);
impl<R: Resources> VertexBufferSet<R> {
pub fn new() -> VertexBufferSet<R> {
VertexBufferSet([None; MAX_VERTEX_ATTRIBUTES])
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct ConstantBufferParam<R: Resources>(pub R::Buffer, pub Usage, pub ConstantBufferSlot);
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct ResourceViewParam<R: Resources>(pub R::ShaderResourceView, pub Usage, pub ResourceViewSlot);
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct UnorderedViewParam<R: Resources>(pub R::UnorderedAccessView, pub Usage, pub UnorderedViewSlot);
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct SamplerParam<R: Resources>(pub R::Sampler, pub Usage, pub SamplerSlot);
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct PixelTargetSet<R: Resources> {
pub colors: [Option<R::RenderTargetView>; MAX_COLOR_TARGETS],
pub depth: Option<R::DepthStencilView>,
pub stencil: Option<R::DepthStencilView>,
pub dimensions: Option<texture::Dimensions>,
}
impl<R: Resources> PixelTargetSet<R> {
pub fn new() -> PixelTargetSet<R> {
PixelTargetSet {
colors: [None; MAX_COLOR_TARGETS],
depth: None,
stencil: None,
dimensions: None,
}
}
pub fn add_color(&mut self,
slot: ColorSlot,
view: &R::RenderTargetView,
dim: texture::Dimensions) {
self.colors[slot as usize] = Some(view.clone());
self.set_dimensions(dim);
}
pub fn add_depth_stencil(&mut self,
view: &R::DepthStencilView,
has_depth: bool,
has_stencil: bool,
dim: texture::Dimensions) {
if has_depth {
self.depth = Some(view.clone());
}
if has_stencil {
self.stencil = Some(view.clone());
}
self.set_dimensions(dim);
}
fn set_dimensions(&mut self, dim: texture::Dimensions) {
debug_assert!(self.dimensions.map(|d| d == dim).unwrap_or(true));
self.dimensions = Some(dim);
}
pub fn get_view(&self) -> (u16, u16, u16) {
use std::cmp::max;
self.dimensions
.map(|(w, h, d, _)| (max(w, 1), max(h, 1), max(d, 1)))
.unwrap_or((1, 1, 1))
}
}