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
use crate::Vertex;
pub type DecalTextureID = usize;

#[derive(Debug)]
pub struct DecalInstances {
    pub id: DecalTextureID,
    pub pos: [(f32, f32); 4],
    pub uv: [(f32, f32); 4],
    pub w: [f32; 4],
}

#[derive(Debug)]
pub struct Decal {
    id: DecalTextureID,
    pub size: (u32, u32),
    pub uv_scale: (f32, f32),
}

pub struct DecalContextManager {
    id_generator: DecalIDGenerator,
    decal_textures:
        std::collections::HashMap<DecalTextureID, (crate::texture::Texture, wgpu::BindGroup)>,
    pub decal_instances: Vec<DecalInstances>,
    buffer_vertex: wgpu::Buffer,
    buffer_index: wgpu::Buffer,
}

impl DecalContextManager {
    pub fn new(device: &wgpu::Device) -> (Self, wgpu::CommandBuffer) {
        let mut encoder =
            device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
        let buffer_vertex = device.create_buffer(&wgpu::BufferDescriptor {
            label: None,
            size: crate::VERTEX_BUFFER_SIZE,
            usage: wgpu::BufferUsage::COPY_DST
                | wgpu::BufferUsage::VERTEX
                | wgpu::BufferUsage::COPY_SRC,
        });
        let buffer_index = {
            let b = device.create_buffer(&wgpu::BufferDescriptor {
                label: None,
                size: std::mem::size_of::<[u16; 6]>() as u64,
                usage: wgpu::BufferUsage::COPY_DST
                    | wgpu::BufferUsage::INDEX
                    | wgpu::BufferUsage::COPY_SRC,
            });
            encoder.copy_buffer_to_buffer(
                &device.create_buffer_with_data(
                    bytemuck::cast_slice(crate::INDICES),
                    wgpu::BufferUsage::INDEX
                        | wgpu::BufferUsage::COPY_DST
                        | wgpu::BufferUsage::COPY_SRC,
                ),
                0,
                &b,
                0,
                std::mem::size_of::<[u16; 6]>() as u64,
            );
            b
        };
        (
            Self {
                id_generator: DecalIDGenerator(0),
                buffer_index,
                buffer_vertex,
                decal_textures: std::collections::HashMap::with_capacity(10),
                decal_instances: Vec::with_capacity(100),
            },
            encoder.finish(),
        )
    }
    pub fn add_instance(&mut self, decal: DecalInstances) {
        self.decal_instances.push(decal);
    }
}

#[derive(Debug, Clone)]
struct DecalIDGenerator(DecalTextureID);
impl DecalIDGenerator {
    fn get(&mut self) -> usize {
        self.0 += 1;
        self.0
    }
}

impl Decal {
    pub fn create(
        ctx: &mut crate::Context,
        sprite: (&[u8], (u32, u32)),
    ) -> (Self, wgpu::CommandBuffer) {
        let id = ctx.dcm.id_generator.get();
        let (tex, cmd) = crate::texture::Texture::from_bytes(&ctx.device, sprite);
        let bind_group = ctx.device.create_bind_group(&wgpu::BindGroupDescriptor {
            layout: &ctx.bind_group_layout,
            bindings: &[
                wgpu::Binding {
                    binding: 0,
                    resource: wgpu::BindingResource::TextureView(&tex.view),
                },
                wgpu::Binding {
                    binding: 1,
                    resource: wgpu::BindingResource::Sampler(&tex.sampler),
                },
            ],
            label: None,
        });
        ctx.dcm.decal_textures.insert(id, (tex, bind_group));
        (
            Self {
                id,
                size: sprite.1,
                uv_scale: (1.0 / (sprite.1).0 as f32, 1.0 / (sprite.1).1 as f32),
            },
            cmd,
        )
    }

    pub fn destroy(self, ctx: &mut crate::Context) {
        ctx.dcm.decal_textures.remove(&self.id);
    }

    pub fn id(&self) -> DecalTextureID {
        self.id
    }
}

pub trait DrawDecals<'a, 'b>
where
    'b: 'a,
{
    fn draw_decals(
        &mut self,
        dcm: &'b mut DecalContextManager,
        device: &'b mut wgpu::Device,
        queue: &'b mut wgpu::Queue,
    );
}

impl<'a, 'b> DrawDecals<'a, 'b> for wgpu::RenderPass<'a>
where
    'b: 'a,
{
    fn draw_decals(
        &mut self,
        dcm: &'b mut DecalContextManager,
        device: &'b mut wgpu::Device,
        queue: &'b mut wgpu::Queue,
    ) {
        for decal_instance in &dcm.decal_instances {
            let texture = {
                let t = dcm.decal_textures.get(&decal_instance.id);
                if t.is_none() {
                    return;
                }
                t.unwrap()
            };

            // Update buffers
            {
                let mut encoder =
                    device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
                encoder.copy_buffer_to_buffer(
                    &device.create_buffer_with_data(
                        bytemuck::cast_slice(&[
                            Vertex {
                                position: [decal_instance.pos[0].0, decal_instance.pos[0].1, 0.0],
                                tex_coords: [
                                    decal_instance.uv[0].0,
                                    decal_instance.uv[0].1,
                                    decal_instance.w[0],
                                ],
                            },
                            Vertex {
                                position: [decal_instance.pos[1].0, decal_instance.pos[1].1, 0.0],
                                tex_coords: [
                                    decal_instance.uv[1].0,
                                    decal_instance.uv[1].1,
                                    decal_instance.w[1],
                                ],
                            },
                            Vertex {
                                position: [decal_instance.pos[2].0, decal_instance.pos[2].1, 0.0],
                                tex_coords: [
                                    decal_instance.uv[2].0,
                                    decal_instance.uv[2].1,
                                    decal_instance.w[2],
                                ],
                            },
                            Vertex {
                                position: [decal_instance.pos[3].0, decal_instance.pos[3].1, 0.0],
                                tex_coords: [
                                    decal_instance.uv[3].0,
                                    decal_instance.uv[3].1,
                                    decal_instance.w[3],
                                ],
                            },
                        ]),
                        wgpu::BufferUsage::COPY_DST | wgpu::BufferUsage::COPY_SRC,
                    ),
                    0,
                    &dcm.buffer_vertex,
                    0,
                    crate::VERTEX_BUFFER_SIZE,
                );
                queue.submit(&[encoder.finish()]);
            }

            // Render things
            self.set_bind_group(0, &texture.1, &[]);
            self.set_index_buffer(&dcm.buffer_index, 0, 0);
            self.set_vertex_buffer(0, &dcm.buffer_vertex, 0, 0);
            self.draw_indexed(0..(crate::INDICES.len() as u32), 0, 0..1);
        }
        dcm.decal_instances.clear();
    }
}