From e66500fd8eea65ffaf146d5a4d22e5f871bf054e Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 13 Apr 2026 18:44:58 -0600 Subject: [PATCH] [gpu] hb_gpu_paint: bake resolved color into blob, drop palette binding Add a raster-style configuration surface to hb_gpu_paint_t: hb_gpu_paint_set_foreground / hb_gpu_paint_get_foreground hb_gpu_paint_set_palette / hb_gpu_paint_get_palette hb_gpu_paint_clear_custom_palette_colors hb_gpu_paint_set_custom_palette_color hb_gpu_paint_glyph now passes paint->palette and paint->foreground through to hb_font_paint_glyph, and the custom-palette-color callback consults the user's overrides map (same pattern as hb_raster_paint_t) before letting harfbuzz fall back to the font's own palette. With that in place, the color() callback can bake the already- resolved RGBA into the encoded op directly, quantized as signed Q15 in the existing second texel, instead of recording a palette index and trusting the shader to look it up. The is_foreground flag stays in the op so a runtime foreground-color change (e.g. dark-mode toggle) still works without re-encoding. Shader side: drop the hb_gpu_palette uniform/storage binding and its HB_GPU_PALETTE_SIZE knob from all four paint-fragment shaders (GLSL / MSL / WGSL / HLSL); read the per-layer color straight out of the blob texel. This removes a per-backend binding slot and the 256-entry uniform-array size ceiling that blocked fonts like Noto-COLRv1 (~6200 entries). Trade-off: changing the palette selection or overrides invalidates previously encoded blobs and requires re-encoding. In practice palette switches are rare, and the rendering hot path gets simpler. Assisted-by: Claude Opus 4.6 (1M context) --- docs/harfbuzz-sections.txt | 6 ++ src/hb-gpu-paint-fragment-glsl.hh | 16 +-- src/hb-gpu-paint-fragment-hlsl.hh | 14 +-- src/hb-gpu-paint-fragment-msl.hh | 15 +-- src/hb-gpu-paint-fragment-wgsl.hh | 16 ++- src/hb-gpu-paint-fragment.glsl | 16 +-- src/hb-gpu-paint-fragment.hlsl | 14 +-- src/hb-gpu-paint-fragment.msl | 15 +-- src/hb-gpu-paint-fragment.wgsl | 16 ++- src/hb-gpu-paint.cc | 167 +++++++++++++++++++++++++----- src/hb-gpu-paint.hh | 59 ++++------- src/hb-gpu.h | 22 ++++ 12 files changed, 225 insertions(+), 151 deletions(-) diff --git a/docs/harfbuzz-sections.txt b/docs/harfbuzz-sections.txt index 4cff60dbe..f738865f2 100644 --- a/docs/harfbuzz-sections.txt +++ b/docs/harfbuzz-sections.txt @@ -1152,6 +1152,12 @@ hb_gpu_paint_destroy hb_gpu_paint_set_user_data hb_gpu_paint_get_user_data hb_gpu_paint_get_funcs +hb_gpu_paint_set_foreground +hb_gpu_paint_get_foreground +hb_gpu_paint_set_palette +hb_gpu_paint_get_palette +hb_gpu_paint_clear_custom_palette_colors +hb_gpu_paint_set_custom_palette_color hb_gpu_paint_glyph hb_gpu_paint_encode hb_gpu_paint_clear diff --git a/src/hb-gpu-paint-fragment-glsl.hh b/src/hb-gpu-paint-fragment-glsl.hh index ef66d52ff..431ae696d 100644 --- a/src/hb-gpu-paint-fragment-glsl.hh +++ b/src/hb-gpu-paint-fragment-glsl.hh @@ -34,13 +34,6 @@ static const char *hb_gpu_paint_fragment_glsl = " */\n" "\n" "\n" -"#ifndef HB_GPU_PALETTE_SIZE\n" -"#define HB_GPU_PALETTE_SIZE 256\n" -"#endif\n" -"\n" -"uniform vec4 hb_gpu_palette[HB_GPU_PALETTE_SIZE];\n" -"\n" -"\n" "/* Walks the paint blob's flat op stream and returns a\n" " * premultiplied RGBA coverage value for the current fragment.\n" " *\n" @@ -67,19 +60,16 @@ static const char *hb_gpu_paint_fragment_glsl = " {\n" " ivec4 op = hb_gpu_fetch (cursor);\n" " int op_type = op.r;\n" +" int flags = op.g;\n" " int payload = (op.b << 16) | (op.a & 0xffff);\n" "\n" " if (op_type == 0) /* LAYER_SOLID */\n" " {\n" +" /* texel 1 holds RGBA as signed Q15. */\n" " ivec4 ct = hb_gpu_fetch (cursor + 1);\n" -" int palette_index = ct.r;\n" -" int flags = ct.g;\n" -" float alpha = float (ct.b) / 32767.0;\n" -"\n" " vec4 col = ((flags & 1) != 0)\n" " ? foreground\n" -" : hb_gpu_palette[palette_index];\n" -" col.a *= alpha;\n" +" : vec4 (ct) / 32767.0;\n" "\n" " /* payload is a blob-relative texel offset; sub-blobs live\n" " * inside the paint blob at atlas offset `base`. */\n" diff --git a/src/hb-gpu-paint-fragment-hlsl.hh b/src/hb-gpu-paint-fragment-hlsl.hh index 4040347d1..0212b706a 100644 --- a/src/hb-gpu-paint-fragment-hlsl.hh +++ b/src/hb-gpu-paint-fragment-hlsl.hh @@ -28,10 +28,7 @@ static const char *hb_gpu_paint_fragment_hlsl = " *\n" " * Assumes the shared fragment helpers (hb-gpu-fragment.hlsl) and\n" " * the draw-renderer fragment helpers (hb-gpu-draw-fragment.hlsl)\n" -" * are prepended to this source. The caller must also declare the\n" -" * palette buffer before this source, e.g.:\n" -" *\n" -" * StructuredBuffer hb_gpu_palette : register(t1);\n" +" * are prepended to this source.\n" " */\n" "\n" "\n" @@ -52,19 +49,16 @@ static const char *hb_gpu_paint_fragment_hlsl = " {\n" " int4 op = hb_gpu_fetch (cursor);\n" " int op_type = op.r;\n" +" int flags = op.g;\n" " int payload = (op.b << 16) | (op.a & 0xffff);\n" "\n" " if (op_type == 0) /* LAYER_SOLID */\n" " {\n" +" /* texel 1 holds RGBA as signed Q15. */\n" " int4 ct = hb_gpu_fetch (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" +" : (float4) ct / 32767.0;\n" "\n" " float cov = _hb_gpu_draw_impl (renderCoord, pixelsPerEm,\n" " (uint) (base + payload));\n" diff --git a/src/hb-gpu-paint-fragment-msl.hh b/src/hb-gpu-paint-fragment-msl.hh index c7e0fa77c..d0af38125 100644 --- a/src/hb-gpu-paint-fragment-msl.hh +++ b/src/hb-gpu-paint-fragment-msl.hh @@ -29,15 +29,11 @@ static const char *hb_gpu_paint_fragment_msl = " * 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" +" device const short4* hb_gpu_atlas)\n" "{\n" " /* fwidth once, at uniform control flow. */\n" " float2 pixelsPerEm = 1.0 / fwidth (renderCoord);\n" @@ -54,19 +50,16 @@ static const char *hb_gpu_paint_fragment_msl = " {\n" " int4 op = int4 (hb_gpu_atlas[cursor]);\n" " int op_type = op.r;\n" +" int flags = op.g;\n" " int payload = (op.b << 16) | (op.a & 0xffff);\n" "\n" " if (op_type == 0) /* LAYER_SOLID */\n" " {\n" +" /* texel 1 holds RGBA as signed Q15. */\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" +" : float4 (ct) / 32767.0;\n" "\n" " float cov = _hb_gpu_draw_impl (renderCoord, pixelsPerEm,\n" " uint (base + payload), hb_gpu_atlas);\n" diff --git a/src/hb-gpu-paint-fragment-wgsl.hh b/src/hb-gpu-paint-fragment-wgsl.hh index 996c9815e..0f0e621b1 100644 --- a/src/hb-gpu-paint-fragment-wgsl.hh +++ b/src/hb-gpu-paint-fragment-wgsl.hh @@ -30,14 +30,13 @@ static const char *hb_gpu_paint_fragment_wgsl = " * the draw-renderer fragment helpers (hb-gpu-draw-fragment.wgsl)\n" " * are prepended to this source.\n" " *\n" -" * atlas and palette are passed as explicit storage-buffer pointer\n" -" * parameters, matching WGSL's scoping rules.\n" +" * atlas is passed as an explicit storage-buffer pointer parameter,\n" +" * matching WGSL's scoping rules.\n" " */\n" "\n" "\n" "fn hb_gpu_paint (renderCoord: vec2f, glyphLoc_: u32, foreground: vec4f,\n" -" hb_gpu_atlas: ptr>, read>,\n" -" hb_gpu_palette: ptr, read>) -> vec4f\n" +" hb_gpu_atlas: ptr>, read>) -> vec4f\n" "{\n" " /* Compute pixelsPerEm once here at uniform control flow. WGSL\n" " * rejects fwidth inside a loop-conditional branch, so we call\n" @@ -59,21 +58,18 @@ static const char *hb_gpu_paint_fragment_wgsl = " {\n" " let op = hb_gpu_fetch (hb_gpu_atlas, cursor);\n" " let op_type = op.r;\n" +" let flags = op.g;\n" " let payload = (op.b << 16) | (op.a & 0xffff);\n" "\n" " if (op_type == 0) { // LAYER_SOLID\n" +" // texel 1 holds RGBA as signed Q15.\n" " let ct = hb_gpu_fetch (hb_gpu_atlas, cursor + 1);\n" -" let palette_index = ct.r;\n" -" let flags = ct.g;\n" -" let alpha = f32 (ct.b) / 32767.0;\n" -"\n" " var col: vec4f;\n" " if ((flags & 1) != 0) {\n" " col = foreground;\n" " } else {\n" -" col = (*hb_gpu_palette)[palette_index];\n" +" col = vec4f (ct) / 32767.0;\n" " }\n" -" col.a = col.a * alpha;\n" "\n" " let cov = _hb_gpu_draw_impl (renderCoord, pixelsPerEm,\n" " u32 (base + payload), hb_gpu_atlas);\n" diff --git a/src/hb-gpu-paint-fragment.glsl b/src/hb-gpu-paint-fragment.glsl index 4e945ad0d..e7213810f 100644 --- a/src/hb-gpu-paint-fragment.glsl +++ b/src/hb-gpu-paint-fragment.glsl @@ -33,13 +33,6 @@ */ -#ifndef HB_GPU_PALETTE_SIZE -#define HB_GPU_PALETTE_SIZE 256 -#endif - -uniform vec4 hb_gpu_palette[HB_GPU_PALETTE_SIZE]; - - /* Walks the paint blob's flat op stream and returns a * premultiplied RGBA coverage value for the current fragment. * @@ -66,19 +59,16 @@ vec4 hb_gpu_paint (vec2 renderCoord, uint glyphLoc, vec4 foreground) { ivec4 op = hb_gpu_fetch (cursor); int op_type = op.r; + int flags = op.g; int payload = (op.b << 16) | (op.a & 0xffff); if (op_type == 0) /* LAYER_SOLID */ { + /* texel 1 holds RGBA as signed Q15. */ ivec4 ct = hb_gpu_fetch (cursor + 1); - int palette_index = ct.r; - int flags = ct.g; - float alpha = float (ct.b) / 32767.0; - vec4 col = ((flags & 1) != 0) ? foreground - : hb_gpu_palette[palette_index]; - col.a *= alpha; + : vec4 (ct) / 32767.0; /* payload is a blob-relative texel offset; sub-blobs live * inside the paint blob at atlas offset `base`. */ diff --git a/src/hb-gpu-paint-fragment.hlsl b/src/hb-gpu-paint-fragment.hlsl index 5222b4d73..10a46f9b8 100644 --- a/src/hb-gpu-paint-fragment.hlsl +++ b/src/hb-gpu-paint-fragment.hlsl @@ -27,10 +27,7 @@ * * Assumes the shared fragment helpers (hb-gpu-fragment.hlsl) and * the draw-renderer fragment helpers (hb-gpu-draw-fragment.hlsl) - * are prepended to this source. The caller must also declare the - * palette buffer before this source, e.g.: - * - * StructuredBuffer hb_gpu_palette : register(t1); + * are prepended to this source. */ @@ -51,19 +48,16 @@ float4 hb_gpu_paint (float2 renderCoord, uint glyphLoc_, float4 foreground) { int4 op = hb_gpu_fetch (cursor); int op_type = op.r; + int flags = op.g; int payload = (op.b << 16) | (op.a & 0xffff); if (op_type == 0) /* LAYER_SOLID */ { + /* texel 1 holds RGBA as signed Q15. */ int4 ct = hb_gpu_fetch (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; + : (float4) ct / 32767.0; float cov = _hb_gpu_draw_impl (renderCoord, pixelsPerEm, (uint) (base + payload)); diff --git a/src/hb-gpu-paint-fragment.msl b/src/hb-gpu-paint-fragment.msl index 345ce32bb..9e4bbc7a2 100644 --- a/src/hb-gpu-paint-fragment.msl +++ b/src/hb-gpu-paint-fragment.msl @@ -28,15 +28,11 @@ * 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) + device const short4* hb_gpu_atlas) { /* fwidth once, at uniform control flow. */ float2 pixelsPerEm = 1.0 / fwidth (renderCoord); @@ -53,19 +49,16 @@ float4 hb_gpu_paint (float2 renderCoord, uint glyphLoc_, float4 foreground, { int4 op = int4 (hb_gpu_atlas[cursor]); int op_type = op.r; + int flags = op.g; int payload = (op.b << 16) | (op.a & 0xffff); if (op_type == 0) /* LAYER_SOLID */ { + /* texel 1 holds RGBA as signed Q15. */ 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; + : float4 (ct) / 32767.0; float cov = _hb_gpu_draw_impl (renderCoord, pixelsPerEm, uint (base + payload), hb_gpu_atlas); diff --git a/src/hb-gpu-paint-fragment.wgsl b/src/hb-gpu-paint-fragment.wgsl index 1c4a3f92e..81de92a46 100644 --- a/src/hb-gpu-paint-fragment.wgsl +++ b/src/hb-gpu-paint-fragment.wgsl @@ -29,14 +29,13 @@ * the draw-renderer fragment helpers (hb-gpu-draw-fragment.wgsl) * are prepended to this source. * - * atlas and palette are passed as explicit storage-buffer pointer - * parameters, matching WGSL's scoping rules. + * atlas is passed as an explicit storage-buffer pointer parameter, + * matching WGSL's scoping rules. */ fn hb_gpu_paint (renderCoord: vec2f, glyphLoc_: u32, foreground: vec4f, - hb_gpu_atlas: ptr>, read>, - hb_gpu_palette: ptr, read>) -> vec4f + hb_gpu_atlas: ptr>, read>) -> vec4f { /* Compute pixelsPerEm once here at uniform control flow. WGSL * rejects fwidth inside a loop-conditional branch, so we call @@ -58,21 +57,18 @@ fn hb_gpu_paint (renderCoord: vec2f, glyphLoc_: u32, foreground: vec4f, { let op = hb_gpu_fetch (hb_gpu_atlas, cursor); let op_type = op.r; + let flags = op.g; let payload = (op.b << 16) | (op.a & 0xffff); if (op_type == 0) { // LAYER_SOLID + // texel 1 holds RGBA as signed Q15. let ct = hb_gpu_fetch (hb_gpu_atlas, cursor + 1); - let palette_index = ct.r; - let flags = ct.g; - let alpha = f32 (ct.b) / 32767.0; - var col: vec4f; if ((flags & 1) != 0) { col = foreground; } else { - col = (*hb_gpu_palette)[palette_index]; + col = vec4f (ct) / 32767.0; } - col.a = col.a * alpha; let cov = _hb_gpu_draw_impl (renderCoord, pixelsPerEm, u32 (base + payload), hb_gpu_atlas); diff --git a/src/hb-gpu-paint.cc b/src/hb-gpu-paint.cc index 5f1173b45..b3c998092 100644 --- a/src/hb-gpu-paint.cc +++ b/src/hb-gpu-paint.cc @@ -46,16 +46,15 @@ static hb_bool_t hb_gpu_paint_custom_palette_color (hb_paint_funcs_t *funcs HB_UNUSED, void *paint_data, unsigned color_index, - hb_color_t *color HB_UNUSED, + hb_color_t *color, void *user_data HB_UNUSED) { hb_gpu_paint_t *c = (hb_gpu_paint_t *) paint_data; - /* Stash the palette index. The next color / gradient-stop - * callback will read it so the encoded blob carries indices - * rather than resolved RGBA. Return false so harfbuzz still - * resolves the color from the palette for the callback -- we - * ignore the resolved value downstream. */ - c->pending_palette_index = color_index; + if (likely (c->custom_palette && hb_map_has (c->custom_palette, color_index))) + { + *color = hb_map_get (c->custom_palette, color_index); + return true; + } return false; } @@ -71,10 +70,6 @@ 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; - /* 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 @@ -90,7 +85,7 @@ static void hb_gpu_paint_color (hb_paint_funcs_t *funcs HB_UNUSED, void *paint_data, hb_bool_t is_foreground, - hb_color_t color HB_UNUSED, + hb_color_t color, void *user_data HB_UNUSED) { hb_gpu_paint_t *c = (hb_gpu_paint_t *) paint_data; @@ -135,10 +130,12 @@ hb_gpu_paint_color (hb_paint_funcs_t *funcs HB_UNUSED, unsigned sub_blob_index = c->sub_blobs.length - 1; /* Emit LAYER_SOLID op: 2 texels (8 int16s). - * op_type, aux, payload_hi, payload_lo, - * palette_index, flags, alpha_q15, _reserved + * texel 0: op_type, flags, payload_hi, payload_lo + * texel 1: r_q15, g_q15, b_q15, a_q15 * Payload holds the sub_blob index; hb_gpu_paint_encode() - * patches it into a texel offset at serialize time. */ + * patches it into a texel offset at serialize time. + * flags bit 0: use shader foreground uniform instead of the + * baked color (so runtime foreground-color changes still work). */ if (unlikely (!c->ops.resize (c->ops.length + 8))) { c->unsupported = true; @@ -146,13 +143,18 @@ hb_gpu_paint_color (hb_paint_funcs_t *funcs HB_UNUSED, } int16_t *o = &c->ops.arrayZ[c->ops.length - 8]; o[0] = HB_GPU_PAINT_OP_LAYER_SOLID; - o[1] = 0; + o[1] = (int16_t) (is_foreground ? 1 : 0); o[2] = (int16_t) ((sub_blob_index >> 16) & 0xffff); o[3] = (int16_t) (sub_blob_index & 0xffff); - o[4] = (int16_t) (is_foreground ? 0 : c->pending_palette_index); - o[5] = (int16_t) (is_foreground ? 1 : 0); - o[6] = 0x7fff; /* alpha_q15 = 1.0 */ - o[7] = 0; + /* Quantize RGBA to signed Q15 (0..1 -> 0..32767). hb_color_t is + * BGRA-ordered in bytes; getters normalize. */ + auto to_q15 = [] (unsigned v) -> int16_t { + return (int16_t) ((v * 32767u + 127u) / 255u); + }; + o[4] = to_q15 (hb_color_get_red (color)); + o[5] = to_q15 (hb_color_get_green (color)); + o[6] = to_q15 (hb_color_get_blue (color)); + o[7] = to_q15 (hb_color_get_alpha (color)); c->num_ops++; } @@ -298,6 +300,118 @@ hb_gpu_paint_get_funcs (void) return static_gpu_paint_funcs.get_unconst (); } +/** + * hb_gpu_paint_set_foreground: + * @paint: a GPU color-glyph paint encoder + * @foreground: the foreground color + * + * Sets the foreground color used to resolve palette references that + * specify the foreground sentinel (palette index 0xFFFF). + * + * XSince: REPLACEME + **/ +void +hb_gpu_paint_set_foreground (hb_gpu_paint_t *paint, + hb_color_t foreground) +{ + paint->foreground = foreground; +} + +/** + * hb_gpu_paint_get_foreground: + * @paint: a GPU color-glyph paint encoder + * + * Returns the foreground color previously set on @paint, or the + * default opaque black if none was set. + * + * Return value: the foreground color. + * + * XSince: REPLACEME + **/ +hb_color_t +hb_gpu_paint_get_foreground (const hb_gpu_paint_t *paint) +{ + return paint->foreground; +} + +/** + * hb_gpu_paint_set_palette: + * @paint: a GPU color-glyph paint encoder + * @palette: palette index + * + * Selects which font palette is used when paint callbacks look up + * indexed colors. Default is palette 0. + * + * XSince: REPLACEME + **/ +void +hb_gpu_paint_set_palette (hb_gpu_paint_t *paint, + unsigned palette) +{ + paint->palette = palette; +} + +/** + * hb_gpu_paint_get_palette: + * @paint: a GPU color-glyph paint encoder + * + * Returns the palette index previously set on @paint, or 0 if none + * was set. + * + * Return value: the palette index. + * + * XSince: REPLACEME + **/ +unsigned +hb_gpu_paint_get_palette (const hb_gpu_paint_t *paint) +{ + return paint->palette; +} + +/** + * hb_gpu_paint_clear_custom_palette_colors: + * @paint: a GPU color-glyph paint encoder + * + * Clears all custom palette color overrides previously set on @paint. + * + * XSince: REPLACEME + **/ +void +hb_gpu_paint_clear_custom_palette_colors (hb_gpu_paint_t *paint) +{ + if (paint->custom_palette) + hb_map_clear (paint->custom_palette); +} + +/** + * hb_gpu_paint_set_custom_palette_color: + * @paint: a GPU color-glyph paint encoder + * @color_index: palette index to override + * @color: replacement color + * + * Overrides one font palette color entry. Overrides are keyed by + * @color_index and persist on @paint until cleared (or replaced for + * the same index). + * + * Return value: `true` if the override was set; `false` on allocation failure. + * + * XSince: REPLACEME + **/ +hb_bool_t +hb_gpu_paint_set_custom_palette_color (hb_gpu_paint_t *paint, + unsigned int color_index, + hb_color_t color) +{ + if (unlikely (!paint->custom_palette)) + { + paint->custom_palette = hb_map_create (); + if (unlikely (!paint->custom_palette)) + return false; + } + hb_map_set (paint->custom_palette, color_index, color); + return hb_map_allocation_successful (paint->custom_palette); +} + /** * hb_gpu_paint_glyph: * @paint: a GPU color-glyph paint encoder @@ -319,17 +433,13 @@ hb_gpu_paint_glyph (hb_gpu_paint_t *paint, hb_codepoint_t glyph) { 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. - * - * Use hb_font_paint_glyph (not _or_fail) so that non-color + /* 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)); + paint->palette, paint->foreground); return !paint->unsupported; } @@ -485,7 +595,6 @@ hb_gpu_paint_clear (hb_gpu_paint_t *paint) hb_blob_destroy (b); paint->sub_blobs.resize (0); paint->pending_clip = false; - paint->pending_palette_index = 0; paint->unsupported = false; paint->ext_min_x = 0x7fffffff; paint->ext_min_y = 0x7fffffff; @@ -507,6 +616,10 @@ hb_gpu_paint_reset (hb_gpu_paint_t *paint) { paint->x_scale = 0; paint->y_scale = 0; + paint->foreground = HB_COLOR (0, 0, 0, 0xff); + paint->palette = 0; + hb_map_destroy (paint->custom_palette); + paint->custom_palette = nullptr; hb_gpu_paint_clear (paint); } diff --git a/src/hb-gpu-paint.hh b/src/hb-gpu-paint.hh index 9c40c50ee..55f4b39a4 100644 --- a/src/hb-gpu-paint.hh +++ b/src/hb-gpu-paint.hh @@ -29,6 +29,7 @@ #include "hb.hh" #include "hb-gpu.h" +#include "hb-map.hh" #include "hb-object.hh" @@ -78,16 +79,13 @@ * op_type values: * * 0 LAYER_SOLID - * aux = 0 + * aux = flags (bit 0 = is_foreground) * payload = clip_glyph_offset (texel index) * followed by one extra texel: - * (palette_index:i16, flags:i16, - * alpha_q15:i16, reserved:i16) - * flags bit 0: is_foreground -- sample - * hb_gpu_foreground uniform instead of - * hb_gpu_palette[palette_index] - * alpha_q15: modulation applied on top of the looked-up - * color, 0..0x7FFF + * (r_q15:i16, g_q15:i16, b_q15:i16, a_q15:i16) + * flags bit 0: is_foreground -- use the shader's foreground + * uniform/varying instead of the baked color + * (so runtime foreground changes still work) * * 1 LAYER_GRADIENT * aux = gradient subtype (0=linear, 1=radial, 2=sweep) @@ -151,27 +149,17 @@ * * Returns premultiplied RGBA. The caller passes foreground in * explicitly; sourcing it from a uniform, a per-quad flat varying, - * or anywhere else is the caller's decision. A per-quad attribute - * is the natural choice for styled text runs (no state flushes - * between colored glyphs); a uniform is fine for single-color - * rendering. + * or anywhere else is the caller's decision. * - * Palette lookups target: - * - * uniform vec4 hb_gpu_palette[HB_GPU_PALETTE_SIZE]; - * - * This is the same implicit-global relationship hb_gpu_draw has with - * hb_gpu_atlas / hb_gpu_fetch. HB_GPU_PALETTE_SIZE defaults to 256 - * -- enough for real COLRv1 fonts (Noto Color Emoji uses ~150 - * entries). The caller fills the uniform from - * hb_ot_color_palette_get_colors() for whichever palette is active - * and updates it whenever the palette changes (dark/light mode, - * user overrides) -- no re-encoding. - * - * Color stops and solid-fill colors in the blob carry a - * palette_index + flags rather than resolved RGBA, so blobs are - * palette-independent. The is_foreground flag routes to the - * foreground parameter instead of the palette array. + * Colors are baked into the blob at encode time (as signed Q15 + * RGBA in a single texel per layer). There is no runtime palette + * buffer to bind or size. Palette selection and per-run custom + * palette overrides are set on the encoder via + * hb_gpu_paint_set_palette() / hb_gpu_paint_set_custom_palette_color(); + * changing either invalidates previously encoded blobs and requires + * re-encoding. The is_foreground flag still routes through the + * shader's foreground parameter so a dark-mode toggle does not + * require re-encoding. * * --- Fragment-shader contract ------------------------------------ * @@ -183,9 +171,7 @@ * for op in ops: * LAYER_SOLID: * cov = hb_gpu_draw (texcoord, clip_glyph_offset) - * col = is_foreground ? v_foreground - * : hb_gpu_palette[palette_index] - * col.a *= alpha_q15 / 32767 + * col = is_foreground ? v_foreground : rgba_q15 / 32767 * acc = src_over (col * cov, acc) * LAYER_GRADIENT: * cov = hb_gpu_draw (texcoord, clip_glyph_offset) @@ -254,6 +240,11 @@ struct hb_gpu_paint_t { hb_object_header_t header; + /* Persistent configuration (survives hb_gpu_paint_clear). */ + hb_color_t foreground = HB_COLOR (0, 0, 0, 0xff); + unsigned palette = 0; + hb_map_t *custom_palette = nullptr; + /* Font scale (set by hb_gpu_paint_glyph()). */ int x_scale = 0; int y_scale = 0; @@ -276,11 +267,6 @@ struct hb_gpu_paint_t bool pending_clip = false; hb_codepoint_t pending_clip_glyph = 0; hb_font_t *pending_clip_font = nullptr; /* borrowed */ - unsigned pending_palette_index = 0; /* written by - * custom_palette_color_func; - * ignored when the color - * callback reports - * is_foreground */ /* Set when the paint walk emits v1-only callbacks we do not yet * support. hb_gpu_paint_encode() returns NULL in that case. */ @@ -300,6 +286,7 @@ struct hb_gpu_paint_t ~hb_gpu_paint_t () { + hb_map_destroy (custom_palette); for (hb_blob_t *b : sub_blobs) hb_blob_destroy (b); hb_gpu_draw_destroy (scratch_draw); hb_blob_destroy (recycled_blob); diff --git a/src/hb-gpu.h b/src/hb-gpu.h index 8699e0423..e68dddfc3 100644 --- a/src/hb-gpu.h +++ b/src/hb-gpu.h @@ -179,6 +179,28 @@ hb_gpu_paint_get_user_data (const hb_gpu_paint_t *paint, HB_EXTERN hb_paint_funcs_t * hb_gpu_paint_get_funcs (void); +HB_EXTERN void +hb_gpu_paint_set_foreground (hb_gpu_paint_t *paint, + hb_color_t foreground); + +HB_EXTERN hb_color_t +hb_gpu_paint_get_foreground (const hb_gpu_paint_t *paint); + +HB_EXTERN void +hb_gpu_paint_set_palette (hb_gpu_paint_t *paint, + unsigned palette); + +HB_EXTERN unsigned +hb_gpu_paint_get_palette (const hb_gpu_paint_t *paint); + +HB_EXTERN void +hb_gpu_paint_clear_custom_palette_colors (hb_gpu_paint_t *paint); + +HB_EXTERN hb_bool_t +hb_gpu_paint_set_custom_palette_color (hb_gpu_paint_t *paint, + unsigned int color_index, + hb_color_t color); + HB_EXTERN hb_bool_t hb_gpu_paint_glyph (hb_gpu_paint_t *paint, hb_font_t *font,