From f546f73b1f93e42d2c3e4e9e7ab16eee0f7a5eaf Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 17 Apr 2026 03:11:59 -0600 Subject: [PATCH] [gpu] Preserve paint-tree alpha for foreground entries in shaders When is_foreground was set, the shader replaced the entire baked color with the foreground uniform, discarding the paint-tree alpha encoded in the color's alpha channel. This meant COLRv1 glyphs with a separate alpha on their foreground color sentinel would lose that alpha at render time. Now the shader multiplies the foreground uniform by the baked alpha (ct.a / 32767 for LAYER_SOLID, b.a / 32767 for gradient stops), preserving the paint-tree modulation. Updated all four shader languages (GLSL, HLSL, MSL, WGSL) and regenerated the .hh artifacts. Assisted-by: Claude Opus 4.6 (1M context) --- src/hb-gpu-paint-fragment-glsl.hh | 6 +++--- src/hb-gpu-paint-fragment-hlsl.hh | 6 +++--- src/hb-gpu-paint-fragment-msl.hh | 6 +++--- src/hb-gpu-paint-fragment-wgsl.hh | 8 ++++---- src/hb-gpu-paint-fragment.glsl | 6 +++--- src/hb-gpu-paint-fragment.hlsl | 6 +++--- src/hb-gpu-paint-fragment.msl | 6 +++--- src/hb-gpu-paint-fragment.wgsl | 8 ++++---- src/hb-gpu-paint.cc | 13 ++++++++----- 9 files changed, 34 insertions(+), 31 deletions(-) diff --git a/src/hb-gpu-paint-fragment-glsl.hh b/src/hb-gpu-paint-fragment-glsl.hh index 59ee560bc..104d931ed 100644 --- a/src/hb-gpu-paint-fragment-glsl.hh +++ b/src/hb-gpu-paint-fragment-glsl.hh @@ -40,9 +40,9 @@ static const char *hb_gpu_paint_fragment_glsl = "{\n" " ivec4 a = hb_gpu_fetch (stops_base + i * 2);\n" " offset = float (a.r) / 32767.0;\n" -" if ((a.g & 1) != 0)\n" -" return foreground;\n" " ivec4 b = hb_gpu_fetch (stops_base + i * 2 + 1);\n" +" if ((a.g & 1) != 0)\n" +" return foreground * (float (b.a) / 32767.0);\n" " return vec4 (b) / 32767.0;\n" "}\n" "\n" @@ -324,7 +324,7 @@ static const char *hb_gpu_paint_fragment_glsl = " int clip3_payload = (op2.b << 16) | (op2.a & 0xffff);\n" " ivec4 ct = hb_gpu_fetch (cursor + 2);\n" " vec4 col = ((aux & 1) != 0)\n" -" ? foreground\n" +" ? foreground * (float (ct.a) / 32767.0)\n" " : vec4 (ct) / 32767.0;\n" "\n" " float cov = _hb_gpu_layer_coverage (renderCoord, pixelsPerEm,\n" diff --git a/src/hb-gpu-paint-fragment-hlsl.hh b/src/hb-gpu-paint-fragment-hlsl.hh index ae36da055..bf2e0eb9b 100644 --- a/src/hb-gpu-paint-fragment-hlsl.hh +++ b/src/hb-gpu-paint-fragment-hlsl.hh @@ -37,9 +37,9 @@ static const char *hb_gpu_paint_fragment_hlsl = "{\n" " int4 a = hb_gpu_fetch (stops_base + i * 2);\n" " offset = (float) a.r / 32767.0;\n" -" if ((a.g & 1) != 0)\n" -" return foreground;\n" " int4 b = hb_gpu_fetch (stops_base + i * 2 + 1);\n" +" if ((a.g & 1) != 0)\n" +" return foreground * ((float) b.a / 32767.0);\n" " return (float4) b / 32767.0;\n" "}\n" "\n" @@ -271,7 +271,7 @@ static const char *hb_gpu_paint_fragment_hlsl = " int clip3_payload = (op2.b << 16) | (op2.a & 0xffff);\n" " int4 ct = hb_gpu_fetch (cursor + 2);\n" " float4 col = ((aux & 1) != 0)\n" -" ? foreground\n" +" ? foreground * ((float) ct.a / 32767.0)\n" " : (float4) ct / 32767.0;\n" "\n" " float cov = _hb_gpu_layer_coverage (renderCoord, pixelsPerEm,\n" diff --git a/src/hb-gpu-paint-fragment-msl.hh b/src/hb-gpu-paint-fragment-msl.hh index 718908ceb..532d2564f 100644 --- a/src/hb-gpu-paint-fragment-msl.hh +++ b/src/hb-gpu-paint-fragment-msl.hh @@ -39,9 +39,9 @@ static const char *hb_gpu_paint_fragment_msl = "{\n" " int4 a = int4 (hb_gpu_atlas[stops_base + i * 2]);\n" " offset = float (a.r) / 32767.0;\n" -" if ((a.g & 1) != 0)\n" -" return foreground;\n" " int4 b = int4 (hb_gpu_atlas[stops_base + i * 2 + 1]);\n" +" if ((a.g & 1) != 0)\n" +" return foreground * (float (b.a) / 32767.0);\n" " return float4 (b) / 32767.0;\n" "}\n" "\n" @@ -292,7 +292,7 @@ static const char *hb_gpu_paint_fragment_msl = " int clip3_payload = (op2.b << 16) | (op2.a & 0xffff);\n" " int4 ct = int4 (hb_gpu_atlas[cursor + 2]);\n" " float4 col = ((aux & 1) != 0)\n" -" ? foreground\n" +" ? foreground * (float (ct.a) / 32767.0)\n" " : float4 (ct) / 32767.0;\n" "\n" " float cov = _hb_gpu_layer_coverage (renderCoord, pixelsPerEm,\n" diff --git a/src/hb-gpu-paint-fragment-wgsl.hh b/src/hb-gpu-paint-fragment-wgsl.hh index 5e2a7e4ac..5c03efc6d 100644 --- a/src/hb-gpu-paint-fragment-wgsl.hh +++ b/src/hb-gpu-paint-fragment-wgsl.hh @@ -41,10 +41,10 @@ static const char *hb_gpu_paint_fragment_wgsl = "{\n" " let a = hb_gpu_fetch (hb_gpu_atlas, stops_base + i * 2);\n" " *offset = f32 (a.r) / 32767.0;\n" -" if ((a.g & 1) != 0) {\n" -" return foreground;\n" -" }\n" " let b = hb_gpu_fetch (hb_gpu_atlas, stops_base + i * 2 + 1);\n" +" if ((a.g & 1) != 0) {\n" +" return foreground * (f32 (b.a) / 32767.0);\n" +" }\n" " return vec4f (b) / 32767.0;\n" "}\n" "\n" @@ -293,7 +293,7 @@ static const char *hb_gpu_paint_fragment_wgsl = " let ct = hb_gpu_fetch (hb_gpu_atlas, cursor + 2);\n" " var col: vec4f;\n" " if ((aux & 1) != 0) {\n" -" col = foreground;\n" +" col = foreground * (f32 (ct.a) / 32767.0);\n" " } else {\n" " col = vec4f (ct) / 32767.0;\n" " }\n" diff --git a/src/hb-gpu-paint-fragment.glsl b/src/hb-gpu-paint-fragment.glsl index 92873cff7..63d18e4be 100644 --- a/src/hb-gpu-paint-fragment.glsl +++ b/src/hb-gpu-paint-fragment.glsl @@ -39,9 +39,9 @@ vec4 _hb_gpu_stop_color (int stops_base, int i, vec4 foreground, out float offse { ivec4 a = hb_gpu_fetch (stops_base + i * 2); offset = float (a.r) / 32767.0; - if ((a.g & 1) != 0) - return foreground; ivec4 b = hb_gpu_fetch (stops_base + i * 2 + 1); + if ((a.g & 1) != 0) + return foreground * (float (b.a) / 32767.0); return vec4 (b) / 32767.0; } @@ -323,7 +323,7 @@ vec4 hb_gpu_paint (vec2 renderCoord, uint glyphLoc, vec4 foreground) int clip3_payload = (op2.b << 16) | (op2.a & 0xffff); ivec4 ct = hb_gpu_fetch (cursor + 2); vec4 col = ((aux & 1) != 0) - ? foreground + ? foreground * (float (ct.a) / 32767.0) : vec4 (ct) / 32767.0; float cov = _hb_gpu_layer_coverage (renderCoord, pixelsPerEm, diff --git a/src/hb-gpu-paint-fragment.hlsl b/src/hb-gpu-paint-fragment.hlsl index 4020578b1..89a4680c4 100644 --- a/src/hb-gpu-paint-fragment.hlsl +++ b/src/hb-gpu-paint-fragment.hlsl @@ -36,9 +36,9 @@ float4 _hb_gpu_stop_color (int stops_base, int i, float4 foreground, { int4 a = hb_gpu_fetch (stops_base + i * 2); offset = (float) a.r / 32767.0; - if ((a.g & 1) != 0) - return foreground; int4 b = hb_gpu_fetch (stops_base + i * 2 + 1); + if ((a.g & 1) != 0) + return foreground * ((float) b.a / 32767.0); return (float4) b / 32767.0; } @@ -270,7 +270,7 @@ float4 hb_gpu_paint (float2 renderCoord, uint glyphLoc_, float4 foreground) int clip3_payload = (op2.b << 16) | (op2.a & 0xffff); int4 ct = hb_gpu_fetch (cursor + 2); float4 col = ((aux & 1) != 0) - ? foreground + ? foreground * ((float) ct.a / 32767.0) : (float4) ct / 32767.0; float cov = _hb_gpu_layer_coverage (renderCoord, pixelsPerEm, diff --git a/src/hb-gpu-paint-fragment.msl b/src/hb-gpu-paint-fragment.msl index 6361fb5fc..5a625aa57 100644 --- a/src/hb-gpu-paint-fragment.msl +++ b/src/hb-gpu-paint-fragment.msl @@ -38,9 +38,9 @@ static float4 _hb_gpu_stop_color (device const short4* hb_gpu_atlas, { int4 a = int4 (hb_gpu_atlas[stops_base + i * 2]); offset = float (a.r) / 32767.0; - if ((a.g & 1) != 0) - return foreground; int4 b = int4 (hb_gpu_atlas[stops_base + i * 2 + 1]); + if ((a.g & 1) != 0) + return foreground * (float (b.a) / 32767.0); return float4 (b) / 32767.0; } @@ -291,7 +291,7 @@ float4 hb_gpu_paint (float2 renderCoord, uint glyphLoc_, float4 foreground, int clip3_payload = (op2.b << 16) | (op2.a & 0xffff); int4 ct = int4 (hb_gpu_atlas[cursor + 2]); float4 col = ((aux & 1) != 0) - ? foreground + ? foreground * (float (ct.a) / 32767.0) : float4 (ct) / 32767.0; float cov = _hb_gpu_layer_coverage (renderCoord, pixelsPerEm, diff --git a/src/hb-gpu-paint-fragment.wgsl b/src/hb-gpu-paint-fragment.wgsl index 42259b1c2..cc91dd250 100644 --- a/src/hb-gpu-paint-fragment.wgsl +++ b/src/hb-gpu-paint-fragment.wgsl @@ -40,10 +40,10 @@ fn _hb_gpu_stop_color (hb_gpu_atlas: ptr>, read>, { let a = hb_gpu_fetch (hb_gpu_atlas, stops_base + i * 2); *offset = f32 (a.r) / 32767.0; - if ((a.g & 1) != 0) { - return foreground; - } let b = hb_gpu_fetch (hb_gpu_atlas, stops_base + i * 2 + 1); + if ((a.g & 1) != 0) { + return foreground * (f32 (b.a) / 32767.0); + } return vec4f (b) / 32767.0; } @@ -292,7 +292,7 @@ fn hb_gpu_paint (renderCoord: vec2f, glyphLoc_: u32, foreground: vec4f, let ct = hb_gpu_fetch (hb_gpu_atlas, cursor + 2); var col: vec4f; if ((aux & 1) != 0) { - col = foreground; + col = foreground * (f32 (ct.a) / 32767.0); } else { col = vec4f (ct) / 32767.0; } diff --git a/src/hb-gpu-paint.cc b/src/hb-gpu-paint.cc index 163711930..76050cf74 100644 --- a/src/hb-gpu-paint.cc +++ b/src/hb-gpu-paint.cc @@ -1276,12 +1276,15 @@ hb_gpu_paint_glyph_or_fail (hb_gpu_paint_t *paint, return hb_font_paint_glyph_or_fail (font, glyph, hb_gpu_paint_get_funcs (paint), paint, paint->palette, - /* Foreground value is never read - * from our encoded blob -- the + /* Foreground RGB is not read from + * the encoded blob -- the * is_foreground flag routes to the - * shader's foreground uniform at - * render time -- so any sentinel - * works here. */ + * shader's foreground uniform. + * Alpha must be opaque so the + * baked color carries only the + * paint-tree alpha (which the + * shader applies on top of the + * uniform). */ HB_COLOR (0, 0, 0, 0xff)); }