sierra 0.6.0

Vulkan-lite graphics API
Documentation
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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
use std::{
    cmp::{Ord, Ordering},
    convert::TryInto,
    fmt::Debug,
    ops::{Deref, DerefMut},
};

use arrayvec::ArrayVec;
use num_traits::{One, ToPrimitive, Zero};

/// Image size is defined to `u32` which is standard for graphics API of today.
pub type ImageSize = u32;

/// Multidimensional size.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[repr(transparent)]
pub struct Extent<const D: usize, T = ImageSize> {
    values: [T; D],
}

pub type Extent1<T = ImageSize> = Extent<1, T>;
pub type Extent2<T = ImageSize> = Extent<2, T>;
pub type Extent3<T = ImageSize> = Extent<3, T>;

#[derive(Debug)]
#[cfg_attr(feature = "serde-1", derive(serde::Serialize, serde::Deserialize))]
#[repr(C)]
pub struct Width<T> {
    pub width: T,
}

impl<T> Deref for Extent1<T> {
    type Target = Width<T>;

    #[inline]
    fn deref(&self) -> &Width<T> {
        unsafe { &*(self as *const _ as *const _) }
    }
}

impl<T> DerefMut for Extent1<T> {
    #[inline]
    fn deref_mut(&mut self) -> &mut Width<T> {
        unsafe { &mut *(self as *mut _ as *mut _) }
    }
}

#[cfg(feature = "serde-1")]
impl<T> serde::Serialize for Extent1<T>
where
    T: serde::Serialize,
{
    #[inline]
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        self.deref().serialize(serializer)
    }
}

#[cfg(feature = "serde-1")]
impl<'de, T> serde::Deserialize<'de> for Extent1<T>
where
    T: serde::Deserialize<'de>,
{
    #[inline]
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let repr = Width::deserialize(deserializer)?;
        Ok(Extent {
            values: [repr.width],
        })
    }
}

impl<T> Extent1<T> {
    #[inline]
    pub const fn new(width: T) -> Self {
        Extent { values: [width] }
    }
}

#[derive(Debug)]
#[cfg_attr(feature = "serde-1", derive(serde::Serialize, serde::Deserialize))]
#[repr(C)]
pub struct WidthHeight<T> {
    pub width: T,
    pub height: T,
}

impl<T> Deref for Extent2<T> {
    type Target = WidthHeight<T>;

    #[inline]
    fn deref(&self) -> &WidthHeight<T> {
        unsafe { &*(self as *const _ as *const _) }
    }
}

impl<T> DerefMut for Extent2<T> {
    #[inline]
    fn deref_mut(&mut self) -> &mut WidthHeight<T> {
        unsafe { &mut *(self as *mut _ as *mut _) }
    }
}

#[cfg(feature = "serde-1")]
impl<T> serde::Serialize for Extent2<T>
where
    T: serde::Serialize,
{
    #[inline]
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        self.deref().serialize(serializer)
    }
}

#[cfg(feature = "serde-1")]
impl<'de, T> serde::Deserialize<'de> for Extent2<T>
where
    T: serde::Deserialize<'de>,
{
    #[inline]
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let repr = WidthHeight::deserialize(deserializer)?;
        Ok(Extent {
            values: [repr.width, repr.height],
        })
    }
}

impl<T> Extent2<T> {
    #[inline]
    pub const fn new(width: T, height: T) -> Self {
        Extent {
            values: [width, height],
        }
    }
}

#[derive(Debug)]
#[cfg_attr(feature = "serde-1", derive(serde::Serialize, serde::Deserialize))]
#[repr(C)]
pub struct WidthHeightDepth<T> {
    pub width: T,
    pub height: T,
    pub depth: T,
}

impl<T> Deref for Extent3<T> {
    type Target = WidthHeightDepth<T>;

    #[inline]
    fn deref(&self) -> &WidthHeightDepth<T> {
        unsafe { &*(self as *const _ as *const _) }
    }
}

