mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-20 22:41:10 +01:00
[gpu] Add Metal backend for macOS demo
Add MSL shader translations of the Slug GPU text rasterizer, and a Metal rendering backend for hb-gpu-demo selectable via --metal flag. Library changes: - Add HB_GPU_SHADER_LANG_MSL enum value - Add hb-gpu-vertex.msl and hb-gpu-fragment.msl - Extend gen-gpu-shader-artifacts.py for MSL Demo changes: - Add demo-metal.h/mm with CAMetalLayer rendering - Add external atlas backend for non-GL paths - Add demo_buffer_get_vertices for Metal draw path - Skip GL init when using Metal (GLFW_NO_API) - Add --metal command-line flag Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
0e97de7909
commit
6910b9a3ed
20 files changed
+1319
-75
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',
|
||||
|
||||
+90
-32
@@ -32,6 +32,7 @@
|
||||
#include "gpu/demo-font.h"
|
||||
#include "gpu/demo-glstate.h"
|
||||
#include "gpu/demo-view.h"
|
||||
#include "gpu/demo-metal.h"
|
||||
|
||||
#include "options.hh"
|
||||
#include "font-options.hh"
|
||||
@@ -45,6 +46,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 +58,77 @@ 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 ();
|
||||
|
||||
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_atlas_t *atlas;
|
||||
if (metal)
|
||||
{
|
||||
static demo_atlas_backend_t metal_backend = {
|
||||
nullptr,
|
||||
[] (void *ctx, const char *data, unsigned int len) -> unsigned int {
|
||||
return demo_metal_atlas_alloc ((demo_metal_t *) ctx, data, len);
|
||||
},
|
||||
[] (void *ctx) -> unsigned int {
|
||||
return demo_metal_atlas_get_used ((demo_metal_t *) ctx);
|
||||
},
|
||||
[] (void *ctx) {
|
||||
demo_metal_atlas_clear ((demo_metal_t *) ctx);
|
||||
},
|
||||
};
|
||||
metal_backend.ctx = metal;
|
||||
atlas = demo_atlas_create_external (&metal_backend);
|
||||
}
|
||||
else
|
||||
atlas = demo_glstate_get_atlas (st);
|
||||
|
||||
demo_font_ = demo_font_create (font, atlas);
|
||||
if (metal)
|
||||
demo_atlas_destroy (atlas); /* font holds a ref */
|
||||
|
||||
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);
|
||||
@@ -92,39 +159,28 @@ struct gpu_output_t
|
||||
|
||||
st = demo_glstate_create ();
|
||||
vu = demo_view_create (st, 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);
|
||||
metal = demo_metal_create (window, 1024 * 1024);
|
||||
if (!metal) {
|
||||
glfwTerminate ();
|
||||
fail (false, "Failed to initialize Metal");
|
||||
}
|
||||
vu = demo_view_create_metal (metal, window);
|
||||
#else
|
||||
fail (false, "Metal is only available on macOS");
|
||||
#endif
|
||||
}
|
||||
|
||||
void new_line ()
|
||||
@@ -183,7 +239,8 @@ struct gpu_output_t
|
||||
demo_buffer_destroy (buf);
|
||||
demo_font_destroy (demo_font_);
|
||||
demo_view_destroy (vu);
|
||||
demo_glstate_destroy (st);
|
||||
if (st) demo_glstate_destroy (st);
|
||||
if (metal) demo_metal_destroy (metal);
|
||||
|
||||
glfwDestroyWindow (window);
|
||||
glfwTerminate ();
|
||||
@@ -197,6 +254,7 @@ struct gpu_output_t
|
||||
|
||||
GLFWwindow *window = nullptr;
|
||||
demo_glstate_t *st = nullptr;
|
||||
demo_metal_t *metal = 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,48 @@
|
||||
/*
|
||||
* Copyright 2026 Behdad Esfahbod. All Rights Reserved.
|
||||
*/
|
||||
|
||||
#ifndef DEMO_METAL_H
|
||||
#define DEMO_METAL_H
|
||||
|
||||
#include "demo-common.h"
|
||||
#include "demo-shader.h" /* for glyph_vertex_t */
|
||||
|
||||
typedef struct demo_metal_t demo_metal_t;
|
||||
|
||||
demo_metal_t *
|
||||
demo_metal_create (GLFWwindow *window, unsigned int atlas_capacity);
|
||||
|
||||
void
|
||||
demo_metal_destroy (demo_metal_t *mt);
|
||||
|
||||
/* Atlas allocation -- same interface as demo_atlas_t */
|
||||
unsigned int
|
||||
demo_metal_atlas_alloc (demo_metal_t *mt,
|
||||
const char *data,
|
||||
unsigned int len_bytes);
|
||||
|
||||
unsigned int
|
||||
demo_metal_atlas_get_used (demo_metal_t *mt);
|
||||
|
||||
void
|
||||
demo_metal_atlas_clear (demo_metal_t *mt);
|
||||
|
||||
/* Rendering state */
|
||||
void
|
||||
demo_metal_set_gamma (demo_metal_t *mt, float gamma);
|
||||
|
||||
void
|
||||
demo_metal_set_foreground (demo_metal_t *mt,
|
||||
float r, float g, float b, float a);
|
||||
|
||||
/* Frame rendering */
|
||||
void
|
||||
demo_metal_display (demo_metal_t *mt,
|
||||
glyph_vertex_t *vertices,
|
||||
unsigned int vertex_count,
|
||||
int width,
|
||||
int height,
|
||||
float mat[16]);
|
||||
|
||||
#endif /* DEMO_METAL_H */
|
||||
@@ -0,0 +1,348 @@
|
||||
/*
|
||||
* Copyright 2026 Behdad Esfahbod. All Rights Reserved.
|
||||
*/
|
||||
|
||||
#ifdef __APPLE__
|
||||
|
||||
#include "demo-metal.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_metal_t {
|
||||
GLFWwindow *window;
|
||||
|
||||
/* Metal objects */
|
||||
id<MTLDevice> device;
|
||||
id<MTLCommandQueue> commandQueue;
|
||||
id<MTLRenderPipelineState> pipelineState;
|
||||
CAMetalLayer *metalLayer;
|
||||
|
||||
/* Atlas (CPU + GPU buffer) */
|
||||
char *atlas_data;
|
||||
unsigned int atlas_capacity; /* in texels */
|
||||
unsigned int atlas_cursor; /* in texels */
|
||||
id<MTLBuffer> atlasBuffer;
|
||||
bool atlas_dirty;
|
||||
|
||||
/* Uniforms */
|
||||
struct {
|
||||
float matViewProjection[16];
|
||||
float viewport[2];
|
||||
float gamma;
|
||||
float _pad1;
|
||||
float foreground[4];
|
||||
} uniforms;
|
||||
};
|
||||
|
||||
|
||||
demo_metal_t *
|
||||
demo_metal_create (GLFWwindow *window, unsigned int atlas_capacity)
|
||||
{
|
||||
@autoreleasepool {
|
||||
|
||||
demo_metal_t *mt = (demo_metal_t *) calloc (1, sizeof (demo_metal_t));
|
||||
mt->window = window;
|
||||
|
||||
/* Get Metal device */
|
||||
mt->device = MTLCreateSystemDefaultDevice ();
|
||||
if (!mt->device)
|
||||
{
|
||||
free (mt);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
mt->commandQueue = [mt->device newCommandQueue];
|
||||
|
||||
/* Set up CAMetalLayer on the GLFW window */
|
||||
NSWindow *nswindow = glfwGetCocoaWindow (window);
|
||||
mt->metalLayer = [CAMetalLayer layer];
|
||||
mt->metalLayer.device = mt->device;
|
||||
mt->metalLayer.pixelFormat = MTLPixelFormatBGRA8Unorm_sRGB;
|
||||
nswindow.contentView.layer = mt->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 = [mt->device newLibraryWithSource:source
|
||||
options:nil
|
||||
error:&error];
|
||||
if (!library)
|
||||
{
|
||||
fprintf (stderr, "Metal shader compilation failed: %s\n",
|
||||
[[error localizedDescription] UTF8String]);
|
||||
free (mt);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
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];
|
||||
/* position: float2 at offset 0 */
|
||||
vertexDesc.attributes[0].format = MTLVertexFormatFloat2;
|
||||
vertexDesc.attributes[0].offset = 0;
|
||||
vertexDesc.attributes[0].bufferIndex = 0;
|
||||
/* texcoord: float2 at offset 8 */
|
||||
vertexDesc.attributes[1].format = MTLVertexFormatFloat2;
|
||||
vertexDesc.attributes[1].offset = 8;
|
||||
vertexDesc.attributes[1].bufferIndex = 0;
|
||||
/* normal: float2 at offset 16 */
|
||||
vertexDesc.attributes[2].format = MTLVertexFormatFloat2;
|
||||
vertexDesc.attributes[2].offset = 16;
|
||||
vertexDesc.attributes[2].bufferIndex = 0;
|
||||
/* emPerPos: float at offset 24 */
|
||||
vertexDesc.attributes[3].format = MTLVertexFormatFloat;
|
||||
vertexDesc.attributes[3].offset = 24;
|
||||
vertexDesc.attributes[3].bufferIndex = 0;
|
||||
/* glyphLoc: uint at offset 28 */
|
||||
vertexDesc.attributes[4].format = MTLVertexFormatUInt;
|
||||
vertexDesc.attributes[4].offset = 28;
|
||||
vertexDesc.attributes[4].bufferIndex = 0;
|
||||
/* stride = sizeof(glyph_vertex_t) = 32 */
|
||||
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;
|
||||
/* Alpha blending: SRC_ALPHA, ONE_MINUS_SRC_ALPHA */
|
||||
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;
|
||||
mt->pipelineState = [mt->device newRenderPipelineStateWithDescriptor:pipelineDesc
|
||||
error:&error];
|
||||
if (!mt->pipelineState)
|
||||
{
|
||||
fprintf (stderr, "Metal pipeline creation failed: %s\n",
|
||||
[[error localizedDescription] UTF8String]);
|
||||
free (mt);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Atlas */
|
||||
mt->atlas_capacity = atlas_capacity;
|
||||
mt->atlas_cursor = 0;
|
||||
mt->atlas_data = (char *) calloc (atlas_capacity, TEXEL_SIZE);
|
||||
mt->atlasBuffer = [mt->device newBufferWithLength:atlas_capacity * TEXEL_SIZE
|
||||
options:MTLResourceStorageModeShared];
|
||||
mt->atlas_dirty = false;
|
||||
|
||||
/* Default uniforms */
|
||||
mt->uniforms.gamma = 1.0f;
|
||||
mt->uniforms.foreground[0] = 0.0f;
|
||||
mt->uniforms.foreground[1] = 0.0f;
|
||||
mt->uniforms.foreground[2] = 0.0f;
|
||||
mt->uniforms.foreground[3] = 1.0f;
|
||||
|
||||
return mt;
|
||||
|
||||
} /* @autoreleasepool */
|
||||
}
|
||||
|
||||
void
|
||||
demo_metal_destroy (demo_metal_t *mt)
|
||||
{
|
||||
if (!mt)
|
||||
return;
|
||||
|
||||
free (mt->atlas_data);
|
||||
free (mt);
|
||||
}
|
||||
|
||||
|
||||
unsigned int
|
||||
demo_metal_atlas_alloc (demo_metal_t *mt,
|
||||
const char *data,
|
||||
unsigned int len_bytes)
|
||||
{
|
||||
unsigned int len_texels = len_bytes / TEXEL_SIZE;
|
||||
|
||||
if (mt->atlas_cursor + len_texels > mt->atlas_capacity)
|
||||
die ("Ran out of atlas memory");
|
||||
|
||||
unsigned int offset = mt->atlas_cursor;
|
||||
mt->atlas_cursor += len_texels;
|
||||
|
||||
memcpy (mt->atlas_data + offset * TEXEL_SIZE, data, len_bytes);
|
||||
mt->atlas_dirty = true;
|
||||
|
||||
return offset;
|
||||
}
|
||||
|
||||
unsigned int
|
||||
demo_metal_atlas_get_used (demo_metal_t *mt)
|
||||
{
|
||||
return mt->atlas_cursor;
|
||||
}
|
||||
|
||||
void
|
||||
demo_metal_atlas_clear (demo_metal_t *mt)
|
||||
{
|
||||
mt->atlas_cursor = 0;
|
||||
mt->atlas_dirty = true;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
demo_metal_set_gamma (demo_metal_t *mt, float gamma)
|
||||
{
|
||||
mt->uniforms.gamma = gamma;
|
||||
}
|
||||
|
||||
void
|
||||
demo_metal_set_foreground (demo_metal_t *mt,
|
||||
float r, float g, float b, float a)
|
||||
{
|
||||
mt->uniforms.foreground[0] = r;
|
||||
mt->uniforms.foreground[1] = g;
|
||||
mt->uniforms.foreground[2] = b;
|
||||
mt->uniforms.foreground[3] = a;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
demo_metal_display (demo_metal_t *mt,
|
||||
glyph_vertex_t *vertices,
|
||||
unsigned int vertex_count,
|
||||
int width,
|
||||
int height,
|
||||
float mat[16])
|
||||
{
|
||||
@autoreleasepool {
|
||||
|
||||
/* Update layer size */
|
||||
CGSize size = { (CGFloat) width, (CGFloat) height };
|
||||
mt->metalLayer.drawableSize = size;
|
||||
|
||||
/* Update uniforms */
|
||||
memcpy (mt->uniforms.matViewProjection, mat, 16 * sizeof (float));
|
||||
mt->uniforms.viewport[0] = (float) width;
|
||||
mt->uniforms.viewport[1] = (float) height;
|
||||
|
||||
/* Sync atlas if dirty */
|
||||
if (mt->atlas_dirty)
|
||||
{
|
||||
memcpy (mt->atlasBuffer.contents, mt->atlas_data,
|
||||
mt->atlas_cursor * TEXEL_SIZE);
|
||||
mt->atlas_dirty = false;
|
||||
}
|
||||
|
||||
/* Get drawable */
|
||||
id<CAMetalDrawable> drawable = [mt->metalLayer nextDrawable];
|
||||
if (!drawable)
|
||||
return;
|
||||
|
||||
/* Render pass */
|
||||
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 (1.0, 1.0, 1.0, 1.0);
|
||||
|
||||
id<MTLCommandBuffer> commandBuffer = [mt->commandQueue commandBuffer];
|
||||
id<MTLRenderCommandEncoder> encoder =
|
||||
[commandBuffer renderCommandEncoderWithDescriptor:passDesc];
|
||||
|
||||
[encoder setViewport:(MTLViewport){0, 0, (double) width, (double) height, 0, 1}];
|
||||
[encoder setRenderPipelineState:mt->pipelineState];
|
||||
|
||||
if (vertex_count > 0)
|
||||
{
|
||||
id<MTLBuffer> vertexBuffer =
|
||||
[mt->device newBufferWithBytes:vertices
|
||||
length:vertex_count * sizeof (glyph_vertex_t)
|
||||
options:MTLResourceStorageModeShared];
|
||||
|
||||
[encoder setVertexBuffer:vertexBuffer offset:0 atIndex:0];
|
||||
[encoder setVertexBytes:&mt->uniforms length:sizeof (mt->uniforms) atIndex:1];
|
||||
[encoder setFragmentBuffer:mt->atlasBuffer offset:0 atIndex:0];
|
||||
[encoder setFragmentBytes:&mt->uniforms length:sizeof (mt->uniforms) atIndex:1];
|
||||
|
||||
[encoder drawPrimitives:MTLPrimitiveTypeTriangle
|
||||
vertexStart:0
|
||||
vertexCount:vertex_count];
|
||||
}
|
||||
|
||||
[encoder endEncoding];
|
||||
[commandBuffer presentDrawable:drawable];
|
||||
[commandBuffer commit];
|
||||
|
||||
} /* @autoreleasepool */
|
||||
}
|
||||
|
||||
#endif /* __APPLE__ */
|
||||
+73
-36
@@ -4,12 +4,14 @@
|
||||
*/
|
||||
|
||||
#include "demo-view.h"
|
||||
#include "demo-metal.h"
|
||||
|
||||
#include "trackball.hh"
|
||||
#include "matrix4x4.hh"
|
||||
|
||||
struct demo_view_t {
|
||||
demo_glstate_t *st;
|
||||
demo_metal_t *metal;
|
||||
GLFWwindow *window;
|
||||
|
||||
/* Output */
|
||||
@@ -72,6 +74,19 @@ demo_view_create (demo_glstate_t *st, GLFWwindow *window)
|
||||
return vu;
|
||||
}
|
||||
|
||||
demo_view_t *
|
||||
demo_view_create_metal (demo_metal_t *mt, GLFWwindow *window)
|
||||
{
|
||||
demo_view_t *vu = (demo_view_t *) calloc (1, sizeof (demo_view_t));
|
||||
|
||||
vu->metal = mt;
|
||||
vu->window = window;
|
||||
vu->needs_redraw = true;
|
||||
demo_view_reset (vu);
|
||||
|
||||
return vu;
|
||||
}
|
||||
|
||||
void
|
||||
demo_view_destroy (demo_view_t *vu)
|
||||
{
|
||||
@@ -130,12 +145,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);
|
||||
@@ -195,32 +206,44 @@ demo_view_toggle_vsync (demo_view_t *vu)
|
||||
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
|
||||
|
||||
if (mode == demo_view_t::GAMMA_SRGB)
|
||||
if (vu->metal)
|
||||
{
|
||||
/* Metal uses sRGB framebuffer; gamma in shader for mode 2.2 */
|
||||
if (mode == demo_view_t::GAMMA_SRGB)
|
||||
mode = demo_view_t::GAMMA_NONE; /* sRGB is automatic with MTLPixelFormatBGRA8Unorm_sRGB */
|
||||
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_metal_set_gamma (vu->metal, gamma);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Disable previous sRGB if any. */
|
||||
#if defined(GL_FRAMEBUFFER_SRGB)
|
||||
glDisable (GL_FRAMEBUFFER_SRGB);
|
||||
while (glGetError () != GL_NO_ERROR)
|
||||
;
|
||||
glEnable (GL_FRAMEBUFFER_SRGB);
|
||||
if (glGetError () != GL_NO_ERROR)
|
||||
{
|
||||
LOGW ("sRGB framebuffer not available.\n");
|
||||
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);
|
||||
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");
|
||||
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);
|
||||
}
|
||||
|
||||
const char *names[] = {"sRGB", "gamma 2.2", "none"};
|
||||
LOGI ("Gamma correction: %s.\n", names[mode]);
|
||||
@@ -580,14 +603,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,23 +619,39 @@ 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);
|
||||
|
||||
if (vu->dark_mode)
|
||||
glClearColor (DARK_BG);
|
||||
if (vu->metal)
|
||||
{
|
||||
unsigned int count;
|
||||
glyph_vertex_t *verts = demo_buffer_get_vertices (buffer, &count);
|
||||
demo_metal_display (vu->metal, verts, count, width, height, mat);
|
||||
}
|
||||
else
|
||||
glClearColor (LIGHT_BG);
|
||||
glClear (GL_COLOR_BUFFER_BIT);
|
||||
{
|
||||
glViewport (0, 0, width, height);
|
||||
demo_glstate_set_matrix (vu->st, mat);
|
||||
|
||||
demo_buffer_draw (buffer);
|
||||
if (vu->dark_mode)
|
||||
glClearColor (DARK_BG);
|
||||
else
|
||||
glClearColor (LIGHT_BG);
|
||||
glClear (GL_COLOR_BUFFER_BIT);
|
||||
|
||||
glfwSwapBuffers (vu->window);
|
||||
demo_buffer_draw (buffer);
|
||||
|
||||
glfwSwapBuffers (vu->window);
|
||||
}
|
||||
vu->needs_redraw = false;
|
||||
}
|
||||
|
||||
void
|
||||
demo_view_setup (demo_view_t *vu)
|
||||
{
|
||||
if (vu->metal)
|
||||
{
|
||||
demo_metal_set_gamma (vu->metal, 1.f);
|
||||
demo_metal_set_foreground (vu->metal, 0.f, 0.f, 0.f, 1.f);
|
||||
return;
|
||||
}
|
||||
if (!vu->vsync)
|
||||
demo_view_toggle_vsync (vu);
|
||||
demo_glstate_setup (vu->st);
|
||||
|
||||
@@ -11,11 +11,15 @@
|
||||
#include "demo-buffer.h"
|
||||
#include "demo-glstate.h"
|
||||
|
||||
typedef struct demo_metal_t demo_metal_t;
|
||||
typedef struct demo_view_t demo_view_t;
|
||||
|
||||
demo_view_t *
|
||||
demo_view_create (demo_glstate_t *st, GLFWwindow *window);
|
||||
|
||||
demo_view_t *
|
||||
demo_view_create_metal (demo_metal_t *mt, GLFWwindow *window);
|
||||
|
||||
void
|
||||
demo_view_destroy (demo_view_t *vu);
|
||||
|
||||
|
||||
@@ -21,11 +21,20 @@ gpu_demo_sources = files(
|
||||
'trackball.hh',
|
||||
)
|
||||
|
||||
if host_machine.system() == 'darwin'
|
||||
gpu_demo_sources += files(
|
||||
'demo-metal.h',
|
||||
'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