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
use std::collections::HashMap;

use crate::{
    coords::WorldTileCoords,
    render::{resource::Texture, settings::Msaa, tile_view_pattern::HasTile},
    tcs::world::World,
};

/// Holds the resources necessary for the raster tiles such as the
/// * sampler
/// * texture
/// * pipeline
/// * bindgroups
pub struct RasterResources {
    sampler: wgpu::Sampler,
    msaa: Msaa,
    pipeline: wgpu::RenderPipeline,
    bound_textures: HashMap<WorldTileCoords, wgpu::BindGroup>,
}

impl RasterResources {
    pub fn new(msaa: Msaa, device: &wgpu::Device, pipeline: wgpu::RenderPipeline) -> Self {
        let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
            address_mode_u: wgpu::AddressMode::ClampToEdge,
            address_mode_v: wgpu::AddressMode::ClampToEdge,
            address_mode_w: wgpu::AddressMode::ClampToEdge,
            mag_filter: wgpu::FilterMode::Linear,
            min_filter: wgpu::FilterMode::Linear,
            mipmap_filter: wgpu::FilterMode::Linear,
            ..Default::default()
        });
        Self {
            sampler,
            msaa,
            pipeline,
            bound_textures: Default::default(),
        }
    }

    pub fn create_texture(
        &mut self,
        label: wgpu::Label,
        device: &wgpu::Device,
        format: wgpu::TextureFormat,
        width: u32,
        height: u32,
        usage: wgpu::TextureUsages,
    ) -> Texture {
        Texture::new(label, device, format, width, height, self.msaa, usage)
    }

    pub fn get_bound_texture(&self, coords: &WorldTileCoords) -> Option<&wgpu::BindGroup> {
        self.bound_textures.get(coords)
    }

    /// Creates a bind group for each fetched raster tile and store it inside a hashmap.
    pub fn bind_texture(
        &mut self,
        device: &wgpu::Device,
        coords: &WorldTileCoords,
        texture: Texture,
    ) {
        self.bound_textures.insert(
            *coords,
            device.create_bind_group(&wgpu::BindGroupDescriptor {
                layout: &self.pipeline.get_bind_group_layout(0),
                entries: &[
                    wgpu::BindGroupEntry {
                        binding: 0,
                        resource: wgpu::BindingResource::TextureView(&texture.view),
                    },
                    wgpu::BindGroupEntry {
                        binding: 1,
                        resource: wgpu::BindingResource::Sampler(&self.sampler),
                    },
                ],
                label: None,
            }),
        );
    }

    pub fn pipeline(&self) -> &wgpu::RenderPipeline {
        &self.pipeline
    }
}

impl HasTile for RasterResources {
    fn has_tile(&self, coords: WorldTileCoords, _world: &World) -> bool {
        self.bound_textures.contains_key(&coords)
    }
}