impl<T> DerefMut for Extent3<T> {
    #[inline]
    fn deref_mut(&mut self) -> &mut WidthHeightDepth<T> {
        unsafe { &mut *(self as *mut _ as *mut _) }
    }
}

#[cfg(feature = "serde-1")]
impl<T> serde::Serialize for Extent3<T>
where
    T: serde::Serialize,
{
    #[inline]
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        self.deref().serialize(serializer)
    }
}

#[cfg(feature = "serde-1")]
impl<'de, T> serde::Deserialize<'de> for Extent3<T>
where
    T: serde::Deserialize<'de>,
{
    #[inline]
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let repr = WidthHeightDepth::deserialize(deserializer)?;
        Ok(Extent {
            values: [repr.width, repr.height, repr.depth],
        })
    }
}

impl<T> Extent3<T> {
    #[inline]
    pub const fn new(width: T, height: T, depth: T) -> Self {
        Extent {
            values: [width, height, depth],
        }
    }
}

impl<T> Extent1<T>
where
    T: One,
{
    #[inline]
    pub fn into_1d(self) -> Extent1<T> {
        self
    }

    #[inline]
    pub fn into_2d(self) -> Extent2<T> {
        let [width] = self.values;

        Extent {
            values: [width, T::one()],
        }
    }

    #[inline]
    pub fn into_3d(self) -> Extent3<T> {
        let [width] = self.values;

        Extent {
            values: [width, T::one(), T::one()],
        }
    }
}

impl<T> Extent2<T>
where
    T: One,
{
    #[inline]
    pub fn into_1d(self) -> Extent1<T> {
        let [width, _] = self.values;

        Extent { values: [width] }
    }

    #[inline]
    pub fn into_2d(self) -> Extent2<T> {
        self
    }

    #[inline]
    pub fn into_3d(self) -> Extent3<T> {
        let [width, height] = self.values;

        Extent {
            values: [width, height, T::one()],
        }
    }
}

impl<T> Extent3<T>
where
    T: One,
{
    #[inline]
    pub fn into_1d(self) -> Extent1<T> {
        let [width, _, _] = self.values;

        Extent { values: [width] }
    }

    #[inline]
    pub fn into_2d(self) -> Extent2<T> {
        let [width, height, _] = self.values;

        Extent {
            values: [width, height],
        }
    }

    #[inline]
    pub fn into_3d(self) -> Extent3<T> {
        self
    }
}

impl<const D: usize, T> Extent<D, T> {
    #[inline]
    pub fn pick<F>(self, rhs: Self, mut picker: F) -> Self
    where
        F: FnMut(T, T) -> T,
    {
        let mut lhs = ArrayVec::<T, D>::from(self.values);
        let mut rhs = ArrayVec::<T, D>::from(rhs.values);

        let mut values = [(); D].map(|()| picker(lhs.pop().unwrap(), rhs.pop().unwrap()));
        values.reverse();

        Extent { values }
    }

    #[inline]
    pub fn min(self, rhs: Self) -> Self
    where
        T: Ord,
    {
        self.pick(rhs, std::cmp::min)
    }

    #[inline]
    pub fn max(self, rhs: Self) -> Self
    where
        T: Ord,
    {
        self.pick(rhs, std::cmp::max)
    }

    #[inline]
    pub fn ones() -> Self
    where
        T: One,
    {
        Extent {
            values: array_fu::array![T::one(); D],
        }
    }

    #[inline]
    pub fn zeros() -> Self
    where
        T: Zero,
    {
        Extent {
            values: array_fu::array![T::zero(); D],
        }
    }
}

impl<T> Extent2<T>
where
    T: ToPrimitive,
{
    #[inline]
    pub fn aspect_ratio(&self) -> f32 {
        self.width.to_f32().unwrap_or(0.0) / self.height.to_f32().unwrap_or(std::f32::INFINITY)
    }
}

