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
//! Immutable or dynamic vertex and index data.

use crate::math::prelude::Aabb3;
use crate::video::assets::shader::Attribute;
use crate::video::errors::{Error, Result};
use crate::video::MAX_VERTEX_ATTRIBUTES;
use smallvec::SmallVec;

impl_handle!(MeshHandle);

/// The setup parameters of mesh object.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct MeshParams {
    /// Usage hints.
    pub hint: MeshHint,
    /// How a single vertex structure looks like.
    pub layout: VertexLayout,
    /// Index format
    pub index_format: IndexFormat,
    /// How the input vertex data is used to assemble primitives.
    pub primitive: MeshPrimitive,
    /// The number of vertices in this mesh.
    pub num_verts: usize,
    /// The number of indices in this mesh.
    pub num_idxes: usize,
    /// The start indices of sub-meshes.
    pub sub_mesh_offsets: SmallVec<[usize; 8]>,
    /// Trivial bounding box of vertices.
    pub aabb: Aabb3<f32>,
}

/// Continuous data of vertices and its indices.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct MeshData {
    /// The bytes of vertices.
    pub vptr: Box<[u8]>,
    /// The bytes of indices.
    pub iptr: Box<[u8]>,
}

impl Default for MeshParams {
    fn default() -> Self {
        MeshParams {
            hint: MeshHint::Immutable,
            layout: VertexLayout::default(),
            index_format: IndexFormat::U16,
            primitive: MeshPrimitive::Triangles,
            num_verts: 0,
            num_idxes: 0,
            aabb: Aabb3::zero(),
            sub_mesh_offsets: SmallVec::new(),
        }
    }
}

impl MeshParams {
    pub fn validate(&self, data: Option<&MeshData>) -> Result<()> {
        if let Some(v) = data {
            if v.vptr.len() > self.vertex_buffer_len() {
                return Err(Error::OutOfBounds);
            }

            if v.iptr.len() > self.index_buffer_len() {
                return Err(Error::OutOfBounds);
            }
        }

        for v in &self.sub_mesh_offsets {
            if *v >= self.num_idxes {
                return Err(Error::OutOfBounds);
            }
        }

        Ok(())
    }

    #[inline]
    pub fn vertex_buffer_len(&self) -> usize {
        self.num_verts * self.layout.stride() as usize
    }

    #[inline]
    pub fn index_buffer_len(&self) -> usize {
        self.num_idxes * self.index_format.stride() as usize
    }
}

/// Mesh index.
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum MeshIndex {
    SubMesh(usize),
    Ptr(usize, usize),
    All,
}

/// Hint abouts the intended update strategy of the data.
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone, Copy)]
pub enum MeshHint {
    /// The resource is initialized with data and cannot be changed later, this
    /// is the most common and most efficient usage. Optimal for render targets
    /// and resourced memory.
    Immutable,
    /// The resource is initialized without data, but will be be updated by the
    /// CPU in each frame.
    Stream,
    /// The resource is initialized without data and will be written by the CPU
    /// before use, updates will be infrequent.
    Dynamic,
}

/// Defines how the input vertex data is used to assemble primitives.
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
pub enum MeshPrimitive {
    /// Separate points.
    Points,
    /// Separate lines.
    Lines,
    /// Line strips.
    LineStrip,
    /// Separate triangles.
    Triangles,
    /// Triangle strips.
    TriangleStrip,
}

impl MeshPrimitive {
    pub fn assemble(self, indices: u32) -> u32 {
        match self {
            MeshPrimitive::Points => indices,
            MeshPrimitive::Lines => indices / 2,
            MeshPrimitive::LineStrip => indices - 1,
            MeshPrimitive::Triangles => indices / 3,
            MeshPrimitive::TriangleStrip => indices - 2,
        }
    }

    pub fn assemble_triangles(self, indices: u32) -> u32 {
        match self {
            MeshPrimitive::Points | MeshPrimitive::Lines | MeshPrimitive::LineStrip => 0,
            MeshPrimitive::Triangles => indices / 3,
            MeshPrimitive::TriangleStrip => indices - 2,
        }
    }
}

/// Vertex indices can be either 16- or 32-bit. You should always prefer
/// 16-bit indices over 32-bit indices, since the latter may have performance
/// penalties on some platforms, and they take up twice as much memory.
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone, Copy)]
pub enum IndexFormat {
    U16,
    U32,
}

