From 923765aff997c36a602d1523d5a605dceb233910 Mon Sep 17 00:00:00 2001 From: Chad Brokaw Date: Mon, 28 Nov 2022 14:07:26 -0500 Subject: [PATCH] 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); } "#;