impl<const D: usize, T> PartialOrd for Extent<D, T>
where
    T: PartialOrd,
{
    #[inline]
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        merge_ordering_iter(
            self.values
                .iter()
                .zip(other.values.iter())
                .map(|(lhs, rhs)| lhs.partial_cmp(rhs)),
        )
    }
}

/// Image offset is defined to `i32` which is standard for graphics API today.
pub type ImageOffset = i32;

/// Multidimensional offset.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]

pub struct Offset<const D: usize, T = ImageOffset> {
    values: [T; D],
}

pub type Offset1<T = ImageOffset> = Offset<1, T>;
pub type Offset2<T = ImageOffset> = Offset<2, T>;
pub type Offset3<T = ImageOffset> = Offset<3, T>;

#[derive(Debug)]
#[cfg_attr(feature = "serde-1", derive(serde::Serialize, serde::Deserialize))]
#[repr(C)]
pub struct X<T> {
    /// Width offset.
    pub x: T,
}

impl<T> Deref for Offset1<T> {
    type Target = X<T>;

    #[inline]
    fn deref(&self) -> &X<T> {
        unsafe { &*(self as *const _ as *const _) }
    }
}

impl<T> DerefMut for Offset1<T> {
    #[inline]
    fn deref_mut(&mut self) -> &mut X<T> {
        unsafe { &mut *(self as *mut _ as *mut _) }
    }
}

#[cfg(feature = "serde-1")]
impl serde::Serialize for Offset1<ImageOffset> {
    #[inline]
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        self.deref().serialize(serializer)
    }
}

#[cfg(feature = "serde-1")]
impl<'de> serde::Deserialize<'de> for Offset1<ImageOffset> {
    #[inline]
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let repr = X::deserialize(deserializer)?;
        Ok(Offset { values: [repr.x] })
    }
}

impl<T> Offset1<T> {
    #[inline]
    pub const fn new(x: T) -> Self {
        Offset { values: [x] }
    }
}

#[derive(Debug)]
#[cfg_attr(feature = "serde-1", derive(serde::Serialize, serde::Deserialize))]
#[repr(C)]
pub struct XY<T> {
    /// Width offset.
    pub x: T,

    /// Height offset.
    pub y: T,
}

impl<T> Deref for Offset2<T> {
    type Target = XY<T>;

    #[inline]
    fn deref(&self) -> &XY<T> {
        unsafe { &*(self as *const _ as *const _) }
    }
}

impl<T> DerefMut for Offset2<T> {
    #[inline]
    fn deref_mut(&mut self) -> &mut XY<T> {
        unsafe { &mut *(self as *mut _ as *mut _) }
    }
}

#[cfg(feature = "serde-1")]
impl serde::Serialize for Offset2<ImageOffset> {
    #[inline]
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        self.deref().serialize(serializer)
    }
}

#[cfg(feature = "serde-1")]
impl<'de> serde::Deserialize<'de> for Offset2<ImageOffset> {
    #[inline]
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let repr = XY::deserialize(deserializer)?;
        Ok(Offset {
            values: [repr.x, repr.y],
        })
    }
}

impl<T> Offset2<T> {
    #[inline]
    pub const fn new(x: T, y: T) -> Self {
        Offset { values: [x, y] }
    }
}

#[derive(Debug)]
#[cfg_attr(feature = "serde-1", derive(serde::Serialize, serde::Deserialize))]
#[repr(C)]
pub struct XYZ<T> {
    /// Width offset.
    pub x: T,

    /// Height offset.
    pub y: T,

    /// Depth offset.
    pub z: T,
}

impl<T> Deref for Offset3<T> {
    type Target = XYZ<T>;

    #[inline]
    fn deref(&self) -> &XYZ<T> {
        unsafe { &*(self as *const _ as *const _) }
    }
}

impl<T> DerefMut for Offset3<T> {
    #[inline]
    fn deref_mut(&mut self) -> &mut XYZ<T> {
        unsafe { &mut *(self as *mut _ as *mut _) }
    }
}

