diff --git a/src/gen-gpu-shader-artifacts.py b/src/gen-gpu-shader-artifacts.py index bc81b8651..1ba2739f7 100755 --- a/src/gen-gpu-shader-artifacts.py +++ b/src/gen-gpu-shader-artifacts.py @@ -19,6 +19,8 @@ decl_map = { 'hb-gpu-vertex-msl.hh': 'hb_gpu_vertex_msl', 'hb-gpu-fragment-wgsl.hh': 'hb_gpu_fragment_wgsl', 'hb-gpu-vertex-wgsl.hh': 'hb_gpu_vertex_wgsl', + 'hb-gpu-fragment-hlsl.hh': 'hb_gpu_fragment_hlsl', + 'hb-gpu-vertex-hlsl.hh': 'hb_gpu_vertex_hlsl', } decl = decl_map.get(hh) diff --git a/src/hb-gpu-fragment-hlsl.hh b/src/hb-gpu-fragment-hlsl.hh new file mode 100644 index 000000000..e7b960a8a --- /dev/null +++ b/src/hb-gpu-fragment-hlsl.hh @@ -0,0 +1,265 @@ +static const char *hb_gpu_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" +" * Permission is hereby granted, without written agreement and without\n" +" * license or royalty fees, to use, copy, modify, and distribute this\n" +" * software and its documentation for any purpose, provided that the\n" +" * above copyright notice and the following two paragraphs appear in\n" +" * all copies of this software.\n" +" *\n" +" * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR\n" +" * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES\n" +" * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN\n" +" * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH\n" +" * DAMAGE.\n" +" *\n" +" * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,\n" +" * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND\n" +" * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS\n" +" * ON AN \"AS IS\" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO\n" +" * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.\n" +" */\n" +"\n" +"\n" +"/* Requires Shader Model 5.0+.\n" +" *\n" +" * The caller must declare:\n" +" * StructuredBuffer hb_gpu_atlas : register(t0);\n" +" */\n" +"\n" +"\n" +"#ifndef HB_GPU_UNITS_PER_EM\n" +"#define HB_GPU_UNITS_PER_EM 4\n" +"#endif\n" +"\n" +"#define HB_GPU_INV_UNITS (1.0 / (float) HB_GPU_UNITS_PER_EM)\n" +"\n" +"\n" +"int4 _hb_gpu_fetch (int offset)\n" +"{\n" +" return hb_gpu_atlas[offset];\n" +"}\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" +"};\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" +"\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" +"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" +"/* Render a glyph and return its coverage in [0, 1].\n" +" *\n" +" * renderCoord: em-space sample position (interpolated from vertex shader)\n" +" * glyphLoc: offset into hb_gpu_atlas for this glyph's encoded blob\n" +" */\n" +"float hb_gpu_render (float2 renderCoord, uint glyphLoc_)\n" +"{\n" +" float2 emsPerPixel = fwidth (renderCoord);\n" +" float2 pixelsPerEm = 1.0 / emsPerPixel;\n" +"\n" +" _hb_gpu_glyph_info gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_);\n" +" int glyphLoc = gi.glyphLoc;\n" +" 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" +; diff --git a/src/hb-gpu-fragment.hlsl b/src/hb-gpu-fragment.hlsl new file mode 100644 index 000000000..ca441ef98 --- /dev/null +++ b/src/hb-gpu-fragment.hlsl @@ -0,0 +1,263 @@ +/* + * 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. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + */ + + +/* Requires Shader Model 5.0+. + * + * The caller must declare: + * StructuredBuffer hb_gpu_atlas : register(t0); + */ + + +#ifndef HB_GPU_UNITS_PER_EM +#define HB_GPU_UNITS_PER_EM 4 +#endif + +#define HB_GPU_INV_UNITS (1.0 / (float) HB_GPU_UNITS_PER_EM) + + +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; +}; + +_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; + + 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; +} + +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); +} + + +/* Render a glyph and return its coverage in [0, 1]. + * + * renderCoord: em-space sample position (interpolated from vertex shader) + * glyphLoc: offset into hb_gpu_atlas for this glyph's encoded blob + */ +float hb_gpu_render (float2 renderCoord, uint glyphLoc_) +{ + float2 emsPerPixel = fwidth (renderCoord); + float2 pixelsPerEm = 1.0 / emsPerPixel; + + _hb_gpu_glyph_info gi = _hb_gpu_decode_glyph (renderCoord, glyphLoc_); + int glyphLoc = gi.glyphLoc; + 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); +} diff --git a/src/hb-gpu-shaders.cc b/src/hb-gpu-shaders.cc index 9962a4418..2462a831b 100644 --- a/src/hb-gpu-shaders.cc +++ b/src/hb-gpu-shaders.cc @@ -34,6 +34,8 @@ #include "hb-gpu-vertex-msl.hh" #include "hb-gpu-fragment-wgsl.hh" #include "hb-gpu-vertex-wgsl.hh" +#include "hb-gpu-fragment-hlsl.hh" +#include "hb-gpu-vertex-hlsl.hh" /** @@ -62,6 +64,8 @@ hb_gpu_shader_fragment_source (hb_gpu_shader_lang_t lang) return hb_gpu_fragment_msl; case HB_GPU_SHADER_LANG_WGSL: return hb_gpu_fragment_wgsl; + case HB_GPU_SHADER_LANG_HLSL: + return hb_gpu_fragment_hlsl; default: return nullptr; } @@ -89,6 +93,8 @@ hb_gpu_shader_vertex_source (hb_gpu_shader_lang_t lang) return hb_gpu_vertex_msl; case HB_GPU_SHADER_LANG_WGSL: return hb_gpu_vertex_wgsl; + case HB_GPU_SHADER_LANG_HLSL: + return hb_gpu_vertex_hlsl; default: return nullptr; } diff --git a/src/hb-gpu-vertex-hlsl.hh b/src/hb-gpu-vertex-hlsl.hh new file mode 100644 index 000000000..5fa003871 --- /dev/null +++ b/src/hb-gpu-vertex-hlsl.hh @@ -0,0 +1,71 @@ +static const char *hb_gpu_vertex_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" +" * Permission is hereby granted, without written agreement and without\n" +" * license or royalty fees, to use, copy, modify, and distribute this\n" +" * software and its documentation for any purpose, provided that the\n" +" * above copyright notice and the following two paragraphs appear in\n" +" * all copies of this software.\n" +" *\n" +" * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR\n" +" * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES\n" +" * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN\n" +" * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH\n" +" * DAMAGE.\n" +" *\n" +" * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,\n" +" * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND\n" +" * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS\n" +" * ON AN \"AS IS\" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO\n" +" * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.\n" +" */\n" +"\n" +"\n" +"/* Requires Shader Model 5.0+. */\n" +"\n" +"\n" +"/* Dilate a glyph vertex by half a pixel on screen.\n" +" *\n" +" * position: object-space vertex position (modified in place)\n" +" * texcoord: em-space sample coordinates (modified in place)\n" +" * normal: object-space outward normal at this vertex\n" +" * jac: inverse of the 2x2 linear part of the em-to-object transform\n" +" * m: model-view-projection matrix\n" +" * viewport: viewport size in pixels\n" +" */\n" +"void hb_gpu_dilate (inout float2 position, inout float2 texcoord,\n" +" float2 normal, float4 jac,\n" +" float4x4 m, float2 viewport)\n" +"{\n" +" float2 n = normalize (normal);\n" +"\n" +" float4 clipPos = mul (m, float4 (position, 0.0, 1.0));\n" +" float4 clipN = mul (m, float4 (n, 0.0, 0.0));\n" +"\n" +" float s = clipPos.w;\n" +" float t = clipN.w;\n" +"\n" +" float u = (s * clipN.x - t * clipPos.x) * viewport.x;\n" +" float v = (s * clipN.y - t * clipPos.y) * viewport.y;\n" +"\n" +" float s2 = s * s;\n" +" float st = s * t;\n" +" float uv = u * u + v * v;\n" +"\n" +" float denom = uv - st * st;\n" +" float d = abs (denom) > 1.0 / 16777216.0\n" +" ? s2 * (st + sqrt (uv)) / denom\n" +" : 0.0;\n" +"\n" +" float2 dPos = d * normal;\n" +" position += dPos;\n" +" texcoord += float2 (dot (dPos, jac.xy), dot (dPos, jac.zw));\n" +"}\n" +; diff --git a/src/hb-gpu-vertex.hlsl b/src/hb-gpu-vertex.hlsl new file mode 100644 index 000000000..4a9528b85 --- /dev/null +++ b/src/hb-gpu-vertex.hlsl @@ -0,0 +1,69 @@ +/* + * 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. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + */ + + +/* Requires Shader Model 5.0+. */ + + +/* Dilate a glyph vertex by half a pixel on screen. + * + * position: object-space vertex position (modified in place) + * texcoord: em-space sample coordinates (modified in place) + * normal: object-space outward normal at this vertex + * jac: inverse of the 2x2 linear part of the em-to-object transform + * m: model-view-projection matrix + * viewport: viewport size in pixels + */ +void hb_gpu_dilate (inout float2 position, inout float2 texcoord, + float2 normal, float4 jac, + float4x4 m, float2 viewport) +{ + float2 n = normalize (normal); + + float4 clipPos = mul (m, float4 (position, 0.0, 1.0)); + float4 clipN = mul (m, float4 (n, 0.0, 0.0)); + + float s = clipPos.w; + float t = clipN.w; + + float u = (s * clipN.x - t * clipPos.x) * viewport.x; + float v = (s * clipN.y - t * clipPos.y) * viewport.y; + + float s2 = s * s; + float st = s * t; + float uv = u * u + v * v; + + float denom = uv - st * st; + float d = abs (denom) > 1.0 / 16777216.0 + ? s2 * (st + sqrt (uv)) / denom + : 0.0; + + float2 dPos = d * normal; + position += dPos; + texcoord += float2 (dot (dPos, jac.xy), dot (dPos, jac.zw)); +} diff --git a/src/hb-gpu.h b/src/hb-gpu.h index 05251c41b..a3811f262 100644 --- a/src/hb-gpu.h +++ b/src/hb-gpu.h @@ -34,9 +34,10 @@ HB_BEGIN_DECLS /** * hb_gpu_shader_lang_t: - * @HB_GPU_SHADER_LANG_GLSL: GLSL (compatible with GLSL 3.30 and GLSL ES 3.00) - * @HB_GPU_SHADER_LANG_MSL: Metal Shading Language 2.0 - * @HB_GPU_SHADER_LANG_WGSL: WGSL (WebGPU Shading Language) + * @HB_GPU_SHADER_LANG_GLSL: GLSL (OpenGL 3.3 / OpenGL ES 3.0 / WebGL 2.0) + * @HB_GPU_SHADER_LANG_WGSL: WGSL (WebGPU) + * @HB_GPU_SHADER_LANG_MSL: MSL (Metal) + * @HB_GPU_SHADER_LANG_HLSL: HLSL (Direct3D) * * Shader language variant. * @@ -44,8 +45,9 @@ HB_BEGIN_DECLS */ typedef enum { HB_GPU_SHADER_LANG_GLSL, - HB_GPU_SHADER_LANG_MSL, HB_GPU_SHADER_LANG_WGSL, + HB_GPU_SHADER_LANG_MSL, + HB_GPU_SHADER_LANG_HLSL, } hb_gpu_shader_lang_t; HB_EXTERN const char * diff --git a/src/meson.build b/src/meson.build index eed501c89..3847c6198 100644 --- a/src/meson.build +++ b/src/meson.build @@ -867,6 +867,11 @@ hb_gpu_wgsl_sources = [ [ 'hb-gpu-vertex.wgsl', 'hb-gpu-vertex-wgsl.hh', 'hb_gpu_vertex_wgsl' ], ] +hb_gpu_hlsl_sources = [ + [ 'hb-gpu-fragment.hlsl', 'hb-gpu-fragment-hlsl.hh', 'hb_gpu_fragment_hlsl' ], + [ 'hb-gpu-vertex.hlsl', 'hb-gpu-vertex-hlsl.hh', 'hb_gpu_vertex_hlsl' ], +] + hb_gpu_shader_helper = find_program('gen-gpu-shader-artifacts.py') foreach p: hb_gpu_glsl_sources glsl = p.get(0) @@ -901,6 +906,17 @@ foreach p: hb_gpu_wgsl_sources meson.current_source_dir(), '@INPUT@' ]) endforeach +foreach p: hb_gpu_hlsl_sources + hlsl = p.get(0) + hh = p.get(1) + custom_target('@0@'.format(hh), + build_by_default: true, + input: hlsl, + output: hh, + command: [ hb_gpu_shader_helper, '@OUTPUT@', + meson.current_source_dir(), '@INPUT@' ]) +endforeach + hb_gpu_sources = files( 'hb-gpu.cc', 'hb-gpu-draw.cc',