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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
use std::error::Error;
use std::{fmt, cmp, hash};
use memory::{Bind, Usage};
use {format, state, target, Resources};
pub use target::{Layer, Level};
pub const MAX_LEVEL: Level = 15;
#[derive(Debug)]
pub struct Raw<R: Resources> {
resource: R::Texture,
info: Info,
}
impl<R: Resources> Raw<R> {
#[doc(hidden)]
pub fn new(resource: R::Texture, info: Info) -> Self {
Raw {
resource: resource,
info: info,
}
}
#[doc(hidden)]
pub fn resource(&self) -> &R::Texture { &self.resource }
pub fn get_info(&self) -> &Info { &self.info }
}
impl<R: Resources + cmp::PartialEq> cmp::PartialEq for Raw<R> {
fn eq(&self, other: &Self) -> bool {
self.resource().eq(other.resource())
}
}
impl<R: Resources + cmp::Eq> cmp::Eq for Raw<R> {}
impl<R: Resources + hash::Hash> hash::Hash for Raw<R> {
fn hash<H: hash::Hasher>(&self, state: &mut H) {
self.resource().hash(state);
}
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum CreationError {
Format(format::SurfaceType, Option<format::ChannelType>),
Kind,
Samples(AaMode),
Size(Size),
Data(usize),
Usage(Usage),
}
impl fmt::Display for CreationError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
CreationError::Format(surf, chan) => write!(f, "{}: ({:?}, {:?})",
self.description(), surf, chan),
CreationError::Samples(aa) => write!(f, "{}: {:?}", self.description(), aa),
CreationError::Size(size) => write!(f, "{}: {}", self.description(), size),
CreationError::Data(data) => write!(f, "{}: {}", self.description(), data),
CreationError::Usage(usage) => write!(f, "{}: {:?}", self.description(), usage),
_ => write!(f, "{}", self.description()),
}
}
}
impl Error for CreationError {
fn description(&self) -> &str {
match *self {
CreationError::Format(..) => "Failed to map a given format to the device",
CreationError::Kind => "The kind doesn't support a particular operation",
CreationError::Samples(_) => "Failed to map a given multisampled kind to the device",
CreationError::Size(_) => "Unsupported size in one of the dimensions",
CreationError::Data(_) => "The given data has a different size than the target texture slice",
CreationError::Usage(_) => "The expected texture usage mode is not supported by a graphic API",
}
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum LayerError {
NotExpected(Kind),
OutOfBounds(target::Layer, target::Layer),
}
impl fmt::Display for LayerError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
LayerError::NotExpected(kind) => write!(f, "{}: {:?}", self.description(), kind),
LayerError::OutOfBounds(layer, count) => write!(f, "{}: {}/{}", self.description(), layer, count),
}
}
}
impl Error for LayerError {
fn description(&self) -> &str {
match *self {
LayerError::NotExpected(_) => "The source texture kind doesn't support array slices",
LayerError::OutOfBounds(_, _) => "Selected layer is outside of the provided range",
}
}
}
pub type Size = u16;
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)]
#[repr(u8)]
pub enum CubeFace {
PosX,
NegX,
PosY,
NegY,
PosZ,
NegZ,
}
pub const CUBE_FACES: [CubeFace; 6] = [
CubeFace::PosX, CubeFace::NegX,
CubeFace::PosY, CubeFace::NegY,
CubeFace::PosZ, CubeFace::NegZ,
];
#[derive(Eq, Ord, PartialEq, PartialOrd, Hash, Copy, Clone, Debug)]
pub enum Kind {
D1(Size),
D1Array(Size, Layer),
D2(Size, Size, AaMode),
D2Array(Size, Size, Layer, AaMode),
D3(Size, Size, Size),
Cube(Size),
CubeArray(Size, Layer),
}
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) => (w, w, 6, s0),
Kind::CubeArray(w, a) => (w, w, 6 * (a as Size), s0)
}
}
pub fn get_level_dimensions(&self, level: Level) -> Dimensions {
use std::cmp::{max, min};
let map = |val| max(min(val, 1), val >> min(level, MAX_LEVEL));
let (w, h, d, _) = self.get_dimensions();
(map(w), map(h), map(d), AaMode::Single)
}
pub fn get_num_levels(&self) -> Level {
use std::cmp::max;
let (w, h, d, aa) = self.get_dimensions();
let dominant = max(max(w, h), d);
if aa == AaMode::Single {
(1..).find(|level| dominant>>level <= 1).unwrap()
}else {
1
}
}
pub fn get_num_slices(&self) -> Option<Layer> {
match *self {
Kind::D1(..) | Kind::D2(..) | Kind::D3(..) | Kind::Cube(..) => None,
Kind::D1Array(_, a) => Some(a),
Kind::D2Array(_, _, a, _) => Some(a),
Kind::CubeArray(_, a) => Some(a),
}
}
pub fn is_cube(&self) -> bool {
match *self {
Kind::Cube(_) | Kind::CubeArray(_, _) => true,
_ => false,
}
}
}
#[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 {
use std::cmp::max;
max(1, self.width) as usize *
max(1, self.height) as usize *
max(1, 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,
Border,
}
#[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 PackedColor(pub u32);
impl From<[f32; 4]> for PackedColor {
fn from(c: [f32; 4]) -> PackedColor {
PackedColor(c.iter().rev().fold(0, |u, &c| {
(u<<8) + (c * 255.0 + 0.5) as u32
}))
}
}
impl Into<[f32; 4]> for PackedColor {
fn into(self) -> [f32; 4] {
let mut out = [0.0; 4];
for i in 0 .. 4 {
let byte = (self.0 >> (i<<3)) & 0xFF;
out[i] = (byte as f32 + 0.5) / 255.0;
}
out
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd)]
pub struct SamplerInfo {
pub filter: FilterMethod,
pub wrap_mode: (WrapMode, WrapMode, WrapMode),
pub lod_bias: Lod,
pub lod_range: (Lod, Lod),
pub comparison: Option<state::Comparison>,
pub border: PackedColor,
}
impl SamplerInfo {
pub fn new(filter: FilterMethod, wrap: WrapMode) -> SamplerInfo {
SamplerInfo {
filter: filter,
wrap_mode: (wrap, wrap, wrap),
lod_bias: Lod(0),
lod_range: (Lod(-8000), Lod(8000)),
comparison: None,
border: PackedColor(0),
}
}
}
#[allow(missing_docs)]
#[derive(Eq, Ord, PartialEq, PartialOrd, Hash, Copy, Clone, Debug)]
pub struct Info {
pub kind: Kind,
pub levels: Level,
pub format: format::SurfaceType,
pub bind: Bind,
pub usage: Usage,
}
impl Info {
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 ResourceDesc {
pub channel: format::ChannelType,
pub layer: Option<Layer>,
pub min: Level,
pub max: Level,
pub swizzle: format::Swizzle,
}
#[allow(missing_docs)]
#[derive(Eq, Ord, PartialEq, PartialOrd, Hash, Copy, Clone, Debug)]
pub struct RenderDesc {
pub channel: format::ChannelType,
pub level: Level,
pub layer: Option<Layer>,
}
bitflags!(
pub flags DepthStencilFlags: u8 {
const RO_DEPTH = 0x1,
const RO_STENCIL = 0x2,
const RO_DEPTH_STENCIL = 0x3,
}
);
#[allow(missing_docs)]
#[derive(Eq, Ord, PartialEq, PartialOrd, Hash, Copy, Clone, Debug)]
pub struct DepthStencilDesc {
pub level: Level,
pub layer: Option<Layer>,
pub flags: DepthStencilFlags,
}
impl From<RenderDesc> for DepthStencilDesc {
fn from(rd: RenderDesc) -> DepthStencilDesc {
DepthStencilDesc {
level: rd.level,
layer: rd.layer,
flags: DepthStencilFlags::empty(),
}
}
}