@@ -59,6 +59,9 @@ pub struct Renderer {
59
59
pub render_pipeline : RenderPipeline ,
60
60
/// GPU resources for rendering (created during prepare)
61
61
resources : Option < GpuResources > ,
62
+
63
+ /// Scratch buffer for staging alpha texture data.
64
+ alpha_data : Vec < u8 > ,
62
65
}
63
66
64
67
/// Contains the data needed for rendering
@@ -196,6 +199,7 @@ impl Renderer {
196
199
render_bind_group_layout,
197
200
render_pipeline,
198
201
resources : None ,
202
+ alpha_data : Vec :: new ( ) ,
199
203
}
200
204
}
201
205
@@ -258,6 +262,11 @@ impl Renderer {
258
262
"Alpha texture height exceeds max texture dimensions"
259
263
) ;
260
264
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
+ ) ;
261
270
// The alpha texture encodes 16 1-byte alpha values per texel, with 4 alpha values packed in each channel
262
271
let alphas_texture = device. create_texture ( & wgpu:: TextureDescriptor {
263
272
label : Some ( "Alpha Texture" ) ,
@@ -333,8 +342,9 @@ impl Renderer {
333
342
render_data. alphas. len( ) <= ( texture_width * texture_height * 16 ) as usize ,
334
343
"Alpha texture dimensions are too small to fit the alpha data"
335
344
) ;
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 ) ;
338
348
339
349
queue. write_texture (
340
350
wgpu:: TexelCopyTextureInfo {
@@ -343,7 +353,7 @@ impl Renderer {
343
353
origin : wgpu:: Origin3d :: ZERO ,
344
354
aspect : wgpu:: TextureAspect :: All ,
345
355
} ,
346
- & alpha_data,
356
+ & self . alpha_data ,
347
357
wgpu:: TexelCopyBufferLayout {
348
358
offset : 0 ,
349
359
// 16 bytes per RGBA32Uint texel (4 u32s × 4 bytes each)
0 commit comments