diff --git a/src/hb-gpu-draw-fragment-glsl.hh b/src/hb-gpu-draw-fragment-glsl.hh index 5bcfad908..9f645860a 100644 --- a/src/hb-gpu-draw-fragment-glsl.hh +++ b/src/hb-gpu-draw-fragment-glsl.hh @@ -1,10 +1,6 @@ static const char *hb_gpu_draw_fragment_glsl = "/*\n" " * Copyright (C) 2026 Behdad Esfahbod\n" -" * Copyright (C) 2017 Eric Lengyel\n" -" *\n" -" * Based on the Slug algorithm by Eric Lengyel:\n" -" * https://github.com/EricLengyel/Slug\n" " *\n" " * This is part of HarfBuzz, a text shaping library.\n" " *\n" @@ -28,288 +24,17 @@ static const char *hb_gpu_draw_fragment_glsl = " */\n" "\n" "\n" -"/* Draw-renderer fragment shader.\n" -" *\n" -" * Assumes the shared fragment helpers (hb-gpu-fragment.glsl) are\n" -" * prepended to this source.\n" -" */\n" +"/* Draw-renderer fragment shader entry. The heavy lifting (Slug\n" +" * coverage, MSAA, ppem, stem darkening) lives in the shared\n" +" * hb-gpu-fragment.glsl that must be prepended to this source;\n" +" * this file only adds the thin hb_gpu_draw() wrapper that lifts\n" +" * pixelsPerEm out of fwidth() at uniform control flow before\n" +" * calling the shared _hb_gpu_slug(). */\n" "\n" "\n" -"uint _hb_gpu_calc_root_code (float y1, float y2, float y3)\n" -"{\n" -" uint i1 = floatBitsToUint (y1) >> 31U;\n" -" uint i2 = floatBitsToUint (y2) >> 30U;\n" -" uint i3 = floatBitsToUint (y3) >> 29U;\n" -"\n" -" uint shift = (i2 & 2U) | (i1 & ~2U);\n" -" shift = (i3 & 4U) | (shift & ~4U);\n" -"\n" -" return (0x2E74U >> shift) & 0x0101U;\n" -"}\n" -"\n" -"vec2 _hb_gpu_solve_horiz_poly (vec2 a, vec2 b, vec2 p1)\n" -"{\n" -" float ra = 1.0 / a.y;\n" -" float rb = 0.5 / b.y;\n" -"\n" -" float d = sqrt (max (b.y * b.y - a.y * p1.y, 0.0));\n" -" float t1 = (b.y - d) * ra;\n" -" float t2 = (b.y + d) * ra;\n" -"\n" -" if (a.y == 0.0)\n" -" t1 = t2 = p1.y * rb;\n" -"\n" -" return vec2 ((a.x * t1 - b.x * 2.0) * t1 + p1.x,\n" -" (a.x * t2 - b.x * 2.0) * t2 + p1.x);\n" -"}\n" -"\n" -"vec2 _hb_gpu_solve_vert_poly (vec2 a, vec2 b, vec2 p1)\n" -"{\n" -" float ra = 1.0 / a.x;\n" -" float rb = 0.5 / b.x;\n" -"\n" -" float d = sqrt (max (b.x * b.x - a.x * p1.x, 0.0));\n" -" float t1 = (b.x - d) * ra;\n" -" float t2 = (b.x + d) * ra;\n" -"\n" -" if (a.x == 0.0)\n" -" t1 = t2 = p1.x * rb;\n" -"\n" -" return vec2 ((a.y * t1 - b.y * 2.0) * t1 + p1.y,\n" -" (a.y * t2 - b.y * 2.0) * t2 + p1.y);\n" -"}\n" -"\n" -"float _hb_gpu_calc_coverage (float xcov, float ycov, float xwgt, float ywgt)\n" -"{\n" -" float coverage = max (abs (xcov * xwgt + ycov * ywgt) /\n" -" max (xwgt + ywgt, 1.0 / 65536.0),\n" -" min (abs (xcov), abs (ycov)));\n" -"\n" -" return clamp (coverage, 0.0, 1.0);\n" -"}\n" -"\n" -"/* Decoded glyph band info for a pixel position. */\n" -"struct _hb_gpu_glyph_info\n" -"{\n" -" int glyphLoc;\n" -" int bandBase;\n" -" ivec2 bandIndex;\n" -" int numHBands;\n" -" int numVBands;\n" -" vec2 scale;\n" -"};\n" -"\n" -"_hb_gpu_glyph_info _hb_gpu_decode_glyph (vec2 renderCoord, uint glyphLoc_)\n" -"{\n" -" _hb_gpu_glyph_info gi;\n" -" gi.glyphLoc = int (glyphLoc_);\n" -"\n" -" ivec4 header0 = hb_gpu_fetch (gi.glyphLoc);\n" -" ivec4 header1 = hb_gpu_fetch (gi.glyphLoc + 1);\n" -" vec4 ext = vec4 (header0) * HB_GPU_INV_UNITS;\n" -" gi.numHBands = header1.r;\n" -" gi.numVBands = header1.g;\n" -" gi.scale = vec2 (float (header1.b), float (header1.a));\n" -"\n" -" vec2 extSize = ext.zw - ext.xy;\n" -" vec2 bandScale = vec2 (float (gi.numVBands), float (gi.numHBands)) / max (extSize, vec2 (1.0 / 65536.0));\n" -" vec2 bandOffset = -ext.xy * bandScale;\n" -"\n" -" gi.bandIndex = clamp (ivec2 (renderCoord * bandScale + bandOffset),\n" -" ivec2 (0, 0),\n" -" ivec2 (gi.numVBands - 1, gi.numHBands - 1));\n" -"\n" -" gi.bandBase = gi.glyphLoc + 2;\n" -" return gi;\n" -"}\n" -"\n" -"/* Return pixels per em at this fragment.\n" -" *\n" -" * renderCoord: em-space sample position\n" -" * glyphLoc: texel offset of glyph blob in atlas\n" -" */\n" -"float hb_gpu_ppem (vec2 renderCoord, uint glyphLoc_)\n" -"{\n" -" _hb_gpu_glyph_info gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_);\n" -" vec2 emsPerPixel = fwidth (renderCoord);\n" -" return min (gi.scale.x, gi.scale.y) /\n" -" max (emsPerPixel.x, emsPerPixel.y);\n" -"}\n" -"\n" -"/* Return per-pixel curve counts: (horizontal, vertical). */\n" -"ivec2 _hb_gpu_curve_counts (vec2 renderCoord, uint glyphLoc_)\n" -"{\n" -" _hb_gpu_glyph_info gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_);\n" -" int hCount = hb_gpu_fetch (gi.bandBase + gi.bandIndex.y).r;\n" -" int vCount = hb_gpu_fetch (gi.bandBase + gi.numHBands + gi.bandIndex.x).r;\n" -" return ivec2 (hCount, vCount);\n" -"}\n" -"\n" -"/* Single-sample coverage in [0, 1]. */\n" -"float _hb_gpu_draw_single (vec2 renderCoord, vec2 pixelsPerEm, uint glyphLoc_)\n" -"{\n" -"\n" -" _hb_gpu_glyph_info gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_);\n" -" int glyphLoc = gi.glyphLoc;\n" -" int bandBase = gi.bandBase;\n" -" int numHBands = gi.numHBands;\n" -"\n" -" float xcov = 0.0;\n" -" float xwgt = 0.0;\n" -"\n" -" ivec4 hbandData = hb_gpu_fetch (bandBase + gi.bandIndex.y);\n" -" int hCurveCount = hbandData.r;\n" -" /* Symmetric: choose rightward (desc) or leftward (asc) sort */\n" -" float hSplit = float (hbandData.a) * HB_GPU_INV_UNITS;\n" -" bool hLeftRay = (renderCoord.x < hSplit);\n" -" int hDataOffset = (hLeftRay ? hbandData.b : hbandData.g) + 32768;\n" -"\n" -" for (int ci = 0; ci < hCurveCount; ci++)\n" -" {\n" -" int curveOffset = hb_gpu_fetch (glyphLoc + hDataOffset + ci).r + 32768;\n" -"\n" -" ivec4 raw12 = hb_gpu_fetch (glyphLoc + curveOffset);\n" -" ivec4 raw3 = hb_gpu_fetch (glyphLoc + curveOffset + 1);\n" -"\n" -" vec4 q12 = vec4 (raw12) * HB_GPU_INV_UNITS;\n" -" vec2 q3 = vec2 (raw3.rg) * HB_GPU_INV_UNITS;\n" -"\n" -" vec4 p12 = q12 - vec4 (renderCoord, renderCoord);\n" -" vec2 p3 = q3 - renderCoord;\n" -"\n" -" if (hLeftRay) {\n" -" if (min (min (p12.x, p12.z), p3.x) * pixelsPerEm.x > 0.5) break;\n" -" } else {\n" -" if (max (max (p12.x, p12.z), p3.x) * pixelsPerEm.x < -0.5) break;\n" -" }\n" -"\n" -" uint code = _hb_gpu_calc_root_code (p12.y, p12.w, p3.y);\n" -" if (code != 0U)\n" -" {\n" -" vec2 a = q12.xy - q12.zw * 2.0 + q3;\n" -" vec2 b = q12.xy - q12.zw;\n" -" vec2 r = _hb_gpu_solve_horiz_poly (a, b, p12.xy) * pixelsPerEm.x;\n" -" /* For leftward ray: saturate(0.5 - r) counts coverage from the left */\n" -" vec2 cov = hLeftRay ? clamp (vec2 (0.5) - r, 0.0, 1.0)\n" -" : clamp (r + vec2 (0.5), 0.0, 1.0);\n" -"\n" -" if ((code & 1U) != 0U)\n" -" {\n" -" xcov += cov.x;\n" -" xwgt = max (xwgt, clamp (1.0 - abs (r.x) * 2.0, 0.0, 1.0));\n" -" }\n" -"\n" -" if (code > 1U)\n" -" {\n" -" xcov -= cov.y;\n" -" xwgt = max (xwgt, clamp (1.0 - abs (r.y) * 2.0, 0.0, 1.0));\n" -" }\n" -" }\n" -" }\n" -"\n" -" float ycov = 0.0;\n" -" float ywgt = 0.0;\n" -"\n" -" ivec4 vbandData = hb_gpu_fetch (bandBase + numHBands + gi.bandIndex.x);\n" -" int vCurveCount = vbandData.r;\n" -" float vSplit = float (vbandData.a) * HB_GPU_INV_UNITS;\n" -" bool vLeftRay = (renderCoord.y < vSplit);\n" -" int vDataOffset = (vLeftRay ? vbandData.b : vbandData.g) + 32768;\n" -"\n" -" for (int ci = 0; ci < vCurveCount; ci++)\n" -" {\n" -" int curveOffset = hb_gpu_fetch (glyphLoc + vDataOffset + ci).r + 32768;\n" -"\n" -" ivec4 raw12 = hb_gpu_fetch (glyphLoc + curveOffset);\n" -" ivec4 raw3 = hb_gpu_fetch (glyphLoc + curveOffset + 1);\n" -"\n" -" vec4 q12 = vec4 (raw12) * HB_GPU_INV_UNITS;\n" -" vec2 q3 = vec2 (raw3.rg) * HB_GPU_INV_UNITS;\n" -"\n" -" vec4 p12 = q12 - vec4 (renderCoord, renderCoord);\n" -" vec2 p3 = q3 - renderCoord;\n" -"\n" -" if (vLeftRay) {\n" -" if (min (min (p12.y, p12.w), p3.y) * pixelsPerEm.y > 0.5) break;\n" -" } else {\n" -" if (max (max (p12.y, p12.w), p3.y) * pixelsPerEm.y < -0.5) break;\n" -" }\n" -"\n" -" uint code = _hb_gpu_calc_root_code (p12.x, p12.z, p3.x);\n" -" if (code != 0U)\n" -" {\n" -" vec2 a = q12.xy - q12.zw * 2.0 + q3;\n" -" vec2 b = q12.xy - q12.zw;\n" -" vec2 r = _hb_gpu_solve_vert_poly (a, b, p12.xy) * pixelsPerEm.y;\n" -" vec2 cov = vLeftRay ? clamp (vec2 (0.5) - r, 0.0, 1.0)\n" -" : clamp (r + vec2 (0.5), 0.0, 1.0);\n" -"\n" -" if ((code & 1U) != 0U)\n" -" {\n" -" ycov -= cov.x;\n" -" ywgt = max (ywgt, clamp (1.0 - abs (r.x) * 2.0, 0.0, 1.0));\n" -" }\n" -"\n" -" if (code > 1U)\n" -" {\n" -" ycov += cov.y;\n" -" ywgt = max (ywgt, clamp (1.0 - abs (r.y) * 2.0, 0.0, 1.0));\n" -" }\n" -" }\n" -" }\n" -"\n" -" return _hb_gpu_calc_coverage (xcov, ycov, xwgt, ywgt);\n" -"}\n" -"\n" -"/* Return coverage in [0, 1].\n" -" *\n" -" * renderCoord: em-space sample position\n" -" * glyphLoc: texel offset of glyph blob in atlas\n" -" */\n" -"/* The MSAA-aware implementation. Caller supplies pixelsPerEm so\n" -" * this function can be invoked from non-uniform control flow (for\n" -" * example inside an op-stream branch in hb-gpu-paint-fragment.wgsl,\n" -" * where WGSL would otherwise reject an fwidth call). */\n" -"float _hb_gpu_draw_impl (vec2 renderCoord, vec2 pixelsPerEm, uint glyphLoc_)\n" -"{\n" -" float c = _hb_gpu_draw_single (renderCoord, pixelsPerEm, glyphLoc_);\n" -"\n" -"#ifndef HB_GPU_NO_MSAA\n" -" float ppem = hb_gpu_ppem (renderCoord, glyphLoc_);\n" -"\n" -" if (ppem < 16.0)\n" -" {\n" -" vec2 emsPerPixel = 1.0 / pixelsPerEm;\n" -" vec2 d = emsPerPixel * (1.0 / 3.0);\n" -" float msaa = 0.25 *\n" -" (_hb_gpu_draw_single (renderCoord + vec2 (-d.x, -d.y), pixelsPerEm, glyphLoc_) +\n" -" _hb_gpu_draw_single (renderCoord + vec2 ( d.x, -d.y), pixelsPerEm, glyphLoc_) +\n" -" _hb_gpu_draw_single (renderCoord + vec2 (-d.x, d.y), pixelsPerEm, glyphLoc_) +\n" -" _hb_gpu_draw_single (renderCoord + vec2 ( d.x, d.y), pixelsPerEm, glyphLoc_));\n" -"\n" -" c = mix (c, msaa, smoothstep (16.0, 8.0, ppem));\n" -" }\n" -"#endif\n" -"\n" -" return c;\n" -"}\n" -"\n" "float hb_gpu_draw (vec2 renderCoord, uint glyphLoc_)\n" "{\n" " vec2 pixelsPerEm = 1.0 / fwidth (renderCoord);\n" -" return _hb_gpu_draw_impl (renderCoord, pixelsPerEm, glyphLoc_);\n" -"}\n" -"\n" -"/* Stem darkening for small sizes.\n" -" *\n" -" * coverage: output of hb_gpu_draw\n" -" * brightness: foreground brightness in [0, 1]\n" -" * ppem: pixels per em at this fragment\n" -" */\n" -"float hb_gpu_stem_darken (float coverage, float brightness, float ppem)\n" -"{\n" -" return pow (coverage,\n" -" mix (pow (2.0, brightness - 0.5), 1.0,\n" -" smoothstep (8.0, 48.0, ppem)));\n" +" return _hb_gpu_slug (renderCoord, pixelsPerEm, glyphLoc_);\n" "}\n" ; diff --git a/src/hb-gpu-draw-fragment-hlsl.hh b/src/hb-gpu-draw-fragment-hlsl.hh index 579ba7816..740d810be 100644 --- a/src/hb-gpu-draw-fragment-hlsl.hh +++ b/src/hb-gpu-draw-fragment-hlsl.hh @@ -1,10 +1,6 @@ static const char *hb_gpu_draw_fragment_hlsl = "/*\n" " * Copyright (C) 2026 Behdad Esfahbod\n" -" * Copyright (C) 2017 Eric Lengyel\n" -" *\n" -" * Based on the Slug algorithm by Eric Lengyel:\n" -" * https://github.com/EricLengyel/Slug\n" " *\n" " * This is part of HarfBuzz, a text shaping library.\n" " *\n" @@ -28,287 +24,14 @@ static const char *hb_gpu_draw_fragment_hlsl = " */\n" "\n" "\n" -"/* Draw-renderer fragment shader.\n" -" *\n" -" * Assumes the shared fragment helpers (hb-gpu-fragment.hlsl) are\n" -" * prepended to this source.\n" -" */\n" +"/* Draw-renderer fragment shader entry (HLSL). Heavy lifting\n" +" * (Slug coverage, MSAA, ppem, stem darkening) lives in the shared\n" +" * hb-gpu-fragment.hlsl that must be prepended to this source. */\n" "\n" "\n" -"uint _hb_gpu_calc_root_code (float y1, float y2, float y3)\n" -"{\n" -" uint i1 = asuint (y1) >> 31u;\n" -" uint i2 = asuint (y2) >> 30u;\n" -" uint i3 = asuint (y3) >> 29u;\n" -"\n" -" uint shift = (i2 & 2u) | (i1 & ~2u);\n" -" shift = (i3 & 4u) | (shift & ~4u);\n" -"\n" -" return (0x2E74u >> shift) & 0x0101u;\n" -"}\n" -"\n" -"float2 _hb_gpu_solve_horiz_poly (float2 a, float2 b, float2 p1)\n" -"{\n" -" float ra = 1.0 / a.y;\n" -" float rb = 0.5 / b.y;\n" -"\n" -" float d = sqrt (max (b.y * b.y - a.y * p1.y, 0.0));\n" -" float t1 = (b.y - d) * ra;\n" -" float t2 = (b.y + d) * ra;\n" -"\n" -" if (a.y == 0.0)\n" -" t1 = t2 = p1.y * rb;\n" -"\n" -" return float2 ((a.x * t1 - b.x * 2.0) * t1 + p1.x,\n" -" (a.x * t2 - b.x * 2.0) * t2 + p1.x);\n" -"}\n" -"\n" -"float2 _hb_gpu_solve_vert_poly (float2 a, float2 b, float2 p1)\n" -"{\n" -" float ra = 1.0 / a.x;\n" -" float rb = 0.5 / b.x;\n" -"\n" -" float d = sqrt (max (b.x * b.x - a.x * p1.x, 0.0));\n" -" float t1 = (b.x - d) * ra;\n" -" float t2 = (b.x + d) * ra;\n" -"\n" -" if (a.x == 0.0)\n" -" t1 = t2 = p1.x * rb;\n" -"\n" -" return float2 ((a.y * t1 - b.y * 2.0) * t1 + p1.y,\n" -" (a.y * t2 - b.y * 2.0) * t2 + p1.y);\n" -"}\n" -"\n" -"float _hb_gpu_calc_coverage (float xcov, float ycov, float xwgt, float ywgt)\n" -"{\n" -" float coverage = max (abs (xcov * xwgt + ycov * ywgt) /\n" -" max (xwgt + ywgt, 1.0 / 65536.0),\n" -" min (abs (xcov), abs (ycov)));\n" -"\n" -" return clamp (coverage, 0.0, 1.0);\n" -"}\n" -"\n" -"\n" -"struct _hb_gpu_glyph_info\n" -"{\n" -" int glyphLoc;\n" -" int bandBase;\n" -" int2 bandIndex;\n" -" int numHBands;\n" -" int numVBands;\n" -" float2 scale;\n" -"};\n" -"\n" -"_hb_gpu_glyph_info _hb_gpu_decode_glyph (float2 renderCoord, uint glyphLoc_)\n" -"{\n" -" _hb_gpu_glyph_info gi;\n" -" gi.glyphLoc = (int) glyphLoc_;\n" -"\n" -" int4 header0 = hb_gpu_fetch (gi.glyphLoc);\n" -" int4 header1 = hb_gpu_fetch (gi.glyphLoc + 1);\n" -" float4 ext = (float4) header0 * HB_GPU_INV_UNITS;\n" -" gi.numHBands = header1.r;\n" -" gi.numVBands = header1.g;\n" -" gi.scale = float2 ((float) header1.b, (float) header1.a);\n" -"\n" -" float2 extSize = ext.zw - ext.xy;\n" -" float2 bandScale = float2 ((float) gi.numVBands, (float) gi.numHBands) / max (extSize, float2 (1.0 / 65536.0, 1.0 / 65536.0));\n" -" float2 bandOffset = -ext.xy * bandScale;\n" -"\n" -" gi.bandIndex = clamp ((int2) (renderCoord * bandScale + bandOffset),\n" -" int2 (0, 0),\n" -" int2 (gi.numVBands - 1, gi.numHBands - 1));\n" -"\n" -" gi.bandBase = gi.glyphLoc + 2;\n" -" return gi;\n" -"}\n" -"\n" -"/* Return pixels per em at this fragment.\n" -" *\n" -" * renderCoord: em-space sample position\n" -" * glyphLoc: texel offset of glyph blob in atlas\n" -" */\n" -"float hb_gpu_ppem (float2 renderCoord, uint glyphLoc_)\n" -"{\n" -" _hb_gpu_glyph_info gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_);\n" -" float2 emsPerPixel = fwidth (renderCoord);\n" -" return min (gi.scale.x, gi.scale.y) /\n" -" max (emsPerPixel.x, emsPerPixel.y);\n" -"}\n" -"\n" -"int2 _hb_gpu_curve_counts (float2 renderCoord, uint glyphLoc_)\n" -"{\n" -" _hb_gpu_glyph_info gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_);\n" -" int hCount = hb_gpu_fetch (gi.bandBase + gi.bandIndex.y).r;\n" -" int vCount = hb_gpu_fetch (gi.bandBase + gi.numHBands + gi.bandIndex.x).r;\n" -" return int2 (hCount, vCount);\n" -"}\n" -"\n" -"\n" -"/* Single-sample coverage in [0, 1]. */\n" -"float _hb_gpu_draw_single (float2 renderCoord, float2 pixelsPerEm, uint glyphLoc_)\n" -"{\n" -"\n" -" _hb_gpu_glyph_info gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_);\n" -" int glyphLoc = gi.glyphLoc;\n" -" int bandBase = gi.bandBase;\n" -" int numHBands = gi.numHBands;\n" -"\n" -" float xcov = 0.0;\n" -" float xwgt = 0.0;\n" -"\n" -" int4 hbandData = hb_gpu_fetch (bandBase + gi.bandIndex.y);\n" -" int hCurveCount = hbandData.r;\n" -" float hSplit = (float) hbandData.a * HB_GPU_INV_UNITS;\n" -" bool hLeftRay = (renderCoord.x < hSplit);\n" -" int hDataOffset = (hLeftRay ? hbandData.b : hbandData.g) + 32768;\n" -"\n" -" for (int ci = 0; ci < hCurveCount; ci++)\n" -" {\n" -" int curveOffset = hb_gpu_fetch (glyphLoc + hDataOffset + ci).r + 32768;\n" -"\n" -" int4 raw12 = hb_gpu_fetch (glyphLoc + curveOffset);\n" -" int4 raw3 = hb_gpu_fetch (glyphLoc + curveOffset + 1);\n" -"\n" -" float4 q12 = (float4) raw12 * HB_GPU_INV_UNITS;\n" -" float2 q3 = (float2) raw3.rg * HB_GPU_INV_UNITS;\n" -"\n" -" float4 p12 = q12 - float4 (renderCoord, renderCoord);\n" -" float2 p3 = q3 - renderCoord;\n" -"\n" -" if (hLeftRay) {\n" -" if (min (min (p12.x, p12.z), p3.x) * pixelsPerEm.x > 0.5) break;\n" -" } else {\n" -" if (max (max (p12.x, p12.z), p3.x) * pixelsPerEm.x < -0.5) break;\n" -" }\n" -"\n" -" uint code = _hb_gpu_calc_root_code (p12.y, p12.w, p3.y);\n" -" if (code != 0u)\n" -" {\n" -" float2 a = q12.xy - q12.zw * 2.0 + q3;\n" -" float2 b = q12.xy - q12.zw;\n" -" float2 r = _hb_gpu_solve_horiz_poly (a, b, p12.xy) * pixelsPerEm.x;\n" -" float2 cov = hLeftRay ? clamp (float2 (0.5, 0.5) - r, 0.0, 1.0)\n" -" : clamp (r + float2 (0.5, 0.5), 0.0, 1.0);\n" -"\n" -" if ((code & 1u) != 0u)\n" -" {\n" -" xcov += cov.x;\n" -" xwgt = max (xwgt, clamp (1.0 - abs (r.x) * 2.0, 0.0, 1.0));\n" -" }\n" -"\n" -" if (code > 1u)\n" -" {\n" -" xcov -= cov.y;\n" -" xwgt = max (xwgt, clamp (1.0 - abs (r.y) * 2.0, 0.0, 1.0));\n" -" }\n" -" }\n" -" }\n" -"\n" -" float ycov = 0.0;\n" -" float ywgt = 0.0;\n" -"\n" -" int4 vbandData = hb_gpu_fetch (bandBase + numHBands + gi.bandIndex.x);\n" -" int vCurveCount = vbandData.r;\n" -" float vSplit = (float) vbandData.a * HB_GPU_INV_UNITS;\n" -" bool vLeftRay = (renderCoord.y < vSplit);\n" -" int vDataOffset = (vLeftRay ? vbandData.b : vbandData.g) + 32768;\n" -"\n" -" for (int ci = 0; ci < vCurveCount; ci++)\n" -" {\n" -" int curveOffset = hb_gpu_fetch (glyphLoc + vDataOffset + ci).r + 32768;\n" -"\n" -" int4 raw12 = hb_gpu_fetch (glyphLoc + curveOffset);\n" -" int4 raw3 = hb_gpu_fetch (glyphLoc + curveOffset + 1);\n" -"\n" -" float4 q12 = (float4) raw12 * HB_GPU_INV_UNITS;\n" -" float2 q3 = (float2) raw3.rg * HB_GPU_INV_UNITS;\n" -"\n" -" float4 p12 = q12 - float4 (renderCoord, renderCoord);\n" -" float2 p3 = q3 - renderCoord;\n" -"\n" -" if (vLeftRay) {\n" -" if (min (min (p12.y, p12.w), p3.y) * pixelsPerEm.y > 0.5) break;\n" -" } else {\n" -" if (max (max (p12.y, p12.w), p3.y) * pixelsPerEm.y < -0.5) break;\n" -" }\n" -"\n" -" uint code = _hb_gpu_calc_root_code (p12.x, p12.z, p3.x);\n" -" if (code != 0u)\n" -" {\n" -" float2 a = q12.xy - q12.zw * 2.0 + q3;\n" -" float2 b = q12.xy - q12.zw;\n" -" float2 r = _hb_gpu_solve_vert_poly (a, b, p12.xy) * pixelsPerEm.y;\n" -" float2 cov = vLeftRay ? clamp (float2 (0.5, 0.5) - r, 0.0, 1.0)\n" -" : clamp (r + float2 (0.5, 0.5), 0.0, 1.0);\n" -"\n" -" if ((code & 1u) != 0u)\n" -" {\n" -" ycov -= cov.x;\n" -" ywgt = max (ywgt, clamp (1.0 - abs (r.x) * 2.0, 0.0, 1.0));\n" -" }\n" -"\n" -" if (code > 1u)\n" -" {\n" -" ycov += cov.y;\n" -" ywgt = max (ywgt, clamp (1.0 - abs (r.y) * 2.0, 0.0, 1.0));\n" -" }\n" -" }\n" -" }\n" -"\n" -" return _hb_gpu_calc_coverage (xcov, ycov, xwgt, ywgt);\n" -"}\n" -"\n" -"/* Return coverage in [0, 1].\n" -" *\n" -" * Caller must declare: StructuredBuffer hb_gpu_atlas\n" -" *\n" -" * renderCoord: em-space sample position\n" -" * glyphLoc: texel offset of glyph blob in atlas\n" -" */\n" -"/* The MSAA-aware implementation. Caller supplies pixelsPerEm so\n" -" * this function can be invoked from non-uniform control flow (for\n" -" * example from a paint op-stream branch). */\n" -"float _hb_gpu_draw_impl (float2 renderCoord, float2 pixelsPerEm, uint glyphLoc_)\n" -"{\n" -" float c = _hb_gpu_draw_single (renderCoord, pixelsPerEm, glyphLoc_);\n" -"\n" -"#ifndef HB_GPU_NO_MSAA\n" -" float ppem = hb_gpu_ppem (renderCoord, glyphLoc_);\n" -"\n" -" if (ppem < 16.0)\n" -" {\n" -" float2 emsPerPixel = 1.0 / pixelsPerEm;\n" -" float2 d = emsPerPixel * (1.0 / 3.0);\n" -" float msaa = 0.25 *\n" -" (_hb_gpu_draw_single (renderCoord + float2 (-d.x, -d.y), pixelsPerEm, glyphLoc_) +\n" -" _hb_gpu_draw_single (renderCoord + float2 ( d.x, -d.y), pixelsPerEm, glyphLoc_) +\n" -" _hb_gpu_draw_single (renderCoord + float2 (-d.x, d.y), pixelsPerEm, glyphLoc_) +\n" -" _hb_gpu_draw_single (renderCoord + float2 ( d.x, d.y), pixelsPerEm, glyphLoc_));\n" -"\n" -" c = lerp (c, msaa, smoothstep (16.0, 8.0, ppem));\n" -" }\n" -"#endif\n" -"\n" -" return c;\n" -"}\n" -"\n" "float hb_gpu_draw (float2 renderCoord, uint glyphLoc_)\n" "{\n" " float2 pixelsPerEm = 1.0 / fwidth (renderCoord);\n" -" return _hb_gpu_draw_impl (renderCoord, pixelsPerEm, glyphLoc_);\n" -"}\n" -"\n" -"/* Stem darkening for small sizes.\n" -" *\n" -" * coverage: output of hb_gpu_draw\n" -" * brightness: foreground brightness in [0, 1]\n" -" * ppem: pixels per em at this fragment\n" -" */\n" -"float hb_gpu_stem_darken (float coverage, float brightness, float ppem)\n" -"{\n" -" return pow (coverage,\n" -" lerp (pow (2.0, brightness - 0.5), 1.0,\n" -" smoothstep (8.0, 48.0, ppem)));\n" +" return _hb_gpu_slug (renderCoord, pixelsPerEm, glyphLoc_);\n" "}\n" ; diff --git a/src/hb-gpu-draw-fragment-msl.hh b/src/hb-gpu-draw-fragment-msl.hh index 450153ca8..0c202eacf 100644 --- a/src/hb-gpu-draw-fragment-msl.hh +++ b/src/hb-gpu-draw-fragment-msl.hh @@ -1,10 +1,6 @@ static const char *hb_gpu_draw_fragment_msl = "/*\n" " * Copyright (C) 2026 Behdad Esfahbod\n" -" * Copyright (C) 2017 Eric Lengyel\n" -" *\n" -" * Based on the Slug algorithm by Eric Lengyel:\n" -" * https://github.com/EricLengyel/Slug\n" " *\n" " * This is part of HarfBuzz, a text shaping library.\n" " *\n" @@ -28,296 +24,15 @@ static const char *hb_gpu_draw_fragment_msl = " */\n" "\n" "\n" -"/* Draw-renderer fragment shader.\n" -" *\n" -" * Assumes the shared fragment helpers (hb-gpu-fragment.msl) are\n" -" * prepended to this source.\n" -" */\n" +"/* Draw-renderer fragment shader entry (Metal). Heavy lifting\n" +" * (Slug coverage, MSAA, ppem, stem darkening) lives in the shared\n" +" * hb-gpu-fragment.msl that must be prepended to this source. */\n" "\n" "\n" -"\n" -"uint _hb_gpu_calc_root_code (float y1, float y2, float y3)\n" -"{\n" -" uint i1 = as_type (y1) >> 31U;\n" -" uint i2 = as_type (y2) >> 30U;\n" -" uint i3 = as_type (y3) >> 29U;\n" -"\n" -" uint shift = (i2 & 2U) | (i1 & ~2U);\n" -" shift = (i3 & 4U) | (shift & ~4U);\n" -"\n" -" return (0x2E74U >> shift) & 0x0101U;\n" -"}\n" -"\n" -"float2 _hb_gpu_solve_horiz_poly (float2 a, float2 b, float2 p1)\n" -"{\n" -" float ra = 1.0 / a.y;\n" -" float rb = 0.5 / b.y;\n" -"\n" -" float d = sqrt (max (b.y * b.y - a.y * p1.y, 0.0));\n" -" float t1 = (b.y - d) * ra;\n" -" float t2 = (b.y + d) * ra;\n" -"\n" -" if (a.y == 0.0)\n" -" t1 = t2 = p1.y * rb;\n" -"\n" -" return float2 ((a.x * t1 - b.x * 2.0) * t1 + p1.x,\n" -" (a.x * t2 - b.x * 2.0) * t2 + p1.x);\n" -"}\n" -"\n" -"float2 _hb_gpu_solve_vert_poly (float2 a, float2 b, float2 p1)\n" -"{\n" -" float ra = 1.0 / a.x;\n" -" float rb = 0.5 / b.x;\n" -"\n" -" float d = sqrt (max (b.x * b.x - a.x * p1.x, 0.0));\n" -" float t1 = (b.x - d) * ra;\n" -" float t2 = (b.x + d) * ra;\n" -"\n" -" if (a.x == 0.0)\n" -" t1 = t2 = p1.x * rb;\n" -"\n" -" return float2 ((a.y * t1 - b.y * 2.0) * t1 + p1.y,\n" -" (a.y * t2 - b.y * 2.0) * t2 + p1.y);\n" -"}\n" -"\n" -"float _hb_gpu_calc_coverage (float xcov, float ycov, float xwgt, float ywgt)\n" -"{\n" -" float coverage = max (abs (xcov * xwgt + ycov * ywgt) /\n" -" max (xwgt + ywgt, 1.0 / 65536.0),\n" -" min (abs (xcov), abs (ycov)));\n" -"\n" -" return clamp (coverage, 0.0, 1.0);\n" -"}\n" -"\n" -"/* Decoded glyph band info for a pixel position. */\n" -"struct _hb_gpu_glyph_info\n" -"{\n" -" int glyphLoc;\n" -" int bandBase;\n" -" int2 bandIndex;\n" -" int numHBands;\n" -" int numVBands;\n" -" float2 scale;\n" -"};\n" -"\n" -"_hb_gpu_glyph_info _hb_gpu_decode_glyph (float2 renderCoord, uint glyphLoc_,\n" -" device const short4* hb_gpu_atlas)\n" -"{\n" -" _hb_gpu_glyph_info gi;\n" -" gi.glyphLoc = int (glyphLoc_);\n" -"\n" -" int4 header0 = hb_gpu_fetch (hb_gpu_atlas, gi.glyphLoc);\n" -" int4 header1 = hb_gpu_fetch (hb_gpu_atlas, gi.glyphLoc + 1);\n" -" float4 ext = float4 (header0) * HB_GPU_INV_UNITS;\n" -" gi.numHBands = header1.r;\n" -" gi.numVBands = header1.g;\n" -" gi.scale = float2 (float (header1.b), float (header1.a));\n" -"\n" -" float2 extSize = ext.zw - ext.xy;\n" -" float2 bandScale = float2 (float (gi.numVBands), float (gi.numHBands)) / max (extSize, float2 (1.0 / 65536.0));\n" -" float2 bandOffset = -ext.xy * bandScale;\n" -"\n" -" gi.bandIndex = clamp (int2 (renderCoord * bandScale + bandOffset),\n" -" int2 (0, 0),\n" -" int2 (gi.numVBands - 1, gi.numHBands - 1));\n" -"\n" -" gi.bandBase = gi.glyphLoc + 2;\n" -" return gi;\n" -"}\n" -"\n" -"/* Return pixels per em at this fragment.\n" -" *\n" -" * renderCoord: em-space sample position\n" -" * glyphLoc: texel offset of glyph blob in atlas\n" -" */\n" -"float hb_gpu_ppem (float2 renderCoord, uint glyphLoc_,\n" -" device const short4* hb_gpu_atlas)\n" -"{\n" -" _hb_gpu_glyph_info gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_, hb_gpu_atlas);\n" -" float2 emsPerPixel = fwidth (renderCoord);\n" -" return min (gi.scale.x, gi.scale.y) /\n" -" max (emsPerPixel.x, emsPerPixel.y);\n" -"}\n" -"\n" -"/* Return per-pixel curve counts: (horizontal, vertical). */\n" -"int2 _hb_gpu_curve_counts (float2 renderCoord, uint glyphLoc_,\n" -" device const short4* hb_gpu_atlas)\n" -"{\n" -" _hb_gpu_glyph_info gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_, hb_gpu_atlas);\n" -" int hCount = hb_gpu_fetch (hb_gpu_atlas, gi.bandBase + gi.bandIndex.y).r;\n" -" int vCount = hb_gpu_fetch (hb_gpu_atlas, gi.bandBase + gi.numHBands + gi.bandIndex.x).r;\n" -" return int2 (hCount, vCount);\n" -"}\n" -"\n" -"/* Single-sample coverage in [0, 1]. */\n" -"float _hb_gpu_draw_single (float2 renderCoord, float2 pixelsPerEm, uint glyphLoc_,\n" -" device const short4* hb_gpu_atlas)\n" -"{\n" -"\n" -" _hb_gpu_glyph_info gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_, hb_gpu_atlas);\n" -" int glyphLoc = gi.glyphLoc;\n" -" int bandBase = gi.bandBase;\n" -" int numHBands = gi.numHBands;\n" -"\n" -" float xcov = 0.0;\n" -" float xwgt = 0.0;\n" -"\n" -" int4 hbandData = hb_gpu_fetch (hb_gpu_atlas, bandBase + gi.bandIndex.y);\n" -" int hCurveCount = hbandData.r;\n" -" /* Symmetric: choose rightward (desc) or leftward (asc) sort */\n" -" float hSplit = float (hbandData.a) * HB_GPU_INV_UNITS;\n" -" bool hLeftRay = (renderCoord.x < hSplit);\n" -" int hDataOffset = (hLeftRay ? hbandData.b : hbandData.g) + 32768;\n" -"\n" -" for (int ci = 0; ci < hCurveCount; ci++)\n" -" {\n" -" int curveOffset = hb_gpu_fetch (hb_gpu_atlas, glyphLoc + hDataOffset + ci).r + 32768;\n" -"\n" -" int4 raw12 = hb_gpu_fetch (hb_gpu_atlas, glyphLoc + curveOffset);\n" -" int4 raw3 = hb_gpu_fetch (hb_gpu_atlas, glyphLoc + curveOffset + 1);\n" -"\n" -" float4 q12 = float4 (raw12) * HB_GPU_INV_UNITS;\n" -" float2 q3 = float2 (raw3.rg) * HB_GPU_INV_UNITS;\n" -"\n" -" float4 p12 = q12 - float4 (renderCoord, renderCoord);\n" -" float2 p3 = q3 - renderCoord;\n" -"\n" -" if (hLeftRay) {\n" -" if (min (min (p12.x, p12.z), p3.x) * pixelsPerEm.x > 0.5) break;\n" -" } else {\n" -" if (max (max (p12.x, p12.z), p3.x) * pixelsPerEm.x < -0.5) break;\n" -" }\n" -"\n" -" uint code = _hb_gpu_calc_root_code (p12.y, p12.w, p3.y);\n" -" if (code != 0U)\n" -" {\n" -" float2 a = q12.xy - q12.zw * 2.0 + q3;\n" -" float2 b = q12.xy - q12.zw;\n" -" float2 r = _hb_gpu_solve_horiz_poly (a, b, p12.xy) * pixelsPerEm.x;\n" -" /* For leftward ray: saturate(0.5 - r) counts coverage from the left */\n" -" float2 cov = hLeftRay ? clamp (float2 (0.5) - r, 0.0, 1.0)\n" -" : clamp (r + float2 (0.5), 0.0, 1.0);\n" -"\n" -" if ((code & 1U) != 0U)\n" -" {\n" -" xcov += cov.x;\n" -" xwgt = max (xwgt, clamp (1.0 - abs (r.x) * 2.0, 0.0, 1.0));\n" -" }\n" -"\n" -" if (code > 1U)\n" -" {\n" -" xcov -= cov.y;\n" -" xwgt = max (xwgt, clamp (1.0 - abs (r.y) * 2.0, 0.0, 1.0));\n" -" }\n" -" }\n" -" }\n" -"\n" -" float ycov = 0.0;\n" -" float ywgt = 0.0;\n" -"\n" -" int4 vbandData = hb_gpu_fetch (hb_gpu_atlas, bandBase + numHBands + gi.bandIndex.x);\n" -" int vCurveCount = vbandData.r;\n" -" float vSplit = float (vbandData.a) * HB_GPU_INV_UNITS;\n" -" bool vLeftRay = (renderCoord.y < vSplit);\n" -" int vDataOffset = (vLeftRay ? vbandData.b : vbandData.g) + 32768;\n" -"\n" -" for (int ci = 0; ci < vCurveCount; ci++)\n" -" {\n" -" int curveOffset = hb_gpu_fetch (hb_gpu_atlas, glyphLoc + vDataOffset + ci).r + 32768;\n" -"\n" -" int4 raw12 = hb_gpu_fetch (hb_gpu_atlas, glyphLoc + curveOffset);\n" -" int4 raw3 = hb_gpu_fetch (hb_gpu_atlas, glyphLoc + curveOffset + 1);\n" -"\n" -" float4 q12 = float4 (raw12) * HB_GPU_INV_UNITS;\n" -" float2 q3 = float2 (raw3.rg) * HB_GPU_INV_UNITS;\n" -"\n" -" float4 p12 = q12 - float4 (renderCoord, renderCoord);\n" -" float2 p3 = q3 - renderCoord;\n" -"\n" -" if (vLeftRay) {\n" -" if (min (min (p12.y, p12.w), p3.y) * pixelsPerEm.y > 0.5) break;\n" -" } else {\n" -" if (max (max (p12.y, p12.w), p3.y) * pixelsPerEm.y < -0.5) break;\n" -" }\n" -"\n" -" uint code = _hb_gpu_calc_root_code (p12.x, p12.z, p3.x);\n" -" if (code != 0U)\n" -" {\n" -" float2 a = q12.xy - q12.zw * 2.0 + q3;\n" -" float2 b = q12.xy - q12.zw;\n" -" float2 r = _hb_gpu_solve_vert_poly (a, b, p12.xy) * pixelsPerEm.y;\n" -" float2 cov = vLeftRay ? clamp (float2 (0.5) - r, 0.0, 1.0)\n" -" : clamp (r + float2 (0.5), 0.0, 1.0);\n" -"\n" -" if ((code & 1U) != 0U)\n" -" {\n" -" ycov -= cov.x;\n" -" ywgt = max (ywgt, clamp (1.0 - abs (r.x) * 2.0, 0.0, 1.0));\n" -" }\n" -"\n" -" if (code > 1U)\n" -" {\n" -" ycov += cov.y;\n" -" ywgt = max (ywgt, clamp (1.0 - abs (r.y) * 2.0, 0.0, 1.0));\n" -" }\n" -" }\n" -" }\n" -"\n" -" return _hb_gpu_calc_coverage (xcov, ycov, xwgt, ywgt);\n" -"}\n" -"\n" -"/* Return coverage in [0, 1].\n" -" *\n" -" * renderCoord: em-space sample position\n" -" * glyphLoc: texel offset of glyph blob in atlas\n" -" * hb_gpu_atlas: device pointer to the atlas buffer\n" -" */\n" -"/* The MSAA-aware implementation. Caller supplies pixelsPerEm so\n" -" * this function can be invoked from non-uniform control flow (for\n" -" * example from a paint op-stream branch where a recomputed fwidth\n" -" * would be rejected by strict derivative-uniformity rules). */\n" -"float _hb_gpu_draw_impl (float2 renderCoord, float2 pixelsPerEm, uint glyphLoc_,\n" -" device const short4* hb_gpu_atlas)\n" -"{\n" -" float c = _hb_gpu_draw_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" -"\n" -" if (ppem < 16.0)\n" -" {\n" -" float2 emsPerPixel = 1.0 / pixelsPerEm;\n" -" float2 d = emsPerPixel * (1.0 / 3.0);\n" -" float msaa = 0.25 *\n" -" (_hb_gpu_draw_single (renderCoord + float2 (-d.x, -d.y), pixelsPerEm, glyphLoc_, hb_gpu_atlas) +\n" -" _hb_gpu_draw_single (renderCoord + float2 ( d.x, -d.y), pixelsPerEm, glyphLoc_, hb_gpu_atlas) +\n" -" _hb_gpu_draw_single (renderCoord + float2 (-d.x, d.y), pixelsPerEm, glyphLoc_, hb_gpu_atlas) +\n" -" _hb_gpu_draw_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" -"#endif\n" -"\n" -" return c;\n" -"}\n" -"\n" "float hb_gpu_draw (float2 renderCoord, uint glyphLoc_,\n" " device const short4* hb_gpu_atlas)\n" "{\n" " float2 pixelsPerEm = 1.0 / fwidth (renderCoord);\n" -" return _hb_gpu_draw_impl (renderCoord, pixelsPerEm, glyphLoc_, hb_gpu_atlas);\n" -"}\n" -"\n" -"/* Stem darkening for small sizes.\n" -" *\n" -" * coverage: output of hb_gpu_draw\n" -" * brightness: foreground brightness in [0, 1]\n" -" * ppem: pixels per em at this fragment\n" -" */\n" -"float hb_gpu_stem_darken (float coverage, float brightness, float ppem)\n" -"{\n" -" return pow (coverage,\n" -" mix (pow (2.0, brightness - 0.5), 1.0,\n" -" smoothstep (8.0, 48.0, ppem)));\n" +" return _hb_gpu_slug (renderCoord, pixelsPerEm, glyphLoc_, hb_gpu_atlas);\n" "}\n" ; diff --git a/src/hb-gpu-draw-fragment-wgsl.hh b/src/hb-gpu-draw-fragment-wgsl.hh index 685da2e76..0e732e263 100644 --- a/src/hb-gpu-draw-fragment-wgsl.hh +++ b/src/hb-gpu-draw-fragment-wgsl.hh @@ -1,10 +1,6 @@ static const char *hb_gpu_draw_fragment_wgsl = "/*\n" " * Copyright (C) 2026 Behdad Esfahbod\n" -" * Copyright (C) 2017 Eric Lengyel\n" -" *\n" -" * Based on the Slug algorithm by Eric Lengyel:\n" -" * https://github.com/EricLengyel/Slug\n" " *\n" " * This is part of HarfBuzz, a text shaping library.\n" " *\n" @@ -28,308 +24,15 @@ static const char *hb_gpu_draw_fragment_wgsl = " */\n" "\n" "\n" -"/* Draw-renderer fragment shader.\n" -" *\n" -" * Assumes the shared fragment helpers (hb-gpu-fragment.wgsl) are\n" -" * prepended to this source.\n" -" */\n" +"/* Draw-renderer fragment shader entry (WGSL). Heavy lifting\n" +" * (Slug coverage, MSAA, ppem, stem darkening) lives in the shared\n" +" * hb-gpu-fragment.wgsl that must be prepended to this source. */\n" "\n" "\n" -"fn _hb_gpu_calc_root_code (y1: f32, y2: f32, y3: f32) -> u32\n" -"{\n" -" let i1 = bitcast (y1) >> 31u;\n" -" let i2 = bitcast (y2) >> 30u;\n" -" let i3 = bitcast (y3) >> 29u;\n" -"\n" -" var shift = (i2 & 2u) | (i1 & ~2u);\n" -" shift = (i3 & 4u) | (shift & ~4u);\n" -"\n" -" return (0x2E74u >> shift) & 0x0101u;\n" -"}\n" -"\n" -"fn _hb_gpu_solve_horiz_poly (a: vec2f, b: vec2f, p1: vec2f) -> vec2f\n" -"{\n" -" let ra = 1.0 / a.y;\n" -" let rb = 0.5 / b.y;\n" -"\n" -" let d = sqrt (max (b.y * b.y - a.y * p1.y, 0.0));\n" -" var t1 = (b.y - d) * ra;\n" -" var t2 = (b.y + d) * ra;\n" -"\n" -" if (a.y == 0.0) {\n" -" t1 = p1.y * rb;\n" -" t2 = t1;\n" -" }\n" -"\n" -" return vec2f ((a.x * t1 - b.x * 2.0) * t1 + p1.x,\n" -" (a.x * t2 - b.x * 2.0) * t2 + p1.x);\n" -"}\n" -"\n" -"fn _hb_gpu_solve_vert_poly (a: vec2f, b: vec2f, p1: vec2f) -> vec2f\n" -"{\n" -" let ra = 1.0 / a.x;\n" -" let rb = 0.5 / b.x;\n" -"\n" -" let d = sqrt (max (b.x * b.x - a.x * p1.x, 0.0));\n" -" var t1 = (b.x - d) * ra;\n" -" var t2 = (b.x + d) * ra;\n" -"\n" -" if (a.x == 0.0) {\n" -" t1 = p1.x * rb;\n" -" t2 = t1;\n" -" }\n" -"\n" -" return vec2f ((a.y * t1 - b.y * 2.0) * t1 + p1.y,\n" -" (a.y * t2 - b.y * 2.0) * t2 + p1.y);\n" -"}\n" -"\n" -"fn _hb_gpu_calc_coverage (xcov: f32, ycov: f32, xwgt: f32, ywgt: f32) -> f32\n" -"{\n" -" let coverage = max (abs (xcov * xwgt + ycov * ywgt) /\n" -" max (xwgt + ywgt, 1.0 / 65536.0),\n" -" min (abs (xcov), abs (ycov)));\n" -"\n" -" return clamp (coverage, 0.0, 1.0);\n" -"}\n" -"\n" -"/* Decoded glyph band info for a pixel position. */\n" -"struct _hb_gpu_glyph_info\n" -"{\n" -" glyphLoc: i32,\n" -" bandBase: i32,\n" -" bandIndex: vec2,\n" -" numHBands: i32,\n" -" numVBands: i32,\n" -" scale: vec2f,\n" -"}\n" -"\n" -"fn _hb_gpu_decode_glyph (renderCoord: vec2f, glyphLoc_: u32,\n" -" hb_gpu_atlas: ptr>, read>) -> _hb_gpu_glyph_info\n" -"{\n" -" var gi: _hb_gpu_glyph_info;\n" -" gi.glyphLoc = i32 (glyphLoc_);\n" -"\n" -" let header0 = hb_gpu_fetch (hb_gpu_atlas, gi.glyphLoc);\n" -" let header1 = hb_gpu_fetch (hb_gpu_atlas, gi.glyphLoc + 1);\n" -" let ext = vec4f (header0) * HB_GPU_INV_UNITS;\n" -" gi.numHBands = header1.r;\n" -" gi.numVBands = header1.g;\n" -" gi.scale = vec2f (f32 (header1.b), f32 (header1.a));\n" -"\n" -" let extSize = ext.zw - ext.xy;\n" -" let bandScale = vec2f (f32 (gi.numVBands), f32 (gi.numHBands)) / max (extSize, vec2f (1.0 / 65536.0));\n" -" let bandOffset = -ext.xy * bandScale;\n" -"\n" -" gi.bandIndex = clamp (vec2 (renderCoord * bandScale + bandOffset),\n" -" vec2 (0, 0),\n" -" vec2 (gi.numVBands - 1, gi.numHBands - 1));\n" -"\n" -" gi.bandBase = gi.glyphLoc + 2;\n" -" return gi;\n" -"}\n" -"\n" -"/* Return pixels per em at this fragment.\n" -" *\n" -" * renderCoord: em-space sample position\n" -" * glyphLoc: texel offset of glyph blob in atlas\n" -" */\n" -"fn hb_gpu_ppem (renderCoord: vec2f, glyphLoc_: u32,\n" -" hb_gpu_atlas: ptr>, read>) -> f32\n" -"{\n" -" let gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_, hb_gpu_atlas);\n" -" let emsPerPixel = fwidth (renderCoord);\n" -" return min (gi.scale.x, gi.scale.y) /\n" -" max (emsPerPixel.x, emsPerPixel.y);\n" -"}\n" -"\n" -"/* Return per-pixel curve counts: (horizontal, vertical). */\n" -"fn _hb_gpu_curve_counts (renderCoord: vec2f, glyphLoc_: u32,\n" -" hb_gpu_atlas: ptr>, read>) -> vec2\n" -"{\n" -" let gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_, hb_gpu_atlas);\n" -" let hCount = hb_gpu_fetch (hb_gpu_atlas, gi.bandBase + gi.bandIndex.y).r;\n" -" let vCount = hb_gpu_fetch (hb_gpu_atlas, gi.bandBase + gi.numHBands + gi.bandIndex.x).r;\n" -" return vec2 (hCount, vCount);\n" -"}\n" -"\n" -"/* Single-sample coverage in [0, 1]. */\n" -"fn _hb_gpu_draw_single (renderCoord: vec2f, pixelsPerEm: vec2f, glyphLoc_: u32,\n" -" hb_gpu_atlas: ptr>, read>) -> f32\n" -"{\n" -"\n" -" let gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_, hb_gpu_atlas);\n" -" let glyphLoc = gi.glyphLoc;\n" -" let bandBase = gi.bandBase;\n" -" let numHBands = gi.numHBands;\n" -"\n" -" var xcov: f32 = 0.0;\n" -" var xwgt: f32 = 0.0;\n" -"\n" -" let hbandData = hb_gpu_fetch (hb_gpu_atlas, bandBase + gi.bandIndex.y);\n" -" let hCurveCount = hbandData.r;\n" -" /* Symmetric: choose rightward (desc) or leftward (asc) sort */\n" -" let hSplit = f32 (hbandData.a) * HB_GPU_INV_UNITS;\n" -" let hLeftRay = (renderCoord.x < hSplit);\n" -" var hDataOffset: i32;\n" -" if (hLeftRay) { hDataOffset = hbandData.b + 32768; }\n" -" else { hDataOffset = hbandData.g + 32768; }\n" -"\n" -" for (var ci: i32 = 0; ci < hCurveCount; ci++)\n" -" {\n" -" let curveOffset = hb_gpu_fetch (hb_gpu_atlas, glyphLoc + hDataOffset + ci).r + 32768;\n" -"\n" -" let raw12 = hb_gpu_fetch (hb_gpu_atlas, glyphLoc + curveOffset);\n" -" let raw3 = hb_gpu_fetch (hb_gpu_atlas, glyphLoc + curveOffset + 1);\n" -"\n" -" let q12 = vec4f (raw12) * HB_GPU_INV_UNITS;\n" -" let q3 = vec2f (vec2 (raw3.r, raw3.g)) * HB_GPU_INV_UNITS;\n" -"\n" -" let p12 = q12 - vec4f (renderCoord, renderCoord);\n" -" let p3 = q3 - renderCoord;\n" -"\n" -" if (hLeftRay) {\n" -" if (min (min (p12.x, p12.z), p3.x) * pixelsPerEm.x > 0.5) { break; }\n" -" } else {\n" -" if (max (max (p12.x, p12.z), p3.x) * pixelsPerEm.x < -0.5) { break; }\n" -" }\n" -"\n" -" let code = _hb_gpu_calc_root_code (p12.y, p12.w, p3.y);\n" -" if (code != 0u)\n" -" {\n" -" let a = q12.xy - q12.zw * 2.0 + q3;\n" -" let b = q12.xy - q12.zw;\n" -" let r = _hb_gpu_solve_horiz_poly (a, b, p12.xy) * pixelsPerEm.x;\n" -" /* For leftward ray: saturate(0.5 - r) counts coverage from the left */\n" -" var cov: vec2f;\n" -" if (hLeftRay) { cov = clamp (vec2f (0.5) - r, vec2f (0.0), vec2f (1.0)); }\n" -" else { cov = clamp (r + vec2f (0.5), vec2f (0.0), vec2f (1.0)); }\n" -"\n" -" if ((code & 1u) != 0u)\n" -" {\n" -" xcov += cov.x;\n" -" xwgt = max (xwgt, clamp (1.0 - abs (r.x) * 2.0, 0.0, 1.0));\n" -" }\n" -"\n" -" if (code > 1u)\n" -" {\n" -" xcov -= cov.y;\n" -" xwgt = max (xwgt, clamp (1.0 - abs (r.y) * 2.0, 0.0, 1.0));\n" -" }\n" -" }\n" -" }\n" -"\n" -" var ycov: f32 = 0.0;\n" -" var ywgt: f32 = 0.0;\n" -"\n" -" let vbandData = hb_gpu_fetch (hb_gpu_atlas, bandBase + numHBands + gi.bandIndex.x);\n" -" let vCurveCount = vbandData.r;\n" -" let vSplit = f32 (vbandData.a) * HB_GPU_INV_UNITS;\n" -" let vLeftRay = (renderCoord.y < vSplit);\n" -" var vDataOffset: i32;\n" -" if (vLeftRay) { vDataOffset = vbandData.b + 32768; }\n" -" else { vDataOffset = vbandData.g + 32768; }\n" -"\n" -" for (var ci: i32 = 0; ci < vCurveCount; ci++)\n" -" {\n" -" let curveOffset = hb_gpu_fetch (hb_gpu_atlas, glyphLoc + vDataOffset + ci).r + 32768;\n" -"\n" -" let raw12 = hb_gpu_fetch (hb_gpu_atlas, glyphLoc + curveOffset);\n" -" let raw3 = hb_gpu_fetch (hb_gpu_atlas, glyphLoc + curveOffset + 1);\n" -"\n" -" let q12 = vec4f (raw12) * HB_GPU_INV_UNITS;\n" -" let q3 = vec2f (vec2 (raw3.r, raw3.g)) * HB_GPU_INV_UNITS;\n" -"\n" -" let p12 = q12 - vec4f (renderCoord, renderCoord);\n" -" let p3 = q3 - renderCoord;\n" -"\n" -" if (vLeftRay) {\n" -" if (min (min (p12.y, p12.w), p3.y) * pixelsPerEm.y > 0.5) { break; }\n" -" } else {\n" -" if (max (max (p12.y, p12.w), p3.y) * pixelsPerEm.y < -0.5) { break; }\n" -" }\n" -"\n" -" let code = _hb_gpu_calc_root_code (p12.x, p12.z, p3.x);\n" -" if (code != 0u)\n" -" {\n" -" let a = q12.xy - q12.zw * 2.0 + q3;\n" -" let b = q12.xy - q12.zw;\n" -" let r = _hb_gpu_solve_vert_poly (a, b, p12.xy) * pixelsPerEm.y;\n" -" var cov: vec2f;\n" -" if (vLeftRay) { cov = clamp (vec2f (0.5) - r, vec2f (0.0), vec2f (1.0)); }\n" -" else { cov = clamp (r + vec2f (0.5), vec2f (0.0), vec2f (1.0)); }\n" -"\n" -" if ((code & 1u) != 0u)\n" -" {\n" -" ycov -= cov.x;\n" -" ywgt = max (ywgt, clamp (1.0 - abs (r.x) * 2.0, 0.0, 1.0));\n" -" }\n" -"\n" -" if (code > 1u)\n" -" {\n" -" ycov += cov.y;\n" -" ywgt = max (ywgt, clamp (1.0 - abs (r.y) * 2.0, 0.0, 1.0));\n" -" }\n" -" }\n" -" }\n" -"\n" -" return _hb_gpu_calc_coverage (xcov, ycov, xwgt, ywgt);\n" -"}\n" -"\n" -"/* Return coverage in [0, 1].\n" -" *\n" -" * renderCoord: em-space sample position\n" -" * glyphLoc: texel offset of glyph blob in atlas\n" -" * hb_gpu_atlas: storage buffer pointer to the atlas\n" -" */\n" -"/* The MSAA-aware implementation. Caller supplies pixelsPerEm so\n" -" * this function can be invoked from non-uniform control flow (for\n" -" * example inside an op-stream branch in hb-gpu-paint-fragment.wgsl,\n" -" * where WGSL would otherwise reject an fwidth call). */\n" -"fn _hb_gpu_draw_impl (renderCoord: vec2f, pixelsPerEm: vec2f, glyphLoc_: u32,\n" -" hb_gpu_atlas: ptr>, read>) -> f32\n" -"{\n" -" var c = _hb_gpu_draw_single (renderCoord, pixelsPerEm, glyphLoc_, hb_gpu_atlas);\n" -"\n" -" /* Inline ppem from pixelsPerEm so we don't re-enter hb_gpu_ppem\n" -" * (which would call fwidth again -- rejected by WGSL when this\n" -" * function is invoked from non-uniform control flow). */\n" -" let gi_pp = _hb_gpu_decode_glyph (renderCoord, glyphLoc_, hb_gpu_atlas);\n" -" let ppem = min (gi_pp.scale.x, gi_pp.scale.y) *\n" -" min (pixelsPerEm.x, pixelsPerEm.y);\n" -"\n" -" if (ppem < 16.0)\n" -" {\n" -" let emsPerPixel = 1.0 / pixelsPerEm;\n" -" let d = emsPerPixel * (1.0 / 3.0);\n" -" let msaa = 0.25 *\n" -" (_hb_gpu_draw_single (renderCoord + vec2f (-d.x, -d.y), pixelsPerEm, glyphLoc_, hb_gpu_atlas) +\n" -" _hb_gpu_draw_single (renderCoord + vec2f ( d.x, -d.y), pixelsPerEm, glyphLoc_, hb_gpu_atlas) +\n" -" _hb_gpu_draw_single (renderCoord + vec2f (-d.x, d.y), pixelsPerEm, glyphLoc_, hb_gpu_atlas) +\n" -" _hb_gpu_draw_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" -"\n" -" return c;\n" -"}\n" -"\n" "fn hb_gpu_draw (renderCoord: vec2f, glyphLoc_: u32,\n" " hb_gpu_atlas: ptr>, read>) -> f32\n" "{\n" " let pixelsPerEm = 1.0 / fwidth (renderCoord);\n" -" return _hb_gpu_draw_impl (renderCoord, pixelsPerEm, glyphLoc_, hb_gpu_atlas);\n" -"}\n" -"\n" -"/* Stem darkening for small sizes.\n" -" *\n" -" * coverage: output of hb_gpu_draw\n" -" * brightness: foreground brightness in [0, 1]\n" -" * ppem: pixels per em at this fragment\n" -" */\n" -"fn hb_gpu_stem_darken (coverage: f32, brightness: f32, ppem: f32) -> f32\n" -"{\n" -" return pow (coverage,\n" -" mix (pow (2.0, brightness - 0.5), 1.0,\n" -" smoothstep (8.0, 48.0, ppem)));\n" +" return _hb_gpu_slug (renderCoord, pixelsPerEm, glyphLoc_, hb_gpu_atlas);\n" "}\n" ; diff --git a/src/hb-gpu-draw-fragment.glsl b/src/hb-gpu-draw-fragment.glsl index dc567dd46..75f3c5ec7 100644 --- a/src/hb-gpu-draw-fragment.glsl +++ b/src/hb-gpu-draw-fragment.glsl @@ -1,9 +1,5 @@ /* * Copyright (C) 2026 Behdad Esfahbod - * Copyright (C) 2017 Eric Lengyel - * - * Based on the Slug algorithm by Eric Lengyel: - * https://github.com/EricLengyel/Slug * * This is part of HarfBuzz, a text shaping library. * @@ -27,287 +23,16 @@ */ -/* Draw-renderer fragment shader. - * - * Assumes the shared fragment helpers (hb-gpu-fragment.glsl) are - * prepended to this source. - */ +/* Draw-renderer fragment shader entry. The heavy lifting (Slug + * coverage, MSAA, ppem, stem darkening) lives in the shared + * hb-gpu-fragment.glsl that must be prepended to this source; + * this file only adds the thin hb_gpu_draw() wrapper that lifts + * pixelsPerEm out of fwidth() at uniform control flow before + * calling the shared _hb_gpu_slug(). */ -uint _hb_gpu_calc_root_code (float y1, float y2, float y3) -{ - uint i1 = floatBitsToUint (y1) >> 31U; - uint i2 = floatBitsToUint (y2) >> 30U; - uint i3 = floatBitsToUint (y3) >> 29U; - - uint shift = (i2 & 2U) | (i1 & ~2U); - shift = (i3 & 4U) | (shift & ~4U); - - return (0x2E74U >> shift) & 0x0101U; -} - -vec2 _hb_gpu_solve_horiz_poly (vec2 a, vec2 b, vec2 p1) -{ - float ra = 1.0 / a.y; - float rb = 0.5 / b.y; - - float d = sqrt (max (b.y * b.y - a.y * p1.y, 0.0)); - float t1 = (b.y - d) * ra; - float t2 = (b.y + d) * ra; - - if (a.y == 0.0) - t1 = t2 = p1.y * rb; - - return vec2 ((a.x * t1 - b.x * 2.0) * t1 + p1.x, - (a.x * t2 - b.x * 2.0) * t2 + p1.x); -} - -vec2 _hb_gpu_solve_vert_poly (vec2 a, vec2 b, vec2 p1) -{ - float ra = 1.0 / a.x; - float rb = 0.5 / b.x; - - float d = sqrt (max (b.x * b.x - a.x * p1.x, 0.0)); - float t1 = (b.x - d) * ra; - float t2 = (b.x + d) * ra; - - if (a.x == 0.0) - t1 = t2 = p1.x * rb; - - return vec2 ((a.y * t1 - b.y * 2.0) * t1 + p1.y, - (a.y * t2 - b.y * 2.0) * t2 + p1.y); -} - -float _hb_gpu_calc_coverage (float xcov, float ycov, float xwgt, float ywgt) -{ - float coverage = max (abs (xcov * xwgt + ycov * ywgt) / - max (xwgt + ywgt, 1.0 / 65536.0), - min (abs (xcov), abs (ycov))); - - return clamp (coverage, 0.0, 1.0); -} - -/* Decoded glyph band info for a pixel position. */ -struct _hb_gpu_glyph_info -{ - int glyphLoc; - int bandBase; - ivec2 bandIndex; - int numHBands; - int numVBands; - vec2 scale; -}; - -_hb_gpu_glyph_info _hb_gpu_decode_glyph (vec2 renderCoord, uint glyphLoc_) -{ - _hb_gpu_glyph_info gi; - gi.glyphLoc = int (glyphLoc_); - - ivec4 header0 = hb_gpu_fetch (gi.glyphLoc); - ivec4 header1 = hb_gpu_fetch (gi.glyphLoc + 1); - vec4 ext = vec4 (header0) * HB_GPU_INV_UNITS; - gi.numHBands = header1.r; - gi.numVBands = header1.g; - gi.scale = vec2 (float (header1.b), float (header1.a)); - - vec2 extSize = ext.zw - ext.xy; - vec2 bandScale = vec2 (float (gi.numVBands), float (gi.numHBands)) / max (extSize, vec2 (1.0 / 65536.0)); - vec2 bandOffset = -ext.xy * bandScale; - - gi.bandIndex = clamp (ivec2 (renderCoord * bandScale + bandOffset), - ivec2 (0, 0), - ivec2 (gi.numVBands - 1, gi.numHBands - 1)); - - gi.bandBase = gi.glyphLoc + 2; - return gi; -} - -/* Return pixels per em at this fragment. - * - * renderCoord: em-space sample position - * glyphLoc: texel offset of glyph blob in atlas - */ -float hb_gpu_ppem (vec2 renderCoord, uint glyphLoc_) -{ - _hb_gpu_glyph_info gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_); - vec2 emsPerPixel = fwidth (renderCoord); - return min (gi.scale.x, gi.scale.y) / - max (emsPerPixel.x, emsPerPixel.y); -} - -/* Return per-pixel curve counts: (horizontal, vertical). */ -ivec2 _hb_gpu_curve_counts (vec2 renderCoord, uint glyphLoc_) -{ - _hb_gpu_glyph_info gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_); - int hCount = hb_gpu_fetch (gi.bandBase + gi.bandIndex.y).r; - int vCount = hb_gpu_fetch (gi.bandBase + gi.numHBands + gi.bandIndex.x).r; - return ivec2 (hCount, vCount); -} - -/* Single-sample coverage in [0, 1]. */ -float _hb_gpu_draw_single (vec2 renderCoord, vec2 pixelsPerEm, uint glyphLoc_) -{ - - _hb_gpu_glyph_info gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_); - int glyphLoc = gi.glyphLoc; - int bandBase = gi.bandBase; - int numHBands = gi.numHBands; - - float xcov = 0.0; - float xwgt = 0.0; - - ivec4 hbandData = hb_gpu_fetch (bandBase + gi.bandIndex.y); - int hCurveCount = hbandData.r; - /* Symmetric: choose rightward (desc) or leftward (asc) sort */ - float hSplit = float (hbandData.a) * HB_GPU_INV_UNITS; - bool hLeftRay = (renderCoord.x < hSplit); - int hDataOffset = (hLeftRay ? hbandData.b : hbandData.g) + 32768; - - for (int ci = 0; ci < hCurveCount; ci++) - { - int curveOffset = hb_gpu_fetch (glyphLoc + hDataOffset + ci).r + 32768; - - ivec4 raw12 = hb_gpu_fetch (glyphLoc + curveOffset); - ivec4 raw3 = hb_gpu_fetch (glyphLoc + curveOffset + 1); - - vec4 q12 = vec4 (raw12) * HB_GPU_INV_UNITS; - vec2 q3 = vec2 (raw3.rg) * HB_GPU_INV_UNITS; - - vec4 p12 = q12 - vec4 (renderCoord, renderCoord); - vec2 p3 = q3 - renderCoord; - - if (hLeftRay) { - if (min (min (p12.x, p12.z), p3.x) * pixelsPerEm.x > 0.5) break; - } else { - if (max (max (p12.x, p12.z), p3.x) * pixelsPerEm.x < -0.5) break; - } - - uint code = _hb_gpu_calc_root_code (p12.y, p12.w, p3.y); - if (code != 0U) - { - vec2 a = q12.xy - q12.zw * 2.0 + q3; - vec2 b = q12.xy - q12.zw; - vec2 r = _hb_gpu_solve_horiz_poly (a, b, p12.xy) * pixelsPerEm.x; - /* For leftward ray: saturate(0.5 - r) counts coverage from the left */ - vec2 cov = hLeftRay ? clamp (vec2 (0.5) - r, 0.0, 1.0) - : clamp (r + vec2 (0.5), 0.0, 1.0); - - if ((code & 1U) != 0U) - { - xcov += cov.x; - xwgt = max (xwgt, clamp (1.0 - abs (r.x) * 2.0, 0.0, 1.0)); - } - - if (code > 1U) - { - xcov -= cov.y; - xwgt = max (xwgt, clamp (1.0 - abs (r.y) * 2.0, 0.0, 1.0)); - } - } - } - - float ycov = 0.0; - float ywgt = 0.0; - - ivec4 vbandData = hb_gpu_fetch (bandBase + numHBands + gi.bandIndex.x); - int vCurveCount = vbandData.r; - float vSplit = float (vbandData.a) * HB_GPU_INV_UNITS; - bool vLeftRay = (renderCoord.y < vSplit); - int vDataOffset = (vLeftRay ? vbandData.b : vbandData.g) + 32768; - - for (int ci = 0; ci < vCurveCount; ci++) - { - int curveOffset = hb_gpu_fetch (glyphLoc + vDataOffset + ci).r + 32768; - - ivec4 raw12 = hb_gpu_fetch (glyphLoc + curveOffset); - ivec4 raw3 = hb_gpu_fetch (glyphLoc + curveOffset + 1); - - vec4 q12 = vec4 (raw12) * HB_GPU_INV_UNITS; - vec2 q3 = vec2 (raw3.rg) * HB_GPU_INV_UNITS; - - vec4 p12 = q12 - vec4 (renderCoord, renderCoord); - vec2 p3 = q3 - renderCoord; - - if (vLeftRay) { - if (min (min (p12.y, p12.w), p3.y) * pixelsPerEm.y > 0.5) break; - } else { - if (max (max (p12.y, p12.w), p3.y) * pixelsPerEm.y < -0.5) break; - } - - uint code = _hb_gpu_calc_root_code (p12.x, p12.z, p3.x); - if (code != 0U) - { - vec2 a = q12.xy - q12.zw * 2.0 + q3; - vec2 b = q12.xy - q12.zw; - vec2 r = _hb_gpu_solve_vert_poly (a, b, p12.xy) * pixelsPerEm.y; - vec2 cov = vLeftRay ? clamp (vec2 (0.5) - r, 0.0, 1.0) - : clamp (r + vec2 (0.5), 0.0, 1.0); - - if ((code & 1U) != 0U) - { - ycov -= cov.x; - ywgt = max (ywgt, clamp (1.0 - abs (r.x) * 2.0, 0.0, 1.0)); - } - - if (code > 1U) - { - ycov += cov.y; - ywgt = max (ywgt, clamp (1.0 - abs (r.y) * 2.0, 0.0, 1.0)); - } - } - } - - return _hb_gpu_calc_coverage (xcov, ycov, xwgt, ywgt); -} - -/* Return coverage in [0, 1]. - * - * renderCoord: em-space sample position - * glyphLoc: texel offset of glyph blob in atlas - */ -/* The MSAA-aware implementation. Caller supplies pixelsPerEm so - * this function can be invoked from non-uniform control flow (for - * example inside an op-stream branch in hb-gpu-paint-fragment.wgsl, - * where WGSL would otherwise reject an fwidth call). */ -float _hb_gpu_draw_impl (vec2 renderCoord, vec2 pixelsPerEm, uint glyphLoc_) -{ - float c = _hb_gpu_draw_single (renderCoord, pixelsPerEm, glyphLoc_); - -#ifndef HB_GPU_NO_MSAA - float ppem = hb_gpu_ppem (renderCoord, glyphLoc_); - - if (ppem < 16.0) - { - vec2 emsPerPixel = 1.0 / pixelsPerEm; - vec2 d = emsPerPixel * (1.0 / 3.0); - float msaa = 0.25 * - (_hb_gpu_draw_single (renderCoord + vec2 (-d.x, -d.y), pixelsPerEm, glyphLoc_) + - _hb_gpu_draw_single (renderCoord + vec2 ( d.x, -d.y), pixelsPerEm, glyphLoc_) + - _hb_gpu_draw_single (renderCoord + vec2 (-d.x, d.y), pixelsPerEm, glyphLoc_) + - _hb_gpu_draw_single (renderCoord + vec2 ( d.x, d.y), pixelsPerEm, glyphLoc_)); - - c = mix (c, msaa, smoothstep (16.0, 8.0, ppem)); - } -#endif - - return c; -} - float hb_gpu_draw (vec2 renderCoord, uint glyphLoc_) { vec2 pixelsPerEm = 1.0 / fwidth (renderCoord); - return _hb_gpu_draw_impl (renderCoord, pixelsPerEm, glyphLoc_); -} - -/* Stem darkening for small sizes. - * - * coverage: output of hb_gpu_draw - * brightness: foreground brightness in [0, 1] - * ppem: pixels per em at this fragment - */ -float hb_gpu_stem_darken (float coverage, float brightness, float ppem) -{ - return pow (coverage, - mix (pow (2.0, brightness - 0.5), 1.0, - smoothstep (8.0, 48.0, ppem))); + return _hb_gpu_slug (renderCoord, pixelsPerEm, glyphLoc_); } diff --git a/src/hb-gpu-draw-fragment.hlsl b/src/hb-gpu-draw-fragment.hlsl index 27563d580..49e94409f 100644 --- a/src/hb-gpu-draw-fragment.hlsl +++ b/src/hb-gpu-draw-fragment.hlsl @@ -1,9 +1,5 @@ /* * Copyright (C) 2026 Behdad Esfahbod - * Copyright (C) 2017 Eric Lengyel - * - * Based on the Slug algorithm by Eric Lengyel: - * https://github.com/EricLengyel/Slug * * This is part of HarfBuzz, a text shaping library. * @@ -27,286 +23,13 @@ */ -/* Draw-renderer fragment shader. - * - * Assumes the shared fragment helpers (hb-gpu-fragment.hlsl) are - * prepended to this source. - */ +/* Draw-renderer fragment shader entry (HLSL). Heavy lifting + * (Slug coverage, MSAA, ppem, stem darkening) lives in the shared + * hb-gpu-fragment.hlsl that must be prepended to this source. */ -uint _hb_gpu_calc_root_code (float y1, float y2, float y3) -{ - uint i1 = asuint (y1) >> 31u; - uint i2 = asuint (y2) >> 30u; - uint i3 = asuint (y3) >> 29u; - - uint shift = (i2 & 2u) | (i1 & ~2u); - shift = (i3 & 4u) | (shift & ~4u); - - return (0x2E74u >> shift) & 0x0101u; -} - -float2 _hb_gpu_solve_horiz_poly (float2 a, float2 b, float2 p1) -{ - float ra = 1.0 / a.y; - float rb = 0.5 / b.y; - - float d = sqrt (max (b.y * b.y - a.y * p1.y, 0.0)); - float t1 = (b.y - d) * ra; - float t2 = (b.y + d) * ra; - - if (a.y == 0.0) - t1 = t2 = p1.y * rb; - - return float2 ((a.x * t1 - b.x * 2.0) * t1 + p1.x, - (a.x * t2 - b.x * 2.0) * t2 + p1.x); -} - -float2 _hb_gpu_solve_vert_poly (float2 a, float2 b, float2 p1) -{ - float ra = 1.0 / a.x; - float rb = 0.5 / b.x; - - float d = sqrt (max (b.x * b.x - a.x * p1.x, 0.0)); - float t1 = (b.x - d) * ra; - float t2 = (b.x + d) * ra; - - if (a.x == 0.0) - t1 = t2 = p1.x * rb; - - return float2 ((a.y * t1 - b.y * 2.0) * t1 + p1.y, - (a.y * t2 - b.y * 2.0) * t2 + p1.y); -} - -float _hb_gpu_calc_coverage (float xcov, float ycov, float xwgt, float ywgt) -{ - float coverage = max (abs (xcov * xwgt + ycov * ywgt) / - max (xwgt + ywgt, 1.0 / 65536.0), - min (abs (xcov), abs (ycov))); - - return clamp (coverage, 0.0, 1.0); -} - - -struct _hb_gpu_glyph_info -{ - int glyphLoc; - int bandBase; - int2 bandIndex; - int numHBands; - int numVBands; - float2 scale; -}; - -_hb_gpu_glyph_info _hb_gpu_decode_glyph (float2 renderCoord, uint glyphLoc_) -{ - _hb_gpu_glyph_info gi; - gi.glyphLoc = (int) glyphLoc_; - - int4 header0 = hb_gpu_fetch (gi.glyphLoc); - int4 header1 = hb_gpu_fetch (gi.glyphLoc + 1); - float4 ext = (float4) header0 * HB_GPU_INV_UNITS; - gi.numHBands = header1.r; - gi.numVBands = header1.g; - gi.scale = float2 ((float) header1.b, (float) header1.a); - - float2 extSize = ext.zw - ext.xy; - float2 bandScale = float2 ((float) gi.numVBands, (float) gi.numHBands) / max (extSize, float2 (1.0 / 65536.0, 1.0 / 65536.0)); - float2 bandOffset = -ext.xy * bandScale; - - gi.bandIndex = clamp ((int2) (renderCoord * bandScale + bandOffset), - int2 (0, 0), - int2 (gi.numVBands - 1, gi.numHBands - 1)); - - gi.bandBase = gi.glyphLoc + 2; - return gi; -} - -/* Return pixels per em at this fragment. - * - * renderCoord: em-space sample position - * glyphLoc: texel offset of glyph blob in atlas - */ -float hb_gpu_ppem (float2 renderCoord, uint glyphLoc_) -{ - _hb_gpu_glyph_info gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_); - float2 emsPerPixel = fwidth (renderCoord); - return min (gi.scale.x, gi.scale.y) / - max (emsPerPixel.x, emsPerPixel.y); -} - -int2 _hb_gpu_curve_counts (float2 renderCoord, uint glyphLoc_) -{ - _hb_gpu_glyph_info gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_); - int hCount = hb_gpu_fetch (gi.bandBase + gi.bandIndex.y).r; - int vCount = hb_gpu_fetch (gi.bandBase + gi.numHBands + gi.bandIndex.x).r; - return int2 (hCount, vCount); -} - - -/* Single-sample coverage in [0, 1]. */ -float _hb_gpu_draw_single (float2 renderCoord, float2 pixelsPerEm, uint glyphLoc_) -{ - - _hb_gpu_glyph_info gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_); - int glyphLoc = gi.glyphLoc; - int bandBase = gi.bandBase; - int numHBands = gi.numHBands; - - float xcov = 0.0; - float xwgt = 0.0; - - int4 hbandData = hb_gpu_fetch (bandBase + gi.bandIndex.y); - int hCurveCount = hbandData.r; - float hSplit = (float) hbandData.a * HB_GPU_INV_UNITS; - bool hLeftRay = (renderCoord.x < hSplit); - int hDataOffset = (hLeftRay ? hbandData.b : hbandData.g) + 32768; - - for (int ci = 0; ci < hCurveCount; ci++) - { - int curveOffset = hb_gpu_fetch (glyphLoc + hDataOffset + ci).r + 32768; - - int4 raw12 = hb_gpu_fetch (glyphLoc + curveOffset); - int4 raw3 = hb_gpu_fetch (glyphLoc + curveOffset + 1); - - float4 q12 = (float4) raw12 * HB_GPU_INV_UNITS; - float2 q3 = (float2) raw3.rg * HB_GPU_INV_UNITS; - - float4 p12 = q12 - float4 (renderCoord, renderCoord); - float2 p3 = q3 - renderCoord; - - if (hLeftRay) { - if (min (min (p12.x, p12.z), p3.x) * pixelsPerEm.x > 0.5) break; - } else { - if (max (max (p12.x, p12.z), p3.x) * pixelsPerEm.x < -0.5) break; - } - - uint code = _hb_gpu_calc_root_code (p12.y, p12.w, p3.y); - if (code != 0u) - { - float2 a = q12.xy - q12.zw * 2.0 + q3; - float2 b = q12.xy - q12.zw; - float2 r = _hb_gpu_solve_horiz_poly (a, b, p12.xy) * pixelsPerEm.x; - float2 cov = hLeftRay ? clamp (float2 (0.5, 0.5) - r, 0.0, 1.0) - : clamp (r + float2 (0.5, 0.5), 0.0, 1.0); - - if ((code & 1u) != 0u) - { - xcov += cov.x; - xwgt = max (xwgt, clamp (1.0 - abs (r.x) * 2.0, 0.0, 1.0)); - } - - if (code > 1u) - { - xcov -= cov.y; - xwgt = max (xwgt, clamp (1.0 - abs (r.y) * 2.0, 0.0, 1.0)); - } - } - } - - float ycov = 0.0; - float ywgt = 0.0; - - int4 vbandData = hb_gpu_fetch (bandBase + numHBands + gi.bandIndex.x); - int vCurveCount = vbandData.r; - float vSplit = (float) vbandData.a * HB_GPU_INV_UNITS; - bool vLeftRay = (renderCoord.y < vSplit); - int vDataOffset = (vLeftRay ? vbandData.b : vbandData.g) + 32768; - - for (int ci = 0; ci < vCurveCount; ci++) - { - int curveOffset = hb_gpu_fetch (glyphLoc + vDataOffset + ci).r + 32768; - - int4 raw12 = hb_gpu_fetch (glyphLoc + curveOffset); - int4 raw3 = hb_gpu_fetch (glyphLoc + curveOffset + 1); - - float4 q12 = (float4) raw12 * HB_GPU_INV_UNITS; - float2 q3 = (float2) raw3.rg * HB_GPU_INV_UNITS; - - float4 p12 = q12 - float4 (renderCoord, renderCoord); - float2 p3 = q3 - renderCoord; - - if (vLeftRay) { - if (min (min (p12.y, p12.w), p3.y) * pixelsPerEm.y > 0.5) break; - } else { - if (max (max (p12.y, p12.w), p3.y) * pixelsPerEm.y < -0.5) break; - } - - uint code = _hb_gpu_calc_root_code (p12.x, p12.z, p3.x); - if (code != 0u) - { - float2 a = q12.xy - q12.zw * 2.0 + q3; - float2 b = q12.xy - q12.zw; - float2 r = _hb_gpu_solve_vert_poly (a, b, p12.xy) * pixelsPerEm.y; - float2 cov = vLeftRay ? clamp (float2 (0.5, 0.5) - r, 0.0, 1.0) - : clamp (r + float2 (0.5, 0.5), 0.0, 1.0); - - if ((code & 1u) != 0u) - { - ycov -= cov.x; - ywgt = max (ywgt, clamp (1.0 - abs (r.x) * 2.0, 0.0, 1.0)); - } - - if (code > 1u) - { - ycov += cov.y; - ywgt = max (ywgt, clamp (1.0 - abs (r.y) * 2.0, 0.0, 1.0)); - } - } - } - - return _hb_gpu_calc_coverage (xcov, ycov, xwgt, ywgt); -} - -/* Return coverage in [0, 1]. - * - * Caller must declare: StructuredBuffer hb_gpu_atlas - * - * renderCoord: em-space sample position - * glyphLoc: texel offset of glyph blob in atlas - */ -/* The MSAA-aware implementation. Caller supplies pixelsPerEm so - * this function can be invoked from non-uniform control flow (for - * example from a paint op-stream branch). */ -float _hb_gpu_draw_impl (float2 renderCoord, float2 pixelsPerEm, uint glyphLoc_) -{ - float c = _hb_gpu_draw_single (renderCoord, pixelsPerEm, glyphLoc_); - -#ifndef HB_GPU_NO_MSAA - float ppem = hb_gpu_ppem (renderCoord, glyphLoc_); - - if (ppem < 16.0) - { - float2 emsPerPixel = 1.0 / pixelsPerEm; - float2 d = emsPerPixel * (1.0 / 3.0); - float msaa = 0.25 * - (_hb_gpu_draw_single (renderCoord + float2 (-d.x, -d.y), pixelsPerEm, glyphLoc_) + - _hb_gpu_draw_single (renderCoord + float2 ( d.x, -d.y), pixelsPerEm, glyphLoc_) + - _hb_gpu_draw_single (renderCoord + float2 (-d.x, d.y), pixelsPerEm, glyphLoc_) + - _hb_gpu_draw_single (renderCoord + float2 ( d.x, d.y), pixelsPerEm, glyphLoc_)); - - c = lerp (c, msaa, smoothstep (16.0, 8.0, ppem)); - } -#endif - - return c; -} - float hb_gpu_draw (float2 renderCoord, uint glyphLoc_) { float2 pixelsPerEm = 1.0 / fwidth (renderCoord); - return _hb_gpu_draw_impl (renderCoord, pixelsPerEm, glyphLoc_); -} - -/* Stem darkening for small sizes. - * - * coverage: output of hb_gpu_draw - * brightness: foreground brightness in [0, 1] - * ppem: pixels per em at this fragment - */ -float hb_gpu_stem_darken (float coverage, float brightness, float ppem) -{ - return pow (coverage, - lerp (pow (2.0, brightness - 0.5), 1.0, - smoothstep (8.0, 48.0, ppem))); + return _hb_gpu_slug (renderCoord, pixelsPerEm, glyphLoc_); } diff --git a/src/hb-gpu-draw-fragment.msl b/src/hb-gpu-draw-fragment.msl index 701ccffa9..d1c2a1923 100644 --- a/src/hb-gpu-draw-fragment.msl +++ b/src/hb-gpu-draw-fragment.msl @@ -1,9 +1,5 @@ /* * Copyright (C) 2026 Behdad Esfahbod - * Copyright (C) 2017 Eric Lengyel - * - * Based on the Slug algorithm by Eric Lengyel: - * https://github.com/EricLengyel/Slug * * This is part of HarfBuzz, a text shaping library. * @@ -27,295 +23,14 @@ */ -/* Draw-renderer fragment shader. - * - * Assumes the shared fragment helpers (hb-gpu-fragment.msl) are - * prepended to this source. - */ +/* Draw-renderer fragment shader entry (Metal). Heavy lifting + * (Slug coverage, MSAA, ppem, stem darkening) lives in the shared + * hb-gpu-fragment.msl that must be prepended to this source. */ - -uint _hb_gpu_calc_root_code (float y1, float y2, float y3) -{ - uint i1 = as_type (y1) >> 31U; - uint i2 = as_type (y2) >> 30U; - uint i3 = as_type (y3) >> 29U; - - uint shift = (i2 & 2U) | (i1 & ~2U); - shift = (i3 & 4U) | (shift & ~4U); - - return (0x2E74U >> shift) & 0x0101U; -} - -float2 _hb_gpu_solve_horiz_poly (float2 a, float2 b, float2 p1) -{ - float ra = 1.0 / a.y; - float rb = 0.5 / b.y; - - float d = sqrt (max (b.y * b.y - a.y * p1.y, 0.0)); - float t1 = (b.y - d) * ra; - float t2 = (b.y + d) * ra; - - if (a.y == 0.0) - t1 = t2 = p1.y * rb; - - return float2 ((a.x * t1 - b.x * 2.0) * t1 + p1.x, - (a.x * t2 - b.x * 2.0) * t2 + p1.x); -} - -float2 _hb_gpu_solve_vert_poly (float2 a, float2 b, float2 p1) -{ - float ra = 1.0 / a.x; - float rb = 0.5 / b.x; - - float d = sqrt (max (b.x * b.x - a.x * p1.x, 0.0)); - float t1 = (b.x - d) * ra; - float t2 = (b.x + d) * ra; - - if (a.x == 0.0) - t1 = t2 = p1.x * rb; - - return float2 ((a.y * t1 - b.y * 2.0) * t1 + p1.y, - (a.y * t2 - b.y * 2.0) * t2 + p1.y); -} - -float _hb_gpu_calc_coverage (float xcov, float ycov, float xwgt, float ywgt) -{ - float coverage = max (abs (xcov * xwgt + ycov * ywgt) / - max (xwgt + ywgt, 1.0 / 65536.0), - min (abs (xcov), abs (ycov))); - - return clamp (coverage, 0.0, 1.0); -} - -/* Decoded glyph band info for a pixel position. */ -struct _hb_gpu_glyph_info -{ - int glyphLoc; - int bandBase; - int2 bandIndex; - int numHBands; - int numVBands; - float2 scale; -}; - -_hb_gpu_glyph_info _hb_gpu_decode_glyph (float2 renderCoord, uint glyphLoc_, - device const short4* hb_gpu_atlas) -{ - _hb_gpu_glyph_info gi; - gi.glyphLoc = int (glyphLoc_); - - int4 header0 = hb_gpu_fetch (hb_gpu_atlas, gi.glyphLoc); - int4 header1 = hb_gpu_fetch (hb_gpu_atlas, gi.glyphLoc + 1); - float4 ext = float4 (header0) * HB_GPU_INV_UNITS; - gi.numHBands = header1.r; - gi.numVBands = header1.g; - gi.scale = float2 (float (header1.b), float (header1.a)); - - float2 extSize = ext.zw - ext.xy; - float2 bandScale = float2 (float (gi.numVBands), float (gi.numHBands)) / max (extSize, float2 (1.0 / 65536.0)); - float2 bandOffset = -ext.xy * bandScale; - - gi.bandIndex = clamp (int2 (renderCoord * bandScale + bandOffset), - int2 (0, 0), - int2 (gi.numVBands - 1, gi.numHBands - 1)); - - gi.bandBase = gi.glyphLoc + 2; - return gi; -} - -/* Return pixels per em at this fragment. - * - * renderCoord: em-space sample position - * glyphLoc: texel offset of glyph blob in atlas - */ -float hb_gpu_ppem (float2 renderCoord, uint glyphLoc_, - device const short4* hb_gpu_atlas) -{ - _hb_gpu_glyph_info gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_, hb_gpu_atlas); - float2 emsPerPixel = fwidth (renderCoord); - return min (gi.scale.x, gi.scale.y) / - max (emsPerPixel.x, emsPerPixel.y); -} - -/* Return per-pixel curve counts: (horizontal, vertical). */ -int2 _hb_gpu_curve_counts (float2 renderCoord, uint glyphLoc_, - device const short4* hb_gpu_atlas) -{ - _hb_gpu_glyph_info gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_, hb_gpu_atlas); - int hCount = hb_gpu_fetch (hb_gpu_atlas, gi.bandBase + gi.bandIndex.y).r; - int vCount = hb_gpu_fetch (hb_gpu_atlas, gi.bandBase + gi.numHBands + gi.bandIndex.x).r; - return int2 (hCount, vCount); -} - -/* Single-sample coverage in [0, 1]. */ -float _hb_gpu_draw_single (float2 renderCoord, float2 pixelsPerEm, uint glyphLoc_, - device const short4* hb_gpu_atlas) -{ - - _hb_gpu_glyph_info gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_, hb_gpu_atlas); - int glyphLoc = gi.glyphLoc; - int bandBase = gi.bandBase; - int numHBands = gi.numHBands; - - float xcov = 0.0; - float xwgt = 0.0; - - int4 hbandData = hb_gpu_fetch (hb_gpu_atlas, bandBase + gi.bandIndex.y); - int hCurveCount = hbandData.r; - /* Symmetric: choose rightward (desc) or leftward (asc) sort */ - float hSplit = float (hbandData.a) * HB_GPU_INV_UNITS; - bool hLeftRay = (renderCoord.x < hSplit); - int hDataOffset = (hLeftRay ? hbandData.b : hbandData.g) + 32768; - - for (int ci = 0; ci < hCurveCount; ci++) - { - int curveOffset = hb_gpu_fetch (hb_gpu_atlas, glyphLoc + hDataOffset + ci).r + 32768; - - int4 raw12 = hb_gpu_fetch (hb_gpu_atlas, glyphLoc + curveOffset); - int4 raw3 = hb_gpu_fetch (hb_gpu_atlas, glyphLoc + curveOffset + 1); - - float4 q12 = float4 (raw12) * HB_GPU_INV_UNITS; - float2 q3 = float2 (raw3.rg) * HB_GPU_INV_UNITS; - - float4 p12 = q12 - float4 (renderCoord, renderCoord); - float2 p3 = q3 - renderCoord; - - if (hLeftRay) { - if (min (min (p12.x, p12.z), p3.x) * pixelsPerEm.x > 0.5) break; - } else { - if (max (max (p12.x, p12.z), p3.x) * pixelsPerEm.x < -0.5) break; - } - - uint code = _hb_gpu_calc_root_code (p12.y, p12.w, p3.y); - if (code != 0U) - { - float2 a = q12.xy - q12.zw * 2.0 + q3; - float2 b = q12.xy - q12.zw; - float2 r = _hb_gpu_solve_horiz_poly (a, b, p12.xy) * pixelsPerEm.x; - /* For leftward ray: saturate(0.5 - r) counts coverage from the left */ - float2 cov = hLeftRay ? clamp (float2 (0.5) - r, 0.0, 1.0) - : clamp (r + float2 (0.5), 0.0, 1.0); - - if ((code & 1U) != 0U) - { - xcov += cov.x; - xwgt = max (xwgt, clamp (1.0 - abs (r.x) * 2.0, 0.0, 1.0)); - } - - if (code > 1U) - { - xcov -= cov.y; - xwgt = max (xwgt, clamp (1.0 - abs (r.y) * 2.0, 0.0, 1.0)); - } - } - } - - float ycov = 0.0; - float ywgt = 0.0; - - int4 vbandData = hb_gpu_fetch (hb_gpu_atlas, bandBase + numHBands + gi.bandIndex.x); - int vCurveCount = vbandData.r; - float vSplit = float (vbandData.a) * HB_GPU_INV_UNITS; - bool vLeftRay = (renderCoord.y < vSplit); - int vDataOffset = (vLeftRay ? vbandData.b : vbandData.g) + 32768; - - for (int ci = 0; ci < vCurveCount; ci++) - { - int curveOffset = hb_gpu_fetch (hb_gpu_atlas, glyphLoc + vDataOffset + ci).r + 32768; - - int4 raw12 = hb_gpu_fetch (hb_gpu_atlas, glyphLoc + curveOffset); - int4 raw3 = hb_gpu_fetch (hb_gpu_atlas, glyphLoc + curveOffset + 1); - - float4 q12 = float4 (raw12) * HB_GPU_INV_UNITS; - float2 q3 = float2 (raw3.rg) * HB_GPU_INV_UNITS; - - float4 p12 = q12 - float4 (renderCoord, renderCoord); - float2 p3 = q3 - renderCoord; - - if (vLeftRay) { - if (min (min (p12.y, p12.w), p3.y) * pixelsPerEm.y > 0.5) break; - } else { - if (max (max (p12.y, p12.w), p3.y) * pixelsPerEm.y < -0.5) break; - } - - uint code = _hb_gpu_calc_root_code (p12.x, p12.z, p3.x); - if (code != 0U) - { - float2 a = q12.xy - q12.zw * 2.0 + q3; - float2 b = q12.xy - q12.zw; - float2 r = _hb_gpu_solve_vert_poly (a, b, p12.xy) * pixelsPerEm.y; - float2 cov = vLeftRay ? clamp (float2 (0.5) - r, 0.0, 1.0) - : clamp (r + float2 (0.5), 0.0, 1.0); - - if ((code & 1U) != 0U) - { - ycov -= cov.x; - ywgt = max (ywgt, clamp (1.0 - abs (r.x) * 2.0, 0.0, 1.0)); - } - - if (code > 1U) - { - ycov += cov.y; - ywgt = max (ywgt, clamp (1.0 - abs (r.y) * 2.0, 0.0, 1.0)); - } - } - } - - return _hb_gpu_calc_coverage (xcov, ycov, xwgt, ywgt); -} - -/* Return coverage in [0, 1]. - * - * renderCoord: em-space sample position - * glyphLoc: texel offset of glyph blob in atlas - * hb_gpu_atlas: device pointer to the atlas buffer - */ -/* The MSAA-aware implementation. Caller supplies pixelsPerEm so - * this function can be invoked from non-uniform control flow (for - * example from a paint op-stream branch where a recomputed fwidth - * would be rejected by strict derivative-uniformity rules). */ -float _hb_gpu_draw_impl (float2 renderCoord, float2 pixelsPerEm, uint glyphLoc_, - device const short4* hb_gpu_atlas) -{ - float c = _hb_gpu_draw_single (renderCoord, pixelsPerEm, glyphLoc_, hb_gpu_atlas); - -#ifndef HB_GPU_NO_MSAA - float ppem = hb_gpu_ppem (renderCoord, glyphLoc_, hb_gpu_atlas); - - if (ppem < 16.0) - { - float2 emsPerPixel = 1.0 / pixelsPerEm; - float2 d = emsPerPixel * (1.0 / 3.0); - float msaa = 0.25 * - (_hb_gpu_draw_single (renderCoord + float2 (-d.x, -d.y), pixelsPerEm, glyphLoc_, hb_gpu_atlas) + - _hb_gpu_draw_single (renderCoord + float2 ( d.x, -d.y), pixelsPerEm, glyphLoc_, hb_gpu_atlas) + - _hb_gpu_draw_single (renderCoord + float2 (-d.x, d.y), pixelsPerEm, glyphLoc_, hb_gpu_atlas) + - _hb_gpu_draw_single (renderCoord + float2 ( d.x, d.y), pixelsPerEm, glyphLoc_, hb_gpu_atlas)); - - c = mix (c, msaa, smoothstep (16.0, 8.0, ppem)); - } -#endif - - return c; -} - float hb_gpu_draw (float2 renderCoord, uint glyphLoc_, device const short4* hb_gpu_atlas) { float2 pixelsPerEm = 1.0 / fwidth (renderCoord); - return _hb_gpu_draw_impl (renderCoord, pixelsPerEm, glyphLoc_, hb_gpu_atlas); -} - -/* Stem darkening for small sizes. - * - * coverage: output of hb_gpu_draw - * brightness: foreground brightness in [0, 1] - * ppem: pixels per em at this fragment - */ -float hb_gpu_stem_darken (float coverage, float brightness, float ppem) -{ - return pow (coverage, - mix (pow (2.0, brightness - 0.5), 1.0, - smoothstep (8.0, 48.0, ppem))); + return _hb_gpu_slug (renderCoord, pixelsPerEm, glyphLoc_, hb_gpu_atlas); } diff --git a/src/hb-gpu-draw-fragment.wgsl b/src/hb-gpu-draw-fragment.wgsl index c62567864..ca8e671c4 100644 --- a/src/hb-gpu-draw-fragment.wgsl +++ b/src/hb-gpu-draw-fragment.wgsl @@ -1,9 +1,5 @@ /* * Copyright (C) 2026 Behdad Esfahbod - * Copyright (C) 2017 Eric Lengyel - * - * Based on the Slug algorithm by Eric Lengyel: - * https://github.com/EricLengyel/Slug * * This is part of HarfBuzz, a text shaping library. * @@ -27,307 +23,14 @@ */ -/* Draw-renderer fragment shader. - * - * Assumes the shared fragment helpers (hb-gpu-fragment.wgsl) are - * prepended to this source. - */ +/* Draw-renderer fragment shader entry (WGSL). Heavy lifting + * (Slug coverage, MSAA, ppem, stem darkening) lives in the shared + * hb-gpu-fragment.wgsl that must be prepended to this source. */ -fn _hb_gpu_calc_root_code (y1: f32, y2: f32, y3: f32) -> u32 -{ - let i1 = bitcast (y1) >> 31u; - let i2 = bitcast (y2) >> 30u; - let i3 = bitcast (y3) >> 29u; - - var shift = (i2 & 2u) | (i1 & ~2u); - shift = (i3 & 4u) | (shift & ~4u); - - return (0x2E74u >> shift) & 0x0101u; -} - -fn _hb_gpu_solve_horiz_poly (a: vec2f, b: vec2f, p1: vec2f) -> vec2f -{ - let ra = 1.0 / a.y; - let rb = 0.5 / b.y; - - let d = sqrt (max (b.y * b.y - a.y * p1.y, 0.0)); - var t1 = (b.y - d) * ra; - var t2 = (b.y + d) * ra; - - if (a.y == 0.0) { - t1 = p1.y * rb; - t2 = t1; - } - - return vec2f ((a.x * t1 - b.x * 2.0) * t1 + p1.x, - (a.x * t2 - b.x * 2.0) * t2 + p1.x); -} - -fn _hb_gpu_solve_vert_poly (a: vec2f, b: vec2f, p1: vec2f) -> vec2f -{ - let ra = 1.0 / a.x; - let rb = 0.5 / b.x; - - let d = sqrt (max (b.x * b.x - a.x * p1.x, 0.0)); - var t1 = (b.x - d) * ra; - var t2 = (b.x + d) * ra; - - if (a.x == 0.0) { - t1 = p1.x * rb; - t2 = t1; - } - - return vec2f ((a.y * t1 - b.y * 2.0) * t1 + p1.y, - (a.y * t2 - b.y * 2.0) * t2 + p1.y); -} - -fn _hb_gpu_calc_coverage (xcov: f32, ycov: f32, xwgt: f32, ywgt: f32) -> f32 -{ - let coverage = max (abs (xcov * xwgt + ycov * ywgt) / - max (xwgt + ywgt, 1.0 / 65536.0), - min (abs (xcov), abs (ycov))); - - return clamp (coverage, 0.0, 1.0); -} - -/* Decoded glyph band info for a pixel position. */ -struct _hb_gpu_glyph_info -{ - glyphLoc: i32, - bandBase: i32, - bandIndex: vec2, - numHBands: i32, - numVBands: i32, - scale: vec2f, -} - -fn _hb_gpu_decode_glyph (renderCoord: vec2f, glyphLoc_: u32, - hb_gpu_atlas: ptr>, read>) -> _hb_gpu_glyph_info -{ - var gi: _hb_gpu_glyph_info; - gi.glyphLoc = i32 (glyphLoc_); - - let header0 = hb_gpu_fetch (hb_gpu_atlas, gi.glyphLoc); - let header1 = hb_gpu_fetch (hb_gpu_atlas, gi.glyphLoc + 1); - let ext = vec4f (header0) * HB_GPU_INV_UNITS; - gi.numHBands = header1.r; - gi.numVBands = header1.g; - gi.scale = vec2f (f32 (header1.b), f32 (header1.a)); - - let extSize = ext.zw - ext.xy; - let bandScale = vec2f (f32 (gi.numVBands), f32 (gi.numHBands)) / max (extSize, vec2f (1.0 / 65536.0)); - let bandOffset = -ext.xy * bandScale; - - gi.bandIndex = clamp (vec2 (renderCoord * bandScale + bandOffset), - vec2 (0, 0), - vec2 (gi.numVBands - 1, gi.numHBands - 1)); - - gi.bandBase = gi.glyphLoc + 2; - return gi; -} - -/* Return pixels per em at this fragment. - * - * renderCoord: em-space sample position - * glyphLoc: texel offset of glyph blob in atlas - */ -fn hb_gpu_ppem (renderCoord: vec2f, glyphLoc_: u32, - hb_gpu_atlas: ptr>, read>) -> f32 -{ - let gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_, hb_gpu_atlas); - let emsPerPixel = fwidth (renderCoord); - return min (gi.scale.x, gi.scale.y) / - max (emsPerPixel.x, emsPerPixel.y); -} - -/* Return per-pixel curve counts: (horizontal, vertical). */ -fn _hb_gpu_curve_counts (renderCoord: vec2f, glyphLoc_: u32, - hb_gpu_atlas: ptr>, read>) -> vec2 -{ - let gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_, hb_gpu_atlas); - let hCount = hb_gpu_fetch (hb_gpu_atlas, gi.bandBase + gi.bandIndex.y).r; - let vCount = hb_gpu_fetch (hb_gpu_atlas, gi.bandBase + gi.numHBands + gi.bandIndex.x).r; - return vec2 (hCount, vCount); -} - -/* Single-sample coverage in [0, 1]. */ -fn _hb_gpu_draw_single (renderCoord: vec2f, pixelsPerEm: vec2f, glyphLoc_: u32, - hb_gpu_atlas: ptr>, read>) -> f32 -{ - - let gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_, hb_gpu_atlas); - let glyphLoc = gi.glyphLoc; - let bandBase = gi.bandBase; - let numHBands = gi.numHBands; - - var xcov: f32 = 0.0; - var xwgt: f32 = 0.0; - - let hbandData = hb_gpu_fetch (hb_gpu_atlas, bandBase + gi.bandIndex.y); - let hCurveCount = hbandData.r; - /* Symmetric: choose rightward (desc) or leftward (asc) sort */ - let hSplit = f32 (hbandData.a) * HB_GPU_INV_UNITS; - let hLeftRay = (renderCoord.x < hSplit); - var hDataOffset: i32; - if (hLeftRay) { hDataOffset = hbandData.b + 32768; } - else { hDataOffset = hbandData.g + 32768; } - - for (var ci: i32 = 0; ci < hCurveCount; ci++) - { - let curveOffset = hb_gpu_fetch (hb_gpu_atlas, glyphLoc + hDataOffset + ci).r + 32768; - - let raw12 = hb_gpu_fetch (hb_gpu_atlas, glyphLoc + curveOffset); - let raw3 = hb_gpu_fetch (hb_gpu_atlas, glyphLoc + curveOffset + 1); - - let q12 = vec4f (raw12) * HB_GPU_INV_UNITS; - let q3 = vec2f (vec2 (raw3.r, raw3.g)) * HB_GPU_INV_UNITS; - - let p12 = q12 - vec4f (renderCoord, renderCoord); - let p3 = q3 - renderCoord; - - if (hLeftRay) { - if (min (min (p12.x, p12.z), p3.x) * pixelsPerEm.x > 0.5) { break; } - } else { - if (max (max (p12.x, p12.z), p3.x) * pixelsPerEm.x < -0.5) { break; } - } - - let code = _hb_gpu_calc_root_code (p12.y, p12.w, p3.y); - if (code != 0u) - { - let a = q12.xy - q12.zw * 2.0 + q3; - let b = q12.xy - q12.zw; - let r = _hb_gpu_solve_horiz_poly (a, b, p12.xy) * pixelsPerEm.x; - /* For leftward ray: saturate(0.5 - r) counts coverage from the left */ - var cov: vec2f; - if (hLeftRay) { cov = clamp (vec2f (0.5) - r, vec2f (0.0), vec2f (1.0)); } - else { cov = clamp (r + vec2f (0.5), vec2f (0.0), vec2f (1.0)); } - - if ((code & 1u) != 0u) - { - xcov += cov.x; - xwgt = max (xwgt, clamp (1.0 - abs (r.x) * 2.0, 0.0, 1.0)); - } - - if (code > 1u) - { - xcov -= cov.y; - xwgt = max (xwgt, clamp (1.0 - abs (r.y) * 2.0, 0.0, 1.0)); - } - } - } - - var ycov: f32 = 0.0; - var ywgt: f32 = 0.0; - - let vbandData = hb_gpu_fetch (hb_gpu_atlas, bandBase + numHBands + gi.bandIndex.x); - let vCurveCount = vbandData.r; - let vSplit = f32 (vbandData.a) * HB_GPU_INV_UNITS; - let vLeftRay = (renderCoord.y < vSplit); - var vDataOffset: i32; - if (vLeftRay) { vDataOffset = vbandData.b + 32768; } - else { vDataOffset = vbandData.g + 32768; } - - for (var ci: i32 = 0; ci < vCurveCount; ci++) - { - let curveOffset = hb_gpu_fetch (hb_gpu_atlas, glyphLoc + vDataOffset + ci).r + 32768; - - let raw12 = hb_gpu_fetch (hb_gpu_atlas, glyphLoc + curveOffset); - let raw3 = hb_gpu_fetch (hb_gpu_atlas, glyphLoc + curveOffset + 1); - - let q12 = vec4f (raw12) * HB_GPU_INV_UNITS; - let q3 = vec2f (vec2 (raw3.r, raw3.g)) * HB_GPU_INV_UNITS; - - let p12 = q12 - vec4f (renderCoord, renderCoord); - let p3 = q3 - renderCoord; - - if (vLeftRay) { - if (min (min (p12.y, p12.w), p3.y) * pixelsPerEm.y > 0.5) { break; } - } else { - if (max (max (p12.y, p12.w), p3.y) * pixelsPerEm.y < -0.5) { break; } - } - - let code = _hb_gpu_calc_root_code (p12.x, p12.z, p3.x); - if (code != 0u) - { - let a = q12.xy - q12.zw * 2.0 + q3; - let b = q12.xy - q12.zw; - let r = _hb_gpu_solve_vert_poly (a, b, p12.xy) * pixelsPerEm.y; - var cov: vec2f; - if (vLeftRay) { cov = clamp (vec2f (0.5) - r, vec2f (0.0), vec2f (1.0)); } - else { cov = clamp (r + vec2f (0.5), vec2f (0.0), vec2f (1.0)); } - - if ((code & 1u) != 0u) - { - ycov -= cov.x; - ywgt = max (ywgt, clamp (1.0 - abs (r.x) * 2.0, 0.0, 1.0)); - } - - if (code > 1u) - { - ycov += cov.y; - ywgt = max (ywgt, clamp (1.0 - abs (r.y) * 2.0, 0.0, 1.0)); - } - } - } - - return _hb_gpu_calc_coverage (xcov, ycov, xwgt, ywgt); -} - -/* Return coverage in [0, 1]. - * - * renderCoord: em-space sample position - * glyphLoc: texel offset of glyph blob in atlas - * hb_gpu_atlas: storage buffer pointer to the atlas - */ -/* The MSAA-aware implementation. Caller supplies pixelsPerEm so - * this function can be invoked from non-uniform control flow (for - * example inside an op-stream branch in hb-gpu-paint-fragment.wgsl, - * where WGSL would otherwise reject an fwidth call). */ -fn _hb_gpu_draw_impl (renderCoord: vec2f, pixelsPerEm: vec2f, glyphLoc_: u32, - hb_gpu_atlas: ptr>, read>) -> f32 -{ - var c = _hb_gpu_draw_single (renderCoord, pixelsPerEm, glyphLoc_, hb_gpu_atlas); - - /* Inline ppem from pixelsPerEm so we don't re-enter hb_gpu_ppem - * (which would call fwidth again -- rejected by WGSL when this - * function is invoked from non-uniform control flow). */ - let gi_pp = _hb_gpu_decode_glyph (renderCoord, glyphLoc_, hb_gpu_atlas); - let ppem = min (gi_pp.scale.x, gi_pp.scale.y) * - min (pixelsPerEm.x, pixelsPerEm.y); - - if (ppem < 16.0) - { - let emsPerPixel = 1.0 / pixelsPerEm; - let d = emsPerPixel * (1.0 / 3.0); - let msaa = 0.25 * - (_hb_gpu_draw_single (renderCoord + vec2f (-d.x, -d.y), pixelsPerEm, glyphLoc_, hb_gpu_atlas) + - _hb_gpu_draw_single (renderCoord + vec2f ( d.x, -d.y), pixelsPerEm, glyphLoc_, hb_gpu_atlas) + - _hb_gpu_draw_single (renderCoord + vec2f (-d.x, d.y), pixelsPerEm, glyphLoc_, hb_gpu_atlas) + - _hb_gpu_draw_single (renderCoord + vec2f ( d.x, d.y), pixelsPerEm, glyphLoc_, hb_gpu_atlas)); - - c = mix (c, msaa, smoothstep (16.0, 8.0, ppem)); - } - - return c; -} - fn hb_gpu_draw (renderCoord: vec2f, glyphLoc_: u32, hb_gpu_atlas: ptr>, read>) -> f32 { let pixelsPerEm = 1.0 / fwidth (renderCoord); - return _hb_gpu_draw_impl (renderCoord, pixelsPerEm, glyphLoc_, hb_gpu_atlas); -} - -/* Stem darkening for small sizes. - * - * coverage: output of hb_gpu_draw - * brightness: foreground brightness in [0, 1] - * ppem: pixels per em at this fragment - */ -fn hb_gpu_stem_darken (coverage: f32, brightness: f32, ppem: f32) -> f32 -{ - return pow (coverage, - mix (pow (2.0, brightness - 0.5), 1.0, - smoothstep (8.0, 48.0, ppem))); + return _hb_gpu_slug (renderCoord, pixelsPerEm, glyphLoc_, hb_gpu_atlas); } diff --git a/src/hb-gpu-fragment-glsl.hh b/src/hb-gpu-fragment-glsl.hh index d292a4d98..c7356fede 100644 --- a/src/hb-gpu-fragment-glsl.hh +++ b/src/hb-gpu-fragment-glsl.hh @@ -1,6 +1,10 @@ static const char *hb_gpu_fragment_glsl = "/*\n" " * Copyright (C) 2026 Behdad Esfahbod\n" +" * Copyright (C) 2017 Eric Lengyel\n" +" *\n" +" * The _hb_gpu_slug body below is based on the Slug algorithm by\n" +" * Eric Lengyel: https://github.com/EricLengyel/Slug\n" " *\n" " * This is part of HarfBuzz, a text shaping library.\n" " *\n" @@ -32,6 +36,18 @@ static const char *hb_gpu_fragment_glsl = " * including this source. The atlas is then a 2D isampler2D\n" " * texture of known width (set via hb_gpu_atlas_width uniform)\n" " * instead of an isamplerBuffer.\n" +" *\n" +" * Exports:\n" +" * hb_gpu_fetch (offset) atlas lookup\n" +" * _hb_gpu_slug (rc, pixelsPerEm, glyphLoc) MSAA-aware Slug\n" +" * coverage (caller supplies\n" +" * pixelsPerEm so it can be\n" +" * invoked from non-uniform\n" +" * control flow)\n" +" * hb_gpu_ppem (rc, glyphLoc) pixels per em at fragment\n" +" * _hb_gpu_curve_counts (rc, glyphLoc) debug: per-pixel curve counts\n" +" * hb_gpu_stem_darken (cov, brightness, ppem) thin-stroke contrast\n" +" * correction\n" " */\n" "\n" "\n" @@ -58,4 +74,268 @@ static const char *hb_gpu_fragment_glsl = " return texelFetch (hb_gpu_atlas, offset);\n" "}\n" "#endif\n" +"\n" +"\n" +"uint _hb_gpu_calc_root_code (float y1, float y2, float y3)\n" +"{\n" +" uint i1 = floatBitsToUint (y1) >> 31U;\n" +" uint i2 = floatBitsToUint (y2) >> 30U;\n" +" uint i3 = floatBitsToUint (y3) >> 29U;\n" +"\n" +" uint shift = (i2 & 2U) | (i1 & ~2U);\n" +" shift = (i3 & 4U) | (shift & ~4U);\n" +"\n" +" return (0x2E74U >> shift) & 0x0101U;\n" +"}\n" +"\n" +"vec2 _hb_gpu_solve_horiz_poly (vec2 a, vec2 b, vec2 p1)\n" +"{\n" +" float ra = 1.0 / a.y;\n" +" float rb = 0.5 / b.y;\n" +"\n" +" float d = sqrt (max (b.y * b.y - a.y * p1.y, 0.0));\n" +" float t1 = (b.y - d) * ra;\n" +" float t2 = (b.y + d) * ra;\n" +"\n" +" if (a.y == 0.0)\n" +" t1 = t2 = p1.y * rb;\n" +"\n" +" return vec2 ((a.x * t1 - b.x * 2.0) * t1 + p1.x,\n" +" (a.x * t2 - b.x * 2.0) * t2 + p1.x);\n" +"}\n" +"\n" +"vec2 _hb_gpu_solve_vert_poly (vec2 a, vec2 b, vec2 p1)\n" +"{\n" +" float ra = 1.0 / a.x;\n" +" float rb = 0.5 / b.x;\n" +"\n" +" float d = sqrt (max (b.x * b.x - a.x * p1.x, 0.0));\n" +" float t1 = (b.x - d) * ra;\n" +" float t2 = (b.x + d) * ra;\n" +"\n" +" if (a.x == 0.0)\n" +" t1 = t2 = p1.x * rb;\n" +"\n" +" return vec2 ((a.y * t1 - b.y * 2.0) * t1 + p1.y,\n" +" (a.y * t2 - b.y * 2.0) * t2 + p1.y);\n" +"}\n" +"\n" +"float _hb_gpu_calc_coverage (float xcov, float ycov, float xwgt, float ywgt)\n" +"{\n" +" float coverage = max (abs (xcov * xwgt + ycov * ywgt) /\n" +" max (xwgt + ywgt, 1.0 / 65536.0),\n" +" min (abs (xcov), abs (ycov)));\n" +"\n" +" return clamp (coverage, 0.0, 1.0);\n" +"}\n" +"\n" +"/* Decoded glyph band info for a pixel position. */\n" +"struct _hb_gpu_glyph_info\n" +"{\n" +" int glyphLoc;\n" +" int bandBase;\n" +" ivec2 bandIndex;\n" +" int numHBands;\n" +" int numVBands;\n" +" vec2 scale;\n" +"};\n" +"\n" +"_hb_gpu_glyph_info _hb_gpu_decode_glyph (vec2 renderCoord, uint glyphLoc_)\n" +"{\n" +" _hb_gpu_glyph_info gi;\n" +" gi.glyphLoc = int (glyphLoc_);\n" +"\n" +" ivec4 header0 = hb_gpu_fetch (gi.glyphLoc);\n" +" ivec4 header1 = hb_gpu_fetch (gi.glyphLoc + 1);\n" +" vec4 ext = vec4 (header0) * HB_GPU_INV_UNITS;\n" +" gi.numHBands = header1.r;\n" +" gi.numVBands = header1.g;\n" +" gi.scale = vec2 (float (header1.b), float (header1.a));\n" +"\n" +" vec2 extSize = ext.zw - ext.xy;\n" +" vec2 bandScale = vec2 (float (gi.numVBands), float (gi.numHBands)) / max (extSize, vec2 (1.0 / 65536.0));\n" +" vec2 bandOffset = -ext.xy * bandScale;\n" +"\n" +" gi.bandIndex = clamp (ivec2 (renderCoord * bandScale + bandOffset),\n" +" ivec2 (0, 0),\n" +" ivec2 (gi.numVBands - 1, gi.numHBands - 1));\n" +"\n" +" gi.bandBase = gi.glyphLoc + 2;\n" +" return gi;\n" +"}\n" +"\n" +"/* Return pixels per em at this fragment. */\n" +"float hb_gpu_ppem (vec2 renderCoord, uint glyphLoc_)\n" +"{\n" +" _hb_gpu_glyph_info gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_);\n" +" vec2 emsPerPixel = fwidth (renderCoord);\n" +" return min (gi.scale.x, gi.scale.y) /\n" +" max (emsPerPixel.x, emsPerPixel.y);\n" +"}\n" +"\n" +"/* Return per-pixel curve counts: (horizontal, vertical). */\n" +"ivec2 _hb_gpu_curve_counts (vec2 renderCoord, uint glyphLoc_)\n" +"{\n" +" _hb_gpu_glyph_info gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_);\n" +" int hCount = hb_gpu_fetch (gi.bandBase + gi.bandIndex.y).r;\n" +" int vCount = hb_gpu_fetch (gi.bandBase + gi.numHBands + gi.bandIndex.x).r;\n" +" return ivec2 (hCount, vCount);\n" +"}\n" +"\n" +"/* Single-sample coverage in [0, 1]. */\n" +"float _hb_gpu_slug_single (vec2 renderCoord, vec2 pixelsPerEm, uint glyphLoc_)\n" +"{\n" +"\n" +" _hb_gpu_glyph_info gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_);\n" +" int glyphLoc = gi.glyphLoc;\n" +" int bandBase = gi.bandBase;\n" +" int numHBands = gi.numHBands;\n" +"\n" +" float xcov = 0.0;\n" +" float xwgt = 0.0;\n" +"\n" +" ivec4 hbandData = hb_gpu_fetch (bandBase + gi.bandIndex.y);\n" +" int hCurveCount = hbandData.r;\n" +" /* Symmetric: choose rightward (desc) or leftward (asc) sort */\n" +" float hSplit = float (hbandData.a) * HB_GPU_INV_UNITS;\n" +" bool hLeftRay = (renderCoord.x < hSplit);\n" +" int hDataOffset = (hLeftRay ? hbandData.b : hbandData.g) + 32768;\n" +"\n" +" for (int ci = 0; ci < hCurveCount; ci++)\n" +" {\n" +" int curveOffset = hb_gpu_fetch (glyphLoc + hDataOffset + ci).r + 32768;\n" +"\n" +" ivec4 raw12 = hb_gpu_fetch (glyphLoc + curveOffset);\n" +" ivec4 raw3 = hb_gpu_fetch (glyphLoc + curveOffset + 1);\n" +"\n" +" vec4 q12 = vec4 (raw12) * HB_GPU_INV_UNITS;\n" +" vec2 q3 = vec2 (raw3.rg) * HB_GPU_INV_UNITS;\n" +"\n" +" vec4 p12 = q12 - vec4 (renderCoord, renderCoord);\n" +" vec2 p3 = q3 - renderCoord;\n" +"\n" +" if (hLeftRay) {\n" +" if (min (min (p12.x, p12.z), p3.x) * pixelsPerEm.x > 0.5) break;\n" +" } else {\n" +" if (max (max (p12.x, p12.z), p3.x) * pixelsPerEm.x < -0.5) break;\n" +" }\n" +"\n" +" uint code = _hb_gpu_calc_root_code (p12.y, p12.w, p3.y);\n" +" if (code != 0U)\n" +" {\n" +" vec2 a = q12.xy - q12.zw * 2.0 + q3;\n" +" vec2 b = q12.xy - q12.zw;\n" +" vec2 r = _hb_gpu_solve_horiz_poly (a, b, p12.xy) * pixelsPerEm.x;\n" +" /* For leftward ray: saturate(0.5 - r) counts coverage from the left */\n" +" vec2 cov = hLeftRay ? clamp (vec2 (0.5) - r, 0.0, 1.0)\n" +" : clamp (r + vec2 (0.5), 0.0, 1.0);\n" +"\n" +" if ((code & 1U) != 0U)\n" +" {\n" +" xcov += cov.x;\n" +" xwgt = max (xwgt, clamp (1.0 - abs (r.x) * 2.0, 0.0, 1.0));\n" +" }\n" +"\n" +" if (code > 1U)\n" +" {\n" +" xcov -= cov.y;\n" +" xwgt = max (xwgt, clamp (1.0 - abs (r.y) * 2.0, 0.0, 1.0));\n" +" }\n" +" }\n" +" }\n" +"\n" +" float ycov = 0.0;\n" +" float ywgt = 0.0;\n" +"\n" +" ivec4 vbandData = hb_gpu_fetch (bandBase + numHBands + gi.bandIndex.x);\n" +" int vCurveCount = vbandData.r;\n" +" float vSplit = float (vbandData.a) * HB_GPU_INV_UNITS;\n" +" bool vLeftRay = (renderCoord.y < vSplit);\n" +" int vDataOffset = (vLeftRay ? vbandData.b : vbandData.g) + 32768;\n" +"\n" +" for (int ci = 0; ci < vCurveCount; ci++)\n" +" {\n" +" int curveOffset = hb_gpu_fetch (glyphLoc + vDataOffset + ci).r + 32768;\n" +"\n" +" ivec4 raw12 = hb_gpu_fetch (glyphLoc + curveOffset);\n" +" ivec4 raw3 = hb_gpu_fetch (glyphLoc + curveOffset + 1);\n" +"\n" +" vec4 q12 = vec4 (raw12) * HB_GPU_INV_UNITS;\n" +" vec2 q3 = vec2 (raw3.rg) * HB_GPU_INV_UNITS;\n" +"\n" +" vec4 p12 = q12 - vec4 (renderCoord, renderCoord);\n" +" vec2 p3 = q3 - renderCoord;\n" +"\n" +" if (vLeftRay) {\n" +" if (min (min (p12.y, p12.w), p3.y) * pixelsPerEm.y > 0.5) break;\n" +" } else {\n" +" if (max (max (p12.y, p12.w), p3.y) * pixelsPerEm.y < -0.5) break;\n" +" }\n" +"\n" +" uint code = _hb_gpu_calc_root_code (p12.x, p12.z, p3.x);\n" +" if (code != 0U)\n" +" {\n" +" vec2 a = q12.xy - q12.zw * 2.0 + q3;\n" +" vec2 b = q12.xy - q12.zw;\n" +" vec2 r = _hb_gpu_solve_vert_poly (a, b, p12.xy) * pixelsPerEm.y;\n" +" vec2 cov = vLeftRay ? clamp (vec2 (0.5) - r, 0.0, 1.0)\n" +" : clamp (r + vec2 (0.5), 0.0, 1.0);\n" +"\n" +" if ((code & 1U) != 0U)\n" +" {\n" +" ycov -= cov.x;\n" +" ywgt = max (ywgt, clamp (1.0 - abs (r.x) * 2.0, 0.0, 1.0));\n" +" }\n" +"\n" +" if (code > 1U)\n" +" {\n" +" ycov += cov.y;\n" +" ywgt = max (ywgt, clamp (1.0 - abs (r.y) * 2.0, 0.0, 1.0));\n" +" }\n" +" }\n" +" }\n" +"\n" +" return _hb_gpu_calc_coverage (xcov, ycov, xwgt, ywgt);\n" +"}\n" +"\n" +"/* MSAA-aware Slug coverage. Caller supplies pixelsPerEm so this\n" +" * function can be invoked from non-uniform control flow (for\n" +" * example inside an op-stream branch in hb-gpu-paint-fragment.wgsl,\n" +" * where WGSL would otherwise reject an fwidth call). */\n" +"float _hb_gpu_slug (vec2 renderCoord, vec2 pixelsPerEm, uint glyphLoc_)\n" +"{\n" +" float c = _hb_gpu_slug_single (renderCoord, pixelsPerEm, glyphLoc_);\n" +"\n" +"#ifndef HB_GPU_NO_MSAA\n" +" float ppem = hb_gpu_ppem (renderCoord, glyphLoc_);\n" +"\n" +" if (ppem < 16.0)\n" +" {\n" +" vec2 emsPerPixel = 1.0 / pixelsPerEm;\n" +" vec2 d = emsPerPixel * (1.0 / 3.0);\n" +" float msaa = 0.25 *\n" +" (_hb_gpu_slug_single (renderCoord + vec2 (-d.x, -d.y), pixelsPerEm, glyphLoc_) +\n" +" _hb_gpu_slug_single (renderCoord + vec2 ( d.x, -d.y), pixelsPerEm, glyphLoc_) +\n" +" _hb_gpu_slug_single (renderCoord + vec2 (-d.x, d.y), pixelsPerEm, glyphLoc_) +\n" +" _hb_gpu_slug_single (renderCoord + vec2 ( d.x, d.y), pixelsPerEm, glyphLoc_));\n" +"\n" +" c = mix (c, msaa, smoothstep (16.0, 8.0, ppem));\n" +" }\n" +"#endif\n" +"\n" +" return c;\n" +"}\n" +"\n" +"/* Stem darkening for small sizes.\n" +" *\n" +" * coverage: output of hb_gpu_draw / _hb_gpu_slug\n" +" * brightness: foreground brightness in [0, 1]\n" +" * ppem: pixels per em at this fragment\n" +" */\n" +"float hb_gpu_stem_darken (float coverage, float brightness, float ppem)\n" +"{\n" +" return pow (coverage,\n" +" mix (pow (2.0, brightness - 0.5), 1.0,\n" +" smoothstep (8.0, 48.0, ppem)));\n" +"}\n" ; diff --git a/src/hb-gpu-fragment-hlsl.hh b/src/hb-gpu-fragment-hlsl.hh index 418a4ff0b..bfe2c9301 100644 --- a/src/hb-gpu-fragment-hlsl.hh +++ b/src/hb-gpu-fragment-hlsl.hh @@ -44,4 +44,273 @@ static const char *hb_gpu_fragment_hlsl = "{\n" " return hb_gpu_atlas[offset];\n" "}\n" +"uint _hb_gpu_calc_root_code (float y1, float y2, float y3)\n" +"{\n" +" uint i1 = asuint (y1) >> 31u;\n" +" uint i2 = asuint (y2) >> 30u;\n" +" uint i3 = asuint (y3) >> 29u;\n" +"\n" +" uint shift = (i2 & 2u) | (i1 & ~2u);\n" +" shift = (i3 & 4u) | (shift & ~4u);\n" +"\n" +" return (0x2E74u >> shift) & 0x0101u;\n" +"}\n" +"\n" +"float2 _hb_gpu_solve_horiz_poly (float2 a, float2 b, float2 p1)\n" +"{\n" +" float ra = 1.0 / a.y;\n" +" float rb = 0.5 / b.y;\n" +"\n" +" float d = sqrt (max (b.y * b.y - a.y * p1.y, 0.0));\n" +" float t1 = (b.y - d) * ra;\n" +" float t2 = (b.y + d) * ra;\n" +"\n" +" if (a.y == 0.0)\n" +" t1 = t2 = p1.y * rb;\n" +"\n" +" return float2 ((a.x * t1 - b.x * 2.0) * t1 + p1.x,\n" +" (a.x * t2 - b.x * 2.0) * t2 + p1.x);\n" +"}\n" +"\n" +"float2 _hb_gpu_solve_vert_poly (float2 a, float2 b, float2 p1)\n" +"{\n" +" float ra = 1.0 / a.x;\n" +" float rb = 0.5 / b.x;\n" +"\n" +" float d = sqrt (max (b.x * b.x - a.x * p1.x, 0.0));\n" +" float t1 = (b.x - d) * ra;\n" +" float t2 = (b.x + d) * ra;\n" +"\n" +" if (a.x == 0.0)\n" +" t1 = t2 = p1.x * rb;\n" +"\n" +" return float2 ((a.y * t1 - b.y * 2.0) * t1 + p1.y,\n" +" (a.y * t2 - b.y * 2.0) * t2 + p1.y);\n" +"}\n" +"\n" +"float _hb_gpu_calc_coverage (float xcov, float ycov, float xwgt, float ywgt)\n" +"{\n" +" float coverage = max (abs (xcov * xwgt + ycov * ywgt) /\n" +" max (xwgt + ywgt, 1.0 / 65536.0),\n" +" min (abs (xcov), abs (ycov)));\n" +"\n" +" return clamp (coverage, 0.0, 1.0);\n" +"}\n" +"\n" +"\n" +"struct _hb_gpu_glyph_info\n" +"{\n" +" int glyphLoc;\n" +" int bandBase;\n" +" int2 bandIndex;\n" +" int numHBands;\n" +" int numVBands;\n" +" float2 scale;\n" +"};\n" +"\n" +"_hb_gpu_glyph_info _hb_gpu_decode_glyph (float2 renderCoord, uint glyphLoc_)\n" +"{\n" +" _hb_gpu_glyph_info gi;\n" +" gi.glyphLoc = (int) glyphLoc_;\n" +"\n" +" int4 header0 = hb_gpu_fetch (gi.glyphLoc);\n" +" int4 header1 = hb_gpu_fetch (gi.glyphLoc + 1);\n" +" float4 ext = (float4) header0 * HB_GPU_INV_UNITS;\n" +" gi.numHBands = header1.r;\n" +" gi.numVBands = header1.g;\n" +" gi.scale = float2 ((float) header1.b, (float) header1.a);\n" +"\n" +" float2 extSize = ext.zw - ext.xy;\n" +" float2 bandScale = float2 ((float) gi.numVBands, (float) gi.numHBands) / max (extSize, float2 (1.0 / 65536.0, 1.0 / 65536.0));\n" +" float2 bandOffset = -ext.xy * bandScale;\n" +"\n" +" gi.bandIndex = clamp ((int2) (renderCoord * bandScale + bandOffset),\n" +" int2 (0, 0),\n" +" int2 (gi.numVBands - 1, gi.numHBands - 1));\n" +"\n" +" gi.bandBase = gi.glyphLoc + 2;\n" +" return gi;\n" +"}\n" +"\n" +"/* Return pixels per em at this fragment.\n" +" *\n" +" * renderCoord: em-space sample position\n" +" * glyphLoc: texel offset of glyph blob in atlas\n" +" */\n" +"float hb_gpu_ppem (float2 renderCoord, uint glyphLoc_)\n" +"{\n" +" _hb_gpu_glyph_info gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_);\n" +" float2 emsPerPixel = fwidth (renderCoord);\n" +" return min (gi.scale.x, gi.scale.y) /\n" +" max (emsPerPixel.x, emsPerPixel.y);\n" +"}\n" +"\n" +"int2 _hb_gpu_curve_counts (float2 renderCoord, uint glyphLoc_)\n" +"{\n" +" _hb_gpu_glyph_info gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_);\n" +" int hCount = hb_gpu_fetch (gi.bandBase + gi.bandIndex.y).r;\n" +" int vCount = hb_gpu_fetch (gi.bandBase + gi.numHBands + gi.bandIndex.x).r;\n" +" return int2 (hCount, vCount);\n" +"}\n" +"\n" +"\n" +"/* Single-sample coverage in [0, 1]. */\n" +"float _hb_gpu_slug_single (float2 renderCoord, float2 pixelsPerEm, uint glyphLoc_)\n" +"{\n" +"\n" +" _hb_gpu_glyph_info gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_);\n" +" int glyphLoc = gi.glyphLoc;\n" +" int bandBase = gi.bandBase;\n" +" int numHBands = gi.numHBands;\n" +"\n" +" float xcov = 0.0;\n" +" float xwgt = 0.0;\n" +"\n" +" int4 hbandData = hb_gpu_fetch (bandBase + gi.bandIndex.y);\n" +" int hCurveCount = hbandData.r;\n" +" float hSplit = (float) hbandData.a * HB_GPU_INV_UNITS;\n" +" bool hLeftRay = (renderCoord.x < hSplit);\n" +" int hDataOffset = (hLeftRay ? hbandData.b : hbandData.g) + 32768;\n" +"\n" +" for (int ci = 0; ci < hCurveCount; ci++)\n" +" {\n" +" int curveOffset = hb_gpu_fetch (glyphLoc + hDataOffset + ci).r + 32768;\n" +"\n" +" int4 raw12 = hb_gpu_fetch (glyphLoc + curveOffset);\n" +" int4 raw3 = hb_gpu_fetch (glyphLoc + curveOffset + 1);\n" +"\n" +" float4 q12 = (float4) raw12 * HB_GPU_INV_UNITS;\n" +" float2 q3 = (float2) raw3.rg * HB_GPU_INV_UNITS;\n" +"\n" +" float4 p12 = q12 - float4 (renderCoord, renderCoord);\n" +" float2 p3 = q3 - renderCoord;\n" +"\n" +" if (hLeftRay) {\n" +" if (min (min (p12.x, p12.z), p3.x) * pixelsPerEm.x > 0.5) break;\n" +" } else {\n" +" if (max (max (p12.x, p12.z), p3.x) * pixelsPerEm.x < -0.5) break;\n" +" }\n" +"\n" +" uint code = _hb_gpu_calc_root_code (p12.y, p12.w, p3.y);\n" +" if (code != 0u)\n" +" {\n" +" float2 a = q12.xy - q12.zw * 2.0 + q3;\n" +" float2 b = q12.xy - q12.zw;\n" +" float2 r = _hb_gpu_solve_horiz_poly (a, b, p12.xy) * pixelsPerEm.x;\n" +" float2 cov = hLeftRay ? clamp (float2 (0.5, 0.5) - r, 0.0, 1.0)\n" +" : clamp (r + float2 (0.5, 0.5), 0.0, 1.0);\n" +"\n" +" if ((code & 1u) != 0u)\n" +" {\n" +" xcov += cov.x;\n" +" xwgt = max (xwgt, clamp (1.0 - abs (r.x) * 2.0, 0.0, 1.0));\n" +" }\n" +"\n" +" if (code > 1u)\n" +" {\n" +" xcov -= cov.y;\n" +" xwgt = max (xwgt, clamp (1.0 - abs (r.y) * 2.0, 0.0, 1.0));\n" +" }\n" +" }\n" +" }\n" +"\n" +" float ycov = 0.0;\n" +" float ywgt = 0.0;\n" +"\n" +" int4 vbandData = hb_gpu_fetch (bandBase + numHBands + gi.bandIndex.x);\n" +" int vCurveCount = vbandData.r;\n" +" float vSplit = (float) vbandData.a * HB_GPU_INV_UNITS;\n" +" bool vLeftRay = (renderCoord.y < vSplit);\n" +" int vDataOffset = (vLeftRay ? vbandData.b : vbandData.g) + 32768;\n" +"\n" +" for (int ci = 0; ci < vCurveCount; ci++)\n" +" {\n" +" int curveOffset = hb_gpu_fetch (glyphLoc + vDataOffset + ci).r + 32768;\n" +"\n" +" int4 raw12 = hb_gpu_fetch (glyphLoc + curveOffset);\n" +" int4 raw3 = hb_gpu_fetch (glyphLoc + curveOffset + 1);\n" +"\n" +" float4 q12 = (float4) raw12 * HB_GPU_INV_UNITS;\n" +" float2 q3 = (float2) raw3.rg * HB_GPU_INV_UNITS;\n" +"\n" +" float4 p12 = q12 - float4 (renderCoord, renderCoord);\n" +" float2 p3 = q3 - renderCoord;\n" +"\n" +" if (vLeftRay) {\n" +" if (min (min (p12.y, p12.w), p3.y) * pixelsPerEm.y > 0.5) break;\n" +" } else {\n" +" if (max (max (p12.y, p12.w), p3.y) * pixelsPerEm.y < -0.5) break;\n" +" }\n" +"\n" +" uint code = _hb_gpu_calc_root_code (p12.x, p12.z, p3.x);\n" +" if (code != 0u)\n" +" {\n" +" float2 a = q12.xy - q12.zw * 2.0 + q3;\n" +" float2 b = q12.xy - q12.zw;\n" +" float2 r = _hb_gpu_solve_vert_poly (a, b, p12.xy) * pixelsPerEm.y;\n" +" float2 cov = vLeftRay ? clamp (float2 (0.5, 0.5) - r, 0.0, 1.0)\n" +" : clamp (r + float2 (0.5, 0.5), 0.0, 1.0);\n" +"\n" +" if ((code & 1u) != 0u)\n" +" {\n" +" ycov -= cov.x;\n" +" ywgt = max (ywgt, clamp (1.0 - abs (r.x) * 2.0, 0.0, 1.0));\n" +" }\n" +"\n" +" if (code > 1u)\n" +" {\n" +" ycov += cov.y;\n" +" ywgt = max (ywgt, clamp (1.0 - abs (r.y) * 2.0, 0.0, 1.0));\n" +" }\n" +" }\n" +" }\n" +"\n" +" return _hb_gpu_calc_coverage (xcov, ycov, xwgt, ywgt);\n" +"}\n" +"\n" +"/* Return coverage in [0, 1].\n" +" *\n" +" * Caller must declare: StructuredBuffer hb_gpu_atlas\n" +" *\n" +" * renderCoord: em-space sample position\n" +" * glyphLoc: texel offset of glyph blob in atlas\n" +" */\n" +"/* The MSAA-aware implementation. Caller supplies pixelsPerEm so\n" +" * this function can be invoked from non-uniform control flow (for\n" +" * example from a paint op-stream branch). */\n" +"float _hb_gpu_slug (float2 renderCoord, float2 pixelsPerEm, uint glyphLoc_)\n" +"{\n" +" float c = _hb_gpu_slug_single (renderCoord, pixelsPerEm, glyphLoc_);\n" +"\n" +"#ifndef HB_GPU_NO_MSAA\n" +" float ppem = hb_gpu_ppem (renderCoord, glyphLoc_);\n" +"\n" +" if (ppem < 16.0)\n" +" {\n" +" float2 emsPerPixel = 1.0 / pixelsPerEm;\n" +" float2 d = emsPerPixel * (1.0 / 3.0);\n" +" float msaa = 0.25 *\n" +" (_hb_gpu_slug_single (renderCoord + float2 (-d.x, -d.y), pixelsPerEm, glyphLoc_) +\n" +" _hb_gpu_slug_single (renderCoord + float2 ( d.x, -d.y), pixelsPerEm, glyphLoc_) +\n" +" _hb_gpu_slug_single (renderCoord + float2 (-d.x, d.y), pixelsPerEm, glyphLoc_) +\n" +" _hb_gpu_slug_single (renderCoord + float2 ( d.x, d.y), pixelsPerEm, glyphLoc_));\n" +"\n" +" c = lerp (c, msaa, smoothstep (16.0, 8.0, ppem));\n" +" }\n" +"#endif\n" +"\n" +" return c;\n" +"}\n" +"/* Stem darkening for small sizes.\n" +" *\n" +" * coverage: output of hb_gpu_draw\n" +" * brightness: foreground brightness in [0, 1]\n" +" * ppem: pixels per em at this fragment\n" +" */\n" +"float hb_gpu_stem_darken (float coverage, float brightness, float ppem)\n" +"{\n" +" return pow (coverage,\n" +" lerp (pow (2.0, brightness - 0.5), 1.0,\n" +" smoothstep (8.0, 48.0, ppem)));\n" +"}\n" ; diff --git a/src/hb-gpu-fragment-msl.hh b/src/hb-gpu-fragment-msl.hh index 763c5b813..fb32da391 100644 --- a/src/hb-gpu-fragment-msl.hh +++ b/src/hb-gpu-fragment-msl.hh @@ -41,4 +41,280 @@ static const char *hb_gpu_fragment_msl = "{\n" " return int4 (hb_gpu_atlas[offset]);\n" "}\n" +"\n" +"uint _hb_gpu_calc_root_code (float y1, float y2, float y3)\n" +"{\n" +" uint i1 = as_type (y1) >> 31U;\n" +" uint i2 = as_type (y2) >> 30U;\n" +" uint i3 = as_type (y3) >> 29U;\n" +"\n" +" uint shift = (i2 & 2U) | (i1 & ~2U);\n" +" shift = (i3 & 4U) | (shift & ~4U);\n" +"\n" +" return (0x2E74U >> shift) & 0x0101U;\n" +"}\n" +"\n" +"float2 _hb_gpu_solve_horiz_poly (float2 a, float2 b, float2 p1)\n" +"{\n" +" float ra = 1.0 / a.y;\n" +" float rb = 0.5 / b.y;\n" +"\n" +" float d = sqrt (max (b.y * b.y - a.y * p1.y, 0.0));\n" +" float t1 = (b.y - d) * ra;\n" +" float t2 = (b.y + d) * ra;\n" +"\n" +" if (a.y == 0.0)\n" +" t1 = t2 = p1.y * rb;\n" +"\n" +" return float2 ((a.x * t1 - b.x * 2.0) * t1 + p1.x,\n" +" (a.x * t2 - b.x * 2.0) * t2 + p1.x);\n" +"}\n" +"\n" +"float2 _hb_gpu_solve_vert_poly (float2 a, float2 b, float2 p1)\n" +"{\n" +" float ra = 1.0 / a.x;\n" +" float rb = 0.5 / b.x;\n" +"\n" +" float d = sqrt (max (b.x * b.x - a.x * p1.x, 0.0));\n" +" float t1 = (b.x - d) * ra;\n" +" float t2 = (b.x + d) * ra;\n" +"\n" +" if (a.x == 0.0)\n" +" t1 = t2 = p1.x * rb;\n" +"\n" +" return float2 ((a.y * t1 - b.y * 2.0) * t1 + p1.y,\n" +" (a.y * t2 - b.y * 2.0) * t2 + p1.y);\n" +"}\n" +"\n" +"float _hb_gpu_calc_coverage (float xcov, float ycov, float xwgt, float ywgt)\n" +"{\n" +" float coverage = max (abs (xcov * xwgt + ycov * ywgt) /\n" +" max (xwgt + ywgt, 1.0 / 65536.0),\n" +" min (abs (xcov), abs (ycov)));\n" +"\n" +" return clamp (coverage, 0.0, 1.0);\n" +"}\n" +"\n" +"/* Decoded glyph band info for a pixel position. */\n" +"struct _hb_gpu_glyph_info\n" +"{\n" +" int glyphLoc;\n" +" int bandBase;\n" +" int2 bandIndex;\n" +" int numHBands;\n" +" int numVBands;\n" +" float2 scale;\n" +"};\n" +"\n" +"_hb_gpu_glyph_info _hb_gpu_decode_glyph (float2 renderCoord, uint glyphLoc_,\n" +" device const short4* hb_gpu_atlas)\n" +"{\n" +" _hb_gpu_glyph_info gi;\n" +" gi.glyphLoc = int (glyphLoc_);\n" +"\n" +" int4 header0 = hb_gpu_fetch (hb_gpu_atlas, gi.glyphLoc);\n" +" int4 header1 = hb_gpu_fetch (hb_gpu_atlas, gi.glyphLoc + 1);\n" +" float4 ext = float4 (header0) * HB_GPU_INV_UNITS;\n" +" gi.numHBands = header1.r;\n" +" gi.numVBands = header1.g;\n" +" gi.scale = float2 (float (header1.b), float (header1.a));\n" +"\n" +" float2 extSize = ext.zw - ext.xy;\n" +" float2 bandScale = float2 (float (gi.numVBands), float (gi.numHBands)) / max (extSize, float2 (1.0 / 65536.0));\n" +" float2 bandOffset = -ext.xy * bandScale;\n" +"\n" +" gi.bandIndex = clamp (int2 (renderCoord * bandScale + bandOffset),\n" +" int2 (0, 0),\n" +" int2 (gi.numVBands - 1, gi.numHBands - 1));\n" +"\n" +" gi.bandBase = gi.glyphLoc + 2;\n" +" return gi;\n" +"}\n" +"\n" +"/* Return pixels per em at this fragment.\n" +" *\n" +" * renderCoord: em-space sample position\n" +" * glyphLoc: texel offset of glyph blob in atlas\n" +" */\n" +"float hb_gpu_ppem (float2 renderCoord, uint glyphLoc_,\n" +" device const short4* hb_gpu_atlas)\n" +"{\n" +" _hb_gpu_glyph_info gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_, hb_gpu_atlas);\n" +" float2 emsPerPixel = fwidth (renderCoord);\n" +" return min (gi.scale.x, gi.scale.y) /\n" +" max (emsPerPixel.x, emsPerPixel.y);\n" +"}\n" +"\n" +"/* Return per-pixel curve counts: (horizontal, vertical). */\n" +"int2 _hb_gpu_curve_counts (float2 renderCoord, uint glyphLoc_,\n" +" device const short4* hb_gpu_atlas)\n" +"{\n" +" _hb_gpu_glyph_info gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_, hb_gpu_atlas);\n" +" int hCount = hb_gpu_fetch (hb_gpu_atlas, gi.bandBase + gi.bandIndex.y).r;\n" +" int vCount = hb_gpu_fetch (hb_gpu_atlas, gi.bandBase + gi.numHBands + gi.bandIndex.x).r;\n" +" return int2 (hCount, vCount);\n" +"}\n" +"\n" +"/* Single-sample coverage in [0, 1]. */\n" +"float _hb_gpu_slug_single (float2 renderCoord, float2 pixelsPerEm, uint glyphLoc_,\n" +" device const short4* hb_gpu_atlas)\n" +"{\n" +"\n" +" _hb_gpu_glyph_info gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_, hb_gpu_atlas);\n" +" int glyphLoc = gi.glyphLoc;\n" +" int bandBase = gi.bandBase;\n" +" int numHBands = gi.numHBands;\n" +"\n" +" float xcov = 0.0;\n" +" float xwgt = 0.0;\n" +"\n" +" int4 hbandData = hb_gpu_fetch (hb_gpu_atlas, bandBase + gi.bandIndex.y);\n" +" int hCurveCount = hbandData.r;\n" +" /* Symmetric: choose rightward (desc) or leftward (asc) sort */\n" +" float hSplit = float (hbandData.a) * HB_GPU_INV_UNITS;\n" +" bool hLeftRay = (renderCoord.x < hSplit);\n" +" int hDataOffset = (hLeftRay ? hbandData.b : hbandData.g) + 32768;\n" +"\n" +" for (int ci = 0; ci < hCurveCount; ci++)\n" +" {\n" +" int curveOffset = hb_gpu_fetch (hb_gpu_atlas, glyphLoc + hDataOffset + ci).r + 32768;\n" +"\n" +" int4 raw12 = hb_gpu_fetch (hb_gpu_atlas, glyphLoc + curveOffset);\n" +" int4 raw3 = hb_gpu_fetch (hb_gpu_atlas, glyphLoc + curveOffset + 1);\n" +"\n" +" float4 q12 = float4 (raw12) * HB_GPU_INV_UNITS;\n" +" float2 q3 = float2 (raw3.rg) * HB_GPU_INV_UNITS;\n" +"\n" +" float4 p12 = q12 - float4 (renderCoord, renderCoord);\n" +" float2 p3 = q3 - renderCoord;\n" +"\n" +" if (hLeftRay) {\n" +" if (min (min (p12.x, p12.z), p3.x) * pixelsPerEm.x > 0.5) break;\n" +" } else {\n" +" if (max (max (p12.x, p12.z), p3.x) * pixelsPerEm.x < -0.5) break;\n" +" }\n" +"\n" +" uint code = _hb_gpu_calc_root_code (p12.y, p12.w, p3.y);\n" +" if (code != 0U)\n" +" {\n" +" float2 a = q12.xy - q12.zw * 2.0 + q3;\n" +" float2 b = q12.xy - q12.zw;\n" +" float2 r = _hb_gpu_solve_horiz_poly (a, b, p12.xy) * pixelsPerEm.x;\n" +" /* For leftward ray: saturate(0.5 - r) counts coverage from the left */\n" +" float2 cov = hLeftRay ? clamp (float2 (0.5) - r, 0.0, 1.0)\n" +" : clamp (r + float2 (0.5), 0.0, 1.0);\n" +"\n" +" if ((code & 1U) != 0U)\n" +" {\n" +" xcov += cov.x;\n" +" xwgt = max (xwgt, clamp (1.0 - abs (r.x) * 2.0, 0.0, 1.0));\n" +" }\n" +"\n" +" if (code > 1U)\n" +" {\n" +" xcov -= cov.y;\n" +" xwgt = max (xwgt, clamp (1.0 - abs (r.y) * 2.0, 0.0, 1.0));\n" +" }\n" +" }\n" +" }\n" +"\n" +" float ycov = 0.0;\n" +" float ywgt = 0.0;\n" +"\n" +" int4 vbandData = hb_gpu_fetch (hb_gpu_atlas, bandBase + numHBands + gi.bandIndex.x);\n" +" int vCurveCount = vbandData.r;\n" +" float vSplit = float (vbandData.a) * HB_GPU_INV_UNITS;\n" +" bool vLeftRay = (renderCoord.y < vSplit);\n" +" int vDataOffset = (vLeftRay ? vbandData.b : vbandData.g) + 32768;\n" +"\n" +" for (int ci = 0; ci < vCurveCount; ci++)\n" +" {\n" +" int curveOffset = hb_gpu_fetch (hb_gpu_atlas, glyphLoc + vDataOffset + ci).r + 32768;\n" +"\n" +" int4 raw12 = hb_gpu_fetch (hb_gpu_atlas, glyphLoc + curveOffset);\n" +" int4 raw3 = hb_gpu_fetch (hb_gpu_atlas, glyphLoc + curveOffset + 1);\n" +"\n" +" float4 q12 = float4 (raw12) * HB_GPU_INV_UNITS;\n" +" float2 q3 = float2 (raw3.rg) * HB_GPU_INV_UNITS;\n" +"\n" +" float4 p12 = q12 - float4 (renderCoord, renderCoord);\n" +" float2 p3 = q3 - renderCoord;\n" +"\n" +" if (vLeftRay) {\n" +" if (min (min (p12.y, p12.w), p3.y) * pixelsPerEm.y > 0.5) break;\n" +" } else {\n" +" if (max (max (p12.y, p12.w), p3.y) * pixelsPerEm.y < -0.5) break;\n" +" }\n" +"\n" +" uint code = _hb_gpu_calc_root_code (p12.x, p12.z, p3.x);\n" +" if (code != 0U)\n" +" {\n" +" float2 a = q12.xy - q12.zw * 2.0 + q3;\n" +" float2 b = q12.xy - q12.zw;\n" +" float2 r = _hb_gpu_solve_vert_poly (a, b, p12.xy) * pixelsPerEm.y;\n" +" float2 cov = vLeftRay ? clamp (float2 (0.5) - r, 0.0, 1.0)\n" +" : clamp (r + float2 (0.5), 0.0, 1.0);\n" +"\n" +" if ((code & 1U) != 0U)\n" +" {\n" +" ycov -= cov.x;\n" +" ywgt = max (ywgt, clamp (1.0 - abs (r.x) * 2.0, 0.0, 1.0));\n" +" }\n" +"\n" +" if (code > 1U)\n" +" {\n" +" ycov += cov.y;\n" +" ywgt = max (ywgt, clamp (1.0 - abs (r.y) * 2.0, 0.0, 1.0));\n" +" }\n" +" }\n" +" }\n" +"\n" +" return _hb_gpu_calc_coverage (xcov, ycov, xwgt, ywgt);\n" +"}\n" +"\n" +"/* Return coverage in [0, 1].\n" +" *\n" +" * renderCoord: em-space sample position\n" +" * glyphLoc: texel offset of glyph blob in atlas\n" +" * hb_gpu_atlas: device pointer to the atlas buffer\n" +" */\n" +"/* The MSAA-aware implementation. Caller supplies pixelsPerEm so\n" +" * this function can be invoked from non-uniform control flow (for\n" +" * example from a paint op-stream branch where a recomputed fwidth\n" +" * would be rejected by strict derivative-uniformity rules). */\n" +"float _hb_gpu_slug (float2 renderCoord, float2 pixelsPerEm, uint glyphLoc_,\n" +" device const short4* hb_gpu_atlas)\n" +"{\n" +" float c = _hb_gpu_slug_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" +"\n" +" if (ppem < 16.0)\n" +" {\n" +" float2 emsPerPixel = 1.0 / pixelsPerEm;\n" +" float2 d = emsPerPixel * (1.0 / 3.0);\n" +" float msaa = 0.25 *\n" +" (_hb_gpu_slug_single (renderCoord + float2 (-d.x, -d.y), pixelsPerEm, glyphLoc_, hb_gpu_atlas) +\n" +" _hb_gpu_slug_single (renderCoord + float2 ( d.x, -d.y), pixelsPerEm, glyphLoc_, hb_gpu_atlas) +\n" +" _hb_gpu_slug_single (renderCoord + float2 (-d.x, d.y), pixelsPerEm, glyphLoc_, hb_gpu_atlas) +\n" +" _hb_gpu_slug_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" +"#endif\n" +"\n" +" return c;\n" +"/* Stem darkening for small sizes.\n" +" *\n" +" * coverage: output of hb_gpu_draw\n" +" * brightness: foreground brightness in [0, 1]\n" +" * ppem: pixels per em at this fragment\n" +" */\n" +"float hb_gpu_stem_darken (float coverage, float brightness, float ppem)\n" +"{\n" +" return pow (coverage,\n" +" mix (pow (2.0, brightness - 0.5), 1.0,\n" +" smoothstep (8.0, 48.0, ppem)));\n" +"}\n" ; diff --git a/src/hb-gpu-fragment-wgsl.hh b/src/hb-gpu-fragment-wgsl.hh index c07735a75..e77698e5e 100644 --- a/src/hb-gpu-fragment-wgsl.hh +++ b/src/hb-gpu-fragment-wgsl.hh @@ -39,4 +39,293 @@ static const char *hb_gpu_fragment_wgsl = "{\n" " return (*hb_gpu_atlas)[offset];\n" "}\n" +"fn _hb_gpu_calc_root_code (y1: f32, y2: f32, y3: f32) -> u32\n" +"{\n" +" let i1 = bitcast (y1) >> 31u;\n" +" let i2 = bitcast (y2) >> 30u;\n" +" let i3 = bitcast (y3) >> 29u;\n" +"\n" +" var shift = (i2 & 2u) | (i1 & ~2u);\n" +" shift = (i3 & 4u) | (shift & ~4u);\n" +"\n" +" return (0x2E74u >> shift) & 0x0101u;\n" +"}\n" +"\n" +"fn _hb_gpu_solve_horiz_poly (a: vec2f, b: vec2f, p1: vec2f) -> vec2f\n" +"{\n" +" let ra = 1.0 / a.y;\n" +" let rb = 0.5 / b.y;\n" +"\n" +" let d = sqrt (max (b.y * b.y - a.y * p1.y, 0.0));\n" +" var t1 = (b.y - d) * ra;\n" +" var t2 = (b.y + d) * ra;\n" +"\n" +" if (a.y == 0.0) {\n" +" t1 = p1.y * rb;\n" +" t2 = t1;\n" +" }\n" +"\n" +" return vec2f ((a.x * t1 - b.x * 2.0) * t1 + p1.x,\n" +" (a.x * t2 - b.x * 2.0) * t2 + p1.x);\n" +"}\n" +"\n" +"fn _hb_gpu_solve_vert_poly (a: vec2f, b: vec2f, p1: vec2f) -> vec2f\n" +"{\n" +" let ra = 1.0 / a.x;\n" +" let rb = 0.5 / b.x;\n" +"\n" +" let d = sqrt (max (b.x * b.x - a.x * p1.x, 0.0));\n" +" var t1 = (b.x - d) * ra;\n" +" var t2 = (b.x + d) * ra;\n" +"\n" +" if (a.x == 0.0) {\n" +" t1 = p1.x * rb;\n" +" t2 = t1;\n" +" }\n" +"\n" +" return vec2f ((a.y * t1 - b.y * 2.0) * t1 + p1.y,\n" +" (a.y * t2 - b.y * 2.0) * t2 + p1.y);\n" +"}\n" +"\n" +"fn _hb_gpu_calc_coverage (xcov: f32, ycov: f32, xwgt: f32, ywgt: f32) -> f32\n" +"{\n" +" let coverage = max (abs (xcov * xwgt + ycov * ywgt) /\n" +" max (xwgt + ywgt, 1.0 / 65536.0),\n" +" min (abs (xcov), abs (ycov)));\n" +"\n" +" return clamp (coverage, 0.0, 1.0);\n" +"}\n" +"\n" +"/* Decoded glyph band info for a pixel position. */\n" +"struct _hb_gpu_glyph_info\n" +"{\n" +" glyphLoc: i32,\n" +" bandBase: i32,\n" +" bandIndex: vec2,\n" +" numHBands: i32,\n" +" numVBands: i32,\n" +" scale: vec2f,\n" +"}\n" +"\n" +"fn _hb_gpu_decode_glyph (renderCoord: vec2f, glyphLoc_: u32,\n" +" hb_gpu_atlas: ptr>, read>) -> _hb_gpu_glyph_info\n" +"{\n" +" var gi: _hb_gpu_glyph_info;\n" +" gi.glyphLoc = i32 (glyphLoc_);\n" +"\n" +" let header0 = hb_gpu_fetch (hb_gpu_atlas, gi.glyphLoc);\n" +" let header1 = hb_gpu_fetch (hb_gpu_atlas, gi.glyphLoc + 1);\n" +" let ext = vec4f (header0) * HB_GPU_INV_UNITS;\n" +" gi.numHBands = header1.r;\n" +" gi.numVBands = header1.g;\n" +" gi.scale = vec2f (f32 (header1.b), f32 (header1.a));\n" +"\n" +" let extSize = ext.zw - ext.xy;\n" +" let bandScale = vec2f (f32 (gi.numVBands), f32 (gi.numHBands)) / max (extSize, vec2f (1.0 / 65536.0));\n" +" let bandOffset = -ext.xy * bandScale;\n" +"\n" +" gi.bandIndex = clamp (vec2 (renderCoord * bandScale + bandOffset),\n" +" vec2 (0, 0),\n" +" vec2 (gi.numVBands - 1, gi.numHBands - 1));\n" +"\n" +" gi.bandBase = gi.glyphLoc + 2;\n" +" return gi;\n" +"}\n" +"\n" +"/* Return pixels per em at this fragment.\n" +" *\n" +" * renderCoord: em-space sample position\n" +" * glyphLoc: texel offset of glyph blob in atlas\n" +" */\n" +"fn hb_gpu_ppem (renderCoord: vec2f, glyphLoc_: u32,\n" +" hb_gpu_atlas: ptr>, read>) -> f32\n" +"{\n" +" let gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_, hb_gpu_atlas);\n" +" let emsPerPixel = fwidth (renderCoord);\n" +" return min (gi.scale.x, gi.scale.y) /\n" +" max (emsPerPixel.x, emsPerPixel.y);\n" +"}\n" +"\n" +"/* Return per-pixel curve counts: (horizontal, vertical). */\n" +"fn _hb_gpu_curve_counts (renderCoord: vec2f, glyphLoc_: u32,\n" +" hb_gpu_atlas: ptr>, read>) -> vec2\n" +"{\n" +" let gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_, hb_gpu_atlas);\n" +" let hCount = hb_gpu_fetch (hb_gpu_atlas, gi.bandBase + gi.bandIndex.y).r;\n" +" let vCount = hb_gpu_fetch (hb_gpu_atlas, gi.bandBase + gi.numHBands + gi.bandIndex.x).r;\n" +" return vec2 (hCount, vCount);\n" +"}\n" +"\n" +"/* Single-sample coverage in [0, 1]. */\n" +"fn _hb_gpu_slug_single (renderCoord: vec2f, pixelsPerEm: vec2f, glyphLoc_: u32,\n" +" hb_gpu_atlas: ptr>, read>) -> f32\n" +"{\n" +"\n" +" let gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_, hb_gpu_atlas);\n" +" let glyphLoc = gi.glyphLoc;\n" +" let bandBase = gi.bandBase;\n" +" let numHBands = gi.numHBands;\n" +"\n" +" var xcov: f32 = 0.0;\n" +" var xwgt: f32 = 0.0;\n" +"\n" +" let hbandData = hb_gpu_fetch (hb_gpu_atlas, bandBase + gi.bandIndex.y);\n" +" let hCurveCount = hbandData.r;\n" +" /* Symmetric: choose rightward (desc) or leftward (asc) sort */\n" +" let hSplit = f32 (hbandData.a) * HB_GPU_INV_UNITS;\n" +" let hLeftRay = (renderCoord.x < hSplit);\n" +" var hDataOffset: i32;\n" +" if (hLeftRay) { hDataOffset = hbandData.b + 32768; }\n" +" else { hDataOffset = hbandData.g + 32768; }\n" +"\n" +" for (var ci: i32 = 0; ci < hCurveCount; ci++)\n" +" {\n" +" let curveOffset = hb_gpu_fetch (hb_gpu_atlas, glyphLoc + hDataOffset + ci).r + 32768;\n" +"\n" +" let raw12 = hb_gpu_fetch (hb_gpu_atlas, glyphLoc + curveOffset);\n" +" let raw3 = hb_gpu_fetch (hb_gpu_atlas, glyphLoc + curveOffset + 1);\n" +"\n" +" let q12 = vec4f (raw12) * HB_GPU_INV_UNITS;\n" +" let q3 = vec2f (vec2 (raw3.r, raw3.g)) * HB_GPU_INV_UNITS;\n" +"\n" +" let p12 = q12 - vec4f (renderCoord, renderCoord);\n" +" let p3 = q3 - renderCoord;\n" +"\n" +" if (hLeftRay) {\n" +" if (min (min (p12.x, p12.z), p3.x) * pixelsPerEm.x > 0.5) { break; }\n" +" } else {\n" +" if (max (max (p12.x, p12.z), p3.x) * pixelsPerEm.x < -0.5) { break; }\n" +" }\n" +"\n" +" let code = _hb_gpu_calc_root_code (p12.y, p12.w, p3.y);\n" +" if (code != 0u)\n" +" {\n" +" let a = q12.xy - q12.zw * 2.0 + q3;\n" +" let b = q12.xy - q12.zw;\n" +" let r = _hb_gpu_solve_horiz_poly (a, b, p12.xy) * pixelsPerEm.x;\n" +" /* For leftward ray: saturate(0.5 - r) counts coverage from the left */\n" +" var cov: vec2f;\n" +" if (hLeftRay) { cov = clamp (vec2f (0.5) - r, vec2f (0.0), vec2f (1.0)); }\n" +" else { cov = clamp (r + vec2f (0.5), vec2f (0.0), vec2f (1.0)); }\n" +"\n" +" if ((code & 1u) != 0u)\n" +" {\n" +" xcov += cov.x;\n" +" xwgt = max (xwgt, clamp (1.0 - abs (r.x) * 2.0, 0.0, 1.0));\n" +" }\n" +"\n" +" if (code > 1u)\n" +" {\n" +" xcov -= cov.y;\n" +" xwgt = max (xwgt, clamp (1.0 - abs (r.y) * 2.0, 0.0, 1.0));\n" +" }\n" +" }\n" +" }\n" +"\n" +" var ycov: f32 = 0.0;\n" +" var ywgt: f32 = 0.0;\n" +"\n" +" let vbandData = hb_gpu_fetch (hb_gpu_atlas, bandBase + numHBands + gi.bandIndex.x);\n" +" let vCurveCount = vbandData.r;\n" +" let vSplit = f32 (vbandData.a) * HB_GPU_INV_UNITS;\n" +" let vLeftRay = (renderCoord.y < vSplit);\n" +" var vDataOffset: i32;\n" +" if (vLeftRay) { vDataOffset = vbandData.b + 32768; }\n" +" else { vDataOffset = vbandData.g + 32768; }\n" +"\n" +" for (var ci: i32 = 0; ci < vCurveCount; ci++)\n" +" {\n" +" let curveOffset = hb_gpu_fetch (hb_gpu_atlas, glyphLoc + vDataOffset + ci).r + 32768;\n" +"\n" +" let raw12 = hb_gpu_fetch (hb_gpu_atlas, glyphLoc + curveOffset);\n" +" let raw3 = hb_gpu_fetch (hb_gpu_atlas, glyphLoc + curveOffset + 1);\n" +"\n" +" let q12 = vec4f (raw12) * HB_GPU_INV_UNITS;\n" +" let q3 = vec2f (vec2 (raw3.r, raw3.g)) * HB_GPU_INV_UNITS;\n" +"\n" +" let p12 = q12 - vec4f (renderCoord, renderCoord);\n" +" let p3 = q3 - renderCoord;\n" +"\n" +" if (vLeftRay) {\n" +" if (min (min (p12.y, p12.w), p3.y) * pixelsPerEm.y > 0.5) { break; }\n" +" } else {\n" +" if (max (max (p12.y, p12.w), p3.y) * pixelsPerEm.y < -0.5) { break; }\n" +" }\n" +"\n" +" let code = _hb_gpu_calc_root_code (p12.x, p12.z, p3.x);\n" +" if (code != 0u)\n" +" {\n" +" let a = q12.xy - q12.zw * 2.0 + q3;\n" +" let b = q12.xy - q12.zw;\n" +" let r = _hb_gpu_solve_vert_poly (a, b, p12.xy) * pixelsPerEm.y;\n" +" var cov: vec2f;\n" +" if (vLeftRay) { cov = clamp (vec2f (0.5) - r, vec2f (0.0), vec2f (1.0)); }\n" +" else { cov = clamp (r + vec2f (0.5), vec2f (0.0), vec2f (1.0)); }\n" +"\n" +" if ((code & 1u) != 0u)\n" +" {\n" +" ycov -= cov.x;\n" +" ywgt = max (ywgt, clamp (1.0 - abs (r.x) * 2.0, 0.0, 1.0));\n" +" }\n" +"\n" +" if (code > 1u)\n" +" {\n" +" ycov += cov.y;\n" +" ywgt = max (ywgt, clamp (1.0 - abs (r.y) * 2.0, 0.0, 1.0));\n" +" }\n" +" }\n" +" }\n" +"\n" +" return _hb_gpu_calc_coverage (xcov, ycov, xwgt, ywgt);\n" +"}\n" +"\n" +"/* Return coverage in [0, 1].\n" +" *\n" +" * renderCoord: em-space sample position\n" +" * glyphLoc: texel offset of glyph blob in atlas\n" +" * hb_gpu_atlas: storage buffer pointer to the atlas\n" +" */\n" +"/* The MSAA-aware implementation. Caller supplies pixelsPerEm so\n" +" * this function can be invoked from non-uniform control flow (for\n" +" * example inside an op-stream branch in hb-gpu-paint-fragment.wgsl,\n" +" * where WGSL would otherwise reject an fwidth call). */\n" +"fn _hb_gpu_slug (renderCoord: vec2f, pixelsPerEm: vec2f, glyphLoc_: u32,\n" +" hb_gpu_atlas: ptr>, read>) -> f32\n" +"{\n" +" var c = _hb_gpu_slug_single (renderCoord, pixelsPerEm, glyphLoc_, hb_gpu_atlas);\n" +"\n" +" /* Inline ppem from pixelsPerEm so we don't re-enter hb_gpu_ppem\n" +" * (which would call fwidth again -- rejected by WGSL when this\n" +" * function is invoked from non-uniform control flow). */\n" +" let gi_pp = _hb_gpu_decode_glyph (renderCoord, glyphLoc_, hb_gpu_atlas);\n" +" let ppem = min (gi_pp.scale.x, gi_pp.scale.y) *\n" +" min (pixelsPerEm.x, pixelsPerEm.y);\n" +"\n" +" if (ppem < 16.0)\n" +" {\n" +" let emsPerPixel = 1.0 / pixelsPerEm;\n" +" let d = emsPerPixel * (1.0 / 3.0);\n" +" let msaa = 0.25 *\n" +" (_hb_gpu_slug_single (renderCoord + vec2f (-d.x, -d.y), pixelsPerEm, glyphLoc_, hb_gpu_atlas) +\n" +" _hb_gpu_slug_single (renderCoord + vec2f ( d.x, -d.y), pixelsPerEm, glyphLoc_, hb_gpu_atlas) +\n" +" _hb_gpu_slug_single (renderCoord + vec2f (-d.x, d.y), pixelsPerEm, glyphLoc_, hb_gpu_atlas) +\n" +" _hb_gpu_slug_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" +"\n" +" return c;\n" +"}\n" +"/* Stem darkening for small sizes.\n" +" *\n" +" * coverage: output of hb_gpu_draw\n" +" * brightness: foreground brightness in [0, 1]\n" +" * ppem: pixels per em at this fragment\n" +" */\n" +"fn hb_gpu_stem_darken (coverage: f32, brightness: f32, ppem: f32) -> f32\n" +"{\n" +" return pow (coverage,\n" +" mix (pow (2.0, brightness - 0.5), 1.0,\n" +" smoothstep (8.0, 48.0, ppem)));\n" +"}\n" ; diff --git a/src/hb-gpu-fragment.glsl b/src/hb-gpu-fragment.glsl index 799a43a51..23b14a0ba 100644 --- a/src/hb-gpu-fragment.glsl +++ b/src/hb-gpu-fragment.glsl @@ -1,5 +1,9 @@ /* * Copyright (C) 2026 Behdad Esfahbod + * Copyright (C) 2017 Eric Lengyel + * + * The _hb_gpu_slug body below is based on the Slug algorithm by + * Eric Lengyel: https://github.com/EricLengyel/Slug * * This is part of HarfBuzz, a text shaping library. * @@ -31,6 +35,18 @@ * including this source. The atlas is then a 2D isampler2D * texture of known width (set via hb_gpu_atlas_width uniform) * instead of an isamplerBuffer. + * + * Exports: + * hb_gpu_fetch (offset) atlas lookup + * _hb_gpu_slug (rc, pixelsPerEm, glyphLoc) MSAA-aware Slug + * coverage (caller supplies + * pixelsPerEm so it can be + * invoked from non-uniform + * control flow) + * hb_gpu_ppem (rc, glyphLoc) pixels per em at fragment + * _hb_gpu_curve_counts (rc, glyphLoc) debug: per-pixel curve counts + * hb_gpu_stem_darken (cov, brightness, ppem) thin-stroke contrast + * correction */ @@ -57,3 +73,267 @@ ivec4 hb_gpu_fetch (int offset) return texelFetch (hb_gpu_atlas, offset); } #endif + + +uint _hb_gpu_calc_root_code (float y1, float y2, float y3) +{ + uint i1 = floatBitsToUint (y1) >> 31U; + uint i2 = floatBitsToUint (y2) >> 30U; + uint i3 = floatBitsToUint (y3) >> 29U; + + uint shift = (i2 & 2U) | (i1 & ~2U); + shift = (i3 & 4U) | (shift & ~4U); + + return (0x2E74U >> shift) & 0x0101U; +} + +vec2 _hb_gpu_solve_horiz_poly (vec2 a, vec2 b, vec2 p1) +{ + float ra = 1.0 / a.y; + float rb = 0.5 / b.y; + + float d = sqrt (max (b.y * b.y - a.y * p1.y, 0.0)); + float t1 = (b.y - d) * ra; + float t2 = (b.y + d) * ra; + + if (a.y == 0.0) + t1 = t2 = p1.y * rb; + + return vec2 ((a.x * t1 - b.x * 2.0) * t1 + p1.x, + (a.x * t2 - b.x * 2.0) * t2 + p1.x); +} + +vec2 _hb_gpu_solve_vert_poly (vec2 a, vec2 b, vec2 p1) +{ + float ra = 1.0 / a.x; + float rb = 0.5 / b.x; + + float d = sqrt (max (b.x * b.x - a.x * p1.x, 0.0)); + float t1 = (b.x - d) * ra; + float t2 = (b.x + d) * ra; + + if (a.x == 0.0) + t1 = t2 = p1.x * rb; + + return vec2 ((a.y * t1 - b.y * 2.0) * t1 + p1.y, + (a.y * t2 - b.y * 2.0) * t2 + p1.y); +} + +float _hb_gpu_calc_coverage (float xcov, float ycov, float xwgt, float ywgt) +{ + float coverage = max (abs (xcov * xwgt + ycov * ywgt) / + max (xwgt + ywgt, 1.0 / 65536.0), + min (abs (xcov), abs (ycov))); + + return clamp (coverage, 0.0, 1.0); +} + +/* Decoded glyph band info for a pixel position. */ +struct _hb_gpu_glyph_info +{ + int glyphLoc; + int bandBase; + ivec2 bandIndex; + int numHBands; + int numVBands; + vec2 scale; +}; + +_hb_gpu_glyph_info _hb_gpu_decode_glyph (vec2 renderCoord, uint glyphLoc_) +{ + _hb_gpu_glyph_info gi; + gi.glyphLoc = int (glyphLoc_); + + ivec4 header0 = hb_gpu_fetch (gi.glyphLoc); + ivec4 header1 = hb_gpu_fetch (gi.glyphLoc + 1); + vec4 ext = vec4 (header0) * HB_GPU_INV_UNITS; + gi.numHBands = header1.r; + gi.numVBands = header1.g; + gi.scale = vec2 (float (header1.b), float (header1.a)); + + vec2 extSize = ext.zw - ext.xy; + vec2 bandScale = vec2 (float (gi.numVBands), float (gi.numHBands)) / max (extSize, vec2 (1.0 / 65536.0)); + vec2 bandOffset = -ext.xy * bandScale; + + gi.bandIndex = clamp (ivec2 (renderCoord * bandScale + bandOffset), + ivec2 (0, 0), + ivec2 (gi.numVBands - 1, gi.numHBands - 1)); + + gi.bandBase = gi.glyphLoc + 2; + return gi; +} + +/* Return pixels per em at this fragment. */ +float hb_gpu_ppem (vec2 renderCoord, uint glyphLoc_) +{ + _hb_gpu_glyph_info gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_); + vec2 emsPerPixel = fwidth (renderCoord); + return min (gi.scale.x, gi.scale.y) / + max (emsPerPixel.x, emsPerPixel.y); +} + +/* Return per-pixel curve counts: (horizontal, vertical). */ +ivec2 _hb_gpu_curve_counts (vec2 renderCoord, uint glyphLoc_) +{ + _hb_gpu_glyph_info gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_); + int hCount = hb_gpu_fetch (gi.bandBase + gi.bandIndex.y).r; + int vCount = hb_gpu_fetch (gi.bandBase + gi.numHBands + gi.bandIndex.x).r; + return ivec2 (hCount, vCount); +} + +/* Single-sample coverage in [0, 1]. */ +float _hb_gpu_slug_single (vec2 renderCoord, vec2 pixelsPerEm, uint glyphLoc_) +{ + + _hb_gpu_glyph_info gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_); + int glyphLoc = gi.glyphLoc; + int bandBase = gi.bandBase; + int numHBands = gi.numHBands; + + float xcov = 0.0; + float xwgt = 0.0; + + ivec4 hbandData = hb_gpu_fetch (bandBase + gi.bandIndex.y); + int hCurveCount = hbandData.r; + /* Symmetric: choose rightward (desc) or leftward (asc) sort */ + float hSplit = float (hbandData.a) * HB_GPU_INV_UNITS; + bool hLeftRay = (renderCoord.x < hSplit); + int hDataOffset = (hLeftRay ? hbandData.b : hbandData.g) + 32768; + + for (int ci = 0; ci < hCurveCount; ci++) + { + int curveOffset = hb_gpu_fetch (glyphLoc + hDataOffset + ci).r + 32768; + + ivec4 raw12 = hb_gpu_fetch (glyphLoc + curveOffset); + ivec4 raw3 = hb_gpu_fetch (glyphLoc + curveOffset + 1); + + vec4 q12 = vec4 (raw12) * HB_GPU_INV_UNITS; + vec2 q3 = vec2 (raw3.rg) * HB_GPU_INV_UNITS; + + vec4 p12 = q12 - vec4 (renderCoord, renderCoord); + vec2 p3 = q3 - renderCoord; + + if (hLeftRay) { + if (min (min (p12.x, p12.z), p3.x) * pixelsPerEm.x > 0.5) break; + } else { + if (max (max (p12.x, p12.z), p3.x) * pixelsPerEm.x < -0.5) break; + } + + uint code = _hb_gpu_calc_root_code (p12.y, p12.w, p3.y); + if (code != 0U) + { + vec2 a = q12.xy - q12.zw * 2.0 + q3; + vec2 b = q12.xy - q12.zw; + vec2 r = _hb_gpu_solve_horiz_poly (a, b, p12.xy) * pixelsPerEm.x; + /* For leftward ray: saturate(0.5 - r) counts coverage from the left */ + vec2 cov = hLeftRay ? clamp (vec2 (0.5) - r, 0.0, 1.0) + : clamp (r + vec2 (0.5), 0.0, 1.0); + + if ((code & 1U) != 0U) + { + xcov += cov.x; + xwgt = max (xwgt, clamp (1.0 - abs (r.x) * 2.0, 0.0, 1.0)); + } + + if (code > 1U) + { + xcov -= cov.y; + xwgt = max (xwgt, clamp (1.0 - abs (r.y) * 2.0, 0.0, 1.0)); + } + } + } + + float ycov = 0.0; + float ywgt = 0.0; + + ivec4 vbandData = hb_gpu_fetch (bandBase + numHBands + gi.bandIndex.x); + int vCurveCount = vbandData.r; + float vSplit = float (vbandData.a) * HB_GPU_INV_UNITS; + bool vLeftRay = (renderCoord.y < vSplit); + int vDataOffset = (vLeftRay ? vbandData.b : vbandData.g) + 32768; + + for (int ci = 0; ci < vCurveCount; ci++) + { + int curveOffset = hb_gpu_fetch (glyphLoc + vDataOffset + ci).r + 32768; + + ivec4 raw12 = hb_gpu_fetch (glyphLoc + curveOffset); + ivec4 raw3 = hb_gpu_fetch (glyphLoc + curveOffset + 1); + + vec4 q12 = vec4 (raw12) * HB_GPU_INV_UNITS; + vec2 q3 = vec2 (raw3.rg) * HB_GPU_INV_UNITS; + + vec4 p12 = q12 - vec4 (renderCoord, renderCoord); + vec2 p3 = q3 - renderCoord; + + if (vLeftRay) { + if (min (min (p12.y, p12.w), p3.y) * pixelsPerEm.y > 0.5) break; + } else { + if (max (max (p12.y, p12.w), p3.y) * pixelsPerEm.y < -0.5) break; + } + + uint code = _hb_gpu_calc_root_code (p12.x, p12.z, p3.x); + if (code != 0U) + { + vec2 a = q12.xy - q12.zw * 2.0 + q3; + vec2 b = q12.xy - q12.zw; + vec2 r = _hb_gpu_solve_vert_poly (a, b, p12.xy) * pixelsPerEm.y; + vec2 cov = vLeftRay ? clamp (vec2 (0.5) - r, 0.0, 1.0) + : clamp (r + vec2 (0.5), 0.0, 1.0); + + if ((code & 1U) != 0U) + { + ycov -= cov.x; + ywgt = max (ywgt, clamp (1.0 - abs (r.x) * 2.0, 0.0, 1.0)); + } + + if (code > 1U) + { + ycov += cov.y; + ywgt = max (ywgt, clamp (1.0 - abs (r.y) * 2.0, 0.0, 1.0)); + } + } + } + + return _hb_gpu_calc_coverage (xcov, ycov, xwgt, ywgt); +} + +/* MSAA-aware Slug coverage. Caller supplies pixelsPerEm so this + * function can be invoked from non-uniform control flow (for + * example inside an op-stream branch in hb-gpu-paint-fragment.wgsl, + * where WGSL would otherwise reject an fwidth call). */ +float _hb_gpu_slug (vec2 renderCoord, vec2 pixelsPerEm, uint glyphLoc_) +{ + float c = _hb_gpu_slug_single (renderCoord, pixelsPerEm, glyphLoc_); + +#ifndef HB_GPU_NO_MSAA + float ppem = hb_gpu_ppem (renderCoord, glyphLoc_); + + if (ppem < 16.0) + { + vec2 emsPerPixel = 1.0 / pixelsPerEm; + vec2 d = emsPerPixel * (1.0 / 3.0); + float msaa = 0.25 * + (_hb_gpu_slug_single (renderCoord + vec2 (-d.x, -d.y), pixelsPerEm, glyphLoc_) + + _hb_gpu_slug_single (renderCoord + vec2 ( d.x, -d.y), pixelsPerEm, glyphLoc_) + + _hb_gpu_slug_single (renderCoord + vec2 (-d.x, d.y), pixelsPerEm, glyphLoc_) + + _hb_gpu_slug_single (renderCoord + vec2 ( d.x, d.y), pixelsPerEm, glyphLoc_)); + + c = mix (c, msaa, smoothstep (16.0, 8.0, ppem)); + } +#endif + + return c; +} + +/* Stem darkening for small sizes. + * + * coverage: output of hb_gpu_draw / _hb_gpu_slug + * brightness: foreground brightness in [0, 1] + * ppem: pixels per em at this fragment + */ +float hb_gpu_stem_darken (float coverage, float brightness, float ppem) +{ + return pow (coverage, + mix (pow (2.0, brightness - 0.5), 1.0, + smoothstep (8.0, 48.0, ppem))); +} diff --git a/src/hb-gpu-fragment.hlsl b/src/hb-gpu-fragment.hlsl index c1da583de..a0c69d6cd 100644 --- a/src/hb-gpu-fragment.hlsl +++ b/src/hb-gpu-fragment.hlsl @@ -43,3 +43,272 @@ int4 hb_gpu_fetch (int offset) { return hb_gpu_atlas[offset]; } +uint _hb_gpu_calc_root_code (float y1, float y2, float y3) +{ + uint i1 = asuint (y1) >> 31u; + uint i2 = asuint (y2) >> 30u; + uint i3 = asuint (y3) >> 29u; + + uint shift = (i2 & 2u) | (i1 & ~2u); + shift = (i3 & 4u) | (shift & ~4u); + + return (0x2E74u >> shift) & 0x0101u; +} + +float2 _hb_gpu_solve_horiz_poly (float2 a, float2 b, float2 p1) +{ + float ra = 1.0 / a.y; + float rb = 0.5 / b.y; + + float d = sqrt (max (b.y * b.y - a.y * p1.y, 0.0)); + float t1 = (b.y - d) * ra; + float t2 = (b.y + d) * ra; + + if (a.y == 0.0) + t1 = t2 = p1.y * rb; + + return float2 ((a.x * t1 - b.x * 2.0) * t1 + p1.x, + (a.x * t2 - b.x * 2.0) * t2 + p1.x); +} + +float2 _hb_gpu_solve_vert_poly (float2 a, float2 b, float2 p1) +{ + float ra = 1.0 / a.x; + float rb = 0.5 / b.x; + + float d = sqrt (max (b.x * b.x - a.x * p1.x, 0.0)); + float t1 = (b.x - d) * ra; + float t2 = (b.x + d) * ra; + + if (a.x == 0.0) + t1 = t2 = p1.x * rb; + + return float2 ((a.y * t1 - b.y * 2.0) * t1 + p1.y, + (a.y * t2 - b.y * 2.0) * t2 + p1.y); +} + +float _hb_gpu_calc_coverage (float xcov, float ycov, float xwgt, float ywgt) +{ + float coverage = max (abs (xcov * xwgt + ycov * ywgt) / + max (xwgt + ywgt, 1.0 / 65536.0), + min (abs (xcov), abs (ycov))); + + return clamp (coverage, 0.0, 1.0); +} + + +struct _hb_gpu_glyph_info +{ + int glyphLoc; + int bandBase; + int2 bandIndex; + int numHBands; + int numVBands; + float2 scale; +}; + +_hb_gpu_glyph_info _hb_gpu_decode_glyph (float2 renderCoord, uint glyphLoc_) +{ + _hb_gpu_glyph_info gi; + gi.glyphLoc = (int) glyphLoc_; + + int4 header0 = hb_gpu_fetch (gi.glyphLoc); + int4 header1 = hb_gpu_fetch (gi.glyphLoc + 1); + float4 ext = (float4) header0 * HB_GPU_INV_UNITS; + gi.numHBands = header1.r; + gi.numVBands = header1.g; + gi.scale = float2 ((float) header1.b, (float) header1.a); + + float2 extSize = ext.zw - ext.xy; + float2 bandScale = float2 ((float) gi.numVBands, (float) gi.numHBands) / max (extSize, float2 (1.0 / 65536.0, 1.0 / 65536.0)); + float2 bandOffset = -ext.xy * bandScale; + + gi.bandIndex = clamp ((int2) (renderCoord * bandScale + bandOffset), + int2 (0, 0), + int2 (gi.numVBands - 1, gi.numHBands - 1)); + + gi.bandBase = gi.glyphLoc + 2; + return gi; +} + +/* Return pixels per em at this fragment. + * + * renderCoord: em-space sample position + * glyphLoc: texel offset of glyph blob in atlas + */ +float hb_gpu_ppem (float2 renderCoord, uint glyphLoc_) +{ + _hb_gpu_glyph_info gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_); + float2 emsPerPixel = fwidth (renderCoord); + return min (gi.scale.x, gi.scale.y) / + max (emsPerPixel.x, emsPerPixel.y); +} + +int2 _hb_gpu_curve_counts (float2 renderCoord, uint glyphLoc_) +{ + _hb_gpu_glyph_info gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_); + int hCount = hb_gpu_fetch (gi.bandBase + gi.bandIndex.y).r; + int vCount = hb_gpu_fetch (gi.bandBase + gi.numHBands + gi.bandIndex.x).r; + return int2 (hCount, vCount); +} + + +/* Single-sample coverage in [0, 1]. */ +float _hb_gpu_slug_single (float2 renderCoord, float2 pixelsPerEm, uint glyphLoc_) +{ + + _hb_gpu_glyph_info gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_); + int glyphLoc = gi.glyphLoc; + int bandBase = gi.bandBase; + int numHBands = gi.numHBands; + + float xcov = 0.0; + float xwgt = 0.0; + + int4 hbandData = hb_gpu_fetch (bandBase + gi.bandIndex.y); + int hCurveCount = hbandData.r; + float hSplit = (float) hbandData.a * HB_GPU_INV_UNITS; + bool hLeftRay = (renderCoord.x < hSplit); + int hDataOffset = (hLeftRay ? hbandData.b : hbandData.g) + 32768; + + for (int ci = 0; ci < hCurveCount; ci++) + { + int curveOffset = hb_gpu_fetch (glyphLoc + hDataOffset + ci).r + 32768; + + int4 raw12 = hb_gpu_fetch (glyphLoc + curveOffset); + int4 raw3 = hb_gpu_fetch (glyphLoc + curveOffset + 1); + + float4 q12 = (float4) raw12 * HB_GPU_INV_UNITS; + float2 q3 = (float2) raw3.rg * HB_GPU_INV_UNITS; + + float4 p12 = q12 - float4 (renderCoord, renderCoord); + float2 p3 = q3 - renderCoord; + + if (hLeftRay) { + if (min (min (p12.x, p12.z), p3.x) * pixelsPerEm.x > 0.5) break; + } else { + if (max (max (p12.x, p12.z), p3.x) * pixelsPerEm.x < -0.5) break; + } + + uint code = _hb_gpu_calc_root_code (p12.y, p12.w, p3.y); + if (code != 0u) + { + float2 a = q12.xy - q12.zw * 2.0 + q3; + float2 b = q12.xy - q12.zw; + float2 r = _hb_gpu_solve_horiz_poly (a, b, p12.xy) * pixelsPerEm.x; + float2 cov = hLeftRay ? clamp (float2 (0.5, 0.5) - r, 0.0, 1.0) + : clamp (r + float2 (0.5, 0.5), 0.0, 1.0); + + if ((code & 1u) != 0u) + { + xcov += cov.x; + xwgt = max (xwgt, clamp (1.0 - abs (r.x) * 2.0, 0.0, 1.0)); + } + + if (code > 1u) + { + xcov -= cov.y; + xwgt = max (xwgt, clamp (1.0 - abs (r.y) * 2.0, 0.0, 1.0)); + } + } + } + + float ycov = 0.0; + float ywgt = 0.0; + + int4 vbandData = hb_gpu_fetch (bandBase + numHBands + gi.bandIndex.x); + int vCurveCount = vbandData.r; + float vSplit = (float) vbandData.a * HB_GPU_INV_UNITS; + bool vLeftRay = (renderCoord.y < vSplit); + int vDataOffset = (vLeftRay ? vbandData.b : vbandData.g) + 32768; + + for (int ci = 0; ci < vCurveCount; ci++) + { + int curveOffset = hb_gpu_fetch (glyphLoc + vDataOffset + ci).r + 32768; + + int4 raw12 = hb_gpu_fetch (glyphLoc + curveOffset); + int4 raw3 = hb_gpu_fetch (glyphLoc + curveOffset + 1); + + float4 q12 = (float4) raw12 * HB_GPU_INV_UNITS; + float2 q3 = (float2) raw3.rg * HB_GPU_INV_UNITS; + + float4 p12 = q12 - float4 (renderCoord, renderCoord); + float2 p3 = q3 - renderCoord; + + if (vLeftRay) { + if (min (min (p12.y, p12.w), p3.y) * pixelsPerEm.y > 0.5) break; + } else { + if (max (max (p12.y, p12.w), p3.y) * pixelsPerEm.y < -0.5) break; + } + + uint code = _hb_gpu_calc_root_code (p12.x, p12.z, p3.x); + if (code != 0u) + { + float2 a = q12.xy - q12.zw * 2.0 + q3; + float2 b = q12.xy - q12.zw; + float2 r = _hb_gpu_solve_vert_poly (a, b, p12.xy) * pixelsPerEm.y; + float2 cov = vLeftRay ? clamp (float2 (0.5, 0.5) - r, 0.0, 1.0) + : clamp (r + float2 (0.5, 0.5), 0.0, 1.0); + + if ((code & 1u) != 0u) + { + ycov -= cov.x; + ywgt = max (ywgt, clamp (1.0 - abs (r.x) * 2.0, 0.0, 1.0)); + } + + if (code > 1u) + { + ycov += cov.y; + ywgt = max (ywgt, clamp (1.0 - abs (r.y) * 2.0, 0.0, 1.0)); + } + } + } + + return _hb_gpu_calc_coverage (xcov, ycov, xwgt, ywgt); +} + +/* Return coverage in [0, 1]. + * + * Caller must declare: StructuredBuffer hb_gpu_atlas + * + * renderCoord: em-space sample position + * glyphLoc: texel offset of glyph blob in atlas + */ +/* The MSAA-aware implementation. Caller supplies pixelsPerEm so + * this function can be invoked from non-uniform control flow (for + * example from a paint op-stream branch). */ +float _hb_gpu_slug (float2 renderCoord, float2 pixelsPerEm, uint glyphLoc_) +{ + float c = _hb_gpu_slug_single (renderCoord, pixelsPerEm, glyphLoc_); + +#ifndef HB_GPU_NO_MSAA + float ppem = hb_gpu_ppem (renderCoord, glyphLoc_); + + if (ppem < 16.0) + { + float2 emsPerPixel = 1.0 / pixelsPerEm; + float2 d = emsPerPixel * (1.0 / 3.0); + float msaa = 0.25 * + (_hb_gpu_slug_single (renderCoord + float2 (-d.x, -d.y), pixelsPerEm, glyphLoc_) + + _hb_gpu_slug_single (renderCoord + float2 ( d.x, -d.y), pixelsPerEm, glyphLoc_) + + _hb_gpu_slug_single (renderCoord + float2 (-d.x, d.y), pixelsPerEm, glyphLoc_) + + _hb_gpu_slug_single (renderCoord + float2 ( d.x, d.y), pixelsPerEm, glyphLoc_)); + + c = lerp (c, msaa, smoothstep (16.0, 8.0, ppem)); + } +#endif + + return c; +} +/* Stem darkening for small sizes. + * + * coverage: output of hb_gpu_draw + * brightness: foreground brightness in [0, 1] + * ppem: pixels per em at this fragment + */ +float hb_gpu_stem_darken (float coverage, float brightness, float ppem) +{ + return pow (coverage, + lerp (pow (2.0, brightness - 0.5), 1.0, + smoothstep (8.0, 48.0, ppem))); +} diff --git a/src/hb-gpu-fragment.msl b/src/hb-gpu-fragment.msl index daab5ae34..9d3e9869c 100644 --- a/src/hb-gpu-fragment.msl +++ b/src/hb-gpu-fragment.msl @@ -40,3 +40,279 @@ int4 hb_gpu_fetch (device const short4* hb_gpu_atlas, int offset) { return int4 (hb_gpu_atlas[offset]); } + +uint _hb_gpu_calc_root_code (float y1, float y2, float y3) +{ + uint i1 = as_type (y1) >> 31U; + uint i2 = as_type (y2) >> 30U; + uint i3 = as_type (y3) >> 29U; + + uint shift = (i2 & 2U) | (i1 & ~2U); + shift = (i3 & 4U) | (shift & ~4U); + + return (0x2E74U >> shift) & 0x0101U; +} + +float2 _hb_gpu_solve_horiz_poly (float2 a, float2 b, float2 p1) +{ + float ra = 1.0 / a.y; + float rb = 0.5 / b.y; + + float d = sqrt (max (b.y * b.y - a.y * p1.y, 0.0)); + float t1 = (b.y - d) * ra; + float t2 = (b.y + d) * ra; + + if (a.y == 0.0) + t1 = t2 = p1.y * rb; + + return float2 ((a.x * t1 - b.x * 2.0) * t1 + p1.x, + (a.x * t2 - b.x * 2.0) * t2 + p1.x); +} + +float2 _hb_gpu_solve_vert_poly (float2 a, float2 b, float2 p1) +{ + float ra = 1.0 / a.x; + float rb = 0.5 / b.x; + + float d = sqrt (max (b.x * b.x - a.x * p1.x, 0.0)); + float t1 = (b.x - d) * ra; + float t2 = (b.x + d) * ra; + + if (a.x == 0.0) + t1 = t2 = p1.x * rb; + + return float2 ((a.y * t1 - b.y * 2.0) * t1 + p1.y, + (a.y * t2 - b.y * 2.0) * t2 + p1.y); +} + +float _hb_gpu_calc_coverage (float xcov, float ycov, float xwgt, float ywgt) +{ + float coverage = max (abs (xcov * xwgt + ycov * ywgt) / + max (xwgt + ywgt, 1.0 / 65536.0), + min (abs (xcov), abs (ycov))); + + return clamp (coverage, 0.0, 1.0); +} + +/* Decoded glyph band info for a pixel position. */ +struct _hb_gpu_glyph_info +{ + int glyphLoc; + int bandBase; + int2 bandIndex; + int numHBands; + int numVBands; + float2 scale; +}; + +_hb_gpu_glyph_info _hb_gpu_decode_glyph (float2 renderCoord, uint glyphLoc_, + device const short4* hb_gpu_atlas) +{ + _hb_gpu_glyph_info gi; + gi.glyphLoc = int (glyphLoc_); + + int4 header0 = hb_gpu_fetch (hb_gpu_atlas, gi.glyphLoc); + int4 header1 = hb_gpu_fetch (hb_gpu_atlas, gi.glyphLoc + 1); + float4 ext = float4 (header0) * HB_GPU_INV_UNITS; + gi.numHBands = header1.r; + gi.numVBands = header1.g; + gi.scale = float2 (float (header1.b), float (header1.a)); + + float2 extSize = ext.zw - ext.xy; + float2 bandScale = float2 (float (gi.numVBands), float (gi.numHBands)) / max (extSize, float2 (1.0 / 65536.0)); + float2 bandOffset = -ext.xy * bandScale; + + gi.bandIndex = clamp (int2 (renderCoord * bandScale + bandOffset), + int2 (0, 0), + int2 (gi.numVBands - 1, gi.numHBands - 1)); + + gi.bandBase = gi.glyphLoc + 2; + return gi; +} + +/* Return pixels per em at this fragment. + * + * renderCoord: em-space sample position + * glyphLoc: texel offset of glyph blob in atlas + */ +float hb_gpu_ppem (float2 renderCoord, uint glyphLoc_, + device const short4* hb_gpu_atlas) +{ + _hb_gpu_glyph_info gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_, hb_gpu_atlas); + float2 emsPerPixel = fwidth (renderCoord); + return min (gi.scale.x, gi.scale.y) / + max (emsPerPixel.x, emsPerPixel.y); +} + +/* Return per-pixel curve counts: (horizontal, vertical). */ +int2 _hb_gpu_curve_counts (float2 renderCoord, uint glyphLoc_, + device const short4* hb_gpu_atlas) +{ + _hb_gpu_glyph_info gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_, hb_gpu_atlas); + int hCount = hb_gpu_fetch (hb_gpu_atlas, gi.bandBase + gi.bandIndex.y).r; + int vCount = hb_gpu_fetch (hb_gpu_atlas, gi.bandBase + gi.numHBands + gi.bandIndex.x).r; + return int2 (hCount, vCount); +} + +/* Single-sample coverage in [0, 1]. */ +float _hb_gpu_slug_single (float2 renderCoord, float2 pixelsPerEm, uint glyphLoc_, + device const short4* hb_gpu_atlas) +{ + + _hb_gpu_glyph_info gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_, hb_gpu_atlas); + int glyphLoc = gi.glyphLoc; + int bandBase = gi.bandBase; + int numHBands = gi.numHBands; + + float xcov = 0.0; + float xwgt = 0.0; + + int4 hbandData = hb_gpu_fetch (hb_gpu_atlas, bandBase + gi.bandIndex.y); + int hCurveCount = hbandData.r; + /* Symmetric: choose rightward (desc) or leftward (asc) sort */ + float hSplit = float (hbandData.a) * HB_GPU_INV_UNITS; + bool hLeftRay = (renderCoord.x < hSplit); + int hDataOffset = (hLeftRay ? hbandData.b : hbandData.g) + 32768; + + for (int ci = 0; ci < hCurveCount; ci++) + { + int curveOffset = hb_gpu_fetch (hb_gpu_atlas, glyphLoc + hDataOffset + ci).r + 32768; + + int4 raw12 = hb_gpu_fetch (hb_gpu_atlas, glyphLoc + curveOffset); + int4 raw3 = hb_gpu_fetch (hb_gpu_atlas, glyphLoc + curveOffset + 1); + + float4 q12 = float4 (raw12) * HB_GPU_INV_UNITS; + float2 q3 = float2 (raw3.rg) * HB_GPU_INV_UNITS; + + float4 p12 = q12 - float4 (renderCoord, renderCoord); + float2 p3 = q3 - renderCoord; + + if (hLeftRay) { + if (min (min (p12.x, p12.z), p3.x) * pixelsPerEm.x > 0.5) break; + } else { + if (max (max (p12.x, p12.z), p3.x) * pixelsPerEm.x < -0.5) break; + } + + uint code = _hb_gpu_calc_root_code (p12.y, p12.w, p3.y); + if (code != 0U) + { + float2 a = q12.xy - q12.zw * 2.0 + q3; + float2 b = q12.xy - q12.zw; + float2 r = _hb_gpu_solve_horiz_poly (a, b, p12.xy) * pixelsPerEm.x; + /* For leftward ray: saturate(0.5 - r) counts coverage from the left */ + float2 cov = hLeftRay ? clamp (float2 (0.5) - r, 0.0, 1.0) + : clamp (r + float2 (0.5), 0.0, 1.0); + + if ((code & 1U) != 0U) + { + xcov += cov.x; + xwgt = max (xwgt, clamp (1.0 - abs (r.x) * 2.0, 0.0, 1.0)); + } + + if (code > 1U) + { + xcov -= cov.y; + xwgt = max (xwgt, clamp (1.0 - abs (r.y) * 2.0, 0.0, 1.0)); + } + } + } + + float ycov = 0.0; + float ywgt = 0.0; + + int4 vbandData = hb_gpu_fetch (hb_gpu_atlas, bandBase + numHBands + gi.bandIndex.x); + int vCurveCount = vbandData.r; + float vSplit = float (vbandData.a) * HB_GPU_INV_UNITS; + bool vLeftRay = (renderCoord.y < vSplit); + int vDataOffset = (vLeftRay ? vbandData.b : vbandData.g) + 32768; + + for (int ci = 0; ci < vCurveCount; ci++) + { + int curveOffset = hb_gpu_fetch (hb_gpu_atlas, glyphLoc + vDataOffset + ci).r + 32768; + + int4 raw12 = hb_gpu_fetch (hb_gpu_atlas, glyphLoc + curveOffset); + int4 raw3 = hb_gpu_fetch (hb_gpu_atlas, glyphLoc + curveOffset + 1); + + float4 q12 = float4 (raw12) * HB_GPU_INV_UNITS; + float2 q3 = float2 (raw3.rg) * HB_GPU_INV_UNITS; + + float4 p12 = q12 - float4 (renderCoord, renderCoord); + float2 p3 = q3 - renderCoord; + + if (vLeftRay) { + if (min (min (p12.y, p12.w), p3.y) * pixelsPerEm.y > 0.5) break; + } else { + if (max (max (p12.y, p12.w), p3.y) * pixelsPerEm.y < -0.5) break; + } + + uint code = _hb_gpu_calc_root_code (p12.x, p12.z, p3.x); + if (code != 0U) + { + float2 a = q12.xy - q12.zw * 2.0 + q3; + float2 b = q12.xy - q12.zw; + float2 r = _hb_gpu_solve_vert_poly (a, b, p12.xy) * pixelsPerEm.y; + float2 cov = vLeftRay ? clamp (float2 (0.5) - r, 0.0, 1.0) + : clamp (r + float2 (0.5), 0.0, 1.0); + + if ((code & 1U) != 0U) + { + ycov -= cov.x; + ywgt = max (ywgt, clamp (1.0 - abs (r.x) * 2.0, 0.0, 1.0)); + } + + if (code > 1U) + { + ycov += cov.y; + ywgt = max (ywgt, clamp (1.0 - abs (r.y) * 2.0, 0.0, 1.0)); + } + } + } + + return _hb_gpu_calc_coverage (xcov, ycov, xwgt, ywgt); +} + +/* Return coverage in [0, 1]. + * + * renderCoord: em-space sample position + * glyphLoc: texel offset of glyph blob in atlas + * hb_gpu_atlas: device pointer to the atlas buffer + */ +/* The MSAA-aware implementation. Caller supplies pixelsPerEm so + * this function can be invoked from non-uniform control flow (for + * example from a paint op-stream branch where a recomputed fwidth + * would be rejected by strict derivative-uniformity rules). */ +float _hb_gpu_slug (float2 renderCoord, float2 pixelsPerEm, uint glyphLoc_, + device const short4* hb_gpu_atlas) +{ + float c = _hb_gpu_slug_single (renderCoord, pixelsPerEm, glyphLoc_, hb_gpu_atlas); + +#ifndef HB_GPU_NO_MSAA + float ppem = hb_gpu_ppem (renderCoord, glyphLoc_, hb_gpu_atlas); + + if (ppem < 16.0) + { + float2 emsPerPixel = 1.0 / pixelsPerEm; + float2 d = emsPerPixel * (1.0 / 3.0); + float msaa = 0.25 * + (_hb_gpu_slug_single (renderCoord + float2 (-d.x, -d.y), pixelsPerEm, glyphLoc_, hb_gpu_atlas) + + _hb_gpu_slug_single (renderCoord + float2 ( d.x, -d.y), pixelsPerEm, glyphLoc_, hb_gpu_atlas) + + _hb_gpu_slug_single (renderCoord + float2 (-d.x, d.y), pixelsPerEm, glyphLoc_, hb_gpu_atlas) + + _hb_gpu_slug_single (renderCoord + float2 ( d.x, d.y), pixelsPerEm, glyphLoc_, hb_gpu_atlas)); + + c = mix (c, msaa, smoothstep (16.0, 8.0, ppem)); + } +#endif + + return c; +/* Stem darkening for small sizes. + * + * coverage: output of hb_gpu_draw + * brightness: foreground brightness in [0, 1] + * ppem: pixels per em at this fragment + */ +float hb_gpu_stem_darken (float coverage, float brightness, float ppem) +{ + return pow (coverage, + mix (pow (2.0, brightness - 0.5), 1.0, + smoothstep (8.0, 48.0, ppem))); +} diff --git a/src/hb-gpu-fragment.wgsl b/src/hb-gpu-fragment.wgsl index 22dd795bf..0fac0c384 100644 --- a/src/hb-gpu-fragment.wgsl +++ b/src/hb-gpu-fragment.wgsl @@ -38,3 +38,292 @@ fn hb_gpu_fetch (hb_gpu_atlas: ptr>, read>, { return (*hb_gpu_atlas)[offset]; } +fn _hb_gpu_calc_root_code (y1: f32, y2: f32, y3: f32) -> u32 +{ + let i1 = bitcast (y1) >> 31u; + let i2 = bitcast (y2) >> 30u; + let i3 = bitcast (y3) >> 29u; + + var shift = (i2 & 2u) | (i1 & ~2u); + shift = (i3 & 4u) | (shift & ~4u); + + return (0x2E74u >> shift) & 0x0101u; +} + +fn _hb_gpu_solve_horiz_poly (a: vec2f, b: vec2f, p1: vec2f) -> vec2f +{ + let ra = 1.0 / a.y; + let rb = 0.5 / b.y; + + let d = sqrt (max (b.y * b.y - a.y * p1.y, 0.0)); + var t1 = (b.y - d) * ra; + var t2 = (b.y + d) * ra; + + if (a.y == 0.0) { + t1 = p1.y * rb; + t2 = t1; + } + + return vec2f ((a.x * t1 - b.x * 2.0) * t1 + p1.x, + (a.x * t2 - b.x * 2.0) * t2 + p1.x); +} + +fn _hb_gpu_solve_vert_poly (a: vec2f, b: vec2f, p1: vec2f) -> vec2f +{ + let ra = 1.0 / a.x; + let rb = 0.5 / b.x; + + let d = sqrt (max (b.x * b.x - a.x * p1.x, 0.0)); + var t1 = (b.x - d) * ra; + var t2 = (b.x + d) * ra; + + if (a.x == 0.0) { + t1 = p1.x * rb; + t2 = t1; + } + + return vec2f ((a.y * t1 - b.y * 2.0) * t1 + p1.y, + (a.y * t2 - b.y * 2.0) * t2 + p1.y); +} + +fn _hb_gpu_calc_coverage (xcov: f32, ycov: f32, xwgt: f32, ywgt: f32) -> f32 +{ + let coverage = max (abs (xcov * xwgt + ycov * ywgt) / + max (xwgt + ywgt, 1.0 / 65536.0), + min (abs (xcov), abs (ycov))); + + return clamp (coverage, 0.0, 1.0); +} + +/* Decoded glyph band info for a pixel position. */ +struct _hb_gpu_glyph_info +{ + glyphLoc: i32, + bandBase: i32, + bandIndex: vec2, + numHBands: i32, + numVBands: i32, + scale: vec2f, +} + +fn _hb_gpu_decode_glyph (renderCoord: vec2f, glyphLoc_: u32, + hb_gpu_atlas: ptr>, read>) -> _hb_gpu_glyph_info +{ + var gi: _hb_gpu_glyph_info; + gi.glyphLoc = i32 (glyphLoc_); + + let header0 = hb_gpu_fetch (hb_gpu_atlas, gi.glyphLoc); + let header1 = hb_gpu_fetch (hb_gpu_atlas, gi.glyphLoc + 1); + let ext = vec4f (header0) * HB_GPU_INV_UNITS; + gi.numHBands = header1.r; + gi.numVBands = header1.g; + gi.scale = vec2f (f32 (header1.b), f32 (header1.a)); + + let extSize = ext.zw - ext.xy; + let bandScale = vec2f (f32 (gi.numVBands), f32 (gi.numHBands)) / max (extSize, vec2f (1.0 / 65536.0)); + let bandOffset = -ext.xy * bandScale; + + gi.bandIndex = clamp (vec2 (renderCoord * bandScale + bandOffset), + vec2 (0, 0), + vec2 (gi.numVBands - 1, gi.numHBands - 1)); + + gi.bandBase = gi.glyphLoc + 2; + return gi; +} + +/* Return pixels per em at this fragment. + * + * renderCoord: em-space sample position + * glyphLoc: texel offset of glyph blob in atlas + */ +fn hb_gpu_ppem (renderCoord: vec2f, glyphLoc_: u32, + hb_gpu_atlas: ptr>, read>) -> f32 +{ + let gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_, hb_gpu_atlas); + let emsPerPixel = fwidth (renderCoord); + return min (gi.scale.x, gi.scale.y) / + max (emsPerPixel.x, emsPerPixel.y); +} + +/* Return per-pixel curve counts: (horizontal, vertical). */ +fn _hb_gpu_curve_counts (renderCoord: vec2f, glyphLoc_: u32, + hb_gpu_atlas: ptr>, read>) -> vec2 +{ + let gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_, hb_gpu_atlas); + let hCount = hb_gpu_fetch (hb_gpu_atlas, gi.bandBase + gi.bandIndex.y).r; + let vCount = hb_gpu_fetch (hb_gpu_atlas, gi.bandBase + gi.numHBands + gi.bandIndex.x).r; + return vec2 (hCount, vCount); +} + +/* Single-sample coverage in [0, 1]. */ +fn _hb_gpu_slug_single (renderCoord: vec2f, pixelsPerEm: vec2f, glyphLoc_: u32, + hb_gpu_atlas: ptr>, read>) -> f32 +{ + + let gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_, hb_gpu_atlas); + let glyphLoc = gi.glyphLoc; + let bandBase = gi.bandBase; + let numHBands = gi.numHBands; + + var xcov: f32 = 0.0; + var xwgt: f32 = 0.0; + + let hbandData = hb_gpu_fetch (hb_gpu_atlas, bandBase + gi.bandIndex.y); + let hCurveCount = hbandData.r; + /* Symmetric: choose rightward (desc) or leftward (asc) sort */ + let hSplit = f32 (hbandData.a) * HB_GPU_INV_UNITS; + let hLeftRay = (renderCoord.x < hSplit); + var hDataOffset: i32; + if (hLeftRay) { hDataOffset = hbandData.b + 32768; } + else { hDataOffset = hbandData.g + 32768; } + + for (var ci: i32 = 0; ci < hCurveCount; ci++) + { + let curveOffset = hb_gpu_fetch (hb_gpu_atlas, glyphLoc + hDataOffset + ci).r + 32768; + + let raw12 = hb_gpu_fetch (hb_gpu_atlas, glyphLoc + curveOffset); + let raw3 = hb_gpu_fetch (hb_gpu_atlas, glyphLoc + curveOffset + 1); + + let q12 = vec4f (raw12) * HB_GPU_INV_UNITS; + let q3 = vec2f (vec2 (raw3.r, raw3.g)) * HB_GPU_INV_UNITS; + + let p12 = q12 - vec4f (renderCoord, renderCoord); + let p3 = q3 - renderCoord; + + if (hLeftRay) { + if (min (min (p12.x, p12.z), p3.x) * pixelsPerEm.x > 0.5) { break; } + } else { + if (max (max (p12.x, p12.z), p3.x) * pixelsPerEm.x < -0.5) { break; } + } + + let code = _hb_gpu_calc_root_code (p12.y, p12.w, p3.y); + if (code != 0u) + { + let a = q12.xy - q12.zw * 2.0 + q3; + let b = q12.xy - q12.zw; + let r = _hb_gpu_solve_horiz_poly (a, b, p12.xy) * pixelsPerEm.x; + /* For leftward ray: saturate(0.5 - r) counts coverage from the left */ + var cov: vec2f; + if (hLeftRay) { cov = clamp (vec2f (0.5) - r, vec2f (0.0), vec2f (1.0)); } + else { cov = clamp (r + vec2f (0.5), vec2f (0.0), vec2f (1.0)); } + + if ((code & 1u) != 0u) + { + xcov += cov.x; + xwgt = max (xwgt, clamp (1.0 - abs (r.x) * 2.0, 0.0, 1.0)); + } + + if (code > 1u) + { + xcov -= cov.y; + xwgt = max (xwgt, clamp (1.0 - abs (r.y) * 2.0, 0.0, 1.0)); + } + } + } + + var ycov: f32 = 0.0; + var ywgt: f32 = 0.0; + + let vbandData = hb_gpu_fetch (hb_gpu_atlas, bandBase + numHBands + gi.bandIndex.x); + let vCurveCount = vbandData.r; + let vSplit = f32 (vbandData.a) * HB_GPU_INV_UNITS; + let vLeftRay = (renderCoord.y < vSplit); + var vDataOffset: i32; + if (vLeftRay) { vDataOffset = vbandData.b + 32768; } + else { vDataOffset = vbandData.g + 32768; } + + for (var ci: i32 = 0; ci < vCurveCount; ci++) + { + let curveOffset = hb_gpu_fetch (hb_gpu_atlas, glyphLoc + vDataOffset + ci).r + 32768; + + let raw12 = hb_gpu_fetch (hb_gpu_atlas, glyphLoc + curveOffset); + let raw3 = hb_gpu_fetch (hb_gpu_atlas, glyphLoc + curveOffset + 1); + + let q12 = vec4f (raw12) * HB_GPU_INV_UNITS; + let q3 = vec2f (vec2 (raw3.r, raw3.g)) * HB_GPU_INV_UNITS; + + let p12 = q12 - vec4f (renderCoord, renderCoord); + let p3 = q3 - renderCoord; + + if (vLeftRay) { + if (min (min (p12.y, p12.w), p3.y) * pixelsPerEm.y > 0.5) { break; } + } else { + if (max (max (p12.y, p12.w), p3.y) * pixelsPerEm.y < -0.5) { break; } + } + + let code = _hb_gpu_calc_root_code (p12.x, p12.z, p3.x); + if (code != 0u) + { + let a = q12.xy - q12.zw * 2.0 + q3; + let b = q12.xy - q12.zw; + let r = _hb_gpu_solve_vert_poly (a, b, p12.xy) * pixelsPerEm.y; + var cov: vec2f; + if (vLeftRay) { cov = clamp (vec2f (0.5) - r, vec2f (0.0), vec2f (1.0)); } + else { cov = clamp (r + vec2f (0.5), vec2f (0.0), vec2f (1.0)); } + + if ((code & 1u) != 0u) + { + ycov -= cov.x; + ywgt = max (ywgt, clamp (1.0 - abs (r.x) * 2.0, 0.0, 1.0)); + } + + if (code > 1u) + { + ycov += cov.y; + ywgt = max (ywgt, clamp (1.0 - abs (r.y) * 2.0, 0.0, 1.0)); + } + } + } + + return _hb_gpu_calc_coverage (xcov, ycov, xwgt, ywgt); +} + +/* Return coverage in [0, 1]. + * + * renderCoord: em-space sample position + * glyphLoc: texel offset of glyph blob in atlas + * hb_gpu_atlas: storage buffer pointer to the atlas + */ +/* The MSAA-aware implementation. Caller supplies pixelsPerEm so + * this function can be invoked from non-uniform control flow (for + * example inside an op-stream branch in hb-gpu-paint-fragment.wgsl, + * where WGSL would otherwise reject an fwidth call). */ +fn _hb_gpu_slug (renderCoord: vec2f, pixelsPerEm: vec2f, glyphLoc_: u32, + hb_gpu_atlas: ptr>, read>) -> f32 +{ + var c = _hb_gpu_slug_single (renderCoord, pixelsPerEm, glyphLoc_, hb_gpu_atlas); + + /* Inline ppem from pixelsPerEm so we don't re-enter hb_gpu_ppem + * (which would call fwidth again -- rejected by WGSL when this + * function is invoked from non-uniform control flow). */ + let gi_pp = _hb_gpu_decode_glyph (renderCoord, glyphLoc_, hb_gpu_atlas); + let ppem = min (gi_pp.scale.x, gi_pp.scale.y) * + min (pixelsPerEm.x, pixelsPerEm.y); + + if (ppem < 16.0) + { + let emsPerPixel = 1.0 / pixelsPerEm; + let d = emsPerPixel * (1.0 / 3.0); + let msaa = 0.25 * + (_hb_gpu_slug_single (renderCoord + vec2f (-d.x, -d.y), pixelsPerEm, glyphLoc_, hb_gpu_atlas) + + _hb_gpu_slug_single (renderCoord + vec2f ( d.x, -d.y), pixelsPerEm, glyphLoc_, hb_gpu_atlas) + + _hb_gpu_slug_single (renderCoord + vec2f (-d.x, d.y), pixelsPerEm, glyphLoc_, hb_gpu_atlas) + + _hb_gpu_slug_single (renderCoord + vec2f ( d.x, d.y), pixelsPerEm, glyphLoc_, hb_gpu_atlas)); + + c = mix (c, msaa, smoothstep (16.0, 8.0, ppem)); + } + + return c; +} +/* Stem darkening for small sizes. + * + * coverage: output of hb_gpu_draw + * brightness: foreground brightness in [0, 1] + * ppem: pixels per em at this fragment + */ +fn hb_gpu_stem_darken (coverage: f32, brightness: f32, ppem: f32) -> f32 +{ + return pow (coverage, + mix (pow (2.0, brightness - 0.5), 1.0, + smoothstep (8.0, 48.0, ppem))); +} diff --git a/src/hb-gpu-paint-fragment-glsl.hh b/src/hb-gpu-paint-fragment-glsl.hh index b27d7c8a1..a8b95e5bd 100644 --- a/src/hb-gpu-paint-fragment-glsl.hh +++ b/src/hb-gpu-paint-fragment-glsl.hh @@ -229,7 +229,7 @@ static const char *hb_gpu_paint_fragment_glsl = "{\n" " /* fwidth once, at uniform control flow: every per-layer\n" " * coverage sample below uses this pre-computed pixelsPerEm via\n" -" * _hb_gpu_draw_impl. */\n" +" * _hb_gpu_slug. */\n" " vec2 pixelsPerEm = 1.0 / fwidth (renderCoord);\n" "\n" " int base = int (glyphLoc);\n" @@ -257,7 +257,7 @@ static const char *hb_gpu_paint_fragment_glsl = " ? foreground\n" " : vec4 (ct) / 32767.0;\n" "\n" -" float cov = _hb_gpu_draw_impl (renderCoord, pixelsPerEm,\n" +" float cov = _hb_gpu_slug (renderCoord, pixelsPerEm,\n" " uint (base + payload));\n" " vec4 src = vec4 (col.rgb * col.a, col.a) * cov;\n" " acc = src + acc * (1.0 - src.a);\n" @@ -286,7 +286,7 @@ static const char *hb_gpu_paint_fragment_glsl = " base + grad_payload,\n" " stop_count, extend, foreground);\n" "\n" -" float cov = _hb_gpu_draw_impl (renderCoord, pixelsPerEm,\n" +" float cov = _hb_gpu_slug (renderCoord, pixelsPerEm,\n" " uint (base + payload));\n" " vec4 src = vec4 (col.rgb * col.a, col.a) * cov;\n" " acc = src + acc * (1.0 - src.a);\n" diff --git a/src/hb-gpu-paint-fragment-hlsl.hh b/src/hb-gpu-paint-fragment-hlsl.hh index e3f6fbd2b..07218c3e3 100644 --- a/src/hb-gpu-paint-fragment-hlsl.hh +++ b/src/hb-gpu-paint-fragment-hlsl.hh @@ -212,7 +212,7 @@ static const char *hb_gpu_paint_fragment_hlsl = " ? foreground\n" " : (float4) ct / 32767.0;\n" "\n" -" float cov = _hb_gpu_draw_impl (renderCoord, pixelsPerEm,\n" +" float cov = _hb_gpu_slug (renderCoord, pixelsPerEm,\n" " (uint) (base + payload));\n" " float4 src = float4 (col.rgb * col.a, col.a) * cov;\n" " acc = src + acc * (1.0 - src.a);\n" @@ -240,7 +240,7 @@ static const char *hb_gpu_paint_fragment_hlsl = " base + grad_payload,\n" " stop_count, extend, foreground);\n" "\n" -" float cov = _hb_gpu_draw_impl (renderCoord, pixelsPerEm,\n" +" float cov = _hb_gpu_slug (renderCoord, pixelsPerEm,\n" " (uint) (base + payload));\n" " float4 src = float4 (col.rgb * col.a, col.a) * cov;\n" " acc = src + acc * (1.0 - src.a);\n" diff --git a/src/hb-gpu-paint-fragment-msl.hh b/src/hb-gpu-paint-fragment-msl.hh index 66c70ed7c..3e3841d70 100644 --- a/src/hb-gpu-paint-fragment-msl.hh +++ b/src/hb-gpu-paint-fragment-msl.hh @@ -223,7 +223,7 @@ static const char *hb_gpu_paint_fragment_msl = " ? foreground\n" " : float4 (ct) / 32767.0;\n" "\n" -" float cov = _hb_gpu_draw_impl (renderCoord, pixelsPerEm,\n" +" float cov = _hb_gpu_slug (renderCoord, pixelsPerEm,\n" " uint (base + payload), hb_gpu_atlas);\n" " float4 src = float4 (col.rgb * col.a, col.a) * cov;\n" " acc = src + acc * (1.0 - src.a);\n" @@ -254,7 +254,7 @@ static const char *hb_gpu_paint_fragment_msl = " stop_count, extend, foreground,\n" " hb_gpu_atlas);\n" "\n" -" float cov = _hb_gpu_draw_impl (renderCoord, pixelsPerEm,\n" +" float cov = _hb_gpu_slug (renderCoord, pixelsPerEm,\n" " uint (base + payload), hb_gpu_atlas);\n" " float4 src = float4 (col.rgb * col.a, col.a) * cov;\n" " acc = src + acc * (1.0 - src.a);\n" diff --git a/src/hb-gpu-paint-fragment-wgsl.hh b/src/hb-gpu-paint-fragment-wgsl.hh index 62c53c46d..e4217b4a0 100644 --- a/src/hb-gpu-paint-fragment-wgsl.hh +++ b/src/hb-gpu-paint-fragment-wgsl.hh @@ -200,7 +200,7 @@ static const char *hb_gpu_paint_fragment_wgsl = "{\n" " /* Compute pixelsPerEm once here at uniform control flow. WGSL\n" " * rejects fwidth inside a loop-conditional branch, so we call\n" -" * _hb_gpu_draw_impl (the MSAA-aware implementation that takes a\n" +" * _hb_gpu_slug (the MSAA-aware implementation that takes a\n" " * pre-computed pixelsPerEm) instead of the top-level\n" " * hb_gpu_draw() which would re-call fwidth. */\n" " let emsPerPixel = fwidth (renderCoord);\n" @@ -233,7 +233,7 @@ static const char *hb_gpu_paint_fragment_wgsl = " col = vec4f (ct) / 32767.0;\n" " }\n" "\n" -" let cov = _hb_gpu_draw_impl (renderCoord, pixelsPerEm,\n" +" let cov = _hb_gpu_slug (renderCoord, pixelsPerEm,\n" " u32 (base + payload), hb_gpu_atlas);\n" " let src = vec4f (col.rgb * col.a, col.a) * cov;\n" " acc = src + acc * (1.0 - src.a);\n" @@ -263,7 +263,7 @@ static const char *hb_gpu_paint_fragment_wgsl = " hb_gpu_atlas);\n" " }\n" "\n" -" let cov = _hb_gpu_draw_impl (renderCoord, pixelsPerEm,\n" +" let cov = _hb_gpu_slug (renderCoord, pixelsPerEm,\n" " u32 (base + payload), hb_gpu_atlas);\n" " let src = vec4f (col.rgb * col.a, col.a) * cov;\n" " acc = src + acc * (1.0 - src.a);\n" diff --git a/src/hb-gpu-paint-fragment.glsl b/src/hb-gpu-paint-fragment.glsl index 22d538a68..f7ae73b0d 100644 --- a/src/hb-gpu-paint-fragment.glsl +++ b/src/hb-gpu-paint-fragment.glsl @@ -228,7 +228,7 @@ vec4 hb_gpu_paint (vec2 renderCoord, uint glyphLoc, vec4 foreground) { /* fwidth once, at uniform control flow: every per-layer * coverage sample below uses this pre-computed pixelsPerEm via - * _hb_gpu_draw_impl. */ + * _hb_gpu_slug. */ vec2 pixelsPerEm = 1.0 / fwidth (renderCoord); int base = int (glyphLoc); @@ -256,7 +256,7 @@ vec4 hb_gpu_paint (vec2 renderCoord, uint glyphLoc, vec4 foreground) ? foreground : vec4 (ct) / 32767.0; - float cov = _hb_gpu_draw_impl (renderCoord, pixelsPerEm, + float cov = _hb_gpu_slug (renderCoord, pixelsPerEm, uint (base + payload)); vec4 src = vec4 (col.rgb * col.a, col.a) * cov; acc = src + acc * (1.0 - src.a); @@ -285,7 +285,7 @@ vec4 hb_gpu_paint (vec2 renderCoord, uint glyphLoc, vec4 foreground) base + grad_payload, stop_count, extend, foreground); - float cov = _hb_gpu_draw_impl (renderCoord, pixelsPerEm, + float cov = _hb_gpu_slug (renderCoord, pixelsPerEm, uint (base + payload)); vec4 src = vec4 (col.rgb * col.a, col.a) * cov; acc = src + acc * (1.0 - src.a); diff --git a/src/hb-gpu-paint-fragment.hlsl b/src/hb-gpu-paint-fragment.hlsl index 56bc338f9..f68eb2d59 100644 --- a/src/hb-gpu-paint-fragment.hlsl +++ b/src/hb-gpu-paint-fragment.hlsl @@ -211,7 +211,7 @@ float4 hb_gpu_paint (float2 renderCoord, uint glyphLoc_, float4 foreground) ? foreground : (float4) ct / 32767.0; - float cov = _hb_gpu_draw_impl (renderCoord, pixelsPerEm, + float cov = _hb_gpu_slug (renderCoord, pixelsPerEm, (uint) (base + payload)); float4 src = float4 (col.rgb * col.a, col.a) * cov; acc = src + acc * (1.0 - src.a); @@ -239,7 +239,7 @@ float4 hb_gpu_paint (float2 renderCoord, uint glyphLoc_, float4 foreground) base + grad_payload, stop_count, extend, foreground); - float cov = _hb_gpu_draw_impl (renderCoord, pixelsPerEm, + float cov = _hb_gpu_slug (renderCoord, pixelsPerEm, (uint) (base + payload)); float4 src = float4 (col.rgb * col.a, col.a) * cov; acc = src + acc * (1.0 - src.a); diff --git a/src/hb-gpu-paint-fragment.msl b/src/hb-gpu-paint-fragment.msl index 70dd4dfaa..4aec75e25 100644 --- a/src/hb-gpu-paint-fragment.msl +++ b/src/hb-gpu-paint-fragment.msl @@ -222,7 +222,7 @@ float4 hb_gpu_paint (float2 renderCoord, uint glyphLoc_, float4 foreground, ? foreground : float4 (ct) / 32767.0; - float cov = _hb_gpu_draw_impl (renderCoord, pixelsPerEm, + float cov = _hb_gpu_slug (renderCoord, pixelsPerEm, uint (base + payload), hb_gpu_atlas); float4 src = float4 (col.rgb * col.a, col.a) * cov; acc = src + acc * (1.0 - src.a); @@ -253,7 +253,7 @@ float4 hb_gpu_paint (float2 renderCoord, uint glyphLoc_, float4 foreground, stop_count, extend, foreground, hb_gpu_atlas); - float cov = _hb_gpu_draw_impl (renderCoord, pixelsPerEm, + float cov = _hb_gpu_slug (renderCoord, pixelsPerEm, uint (base + payload), hb_gpu_atlas); float4 src = float4 (col.rgb * col.a, col.a) * cov; acc = src + acc * (1.0 - src.a); diff --git a/src/hb-gpu-paint-fragment.wgsl b/src/hb-gpu-paint-fragment.wgsl index 9270de0d9..c5d0d2fd3 100644 --- a/src/hb-gpu-paint-fragment.wgsl +++ b/src/hb-gpu-paint-fragment.wgsl @@ -199,7 +199,7 @@ fn hb_gpu_paint (renderCoord: vec2f, glyphLoc_: u32, foreground: vec4f, { /* Compute pixelsPerEm once here at uniform control flow. WGSL * rejects fwidth inside a loop-conditional branch, so we call - * _hb_gpu_draw_impl (the MSAA-aware implementation that takes a + * _hb_gpu_slug (the MSAA-aware implementation that takes a * pre-computed pixelsPerEm) instead of the top-level * hb_gpu_draw() which would re-call fwidth. */ let emsPerPixel = fwidth (renderCoord); @@ -232,7 +232,7 @@ fn hb_gpu_paint (renderCoord: vec2f, glyphLoc_: u32, foreground: vec4f, col = vec4f (ct) / 32767.0; } - let cov = _hb_gpu_draw_impl (renderCoord, pixelsPerEm, + let cov = _hb_gpu_slug (renderCoord, pixelsPerEm, u32 (base + payload), hb_gpu_atlas); let src = vec4f (col.rgb * col.a, col.a) * cov; acc = src + acc * (1.0 - src.a); @@ -262,7 +262,7 @@ fn hb_gpu_paint (renderCoord: vec2f, glyphLoc_: u32, foreground: vec4f, hb_gpu_atlas); } - let cov = _hb_gpu_draw_impl (renderCoord, pixelsPerEm, + let cov = _hb_gpu_slug (renderCoord, pixelsPerEm, u32 (base + payload), hb_gpu_atlas); let src = vec4f (col.rgb * col.a, col.a) * cov; acc = src + acc * (1.0 - src.a);