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
use crate::{AHashMap, GpuDevice, LayoutStorage};
use bytemuck::{Pod, Zeroable};
use std::any::{Any, TypeId};
/// Trait used to Create and Load [`wgpu::RenderPipeline`] to and from a HashMap.
///
pub trait PipeLineLayout: Pod + Zeroable {
/// Creates the [`wgpu::RenderPipeline`] to be added to the HashMap
///
fn create_layout(
&self,
gpu_device: &mut GpuDevice,
layouts: &mut LayoutStorage,
surface_format: wgpu::TextureFormat,
) -> wgpu::RenderPipeline;
/// Gives a Hashable Key of the [`wgpu::RenderPipeline`] to use to Retrieve it from the HashMap.
///
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)
}
}
/// [`wgpu::RenderPipeline`] Storage using a hashmap.
///
#[derive(Debug)]
pub struct PipelineStorage {
pub(crate) map: AHashMap<(TypeId, Vec<u8>), wgpu::RenderPipeline>,
}
impl PipelineStorage {
/// Creates a new [`PipelineStorage`] with default HashMap.
///
pub fn new() -> Self {
Self {
map: AHashMap::default(),
}
}
/// Creates a new [`wgpu::RenderPipeline`] from [`PipeLineLayout`] and adds it to the internal map.
///
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),
);
}
/// Retrieves a Reference to a [`wgpu::RenderPipeline`] within the internal map for rendering.
///
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()
}
}