-
Notifications
You must be signed in to change notification settings - Fork 306
[Vello Hybrid]: Clipping (Spatiotemporal Allocation) #957
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
d6b2c02
c220a65
4b60027
24bd54e
dc61c48
2ab36ec
9523ded
41f78ba
b7dbcec
e137620
ccb0ea6
7585b31
4711939
cbf6ba3
007ae05
46c7a10
c1e9097
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| // Copyright 2024 the Vello Authors | ||
| // SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
|
||
| // This shader clears specific slots in slot textures to transparent pixels. | ||
|
|
||
| // Assumes this texture consists of a single column of slots of `config.slot_height`, | ||
| // numbering from 0 to `texture_height / slot_height - 1` from top to bottom. | ||
|
|
||
| struct Config { | ||
| // Width of a slot (typically matching `WideTile::WIDTH`) | ||
| slot_width: u32, | ||
|
taj-p marked this conversation as resolved.
Outdated
|
||
| // Height of a slot (typically matching `Tile::HEIGHT`) | ||
| slot_height: u32, | ||
| // Total height of the texture (slot_height * number_of_slots) | ||
| texture_height: u32, | ||
| // Padding for 16-byte alignment | ||
| _padding: u32, | ||
| } | ||
|
|
||
| @group(0) @binding(0) | ||
| var<uniform> config: Config; | ||
|
|
||
| @vertex | ||
| fn vs_main( | ||
| @builtin(vertex_index) vertex_index: u32, | ||
| @location(0) index: u32, | ||
| ) -> @builtin(position) vec4<f32> { | ||
| // Map vertex_index (0-3) to quad corners: | ||
| // 0 β (0,0), 1 β (1,0), 2 β (0,1), 3 β (1,1) | ||
| let x = f32(vertex_index & 1u); | ||
| let y = f32(vertex_index >> 1u); | ||
|
|
||
| // Calculate the y-position based on the slot index | ||
| let slot_y_offset = f32(index * config.slot_height); | ||
|
|
||
| // Scale to match slot dimensions | ||
| let pix_x = x * f32(config.slot_width); | ||
| let pix_y = slot_y_offset + y * f32(config.slot_height); | ||
|
|
||
| // Convert to NDC | ||
| let ndc_x = pix_x * 2.0 / f32(config.slot_width) - 1.0; | ||
| let ndc_y = 1.0 - pix_y * 2.0 / f32(config.texture_height); | ||
|
|
||
| return vec4<f32>(ndc_x, ndc_y, 0.0, 1.0); | ||
| } | ||
|
|
||
| @fragment | ||
| fn fs_main(@builtin(position) position: vec4<f32>) -> @location(0) vec4<f32> { | ||
| // Clear with transparent pixels | ||
| return vec4<f32>(0.0, 0.0, 0.0, 0.0); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,9 +9,13 @@ | |
| // | ||
| // The alpha values are stored in a texture and sampled during fragment shading. | ||
| // This approach optimizes memory usage by only storing alpha data where needed. | ||
| // | ||
| // The `StripInstance`'s `rgba_or_slot` field can either encode a color or a slot index. | ||
| // If the alpha value is non-zero, the fragment shader samples the alpha texture. | ||
| // Otherwise, the fragment shader samples the source clip texture using the given slot index. | ||
|
|
||
| struct Config { | ||
| // Width of the rendering target | ||
| // Width of the rendering target | ||
| width: u32, | ||
| // Height of the rendering target | ||
| height: u32, | ||
|
|
@@ -29,17 +33,17 @@ struct StripInstance { | |
| @location(1) widths: u32, | ||
| // Alpha texture column index where this strip's alpha values begin | ||
| @location(2) col: u32, | ||
| // [r, g, b, a] packed as u8's | ||
| @location(3) rgba: u32, | ||
| // [r, g, b, a] packed as u8's or a slot index when alpha is 0 | ||
| @location(3) rgba_or_slot: u32, | ||
| } | ||
|
|
||
| struct VertexOutput { | ||
| // Texture coordinates for the current fragment | ||
| // Texture coordinates for the current fragment | ||
| @location(0) tex_coord: vec2<f32>, | ||
| // Ending x-position of the dense (alpha) region | ||
| @location(1) @interpolate(flat) dense_end: u32, | ||
| // RGBA color value | ||
| @location(2) @interpolate(flat) color: u32, | ||
| // Color value or slot index when alpha is 0 | ||
| @location(2) @interpolate(flat) rgba_or_slot: u32, | ||
| // Normalized device coordinates (NDC) for the current vertex | ||
| @builtin(position) position: vec4<f32>, | ||
| }; | ||
|
|
@@ -77,21 +81,22 @@ fn vs_main( | |
|
|
||
| out.position = vec4<f32>(ndc_x, ndc_y, 0.0, 1.0); | ||
| out.tex_coord = vec2<f32>(f32(instance.col) + x * f32(width), y * f32(config.strip_height)); | ||
| out.color = instance.rgba; | ||
| out.rgba_or_slot = instance.rgba_or_slot; | ||
| return out; | ||
| } | ||
|
|
||
| @group(0) @binding(0) | ||
| var alphas_texture: texture_2d<u32>; | ||
|
|
||
| @group(0) @binding(2) | ||
| var clip_input_texture: texture_2d<f32>; | ||
|
|
||
| @fragment | ||
| fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> { | ||
| let x = u32(floor(in.tex_coord.x)); | ||
| var alpha = 1.0; | ||
| // Determine if the current fragment is within the dense (alpha) region | ||
| // If so, sample the alpha value from the texture; otherwise, alpha remains fully opaque (1.0) | ||
| // TODO: This is a branch, but we can make it branchless by using a select | ||
| // 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. We store 16 1-byte alpha | ||
|
|
@@ -108,18 +113,28 @@ fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> { | |
| let channel_index = alphas_index % 4u; | ||
| // Calculate texel coordinates | ||
| let tex_x = texel_index & (alphas_tex_width - 1u); | ||
| let tex_y = texel_index >> config.alphas_tex_width_bits; | ||
| let tex_y = texel_index >> config.alphas_tex_width_bits; | ||
|
|
||
| // Load all 4 channels from the texture | ||
| let rgba_values = textureLoad(alphas_texture, vec2<u32>(tex_x, tex_y), 0); | ||
|
|
||
| // 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); | ||
| } | ||
| // Apply the alpha value to the unpacked RGBA color | ||
| return alpha * unpack4x8unorm(in.color); | ||
| // Apply the alpha value to the unpacked RGBA color or slot index | ||
| let alpha_byte = in.rgba_or_slot >> 24u; | ||
| if alpha_byte != 0 { | ||
| // in.rgba_or_slot encodes a color | ||
| return alpha * unpack4x8unorm(in.rgba_or_slot); | ||
| } else { | ||
| // in.rgba_or_slot encodes a slot in the source clip texture | ||
| let clip_x = u32(in.position.x) & 0xFFu; | ||
| let clip_y = (u32(in.position.y) & 3) + in.rgba_or_slot * config.strip_height; | ||
|
Comment on lines
+135
to
+136
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is not clear to me how
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the answer to this question should also potentially result in a code comment in the wgsl shader.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
In time, we will want to make this configurable, but I'm not sure how that will present. We could make the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh whoops, I got my x and y's confused! Thanks for the great answer :D |
||
| let clip_in_color = textureLoad(clip_input_texture, vec2(clip_x, clip_y), 0); | ||
| return alpha * clip_in_color; | ||
| } | ||
| } | ||
|
|
||
| fn unpack_alphas_from_channel(rgba: vec4<u32>, channel_index: u32) -> u32 { | ||
|
|
@@ -136,6 +151,7 @@ fn unpack_alphas_from_channel(rgba: vec4<u32>, channel_index: u32) -> u32 { | |
| // Polyfills `unpack4x8unorm`. | ||
| // | ||
| // Downlevel targets do not support native WGSL `unpack4x8unorm`. | ||
| // TODO: Remove once we upgrade to WGPU 25. | ||
| fn unpack4x8unorm(rgba_packed: u32) -> vec4<f32> { | ||
| // Extract each byte and convert to float in range [0,1] | ||
| return vec4<f32>( | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.