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
|
use iced_native::image;
use std::{
collections::{HashMap, HashSet},
rc::Rc,
};
#[derive(Debug)]
pub enum Memory {
Host(::image::ImageBuffer<::image::Bgra<u8>, Vec<u8>>),
Device {
bind_group: Rc<wgpu::BindGroup>,
width: u32,
height: u32,
},
NotFound,
Invalid,
}
impl Memory {
pub fn dimensions(&self) -> (u32, u32) {
match self {
Memory::Host(image) => image.dimensions(),
Memory::Device { width, height, .. } => (*width, *height),
Memory::NotFound => (1, 1),
Memory::Invalid => (1, 1),
}
}
pub fn upload(
&mut self,
device: &wgpu::Device,
encoder: &mut wgpu::CommandEncoder,
texture_layout: &wgpu::BindGroupLayout,
) -> Option<Rc<wgpu::BindGroup>> {
match self {
Memory::Host(image) => {
let (width, height) = image.dimensions();
let extent = wgpu::Extent3d {
width,
height,
depth: 1,
};
let texture = device.create_texture(&wgpu::TextureDescriptor {
size: extent,
array_layer_count: 1,
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: wgpu::TextureFormat::Bgra8UnormSrgb,
usage: wgpu::TextureUsage::COPY_DST
| wgpu::TextureUsage::SAMPLED,
});
let temp_buf = {
let flat_samples = image.as_flat_samples();
let slice = flat_samples.as_slice();
device
.create_buffer_mapped(
slice.len(),
wgpu::BufferUsage::COPY_SRC,
)
.fill_from_slice(slice)
};
encoder.copy_buffer_to_texture(
wgpu::BufferCopyView {
buffer: &temp_buf,
offset: 0,
row_pitch: 4 * width as u32,
image_height: height as u32,
},
wgpu::TextureCopyView {
texture: &texture,
array_layer: 0,
mip_level: 0,
origin: wgpu::Origin3d {
x: 0.0,
y: 0.0,
z: 0.0,
},
},
extent,
);
let bind_group =
device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: texture_layout,
bindings: &[wgpu::Binding {
binding: 0,
resource: wgpu::BindingResource::TextureView(
&texture.create_default_view(),
),
}],
});
let bind_group = Rc::new(bind_group);
*self = Memory::Device {
bind_group: bind_group.clone(),
width,
height,
};
Some(bind_group)
}
Memory::Device { bind_group, .. } => Some(bind_group.clone()),
Memory::NotFound => None,
Memory::Invalid => None,
}
}
}
#[derive(Debug)]
pub struct Cache {
map: HashMap<u64, Memory>,
hits: HashSet<u64>,
}
impl Cache {
pub fn new() -> Self {
Self {
map: HashMap::new(),
hits: HashSet::new(),
}
}
pub fn load(&mut self, handle: &image::Handle) -> &mut Memory {
if self.contains(handle) {
return self.get(handle).unwrap();
}
let memory = match handle.data() {
image::Data::Path(path) => {
if let Ok(image) = ::image::open(path) {
Memory::Host(image.to_bgra())
} else {
Memory::NotFound
}
}
image::Data::Bytes(bytes) => {
if let Ok(image) = ::image::load_from_memory(&bytes) {
Memory::Host(image.to_bgra())
} else {
Memory::Invalid
}
}
};
self.insert(handle, memory);
self.get(handle).unwrap()
}
pub fn trim(&mut self) {
let hits = &self.hits;
self.map.retain(|k, _| hits.contains(k));
self.hits.clear();
}
fn get(&mut self, handle: &image::Handle) -> Option<&mut Memory> {
let _ = self.hits.insert(handle.id());
self.map.get_mut(&handle.id())
}
fn insert(&mut self, handle: &image::Handle, memory: Memory) {
let _ = self.map.insert(handle.id(), memory);
}
fn contains(&self, handle: &image::Handle) -> bool {
self.map.contains_key(&handle.id())
}
}
|