[gpu] Port paint fragment shader to MSL; wire Metal demo

MSL transcription of the GLSL paint fragment interpreter.  Same
per-pixel op-stream walk and LAYER_SOLID SRC_OVER composite; the
only surface difference is that MSL's hb_gpu_fetch /
hb_gpu_paint take the atlas and palette as explicit buffer
pointer parameters (matching MSL's lack of globally-scoped
device-address-space globals).

  float4 hb_gpu_paint (float2 renderCoord, uint glyphLoc,
		       float4 foreground,
		       device const short4* hb_gpu_atlas,
		       constant float4* hb_gpu_palette);

hb_gpu_paint_shader_source() now returns a real string for
HB_GPU_SHADER_LANG_MSL fragment; vertex and WGSL/HLSL still stub
to "".

util/gpu/demo-metal.mm: concatenates the paint source alongside
the draw source into the Metal library, fragment_main switches
from hb_gpu_draw to hb_gpu_paint, and a 256-element palette
MTLBuffer is created + bound at fragment buffer index 2.  The
palette buffer is zero-filled for now; real palette upload from
the font's active palette is a follow-up (the GL path in
demo-font.cc uses glUniform4fv, which is GL-specific; a cleaner
cross-backend upload will come with the general palette plumbing).

Non-color text (u_foreground path) should render correctly on
Metal.  COLRv0 color glyphs will render transparent until the
palette upload lands -- ready for Mac smoke-testing.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Behdad Esfahbod committed 2026-04-13 15:14:36 -06:00
1 parent 34a9f22f25
commit 5e2b77d2ea
5 files changed
+200 -18

No files matched your search

