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
//! Draw meshes of triangles.
pub mod solid;

#[cfg(feature = "gradient")]
pub mod gradient;

use crate::MeshUniforms;

const INITIAL_INDEX_COUNT: usize = 256;
const INITIAL_VERTEX_COUNT: usize = 256;

fn instance_uniforms_layout(device: &wgpu::Device) -> wgpu::BindGroupLayout {
    device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
        label: Some("rootvg-mesh instance uniforms layout"),
        entries: &[InstanceUniforms::entry()],
    })
}

fn color_target_state(format: wgpu::TextureFormat) -> [Option<wgpu::ColorTargetState>; 1] {
    [Some(wgpu::ColorTargetState {
        format,
        blend: Some(wgpu::BlendState::ALPHA_BLENDING),
        write_mask: wgpu::ColorWrites::ALL,
    })]
}

// TODO: Use push constants instead if it is available.
#[derive(Debug, Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
#[repr(C)]
struct InstanceUniforms {
    inner: MeshUniforms,
    /// Uniform values must be 256-aligned;
    /// see: [`wgpu::Limits`] `min_uniform_buffer_offset_alignment`.
    _padding1: [f32; 32],
    /// Bytemuck doesn't derive for arrays of size 55, so split it up.
    _padding2: [f32; 23],
}

impl InstanceUniforms {
    pub fn new(inner: MeshUniforms) -> Self {
        // # SAFETY:
        //
        // Neither the rust code nor the shader code reads these padding bytes.
        #[allow(invalid_value)]
        let (_padding1, _padding2): ([f32; 32], [f32; 23]) = unsafe {
            (
                std::mem::MaybeUninit::uninit().assume_init(),
                std::mem::MaybeUninit::uninit().assume_init(),
            )
        };

        Self {
            inner,
            _padding1,
            _padding2,
        }
    }

    fn entry() -> wgpu::BindGroupLayoutEntry {
        wgpu::BindGroupLayoutEntry {
            binding: 0,
            visibility: wgpu::ShaderStages::VERTEX_FRAGMENT,
            ty: wgpu::BindingType::Buffer {
                ty: wgpu::BufferBindingType::Uniform,
                has_dynamic_offset: true,
                min_binding_size: wgpu::BufferSize::new(std::mem::size_of::<Self>() as u64),
            },
            count: None,
        }
    }

    pub fn min_size() -> Option<wgpu::BufferSize> {
        wgpu::BufferSize::new(std::mem::size_of::<Self>() as u64)
    }
}