comfy_wgpu/
instance.rs

1use crate::*;
2
3pub struct Instance {
4    pub position: Vec3,
5    pub rotation: f32,
6    pub scale: Vec2,
7    pub color: Vec4,
8}
9
10impl Instance {
11    pub fn to_raw(&self) -> InstanceRaw {
12        InstanceRaw {
13            model: (Mat4::from_translation(self.position) *
14                Mat4::from_rotation_z(self.rotation) *
15                Mat4::from_scale(self.scale.extend(1.0)))
16            .to_cols_array_2d(),
17
18            color: self.color.into(),
19        }
20    }
21}
22
23#[repr(C)]
24#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)]
25pub struct InstanceRaw {
26    model: [[f32; 4]; 4],
27    color: [f32; 4],
28}
29
30impl InstanceRaw {
31    const ATTRIBS: [wgpu::VertexAttribute; 5] = wgpu::vertex_attr_array![
32        3 => Float32x4,
33        4 => Float32x4,
34        5 => Float32x4,
35        6 => Float32x4,
36        7 => Float32x4,
37    ];
38}
39
40impl InstanceRaw {
41    pub fn desc<'a>() -> wgpu::VertexBufferLayout<'a> {
42        use std::mem;
43
44        wgpu::VertexBufferLayout {
45            array_stride: mem::size_of::<InstanceRaw>() as wgpu::BufferAddress,
46            // We need to switch from using a step mode of Vertex to Instance.
47            // This means that our shaders will only change to use the next
48            // instance when the shader starts processing a new instance.
49            step_mode: wgpu::VertexStepMode::Instance,
50            attributes: &Self::ATTRIBS,
51            // attributes: &[
52            //     wgpu::VertexAttribute {
53            //         offset: 0,
54            //         // Vertex shader currently only uses 0 and 1, but will
55            // use more soon.         shader_location: 2,
56            //         format: wgpu::VertexFormat::Float32x4,
57            //     },
58            //     // A mat4 takes up 4 vertex slots as it is technically 4
59            // vec4s. We need     // to define a slot for each vec4.
60            // We'll have to reassemble the mat4     // in the
61            // shader.     wgpu::VertexAttribute {
62            //         offset: mem::size_of::<[f32; 4]>() as
63            // wgpu::BufferAddress,         shader_location: 3,
64            //         format: wgpu::VertexFormat::Float32x4,
65            //     },
66            //     wgpu::VertexAttribute {
67            //         offset: mem::size_of::<[f32; 8]>() as
68            // wgpu::BufferAddress,         shader_location: 4,
69            //         format: wgpu::VertexFormat::Float32x4,
70            //     },
71            //     wgpu::VertexAttribute {
72            //         offset: mem::size_of::<[f32; 12]>() as
73            // wgpu::BufferAddress,         shader_location: 5,
74            //         format: wgpu::VertexFormat::Float32x4,
75            //     },
76            //     wgpu::VertexAttribute {
77            //         offset: mem::size_of::<[f32; 16]>() as
78            // wgpu::BufferAddress,         shader_location: 6,
79            //         format: wgpu::VertexFormat::Float32x3,
80            //     },
81            // ],
82        }
83    }
84}