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
use crate::{AHashMap, GpuDevice, LayoutStorage};
use bytemuck::{Pod, Zeroable};
use std::any::{Any, TypeId};

pub trait PipeLineLayout: Pod + Zeroable {
    fn create_layout(
        &self,
        gpu_device: &mut GpuDevice,
        layouts: &mut LayoutStorage,
        surface_format: wgpu::TextureFormat,
    ) -> wgpu::RenderPipeline;

    fn layout_key(&self) -> (TypeId, Vec<u8>) {
        let type_id = self.type_id();
        let bytes: Vec<u8> =
            bytemuck::try_cast_slice(&[*self]).unwrap_or(&[]).to_vec();

        (type_id, bytes)
    }
}

pub struct PipelineStorage {
    pub(crate) map: AHashMap<(TypeId, Vec<u8>), wgpu::RenderPipeline>,
}

impl PipelineStorage {
    pub fn new() -> Self {
        Self {
            map: AHashMap::default(),
        }
    }

    pub fn create_pipeline<K: PipeLineLayout>(
        &mut self,
        device: &mut GpuDevice,
        layout_storage: &mut LayoutStorage,
        surface_format: wgpu::TextureFormat,
        pipeline: K,
    ) {
        let key = pipeline.layout_key();

        self.map.insert(
            key,
            pipeline.create_layout(device, layout_storage, surface_format),
        );
    }

    pub fn get_pipeline<K: PipeLineLayout>(
        &self,
        pipeline: K,
    ) -> Option<&wgpu::RenderPipeline> {
        let key = pipeline.layout_key();

        self.map.get(&key)
    }
}

impl Default for PipelineStorage {
    fn default() -> Self {
        Self::new()
    }
}