mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-12-01 10:34:23 +01:00
[gpu] Add WGSL shader sources for WebGPU
Port fragment and vertex shaders to WGSL. Key differences from GLSL/MSL: no preprocessor (constants instead of #define), bitcast instead of floatBitsToUint, storage buffer pointer parameter for atlas, and emsPerPixel passed explicitly since fwidth() is only available in the caller's fragment entry point. Adds HB_GPU_SHADER_LANG_WGSL to the shader language enum. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
0c150b97b4
commit
09f6ef66e8
7 files changed
+668
No files matched your search
@@ -17,6 +17,8 @@ decl_map = {
|
||||
'hb-gpu-vertex-glsl.hh': 'hb_gpu_vertex_glsl',
|
||||
'hb-gpu-fragment-msl.hh': 'hb_gpu_fragment_msl',
|
||||
'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',
|
||||
}
|
||||
|
||||
decl = decl_map.get(hh)
|
||||
|
||||
@@ -0,0 +1,248 @@
|
||||
static const char *hb_gpu_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"
|
||||
" * 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 WGSL (WebGPU Shading Language). */\n"
|
||||
"\n"
|
||||
"\n"
|
||||
"const HB_GPU_UNITS_PER_EM: f32 = 4.0;\n"
|
||||
"const HB_GPU_INV_UNITS: f32 = 1.0 / 4.0;\n"
|
||||
"\n"
|
||||
"\n"
|
||||
"fn _hb_gpu_fetch (hb_gpu_atlas: ptr<storage, array<vec4<i32>>, read>,\n"
|
||||
" offset: i32) -> vec4<i32>\n"
|
||||
"{\n"
|
||||
" return (*hb_gpu_atlas)[offset];\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"\n"
|
||||
"fn _hb_gpu_calc_root_code (y1: f32, y2: f32, y3: f32) -> u32\n"
|
||||
"{\n"
|
||||
" let i1 = bitcast<u32> (y1) >> 31u;\n"
|
||||
" let i2 = bitcast<u32> (y2) >> 30u;\n"
|
||||
" let i3 = bitcast<u32> (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"
|
||||
"/* 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"
|
||||
" * hb_gpu_atlas: storage buffer with the glyph atlas data\n"
|
||||
" * emsPerPixel: screen-space derivatives of renderCoord (fwidth equivalent)\n"
|
||||
" */\n"
|
||||
"fn hb_gpu_render (renderCoord: vec2f, glyphLoc_: u32,\n"
|
||||
" hb_gpu_atlas: ptr<storage, array<vec4<i32>>, read>,\n"
|
||||
" emsPerPixel: vec2f) -> f32\n"
|
||||
"{\n"
|
||||
" let pixelsPerEm = 1.0 / emsPerPixel;\n"
|
||||
"\n"
|
||||
" let glyphLoc = i32 (glyphLoc_);\n"
|
||||
"\n"
|
||||
" /* Read blob header */\n"
|
||||
" let header0 = _hb_gpu_fetch (hb_gpu_atlas, glyphLoc);\n"
|
||||
" let header1 = _hb_gpu_fetch (hb_gpu_atlas, glyphLoc + 1);\n"
|
||||
" let ext = vec4f (header0) * HB_GPU_INV_UNITS; /* min_x, min_y, max_x, max_y */\n"
|
||||
" let numHBands = header1.r;\n"
|
||||
" let numVBands = header1.g;\n"
|
||||
"\n"
|
||||
" /* Compute band transform from extents */\n"
|
||||
" let extSize = ext.zw - ext.xy; /* (width, height) */\n"
|
||||
" let bandScale = vec2f (f32 (numVBands), f32 (numHBands)) / max (extSize, vec2f (1.0 / 65536.0));\n"
|
||||
" let bandOffset = -ext.xy * bandScale;\n"
|
||||
"\n"
|
||||
" let bandIndex = clamp (vec2<i32> (renderCoord * bandScale + bandOffset),\n"
|
||||
" vec2<i32> (0, 0),\n"
|
||||
" vec2<i32> (numVBands - 1, numHBands - 1));\n"
|
||||
"\n"
|
||||
" /* Skip past header (2 texels) */\n"
|
||||
" let bandBase = glyphLoc + 2;\n"
|
||||
"\n"
|
||||
" var xcov: f32 = 0.0;\n"
|
||||
" var xwgt: f32 = 0.0;\n"
|
||||
"\n"
|
||||
" let hbandData = _hb_gpu_fetch (hb_gpu_atlas, bandBase + 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<i32> (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 + 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<i32> (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"
|
||||
;
|
||||
@@ -0,0 +1,246 @@
|
||||
/*
|
||||
* 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 WGSL (WebGPU Shading Language). */
|
||||
|
||||
|
||||
const HB_GPU_UNITS_PER_EM: f32 = 4.0;
|
||||
const HB_GPU_INV_UNITS: f32 = 1.0 / 4.0;
|
||||
|
||||
|
||||
fn _hb_gpu_fetch (hb_gpu_atlas: ptr<storage, array<vec4<i32>>, read>,
|
||||
offset: i32) -> vec4<i32>
|
||||
{
|
||||
return (*hb_gpu_atlas)[offset];
|
||||
}
|
||||
|
||||
|
||||
fn _hb_gpu_calc_root_code (y1: f32, y2: f32, y3: f32) -> u32
|
||||
{
|
||||
let i1 = bitcast<u32> (y1) >> 31u;
|
||||
let i2 = bitcast<u32> (y2) >> 30u;
|
||||
let i3 = bitcast<u32> (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);
|
||||
}
|
||||
|
||||
/* 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
|
||||
* hb_gpu_atlas: storage buffer with the glyph atlas data
|
||||
* emsPerPixel: screen-space derivatives of renderCoord (fwidth equivalent)
|
||||
*/
|
||||
fn hb_gpu_render (renderCoord: vec2f, glyphLoc_: u32,
|
||||
hb_gpu_atlas: ptr<storage, array<vec4<i32>>, read>,
|
||||
emsPerPixel: vec2f) -> f32
|
||||
{
|
||||
let pixelsPerEm = 1.0 / emsPerPixel;
|
||||
|
||||
let glyphLoc = i32 (glyphLoc_);
|
||||
|
||||
/* Read blob header */
|
||||
let header0 = _hb_gpu_fetch (hb_gpu_atlas, glyphLoc);
|
||||
let header1 = _hb_gpu_fetch (hb_gpu_atlas, glyphLoc + 1);
|
||||
let ext = vec4f (header0) * HB_GPU_INV_UNITS; /* min_x, min_y, max_x, max_y */
|
||||
let numHBands = header1.r;
|
||||
let numVBands = header1.g;
|
||||
|
||||
/* Compute band transform from extents */
|
||||
let extSize = ext.zw - ext.xy; /* (width, height) */
|
||||
let bandScale = vec2f (f32 (numVBands), f32 (numHBands)) / max (extSize, vec2f (1.0 / 65536.0));
|
||||
let bandOffset = -ext.xy * bandScale;
|
||||
|
||||
let bandIndex = clamp (vec2<i32> (renderCoord * bandScale + bandOffset),
|
||||
vec2<i32> (0, 0),
|
||||
vec2<i32> (numVBands - 1, numHBands - 1));
|
||||
|
||||
/* Skip past header (2 texels) */
|
||||
let bandBase = glyphLoc + 2;
|
||||
|
||||
var xcov: f32 = 0.0;
|
||||
var xwgt: f32 = 0.0;
|
||||
|
||||
let hbandData = _hb_gpu_fetch (hb_gpu_atlas, bandBase + 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<i32> (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 + 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<i32> (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);
|
||||
}
|
||||
@@ -32,6 +32,8 @@
|
||||
#include "hb-gpu-vertex-glsl.hh"
|
||||
#include "hb-gpu-fragment-msl.hh"
|
||||
#include "hb-gpu-vertex-msl.hh"
|
||||
#include "hb-gpu-fragment-wgsl.hh"
|
||||
#include "hb-gpu-vertex-wgsl.hh"
|
||||
|
||||
|
||||
/**
|
||||
@@ -58,6 +60,8 @@ hb_gpu_shader_fragment_source (hb_gpu_shader_lang_t lang)
|
||||
return hb_gpu_fragment_glsl;
|
||||
case HB_GPU_SHADER_LANG_MSL:
|
||||
return hb_gpu_fragment_msl;
|
||||
case HB_GPU_SHADER_LANG_WGSL:
|
||||
return hb_gpu_fragment_wgsl;
|
||||
default:
|
||||
return nullptr;
|
||||
}
|
||||
@@ -83,6 +87,8 @@ hb_gpu_shader_vertex_source (hb_gpu_shader_lang_t lang)
|
||||
return hb_gpu_vertex_glsl;
|
||||
case HB_GPU_SHADER_LANG_MSL:
|
||||
return hb_gpu_vertex_msl;
|
||||
case HB_GPU_SHADER_LANG_WGSL:
|
||||
return hb_gpu_vertex_wgsl;
|
||||
default:
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
static const char *hb_gpu_vertex_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"
|
||||
" * 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 WGSL (WebGPU Shading Language). */\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"
|
||||
" * stored row-major as (j00, j01, j10, j11). Maps object-space\n"
|
||||
" * displacements back to em-space for texcoord adjustment.\n"
|
||||
" * For simple scaling with y-flip (the common case):\n"
|
||||
" * em-to-object = [[s, 0], [0, -s]]\n"
|
||||
" * jac = (1/s, 0, 0, -1/s)\n"
|
||||
" * m: model-view-projection matrix\n"
|
||||
" * viewport: viewport size in pixels\n"
|
||||
" *\n"
|
||||
" * Returns (new_position, new_texcoord).\n"
|
||||
" */\n"
|
||||
"fn hb_gpu_dilate (position: vec2f, texcoord: vec2f,\n"
|
||||
" normal: vec2f, jac: vec4f,\n"
|
||||
" m: mat4x4f, viewport: vec2f) -> array<vec2f, 2>\n"
|
||||
"{\n"
|
||||
" let n = normalize (normal);\n"
|
||||
"\n"
|
||||
" let clipPos = m * vec4f (position, 0.0, 1.0);\n"
|
||||
" let clipN = m * vec4f (n, 0.0, 0.0);\n"
|
||||
"\n"
|
||||
" let s = clipPos.w;\n"
|
||||
" let t = clipN.w;\n"
|
||||
"\n"
|
||||
" let u = (s * clipN.x - t * clipPos.x) * viewport.x;\n"
|
||||
" let v = (s * clipN.y - t * clipPos.y) * viewport.y;\n"
|
||||
"\n"
|
||||
" let s2 = s * s;\n"
|
||||
" let st = s * t;\n"
|
||||
" let uv = u * u + v * v;\n"
|
||||
"\n"
|
||||
" let denom = uv - st * st;\n"
|
||||
" var d: f32;\n"
|
||||
" if (abs (denom) > 1.0 / 16777216.0) {\n"
|
||||
" d = s2 * (st + sqrt (uv)) / denom;\n"
|
||||
" } else {\n"
|
||||
" d = 0.0;\n"
|
||||
" }\n"
|
||||
"\n"
|
||||
" let dPos = d * normal;\n"
|
||||
" return array<vec2f, 2> (\n"
|
||||
" position + dPos,\n"
|
||||
" texcoord + vec2f (dot (dPos, jac.xy), dot (dPos, jac.zw))\n"
|
||||
" );\n"
|
||||
"}\n"
|
||||
;
|
||||
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* 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 WGSL (WebGPU Shading Language). */
|
||||
|
||||
|
||||
/* 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,
|
||||
* stored row-major as (j00, j01, j10, j11). Maps object-space
|
||||
* displacements back to em-space for texcoord adjustment.
|
||||
* For simple scaling with y-flip (the common case):
|
||||
* em-to-object = [[s, 0], [0, -s]]
|
||||
* jac = (1/s, 0, 0, -1/s)
|
||||
* m: model-view-projection matrix
|
||||
* viewport: viewport size in pixels
|
||||
*
|
||||
* Returns (new_position, new_texcoord).
|
||||
*/
|
||||
fn hb_gpu_dilate (position: vec2f, texcoord: vec2f,
|
||||
normal: vec2f, jac: vec4f,
|
||||
m: mat4x4f, viewport: vec2f) -> array<vec2f, 2>
|
||||
{
|
||||
let n = normalize (normal);
|
||||
|
||||
let clipPos = m * vec4f (position, 0.0, 1.0);
|
||||
let clipN = m * vec4f (n, 0.0, 0.0);
|
||||
|
||||
let s = clipPos.w;
|
||||
let t = clipN.w;
|
||||
|
||||
let u = (s * clipN.x - t * clipPos.x) * viewport.x;
|
||||
let v = (s * clipN.y - t * clipPos.y) * viewport.y;
|
||||
|
||||
let s2 = s * s;
|
||||
let st = s * t;
|
||||
let uv = u * u + v * v;
|
||||
|
||||
let denom = uv - st * st;
|
||||
var d: f32;
|
||||
if (abs (denom) > 1.0 / 16777216.0) {
|
||||
d = s2 * (st + sqrt (uv)) / denom;
|
||||
} else {
|
||||
d = 0.0;
|
||||
}
|
||||
|
||||
let dPos = d * normal;
|
||||
return array<vec2f, 2> (
|
||||
position + dPos,
|
||||
texcoord + vec2f (dot (dPos, jac.xy), dot (dPos, jac.zw))
|
||||
);
|
||||
}
|
||||
@@ -36,6 +36,7 @@ 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)
|
||||
*
|
||||
* Shader language variant.
|
||||
*
|
||||
@@ -44,6 +45,7 @@ HB_BEGIN_DECLS
|
||||
typedef enum {
|
||||
HB_GPU_SHADER_LANG_GLSL,
|
||||
HB_GPU_SHADER_LANG_MSL,
|
||||
HB_GPU_SHADER_LANG_WGSL,
|
||||
} hb_gpu_shader_lang_t;
|
||||
|
||||
HB_EXTERN const char *
|
||||
|
||||
Reference in new issue
Block a user