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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
use factory::Bind;
use format;
use state;
pub use target::{Layer, Level};
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Error {
Format(format::SurfaceType, Option<format::ChannelType>),
Kind,
Samples(AaMode),
Size(Size),
Data(usize),
}
pub type Size = u16;
pub type ArraySize = u8;
pub type Bits = u8;
pub type NumSamples = u8;
pub type NumFragments = u8;
pub type Dimensions = (Size, Size, Size, AaMode);
#[derive(Eq, Ord, PartialEq, PartialOrd, Hash, Copy, Clone, Debug)]
pub enum AaMode {
Single,
Multi(NumSamples),
Coverage(NumSamples, NumFragments),
}
impl From<NumSamples> for AaMode {
fn from(ns: NumSamples) -> AaMode {
if ns > 1 {
AaMode::Multi(ns)
}else {
AaMode::Single
}
}
}
impl AaMode {
pub fn get_num_fragments(&self) -> NumFragments {
match *self {
AaMode::Single => 1,
AaMode::Multi(n) => n,
AaMode::Coverage(_, nf) => nf,
}
}
pub fn needs_resolve(&self) -> bool {
self.get_num_fragments() > 1
}
}
#[derive(Eq, Ord, PartialEq, PartialOrd, Hash, Copy, Clone, Debug)]
pub enum FilterMethod {
Scale,
Mipmap,
Bilinear,
Trilinear,
Anisotropic(u8)
}
#[derive(Eq, Ord, PartialEq, PartialOrd, Hash, Copy, Clone, Debug)]
#[allow(missing_docs)]
pub enum CubeFace {
PosZ,
NegZ,
PosX,
NegX,
PosY,
NegY
}
#[derive(Eq, Ord, PartialEq, PartialOrd, Hash, Copy, Clone, Debug)]
pub enum Kind {
D1(Size),
D1Array(Size, ArraySize),
D2(Size, Size, AaMode),
D2Array(Size, Size, ArraySize, AaMode),
D3(Size, Size, Size),
Cube(Size, Size),
CubeArray(Size, Size, ArraySize),
}
impl Kind {
pub fn get_dimensions(&self) -> Dimensions {
let s0 = AaMode::Single;
match *self {
Kind::D1(w) => (w, 0, 0, s0),
Kind::D1Array(w, a) => (w, 0, a as Size, s0),
Kind::D2(w, h, s) => (w, h, 0, s),
Kind::D2Array(w, h, a, s) => (w, h, a as Size, s),
Kind::D3(w, h, d) => (w, h, d, s0),
Kind::Cube(w, h) => (w, h, 6, s0),
Kind::CubeArray(w, h, a) => (w, h, 6 * (a as Size), s0)
}
}
pub fn get_level_dimensions(&self, level: Level) -> Dimensions {
use std::cmp::max;
let (w, h, d, _) = self.get_dimensions();
(max(1, w >> level), max(1, h >> level), max(1, d >> level), AaMode::Single)
}
}
#[allow(missing_docs)]
#[derive(Eq, Ord, PartialEq, PartialOrd, Hash, Copy, Clone, Debug)]
pub struct ImageInfoCommon<F> {
pub xoffset: Size,
pub yoffset: Size,
pub zoffset: Size,
pub width: Size,
pub height: Size,
pub depth: Size,
pub format: F,
pub mipmap: Level,
}
pub type RawImageInfo = ImageInfoCommon<format::Format>;
pub type NewImageInfo = ImageInfoCommon<()>;
impl<F> ImageInfoCommon<F> {
pub fn get_texel_count(&self) -> usize {
self.width as usize *
self.height as usize *
self.depth as usize
}
pub fn convert<T>(&self, new_format: T) -> ImageInfoCommon<T> {
ImageInfoCommon {
xoffset: self.xoffset,
yoffset: self.yoffset,
zoffset: self.zoffset,
width: self.width,
height: self.height,
depth: self.depth,
format: new_format,
mipmap: self.mipmap,
}
}
pub fn is_inside(&self, (w, h, d, aa): Dimensions) -> bool {
aa == AaMode::Single &&
self.xoffset + self.width <= w &&
self.yoffset + self.height <= h &&
self.zoffset + self.depth <= d
}
}
#[derive(Eq, Ord, PartialEq, PartialOrd, Hash, Copy, Clone, Debug)]
pub enum WrapMode {
Tile,
Mirror,
Clamp,
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd)]
pub struct Lod(i16);
impl From<f32> for Lod {
fn from(v: f32) -> Lod {
Lod((v * 8.0) as i16)
}
}
impl Into<f32> for Lod {
fn into(self) -> f32 {
self.0 as f32 / 8.0
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd)]
pub struct SamplerInfo {
pub filtering: FilterMethod,
pub wrap_mode: (WrapMode, WrapMode, WrapMode),
pub lod_bias: Lod,
pub lod_range: (Lod, Lod),
pub comparison: Option<state::Comparison>,
}
impl SamplerInfo {
pub fn new(filtering: FilterMethod, wrap: WrapMode) -> SamplerInfo {
SamplerInfo {
filtering: filtering,
wrap_mode: (wrap, wrap, wrap),
lod_bias: Lod(0),
lod_range: (Lod(-8000), Lod(8000)),
comparison: None,
}
}
}
#[allow(missing_docs)]
#[derive(Eq, Ord, PartialEq, PartialOrd, Hash, Copy, Clone, Debug)]
pub struct Descriptor {
pub kind: Kind,
pub levels: Level,
pub format: format::SurfaceType,
pub bind: Bind,
}
impl Descriptor {
pub fn to_image_info(&self, mip: Level) -> NewImageInfo {
let (w, h, d, _) = self.kind.get_level_dimensions(mip);
ImageInfoCommon {
xoffset: 0,
yoffset: 0,
zoffset: 0,
width: w,
height: h,
depth: d,
format: (),
mipmap: mip,
}
}
pub fn to_raw_image_info(&self, cty: format::ChannelType, mip: Level) -> RawImageInfo {
let format = format::Format(self.format, cty.into());
self.to_image_info(mip).convert(format)
}
}
#[allow(missing_docs)]
#[derive(Eq, Ord, PartialEq, PartialOrd, Hash, Copy, Clone, Debug)]
pub struct ViewDesc {
pub channel: format::ChannelType,
pub min: Level,
pub max: Level,
pub swizzle: format::Swizzle,
}