Skip to content

Commit bf95b85

Browse files
authored
vello_hybrid: reuse allocation for staging alpha data (linebender#882)
This drops the need to always allocate for alphas in `Renderer::prepare`.
1 parent 0599adb commit bf95b85

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

sparse_strips/vello_hybrid/src/render.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ pub struct Renderer {
5959
pub render_pipeline: RenderPipeline,
6060
/// GPU resources for rendering (created during prepare)
6161
resources: Option<GpuResources>,
62+
63+
/// Scratch buffer for staging alpha texture data.
64+
alpha_data: Vec<u8>,
6265
}
6366

6467
/// Contains the data needed for rendering
@@ -196,6 +199,7 @@ impl Renderer {
196199
render_bind_group_layout,
197200
render_pipeline,
198201
resources: None,
202+
alpha_data: Vec::new(),
199203
}
200204
}
201205

@@ -258,6 +262,11 @@ impl Renderer {
258262
"Alpha texture height exceeds max texture dimensions"
259263
);
260264

265+
// Resize the alpha texture staging buffer.
266+
self.alpha_data.resize(
267+
(max_texture_dimension_2d * alpha_texture_height * 16) as usize,
268+
0,
269+
);
261270
// The alpha texture encodes 16 1-byte alpha values per texel, with 4 alpha values packed in each channel
262271
let alphas_texture = device.create_texture(&wgpu::TextureDescriptor {
263272
label: Some("Alpha Texture"),
@@ -333,8 +342,9 @@ impl Renderer {
333342
render_data.alphas.len() <= (texture_width * texture_height * 16) as usize,
334343
"Alpha texture dimensions are too small to fit the alpha data"
335344
);
336-
let mut alpha_data = render_data.alphas.clone();
337-
alpha_data.resize((texture_width * texture_height * 16) as usize, 0);
345+
// After this copy to `self.alpha_data`, there may be stale trailing alpha values. These
346+
// are not sampled, so can be left as-is.
347+
self.alpha_data[0..render_data.alphas.len()].copy_from_slice(&render_data.alphas);
338348

339349
queue.write_texture(
340350
wgpu::TexelCopyTextureInfo {
@@ -343,7 +353,7 @@ impl Renderer {
343353
origin: wgpu::Origin3d::ZERO,
344354
aspect: wgpu::TextureAspect::All,
345355
},
346-
&alpha_data,
356+
&self.alpha_data,
347357
wgpu::TexelCopyBufferLayout {
348358
offset: 0,
349359
// 16 bytes per RGBA32Uint texel (4 u32s × 4 bytes each)

0 commit comments

Comments
 (0)