diff --git a/src/hb-gpu-fragment-glsl.hh b/src/hb-gpu-fragment-glsl.hh index 0f52a20e3..2cde09302 100644 --- a/src/hb-gpu-fragment-glsl.hh +++ b/src/hb-gpu-fragment-glsl.hh @@ -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" diff --git a/src/hb-gpu-fragment.glsl b/src/hb-gpu-fragment.glsl index c3648f0dd..5d2c70a37 100644 --- a/src/hb-gpu-fragment.glsl +++ b/src/hb-gpu-fragment.glsl @@ -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