impl IndexFormat {
    pub fn stride(self) -> usize {
        match self {
            IndexFormat::U16 => 2,
            IndexFormat::U32 => 4,
        }
    }

    pub fn encode<T>(values: &[T]) -> &[u8]
    where
        T: Copy,
    {
        let len = values.len() * ::std::mem::size_of::<T>();
        unsafe { ::std::slice::from_raw_parts(values.as_ptr() as *const u8, len) }
    }
}

/// The data type in the vertex component.
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone, Copy)]
pub enum VertexFormat {
    Byte,
    UByte,
    Short,
    UShort,
    Float,
}

/// The details of a vertex attribute.
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone, Copy)]
pub struct VertexAttribute {
    /// The name of this description.
    pub name: Attribute,
    /// The data type of each component of this element.
    pub format: VertexFormat,
    /// The number of components per generic vertex element.
    pub size: u8,
    /// Whether fixed-point data values should be normalized.
    pub normalized: bool,
}

impl Default for VertexAttribute {
    fn default() -> Self {
        VertexAttribute {
            name: Attribute::Position,
            format: VertexFormat::Byte,
            size: 0,
            normalized: false,
        }
    }
}

/// `VertexLayout` defines how a single vertex structure looks like.  A vertex
/// layout is a collection of vertex components, and each vertex component
/// consists of a vertex attribute and the vertex format.
#[derive(Serialize, Deserialize, Debug, Default, PartialEq, Eq, Clone, Copy)]
pub struct VertexLayout {
    stride: u8,
    len: u8,
    offset: [u8; MAX_VERTEX_ATTRIBUTES],
    elements: [VertexAttribute; MAX_VERTEX_ATTRIBUTES],
}

impl VertexLayout {
    /// Creates a new an empty `VertexLayoutBuilder`.
    #[inline]
    pub fn build() -> VertexLayoutBuilder {
        VertexLayoutBuilder::new()
    }

    /// Stride of single vertex structure.
    #[inline]
    pub fn stride(&self) -> u8 {
        self.stride
    }

    /// Returns the number of elements in the layout.
    #[inline]
    pub fn len(&self) -> u8 {
        self.len
    }

    /// Checks if the vertex layout is empty.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.len == 0
    }

    /// Relative element offset from the layout.
    pub fn offset(&self, name: Attribute) -> Option<u8> {
        for i in 0..self.elements.len() {
            match self.elements[i].name {
                v if v == name => return Some(self.offset[i]),
                _ => (),
            }
        }

        None
    }

    /// Returns named `Attribute` from the layout.
    pub fn element(&self, name: Attribute) -> Option<VertexAttribute> {
        for i in 0..self.elements.len() {
            match self.elements[i].name {
                v if v == name => return Some(self.elements[i]),
                _ => (),
            }
        }

        None
    }
}

/// Helper structure to build a vertex layout.
#[derive(Default)]
pub struct VertexLayoutBuilder(VertexLayout);

impl VertexLayoutBuilder {
    #[inline]
    pub fn new() -> Self {
        Default::default()
    }

    pub fn with(
        mut self,
        name: Attribute,
        format: VertexFormat,
        size: u8,
        normalized: bool,
    ) -> Self {
        assert!(size > 0 && size <= 4);

        let desc = VertexAttribute {
            name,
            format,
            size,
            normalized,
        };

        for i in 0..self.0.len {
            let i = i as usize;
            if self.0.elements[i].name == name {
                self.0.elements[i] = desc;
                return self;
            }
        }

        assert!((self.0.len as usize) < MAX_VERTEX_ATTRIBUTES);
        self.0.elements[self.0.len as usize] = desc;
        self.0.len += 1;

        self
    }

    #[inline]
    pub fn finish(mut self) -> VertexLayout {
        self.0.stride = 0;
        for i in 0..self.0.len {
            let i = i as usize;
            let len = self.0.elements[i].size * size_of_vertex(self.0.elements[i].format);
            self.0.offset[i] = self.0.stride;
            self.0.stride += len;
        }
        self.0
    }
}

