[gpu] Hoist fwidth out of _hb_gpu_render_single

Compute pixelsPerEm once in hb_gpu_render and pass it down,
so MSAA samples share one fwidth call instead of each
computing their own.  Required for WGSL (fwidth must be
called from uniform control flow) and a minor win for all
backends.

Assisted-by: Claude <noreply@anthropic.com>
This commit is contained in:
Behdad Esfahbod committed 2026-04-02 01:20:10 -06:00
1 parent 33baa306b2
commit 84f15592dc
8 files changed
+72 -72

No files matched your search

+9 -9
View File
@@ -173,10 +173,8 @@ static const char *hb_gpu_fragment_glsl =
"}\n"
"\n"
"/* Single-sample coverage in [0, 1]. */\n"
"float _hb_gpu_render_single (vec2 renderCoord, uint glyphLoc_)\n"
"float _hb_gpu_render_single (vec2 renderCoord, vec2 pixelsPerEm, uint glyphLoc_)\n"
"{\n"
" vec2 emsPerPixel = fwidth (renderCoord);\n"
" vec2 pixelsPerEm = 1.0 / emsPerPixel;\n"
"\n"
" _hb_gpu_glyph_info gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_);\n"
" int glyphLoc = gi.glyphLoc;\n"
@@ -297,20 +295,22 @@ static const char *hb_gpu_fragment_glsl =
" */\n"
"float hb_gpu_render (vec2 renderCoord, uint glyphLoc_)\n"
"{\n"
" float c = _hb_gpu_render_single (renderCoord, glyphLoc_);\n"
" vec2 emsPerPixel = fwidth (renderCoord);\n"
" vec2 pixelsPerEm = 1.0 / emsPerPixel;\n"
"\n"
" float c = _hb_gpu_render_single (renderCoord, pixelsPerEm, glyphLoc_);\n"
"\n"
"#ifndef HB_GPU_NO_MSAA\n"
" float ppem = hb_gpu_ppem (renderCoord, glyphLoc_);\n"
" vec2 emsPerPixel = fwidth (renderCoord);\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"
" (_hb_gpu_render_single (renderCoord + vec2 (-d.x, -d.y), pixelsPerEm, glyphLoc_) +\n"
" _hb_gpu_render_single (renderCoord + vec2 ( d.x, -d.y), pixelsPerEm, glyphLoc_) +\n"
" _hb_gpu_render_single (renderCoord + vec2 (-d.x, d.y), pixelsPerEm, glyphLoc_) +\n"
" _hb_gpu_render_single (renderCoord + vec2 ( d.x, d.y), pixelsPerEm, glyphLoc_));\n"
"\n"
" c = mix (c, msaa, smoothstep (16.0, 8.0, ppem));\n"
" }\n"
+9 -9
View File
@@ -159,10 +159,8 @@ static const char *hb_gpu_fragment_hlsl =
"\n"
"\n"
"/* Single-sample coverage in [0, 1]. */\n"
"float _hb_gpu_render_single (float2 renderCoord, uint glyphLoc_)\n"
"float _hb_gpu_render_single (float2 renderCoord, float2 pixelsPerEm, uint glyphLoc_)\n"
"{\n"
" float2 emsPerPixel = fwidth (renderCoord);\n"
" float2 pixelsPerEm = 1.0 / emsPerPixel;\n"
"\n"
" _hb_gpu_glyph_info gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_);\n"
" int glyphLoc = gi.glyphLoc;\n"
@@ -283,20 +281,22 @@ static const char *hb_gpu_fragment_hlsl =
" */\n"
"float hb_gpu_render (float2 renderCoord, uint glyphLoc_)\n"
"{\n"
" float c = _hb_gpu_render_single (renderCoord, glyphLoc_);\n"
" float2 emsPerPixel = fwidth (renderCoord);\n"
" float2 pixelsPerEm = 1.0 / emsPerPixel;\n"
"\n"
" float c = _hb_gpu_render_single (renderCoord, pixelsPerEm, glyphLoc_);\n"
"\n"
"#ifndef HB_GPU_NO_MSAA\n"
" float ppem = hb_gpu_ppem (renderCoord, glyphLoc_);\n"
" float2 emsPerPixel = fwidth (renderCoord);\n"
"\n"
" if (ppem < 16.0)\n"
" {\n"
" float2 d = emsPerPixel * (1.0 / 3.0);\n"
" float msaa = 0.25 *\n"
" (_hb_gpu_render_single (renderCoord + float2 (-d.x, -d.y), glyphLoc_) +\n"
" _hb_gpu_render_single (renderCoord + float2 ( d.x, -d.y), glyphLoc_) +\n"
" _hb_gpu_render_single (renderCoord + float2 (-d.x, d.y), glyphLoc_) +\n"
" _hb_gpu_render_single (renderCoord + float2 ( d.x, d.y), glyphLoc_));\n"
" (_hb_gpu_render_single (renderCoord + float2 (-d.x, -d.y), pixelsPerEm, glyphLoc_) +\n"
" _hb_gpu_render_single (renderCoord + float2 ( d.x, -d.y), pixelsPerEm, glyphLoc_) +\n"
" _hb_gpu_render_single (renderCoord + float2 (-d.x, d.y), pixelsPerEm, glyphLoc_) +\n"
" _hb_gpu_render_single (renderCoord + float2 ( d.x, d.y), pixelsPerEm, glyphLoc_));\n"
"\n"
" c = lerp (c, msaa, smoothstep (16.0, 8.0, ppem));\n"
" }\n"
+9 -9
View File
@@ -158,11 +158,9 @@ static const char *hb_gpu_fragment_msl =
"}\n"
"\n"
"/* Single-sample coverage in [0, 1]. */\n"
"float _hb_gpu_render_single (float2 renderCoord, uint glyphLoc_,\n"
"float _hb_gpu_render_single (float2 renderCoord, float2 pixelsPerEm, uint glyphLoc_,\n"
" device const short4* hb_gpu_atlas)\n"
"{\n"
" float2 emsPerPixel = fwidth (renderCoord);\n"
" float2 pixelsPerEm = 1.0 / emsPerPixel;\n"
"\n"
" _hb_gpu_glyph_info gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_, hb_gpu_atlas);\n"
" int glyphLoc = gi.glyphLoc;\n"
@@ -285,20 +283,22 @@ static const char *hb_gpu_fragment_msl =
"float hb_gpu_render (float2 renderCoord, uint glyphLoc_,\n"
" device const short4* hb_gpu_atlas)\n"
"{\n"
" float c = _hb_gpu_render_single (renderCoord, glyphLoc_, hb_gpu_atlas);\n"
" float2 emsPerPixel = fwidth (renderCoord);\n"
" float2 pixelsPerEm = 1.0 / emsPerPixel;\n"
"\n"
" float c = _hb_gpu_render_single (renderCoord, pixelsPerEm, glyphLoc_, hb_gpu_atlas);\n"
"\n"
"#ifndef HB_GPU_NO_MSAA\n"
" float ppem = hb_gpu_ppem (renderCoord, glyphLoc_, hb_gpu_atlas);\n"
" float2 emsPerPixel = fwidth (renderCoord);\n"
"\n"
" if (ppem < 16.0)\n"
" {\n"
" float2 d = emsPerPixel * (1.0 / 3.0);\n"
" float msaa = 0.25 *\n"
" (_hb_gpu_render_single (renderCoord + float2 (-d.x, -d.y), glyphLoc_, hb_gpu_atlas) +\n"
" _hb_gpu_render_single (renderCoord + float2 ( d.x, -d.y), glyphLoc_, hb_gpu_atlas) +\n"
" _hb_gpu_render_single (renderCoord + float2 (-d.x, d.y), glyphLoc_, hb_gpu_atlas) +\n"
" _hb_gpu_render_single (renderCoord + float2 ( d.x, d.y), glyphLoc_, hb_gpu_atlas));\n"
" (_hb_gpu_render_single (renderCoord + float2 (-d.x, -d.y), pixelsPerEm, glyphLoc_, hb_gpu_atlas) +\n"
" _hb_gpu_render_single (renderCoord + float2 ( d.x, -d.y), pixelsPerEm, glyphLoc_, hb_gpu_atlas) +\n"
" _hb_gpu_render_single (renderCoord + float2 (-d.x, d.y), pixelsPerEm, glyphLoc_, hb_gpu_atlas) +\n"
" _hb_gpu_render_single (renderCoord + float2 ( d.x, d.y), pixelsPerEm, glyphLoc_, hb_gpu_atlas));\n"
"\n"
" c = mix (c, msaa, smoothstep (16.0, 8.0, ppem));\n"
" }\n"
+9 -9
View File
@@ -160,11 +160,9 @@ static const char *hb_gpu_fragment_wgsl =
"}\n"
"\n"
"/* Single-sample coverage in [0, 1]. */\n"
"fn _hb_gpu_render_single (renderCoord: vec2f, glyphLoc_: u32,\n"
"fn _hb_gpu_render_single (renderCoord: vec2f, pixelsPerEm: vec2f, glyphLoc_: u32,\n"
" hb_gpu_atlas: ptr<storage, array<vec4<i32>>, read>) -> f32\n"
"{\n"
" let emsPerPixel = fwidth (renderCoord);\n"
" let pixelsPerEm = 1.0 / emsPerPixel;\n"
"\n"
" let gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_, hb_gpu_atlas);\n"
" let glyphLoc = gi.glyphLoc;\n"
@@ -293,19 +291,21 @@ static const char *hb_gpu_fragment_wgsl =
"fn hb_gpu_render (renderCoord: vec2f, glyphLoc_: u32,\n"
" hb_gpu_atlas: ptr<storage, array<vec4<i32>>, read>) -> f32\n"
"{\n"
" var c = _hb_gpu_render_single (renderCoord, glyphLoc_, hb_gpu_atlas);\n"
" let emsPerPixel = fwidth (renderCoord);\n"
" let pixelsPerEm = 1.0 / emsPerPixel;\n"
"\n"
" var c = _hb_gpu_render_single (renderCoord, pixelsPerEm, glyphLoc_, hb_gpu_atlas);\n"
"\n"
" let ppem = hb_gpu_ppem (renderCoord, glyphLoc_, hb_gpu_atlas);\n"
" let emsPerPixel = fwidth (renderCoord);\n"
"\n"
" if (ppem < 16.0)\n"
" {\n"
" let d = emsPerPixel * (1.0 / 3.0);\n"
" let msaa = 0.25 *\n"
" (_hb_gpu_render_single (renderCoord + vec2f (-d.x, -d.y), glyphLoc_, hb_gpu_atlas) +\n"
" _hb_gpu_render_single (renderCoord + vec2f ( d.x, -d.y), glyphLoc_, hb_gpu_atlas) +\n"
" _hb_gpu_render_single (renderCoord + vec2f (-d.x, d.y), glyphLoc_, hb_gpu_atlas) +\n"
" _hb_gpu_render_single (renderCoord + vec2f ( d.x, d.y), glyphLoc_, hb_gpu_atlas));\n"
" (_hb_gpu_render_single (renderCoord + vec2f (-d.x, -d.y), pixelsPerEm, glyphLoc_, hb_gpu_atlas) +\n"
" _hb_gpu_render_single (renderCoord + vec2f ( d.x, -d.y), pixelsPerEm, glyphLoc_, hb_gpu_atlas) +\n"
" _hb_gpu_render_single (renderCoord + vec2f (-d.x, d.y), pixelsPerEm, glyphLoc_, hb_gpu_atlas) +\n"
" _hb_gpu_render_single (renderCoord + vec2f ( d.x, d.y), pixelsPerEm, glyphLoc_, hb_gpu_atlas));\n"
"\n"
" c = mix (c, msaa, smoothstep (16.0, 8.0, ppem));\n"
" }\n"
+9 -9
View File
@@ -172,10 +172,8 @@ ivec2 _hb_gpu_curve_counts (vec2 renderCoord, uint glyphLoc_)
}
/* Single-sample coverage in [0, 1]. */
float _hb_gpu_render_single (vec2 renderCoord, uint glyphLoc_)
float _hb_gpu_render_single (vec2 renderCoord, vec2 pixelsPerEm, uint glyphLoc_)
{
vec2 emsPerPixel = fwidth (renderCoord);
vec2 pixelsPerEm = 1.0 / emsPerPixel;
_hb_gpu_glyph_info gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_);
int glyphLoc = gi.glyphLoc;
@@ -296,20 +294,22 @@ float _hb_gpu_render_single (vec2 renderCoord, uint glyphLoc_)
*/
float hb_gpu_render (vec2 renderCoord, uint glyphLoc_)
{
float c = _hb_gpu_render_single (renderCoord, glyphLoc_);
vec2 emsPerPixel = fwidth (renderCoord);
vec2 pixelsPerEm = 1.0 / emsPerPixel;
float c = _hb_gpu_render_single (renderCoord, pixelsPerEm, glyphLoc_);
#ifndef HB_GPU_NO_MSAA
float ppem = hb_gpu_ppem (renderCoord, glyphLoc_);
vec2 emsPerPixel = fwidth (renderCoord);
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_));
(_hb_gpu_render_single (renderCoord + vec2 (-d.x, -d.y), pixelsPerEm, glyphLoc_) +
_hb_gpu_render_single (renderCoord + vec2 ( d.x, -d.y), pixelsPerEm, glyphLoc_) +
_hb_gpu_render_single (renderCoord + vec2 (-d.x, d.y), pixelsPerEm, glyphLoc_) +
_hb_gpu_render_single (renderCoord + vec2 ( d.x, d.y), pixelsPerEm, glyphLoc_));
c = mix (c, msaa, smoothstep (16.0, 8.0, ppem));
}
+9 -9
View File
@@ -158,10 +158,8 @@ int2 _hb_gpu_curve_counts (float2 renderCoord, uint glyphLoc_)
/* Single-sample coverage in [0, 1]. */
float _hb_gpu_render_single (float2 renderCoord, uint glyphLoc_)
float _hb_gpu_render_single (float2 renderCoord, float2 pixelsPerEm, uint glyphLoc_)
{
float2 emsPerPixel = fwidth (renderCoord);
float2 pixelsPerEm = 1.0 / emsPerPixel;
_hb_gpu_glyph_info gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_);
int glyphLoc = gi.glyphLoc;
@@ -282,20 +280,22 @@ float _hb_gpu_render_single (float2 renderCoord, uint glyphLoc_)
*/
float hb_gpu_render (float2 renderCoord, uint glyphLoc_)
{
float c = _hb_gpu_render_single (renderCoord, glyphLoc_);
float2 emsPerPixel = fwidth (renderCoord);
float2 pixelsPerEm = 1.0 / emsPerPixel;
float c = _hb_gpu_render_single (renderCoord, pixelsPerEm, glyphLoc_);
#ifndef HB_GPU_NO_MSAA
float ppem = hb_gpu_ppem (renderCoord, glyphLoc_);
float2 emsPerPixel = fwidth (renderCoord);
if (ppem < 16.0)
{
float2 d = emsPerPixel * (1.0 / 3.0);
float msaa = 0.25 *
(_hb_gpu_render_single (renderCoord + float2 (-d.x, -d.y), glyphLoc_) +
_hb_gpu_render_single (renderCoord + float2 ( d.x, -d.y), glyphLoc_) +
_hb_gpu_render_single (renderCoord + float2 (-d.x, d.y), glyphLoc_) +
_hb_gpu_render_single (renderCoord + float2 ( d.x, d.y), glyphLoc_));
(_hb_gpu_render_single (renderCoord + float2 (-d.x, -d.y), pixelsPerEm, glyphLoc_) +
_hb_gpu_render_single (renderCoord + float2 ( d.x, -d.y), pixelsPerEm, glyphLoc_) +
_hb_gpu_render_single (renderCoord + float2 (-d.x, d.y), pixelsPerEm, glyphLoc_) +
_hb_gpu_render_single (renderCoord + float2 ( d.x, d.y), pixelsPerEm, glyphLoc_));
c = lerp (c, msaa, smoothstep (16.0, 8.0, ppem));
}
+9 -9
View File
@@ -157,11 +157,9 @@ int2 _hb_gpu_curve_counts (float2 renderCoord, uint glyphLoc_,
}
/* Single-sample coverage in [0, 1]. */
float _hb_gpu_render_single (float2 renderCoord, uint glyphLoc_,
float _hb_gpu_render_single (float2 renderCoord, float2 pixelsPerEm, uint glyphLoc_,
device const short4* hb_gpu_atlas)
{
float2 emsPerPixel = fwidth (renderCoord);
float2 pixelsPerEm = 1.0 / emsPerPixel;
_hb_gpu_glyph_info gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_, hb_gpu_atlas);
int glyphLoc = gi.glyphLoc;
@@ -284,20 +282,22 @@ float _hb_gpu_render_single (float2 renderCoord, uint glyphLoc_,
float hb_gpu_render (float2 renderCoord, uint glyphLoc_,
device const short4* hb_gpu_atlas)
{
float c = _hb_gpu_render_single (renderCoord, glyphLoc_, hb_gpu_atlas);
float2 emsPerPixel = fwidth (renderCoord);
float2 pixelsPerEm = 1.0 / emsPerPixel;
float c = _hb_gpu_render_single (renderCoord, pixelsPerEm, glyphLoc_, hb_gpu_atlas);
#ifndef HB_GPU_NO_MSAA
float ppem = hb_gpu_ppem (renderCoord, glyphLoc_, hb_gpu_atlas);
float2 emsPerPixel = fwidth (renderCoord);
if (ppem < 16.0)
{
float2 d = emsPerPixel * (1.0 / 3.0);
float msaa = 0.25 *
(_hb_gpu_render_single (renderCoord + float2 (-d.x, -d.y), glyphLoc_, hb_gpu_atlas) +
_hb_gpu_render_single (renderCoord + float2 ( d.x, -d.y), glyphLoc_, hb_gpu_atlas) +
_hb_gpu_render_single (renderCoord + float2 (-d.x, d.y), glyphLoc_, hb_gpu_atlas) +
_hb_gpu_render_single (renderCoord + float2 ( d.x, d.y), glyphLoc_, hb_gpu_atlas));
(_hb_gpu_render_single (renderCoord + float2 (-d.x, -d.y), pixelsPerEm, glyphLoc_, hb_gpu_atlas) +
_hb_gpu_render_single (renderCoord + float2 ( d.x, -d.y), pixelsPerEm, glyphLoc_, hb_gpu_atlas) +
_hb_gpu_render_single (renderCoord + float2 (-d.x, d.y), pixelsPerEm, glyphLoc_, hb_gpu_atlas) +
_hb_gpu_render_single (renderCoord + float2 ( d.x, d.y), pixelsPerEm, glyphLoc_, hb_gpu_atlas));
c = mix (c, msaa, smoothstep (16.0, 8.0, ppem));
}
+9 -9
View File
@@ -159,11 +159,9 @@ fn _hb_gpu_curve_counts (renderCoord: vec2f, glyphLoc_: u32,
}
/* Single-sample coverage in [0, 1]. */
fn _hb_gpu_render_single (renderCoord: vec2f, glyphLoc_: u32,
fn _hb_gpu_render_single (renderCoord: vec2f, pixelsPerEm: vec2f, glyphLoc_: u32,
hb_gpu_atlas: ptr<storage, array<vec4<i32>>, read>) -> f32
{
let emsPerPixel = fwidth (renderCoord);
let pixelsPerEm = 1.0 / emsPerPixel;
let gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_, hb_gpu_atlas);
let glyphLoc = gi.glyphLoc;
@@ -292,19 +290,21 @@ fn _hb_gpu_render_single (renderCoord: vec2f, glyphLoc_: u32,
fn hb_gpu_render (renderCoord: vec2f, glyphLoc_: u32,
hb_gpu_atlas: ptr<storage, array<vec4<i32>>, read>) -> f32
{
var c = _hb_gpu_render_single (renderCoord, glyphLoc_, hb_gpu_atlas);
let emsPerPixel = fwidth (renderCoord);
let pixelsPerEm = 1.0 / emsPerPixel;
var c = _hb_gpu_render_single (renderCoord, pixelsPerEm, glyphLoc_, hb_gpu_atlas);
let ppem = hb_gpu_ppem (renderCoord, glyphLoc_, hb_gpu_atlas);
let emsPerPixel = fwidth (renderCoord);
if (ppem < 16.0)
{
let d = emsPerPixel * (1.0 / 3.0);
let msaa = 0.25 *
(_hb_gpu_render_single (renderCoord + vec2f (-d.x, -d.y), glyphLoc_, hb_gpu_atlas) +
_hb_gpu_render_single (renderCoord + vec2f ( d.x, -d.y), glyphLoc_, hb_gpu_atlas) +
_hb_gpu_render_single (renderCoord + vec2f (-d.x, d.y), glyphLoc_, hb_gpu_atlas) +
_hb_gpu_render_single (renderCoord + vec2f ( d.x, d.y), glyphLoc_, hb_gpu_atlas));
(_hb_gpu_render_single (renderCoord + vec2f (-d.x, -d.y), pixelsPerEm, glyphLoc_, hb_gpu_atlas) +
_hb_gpu_render_single (renderCoord + vec2f ( d.x, -d.y), pixelsPerEm, glyphLoc_, hb_gpu_atlas) +
_hb_gpu_render_single (renderCoord + vec2f (-d.x, d.y), pixelsPerEm, glyphLoc_, hb_gpu_atlas) +
_hb_gpu_render_single (renderCoord + vec2f ( d.x, d.y), pixelsPerEm, glyphLoc_, hb_gpu_atlas));
c = mix (c, msaa, smoothstep (16.0, 8.0, ppem));
}