From eb46e5349ec7bac3a2eef0ef7e37faf0940060da Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 13 Apr 2026 14:36:32 -0600 Subject: [PATCH] [gpu] Wire GL demo to the paint pipeline end-to-end * hb_gpu_paint_glyph() switches from hb_font_paint_glyph_or_fail to hb_font_paint_glyph so non-color glyphs also produce a blob (harfbuzz synthesizes push_clip_glyph + color(is_foreground) + pop_clip which our callbacks turn into a single LAYER_SOLID op). Return value is now "no internal error" rather than "glyph had color paint". * util/gpu/demo-font.cc: drops the draw-only upload path and routes every glyph through hb_gpu_paint_{clear,glyph,encode, recycle_blob}. Uploads the active palette to the hb_gpu_palette uniform array in demo_font_create. * util/gpu/demo-shader.cc: concatenates hb_gpu_paint_shader_source after the shared + draw sources, and the demo's fragment main now calls hb_gpu_paint(texcoord, glyphLoc, u_foreground) directly. Stem-darkening / debug overlay paths preserved against the premultiplied alpha channel returned by paint. MSL / WGSL / HLSL demo paths are unchanged for now -- the paint shader is GLSL-only and those demos keep using the draw path. wasm size impact (local Emscripten build): baseline 412722 170342 gz +HB_NO_COLOR/HB_NO_PAINT 437752 179165 gz +demo paint wiring 443313 181502 gz delta (vs baseline) +30591 +11160 All 241 tests pass. Assisted-by: Claude Opus 4.6 (1M context) --- src/hb-gpu-paint.cc | 14 ++++++++---- util/gpu/demo-font.cc | 50 +++++++++++++++++++++++++++++++++-------- util/gpu/demo-shader.cc | 27 ++++++++++------------ 3 files changed, 63 insertions(+), 28 deletions(-) diff --git a/src/hb-gpu-paint.cc b/src/hb-gpu-paint.cc index bbb67c397..cf1a55f1e 100644 --- a/src/hb-gpu-paint.cc +++ b/src/hb-gpu-paint.cc @@ -316,10 +316,16 @@ hb_gpu_paint_glyph (hb_gpu_paint_t *paint, hb_font_get_scale (font, &paint->x_scale, &paint->y_scale); /* Sentinel foreground (zero). The encoder never stores the * actual foreground value -- it just records the is_foreground - * flag; the shader resolves foreground per quad. */ - return hb_font_paint_glyph_or_fail (font, glyph, - hb_gpu_paint_get_funcs (), paint, - 0, HB_COLOR (0, 0, 0, 0)); + * flag; the shader resolves foreground per quad. + * + * Use hb_font_paint_glyph (not _or_fail) so that non-color + * glyphs still produce a blob: harfbuzz synthesizes + * push_clip_glyph + color(is_foreground=true) + pop_clip, which + * our callbacks turn into a single LAYER_SOLID op. */ + hb_font_paint_glyph (font, glyph, + hb_gpu_paint_get_funcs (), paint, + 0, HB_COLOR (0, 0, 0, 0)); + return !paint->unsupported; } /** diff --git a/util/gpu/demo-font.cc b/util/gpu/demo-font.cc index 32be0610e..4135b359b 100644 --- a/util/gpu/demo-font.cc +++ b/util/gpu/demo-font.cc @@ -4,6 +4,8 @@ #include "demo-font.h" +#include + #include typedef std::map glyph_cache_t; @@ -13,12 +15,42 @@ struct demo_font_t { hb_font_t *font; glyph_cache_t *glyph_cache; demo_atlas_t *atlas; - hb_gpu_draw_t *g; + hb_gpu_paint_t *p; unsigned int num_glyphs; unsigned int sum_bytes; }; +static void +demo_font_upload_palette (hb_face_t *face) +{ + GLint program; + glGetIntegerv (GL_CURRENT_PROGRAM, &program); + if (!program) return; + + GLint loc = glGetUniformLocation (program, "hb_gpu_palette"); + if (loc < 0) return; + + unsigned count = hb_ot_color_palette_get_colors (face, 0, 0, NULL, NULL); + if (!count) return; + if (count > 256) count = 256; + + hb_color_t colors[256]; + unsigned returned = count; + hb_ot_color_palette_get_colors (face, 0, 0, &returned, colors); + + float palette[256 * 4] = {0}; + for (unsigned i = 0; i < returned; i++) + { + palette[i * 4 + 0] = hb_color_get_red (colors[i]) / 255.f; + palette[i * 4 + 1] = hb_color_get_green (colors[i]) / 255.f; + palette[i * 4 + 2] = hb_color_get_blue (colors[i]) / 255.f; + palette[i * 4 + 3] = hb_color_get_alpha (colors[i]) / 255.f; + } + + glUniform4fv (loc, 256, palette); +} + demo_font_t * demo_font_create (hb_font_t *hb_font, demo_atlas_t *atlas) @@ -29,7 +61,9 @@ demo_font_create (hb_font_t *hb_font, font->font = hb_font_reference (hb_font); font->glyph_cache = new glyph_cache_t (); font->atlas = demo_atlas_reference (atlas); - font->g = hb_gpu_draw_create_or_fail (); + font->p = hb_gpu_paint_create_or_fail (); + + demo_font_upload_palette (font->face); return font; } @@ -40,7 +74,7 @@ demo_font_destroy (demo_font_t *font) if (!font) return; - hb_gpu_draw_destroy (font->g); + hb_gpu_paint_destroy (font->p); demo_atlas_destroy (font->atlas); delete font->glyph_cache; hb_font_destroy (font->font); @@ -67,13 +101,11 @@ _demo_font_upload_glyph (demo_font_t *font, unsigned int glyph_index, glyph_info_t *glyph_info) { - hb_gpu_draw_reset (font->g); + hb_gpu_paint_clear (font->p); + hb_gpu_paint_glyph (font->p, font->font, glyph_index); - hb_gpu_draw_glyph (font->g, font->font, glyph_index); - - /* Get extents in font design units */ hb_glyph_extents_t hb_ext; - hb_blob_t *blob = hb_gpu_draw_encode (font->g, &hb_ext); + hb_blob_t *blob = hb_gpu_paint_encode (font->p, &hb_ext); if (!blob) die ("Failed encoding glyph"); @@ -97,7 +129,7 @@ _demo_font_upload_glyph (demo_font_t *font, font->num_glyphs++; font->sum_bytes += len; - hb_gpu_draw_recycle_blob (font->g, blob); + hb_gpu_paint_recycle_blob (font->p, blob); } void diff --git a/util/gpu/demo-shader.cc b/util/gpu/demo-shader.cc index fd7c9f118..9bbb1c368 100644 --- a/util/gpu/demo-shader.cc +++ b/util/gpu/demo-shader.cc @@ -46,30 +46,26 @@ out vec4 fragColor; void main () { - float coverage = hb_gpu_draw (v_texcoord, v_glyphLoc); - - /* Stem darkening / thinning at small sizes. - * Light text on dark: stems get too fat → thin them (exponent > 1). - * Dark text on light: stems get too thin → darken them (exponent < 1). - * The foreground brightness tells us which mode we're in. */ - if (u_stem_darkening > 0.0) - coverage = hb_gpu_stem_darken (coverage, - dot (u_foreground.rgb, vec3 (1.0 / 3.0)), - hb_gpu_ppem (v_texcoord, v_glyphLoc)); + /* The paint interpreter returns a premultiplied RGBA. For + * non-color glyphs the encoder synthesizes a single + * LAYER_SOLID with is_foreground=true, so the result is + * u_foreground * coverage -- same visual as the old draw + * path. For COLRv0 glyphs it returns the composited color. */ + vec4 c = hb_gpu_paint (v_texcoord, v_glyphLoc, u_foreground); if (u_gamma != 1.0) - coverage = pow (coverage, u_gamma); + c.a = pow (c.a, u_gamma); if (u_debug > 0.0) { ivec2 counts = _hb_gpu_curve_counts (v_texcoord, v_glyphLoc); float r = clamp (float (counts.x) / 8.0, 0.0, 1.0); float g = clamp (float (counts.y) / 8.0, 0.0, 1.0); - fragColor = vec4 (r, g, coverage, max (max (r, g), coverage)); + fragColor = vec4 (r, g, c.a, max (max (r, g), c.a)); return; } - fragColor = vec4 (u_foreground.rgb, u_foreground.a * coverage); + fragColor = c; } )glsl"; @@ -164,8 +160,9 @@ demo_shader_create_program (void) vert_sources); const GLchar *frag_sources[] = {preamble, - hb_gpu_shader_source (HB_GPU_SHADER_STAGE_FRAGMENT, HB_GPU_SHADER_LANG_GLSL), - hb_gpu_draw_shader_source (HB_GPU_SHADER_STAGE_FRAGMENT, HB_GPU_SHADER_LANG_GLSL), + hb_gpu_shader_source (HB_GPU_SHADER_STAGE_FRAGMENT, HB_GPU_SHADER_LANG_GLSL), + hb_gpu_draw_shader_source (HB_GPU_SHADER_STAGE_FRAGMENT, HB_GPU_SHADER_LANG_GLSL), + hb_gpu_paint_shader_source (HB_GPU_SHADER_STAGE_FRAGMENT, HB_GPU_SHADER_LANG_GLSL), demo_fragment_glsl}; fragment_shader = compile_shader (GL_FRAGMENT_SHADER, ARRAY_LEN (frag_sources),