-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathcached_blitter.rs
More file actions
198 lines (182 loc) · 6.35 KB
/
cached_blitter.rs
File metadata and controls
198 lines (182 loc) · 6.35 KB
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
use crate::VELLO_SURFACE_FORMAT;
/// A texture blitter that caches its bind group to avoid recreating it every frame.
///
/// The standard wgpu `TextureBlitter` creates a new bind group on every `copy()` call,
/// which causes excessive GPU resource allocation during viewport panning. This blitter
/// maintains a persistent intermediate texture (recreated only on size change) and a cached
/// bind group bound to it. Each frame, the source is copied into the persistent texture
/// via `copy_texture_to_texture` (same format, no bind groups), then the cached bind group
/// is used for the format-converting render pass.
pub struct CachedBlitter {
pipeline: wgpu::RenderPipeline,
bind_group_layout: wgpu::BindGroupLayout,
sampler: wgpu::Sampler,
cache: std::sync::Mutex<Option<BlitCache>>,
}
struct BlitCache {
source_texture: wgpu::Texture,
bind_group: wgpu::BindGroup,
size: wgpu::Extent3d,
}
const BLIT_SHADER: &str = include_str!("blit.wgsl");
impl CachedBlitter {
pub fn new(device: &wgpu::Device, format: wgpu::TextureFormat) -> Self {
let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
label: Some("CachedBlitter::sampler"),
address_mode_u: wgpu::AddressMode::ClampToEdge,
address_mode_v: wgpu::AddressMode::ClampToEdge,
address_mode_w: wgpu::AddressMode::ClampToEdge,
mag_filter: wgpu::FilterMode::Nearest,
..Default::default()
});
let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("CachedBlitter::bind_group_layout"),
entries: &[
wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Texture {
sample_type: wgpu::TextureSampleType::Float { filterable: false },
view_dimension: wgpu::TextureViewDimension::D2,
multisampled: false,
},
count: None,
},
wgpu::BindGroupLayoutEntry {
binding: 1,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::NonFiltering),
count: None,
},
],
});
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("CachedBlitter::pipeline_layout"),
bind_group_layouts: &[&bind_group_layout],
push_constant_ranges: &[],
});
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: Some("CachedBlitter::shader"),
source: wgpu::ShaderSource::Wgsl(BLIT_SHADER.into()),
});
let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("CachedBlitter::pipeline"),
layout: Some(&pipeline_layout),
vertex: wgpu::VertexState {
module: &shader,
entry_point: Some("vs_main"),
compilation_options: wgpu::PipelineCompilationOptions::default(),
buffers: &[],
},
primitive: wgpu::PrimitiveState {
topology: wgpu::PrimitiveTopology::TriangleList,
..Default::default()
},
depth_stencil: None,
multisample: wgpu::MultisampleState::default(),
fragment: Some(wgpu::FragmentState {
module: &shader,
entry_point: Some("fs_main"),
compilation_options: wgpu::PipelineCompilationOptions::default(),
targets: &[Some(wgpu::ColorTargetState {
format,
blend: None,
write_mask: wgpu::ColorWrites::ALL,
})],
}),
multiview: None,
cache: None,
});
Self {
pipeline,
bind_group_layout,
sampler,
cache: std::sync::Mutex::new(None),
}
}
/// Copies the source texture to the target with format conversion, using a cached bind group.
///
/// Internally maintains a persistent intermediate texture. Each frame:
/// 1. Copies `source` → intermediate via `copy_texture_to_texture` (same format, no bind groups)
/// 2. Blits intermediate → `target` via a render pass with the cached bind group
///
/// The bind group and intermediate texture are only recreated when the source size changes.
pub fn copy(&self, device: &wgpu::Device, encoder: &mut wgpu::CommandEncoder, source: &wgpu::Texture, target: &wgpu::TextureView) {
let size = source.size();
// Take cache out of mutex to avoid holding the lock during GPU operations
let mut cache = self.cache.lock().unwrap().take();
// Recreate the persistent texture and bind group if size changed
if !matches!(&cache, Some(c) if c.size == size) {
let texture = device.create_texture(&wgpu::TextureDescriptor {
label: Some("CachedBlitter::intermediate"),
size,
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: VELLO_SURFACE_FORMAT,
usage: wgpu::TextureUsages::COPY_DST | wgpu::TextureUsages::TEXTURE_BINDING,
view_formats: &[],
});
let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("CachedBlitter::bind_group"),
layout: &self.bind_group_layout,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::TextureView(&view),
},
wgpu::BindGroupEntry {
binding: 1,
resource: wgpu::BindingResource::Sampler(&self.sampler),
},
],
});
cache = Some(BlitCache {
source_texture: texture,
bind_group,
size,
});
}
let c = cache.as_ref().unwrap();
// Copy source → persistent intermediate texture (same format, no bind group creation)
encoder.copy_texture_to_texture(
wgpu::TexelCopyTextureInfoBase {
texture: source,
mip_level: 0,
origin: Default::default(),
aspect: Default::default(),
},
wgpu::TexelCopyTextureInfoBase {
texture: &c.source_texture,
mip_level: 0,
origin: Default::default(),
aspect: Default::default(),
},
size,
);
// Blit intermediate → target with format conversion using the cached bind group
{
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("CachedBlitter::pass"),
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: target,
depth_slice: None,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Load,
store: wgpu::StoreOp::Store,
},
})],
depth_stencil_attachment: None,
timestamp_writes: None,
occlusion_query_set: None,
});
pass.set_pipeline(&self.pipeline);
pass.set_bind_group(0, &c.bind_group, &[]);
pass.draw(0..3, 0..1);
}
// Put cache back for next frame
*self.cache.lock().unwrap() = cache;
}
}