[gpu] Add small-size MSAA to reduce moiré aliasing

At ppem < 16, blend in 4 extra samples at ±1/3 pixel offsets
to reduce moiré patterns and aliasing at small sizes.  The
blend factor transitions smoothly from full MSAA at ppem 8
to single-sample at ppem 16.

GLSL only for now; MSL, WGSL, and HLSL still use single-sample.

Assisted-by: Claude <noreply@anthropic.com>
This commit is contained in:
Behdad Esfahbod committed 2026-04-01 22:50:46 -06:00
1 parent 5151ae0b34
commit 7ac851ab6f
2 files changed
+64 -16

No files matched your search

+32 -8
View File
@@ -157,14 +157,8 @@ static const char *hb_gpu_fragment_glsl =
" return ivec2 (hCount, vCount);\n"
"}\n"
"\n"
"/* Return coverage in [0, 1].\n"
" *\n"
" * Requires the hb_gpu_atlas uniform to be bound.\n"
" *\n"
" * renderCoord: em-space sample position\n"
" * glyphLoc: texel offset of glyph blob in atlas\n"
" */\n"
"float hb_gpu_render (vec2 renderCoord, uint glyphLoc_)\n"
"/* Single-sample coverage. */\n"
"float _hb_gpu_render_single (vec2 renderCoord, uint glyphLoc_)\n"
"{\n"
" vec2 emsPerPixel = fwidth (renderCoord);\n"
" vec2 pixelsPerEm = 1.0 / emsPerPixel;\n"
@@ -281,6 +275,36 @@ static const char *hb_gpu_fragment_glsl =
" return _hb_gpu_calc_coverage (xcov, ycov, xwgt, ywgt);\n"
"}\n"
"\n"
"/* Return coverage in [0, 1].\n"
" *\n"
" * At small sizes (ppem < 16), blends in 4 extra samples at\n"
" * ±1/3 pixel offsets to reduce moiré and aliasing.\n"
" *\n"
" * renderCoord: em-space sample position\n"
" * glyphLoc: texel offset of glyph blob in atlas\n"
" */\n"
"float hb_gpu_render (vec2 renderCoord, uint glyphLoc_)\n"
"{\n"
" float c = _hb_gpu_render_single (renderCoord, glyphLoc_);\n"
"\n"
" vec2 emsPerPixel = fwidth (renderCoord);\n"
" float ppem = 1.0 / max (emsPerPixel.x, emsPerPixel.y);\n"
"\n"
" if (ppem < 16.0)\n"
" {\n"
" vec2 d = emsPerPixel * (1.0 / 3.0);\n"
" float msaa = 0.25 *\n"
" (_hb_gpu_render_single (renderCoord + vec2 (-d.x, -d.y), glyphLoc_) +\n"
" _hb_gpu_render_single (renderCoord + vec2 ( d.x, -d.y), glyphLoc_) +\n"
" _hb_gpu_render_single (renderCoord + vec2 (-d.x, d.y), glyphLoc_) +\n"
" _hb_gpu_render_single (renderCoord + vec2 ( d.x, d.y), glyphLoc_));\n"
"\n"
" c = mix (c, msaa, smoothstep (16.0, 8.0, ppem));\n"
" }\n"
"\n"
" return c;\n"
"}\n"
"\n"
"/* Stem darkening for small sizes.\n"
" *\n"
" * coverage: output of hb_gpu_render\n"
+32 -8
View File
@@ -156,14 +156,8 @@ ivec2 _hb_gpu_curve_counts (vec2 renderCoord, uint glyphLoc_)
return ivec2 (hCount, vCount);
}
/* Return coverage in [0, 1].
*
* Requires the hb_gpu_atlas uniform to be bound.
*
* renderCoord: em-space sample position
* glyphLoc: texel offset of glyph blob in atlas
*/
float hb_gpu_render (vec2 renderCoord, uint glyphLoc_)
/* Single-sample coverage. */
float _hb_gpu_render_single (vec2 renderCoord, uint glyphLoc_)
{
vec2 emsPerPixel = fwidth (renderCoord);
vec2 pixelsPerEm = 1.0 / emsPerPixel;
@@ -280,6 +274,36 @@ float hb_gpu_render (vec2 renderCoord, uint glyphLoc_)
return _hb_gpu_calc_coverage (xcov, ycov, xwgt, ywgt);
}
/* Return coverage in [0, 1].
*
* At small sizes (ppem < 16), blends in 4 extra samples at
* ±1/3 pixel offsets to reduce moiré and aliasing.
*
* renderCoord: em-space sample position
* glyphLoc: texel offset of glyph blob in atlas
*/
float hb_gpu_render (vec2 renderCoord, uint glyphLoc_)
{
float c = _hb_gpu_render_single (renderCoord, glyphLoc_);
vec2 emsPerPixel = fwidth (renderCoord);
float ppem = 1.0 / max (emsPerPixel.x, emsPerPixel.y);
if (ppem < 16.0)
{
vec2 d = emsPerPixel * (1.0 / 3.0);
float msaa = 0.25 *
(_hb_gpu_render_single (renderCoord + vec2 (-d.x, -d.y), glyphLoc_) +
_hb_gpu_render_single (renderCoord + vec2 ( d.x, -d.y), glyphLoc_) +
_hb_gpu_render_single (renderCoord + vec2 (-d.x, d.y), glyphLoc_) +
_hb_gpu_render_single (renderCoord + vec2 ( d.x, d.y), glyphLoc_));
c = mix (c, msaa, smoothstep (16.0, 8.0, ppem));
}
return c;
}
/* Stem darkening for small sizes.
*
* coverage: output of hb_gpu_render