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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
use crate::ImageContext;
use std::collections::HashMap;
use std::sync::Weak;
use valo_dl::{BlendMode, ColorFilter, Filter, Image, ImageInner, MipmapMode, Sampling, TileMode};
/// `ImageDesc` describes an RGBA8 image upload.
#[derive(Clone, Copy, Debug)]
pub struct ImageDesc {
/// `size` is the image dimensions in pixels.
pub size: [u32; 2],
/// `premultiplied` indicates whether the supplied RGB channels already
/// contain alpha multiplication.
///
/// When `false`, Valo premultiplies them during upload.
pub premultiplied: bool,
/// `mips` controls whether Valo builds a full mip chain.
///
/// Enable it when the image may be drawn smaller than its source size.
pub mips: bool,
}
impl Default for ImageDesc {
fn default() -> Self {
Self {
size: [0, 0],
premultiplied: false,
mips: true,
}
}
}
/// `ImageStore` caches image samplers, bind groups and filtered snapshots.
///
/// Bind groups are created once per (image, sampling) pair and reused. Dead
/// images are swept via `Weak` so the store does not pin host-dropped images.
pub struct ImageStore {
device: wgpu::Device,
samplers: HashMap<Sampling, wgpu::Sampler>,
binds: HashMap<(u64, Sampling), (Weak<ImageInner>, wgpu::BindGroup)>,
filtered: HashMap<(u64, ColorFilterKey), FilteredImage>,
frame: u64,
pub(crate) context: ImageContext,
}
/// `IMAGE_FORMAT` is the GPU format of every uploaded image texture.
pub const IMAGE_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Rgba8Unorm;
impl ImageStore {
/// `new` creates an empty image store for `device` and `queue`.
pub fn new(device: &wgpu::Device, queue: &wgpu::Queue) -> Self {
Self {
device: device.clone(),
samplers: HashMap::new(),
binds: HashMap::new(),
filtered: HashMap::new(),
frame: 0,
context: ImageContext::new(device.clone(), queue.clone()),
}
}
/// `filtered_image` returns the immutable texture that represents `image` after `filter`.
///
/// The second return value is `true` when this call created the texture.
/// The caller records the producing pass only in that case.
pub fn filtered_image(&mut self, image: &Image, filter: ColorFilter) -> (Image, bool) {
self.sweep_if_crowded();
let key = (image.id(), ColorFilterKey::from(filter));
if let Some(entry) = self.filtered.get_mut(&key) {
entry.last_used = self.frame;
return (entry.image.clone(), false);
}
let texture = self.create_image_texture(image.size(), 1);
let filtered = Image::from_texture(texture, image.size(), 1);
self.filtered.insert(
key,
FilteredImage {
source: image.downgrade(),
image: filtered.clone(),
last_used: self.frame,
},
);
(filtered, true)
}
/// `end_frame` drops filtered snapshots unused this frame.
///
/// One idle frame releases a filtered snapshot even when the host keeps
/// its source image alive, bounding retention to the visible working set.
pub fn end_frame(&mut self) {
let current = self.frame;
self.frame += 1;
let before = self.filtered.len();
self.filtered
.retain(|_, entry| entry.last_used >= current && entry.source.strong_count() > 0);
if self.filtered.len() != before {
// Bind groups retain texture views. Drop dead ones now so cache
// eviction releases the corresponding GPU textures promptly.
self.binds.retain(|_, (weak, _)| weak.strong_count() > 0);
}
}
/// `upload` creates a retained [`Image`] from RGBA8 pixels.
///
/// Premultiplies when `desc.premultiplied` is false, writes mip level 0,
/// and builds the mip chain when `desc.mips` is true. Panics if
/// `pixels.len()` is not `width * height * 4`.
pub fn upload(&self, desc: ImageDesc, pixels: &[u8]) -> Image {
self.context.upload_rgba(desc, pixels)
}
/// `finish_external` wraps an already-populated texture as a retained [`Image`].
///
/// Use this when the host copied pixels itself (for example an
/// `ImageBitmap` upload). Builds the mip chain when `mip_levels` is
/// greater than 1.
pub fn finish_external(
&self,
texture: wgpu::Texture,
size: [u32; 2],
mip_levels: u32,
) -> Image {
self.context.finish_external(texture, size, mip_levels)
}
/// `regenerate_mips` rebuilds the mip chain after level 0 was rewritten in place.
///
/// Call this after each copy from a per-frame source such as a video frame.
pub fn regenerate_mips(&self, image: &Image) {
self.context.regenerate_mips(image)
}
/// `create_image_texture` allocates an empty image texture of `size` and `mip_levels`.
///
/// The texture is bindable, copy-destination, and a render attachment so
/// mip levels can be generated by rendering into them.
pub fn create_image_texture(&self, size: [u32; 2], mip_levels: u32) -> wgpu::Texture {
self.context.create_image_texture(size, mip_levels)
}
/// `bind_group` returns the cached (texture, sampler) bind group for a draw.
pub fn bind_group(
&mut self,
texture_layout: &wgpu::BindGroupLayout,
image: &Image,
sampling: Sampling,
) -> wgpu::BindGroup {
self.sweep_if_crowded();
let key = (image.id(), sampling);
if let Some((_, bind)) = self.binds.get(&key) {
return bind.clone();
}
let sampler = self.sampler(sampling).clone();
let bind = self.device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("valo.image"),
layout: texture_layout,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::TextureView(image.view()),
},
wgpu::BindGroupEntry {
binding: 1,
resource: wgpu::BindingResource::Sampler(&sampler),
},
],
});
self.binds.insert(key, (image.downgrade(), bind.clone()));
bind
}
/// `sampler` is the sampler that reads as `sampling` says, created once.
pub(crate) fn sampler(&mut self, sampling: Sampling) -> &wgpu::Sampler {
self.samplers.entry(sampling).or_insert_with(|| {
let filter = match sampling.filter {
Filter::Linear => wgpu::FilterMode::Linear,
Filter::Nearest => wgpu::FilterMode::Nearest,
};
let mip_filter = match sampling.mipmap {
MipmapMode::Linear => wgpu::MipmapFilterMode::Linear,
MipmapMode::None | MipmapMode::Nearest => wgpu::MipmapFilterMode::Nearest,
};
// `None` is a LOD clamp rather than a filter mode: WebGPU has no
// "ignore the chain" switch, so pinning the max LOD to level 0 is
// how a sampler is told to stay sharp.
let max_lod = match sampling.mipmap {
MipmapMode::None => 0.0,
_ => 32.0,
};
self.device.create_sampler(&wgpu::SamplerDescriptor {
label: Some("valo.image"),
address_mode_u: address_mode(sampling.tile_x),
address_mode_v: address_mode(sampling.tile_y),
mag_filter: filter,
min_filter: filter,
mipmap_filter: mip_filter,
lod_max_clamp: max_lod,
..Default::default()
})
})
}
/// Bind groups whose image died are dropped; runs only when the cache
/// grows past a threshold (posters hold tens of images, not thousands).
fn sweep_if_crowded(&mut self) {
if self.binds.len() > 256 {
self.binds.retain(|_, (weak, _)| weak.strong_count() > 0);
}
}
}
struct FilteredImage {
source: Weak<ImageInner>,
image: Image,
last_used: u64,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
enum ColorFilterKey {
Matrix([u32; 20]),
Blend([u32; 4], BlendMode),
LinearToSrgbGamma,
SrgbToLinearGamma,
}
impl From<ColorFilter> for ColorFilterKey {
fn from(filter: ColorFilter) -> Self {
match filter {
ColorFilter::Matrix(matrix) => Self::Matrix(matrix.map(f32::to_bits)),
ColorFilter::Blend(color, mode) => Self::Blend(
[
color.r.to_bits(),
color.g.to_bits(),
color.b.to_bits(),
color.a.to_bits(),
],
mode,
),
ColorFilter::LinearToSrgbGamma => Self::LinearToSrgbGamma,
ColorFilter::SrgbToLinearGamma => Self::SrgbToLinearGamma,
}
}
}
fn address_mode(tile: TileMode) -> wgpu::AddressMode {
match tile {
// Decal clamps at the sampler and cuts off in the shader: WebGPU has
// no transparent border colour (`ADDRESS_MODE_CLAMP_TO_BORDER` is not
// in the baseline), so the alternative would be a feature the web
// target cannot have.
TileMode::Clamp | TileMode::Decal => wgpu::AddressMode::ClampToEdge,
TileMode::Repeat => wgpu::AddressMode::Repeat,
TileMode::Mirror => wgpu::AddressMode::MirrorRepeat,
}
}
impl ImageStore {
/// Live uploaded images, deduped across sampler variants; bytes cover
/// the mip chain (a full chain adds ~1/3).
pub(crate) fn report(&self) -> crate::PoolReport {
let mut seen = std::collections::HashSet::new();
let mut bytes = 0u64;
for (weak, _) in self.binds.values() {
let Some(inner) = weak.upgrade() else {
continue;
};
if !seen.insert(inner.id) {
continue;
}
let base = inner.size[0] as u64 * inner.size[1] as u64 * 4;
bytes += if inner.mip_levels > 1 {
base * 4 / 3
} else {
base
};
}
crate::PoolReport {
count: seen.len() as u32,
bytes,
}
}
}