[gpu] First working COLRv0 GPU render

Two paint-encoder bugs and two demo-plumbing bugs together
prevented end-to-end rendering.  With all four fixed, BungeeTint
and similar COLRv0 fonts now render through the GL demo in their
proper palette colors.

### Paint encoder

1. hb-gpu-paint-fragment.glsl: the LAYER_SOLID op's payload is a
   blob-relative texel offset, but the shader was passing it to
   hb_gpu_draw() as-is -- which treats its argument as an absolute
   atlas offset.  Add the glyph's `base` to the payload before the
   draw-coverage call so the sub-blob reference resolves inside
   the paint blob instead of elsewhere in the atlas.

2. hb-gpu-paint.cc push_clip_glyph callback: was resetting
   pending_palette_index to 0.  But harfbuzz's COLRv0 paint
   emitter calls custom_palette_color BEFORE push_clip_glyph for
   each layer, so the index set by custom_palette_color was being
   clobbered before the color callback consumed it -- every layer
   ended up tagged palette_index=0.  Leave pending_palette_index
   alone in push_clip_glyph.

### Demo plumbing

3. util/gpu/demo-glstate.cc: demo_glstate_create now glUseProgram's
   its program immediately.  The palette uniform upload reads
   GL_CURRENT_PROGRAM, and glyph upload happens from
   demo_buffer_current_line() -- which runs before
   demo_view_setup() would have bound the program.  Without this,
   glGetIntegerv returned 0 and the palette was never uploaded.

4. util/gpu/demo-font.cc: adds lazy demo_font_upload_palette() on
   first glyph request.  Also tries both "hb_gpu_palette" and
   "hb_gpu_palette[0]" for the uniform lookup since drivers
   disagree.

### Bonus

util/hb-gpu-encode-all.cc: reports paint-encoded and draw-fallback
counts / sizes / timings separately so you can tell where time
goes in mixed-content fonts.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Behdad Esfahbod committed 2026-04-13 15:03:58 -06:00
1 parent 7c97464406
commit f3fcd46ba9
6 files changed
+98 -43

No files matched your search

