From 9f2f03173b7fee860cc00d999857d09fa4a362e2 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Tue, 4 Aug 2026 23:08:39 -0600 Subject: [PATCH] [gpu] Fix coverage sign of the split-ray optimization (#6133) _hb_gpu_slug_single casts its horizontal ray leftward or rightward depending on which side of the band's split point the fragment is on, and likewise for the vertical ray. The crossing signs it accumulates are the same either way. But over a closed contour the signed crossings of a line sum to zero, so counting the crossings on the left gives exactly the negative of counting them on the right (clamp(0.5 + u) + clamp(0.5 - u) == 1, so this holds for the antialiased partial coverages too). Whenever hLeftRay and vLeftRay disagree, xcov and ycov therefore came out with opposite signs, and _hb_gpu_calc_coverage's abs (xcov * xwgt + ycov * ywgt) / (xwgt + ywgt) subtracted the two estimates instead of averaging them. Glyph interiors were unaffected -- the min (abs (xcov), abs (ycov)) term floors the result there -- so this never showed as a hole. It showed as edges that antialias too light wherever both weights are nonzero and the two estimates differ. Flip the sign back after each loop. With a CPU port of _hb_gpu_slug_single, comparing against plain Slug (rightward rays over the full curve list, which is what the sign convention in _hb_gpu_calc_coverage assumes), util/gpu/default-font.ttf at scale 163 rendered at 107px had 1700 pixels differing by more than 0.05 before this change, always too dark, and none differing by more than 0.001 after -- the split optimization is now exact. Same fix in all four shading languages. The GLSL was checked with glslc; the HLSL, MSL and WGSL edits are the same two statements and were not machine-compiled. Assisted-by: Claude Opus 5 (1M context) --- src/hb-gpu-fragment-glsl.hh | 10 ++++++++++ src/hb-gpu-fragment-hlsl.hh | 10 ++++++++++ src/hb-gpu-fragment-msl.hh | 10 ++++++++++ src/hb-gpu-fragment-wgsl.hh | 8 ++++++++ src/hb-gpu-fragment.glsl | 10 ++++++++++ src/hb-gpu-fragment.hlsl | 10 ++++++++++ src/hb-gpu-fragment.msl | 10 ++++++++++ src/hb-gpu-fragment.wgsl | 8 ++++++++ 8 files changed, 76 insertions(+) diff --git a/src/hb-gpu-fragment-glsl.hh b/src/hb-gpu-fragment-glsl.hh index c7356fede..1db7066c7 100644 --- a/src/hb-gpu-fragment-glsl.hh +++ b/src/hb-gpu-fragment-glsl.hh @@ -244,6 +244,12 @@ static const char *hb_gpu_fragment_glsl = " }\n" " }\n" "\n" +" /* Crossings over a closed contour sum to zero, so a leftward ray\n" +" * returns the negative of what a rightward ray would; flip it back\n" +" * so that xcov and ycov keep a common sign convention. */\n" +" if (hLeftRay)\n" +" xcov = -xcov;\n" +"\n" " float ycov = 0.0;\n" " float ywgt = 0.0;\n" "\n" @@ -295,6 +301,10 @@ static const char *hb_gpu_fragment_glsl = " }\n" " }\n" "\n" +" /* Ditto, for the vertical ray. */\n" +" if (vLeftRay)\n" +" ycov = -ycov;\n" +"\n" " return _hb_gpu_calc_coverage (xcov, ycov, xwgt, ywgt);\n" "}\n" "\n" diff --git a/src/hb-gpu-fragment-hlsl.hh b/src/hb-gpu-fragment-hlsl.hh index bfe2c9301..3ad54d183 100644 --- a/src/hb-gpu-fragment-hlsl.hh +++ b/src/hb-gpu-fragment-hlsl.hh @@ -214,6 +214,12 @@ static const char *hb_gpu_fragment_hlsl = " }\n" " }\n" "\n" +" /* Crossings over a closed contour sum to zero, so a leftward ray\n" +" * returns the negative of what a rightward ray would; flip it back\n" +" * so that xcov and ycov keep a common sign convention. */\n" +" if (hLeftRay)\n" +" xcov = -xcov;\n" +"\n" " float ycov = 0.0;\n" " float ywgt = 0.0;\n" "\n" @@ -265,6 +271,10 @@ static const char *hb_gpu_fragment_hlsl = " }\n" " }\n" "\n" +" /* Ditto, for the vertical ray. */\n" +" if (vLeftRay)\n" +" ycov = -ycov;\n" +"\n" " return _hb_gpu_calc_coverage (xcov, ycov, xwgt, ywgt);\n" "}\n" "\n" diff --git a/src/hb-gpu-fragment-msl.hh b/src/hb-gpu-fragment-msl.hh index e30a50421..83f04b14f 100644 --- a/src/hb-gpu-fragment-msl.hh +++ b/src/hb-gpu-fragment-msl.hh @@ -218,6 +218,12 @@ static const char *hb_gpu_fragment_msl = " }\n" " }\n" "\n" +" /* Crossings over a closed contour sum to zero, so a leftward ray\n" +" * returns the negative of what a rightward ray would; flip it back\n" +" * so that xcov and ycov keep a common sign convention. */\n" +" if (hLeftRay)\n" +" xcov = -xcov;\n" +"\n" " float ycov = 0.0;\n" " float ywgt = 0.0;\n" "\n" @@ -269,6 +275,10 @@ static const char *hb_gpu_fragment_msl = " }\n" " }\n" "\n" +" /* Ditto, for the vertical ray. */\n" +" if (vLeftRay)\n" +" ycov = -ycov;\n" +"\n" " return _hb_gpu_calc_coverage (xcov, ycov, xwgt, ywgt);\n" "}\n" "\n" diff --git a/src/hb-gpu-fragment-wgsl.hh b/src/hb-gpu-fragment-wgsl.hh index e77698e5e..3a8fa31d2 100644 --- a/src/hb-gpu-fragment-wgsl.hh +++ b/src/hb-gpu-fragment-wgsl.hh @@ -222,6 +222,11 @@ static const char *hb_gpu_fragment_wgsl = " }\n" " }\n" "\n" +" /* Crossings over a closed contour sum to zero, so a leftward ray\n" +" * returns the negative of what a rightward ray would; flip it back\n" +" * so that xcov and ycov keep a common sign convention. */\n" +" if (hLeftRay) { xcov = -xcov; }\n" +"\n" " var ycov: f32 = 0.0;\n" " var ywgt: f32 = 0.0;\n" "\n" @@ -276,6 +281,9 @@ static const char *hb_gpu_fragment_wgsl = " }\n" " }\n" "\n" +" /* Ditto, for the vertical ray. */\n" +" if (vLeftRay) { ycov = -ycov; }\n" +"\n" " return _hb_gpu_calc_coverage (xcov, ycov, xwgt, ywgt);\n" "}\n" "\n" diff --git a/src/hb-gpu-fragment.glsl b/src/hb-gpu-fragment.glsl index 23b14a0ba..734f136c3 100644 --- a/src/hb-gpu-fragment.glsl +++ b/src/hb-gpu-fragment.glsl @@ -243,6 +243,12 @@ float _hb_gpu_slug_single (vec2 renderCoord, vec2 pixelsPerEm, uint glyphLoc_) } } + /* Crossings over a closed contour sum to zero, so a leftward ray + * returns the negative of what a rightward ray would; flip it back + * so that xcov and ycov keep a common sign convention. */ + if (hLeftRay) + xcov = -xcov; + float ycov = 0.0; float ywgt = 0.0; @@ -294,6 +300,10 @@ float _hb_gpu_slug_single (vec2 renderCoord, vec2 pixelsPerEm, uint glyphLoc_) } } + /* Ditto, for the vertical ray. */ + if (vLeftRay) + ycov = -ycov; + return _hb_gpu_calc_coverage (xcov, ycov, xwgt, ywgt); } diff --git a/src/hb-gpu-fragment.hlsl b/src/hb-gpu-fragment.hlsl index a0c69d6cd..6ab728c8a 100644 --- a/src/hb-gpu-fragment.hlsl +++ b/src/hb-gpu-fragment.hlsl @@ -213,6 +213,12 @@ float _hb_gpu_slug_single (float2 renderCoord, float2 pixelsPerEm, uint glyphLoc } } + /* Crossings over a closed contour sum to zero, so a leftward ray + * returns the negative of what a rightward ray would; flip it back + * so that xcov and ycov keep a common sign convention. */ + if (hLeftRay) + xcov = -xcov; + float ycov = 0.0; float ywgt = 0.0; @@ -264,6 +270,10 @@ float _hb_gpu_slug_single (float2 renderCoord, float2 pixelsPerEm, uint glyphLoc } } + /* Ditto, for the vertical ray. */ + if (vLeftRay) + ycov = -ycov; + return _hb_gpu_calc_coverage (xcov, ycov, xwgt, ywgt); } diff --git a/src/hb-gpu-fragment.msl b/src/hb-gpu-fragment.msl index bb7da496d..3f3220bba 100644 --- a/src/hb-gpu-fragment.msl +++ b/src/hb-gpu-fragment.msl @@ -217,6 +217,12 @@ float _hb_gpu_slug_single (float2 renderCoord, float2 pixelsPerEm, uint glyphLoc } } + /* Crossings over a closed contour sum to zero, so a leftward ray + * returns the negative of what a rightward ray would; flip it back + * so that xcov and ycov keep a common sign convention. */ + if (hLeftRay) + xcov = -xcov; + float ycov = 0.0; float ywgt = 0.0; @@ -268,6 +274,10 @@ float _hb_gpu_slug_single (float2 renderCoord, float2 pixelsPerEm, uint glyphLoc } } + /* Ditto, for the vertical ray. */ + if (vLeftRay) + ycov = -ycov; + return _hb_gpu_calc_coverage (xcov, ycov, xwgt, ywgt); } diff --git a/src/hb-gpu-fragment.wgsl b/src/hb-gpu-fragment.wgsl index 0fac0c384..ae9c1da41 100644 --- a/src/hb-gpu-fragment.wgsl +++ b/src/hb-gpu-fragment.wgsl @@ -221,6 +221,11 @@ fn _hb_gpu_slug_single (renderCoord: vec2f, pixelsPerEm: vec2f, glyphLoc_: u32, } } + /* Crossings over a closed contour sum to zero, so a leftward ray + * returns the negative of what a rightward ray would; flip it back + * so that xcov and ycov keep a common sign convention. */ + if (hLeftRay) { xcov = -xcov; } + var ycov: f32 = 0.0; var ywgt: f32 = 0.0; @@ -275,6 +280,9 @@ fn _hb_gpu_slug_single (renderCoord: vec2f, pixelsPerEm: vec2f, glyphLoc_: u32, } } + /* Ditto, for the vertical ray. */ + if (vLeftRay) { ycov = -ycov; } + return _hb_gpu_calc_coverage (xcov, ycov, xwgt, ywgt); }