[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) <noreply@anthropic.com>
This commit is contained in:
Behdad Esfahbod authored and GitHub committed 2026-08-04 23:08:39 -06:00
1 parent dde1f1baee
commit 9f2f03173b
8 files changed
+76

No files matched your search

+10
View File
@@ -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"
+10
View File
@@ -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"
+10
View File
@@ -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"
+8
View File
@@ -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"
+10
View File
@@ -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);
}
+10
View File
@@ -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);
}
+10
View File
@@ -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);
}
+8
View File
@@ -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);
}