+85
View File
@@ -0,0 +1,85 @@
static const char *hb_gpu_paint_fragment_msl =
"/*\n"
" * Copyright (C) 2026 Behdad Esfahbod\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"
"/* Paint-renderer fragment shader (Metal).\n"
" *\n"
" * Assumes the shared fragment helpers (hb-gpu-fragment.msl) and\n"
" * the draw-renderer fragment helpers (hb-gpu-draw-fragment.msl)\n"
" * are prepended to this source.\n"
" *\n"
" * The palette array is passed as a pointer parameter, same pattern\n"
" * as the atlas. Callers typically supply it as a constant buffer.\n"
" */\n"
"\n"
"\n"
"float4 hb_gpu_paint (float2 renderCoord, uint glyphLoc_, float4 foreground,\n"
" device const short4* hb_gpu_atlas,\n"
" constant float4* hb_gpu_palette)\n"
"{\n"
" int base = int (glyphLoc_);\n"
" int4 h0 = int4 (hb_gpu_atlas[base]); /* (num_ops, _, _, _) */\n"
" int4 h2 = int4 (hb_gpu_atlas[base + 2]); /* (ops_offset, _, _, _) */\n"
" int num_ops = h0.r;\n"
" int cursor = base + h2.r;\n"
"\n"
" float4 acc = float4 (0.0);\n"
"\n"
" for (int i = 0; i < num_ops; i++)\n"
" {\n"
" int4 op = int4 (hb_gpu_atlas[cursor]);\n"
" int op_type = op.r;\n"
" int payload = (op.b << 16) | (op.a & 0xffff);\n"
"\n"
" if (op_type == 0) /* LAYER_SOLID */\n"
" {\n"
" int4 ct = int4 (hb_gpu_atlas[cursor + 1]);\n"
" int palette_index = ct.r;\n"
" int flags = ct.g;\n"
" float alpha = float (ct.b) / 32767.0;\n"
"\n"
" float4 col = ((flags & 1) != 0)\n"
" ? foreground\n"
" : hb_gpu_palette[palette_index];\n"
" col.a *= alpha;\n"
"\n"
" float cov = hb_gpu_draw (renderCoord, uint (base + payload),\n"
" hb_gpu_atlas);\n"
" float4 src = col * cov;\n"
" /* SRC_OVER with premultiplied source. */\n"
" acc = src + acc * (1.0 - src.a);\n"
"\n"
" cursor += 2;\n"
" }\n"
" else\n"
" {\n"
" /* LAYER_GRADIENT, PUSH_GROUP, POP_GROUP not yet handled. */\n"
" break;\n"
" }\n"
" }\n"
"\n"
" return acc;\n"
"}\n"
;
+83
View File
@@ -0,0 +1,83 @@
/*
* Copyright (C) 2026 Behdad Esfahbod
*
* 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.
*/
/* Paint-renderer fragment shader (Metal).
*
* Assumes the shared fragment helpers (hb-gpu-fragment.msl) and
* the draw-renderer fragment helpers (hb-gpu-draw-fragment.msl)
* are prepended to this source.
*
* The palette array is passed as a pointer parameter, same pattern
* as the atlas. Callers typically supply it as a constant buffer.
*/
float4 hb_gpu_paint (float2 renderCoord, uint glyphLoc_, float4 foreground,
device const short4* hb_gpu_atlas,
constant float4* hb_gpu_palette)
{
int base = int (glyphLoc_);
int4 h0 = int4 (hb_gpu_atlas[base]); /* (num_ops, _, _, _) */
int4 h2 = int4 (hb_gpu_atlas[base + 2]); /* (ops_offset, _, _, _) */
int num_ops = h0.r;
int cursor = base + h2.r;
float4 acc = float4 (0.0);
for (int i = 0; i < num_ops; i++)
{
int4 op = int4 (hb_gpu_atlas[cursor]);
int op_type = op.r;
int payload = (op.b << 16) | (op.a & 0xffff);
if (op_type == 0) /* LAYER_SOLID */
{
int4 ct = int4 (hb_gpu_atlas[cursor + 1]);
int palette_index = ct.r;
int flags = ct.g;
float alpha = float (ct.b) / 32767.0;
float4 col = ((flags & 1) != 0)
? foreground
: hb_gpu_palette[palette_index];
col.a *= alpha;
float cov = hb_gpu_draw (renderCoord, uint (base + payload),
hb_gpu_atlas);
float4 src = col * cov;
/* SRC_OVER with premultiplied source. */
acc = src + acc * (1.0 - src.a);
cursor += 2;
}
else
{
/* LAYER_GRADIENT, PUSH_GROUP, POP_GROUP not yet handled. */
break;
}
}
return acc;
}
+2 -1
View File
@@ -562,6 +562,7 @@ hb_gpu_paint_recycle_blob (hb_gpu_paint_t *paint HB_UNUSED,
* XSince: REPLACEME
**/
#include "hb-gpu-paint-fragment-glsl.hh"
#include "hb-gpu-paint-fragment-msl.hh"
const char *
hb_gpu_paint_shader_source (hb_gpu_shader_stage_t stage,
@@ -571,7 +572,7 @@ hb_gpu_paint_shader_source (hb_gpu_shader_stage_t stage,
case HB_GPU_SHADER_STAGE_FRAGMENT:
switch (lang) {
case HB_GPU_SHADER_LANG_GLSL: return hb_gpu_paint_fragment_glsl;
case HB_GPU_SHADER_LANG_MSL:
case HB_GPU_SHADER_LANG_MSL: return hb_gpu_paint_fragment_msl;
case HB_GPU_SHADER_LANG_WGSL:
case HB_GPU_SHADER_LANG_HLSL: return "";
default: return nullptr;
+1
View File
@@ -838,6 +838,7 @@ hb_gpu_shader_sources = [
[ 'hb-gpu-draw-fragment.msl', 'hb-gpu-draw-fragment-msl.hh' ],
[ 'hb-gpu-draw-fragment.hlsl', 'hb-gpu-draw-fragment-hlsl.hh' ],
[ 'hb-gpu-paint-fragment.glsl', 'hb-gpu-paint-fragment-glsl.hh' ],
[ 'hb-gpu-paint-fragment.msl', 'hb-gpu-paint-fragment-msl.hh' ],
]
hb_gpu_shader_helper = find_program('gen-gpu-shader-artifacts.py')
+29 -17
View File
@@ -59,26 +59,23 @@ vertex VertexOut vertex_main(VertexIn in [[stage_in]],
fragment float4 fragment_main(VertexOut in [[stage_in]],
constant Uniforms& uniforms [[buffer(1)]],
device const short4* atlas [[buffer(0)]]) {
float coverage = hb_gpu_draw(in.texcoord, in.glyphLoc, atlas);
/* Stem darkening / thinning at small sizes. */
if (uniforms.stem_darkening > 0.0)
coverage = hb_gpu_stem_darken(coverage,
dot(uniforms.foreground.rgb, float3(1.0 / 3.0)),
1.0 / max(fwidth(in.texcoord).x, fwidth(in.texcoord).y));
device const short4* atlas [[buffer(0)]],
constant float4* palette [[buffer(2)]]) {
/* Paint interpreter returns premultiplied RGBA. */
float4 c = hb_gpu_paint(in.texcoord, in.glyphLoc, uniforms.foreground,
atlas, palette);
if (uniforms.gamma != 1.0)
coverage = pow(coverage, uniforms.gamma);
c.a = pow(c.a, uniforms.gamma);
if (uniforms.debug > 0.0) {
int2 counts = _hb_gpu_curve_counts(in.texcoord, in.glyphLoc, atlas);
float r = clamp(float(counts.x) / 8.0, 0.0, 1.0);
float g = clamp(float(counts.y) / 8.0, 0.0, 1.0);
return float4(r, g, coverage, max(max(r, g), coverage));
return float4(r, g, c.a, max(max(r, g), c.a));
}
return float4(uniforms.foreground.xyz, uniforms.foreground.w * coverage);
return c;
}
)msl";
@@ -109,6 +106,10 @@ struct demo_renderer_metal_t : demo_renderer_t
id<MTLBuffer> atlasBuffer;
bool atlas_dirty;
/* Palette buffer (256 x float4). Currently zero-filled; real
* palette upload is a TODO (see demo-font.cc for the GL path). */
id<MTLBuffer> paletteBuffer;
/* Cached vertex buffer */
id<MTLBuffer> vertexBuffer;
glyph_vertex_t *uploaded_ptr;
@@ -312,6 +313,7 @@ struct demo_renderer_metal_t : demo_renderer_t
[encoder setVertexBytes:&uniforms length:sizeof (uniforms) atIndex:1];
[encoder setFragmentBuffer:atlasBuffer offset:0 atIndex:0];
[encoder setFragmentBytes:&uniforms length:sizeof (uniforms) atIndex:1];
[encoder setFragmentBuffer:paletteBuffer offset:0 atIndex:2];
[encoder drawPrimitives:MTLPrimitiveTypeTriangle
vertexStart:0
@@ -363,6 +365,7 @@ struct demo_renderer_metal_t : demo_renderer_t
[encoder setVertexBytes:&uniforms length:sizeof (uniforms) atIndex:1];
[encoder setFragmentBuffer:atlasBuffer offset:0 atIndex:0];
[encoder setFragmentBytes:&uniforms length:sizeof (uniforms) atIndex:1];
[encoder setFragmentBuffer:paletteBuffer offset:0 atIndex:2];
[encoder drawPrimitives:MTLPrimitiveTypeTriangle
vertexStart:0
@@ -405,14 +408,17 @@ demo_renderer_create_metal (GLFWwindow *window)
nswindow.contentView.wantsLayer = YES;
/* Compile shaders */
const char *vert_common = hb_gpu_shader_source (HB_GPU_SHADER_STAGE_VERTEX, HB_GPU_SHADER_LANG_MSL);
const char *vert_src = hb_gpu_draw_shader_source (HB_GPU_SHADER_STAGE_VERTEX, HB_GPU_SHADER_LANG_MSL);
const char *frag_common = hb_gpu_shader_source (HB_GPU_SHADER_STAGE_FRAGMENT, HB_GPU_SHADER_LANG_MSL);
const char *frag_src = hb_gpu_draw_shader_source (HB_GPU_SHADER_STAGE_FRAGMENT, HB_GPU_SHADER_LANG_MSL);
const char *vert_common = hb_gpu_shader_source (HB_GPU_SHADER_STAGE_VERTEX, HB_GPU_SHADER_LANG_MSL);
const char *vert_src = hb_gpu_draw_shader_source (HB_GPU_SHADER_STAGE_VERTEX, HB_GPU_SHADER_LANG_MSL);
const char *frag_common = hb_gpu_shader_source (HB_GPU_SHADER_STAGE_FRAGMENT, HB_GPU_SHADER_LANG_MSL);
const char *frag_draw = hb_gpu_draw_shader_source (HB_GPU_SHADER_STAGE_FRAGMENT, HB_GPU_SHADER_LANG_MSL);
const char *frag_paint = hb_gpu_paint_shader_source (HB_GPU_SHADER_STAGE_FRAGMENT, HB_GPU_SHADER_LANG_MSL);
NSString *preamble = @"#include <metal_stdlib>\nusing namespace metal;\n";
NSString *source = [NSString stringWithFormat:@"%@%s%s%s%s%s",
preamble, vert_common, vert_src, frag_common, frag_src, demo_metal_shader_source];
NSString *source = [NSString stringWithFormat:@"%@%s%s%s%s%s%s",
preamble, vert_common, vert_src,
frag_common, frag_draw, frag_paint,
demo_metal_shader_source];
NSError *error = nil;
id<MTLLibrary> library = [r->device newLibraryWithSource:source
@@ -480,6 +486,12 @@ demo_renderer_create_metal (GLFWwindow *window)
options:MTLResourceStorageModeShared];
r->atlas_dirty = false;
/* Palette: 256 float4 entries, zero-filled for now. Real upload
* from the font's active palette is TODO. */
r->paletteBuffer = [r->device newBufferWithLength:256 * 4 * sizeof (float)
options:MTLResourceStorageModeShared];
memset (r->paletteBuffer.contents, 0, 256 * 4 * sizeof (float));
/* Create atlas wrapper for demo_font_t */
demo_atlas_backend_t backend = {
r,