#[cfg(feature = "serde-1")]
impl serde::Serialize for Offset3<ImageOffset> {
    #[inline]
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        self.deref().serialize(serializer)
    }
}

#[cfg(feature = "serde-1")]
impl<'de> serde::Deserialize<'de> for Offset3<ImageOffset> {
    #[inline]
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let repr = XYZ::deserialize(deserializer)?;
        Ok(Offset {
            values: [repr.x, repr.y, repr.z],
        })
    }
}

impl<T> Offset3<T> {
    #[inline]
    pub const fn new(x: T, y: T, z: T) -> Self {
        Offset { values: [x, y, z] }
    }
}

macro_rules! zero_offset {
    ($($t:ty = $zero:expr),* $(,)?) => {
        $(
            impl<const D: usize> Offset<D, $t> {
                pub const ZERO: Self = Offset { values: [$zero; D] };
            }
        )*
    };
}

zero_offset! {
    i8 = 0,
    i16 = 0,
    i32 = 0,
    i64 = 0,
    isize = 0,
    u8 = 0,
    u16 = 0,
    u32 = 0,
    u64 = 0,
    usize = 0,
    f32 = 0.0,
    f64 = 0.0,
}

impl<const D: usize, T> Offset<D, T>
where
    T: Zero,
{
    #[inline]
    pub fn zeros() -> Self {
        Offset {
            values: array_fu::array![T::zero(); D],
        }
    }
}

impl<const D: usize, T> Offset<D, T> {
    #[inline]
    pub fn from_extent<U>(extent: Extent<D, U>) -> Result<Self, U::Error>
    where
        U: TryInto<T>,
    {
        let mut result = ArrayVec::<T, D>::new();

        for value in extent.values {
            result.push(value.try_into()?);
        }

        Ok(Offset {
            values: match result.into_inner() {
                Ok(values) => values,
                Err(_) => unreachable!(),
            },
        })
    }
}

#[inline]
fn merge_ordering(left: Ordering, right: Ordering) -> Option<Ordering> {
    match (left, right) {
        (Ordering::Equal, right) => Some(right),
        (left, Ordering::Equal) => Some(left),
        (Ordering::Less, Ordering::Less) => Some(Ordering::Less),
        (Ordering::Greater, Ordering::Greater) => Some(Ordering::Greater),
        _ => None,
    }
}

#[inline]
fn merge_ordering_iter(mut iter: impl Iterator<Item = Option<Ordering>>) -> Option<Ordering> {
    iter.try_fold(Ordering::Equal, |acc, item| merge_ordering(acc, item?))
}

#[allow(missing_debug_implementations)]
pub struct MinimalExtent<const D: usize, T> {
    values: Option<[T; D]>,
}

impl<const D: usize, T> MinimalExtent<D, T> {
    #[inline]
    pub const fn new() -> Self {
        MinimalExtent { values: None }
    }

    #[inline]
    pub fn add(&mut self, extent: Extent<D, T>)
    where
        T: Ord,
    {
        match &mut self.values {
            None => self.values = Some(extent.values),
            Some(values) => {
                for (value, other) in values.iter_mut().zip(extent.values) {
                    if Ord::cmp(&*value, &other) == Ordering::Greater {
                        *value = other
                    }
                }
            }
        }
    }

    #[inline]
    pub fn get(self) -> Extent<D, T>
    where
        T: One,
    {
        self.get_or(Extent {
            values: array_fu::array![T::one(); D],
        })
    }

    #[inline]
    pub fn get_or(self, default: Extent<D, T>) -> Extent<D, T>
    where
        T: One,
    {
        match self.values {
            None => default,
            Some(values) => Extent { values },
        }
    }
}

pub fn minimal_extent<const D: usize, T>() -> MinimalExtent<D, T> {
    MinimalExtent::new()
}