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
use crate::Color;
use crate::Rect;

/// Instance data.
/// Data passed to the GPU per sprite in a sprite batch.
#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub struct Instance {
    /// [x, y] representing upper-left corner of the rectangle cropped from the source.
    /// Coordinates are between 0 and 1.
    /// Upper left corner is the origin [0, 0].
    src_ul: [f32; 2],

    /// [x, y] representing the lower-right corner of the rectangle cropped from the source.
    /// Coordinates are between 0 and 1.
    /// Upper left corner is the origin [0, 0].
    src_lr: [f32; 2],

    /// [x, y] representing upper-left corner of the destination rectangle.
    /// Coordinates are between 0 and 1.
    /// Upper left corner is the origin [0, 0].
    dst_ul: [f32; 2],

    /// [x, y] representing lower-right corner of the destination rectangle.
    /// Coordinates are between 0 and 1.
    /// Upper left corner is the origin [0, 0].
    dst_lr: [f32; 2],

    /// clockwise rotation in radians.
    /// around the center of the rectangle after moving to the destination rectangle.
    rotate: f32,

    /// Multiplied by the texture color per-fragment to get the final color returned
    /// by the fragment shader
    ///
    /// Defaults to [1.0, 1.0, 1.0, 1.0], so that the color remains unchanged
    color_factor: [f32; 4],
}

unsafe impl bytemuck::Pod for Instance {}
unsafe impl bytemuck::Zeroable for Instance {}

const FLOAT_SIZE: wgpu::BufferAddress = std::mem::size_of::<f32>() as wgpu::BufferAddress;

impl Instance {
    pub fn builder() -> InstanceBuilder {
        InstanceBuilder {
            src: [0.0, 0.0, 1.0, 1.0].into(),
            dest: [0.0, 0.0, 1.0, 1.0].into(),
            rotate: 0.0,
            color_factor: [1.0, 1.0, 1.0, 1.0],
        }
    }
    fn new<R1: Into<Rect>, R2: Into<Rect>>(
        src: R1,
        dest: R2,
        rotate: f32,
        color_factor: [f32; 4],
    ) -> Instance {
        let src = src.into();
        let dest = dest.into();
        Instance {
            src_ul: src.upper_left(),
            src_lr: src.lower_right(),
            dst_ul: dest.upper_left(),
            dst_lr: dest.lower_right(),
            rotate,
            color_factor,
        }
    }

    pub fn src(&self) -> Rect {
        [self.src_ul, self.src_lr].into()
    }

    pub fn set_src<R: Into<Rect>>(&mut self, rect: R) {
        let rect = rect.into();
        self.src_ul = rect.upper_left();
        self.src_lr = rect.lower_right();
    }

    pub fn dest(&self) -> Rect {
        [self.dst_ul, self.dst_lr].into()
    }

    pub fn set_dest<R: Into<Rect>>(&mut self, rect: R) {
        let rect = rect.into();
        self.dst_ul = rect.upper_left();
        self.dst_lr = rect.lower_right();
    }

    pub fn rotation(&self) -> f32 {
        self.rotate
    }

    pub fn set_rotation(&mut self, rotate: f32) {
        self.rotate = rotate;
    }

    pub fn set_color_factor<C: Into<Color>>(&mut self, color_factor: C) {
        self.color_factor = color_factor.into().to_array();
    }

    pub(super) fn desc<'a>() -> wgpu::VertexBufferDescriptor<'a> {
        assert_eq!(
            std::mem::align_of::<Instance>(),
            std::mem::align_of::<f32>(),
        );
        assert_eq!(
            std::mem::size_of::<Instance>(),
            std::mem::size_of::<f32>() * 13,
        );
        use std::mem;
        wgpu::VertexBufferDescriptor {
            stride: mem::size_of::<Instance>() as wgpu::BufferAddress,
            step_mode: wgpu::InputStepMode::Instance,
            attributes: &[
                wgpu::VertexAttributeDescriptor {
                    offset: 0,
                    shader_location: 0,
                    format: wgpu::VertexFormat::Float2,
                },
                wgpu::VertexAttributeDescriptor {
                    offset: FLOAT_SIZE * 2,
                    shader_location: 1,
                    format: wgpu::VertexFormat::Float2,
                },
                wgpu::VertexAttributeDescriptor {
                    offset: FLOAT_SIZE * (2 + 2),
                    shader_location: 2,
                    format: wgpu::VertexFormat::Float2,
                },
                wgpu::VertexAttributeDescriptor {
                    offset: FLOAT_SIZE * (2 + 2 + 2),
                    shader_location: 3,
                    format: wgpu::VertexFormat::Float2,
                },
                wgpu::VertexAttributeDescriptor {
                    offset: FLOAT_SIZE * (2 + 2 + 2 + 2),
                    shader_location: 4,
                    format: wgpu::VertexFormat::Float,
                },
                wgpu::VertexAttributeDescriptor {
                    offset: FLOAT_SIZE * (2 + 2 + 2 + 2 + 1),
                    shader_location: 5,
                    format: wgpu::VertexFormat::Float4,
                },
            ],
        }
    }
}

pub struct InstanceBuilder {
    src: Rect,
    dest: Rect,
    rotate: f32,
    color_factor: [f32; 4],
}

impl InstanceBuilder {
    pub fn build(self) -> Instance {
        Instance::new(self.src, self.dest, self.rotate, self.color_factor)
    }

    pub fn src<R: Into<Rect>>(mut self, src: R) -> Self {
        self.src = src.into();
        self
    }

    pub fn dest<R: Into<Rect>>(mut self, dest: R) -> Self {
        self.dest = dest.into();
        self
    }

    pub fn rotate(mut self, rotate: f32) -> Self {
        self.rotate = rotate;
        self
    }

    /// Sets the color factor
    /// NOTE: this isn't actually the color per-se;
    /// the value passed here is multiplied with the color returned
    /// by the texture to get the color to return from the fragment
    /// shader
    pub fn color_factor<C: Into<Color>>(mut self, color_factor: C) -> Self {
        self.color_factor = color_factor.into().to_array();
        self
    }
}

impl From<InstanceBuilder> for Instance {
    fn from(builder: InstanceBuilder) -> Self {
        builder.build()
    }
}