fn size_of_vertex(format: VertexFormat) -> u8 {
    match format {
        VertexFormat::Byte | VertexFormat::UByte => 1,
        VertexFormat::Short | VertexFormat::UShort => 2,
        VertexFormat::Float => 4,
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn basic() {
        let layout = VertexLayout::build()
            .with(Attribute::Position, VertexFormat::Float, 3, true)
            .with(Attribute::Texcoord0, VertexFormat::Float, 2, true)
            .finish();

        assert_eq!(layout.stride(), 20);
        assert_eq!(layout.offset(Attribute::Position), Some(0));
        assert_eq!(layout.offset(Attribute::Texcoord0), Some(12));
        assert_eq!(layout.offset(Attribute::Normal), None);

        let element = layout.element(Attribute::Position).unwrap();
        assert_eq!(element.format, VertexFormat::Float);
        assert_eq!(element.size, 3);
        assert_eq!(element.normalized, true);
        assert_eq!(layout.element(Attribute::Normal), None);
    }

    #[test]
    fn rewrite() {
        let layout = VertexLayout::build()
            .with(Attribute::Position, VertexFormat::Byte, 1, false)
            .with(Attribute::Texcoord0, VertexFormat::Float, 2, true)
            .with(Attribute::Position, VertexFormat::Float, 3, true)
            .finish();

        assert_eq!(layout.stride(), 20);
        assert_eq!(layout.offset(Attribute::Position), Some(0));
        assert_eq!(layout.offset(Attribute::Texcoord0), Some(12));
        assert_eq!(layout.offset(Attribute::Normal), None);

        let element = layout.element(Attribute::Position).unwrap();
        assert_eq!(element.format, VertexFormat::Float);
        assert_eq!(element.size, 3);
        assert_eq!(element.normalized, true);
        assert_eq!(layout.element(Attribute::Normal), None);
    }
}

#[macro_use]
pub mod macros {
    use super::*;

    #[doc(hidden)]
    #[derive(Default)]
    pub struct CustomVertexLayoutBuilder(VertexLayout);

    impl CustomVertexLayoutBuilder {
        #[inline]
        pub fn new() -> Self {
            Default::default()
        }

        pub fn with(
            &mut self,
            name: Attribute,
            format: VertexFormat,
            size: u8,
            normalized: bool,
            offset_of_field: u8,
        ) -> &mut Self {
            assert!(size > 0 && size <= 4);

            let desc = VertexAttribute {
                name,
                format,
                size,
                normalized,
            };

            for i in 0..self.0.len {
                let i = i as usize;
                if self.0.elements[i].name == name {
                    self.0.elements[i] = desc;
                    return self;
                }
            }

            assert!((self.0.len as usize) < MAX_VERTEX_ATTRIBUTES);
            self.0.offset[self.0.len as usize] = offset_of_field;
            self.0.elements[self.0.len as usize] = desc;
            self.0.len += 1;

            self
        }

        #[inline]
        pub fn finish(&mut self, stride: u8) -> VertexLayout {
            self.0.stride = stride;
            self.0
        }
    }

    #[macro_export]
    macro_rules! impl_vertex {
        ($name: ident { $($field: ident => [$attribute: tt; $format: tt; $size: tt; $normalized: tt],)* }) => (
            #[repr(C)]
            #[derive(Debug, Copy, Clone, Default)]
            pub struct $name {
                $($field: $crate::impl_vertex_field!{VertexFormat::$format, $size}, )*
            }

            impl $name {
                #[allow(dead_code)]
                pub fn new($($field: $crate::impl_vertex_field!{VertexFormat::$format, $size}, ) *) -> Self {
                    $name {
                        $($field: $field,)*
                    }
                }

                #[allow(dead_code)]
                pub fn layout() -> $crate::video::assets::mesh::VertexLayout {
                    let mut builder = $crate::video::assets::mesh::macros::CustomVertexLayoutBuilder::new();

                    $( builder.with(
                        $crate::video::assets::shader::Attribute::$attribute,
                        $crate::video::assets::mesh::VertexFormat::$format,
                        $size,
                        $normalized,
                        $crate::offset_of!($name, $field) as u8); ) *

                    builder.finish(::std::mem::size_of::<$name>() as u8)
                }

                #[allow(dead_code)]
                pub fn attributes() -> $crate::video::assets::shader::AttributeLayout {
                    let builder = $crate::video::assets::shader::AttributeLayoutBuilder::new();

                    $(
                        let builder = builder.with(
                            $crate::video::assets::shader::Attribute::$attribute,
                            $size);
                    ) *

                    builder.finish()
                }

                #[allow(dead_code)]
                pub fn encode(values: &[Self]) -> &[u8] {
                    let len = values.len() * ::std::mem::size_of::<Self>();
                    unsafe { ::std::slice::from_raw_parts(values.as_ptr() as *const u8, len) }
                }
            }
        )
    }

    #[doc(hidden)]
    #[macro_export]
    macro_rules! offset_of {
        ($ty:ty, $field:ident) => {{
            use std;
            let ptr: *const $ty = std::ptr::null();
            unsafe { &(*ptr).$field as *const _ as usize }
        }};
    }

    #[doc(hidden)]
    #[macro_export]
    macro_rules! impl_vertex_field {
        (VertexFormat::Byte,2) => {
            [i8; 2]
        };
        (VertexFormat::Byte,3) => {
            [i8; 3]
        };
        (VertexFormat::Byte,4) => {
            [i8; 4]
        };
        (VertexFormat::UByte,2) => {
            [u8; 2]
        };
        (VertexFormat::UByte,3) => {
            [u8; 3]
        };
        (VertexFormat::UByte,4) => {
            [u8; 4]
        };
        (VertexFormat::Short,2) => {
            [i16; 2]
        };
        (VertexFormat::Short,3) => {
            [i16; 3]
        };
        (VertexFormat::Short,4) => {
            [i16; 4]
        };
        (VertexFormat::UShort,2) => {
            [u16; 2]
        };
        (VertexFormat::UShort,3) => {
            [u16; 3]
        };
        (VertexFormat::UShort,4) => {
            [u16; 4]
        };
        (VertexFormat::Float,2) => {
            [f32; 2]
        };
        (VertexFormat::Float,3) => {
            [f32; 3]
        };
        (VertexFormat::Float,4) => {
            [f32; 4]
        };
    }

    #[cfg(test)]
    mod test {
        use super::super::*;

        impl_vertex! {
            Vertex {
                position => [Position; Float; 3; false],
                texcoord => [Texcoord0; Float; 2; false],
            }
        }

        impl_vertex! {
            Vertex2 {
                position => [Position; Float; 2; false],
                color => [Color0; UByte; 4; true],
                texcoord => [Texcoord0; Byte; 2; false],
            }
        }

        fn as_bytes<T>(values: &[T]) -> &[u8]
        where
            T: Copy,
        {
            let len = values.len() * ::std::mem::size_of::<T>();
            unsafe { ::std::slice::from_raw_parts(values.as_ptr() as *const u8, len) }
        }

        #[test]
        fn basic() {
            let layout = Vertex::layout();
            assert_eq!(layout.stride(), 20);
            assert_eq!(layout.offset(Attribute::Position), Some(0));
            assert_eq!(layout.offset(Attribute::Texcoord0), Some(12));
            assert_eq!(layout.offset(Attribute::Normal), None);

            let bytes: [f32; 5] = [1.0, 1.0, 1.0, 0.0, 0.0];
            let bytes = as_bytes(&bytes);
            assert_eq!(
                bytes,
                Vertex::encode(&[Vertex::new([1.0, 1.0, 1.0], [0.0, 0.0])])
            );

            let bytes: [f32; 10] = [1.0, 1.0, 1.0, 0.0, 0.0, 2.0, 2.0, 2.0, 3.0, 3.0];
            let bytes = as_bytes(&bytes);
            assert_eq!(
                bytes,
                Vertex::encode(&[
                    Vertex::new([1.0, 1.0, 1.0], [0.0, 0.0]),
                    Vertex::new([2.0, 2.0, 2.0], [3.0, 3.0])
                ])
            );
        }

        #[test]
        fn representation() {
            let layout = Vertex::layout();
            assert_eq!(layout.stride() as usize, ::std::mem::size_of::<Vertex>());

            let layout = Vertex2::layout();
            let _v = Vertex2::new([1.0, 1.0], [0, 0, 0, 0], [0, 0]);
            let _b = Vertex2::encode(&[]);
            assert_eq!(layout.stride() as usize, ::std::mem::size_of::<Vertex2>());
        }
    }
}