`vello_hybrid`: Improve alpha mask documentation
diff --git a/sparse_strips/vello_hybrid/shaders/sparse_strip_renderer.wgsl b/sparse_strips/vello_hybrid/shaders/sparse_strip_renderer.wgsl index c10adcf..e22ad2d 100644 --- a/sparse_strips/vello_hybrid/shaders/sparse_strip_renderer.wgsl +++ b/sparse_strips/vello_hybrid/shaders/sparse_strip_renderer.wgsl
@@ -91,15 +91,17 @@ // would it be faster to do a texture lookup for every pixel? if x < in.dense_end { let y = u32(floor(in.tex_coord.y)); - // Retrieve alpha value from the texture - // Calculate texture coordinates based on the fragment's x-position - // Since we store 4 alpha values per texel, divide x by 4 to get the texel position + // Retrieve alpha value from the texture. We store 16 1-byte alpha + // values per texel, with each color channel packing 4 alpha values. + // The code here assumes the strip height is 4, i.e., each color + // channel encodes the alpha values for a single column within a strip. + // Divide x by 4 to get the texel position. let alphas_index = x; let tex_dimensions = textureDimensions(alphas_texture); let alphas_tex_width = tex_dimensions.x; - // Which texel contains our alpha value + // Which texel contains the alpha values for this column let texel_index = alphas_index / 4u; - // Which channel (R,G,B,A) in the texel + // Which channel (R,G,B,A) in the texel contains the alpha values for this column let channel_index = alphas_index % 4u; // Calculate texture coordinates using bitwise operations // This is more efficient than using modulo and division when width is a power of 2 @@ -110,7 +112,7 @@ // Load all 4 channels from the texture let rgba_values = textureLoad(alphas_texture, vec2<u32>(tex_x, tex_y), 0); - // Get the alphas from the appropriate RGBA channel based on the index + // Get the column's alphas from the appropriate RGBA channel based on the index let alphas_u32 = unpack_alphas_from_channel(rgba_values, channel_index); // Extract the alpha value for the current y-position from the packed u32 data alpha = f32((alphas_u32 >> (y * 8u)) & 0xffu) * (1.0 / 255.0);
diff --git a/sparse_strips/vello_hybrid/src/render.rs b/sparse_strips/vello_hybrid/src/render.rs index 687bf6e..ad354f5 100644 --- a/sparse_strips/vello_hybrid/src/render.rs +++ b/sparse_strips/vello_hybrid/src/render.rs
@@ -210,7 +210,7 @@ let max_texture_dimension_2d = device.limits().max_texture_dimension_2d; let alpha_len = render_data.alphas.len(); - // 4 alpha values u32 each per texel + // There are 16 1-byte alpha values per texel, and 4 alpha values per `render_data.alphas`. let required_alpha_height = (u32::try_from(alpha_len).unwrap()).div_ceil(max_texture_dimension_2d * 4); let required_alpha_size = max_texture_dimension_2d * required_alpha_height * 4; @@ -243,7 +243,7 @@ let (alphas_texture, render_bind_group) = if needs_new_alpha_texture { let max_texture_dimension_2d = device.limits().max_texture_dimension_2d; let alpha_len = render_data.alphas.len(); - // 4 alpha values u32 each per texel + // There are 16 1-byte alpha values per texel, and 4 alpha values per `render_data.alphas`. let alpha_texture_height = (u32::try_from(alpha_len).unwrap()).div_ceil(max_texture_dimension_2d * 4); @@ -252,6 +252,7 @@ "Alpha texture height exceeds max texture dimensions" ); + // The alpha texture encodes 16 1-byte alpha values per texel, with 4 alpha values packed in each channel let alphas_texture = device.create_texture(&wgpu::TextureDescriptor { label: Some("Alpha Texture"), size: wgpu::Extent3d { @@ -318,7 +319,7 @@ bytemuck::cast_slice(&render_data.strips), ); - // Prepare alpha data for the texture with 4 alpha values per texel + // Prepare alpha data for the texture with 16 1-byte alpha values per texel (4 per channel) let texture_width = resources.alphas_texture.width(); let texture_height = resources.alphas_texture.height(); assert!(