+3 -1
View File
@@ -76,7 +76,9 @@ static const char *hb_gpu_paint_fragment_glsl =
" : hb_gpu_palette[palette_index];\n"
" col.a *= alpha;\n"
"\n"
" float cov = hb_gpu_draw (renderCoord, uint (payload));\n"
" /* payload is a blob-relative texel offset; sub-blobs live\n"
" * inside the paint blob at atlas offset `base`. */\n"
" float cov = hb_gpu_draw (renderCoord, uint (base + payload));\n"
" vec4 src = col * cov;\n"
" /* SRC_OVER with premultiplied source. */\n"
" acc = src + acc * (1.0 - src.a);\n"
+3 -1
View File
@@ -75,7 +75,9 @@ vec4 hb_gpu_paint (vec2 renderCoord, uint glyphLoc, vec4 foreground)
: hb_gpu_palette[palette_index];
col.a *= alpha;
float cov = hb_gpu_draw (renderCoord, uint (payload));
/* payload is a blob-relative texel offset; sub-blobs live
* inside the paint blob at atlas offset `base`. */
float cov = hb_gpu_draw (renderCoord, uint (base + payload));
vec4 src = col * cov;
/* SRC_OVER with premultiplied source. */
acc = src + acc * (1.0 - src.a);
+4 -1
View File
@@ -71,7 +71,10 @@ hb_gpu_paint_push_clip_glyph (hb_paint_funcs_t *funcs HB_UNUSED,
c->pending_clip = true;
c->pending_clip_glyph = glyph;
c->pending_clip_font = font;
c->pending_palette_index = 0;
/* Don't reset pending_palette_index here: COLR's paint emitter
* calls custom_palette_color BEFORE push_clip_glyph, so the
* index set by the custom-palette-color callback must survive
* this call and be consumed by the next color callback. */
}
static void
+14 -2
View File
@@ -17,6 +17,8 @@ struct demo_font_t {
demo_atlas_t *atlas;
hb_gpu_paint_t *p;
bool palette_uploaded;
unsigned int num_glyphs;
unsigned int sum_bytes;
};
@@ -29,6 +31,8 @@ demo_font_upload_palette (hb_face_t *face)
if (!program) return;
GLint loc = glGetUniformLocation (program, "hb_gpu_palette");
if (loc < 0)
loc = glGetUniformLocation (program, "hb_gpu_palette[0]");
if (loc < 0) return;
unsigned count = hb_ot_color_palette_get_colors (face, 0, 0, NULL, NULL);
@@ -62,8 +66,10 @@ demo_font_create (hb_font_t *hb_font,
font->glyph_cache = new glyph_cache_t ();
font->atlas = demo_atlas_reference (atlas);
font->p = hb_gpu_paint_create_or_fail ();
demo_font_upload_palette (font->face);
/* Palette uniform needs a bound GL program; demo_font_create is
* called before the renderer's program is made current. Upload
* lazily on the first glyph request. */
font->palette_uploaded = false;
return font;
}
@@ -101,6 +107,12 @@ _demo_font_upload_glyph (demo_font_t *font,
unsigned int glyph_index,
glyph_info_t *glyph_info)
{
if (!font->palette_uploaded)
{
demo_font_upload_palette (font->face);
font->palette_uploaded = true;
}
hb_gpu_paint_clear (font->p);
hb_gpu_paint_glyph (font->p, font->font, glyph_index);
+5
View File
@@ -17,6 +17,11 @@ demo_glstate_create (void)
st->program = demo_shader_create_program ();
st->atlas = demo_atlas_create (1024 * 1024);
/* Bind the program so demo_font_t can upload the palette
* uniform during glyph upload (which happens before the first
* render, i.e. before demo_view_setup re-binds it). */
glUseProgram (st->program);
return st;
}
+69 -38
View File
@@ -94,15 +94,20 @@ main (int argc, char **argv)
return 1;
}
unsigned num_encoded = 0;
unsigned num_fallback = 0;
unsigned num_paint_encoded = 0;
unsigned num_draw_encoded = 0;
unsigned num_empty = 0;
unsigned num_failed = 0;
uint64_t total_bytes = 0;
unsigned max_bytes = 0;
unsigned max_gid = 0;
uint64_t paint_ns = 0;
uint64_t encode_ns = 0;
uint64_t paint_bytes = 0;
uint64_t draw_bytes = 0;
unsigned paint_max_bytes = 0;
unsigned draw_max_bytes = 0;
unsigned paint_max_gid = 0;
unsigned draw_max_gid = 0;
uint64_t paint_walk_ns = 0;
uint64_t paint_encode_ns = 0;
uint64_t draw_walk_ns = 0;
uint64_t draw_encode_ns = 0;
typedef std::chrono::steady_clock clock;
clock::time_point wall_start = clock::now ();
@@ -119,8 +124,8 @@ main (int argc, char **argv)
hb_blob_t *encoded = hb_gpu_paint_encode (paint, nullptr);
clock::time_point t2 = clock::now ();
paint_ns += std::chrono::duration_cast<std::chrono::nanoseconds> (t1 - t0).count ();
encode_ns += std::chrono::duration_cast<std::chrono::nanoseconds> (t2 - t1).count ();
paint_walk_ns += std::chrono::duration_cast<std::chrono::nanoseconds> (t1 - t0).count ();
paint_encode_ns += std::chrono::duration_cast<std::chrono::nanoseconds> (t2 - t1).count ();
bool is_fallback = false;
if (!encoded)
@@ -128,8 +133,13 @@ main (int argc, char **argv)
/* Paint did not produce a blob (v1 feature, empty glyph).
* Fall back to the draw encoder. */
hb_gpu_draw_clear (draw);
clock::time_point f0 = clock::now ();
hb_gpu_draw_glyph (draw, font, gid);
clock::time_point f1 = clock::now ();
encoded = hb_gpu_draw_encode (draw, nullptr);
clock::time_point f2 = clock::now ();
draw_walk_ns += std::chrono::duration_cast<std::chrono::nanoseconds> (f1 - f0).count ();
draw_encode_ns += std::chrono::duration_cast<std::chrono::nanoseconds> (f2 - f1).count ();
is_fallback = true;
}
@@ -148,17 +158,18 @@ main (int argc, char **argv)
unsigned len = hb_blob_get_length (encoded);
if (len == 0)
num_empty++;
else if (is_fallback)
{
num_draw_encoded++;
draw_bytes += len;
if (len > draw_max_bytes) { draw_max_bytes = len; draw_max_gid = gid; }
}
else
{
if (is_fallback) num_fallback++;
else num_encoded++;
if (len > max_bytes)
{
max_bytes = len;
max_gid = gid;
}
num_paint_encoded++;
paint_bytes += len;
if (len > paint_max_bytes) { paint_max_bytes = len; paint_max_gid = gid; }
}
total_bytes += len;
}
if (is_fallback)
hb_gpu_draw_recycle_blob (draw, encoded);
@@ -168,29 +179,49 @@ main (int argc, char **argv)
uint64_t wall_ns = std::chrono::duration_cast<std::chrono::nanoseconds> (clock::now () - wall_start).count ();
printf ("font: %s\n", font_path);
printf ("glyphs: %u\n", glyph_count);
printf ("paint: %u\n", num_encoded);
if (num_fallback)
printf ("draw fb: %u\n", num_fallback);
printf ("empty: %u\n", num_empty);
printf ("font: %s\n", font_path);
printf ("glyphs: %u\n", glyph_count);
printf ("empty: %u\n", num_empty);
if (num_failed)
printf ("FAILED: %u\n", num_failed);
printf ("total: %.2f KiB\n", total_bytes / 1024.);
if (num_encoded)
{
printf ("avg: %.2f KiB/glyph (non-empty)\n",
total_bytes / 1024. / num_encoded);
printf ("max: %.2f KiB (gid %u)\n",
max_bytes / 1024., max_gid);
}
printf ("FAILED: %u\n", num_failed);
uint64_t total_glyphs = (uint64_t) glyph_count * num_iters;
printf ("paint: %.3fms (%.1fus/glyph)\n",
paint_ns / 1e6 / num_iters,
total_glyphs ? paint_ns / 1e3 / total_glyphs : 0.);
printf ("encode: %.3fms (%.1fus/glyph)\n",
encode_ns / 1e6 / num_iters,
total_glyphs ? encode_ns / 1e3 / total_glyphs : 0.);
if (num_paint_encoded)
{
printf ("\n");
printf ("paint encoded: %u\n", num_paint_encoded);
printf (" total: %.2f KiB\n", paint_bytes / 1024.);
printf (" avg: %.2f KiB/glyph\n",
paint_bytes / 1024. / num_paint_encoded);
printf (" max: %.2f KiB (gid %u)\n",
paint_max_bytes / 1024., paint_max_gid);
printf (" walk: %.3fms (%.1fus/glyph)\n",
paint_walk_ns / 1e6 / num_iters,
total_glyphs ? paint_walk_ns / 1e3 / total_glyphs : 0.);
printf (" encode: %.3fms (%.1fus/glyph)\n",
paint_encode_ns / 1e6 / num_iters,
total_glyphs ? paint_encode_ns / 1e3 / total_glyphs : 0.);
}
if (num_draw_encoded)
{
printf ("\n");
printf ("draw fallback: %u\n", num_draw_encoded);
printf (" total: %.2f KiB\n", draw_bytes / 1024.);
printf (" avg: %.2f KiB/glyph\n",
draw_bytes / 1024. / num_draw_encoded);
printf (" max: %.2f KiB (gid %u)\n",
draw_max_bytes / 1024., draw_max_gid);
printf (" walk: %.3fms (%.1fus/glyph)\n",
draw_walk_ns / 1e6 / num_iters,
num_draw_encoded ? draw_walk_ns / 1e3 / num_draw_encoded : 0.);
printf (" encode: %.3fms (%.1fus/glyph)\n",
draw_encode_ns / 1e6 / num_iters,
num_draw_encoded ? draw_encode_ns / 1e3 / num_draw_encoded : 0.);
}
printf ("\n");
printf ("wall: %.3fms (%.0f glyphs/s)\n",
wall_ns / 1e6 / num_iters,
wall_ns ? total_glyphs * 1e9 / wall_ns : 0.);