mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-24 08:21:32 +01:00
Merge branch 'metal'
This commit is contained in:
commit
76fb16c729
21 files changed
+1538
-94
No files matched your search
@@ -15,6 +15,8 @@ hh = os.path.basename(OUTPUT)
|
||||
decl_map = {
|
||||
'hb-gpu-fragment-glsl.hh': 'hb_gpu_fragment_glsl',
|
||||
'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',
|
||||
}
|
||||
|
||||
decl = decl_map.get(hh)
|
||||
|
||||
@@ -0,0 +1,241 @@
|
||||
static const char *hb_gpu_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"
|
||||
" * 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 Metal Shading Language 2.0. */\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 float(1.0 / float(HB_GPU_UNITS_PER_EM))\n"
|
||||
"\n"
|
||||
"\n"
|
||||
"int4 _hb_gpu_fetch (device const short4* hb_gpu_atlas, int offset)\n"
|
||||
"{\n"
|
||||
" return int4 (hb_gpu_atlas[offset]);\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"\n"
|
||||
"uint _hb_gpu_calc_root_code (float y1, float y2, float y3)\n"
|
||||
"{\n"
|
||||
" uint i1 = as_type<uint> (y1) >> 31U;\n"
|
||||
" uint i2 = as_type<uint> (y2) >> 30U;\n"
|
||||
" uint i3 = as_type<uint> (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"
|
||||
"/* Render a glyph and return its coverage in [0, 1].\n"
|
||||
" *\n"
|
||||
" * Requires hb_gpu_atlas to point to the glyph atlas buffer.\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: device pointer to the glyph atlas buffer\n"
|
||||
" */\n"
|
||||
"float hb_gpu_render (float2 renderCoord, uint glyphLoc_,\n"
|
||||
" device const short4* hb_gpu_atlas)\n"
|
||||
"{\n"
|
||||
" float2 emsPerPixel = fwidth (renderCoord);\n"
|
||||
" float2 pixelsPerEm = 1.0 / emsPerPixel;\n"
|
||||
"\n"
|
||||
" int glyphLoc = int (glyphLoc_);\n"
|
||||
"\n"
|
||||
" /* Read blob header */\n"
|
||||
" int4 header0 = _hb_gpu_fetch (hb_gpu_atlas, glyphLoc);\n"
|
||||
" int4 header1 = _hb_gpu_fetch (hb_gpu_atlas, glyphLoc + 1);\n"
|
||||
" float4 ext = float4 (header0) * HB_GPU_INV_UNITS; /* min_x, min_y, max_x, max_y */\n"
|
||||
" int numHBands = header1.r;\n"
|
||||
" int numVBands = header1.g;\n"
|
||||
"\n"
|
||||
" /* Compute band transform from extents */\n"
|
||||
" float2 extSize = ext.zw - ext.xy; /* (width, height) */\n"
|
||||
" float2 bandScale = float2 (float (numVBands), float (numHBands)) / max (extSize, float2 (1.0 / 65536.0));\n"
|
||||
" float2 bandOffset = -ext.xy * bandScale;\n"
|
||||
"\n"
|
||||
" int2 bandIndex = clamp (int2 (renderCoord * bandScale + bandOffset),\n"
|
||||
" int2 (0, 0),\n"
|
||||
" int2 (numVBands - 1, numHBands - 1));\n"
|
||||
"\n"
|
||||
" /* Skip past header (2 texels) */\n"
|
||||
" int bandBase = glyphLoc + 2;\n"
|
||||
"\n"
|
||||
" float xcov = 0.0;\n"
|
||||
" float xwgt = 0.0;\n"
|
||||
"\n"
|
||||
" int4 hbandData = _hb_gpu_fetch (hb_gpu_atlas, bandBase + 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 + 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"
|
||||
;
|
||||
@@ -0,0 +1,239 @@
|
||||
/*
|
||||
* 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 Metal Shading Language 2.0. */
|
||||
|
||||
|
||||
#ifndef HB_GPU_UNITS_PER_EM
|
||||
#define HB_GPU_UNITS_PER_EM 4
|
||||
#endif
|
||||
|
||||
#define HB_GPU_INV_UNITS float(1.0 / float(HB_GPU_UNITS_PER_EM))
|
||||
|
||||
|
||||
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<uint> (y1) >> 31U;
|
||||
uint i2 = as_type<uint> (y2) >> 30U;
|
||||
uint i3 = as_type<uint> (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);
|
||||
}
|
||||
|
||||
/* Render a glyph and return its coverage in [0, 1].
|
||||
*
|
||||
* Requires hb_gpu_atlas to point to the glyph atlas buffer.
|
||||
*
|
||||
* renderCoord: em-space sample position (interpolated from vertex shader)
|
||||
* glyphLoc: offset into hb_gpu_atlas for this glyph's encoded blob
|
||||
* hb_gpu_atlas: device pointer to the glyph atlas buffer
|
||||
*/
|
||||
float hb_gpu_render (float2 renderCoord, uint glyphLoc_,
|
||||
device const short4* hb_gpu_atlas)
|
||||
{
|
||||
float2 emsPerPixel = fwidth (renderCoord);
|
||||
float2 pixelsPerEm = 1.0 / emsPerPixel;
|
||||
|
||||
int glyphLoc = int (glyphLoc_);
|
||||
|
||||
/* Read blob header */
|
||||
int4 header0 = _hb_gpu_fetch (hb_gpu_atlas, glyphLoc);
|
||||
int4 header1 = _hb_gpu_fetch (hb_gpu_atlas, glyphLoc + 1);
|
||||
float4 ext = float4 (header0) * HB_GPU_INV_UNITS; /* min_x, min_y, max_x, max_y */
|
||||
int numHBands = header1.r;
|
||||
int numVBands = header1.g;
|
||||
|
||||
/* Compute band transform from extents */
|
||||
float2 extSize = ext.zw - ext.xy; /* (width, height) */
|
||||
float2 bandScale = float2 (float (numVBands), float (numHBands)) / max (extSize, float2 (1.0 / 65536.0));
|
||||
float2 bandOffset = -ext.xy * bandScale;
|
||||
|
||||
int2 bandIndex = clamp (int2 (renderCoord * bandScale + bandOffset),
|
||||
int2 (0, 0),
|
||||
int2 (numVBands - 1, numHBands - 1));
|
||||
|
||||
/* Skip past header (2 texels) */
|
||||
int bandBase = glyphLoc + 2;
|
||||
|
||||
float xcov = 0.0;
|
||||
float xwgt = 0.0;
|
||||
|
||||
int4 hbandData = _hb_gpu_fetch (hb_gpu_atlas, bandBase + 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 + 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);
|
||||
}
|
||||
@@ -30,6 +30,8 @@
|
||||
|
||||
#include "hb-gpu-fragment-glsl.hh"
|
||||
#include "hb-gpu-vertex-glsl.hh"
|
||||
#include "hb-gpu-fragment-msl.hh"
|
||||
#include "hb-gpu-vertex-msl.hh"
|
||||
|
||||
|
||||
/**
|
||||
@@ -54,6 +56,8 @@ hb_gpu_shader_fragment_source (hb_gpu_shader_lang_t lang)
|
||||
switch (lang) {
|
||||
case HB_GPU_SHADER_LANG_GLSL:
|
||||
return hb_gpu_fragment_glsl;
|
||||
case HB_GPU_SHADER_LANG_MSL:
|
||||
return hb_gpu_fragment_msl;
|
||||
default:
|
||||
return nullptr;
|
||||
}
|
||||
@@ -77,6 +81,8 @@ hb_gpu_shader_vertex_source (hb_gpu_shader_lang_t lang)
|
||||
switch (lang) {
|
||||
case HB_GPU_SHADER_LANG_GLSL:
|
||||
return hb_gpu_vertex_glsl;
|
||||
case HB_GPU_SHADER_LANG_MSL:
|
||||
return hb_gpu_vertex_msl;
|
||||
default:
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
static const char *hb_gpu_vertex_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"
|
||||
" * 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 Metal Shading Language 2.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"
|
||||
" * 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"
|
||||
"void hb_gpu_dilate (thread float2& position, thread float2& texcoord,\n"
|
||||
" float2 normal, float4 jac,\n"
|
||||
" float4x4 m, float2 viewport)\n"
|
||||
"{\n"
|
||||
" float2 n = normalize (normal);\n"
|
||||
"\n"
|
||||
" float4 clipPos = m * float4 (position, 0.0, 1.0);\n"
|
||||
" float4 clipN = 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"
|
||||
;
|
||||
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* 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 Metal Shading Language 2.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,
|
||||
* 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
|
||||
*/
|
||||
void hb_gpu_dilate (thread float2& position, thread float2& texcoord,
|
||||
float2 normal, float4 jac,
|
||||
float4x4 m, float2 viewport)
|
||||
{
|
||||
float2 n = normalize (normal);
|
||||
|
||||
float4 clipPos = m * float4 (position, 0.0, 1.0);
|
||||
float4 clipN = 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));
|
||||
}
|
||||
@@ -35,6 +35,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
|
||||
*
|
||||
* Shader language variant.
|
||||
*
|
||||
@@ -42,6 +43,7 @@ HB_BEGIN_DECLS
|
||||
*/
|
||||
typedef enum {
|
||||
HB_GPU_SHADER_LANG_GLSL,
|
||||
HB_GPU_SHADER_LANG_MSL,
|
||||
} hb_gpu_shader_lang_t;
|
||||
|
||||
HB_EXTERN const char *
|
||||
|
||||
@@ -857,6 +857,11 @@ hb_gpu_glsl_sources = [
|
||||
[ 'hb-gpu-vertex.glsl', 'hb-gpu-vertex-glsl.hh', 'hb_gpu_vertex_glsl' ],
|
||||
]
|
||||
|
||||
hb_gpu_msl_sources = [
|
||||
[ 'hb-gpu-fragment.msl', 'hb-gpu-fragment-msl.hh', 'hb_gpu_fragment_msl' ],
|
||||
[ 'hb-gpu-vertex.msl', 'hb-gpu-vertex-msl.hh', 'hb_gpu_vertex_msl' ],
|
||||
]
|
||||
|
||||
hb_gpu_shader_helper = find_program('gen-gpu-shader-artifacts.py')
|
||||
foreach p: hb_gpu_glsl_sources
|
||||
glsl = p.get(0)
|
||||
@@ -869,6 +874,17 @@ foreach p: hb_gpu_glsl_sources
|
||||
meson.current_source_dir(), '@INPUT@' ])
|
||||
endforeach
|
||||
|
||||
foreach p: hb_gpu_msl_sources
|
||||
msl = p.get(0)
|
||||
hh = p.get(1)
|
||||
custom_target('@0@'.format(hh),
|
||||
build_by_default: true,
|
||||
input: msl,
|
||||
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',
|
||||
|
||||
+67
-36
@@ -30,7 +30,7 @@
|
||||
#include "gpu/demo-common.h"
|
||||
#include "gpu/demo-buffer.h"
|
||||
#include "gpu/demo-font.h"
|
||||
#include "gpu/demo-glstate.h"
|
||||
#include "gpu/demo-renderer.h"
|
||||
#include "gpu/demo-view.h"
|
||||
|
||||
#include "options.hh"
|
||||
@@ -45,6 +45,8 @@ struct gpu_output_t
|
||||
|
||||
void add_options (option_parser_t *parser HB_UNUSED) {}
|
||||
|
||||
bool use_metal = false;
|
||||
|
||||
template <typename app_t>
|
||||
void init (hb_buffer_t *buffer_ HB_UNUSED, const app_t *app)
|
||||
{
|
||||
@@ -55,13 +57,55 @@ struct gpu_output_t
|
||||
hb_font_get_scale (font, &x_scale, &y_scale);
|
||||
font_size = (double) y_scale;
|
||||
|
||||
/* Setup GL */
|
||||
glfwSetErrorCallback ([] (int error, const char *desc) {
|
||||
fprintf (stderr, "GLFW error %d: %s\n", error, desc);
|
||||
});
|
||||
if (!glfwInit ())
|
||||
fail (false, "Failed to initialize GLFW");
|
||||
|
||||
if (use_metal)
|
||||
init_metal ();
|
||||
else
|
||||
init_gl ();
|
||||
|
||||
vu = demo_view_create (renderer, window);
|
||||
|
||||
glfwSetWindowUserPointer (window, this);
|
||||
glfwSetFramebufferSizeCallback (window, [] (GLFWwindow *w, int width, int height) {
|
||||
auto *self = (gpu_output_t *) glfwGetWindowUserPointer (w);
|
||||
demo_view_reshape_func (self->vu, width, height);
|
||||
});
|
||||
glfwSetKeyCallback (window, [] (GLFWwindow *w, int key, int scancode, int action, int mods) {
|
||||
auto *self = (gpu_output_t *) glfwGetWindowUserPointer (w);
|
||||
demo_view_key_func (self->vu, key, scancode, action, mods);
|
||||
});
|
||||
glfwSetCharCallback (window, [] (GLFWwindow *w, unsigned int cp) {
|
||||
auto *self = (gpu_output_t *) glfwGetWindowUserPointer (w);
|
||||
demo_view_char_func (self->vu, cp);
|
||||
});
|
||||
glfwSetMouseButtonCallback (window, [] (GLFWwindow *w, int button, int action, int mods) {
|
||||
auto *self = (gpu_output_t *) glfwGetWindowUserPointer (w);
|
||||
demo_view_mouse_func (self->vu, button, action, mods);
|
||||
});
|
||||
glfwSetScrollCallback (window, [] (GLFWwindow *w, double xoff, double yoff) {
|
||||
auto *self = (gpu_output_t *) glfwGetWindowUserPointer (w);
|
||||
demo_view_scroll_func (self->vu, xoff, yoff);
|
||||
});
|
||||
glfwSetCursorPosCallback (window, [] (GLFWwindow *w, double x, double y) {
|
||||
auto *self = (gpu_output_t *) glfwGetWindowUserPointer (w);
|
||||
demo_view_motion_func (self->vu, x, y);
|
||||
});
|
||||
|
||||
demo_font_ = demo_font_create (font, renderer->get_atlas ());
|
||||
buf = demo_buffer_create ();
|
||||
|
||||
demo_point_t top_left = {0, 0};
|
||||
demo_buffer_move_to (buf, &top_left);
|
||||
demo_buffer_current_line (buf, font_size);
|
||||
}
|
||||
|
||||
void init_gl ()
|
||||
{
|
||||
glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
@@ -90,41 +134,28 @@ struct gpu_output_t
|
||||
glViewport (0, 0, fb_width, fb_height);
|
||||
}
|
||||
|
||||
st = demo_glstate_create ();
|
||||
vu = demo_view_create (st, window);
|
||||
renderer = demo_renderer_create_gl (window);
|
||||
}
|
||||
|
||||
glfwSetWindowUserPointer (window, this);
|
||||
glfwSetFramebufferSizeCallback (window, [] (GLFWwindow *w, int width, int height) {
|
||||
auto *self = (gpu_output_t *) glfwGetWindowUserPointer (w);
|
||||
demo_view_reshape_func (self->vu, width, height);
|
||||
});
|
||||
glfwSetKeyCallback (window, [] (GLFWwindow *w, int key, int scancode, int action, int mods) {
|
||||
auto *self = (gpu_output_t *) glfwGetWindowUserPointer (w);
|
||||
demo_view_key_func (self->vu, key, scancode, action, mods);
|
||||
});
|
||||
glfwSetCharCallback (window, [] (GLFWwindow *w, unsigned int cp) {
|
||||
auto *self = (gpu_output_t *) glfwGetWindowUserPointer (w);
|
||||
demo_view_char_func (self->vu, cp);
|
||||
});
|
||||
glfwSetMouseButtonCallback (window, [] (GLFWwindow *w, int button, int action, int mods) {
|
||||
auto *self = (gpu_output_t *) glfwGetWindowUserPointer (w);
|
||||
demo_view_mouse_func (self->vu, button, action, mods);
|
||||
});
|
||||
glfwSetScrollCallback (window, [] (GLFWwindow *w, double xoff, double yoff) {
|
||||
auto *self = (gpu_output_t *) glfwGetWindowUserPointer (w);
|
||||
demo_view_scroll_func (self->vu, xoff, yoff);
|
||||
});
|
||||
glfwSetCursorPosCallback (window, [] (GLFWwindow *w, double x, double y) {
|
||||
auto *self = (gpu_output_t *) glfwGetWindowUserPointer (w);
|
||||
demo_view_motion_func (self->vu, x, y);
|
||||
});
|
||||
void init_metal ()
|
||||
{
|
||||
#ifdef __APPLE__
|
||||
glfwWindowHint (GLFW_CLIENT_API, GLFW_NO_API);
|
||||
|
||||
demo_font_ = demo_font_create (font, demo_glstate_get_atlas (st));
|
||||
buf = demo_buffer_create ();
|
||||
window = glfwCreateWindow (WINDOW_W, WINDOW_H, "HarfBuzz GPU Demo (Metal)", NULL, NULL);
|
||||
if (!window) {
|
||||
glfwTerminate ();
|
||||
fail (false, "Failed to create GLFW window");
|
||||
}
|
||||
|
||||
demo_point_t top_left = {0, 0};
|
||||
demo_buffer_move_to (buf, &top_left);
|
||||
demo_buffer_current_line (buf, font_size);
|
||||
renderer = demo_renderer_create_metal (window);
|
||||
if (!renderer) {
|
||||
glfwTerminate ();
|
||||
fail (false, "Failed to initialize Metal");
|
||||
}
|
||||
#else
|
||||
fail (false, "Metal is only available on macOS");
|
||||
#endif
|
||||
}
|
||||
|
||||
void new_line ()
|
||||
@@ -183,7 +214,7 @@ struct gpu_output_t
|
||||
demo_buffer_destroy (buf);
|
||||
demo_font_destroy (demo_font_);
|
||||
demo_view_destroy (vu);
|
||||
demo_glstate_destroy (st);
|
||||
delete renderer;
|
||||
|
||||
glfwDestroyWindow (window);
|
||||
glfwTerminate ();
|
||||
@@ -196,7 +227,7 @@ struct gpu_output_t
|
||||
hb_face_t *face = nullptr;
|
||||
|
||||
GLFWwindow *window = nullptr;
|
||||
demo_glstate_t *st = nullptr;
|
||||
demo_renderer_t *renderer = nullptr;
|
||||
demo_view_t *vu = nullptr;
|
||||
demo_font_t *demo_font_ = nullptr;
|
||||
demo_buffer_t *buf = nullptr;
|
||||
|
||||
+30
-2
@@ -13,6 +13,9 @@
|
||||
struct demo_atlas_t {
|
||||
unsigned int refcount;
|
||||
|
||||
/* External backend (when set, GL fields are unused) */
|
||||
demo_atlas_backend_t ext;
|
||||
|
||||
GLuint tex_unit;
|
||||
GLuint tex_name;
|
||||
#ifndef HB_GPU_ATLAS_2D
|
||||
@@ -63,16 +66,28 @@ demo_atlas_reference (demo_atlas_t *at)
|
||||
return at;
|
||||
}
|
||||
|
||||
demo_atlas_t *
|
||||
demo_atlas_create_external (const demo_atlas_backend_t *backend)
|
||||
{
|
||||
demo_atlas_t *at = (demo_atlas_t *) calloc (1, sizeof (demo_atlas_t));
|
||||
at->refcount = 1;
|
||||
at->ext = *backend;
|
||||
return at;
|
||||
}
|
||||
|
||||
void
|
||||
demo_atlas_destroy (demo_atlas_t *at)
|
||||
{
|
||||
if (!at || --at->refcount)
|
||||
return;
|
||||
|
||||
glDeleteTextures (1, &at->tex_name);
|
||||
if (!at->ext.ctx)
|
||||
{
|
||||
glDeleteTextures (1, &at->tex_name);
|
||||
#ifndef HB_GPU_ATLAS_2D
|
||||
glDeleteBuffers (1, &at->buf_name);
|
||||
glDeleteBuffers (1, &at->buf_name);
|
||||
#endif
|
||||
}
|
||||
free (at);
|
||||
}
|
||||
|
||||
@@ -90,6 +105,9 @@ demo_atlas_bind_texture (demo_atlas_t *at)
|
||||
void
|
||||
demo_atlas_set_uniforms (demo_atlas_t *at)
|
||||
{
|
||||
if (at->ext.ctx)
|
||||
return; /* External backend handles uniforms separately */
|
||||
|
||||
GLuint program;
|
||||
glGetIntegerv (GL_CURRENT_PROGRAM, (GLint *) &program);
|
||||
|
||||
@@ -104,6 +122,9 @@ demo_atlas_alloc (demo_atlas_t *at,
|
||||
const char *data,
|
||||
unsigned int len_bytes)
|
||||
{
|
||||
if (at->ext.ctx)
|
||||
return at->ext.alloc (at->ext.ctx, data, len_bytes);
|
||||
|
||||
unsigned int len_texels = len_bytes / TEXEL_SIZE;
|
||||
|
||||
if (at->cursor + len_texels > at->capacity)
|
||||
@@ -147,11 +168,18 @@ demo_atlas_alloc (demo_atlas_t *at,
|
||||
unsigned int
|
||||
demo_atlas_get_used (demo_atlas_t *at)
|
||||
{
|
||||
if (at->ext.ctx)
|
||||
return at->ext.get_used (at->ext.ctx);
|
||||
return at->cursor;
|
||||
}
|
||||
|
||||
void
|
||||
demo_atlas_clear (demo_atlas_t *at)
|
||||
{
|
||||
if (at->ext.ctx)
|
||||
{
|
||||
at->ext.clear (at->ext.ctx);
|
||||
return;
|
||||
}
|
||||
at->cursor = 0;
|
||||
}
|
||||
@@ -36,4 +36,16 @@ void
|
||||
demo_atlas_set_uniforms (demo_atlas_t *at);
|
||||
|
||||
|
||||
/* Create an atlas backed by external callbacks (for Metal, etc.) */
|
||||
typedef struct {
|
||||
void *ctx;
|
||||
unsigned int (*alloc) (void *ctx, const char *data, unsigned int len_bytes);
|
||||
unsigned int (*get_used) (void *ctx);
|
||||
void (*clear) (void *ctx);
|
||||
} demo_atlas_backend_t;
|
||||
|
||||
demo_atlas_t *
|
||||
demo_atlas_create_external (const demo_atlas_backend_t *backend);
|
||||
|
||||
|
||||
#endif /* DEMO_ATLAS_H */
|
||||
+25
-4
@@ -11,6 +11,7 @@ struct demo_buffer_t {
|
||||
demo_extents_t ink_extents;
|
||||
demo_extents_t logical_extents;
|
||||
bool dirty;
|
||||
bool has_gl;
|
||||
GLuint vao_name;
|
||||
GLuint buf_name;
|
||||
};
|
||||
@@ -21,8 +22,14 @@ demo_buffer_create (void)
|
||||
demo_buffer_t *buffer = (demo_buffer_t *) calloc (1, sizeof (demo_buffer_t));
|
||||
|
||||
buffer->vertices = new std::vector<glyph_vertex_t>;
|
||||
glGenVertexArrays (1, &buffer->vao_name);
|
||||
glGenBuffers (1, &buffer->buf_name);
|
||||
|
||||
/* Only create GL objects if a GL context is available. */
|
||||
if (glfwGetCurrentContext ())
|
||||
{
|
||||
glGenVertexArrays (1, &buffer->vao_name);
|
||||
glGenBuffers (1, &buffer->buf_name);
|
||||
buffer->has_gl = true;
|
||||
}
|
||||
|
||||
demo_buffer_clear (buffer);
|
||||
|
||||
@@ -35,8 +42,11 @@ demo_buffer_destroy (demo_buffer_t *buffer)
|
||||
if (!buffer)
|
||||
return;
|
||||
|
||||
glDeleteVertexArrays (1, &buffer->vao_name);
|
||||
glDeleteBuffers (1, &buffer->buf_name);
|
||||
if (buffer->has_gl)
|
||||
{
|
||||
glDeleteVertexArrays (1, &buffer->vao_name);
|
||||
glDeleteBuffers (1, &buffer->buf_name);
|
||||
}
|
||||
delete buffer->vertices;
|
||||
free (buffer);
|
||||
}
|
||||
@@ -224,3 +234,14 @@ demo_buffer_draw (demo_buffer_t *buffer)
|
||||
glDisableVertexAttribArray (loc_glyph);
|
||||
glBindVertexArray (0);
|
||||
}
|
||||
|
||||
glyph_vertex_t *
|
||||
demo_buffer_get_vertices (demo_buffer_t *buffer,
|
||||
unsigned int *count)
|
||||
{
|
||||
if (count)
|
||||
*count = buffer->vertices->size ();
|
||||
if (buffer->vertices->empty ())
|
||||
return nullptr;
|
||||
return &(*buffer->vertices)[0];
|
||||
}
|
||||
@@ -54,5 +54,9 @@ demo_buffer_current_line (demo_buffer_t *buffer,
|
||||
void
|
||||
demo_buffer_draw (demo_buffer_t *buffer);
|
||||
|
||||
glyph_vertex_t *
|
||||
demo_buffer_get_vertices (demo_buffer_t *buffer,
|
||||
unsigned int *count);
|
||||
|
||||
|
||||
#endif /* DEMO_BUFFER_H */
|
||||
@@ -0,0 +1,467 @@
|
||||
/*
|
||||
* Copyright 2026 Behdad Esfahbod. All Rights Reserved.
|
||||
*/
|
||||
|
||||
#ifdef __APPLE__
|
||||
|
||||
#include "demo-renderer.h"
|
||||
#include "demo-atlas.h"
|
||||
#include <hb-gpu.h>
|
||||
|
||||
#import <Metal/Metal.h>
|
||||
#import <QuartzCore/CAMetalLayer.h>
|
||||
|
||||
#define GLFW_EXPOSE_NATIVE_COCOA
|
||||
#include <GLFW/glfw3native.h>
|
||||
|
||||
#define TEXEL_SIZE 8 /* sizeof short4 texel */
|
||||
|
||||
static const char *demo_metal_shader_source = R"msl(
|
||||
|
||||
struct Uniforms {
|
||||
float4x4 matViewProjection;
|
||||
float2 viewport;
|
||||
float gamma;
|
||||
float _pad1;
|
||||
float4 foreground;
|
||||
};
|
||||
|
||||
struct VertexIn {
|
||||
float2 position [[attribute(0)]];
|
||||
float2 texcoord [[attribute(1)]];
|
||||
float2 normal [[attribute(2)]];
|
||||
float emPerPos [[attribute(3)]];
|
||||
uint glyphLoc [[attribute(4)]];
|
||||
};
|
||||
|
||||
struct VertexOut {
|
||||
float4 position [[position]];
|
||||
float2 texcoord;
|
||||
uint glyphLoc [[flat]];
|
||||
};
|
||||
|
||||
vertex VertexOut vertex_main(VertexIn in [[stage_in]],
|
||||
constant Uniforms& uniforms [[buffer(1)]]) {
|
||||
float2 pos = in.position;
|
||||
float2 tex = in.texcoord;
|
||||
float4 jac = float4(in.emPerPos, 0.0, 0.0, -in.emPerPos);
|
||||
|
||||
hb_gpu_dilate(pos, tex, in.normal, jac,
|
||||
uniforms.matViewProjection, uniforms.viewport);
|
||||
|
||||
VertexOut out;
|
||||
out.position = uniforms.matViewProjection * float4(pos, 0.0, 1.0);
|
||||
out.texcoord = tex;
|
||||
out.glyphLoc = in.glyphLoc;
|
||||
return out;
|
||||
}
|
||||
|
||||
fragment float4 fragment_main(VertexOut in [[stage_in]],
|
||||
constant Uniforms& uniforms [[buffer(1)]],
|
||||
device const short4* atlas [[buffer(0)]]) {
|
||||
float coverage = hb_gpu_render(in.texcoord, in.glyphLoc, atlas);
|
||||
|
||||
if (uniforms.gamma != 1.0)
|
||||
coverage = pow(coverage, uniforms.gamma);
|
||||
|
||||
return float4(uniforms.foreground.xyz, uniforms.foreground.w * coverage);
|
||||
}
|
||||
|
||||
)msl";
|
||||
|
||||
|
||||
struct demo_renderer_metal_t : demo_renderer_t
|
||||
{
|
||||
GLFWwindow *window;
|
||||
|
||||
/* Metal objects */
|
||||
id<MTLDevice> device;
|
||||
id<MTLCommandQueue> commandQueue;
|
||||
id<MTLRenderPipelineState> pipelineState;
|
||||
id<MTLRenderPipelineState> blitPipelineState;
|
||||
CAMetalLayer *metalLayer;
|
||||
|
||||
/* Offscreen render target for vsync-off mode */
|
||||
id<MTLTexture> offscreenTexture;
|
||||
int offscreen_w, offscreen_h;
|
||||
bool vsync_on;
|
||||
double last_present_time;
|
||||
|
||||
/* Atlas (CPU shadow + GPU buffer) */
|
||||
demo_atlas_t *atlas;
|
||||
char *atlas_data;
|
||||
unsigned int atlas_capacity; /* in texels */
|
||||
unsigned int atlas_cursor; /* in texels */
|
||||
id<MTLBuffer> atlasBuffer;
|
||||
bool atlas_dirty;
|
||||
|
||||
/* Cached vertex buffer */
|
||||
id<MTLBuffer> vertexBuffer;
|
||||
glyph_vertex_t *uploaded_ptr;
|
||||
unsigned int uploaded_count;
|
||||
|
||||
/* Clear color */
|
||||
float bg[4];
|
||||
|
||||
/* Uniforms */
|
||||
struct {
|
||||
float matViewProjection[16];
|
||||
float viewport[2];
|
||||
float gamma;
|
||||
float _pad1;
|
||||
float foreground[4];
|
||||
} uniforms;
|
||||
|
||||
|
||||
demo_renderer_metal_t (GLFWwindow *window_) : window (window_)
|
||||
{
|
||||
uploaded_ptr = nullptr;
|
||||
uploaded_count = 0;
|
||||
offscreen_w = offscreen_h = 0;
|
||||
vsync_on = true;
|
||||
last_present_time = 0;
|
||||
bg[0] = bg[1] = bg[2] = bg[3] = 1.0f;
|
||||
memset (&uniforms, 0, sizeof (uniforms));
|
||||
uniforms.gamma = 1.0f;
|
||||
uniforms.foreground[3] = 1.0f;
|
||||
}
|
||||
|
||||
~demo_renderer_metal_t () override
|
||||
{
|
||||
demo_atlas_destroy (atlas);
|
||||
free (atlas_data);
|
||||
}
|
||||
|
||||
|
||||
/* -- Atlas -- */
|
||||
|
||||
static unsigned int
|
||||
atlas_alloc_cb (void *ctx, const char *data, unsigned int len_bytes)
|
||||
{
|
||||
auto *self = (demo_renderer_metal_t *) ctx;
|
||||
unsigned int len_texels = len_bytes / TEXEL_SIZE;
|
||||
if (self->atlas_cursor + len_texels > self->atlas_capacity)
|
||||
die ("Ran out of atlas memory");
|
||||
unsigned int offset = self->atlas_cursor;
|
||||
self->atlas_cursor += len_texels;
|
||||
memcpy (self->atlas_data + offset * TEXEL_SIZE, data, len_bytes);
|
||||
self->atlas_dirty = true;
|
||||
return offset;
|
||||
}
|
||||
|
||||
static unsigned int
|
||||
atlas_get_used_cb (void *ctx)
|
||||
{
|
||||
return ((demo_renderer_metal_t *) ctx)->atlas_cursor;
|
||||
}
|
||||
|
||||
static void
|
||||
atlas_clear_cb (void *ctx)
|
||||
{
|
||||
auto *self = (demo_renderer_metal_t *) ctx;
|
||||
self->atlas_cursor = 0;
|
||||
self->atlas_dirty = true;
|
||||
}
|
||||
|
||||
demo_atlas_t *get_atlas () override
|
||||
{
|
||||
return atlas;
|
||||
}
|
||||
|
||||
|
||||
/* -- State -- */
|
||||
|
||||
void setup () override {}
|
||||
|
||||
void set_gamma (float gamma) override
|
||||
{
|
||||
uniforms.gamma = gamma;
|
||||
}
|
||||
|
||||
void set_foreground (float r, float g, float b, float a) override
|
||||
{
|
||||
uniforms.foreground[0] = r;
|
||||
uniforms.foreground[1] = g;
|
||||
uniforms.foreground[2] = b;
|
||||
uniforms.foreground[3] = a;
|
||||
}
|
||||
|
||||
void set_background (float r, float g, float b, float a) override
|
||||
{
|
||||
bg[0] = r; bg[1] = g; bg[2] = b; bg[3] = a;
|
||||
}
|
||||
|
||||
bool set_srgb (bool enabled [[maybe_unused]]) override
|
||||
{
|
||||
/* Metal uses MTLPixelFormatBGRA8Unorm_sRGB; sRGB is always on. */
|
||||
return true;
|
||||
}
|
||||
|
||||
void toggle_vsync (bool &vsync) override
|
||||
{
|
||||
vsync = !vsync;
|
||||
vsync_on = vsync;
|
||||
metalLayer.displaySyncEnabled = vsync;
|
||||
}
|
||||
|
||||
void ensure_offscreen (int width, int height)
|
||||
{
|
||||
if (offscreenTexture && offscreen_w == width && offscreen_h == height)
|
||||
return;
|
||||
MTLTextureDescriptor *desc =
|
||||
[MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatBGRA8Unorm_sRGB
|
||||
width:width
|
||||
height:height
|
||||
mipmapped:NO];
|
||||
desc.usage = MTLTextureUsageRenderTarget | MTLTextureUsageShaderRead;
|
||||
desc.storageMode = MTLStorageModePrivate;
|
||||
offscreenTexture = [device newTextureWithDescriptor:desc];
|
||||
offscreen_w = width;
|
||||
offscreen_h = height;
|
||||
}
|
||||
|
||||
|
||||
/* -- Frame -- */
|
||||
|
||||
void display (glyph_vertex_t *vertices, unsigned int count,
|
||||
int width, int height, float mat[16]) override
|
||||
{
|
||||
@autoreleasepool {
|
||||
|
||||
/* Update layer size */
|
||||
CGSize size = { (CGFloat) width, (CGFloat) height };
|
||||
metalLayer.drawableSize = size;
|
||||
|
||||
/* Update uniforms */
|
||||
memcpy (uniforms.matViewProjection, mat, 16 * sizeof (float));
|
||||
uniforms.viewport[0] = (float) width;
|
||||
uniforms.viewport[1] = (float) height;
|
||||
|
||||
/* Sync atlas if dirty */
|
||||
if (atlas_dirty)
|
||||
{
|
||||
memcpy (atlasBuffer.contents, atlas_data,
|
||||
atlas_cursor * TEXEL_SIZE);
|
||||
atlas_dirty = false;
|
||||
}
|
||||
|
||||
/* Only re-upload vertices if data changed. */
|
||||
if (count > 0 && (vertices != uploaded_ptr || count != uploaded_count))
|
||||
{
|
||||
vertexBuffer = [device newBufferWithBytes:vertices
|
||||
length:count * sizeof (glyph_vertex_t)
|
||||
options:MTLResourceStorageModeShared];
|
||||
uploaded_ptr = vertices;
|
||||
uploaded_count = count;
|
||||
}
|
||||
|
||||
/*
|
||||
* When vsync is off, render to an offscreen texture (no blocking)
|
||||
* and blit to a drawable only when one is immediately available.
|
||||
* This decouples the render loop from the display refresh rate.
|
||||
*/
|
||||
if (!vsync_on)
|
||||
{
|
||||
ensure_offscreen (width, height);
|
||||
|
||||
/* Render to offscreen */
|
||||
MTLRenderPassDescriptor *passDesc = [MTLRenderPassDescriptor renderPassDescriptor];
|
||||
passDesc.colorAttachments[0].texture = offscreenTexture;
|
||||
passDesc.colorAttachments[0].loadAction = MTLLoadActionClear;
|
||||
passDesc.colorAttachments[0].storeAction = MTLStoreActionStore;
|
||||
passDesc.colorAttachments[0].clearColor = MTLClearColorMake (bg[0], bg[1], bg[2], bg[3]);
|
||||
|
||||
id<MTLCommandBuffer> commandBuffer = [commandQueue commandBuffer];
|
||||
id<MTLRenderCommandEncoder> encoder =
|
||||
[commandBuffer renderCommandEncoderWithDescriptor:passDesc];
|
||||
|
||||
[encoder setViewport:(MTLViewport){0, 0, (double) width, (double) height, 0, 1}];
|
||||
[encoder setRenderPipelineState:pipelineState];
|
||||
|
||||
if (count > 0)
|
||||
{
|
||||
[encoder setVertexBuffer:vertexBuffer offset:0 atIndex:0];
|
||||
[encoder setVertexBytes:&uniforms length:sizeof (uniforms) atIndex:1];
|
||||
[encoder setFragmentBuffer:atlasBuffer offset:0 atIndex:0];
|
||||
[encoder setFragmentBytes:&uniforms length:sizeof (uniforms) atIndex:1];
|
||||
|
||||
[encoder drawPrimitives:MTLPrimitiveTypeTriangle
|
||||
vertexStart:0
|
||||
vertexCount:count];
|
||||
}
|
||||
|
||||
[encoder endEncoding];
|
||||
|
||||
/* Present at most ~120 times/sec so the display stays live. */
|
||||
double now = glfwGetTime ();
|
||||
if (now - last_present_time > 1.0 / 120.0)
|
||||
{
|
||||
id<CAMetalDrawable> drawable = [metalLayer nextDrawable];
|
||||
if (drawable)
|
||||
{
|
||||
id<MTLBlitCommandEncoder> blit = [commandBuffer blitCommandEncoder];
|
||||
[blit copyFromTexture:offscreenTexture toTexture:drawable.texture];
|
||||
[blit endEncoding];
|
||||
[commandBuffer presentDrawable:drawable];
|
||||
last_present_time = now;
|
||||
}
|
||||
}
|
||||
|
||||
[commandBuffer commit];
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Vsync on: render directly to drawable. */
|
||||
id<CAMetalDrawable> drawable = [metalLayer nextDrawable];
|
||||
if (!drawable)
|
||||
return;
|
||||
|
||||
MTLRenderPassDescriptor *passDesc = [MTLRenderPassDescriptor renderPassDescriptor];
|
||||
passDesc.colorAttachments[0].texture = drawable.texture;
|
||||
passDesc.colorAttachments[0].loadAction = MTLLoadActionClear;
|
||||
passDesc.colorAttachments[0].storeAction = MTLStoreActionStore;
|
||||
passDesc.colorAttachments[0].clearColor = MTLClearColorMake (bg[0], bg[1], bg[2], bg[3]);
|
||||
|
||||
id<MTLCommandBuffer> commandBuffer = [commandQueue commandBuffer];
|
||||
id<MTLRenderCommandEncoder> encoder =
|
||||
[commandBuffer renderCommandEncoderWithDescriptor:passDesc];
|
||||
|
||||
[encoder setViewport:(MTLViewport){0, 0, (double) width, (double) height, 0, 1}];
|
||||
[encoder setRenderPipelineState:pipelineState];
|
||||
|
||||
if (count > 0)
|
||||
{
|
||||
[encoder setVertexBuffer:vertexBuffer offset:0 atIndex:0];
|
||||
[encoder setVertexBytes:&uniforms length:sizeof (uniforms) atIndex:1];
|
||||
[encoder setFragmentBuffer:atlasBuffer offset:0 atIndex:0];
|
||||
[encoder setFragmentBytes:&uniforms length:sizeof (uniforms) atIndex:1];
|
||||
|
||||
[encoder drawPrimitives:MTLPrimitiveTypeTriangle
|
||||
vertexStart:0
|
||||
vertexCount:count];
|
||||
}
|
||||
|
||||
[encoder endEncoding];
|
||||
[commandBuffer presentDrawable:drawable];
|
||||
[commandBuffer commit];
|
||||
}
|
||||
|
||||
} /* @autoreleasepool */
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
demo_renderer_t *
|
||||
demo_renderer_create_metal (GLFWwindow *window)
|
||||
{
|
||||
@autoreleasepool {
|
||||
|
||||
auto *r = new demo_renderer_metal_t (window);
|
||||
|
||||
/* Get Metal device */
|
||||
r->device = MTLCreateSystemDefaultDevice ();
|
||||
if (!r->device)
|
||||
{
|
||||
delete r;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
r->commandQueue = [r->device newCommandQueue];
|
||||
|
||||
/* Set up CAMetalLayer on the GLFW window */
|
||||
NSWindow *nswindow = glfwGetCocoaWindow (window);
|
||||
r->metalLayer = [CAMetalLayer layer];
|
||||
r->metalLayer.device = r->device;
|
||||
r->metalLayer.pixelFormat = MTLPixelFormatBGRA8Unorm_sRGB;
|
||||
nswindow.contentView.layer = r->metalLayer;
|
||||
nswindow.contentView.wantsLayer = YES;
|
||||
|
||||
/* Compile shaders */
|
||||
const char *vert_src = hb_gpu_shader_vertex_source (HB_GPU_SHADER_LANG_MSL);
|
||||
const char *frag_src = hb_gpu_shader_fragment_source (HB_GPU_SHADER_LANG_MSL);
|
||||
|
||||
NSString *preamble = @"#include <metal_stdlib>\nusing namespace metal;\n";
|
||||
NSString *source = [NSString stringWithFormat:@"%@%s%s%s",
|
||||
preamble, vert_src, frag_src, demo_metal_shader_source];
|
||||
|
||||
NSError *error = nil;
|
||||
id<MTLLibrary> library = [r->device newLibraryWithSource:source
|
||||
options:nil
|
||||
error:&error];
|
||||
if (!library)
|
||||
{
|
||||
fprintf (stderr, "Metal shader compilation failed: %s\n",
|
||||
[[error localizedDescription] UTF8String]);
|
||||
delete r;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
id<MTLFunction> vertexFunc = [library newFunctionWithName:@"vertex_main"];
|
||||
id<MTLFunction> fragmentFunc = [library newFunctionWithName:@"fragment_main"];
|
||||
|
||||
/* Create vertex descriptor matching glyph_vertex_t layout */
|
||||
MTLVertexDescriptor *vertexDesc = [MTLVertexDescriptor vertexDescriptor];
|
||||
vertexDesc.attributes[0].format = MTLVertexFormatFloat2;
|
||||
vertexDesc.attributes[0].offset = 0;
|
||||
vertexDesc.attributes[0].bufferIndex = 0;
|
||||
vertexDesc.attributes[1].format = MTLVertexFormatFloat2;
|
||||
vertexDesc.attributes[1].offset = 8;
|
||||
vertexDesc.attributes[1].bufferIndex = 0;
|
||||
vertexDesc.attributes[2].format = MTLVertexFormatFloat2;
|
||||
vertexDesc.attributes[2].offset = 16;
|
||||
vertexDesc.attributes[2].bufferIndex = 0;
|
||||
vertexDesc.attributes[3].format = MTLVertexFormatFloat;
|
||||
vertexDesc.attributes[3].offset = 24;
|
||||
vertexDesc.attributes[3].bufferIndex = 0;
|
||||
vertexDesc.attributes[4].format = MTLVertexFormatUInt;
|
||||
vertexDesc.attributes[4].offset = 28;
|
||||
vertexDesc.attributes[4].bufferIndex = 0;
|
||||
vertexDesc.layouts[0].stride = 32;
|
||||
|
||||
/* Create pipeline */
|
||||
MTLRenderPipelineDescriptor *pipelineDesc = [[MTLRenderPipelineDescriptor alloc] init];
|
||||
pipelineDesc.vertexFunction = vertexFunc;
|
||||
pipelineDesc.fragmentFunction = fragmentFunc;
|
||||
pipelineDesc.vertexDescriptor = vertexDesc;
|
||||
pipelineDesc.colorAttachments[0].pixelFormat = MTLPixelFormatBGRA8Unorm_sRGB;
|
||||
pipelineDesc.colorAttachments[0].blendingEnabled = YES;
|
||||
pipelineDesc.colorAttachments[0].sourceRGBBlendFactor = MTLBlendFactorSourceAlpha;
|
||||
pipelineDesc.colorAttachments[0].destinationRGBBlendFactor = MTLBlendFactorOneMinusSourceAlpha;
|
||||
pipelineDesc.colorAttachments[0].sourceAlphaBlendFactor = MTLBlendFactorSourceAlpha;
|
||||
pipelineDesc.colorAttachments[0].destinationAlphaBlendFactor = MTLBlendFactorOneMinusSourceAlpha;
|
||||
|
||||
error = nil;
|
||||
r->pipelineState = [r->device newRenderPipelineStateWithDescriptor:pipelineDesc
|
||||
error:&error];
|
||||
if (!r->pipelineState)
|
||||
{
|
||||
fprintf (stderr, "Metal pipeline creation failed: %s\n",
|
||||
[[error localizedDescription] UTF8String]);
|
||||
delete r;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
/* Atlas */
|
||||
unsigned int atlas_capacity = 1024 * 1024;
|
||||
r->atlas_capacity = atlas_capacity;
|
||||
r->atlas_cursor = 0;
|
||||
r->atlas_data = (char *) calloc (atlas_capacity, TEXEL_SIZE);
|
||||
r->atlasBuffer = [r->device newBufferWithLength:atlas_capacity * TEXEL_SIZE
|
||||
options:MTLResourceStorageModeShared];
|
||||
r->atlas_dirty = false;
|
||||
|
||||
/* Create atlas wrapper for demo_font_t */
|
||||
demo_atlas_backend_t backend = {
|
||||
r,
|
||||
demo_renderer_metal_t::atlas_alloc_cb,
|
||||
demo_renderer_metal_t::atlas_get_used_cb,
|
||||
demo_renderer_metal_t::atlas_clear_cb,
|
||||
};
|
||||
r->atlas = demo_atlas_create_external (&backend);
|
||||
|
||||
return r;
|
||||
|
||||
} /* @autoreleasepool */
|
||||
}
|
||||
|
||||
#endif /* __APPLE__ */
|
||||
@@ -0,0 +1,167 @@
|
||||
/*
|
||||
* Copyright 2026 Behdad Esfahbod. All Rights Reserved.
|
||||
*/
|
||||
|
||||
#include "demo-renderer.h"
|
||||
#include "demo-atlas.h"
|
||||
#include "demo-glstate.h"
|
||||
|
||||
|
||||
struct demo_renderer_gl_t : demo_renderer_t
|
||||
{
|
||||
GLFWwindow *window;
|
||||
demo_glstate_t *st;
|
||||
GLuint vao_name;
|
||||
GLuint buf_name;
|
||||
bool vao_ready;
|
||||
unsigned int uploaded_count;
|
||||
glyph_vertex_t *uploaded_ptr;
|
||||
|
||||
demo_renderer_gl_t (GLFWwindow *window_)
|
||||
: window (window_), vao_ready (false),
|
||||
uploaded_count (0), uploaded_ptr (nullptr)
|
||||
{
|
||||
st = demo_glstate_create ();
|
||||
glGenVertexArrays (1, &vao_name);
|
||||
glGenBuffers (1, &buf_name);
|
||||
}
|
||||
|
||||
~demo_renderer_gl_t () override
|
||||
{
|
||||
glDeleteVertexArrays (1, &vao_name);
|
||||
glDeleteBuffers (1, &buf_name);
|
||||
demo_glstate_destroy (st);
|
||||
}
|
||||
|
||||
void setup_vao ()
|
||||
{
|
||||
if (vao_ready)
|
||||
return;
|
||||
vao_ready = true;
|
||||
|
||||
GLint program;
|
||||
glGetIntegerv (GL_CURRENT_PROGRAM, &program);
|
||||
|
||||
glBindVertexArray (vao_name);
|
||||
glBindBuffer (GL_ARRAY_BUFFER, buf_name);
|
||||
|
||||
GLsizei stride = sizeof (glyph_vertex_t);
|
||||
|
||||
GLint loc;
|
||||
loc = glGetAttribLocation (program, "a_position");
|
||||
glEnableVertexAttribArray (loc);
|
||||
glVertexAttribPointer (loc, 2, GL_FLOAT, GL_FALSE, stride,
|
||||
(const void *) offsetof (glyph_vertex_t, x));
|
||||
|
||||
loc = glGetAttribLocation (program, "a_texcoord");
|
||||
glEnableVertexAttribArray (loc);
|
||||
glVertexAttribPointer (loc, 2, GL_FLOAT, GL_FALSE, stride,
|
||||
(const void *) offsetof (glyph_vertex_t, tx));
|
||||
|
||||
loc = glGetAttribLocation (program, "a_normal");
|
||||
glEnableVertexAttribArray (loc);
|
||||
glVertexAttribPointer (loc, 2, GL_FLOAT, GL_FALSE, stride,
|
||||
(const void *) offsetof (glyph_vertex_t, nx));
|
||||
|
||||
loc = glGetAttribLocation (program, "a_emPerPos");
|
||||
glEnableVertexAttribArray (loc);
|
||||
glVertexAttribPointer (loc, 1, GL_FLOAT, GL_FALSE, stride,
|
||||
(const void *) offsetof (glyph_vertex_t, emPerPos));
|
||||
|
||||
loc = glGetAttribLocation (program, "a_glyphLoc");
|
||||
glEnableVertexAttribArray (loc);
|
||||
glVertexAttribIPointer (loc, 1, GL_UNSIGNED_INT, stride,
|
||||
(const void *) offsetof (glyph_vertex_t, atlas_offset));
|
||||
|
||||
glBindVertexArray (0);
|
||||
}
|
||||
|
||||
demo_atlas_t *get_atlas () override
|
||||
{
|
||||
return demo_glstate_get_atlas (st);
|
||||
}
|
||||
|
||||
void setup () override
|
||||
{
|
||||
demo_glstate_setup (st);
|
||||
}
|
||||
|
||||
void set_gamma (float gamma) override
|
||||
{
|
||||
demo_glstate_set_gamma (st, gamma);
|
||||
}
|
||||
|
||||
void set_foreground (float r, float g, float b, float a) override
|
||||
{
|
||||
demo_glstate_set_foreground (st, r, g, b, a);
|
||||
}
|
||||
|
||||
void set_background (float r, float g, float b, float a) override
|
||||
{
|
||||
glClearColor (r, g, b, a);
|
||||
}
|
||||
|
||||
bool set_srgb (bool enabled) override
|
||||
{
|
||||
#if defined(GL_FRAMEBUFFER_SRGB)
|
||||
while (glGetError () != GL_NO_ERROR)
|
||||
;
|
||||
if (enabled)
|
||||
{
|
||||
glEnable (GL_FRAMEBUFFER_SRGB);
|
||||
if (glGetError () != GL_NO_ERROR)
|
||||
return false;
|
||||
}
|
||||
else
|
||||
glDisable (GL_FRAMEBUFFER_SRGB);
|
||||
return true;
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
void toggle_vsync (bool &vsync) override
|
||||
{
|
||||
vsync = !vsync;
|
||||
glfwSwapInterval (vsync ? 1 : 0);
|
||||
}
|
||||
|
||||
void display (glyph_vertex_t *vertices, unsigned int count,
|
||||
int width, int height, float mat[16]) override
|
||||
{
|
||||
glViewport (0, 0, width, height);
|
||||
demo_glstate_set_matrix (st, mat);
|
||||
|
||||
glClear (GL_COLOR_BUFFER_BIT);
|
||||
|
||||
if (count > 0)
|
||||
{
|
||||
setup_vao ();
|
||||
|
||||
glBindVertexArray (vao_name);
|
||||
|
||||
if (vertices != uploaded_ptr || count != uploaded_count)
|
||||
{
|
||||
glBindBuffer (GL_ARRAY_BUFFER, buf_name);
|
||||
glBufferData (GL_ARRAY_BUFFER,
|
||||
sizeof (glyph_vertex_t) * count,
|
||||
(const char *) vertices, GL_STATIC_DRAW);
|
||||
uploaded_ptr = vertices;
|
||||
uploaded_count = count;
|
||||
}
|
||||
|
||||
glDrawArrays (GL_TRIANGLES, 0, count);
|
||||
|
||||
glBindVertexArray (0);
|
||||
}
|
||||
|
||||
glfwSwapBuffers (window);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
demo_renderer_t *
|
||||
demo_renderer_create_gl (GLFWwindow *window)
|
||||
{
|
||||
return new demo_renderer_gl_t (window);
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright 2026 Behdad Esfahbod. All Rights Reserved.
|
||||
*/
|
||||
|
||||
#ifndef DEMO_RENDERER_H
|
||||
#define DEMO_RENDERER_H
|
||||
|
||||
#include "demo-common.h"
|
||||
#include "demo-shader.h"
|
||||
|
||||
typedef struct demo_atlas_t demo_atlas_t;
|
||||
|
||||
|
||||
struct demo_renderer_t
|
||||
{
|
||||
virtual ~demo_renderer_t () {}
|
||||
|
||||
/* Atlas for glyph data upload. */
|
||||
virtual demo_atlas_t *get_atlas () = 0;
|
||||
|
||||
/* One-time setup after atlas is populated. */
|
||||
virtual void setup () = 0;
|
||||
|
||||
/* Rendering state. */
|
||||
virtual void set_gamma (float gamma) = 0;
|
||||
virtual void set_foreground (float r, float g, float b, float a) = 0;
|
||||
virtual void set_background (float r, float g, float b, float a) = 0;
|
||||
|
||||
/* sRGB framebuffer control; returns false if unsupported. */
|
||||
virtual bool set_srgb (bool enabled) = 0;
|
||||
|
||||
/* Vsync toggle. */
|
||||
virtual void toggle_vsync (bool &vsync) = 0;
|
||||
|
||||
/* Render a frame. */
|
||||
virtual void display (glyph_vertex_t *vertices, unsigned int count,
|
||||
int width, int height, float mat[16]) = 0;
|
||||
};
|
||||
|
||||
|
||||
demo_renderer_t *
|
||||
demo_renderer_create_gl (GLFWwindow *window);
|
||||
|
||||
#ifdef __APPLE__
|
||||
demo_renderer_t *
|
||||
demo_renderer_create_metal (GLFWwindow *window);
|
||||
#endif
|
||||
|
||||
|
||||
#endif /* DEMO_RENDERER_H */
|
||||
+28
-49
@@ -9,13 +9,13 @@
|
||||
#include "matrix4x4.hh"
|
||||
|
||||
struct demo_view_t {
|
||||
demo_glstate_t *st;
|
||||
demo_renderer_t *renderer;
|
||||
GLFWwindow *window;
|
||||
|
||||
/* Output */
|
||||
GLint vsync;
|
||||
hb_bool_t fullscreen;
|
||||
hb_bool_t dark_mode;
|
||||
bool vsync;
|
||||
bool fullscreen;
|
||||
bool dark_mode;
|
||||
enum { GAMMA_SRGB, GAMMA_2_2, GAMMA_NONE } gamma_mode;
|
||||
|
||||
/* Mouse handling */
|
||||
@@ -60,11 +60,11 @@ struct demo_view_t {
|
||||
};
|
||||
|
||||
demo_view_t *
|
||||
demo_view_create (demo_glstate_t *st, GLFWwindow *window)
|
||||
demo_view_create (demo_renderer_t *renderer, GLFWwindow *window)
|
||||
{
|
||||
demo_view_t *vu = (demo_view_t *) calloc (1, sizeof (demo_view_t));
|
||||
|
||||
vu->st = st;
|
||||
vu->renderer = renderer;
|
||||
vu->window = window;
|
||||
vu->needs_redraw = true;
|
||||
demo_view_reset (vu);
|
||||
@@ -130,12 +130,8 @@ demo_view_translate (demo_view_t *vu, double dx, double dy)
|
||||
}
|
||||
|
||||
static void
|
||||
demo_view_apply_transform (demo_view_t *vu, float *mat)
|
||||
demo_view_apply_transform (demo_view_t *vu, float *mat, int width, int height)
|
||||
{
|
||||
int viewport[4];
|
||||
glGetIntegerv (GL_VIEWPORT, viewport);
|
||||
GLint width = viewport[2];
|
||||
GLint height = viewport[3];
|
||||
|
||||
m4Scale (mat, vu->scalex, vu->scaley, 1);
|
||||
m4Translate (mat, vu->translate.x, vu->translate.y, 0);
|
||||
@@ -168,7 +164,7 @@ start_animation (demo_view_t *vu)
|
||||
{
|
||||
vu->num_frames = 0;
|
||||
vu->last_frame_time = vu->fps_start_time = current_time ();
|
||||
vu->fps_timer_interval = 5.0;
|
||||
vu->fps_timer_interval = 2.0;
|
||||
vu->fps_timer_last = current_time ();
|
||||
vu->has_fps_timer = true;
|
||||
}
|
||||
@@ -186,41 +182,24 @@ demo_view_toggle_animation (demo_view_t *vu)
|
||||
static void
|
||||
demo_view_toggle_vsync (demo_view_t *vu)
|
||||
{
|
||||
GLint vsync = !vu->vsync;
|
||||
glfwSwapInterval (vsync);
|
||||
vu->vsync = vsync;
|
||||
vu->renderer->toggle_vsync (vu->vsync);
|
||||
LOGI ("Setting vsync %s.\n", vu->vsync ? "on" : "off");
|
||||
}
|
||||
|
||||
static void
|
||||
demo_view_set_gamma_mode (demo_view_t *vu, int mode)
|
||||
{
|
||||
/* Disable previous sRGB if any. */
|
||||
#if defined(GL_FRAMEBUFFER_SRGB)
|
||||
glDisable (GL_FRAMEBUFFER_SRGB);
|
||||
while (glGetError () != GL_NO_ERROR)
|
||||
;
|
||||
#endif
|
||||
vu->renderer->set_srgb (false);
|
||||
|
||||
if (mode == demo_view_t::GAMMA_SRGB)
|
||||
{
|
||||
#if defined(GL_FRAMEBUFFER_SRGB)
|
||||
while (glGetError () != GL_NO_ERROR)
|
||||
;
|
||||
glEnable (GL_FRAMEBUFFER_SRGB);
|
||||
if (glGetError () != GL_NO_ERROR)
|
||||
{
|
||||
LOGW ("sRGB framebuffer not available.\n");
|
||||
if (!vu->renderer->set_srgb (true))
|
||||
mode = demo_view_t::GAMMA_2_2;
|
||||
}
|
||||
#else
|
||||
mode = demo_view_t::GAMMA_2_2;
|
||||
#endif
|
||||
}
|
||||
|
||||
vu->gamma_mode = (decltype(vu->gamma_mode)) mode;
|
||||
float gamma = mode == demo_view_t::GAMMA_2_2 ? (vu->dark_mode ? 1.f/2.2f : 2.2f) : 1.f;
|
||||
demo_glstate_set_gamma (vu->st, gamma);
|
||||
vu->renderer->set_gamma (gamma);
|
||||
|
||||
const char *names[] = {"sRGB", "gamma 2.2", "none"};
|
||||
LOGI ("Gamma correction: %s.\n", names[mode]);
|
||||
@@ -243,10 +222,17 @@ static void
|
||||
demo_view_toggle_dark (demo_view_t *vu)
|
||||
{
|
||||
vu->dark_mode = !vu->dark_mode;
|
||||
LOGI ("Setting %s mode.\n", vu->dark_mode ? "dark" : "light");
|
||||
if (vu->dark_mode)
|
||||
demo_glstate_set_foreground (vu->st, DARK_FG);
|
||||
{
|
||||
vu->renderer->set_foreground (DARK_FG);
|
||||
vu->renderer->set_background (DARK_BG);
|
||||
}
|
||||
else
|
||||
demo_glstate_set_foreground (vu->st, LIGHT_FG);
|
||||
{
|
||||
vu->renderer->set_foreground (LIGHT_FG);
|
||||
vu->renderer->set_background (LIGHT_BG);
|
||||
}
|
||||
demo_view_set_gamma_mode (vu, vu->gamma_mode);
|
||||
vu->needs_redraw = true;
|
||||
}
|
||||
@@ -580,14 +566,12 @@ demo_view_display (demo_view_t *vu, demo_buffer_t *buffer)
|
||||
|
||||
int width, height;
|
||||
glfwGetFramebufferSize (vu->window, &width, &height);
|
||||
glViewport (0, 0, width, height);
|
||||
|
||||
|
||||
float mat[16];
|
||||
|
||||
m4LoadIdentity (mat);
|
||||
|
||||
demo_view_apply_transform (vu, mat);
|
||||
demo_view_apply_transform (vu, mat, width, height);
|
||||
|
||||
demo_extents_t extents;
|
||||
demo_buffer_extents (buffer, NULL, &extents);
|
||||
@@ -598,17 +582,10 @@ demo_view_display (demo_view_t *vu, demo_buffer_t *buffer)
|
||||
-(extents.max_x + extents.min_x) / 2.,
|
||||
-(extents.max_y + extents.min_y) / 2., 0);
|
||||
|
||||
demo_glstate_set_matrix (vu->st, mat);
|
||||
unsigned int count;
|
||||
glyph_vertex_t *verts = demo_buffer_get_vertices (buffer, &count);
|
||||
vu->renderer->display (verts, count, width, height, mat);
|
||||
|
||||
if (vu->dark_mode)
|
||||
glClearColor (DARK_BG);
|
||||
else
|
||||
glClearColor (LIGHT_BG);
|
||||
glClear (GL_COLOR_BUFFER_BIT);
|
||||
|
||||
demo_buffer_draw (buffer);
|
||||
|
||||
glfwSwapBuffers (vu->window);
|
||||
vu->needs_redraw = false;
|
||||
}
|
||||
|
||||
@@ -617,7 +594,9 @@ demo_view_setup (demo_view_t *vu)
|
||||
{
|
||||
if (!vu->vsync)
|
||||
demo_view_toggle_vsync (vu);
|
||||
demo_glstate_setup (vu->st);
|
||||
vu->renderer->setup ();
|
||||
vu->renderer->set_foreground (LIGHT_FG);
|
||||
vu->renderer->set_background (LIGHT_BG);
|
||||
demo_view_set_gamma_mode (vu, demo_view_t::GAMMA_SRGB);
|
||||
}
|
||||
|
||||
|
||||
@@ -9,12 +9,12 @@
|
||||
|
||||
#include "demo-common.h"
|
||||
#include "demo-buffer.h"
|
||||
#include "demo-glstate.h"
|
||||
#include "demo-renderer.h"
|
||||
|
||||
typedef struct demo_view_t demo_view_t;
|
||||
|
||||
demo_view_t *
|
||||
demo_view_create (demo_glstate_t *st, GLFWwindow *window);
|
||||
demo_view_create (demo_renderer_t *renderer, GLFWwindow *window);
|
||||
|
||||
void
|
||||
demo_view_destroy (demo_view_t *vu);
|
||||
|
||||
@@ -8,6 +8,8 @@ gpu_demo_sources = files(
|
||||
'demo-font.cc',
|
||||
'demo-glstate.h',
|
||||
'demo-glstate.cc',
|
||||
'demo-renderer.h',
|
||||
'demo-renderer-gl.cc',
|
||||
'demo-shader.h',
|
||||
'demo-shader.cc',
|
||||
'demo-view.h',
|
||||
@@ -21,11 +23,19 @@ gpu_demo_sources = files(
|
||||
'trackball.hh',
|
||||
)
|
||||
|
||||
if host_machine.system() == 'darwin'
|
||||
gpu_demo_sources += files(
|
||||
'demo-metal.mm',
|
||||
)
|
||||
endif
|
||||
|
||||
glew_dep = dependency('glew', required: false)
|
||||
glfw_dep = dependency('glfw3', required: false)
|
||||
if host_machine.system() == 'darwin'
|
||||
gl_dep = dependency('appleframeworks', modules: ['OpenGL'], required: false)
|
||||
metal_dep = dependency('appleframeworks', modules: ['Metal', 'QuartzCore', 'Foundation', 'AppKit'], required: false)
|
||||
else
|
||||
gl_dep = dependency('gl', required: false)
|
||||
metal_dep = disabler()
|
||||
endif
|
||||
have_gpu_demo_deps = glew_dep.found() and glfw_dep.found() and gl_dep.found()
|
||||
@@ -156,6 +156,18 @@ struct gpu_main_t : base_t
|
||||
add_options ();
|
||||
add_exit_code (RETURN_VALUE_OPERATION_FAILED, "Operation failed.");
|
||||
|
||||
/* Check for --metal before GLib eats the options. */
|
||||
for (int i = 1; i < argc; i++)
|
||||
if (!strcmp (argv[i], "--metal"))
|
||||
{
|
||||
this->output.use_metal = true;
|
||||
/* Remove from argv so GLib doesn't complain. */
|
||||
for (int j = i; j < argc - 1; j++)
|
||||
argv[j] = argv[j + 1];
|
||||
argc--;
|
||||
i--;
|
||||
}
|
||||
|
||||
parse (&argc, &argv);
|
||||
|
||||
/* If no text was provided, build from font's supported scripts. */
|
||||
|
||||
+8
-1
@@ -215,14 +215,21 @@ else
|
||||
endif
|
||||
|
||||
if not get_option('gpu').disabled()
|
||||
if host_machine.system() == 'darwin'
|
||||
add_languages('objcpp', required: true)
|
||||
endif
|
||||
subdir('gpu')
|
||||
|
||||
if have_gpu_demo_deps and conf.get('HAVE_GLIB', 0) == 1
|
||||
gpu_demo_deps = [util_deps, gl_dep, glew_dep, glfw_dep]
|
||||
if host_machine.system() == 'darwin'
|
||||
gpu_demo_deps += metal_dep
|
||||
endif
|
||||
hb_gpu_util = executable('hb-gpu-demo',
|
||||
['hb-gpu-demo.cc'] + gpu_demo_sources,
|
||||
cpp_args: cpp_args,
|
||||
include_directories: [incconfig, incsrc],
|
||||
dependencies: [util_deps, gl_dep, glew_dep, glfw_dep],
|
||||
dependencies: gpu_demo_deps,
|
||||
link_with: [libharfbuzz, libharfbuzz_gpu],
|
||||
install: false,
|
||||
)
|
||||
|
||||
Reference in new issue
Block a user