From 4fea3058839c612e9ba4a6fd23b5831e7cce32e0 Mon Sep 17 00:00:00 2001 From: Chad Brokaw Date: Mon, 28 Nov 2022 00:03:02 -0500 Subject: [PATCH 1/2] Hodgepodge of small fixes This just a collection of minor fixes to various things I noticed while looking over the code. Adds a `Device::poll` call to the winit example. Removes some lingering unnecessary ``s in the blit shaders. Removes a duplicated bbox entry in the shared shader inclusion code and collapses nested `concat!`s. Finally, it reintroduces the computation of separated alpha in the output of fine which I removed in a previous PR. This allows the `render_to_texture` mode to be useful for generating images that can be properly blended over other content. The blit shader has been changed to accommodate this. --- Cargo.lock | 1 + piet-wgsl/examples/winit/Cargo.toml | 1 + piet-wgsl/examples/winit/src/main.rs | 1 + piet-wgsl/shader/fine.wgsl | 5 ++++- piet-wgsl/src/lib.rs | 15 +++++++-------- piet-wgsl/src/shaders.rs | 5 ++--- 6 files changed, 16 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c84704c..6cfddef 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1704,6 +1704,7 @@ dependencies = [ "piet-wgsl", "pollster", "roxmltree", + "wgpu", "winit 0.27.5", ] diff --git a/piet-wgsl/examples/winit/Cargo.toml b/piet-wgsl/examples/winit/Cargo.toml index 0898487..34dfbdd 100644 --- a/piet-wgsl/examples/winit/Cargo.toml +++ b/piet-wgsl/examples/winit/Cargo.toml @@ -7,6 +7,7 @@ publish = false # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +wgpu = "0.14" piet-wgsl = { path = "../../../piet-wgsl" } piet-scene = { path = "../../../piet-scene" } winit = "0.27.5" diff --git a/piet-wgsl/examples/winit/src/main.rs b/piet-wgsl/examples/winit/src/main.rs index ba6226c..f34bd55 100644 --- a/piet-wgsl/examples/winit/src/main.rs +++ b/piet-wgsl/examples/winit/src/main.rs @@ -96,6 +96,7 @@ async fn run() -> Result<()> { ) .expect("failed to render to surface"); surface_texture.present(); + render_cx.device.poll(wgpu::Maintain::Wait); } _ => {} }); diff --git a/piet-wgsl/shader/fine.wgsl b/piet-wgsl/shader/fine.wgsl index 87ed3f8..cd6d940 100644 --- a/piet-wgsl/shader/fine.wgsl +++ b/piet-wgsl/shader/fine.wgsl @@ -281,7 +281,10 @@ fn main( for (var i = 0u; i < PIXELS_PER_THREAD; i += 1u) { let coords = xy_uint + vec2(i, 0u); if coords.x < config.target_width && coords.y < config.target_height { - textureStore(output, vec2(coords), rgba[i]); + let fg = rgba[i]; + let a_inv = 1.0 / (fg.a + 1e-6); + let rgba_sep = vec4(fg.r * a_inv, fg.g * a_inv, fg.b * a_inv, fg.a); + textureStore(output, vec2(coords), rgba_sep); } } #else diff --git a/piet-wgsl/src/lib.rs b/piet-wgsl/src/lib.rs index 59641eb..91262f2 100644 --- a/piet-wgsl/src/lib.rs +++ b/piet-wgsl/src/lib.rs @@ -143,7 +143,6 @@ impl Renderer { } struct TargetTexture { - texture: Texture, view: TextureView, width: u32, height: u32, @@ -166,7 +165,6 @@ impl TargetTexture { }); let view = texture.create_view(&wgpu::TextureViewDescriptor::default()); Self { - texture, view, width, height, @@ -185,20 +183,20 @@ impl BlitPipeline { @vertex fn vs_main(@builtin(vertex_index) ix: u32) -> @builtin(position) vec4 { // Generate a full screen quad in NDCs - var vertex = vec2(-1.0, 1.0); + var vertex = vec2(-1.0, 1.0); switch ix { case 1u: { - vertex = vec2(-1.0, -1.0); + vertex = vec2(-1.0, -1.0); } case 2u, 4u: { - vertex = vec2(1.0, -1.0); + vertex = vec2(1.0, -1.0); } case 5u: { - vertex = vec2(1.0, 1.0); + vertex = vec2(1.0, 1.0); } default: {} } - return vec4(vertex, 0.0, 1.0); + return vec4(vertex, 0.0, 1.0); } @group(0) @binding(0) @@ -206,7 +204,8 @@ impl BlitPipeline { @fragment fn fs_main(@builtin(position) pos: vec4) -> @location(0) vec4 { - return textureLoad(fine_output, vec2(pos.xy), 0); + let rgba_sep = textureLoad(fine_output, vec2(pos.xy), 0); + return vec4(rgba_sep.rgb * rgba_sep.a, 1.0); } "#; diff --git a/piet-wgsl/src/shaders.rs b/piet-wgsl/src/shaders.rs index 8cb6129..ef35a8f 100644 --- a/piet-wgsl/src/shaders.rs +++ b/piet-wgsl/src/shaders.rs @@ -32,7 +32,7 @@ pub const CLIP_REDUCE_WG: u32 = 256; macro_rules! shader { ($name:expr) => { - include_str!(concat!(concat!("../shader/", $name), ".wgsl")) + include_str!(concat!("../shader/", $name, ".wgsl")) }; } @@ -314,7 +314,7 @@ macro_rules! shared_shader { ($name:expr) => { ( $name, - include_str!(concat!(concat!("../shader/shared/", $name), ".wgsl")), + include_str!(concat!("../shader/shared/", $name, ".wgsl")), ) }; } @@ -326,7 +326,6 @@ const SHARED_SHADERS: &[(&str, &str)] = &[ shared_shader!("clip"), shared_shader!("config"), shared_shader!("cubic"), - shared_shader!("bbox"), shared_shader!("drawtag"), shared_shader!("pathtag"), shared_shader!("ptcl"), From 923765aff997c36a602d1523d5a605dceb233910 Mon Sep 17 00:00:00 2001 From: Chad Brokaw Date: Mon, 28 Nov 2022 14:07:26 -0500 Subject: [PATCH 2/2] Address review feedback * Add comment about avoiding NaNs to inverse alpha divisions * Change inverse alpha computations to use max(a, epsilon) rather than + * Emit alpha component in blit fragment shader (enables support for partially transparent windows) --- piet-wgsl/shader/fine.wgsl | 5 +++-- piet-wgsl/shader/shared/blend.wgsl | 6 +++--- piet-wgsl/src/lib.rs | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/piet-wgsl/shader/fine.wgsl b/piet-wgsl/shader/fine.wgsl index cd6d940..74a783e 100644 --- a/piet-wgsl/shader/fine.wgsl +++ b/piet-wgsl/shader/fine.wgsl @@ -282,8 +282,9 @@ fn main( let coords = xy_uint + vec2(i, 0u); if coords.x < config.target_width && coords.y < config.target_height { let fg = rgba[i]; - let a_inv = 1.0 / (fg.a + 1e-6); - let rgba_sep = vec4(fg.r * a_inv, fg.g * a_inv, fg.b * a_inv, fg.a); + // Max with a small epsilon to avoid NaNs + let a_inv = 1.0 / max(fg.a, 1e-6); + let rgba_sep = vec4(fg.rgb * a_inv, fg.a); textureStore(output, vec2(coords), rgba_sep); } } diff --git a/piet-wgsl/shader/shared/blend.wgsl b/piet-wgsl/shader/shared/blend.wgsl index 7b72f2e..eef670d 100644 --- a/piet-wgsl/shader/shared/blend.wgsl +++ b/piet-wgsl/shader/shared/blend.wgsl @@ -318,10 +318,10 @@ fn blend_mix_compose(backdrop: vec4, src: vec4, mode: u32) -> vec4> 8u; let mixed = blend_mix(cb, cs, mix_mode); diff --git a/piet-wgsl/src/lib.rs b/piet-wgsl/src/lib.rs index 91262f2..dde1ad2 100644 --- a/piet-wgsl/src/lib.rs +++ b/piet-wgsl/src/lib.rs @@ -205,7 +205,7 @@ impl BlitPipeline { @fragment fn fs_main(@builtin(position) pos: vec4) -> @location(0) vec4 { let rgba_sep = textureLoad(fine_output, vec2(pos.xy), 0); - return vec4(rgba_sep.rgb * rgba_sep.a, 1.0); + return vec4(rgba_sep.rgb * rgba_sep.a, rgba_sep.a); } "#;