From 6cf5e1a8581385fa86e4dd2627cd71bb97b8c577 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 13 Apr 2026 19:31:04 -0600 Subject: [PATCH] [gpu] hb_gpu_paint: encode linear gradients as LAYER_GRADIENT The linear_gradient callback now emits a real LAYER_GRADIENT op instead of falling back to a flat first-stop solid. Per-layer op layout: texel 0: (op_type=1, subtype=0, clip_payload_hi, clip_payload_lo) texel 1: (grad_payload_hi, grad_payload_lo, extend_mode, stop_count) Both payloads are sub_blob indices that hb_gpu_paint_encode() patches to absolute atlas offsets. The clip payload is the existing Slug draw blob for the layer's clip glyph; the gradient payload is a fresh sub-blob laid out as: texel 0 : (lx0, ly0, lx1, ly1) -- 2-point axis in font units, reduced from COLRv1's 3-point anchors the same way hb_raster_paint's reduce_anchors does it texels 1..N : color stops, 2 texels each texel a: (offset_q15, flags, _, _) flags bit 0 = is_foreground texel b: (r_q15, g_q15, b_q15, a_q15) All four paint fragment shaders (GLSL/MSL/HLSL/WGSL) grew a _hb_gpu_sample_linear helper that projects the fragment onto the axis, applies the COLRv1 extend mode, and walks stops to find the enclosing segment. Refactored the existing LAYER_SOLID emission body into emit_clip_sub_blob + hb_gpu_paint_emit_solid so the gradient path can share the clip-outline encode and extents accumulation. The LAYER_GRADIENT encode-time patching walks both payload slots. Radial and sweep callbacks still fall back to the first-stop solid placeholder; they'll be replaced next. Assisted-by: Claude Opus 4.6 (1M context) --- src/hb-gpu-paint-fragment-glsl.hh | 93 +++++++++++- src/hb-gpu-paint-fragment-hlsl.hh | 83 ++++++++++- src/hb-gpu-paint-fragment-msl.hh | 88 ++++++++++- src/hb-gpu-paint-fragment-wgsl.hh | 87 ++++++++++- src/hb-gpu-paint-fragment.glsl | 93 +++++++++++- src/hb-gpu-paint-fragment.hlsl | 83 ++++++++++- src/hb-gpu-paint-fragment.msl | 88 ++++++++++- src/hb-gpu-paint-fragment.wgsl | 87 ++++++++++- src/hb-gpu-paint.cc | 236 ++++++++++++++++++++++++++---- 9 files changed, 863 insertions(+), 75 deletions(-) diff --git a/src/hb-gpu-paint-fragment-glsl.hh b/src/hb-gpu-paint-fragment-glsl.hh index 431ae696d..d423e96b5 100644 --- a/src/hb-gpu-paint-fragment-glsl.hh +++ b/src/hb-gpu-paint-fragment-glsl.hh @@ -34,6 +34,66 @@ static const char *hb_gpu_paint_fragment_glsl = " */\n" "\n" "\n" +"/* Fetch the i'th stop of a gradient color line starting at @stops_base\n" +" * (2 texels per stop). Resolves is_foreground to @foreground. */\n" +"vec4 _hb_gpu_stop_color (int stops_base, int i, vec4 foreground, out float offset)\n" +"{\n" +" ivec4 a = hb_gpu_fetch (stops_base + i * 2);\n" +" offset = float (a.r) / 32767.0;\n" +" if ((a.g & 1) != 0)\n" +" return foreground;\n" +" ivec4 b = hb_gpu_fetch (stops_base + i * 2 + 1);\n" +" return vec4 (b) / 32767.0;\n" +"}\n" +"\n" +"/* Apply the color-line extend mode to a projected `t` value. */\n" +"float _hb_gpu_extend_t (float t, int extend)\n" +"{\n" +" if (extend == 1) { /* HB_PAINT_EXTEND_REPEAT */\n" +" return t - floor (t);\n" +" } else if (extend == 2) { /* HB_PAINT_EXTEND_REFLECT */\n" +" float u = t - 2.0 * floor (t * 0.5);\n" +" return u > 1.0 ? 2.0 - u : u;\n" +" }\n" +" return clamp (t, 0.0, 1.0); /* PAD (default) */\n" +"}\n" +"\n" +"/* Sample a linear gradient whose param blob starts at @grad_base:\n" +" * texel 0: (x0, y0, x1, y1) in font units (i16)\n" +" * texels 1..: stops (2 texels each) */\n" +"vec4 _hb_gpu_sample_linear (vec2 renderCoord, int grad_base,\n" +" int stop_count, int extend, vec4 foreground)\n" +"{\n" +" ivec4 axis = hb_gpu_fetch (grad_base);\n" +" vec2 p0 = vec2 (float (axis.r), float (axis.g));\n" +" vec2 p1 = vec2 (float (axis.b), float (axis.a));\n" +" vec2 d = p1 - p0;\n" +" float denom = dot (d, d);\n" +" if (denom < 1e-6) return vec4 (0.0);\n" +" float t = dot (renderCoord - p0, d) / denom;\n" +" t = _hb_gpu_extend_t (t, extend);\n" +"\n" +" int stops_base = grad_base + 1;\n" +" float off_prev;\n" +" vec4 col_prev = _hb_gpu_stop_color (stops_base, 0, foreground, off_prev);\n" +" if (t <= off_prev)\n" +" return col_prev;\n" +" for (int i = 1; i < stop_count; i++)\n" +" {\n" +" float off;\n" +" vec4 col = _hb_gpu_stop_color (stops_base, i, foreground, off);\n" +" if (t <= off)\n" +" {\n" +" float span = off - off_prev;\n" +" float f = span > 1e-6 ? (t - off_prev) / span : 0.0;\n" +" return mix (col_prev, col, f);\n" +" }\n" +" col_prev = col;\n" +" off_prev = off;\n" +" }\n" +" return col_prev;\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" @@ -60,30 +120,49 @@ 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 aux = 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" -" vec4 col = ((flags & 1) != 0)\n" +" vec4 col = ((aux & 1) != 0)\n" " ? foreground\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" " float cov = _hb_gpu_draw_impl (renderCoord, pixelsPerEm,\n" " uint (base + payload));\n" -" vec4 src = col * cov;\n" -" /* SRC_OVER with premultiplied source. */\n" +" vec4 src = vec4 (col.rgb * col.a, col.a) * cov;\n" +" acc = src + acc * (1.0 - src.a);\n" +"\n" +" cursor += 2;\n" +" }\n" +" else if (op_type == 1) /* LAYER_GRADIENT */\n" +" {\n" +" /* texel 1: (grad_payload_hi, grad_payload_lo, extend, stop_count) */\n" +" ivec4 op2 = hb_gpu_fetch (cursor + 1);\n" +" int grad_payload = (op2.r << 16) | (op2.g & 0xffff);\n" +" int extend = op2.b;\n" +" int stop_count = op2.a;\n" +"\n" +" vec4 col = vec4 (0.0);\n" +" if (aux == 0) /* linear */\n" +" col = _hb_gpu_sample_linear (renderCoord,\n" +" base + grad_payload,\n" +" stop_count, extend, foreground);\n" +" /* Radial / sweep fall through as transparent for now. */\n" +"\n" +" float cov = _hb_gpu_draw_impl (renderCoord, pixelsPerEm,\n" +" uint (base + payload));\n" +" vec4 src = vec4 (col.rgb * col.a, col.a) * cov;\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" +" /* PUSH_GROUP, POP_GROUP not yet handled. */\n" " break;\n" " }\n" " }\n" diff --git a/src/hb-gpu-paint-fragment-hlsl.hh b/src/hb-gpu-paint-fragment-hlsl.hh index 0212b706a..f2575c307 100644 --- a/src/hb-gpu-paint-fragment-hlsl.hh +++ b/src/hb-gpu-paint-fragment-hlsl.hh @@ -32,6 +32,59 @@ static const char *hb_gpu_paint_fragment_hlsl = " */\n" "\n" "\n" +"float4 _hb_gpu_stop_color (int stops_base, int i, float4 foreground,\n" +" out float offset)\n" +"{\n" +" int4 a = hb_gpu_fetch (stops_base + i * 2);\n" +" offset = (float) a.r / 32767.0;\n" +" if ((a.g & 1) != 0)\n" +" return foreground;\n" +" int4 b = hb_gpu_fetch (stops_base + i * 2 + 1);\n" +" return (float4) b / 32767.0;\n" +"}\n" +"\n" +"float _hb_gpu_extend_t (float t, int extend)\n" +"{\n" +" if (extend == 1) return t - floor (t);\n" +" if (extend == 2) {\n" +" float u = t - 2.0 * floor (t * 0.5);\n" +" return u > 1.0 ? 2.0 - u : u;\n" +" }\n" +" return clamp (t, 0.0, 1.0);\n" +"}\n" +"\n" +"float4 _hb_gpu_sample_linear (float2 renderCoord, int grad_base,\n" +" int stop_count, int extend, float4 foreground)\n" +"{\n" +" int4 axis = hb_gpu_fetch (grad_base);\n" +" float2 p0 = float2 ((float) axis.r, (float) axis.g);\n" +" float2 p1 = float2 ((float) axis.b, (float) axis.a);\n" +" float2 d = p1 - p0;\n" +" float denom = dot (d, d);\n" +" if (denom < 1e-6) return float4 (0.0, 0.0, 0.0, 0.0);\n" +" float t = dot (renderCoord - p0, d) / denom;\n" +" t = _hb_gpu_extend_t (t, extend);\n" +"\n" +" int stops_base = grad_base + 1;\n" +" float off_prev;\n" +" float4 col_prev = _hb_gpu_stop_color (stops_base, 0, foreground, off_prev);\n" +" if (t <= off_prev) return col_prev;\n" +" for (int i = 1; i < stop_count; i++)\n" +" {\n" +" float off;\n" +" float4 col = _hb_gpu_stop_color (stops_base, i, foreground, off);\n" +" if (t <= off)\n" +" {\n" +" float span = off - off_prev;\n" +" float f = span > 1e-6 ? (t - off_prev) / span : 0.0;\n" +" return lerp (col_prev, col, f);\n" +" }\n" +" col_prev = col;\n" +" off_prev = off;\n" +" }\n" +" return col_prev;\n" +"}\n" +"\n" "float4 hb_gpu_paint (float2 renderCoord, uint glyphLoc_, float4 foreground)\n" "{\n" " /* fwidth once, at uniform control flow. */\n" @@ -49,28 +102,46 @@ 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 aux = 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" -" float4 col = ((flags & 1) != 0)\n" +" float4 col = ((aux & 1) != 0)\n" " ? foreground\n" " : (float4) ct / 32767.0;\n" "\n" " float cov = _hb_gpu_draw_impl (renderCoord, pixelsPerEm,\n" " (uint) (base + payload));\n" -" float4 src = col * cov;\n" -" /* SRC_OVER with premultiplied source. */\n" +" float4 src = float4 (col.rgb * col.a, col.a) * cov;\n" +" acc = src + acc * (1.0 - src.a);\n" +"\n" +" cursor += 2;\n" +" }\n" +" else if (op_type == 1) /* LAYER_GRADIENT */\n" +" {\n" +" int4 op2 = hb_gpu_fetch (cursor + 1);\n" +" int grad_payload = (op2.r << 16) | (op2.g & 0xffff);\n" +" int extend = op2.b;\n" +" int stop_count = op2.a;\n" +"\n" +" float4 col = float4 (0.0, 0.0, 0.0, 0.0);\n" +" if (aux == 0)\n" +" col = _hb_gpu_sample_linear (renderCoord,\n" +" base + grad_payload,\n" +" stop_count, extend, foreground);\n" +"\n" +" float cov = _hb_gpu_draw_impl (renderCoord, pixelsPerEm,\n" +" (uint) (base + payload));\n" +" float4 src = float4 (col.rgb * col.a, col.a) * cov;\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" +" /* PUSH_GROUP, POP_GROUP not yet handled. */\n" " break;\n" " }\n" " }\n" diff --git a/src/hb-gpu-paint-fragment-msl.hh b/src/hb-gpu-paint-fragment-msl.hh index d0af38125..5a7f19b53 100644 --- a/src/hb-gpu-paint-fragment-msl.hh +++ b/src/hb-gpu-paint-fragment-msl.hh @@ -32,6 +32,63 @@ static const char *hb_gpu_paint_fragment_msl = " */\n" "\n" "\n" +"/* Fetch the i'th stop (2 texels per stop), resolving is_foreground. */\n" +"static float4 _hb_gpu_stop_color (device const short4* hb_gpu_atlas,\n" +" int stops_base, int i, float4 foreground,\n" +" thread float &offset)\n" +"{\n" +" int4 a = int4 (hb_gpu_atlas[stops_base + i * 2]);\n" +" offset = float (a.r) / 32767.0;\n" +" if ((a.g & 1) != 0)\n" +" return foreground;\n" +" int4 b = int4 (hb_gpu_atlas[stops_base + i * 2 + 1]);\n" +" return float4 (b) / 32767.0;\n" +"}\n" +"\n" +"static float _hb_gpu_extend_t (float t, int extend)\n" +"{\n" +" if (extend == 1) return t - floor (t);\n" +" if (extend == 2) {\n" +" float u = t - 2.0 * floor (t * 0.5);\n" +" return u > 1.0 ? 2.0 - u : u;\n" +" }\n" +" return clamp (t, 0.0, 1.0);\n" +"}\n" +"\n" +"static float4 _hb_gpu_sample_linear (float2 renderCoord, int grad_base,\n" +" int stop_count, int extend,\n" +" float4 foreground,\n" +" device const short4* hb_gpu_atlas)\n" +"{\n" +" int4 axis = int4 (hb_gpu_atlas[grad_base]);\n" +" float2 p0 = float2 (float (axis.r), float (axis.g));\n" +" float2 p1 = float2 (float (axis.b), float (axis.a));\n" +" float2 d = p1 - p0;\n" +" float denom = dot (d, d);\n" +" if (denom < 1e-6) return float4 (0.0);\n" +" float t = dot (renderCoord - p0, d) / denom;\n" +" t = _hb_gpu_extend_t (t, extend);\n" +"\n" +" int stops_base = grad_base + 1;\n" +" float off_prev;\n" +" float4 col_prev = _hb_gpu_stop_color (hb_gpu_atlas, stops_base, 0, foreground, off_prev);\n" +" if (t <= off_prev) return col_prev;\n" +" for (int i = 1; i < stop_count; i++)\n" +" {\n" +" float off;\n" +" float4 col = _hb_gpu_stop_color (hb_gpu_atlas, stops_base, i, foreground, off);\n" +" if (t <= off)\n" +" {\n" +" float span = off - off_prev;\n" +" float f = span > 1e-6 ? (t - off_prev) / span : 0.0;\n" +" return mix (col_prev, col, f);\n" +" }\n" +" col_prev = col;\n" +" off_prev = off;\n" +" }\n" +" return col_prev;\n" +"}\n" +"\n" "float4 hb_gpu_paint (float2 renderCoord, uint glyphLoc_, float4 foreground,\n" " device const short4* hb_gpu_atlas)\n" "{\n" @@ -50,28 +107,47 @@ 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 aux = 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" -" float4 col = ((flags & 1) != 0)\n" +" float4 col = ((aux & 1) != 0)\n" " ? foreground\n" " : float4 (ct) / 32767.0;\n" "\n" " float cov = _hb_gpu_draw_impl (renderCoord, pixelsPerEm,\n" " uint (base + payload), hb_gpu_atlas);\n" -" float4 src = col * cov;\n" -" /* SRC_OVER with premultiplied source. */\n" +" float4 src = float4 (col.rgb * col.a, col.a) * cov;\n" +" acc = src + acc * (1.0 - src.a);\n" +"\n" +" cursor += 2;\n" +" }\n" +" else if (op_type == 1) /* LAYER_GRADIENT */\n" +" {\n" +" int4 op2 = int4 (hb_gpu_atlas[cursor + 1]);\n" +" int grad_payload = (op2.r << 16) | (op2.g & 0xffff);\n" +" int extend = op2.b;\n" +" int stop_count = op2.a;\n" +"\n" +" float4 col = float4 (0.0);\n" +" if (aux == 0)\n" +" col = _hb_gpu_sample_linear (renderCoord,\n" +" base + grad_payload,\n" +" stop_count, extend, foreground,\n" +" hb_gpu_atlas);\n" +"\n" +" float cov = _hb_gpu_draw_impl (renderCoord, pixelsPerEm,\n" +" uint (base + payload), hb_gpu_atlas);\n" +" float4 src = float4 (col.rgb * col.a, col.a) * cov;\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" +" /* PUSH_GROUP, POP_GROUP not yet handled. */\n" " break;\n" " }\n" " }\n" diff --git a/src/hb-gpu-paint-fragment-wgsl.hh b/src/hb-gpu-paint-fragment-wgsl.hh index 0f0e621b1..e89e8e703 100644 --- a/src/hb-gpu-paint-fragment-wgsl.hh +++ b/src/hb-gpu-paint-fragment-wgsl.hh @@ -35,6 +35,64 @@ static const char *hb_gpu_paint_fragment_wgsl = " */\n" "\n" "\n" +"fn _hb_gpu_stop_color (hb_gpu_atlas: ptr>, read>,\n" +" stops_base: i32, i: i32, foreground: vec4f,\n" +" offset: ptr) -> vec4f\n" +"{\n" +" let a = hb_gpu_fetch (hb_gpu_atlas, stops_base + i * 2);\n" +" *offset = f32 (a.r) / 32767.0;\n" +" if ((a.g & 1) != 0) {\n" +" return foreground;\n" +" }\n" +" let b = hb_gpu_fetch (hb_gpu_atlas, stops_base + i * 2 + 1);\n" +" return vec4f (b) / 32767.0;\n" +"}\n" +"\n" +"fn _hb_gpu_extend_t (t: f32, extend: i32) -> f32\n" +"{\n" +" if (extend == 1) { return t - floor (t); }\n" +" if (extend == 2) {\n" +" let u = t - 2.0 * floor (t * 0.5);\n" +" if (u > 1.0) { return 2.0 - u; }\n" +" return u;\n" +" }\n" +" return clamp (t, 0.0, 1.0);\n" +"}\n" +"\n" +"fn _hb_gpu_sample_linear (renderCoord: vec2f, grad_base: i32,\n" +" stop_count: i32, extend: i32, foreground: vec4f,\n" +" hb_gpu_atlas: ptr>, read>) -> vec4f\n" +"{\n" +" let axis = hb_gpu_fetch (hb_gpu_atlas, grad_base);\n" +" let p0 = vec2f (f32 (axis.r), f32 (axis.g));\n" +" let p1 = vec2f (f32 (axis.b), f32 (axis.a));\n" +" let d = p1 - p0;\n" +" let denom = dot (d, d);\n" +" if (denom < 1e-6) { return vec4f (0.0); }\n" +" var t = dot (renderCoord - p0, d) / denom;\n" +" t = _hb_gpu_extend_t (t, extend);\n" +"\n" +" let stops_base = grad_base + 1;\n" +" var off_prev: f32;\n" +" var col_prev = _hb_gpu_stop_color (hb_gpu_atlas, stops_base, 0, foreground, &off_prev);\n" +" if (t <= off_prev) { return col_prev; }\n" +" for (var i: i32 = 1; i < stop_count; i = i + 1)\n" +" {\n" +" var off: f32;\n" +" let col = _hb_gpu_stop_color (hb_gpu_atlas, stops_base, i, foreground, &off);\n" +" if (t <= off)\n" +" {\n" +" let span = off - off_prev;\n" +" var f: f32 = 0.0;\n" +" if (span > 1e-6) { f = (t - off_prev) / span; }\n" +" return mix (col_prev, col, f);\n" +" }\n" +" col_prev = col;\n" +" off_prev = off;\n" +" }\n" +" return col_prev;\n" +"}\n" +"\n" "fn hb_gpu_paint (renderCoord: vec2f, glyphLoc_: u32, foreground: vec4f,\n" " hb_gpu_atlas: ptr>, read>) -> vec4f\n" "{\n" @@ -58,14 +116,14 @@ 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 aux = 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" " var col: vec4f;\n" -" if ((flags & 1) != 0) {\n" +" if ((aux & 1) != 0) {\n" " col = foreground;\n" " } else {\n" " col = vec4f (ct) / 32767.0;\n" @@ -73,13 +131,32 @@ static const char *hb_gpu_paint_fragment_wgsl = "\n" " let cov = _hb_gpu_draw_impl (renderCoord, pixelsPerEm,\n" " u32 (base + payload), hb_gpu_atlas);\n" -" let src = col * cov;\n" -" /* SRC_OVER with premultiplied source. */\n" +" let src = vec4f (col.rgb * col.a, col.a) * cov;\n" +" acc = src + acc * (1.0 - src.a);\n" +"\n" +" cursor = cursor + 2;\n" +" } else if (op_type == 1) { // LAYER_GRADIENT\n" +" let op2 = hb_gpu_fetch (hb_gpu_atlas, cursor + 1);\n" +" let grad_payload = (op2.r << 16) | (op2.g & 0xffff);\n" +" let extend = op2.b;\n" +" let stop_count = op2.a;\n" +"\n" +" var col = vec4f (0.0);\n" +" if (aux == 0) {\n" +" col = _hb_gpu_sample_linear (renderCoord,\n" +" base + grad_payload,\n" +" stop_count, extend, foreground,\n" +" hb_gpu_atlas);\n" +" }\n" +"\n" +" let cov = _hb_gpu_draw_impl (renderCoord, pixelsPerEm,\n" +" u32 (base + payload), hb_gpu_atlas);\n" +" let src = vec4f (col.rgb * col.a, col.a) * cov;\n" " acc = src + acc * (1.0 - src.a);\n" "\n" " cursor = cursor + 2;\n" " } else {\n" -" /* LAYER_GRADIENT, PUSH_GROUP, POP_GROUP not yet handled. */\n" +" /* PUSH_GROUP, POP_GROUP not yet handled. */\n" " break;\n" " }\n" " }\n" diff --git a/src/hb-gpu-paint-fragment.glsl b/src/hb-gpu-paint-fragment.glsl index e7213810f..00155c9f8 100644 --- a/src/hb-gpu-paint-fragment.glsl +++ b/src/hb-gpu-paint-fragment.glsl @@ -33,6 +33,66 @@ */ +/* Fetch the i'th stop of a gradient color line starting at @stops_base + * (2 texels per stop). Resolves is_foreground to @foreground. */ +vec4 _hb_gpu_stop_color (int stops_base, int i, vec4 foreground, out float offset) +{ + ivec4 a = hb_gpu_fetch (stops_base + i * 2); + offset = float (a.r) / 32767.0; + if ((a.g & 1) != 0) + return foreground; + ivec4 b = hb_gpu_fetch (stops_base + i * 2 + 1); + return vec4 (b) / 32767.0; +} + +/* Apply the color-line extend mode to a projected `t` value. */ +float _hb_gpu_extend_t (float t, int extend) +{ + if (extend == 1) { /* HB_PAINT_EXTEND_REPEAT */ + return t - floor (t); + } else if (extend == 2) { /* HB_PAINT_EXTEND_REFLECT */ + float u = t - 2.0 * floor (t * 0.5); + return u > 1.0 ? 2.0 - u : u; + } + return clamp (t, 0.0, 1.0); /* PAD (default) */ +} + +/* Sample a linear gradient whose param blob starts at @grad_base: + * texel 0: (x0, y0, x1, y1) in font units (i16) + * texels 1..: stops (2 texels each) */ +vec4 _hb_gpu_sample_linear (vec2 renderCoord, int grad_base, + int stop_count, int extend, vec4 foreground) +{ + ivec4 axis = hb_gpu_fetch (grad_base); + vec2 p0 = vec2 (float (axis.r), float (axis.g)); + vec2 p1 = vec2 (float (axis.b), float (axis.a)); + vec2 d = p1 - p0; + float denom = dot (d, d); + if (denom < 1e-6) return vec4 (0.0); + float t = dot (renderCoord - p0, d) / denom; + t = _hb_gpu_extend_t (t, extend); + + int stops_base = grad_base + 1; + float off_prev; + vec4 col_prev = _hb_gpu_stop_color (stops_base, 0, foreground, off_prev); + if (t <= off_prev) + return col_prev; + for (int i = 1; i < stop_count; i++) + { + float off; + vec4 col = _hb_gpu_stop_color (stops_base, i, foreground, off); + if (t <= off) + { + float span = off - off_prev; + float f = span > 1e-6 ? (t - off_prev) / span : 0.0; + return mix (col_prev, col, f); + } + col_prev = col; + off_prev = off; + } + return col_prev; +} + /* Walks the paint blob's flat op stream and returns a * premultiplied RGBA coverage value for the current fragment. * @@ -59,30 +119,49 @@ 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 aux = 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); - vec4 col = ((flags & 1) != 0) + vec4 col = ((aux & 1) != 0) ? foreground : vec4 (ct) / 32767.0; - /* payload is a blob-relative texel offset; sub-blobs live - * inside the paint blob at atlas offset `base`. */ float cov = _hb_gpu_draw_impl (renderCoord, pixelsPerEm, uint (base + payload)); - vec4 src = col * cov; - /* SRC_OVER with premultiplied source. */ + vec4 src = vec4 (col.rgb * col.a, col.a) * cov; + acc = src + acc * (1.0 - src.a); + + cursor += 2; + } + else if (op_type == 1) /* LAYER_GRADIENT */ + { + /* texel 1: (grad_payload_hi, grad_payload_lo, extend, stop_count) */ + ivec4 op2 = hb_gpu_fetch (cursor + 1); + int grad_payload = (op2.r << 16) | (op2.g & 0xffff); + int extend = op2.b; + int stop_count = op2.a; + + vec4 col = vec4 (0.0); + if (aux == 0) /* linear */ + col = _hb_gpu_sample_linear (renderCoord, + base + grad_payload, + stop_count, extend, foreground); + /* Radial / sweep fall through as transparent for now. */ + + float cov = _hb_gpu_draw_impl (renderCoord, pixelsPerEm, + uint (base + payload)); + vec4 src = vec4 (col.rgb * col.a, col.a) * cov; acc = src + acc * (1.0 - src.a); cursor += 2; } else { - /* LAYER_GRADIENT, PUSH_GROUP, POP_GROUP not yet handled. */ + /* PUSH_GROUP, POP_GROUP not yet handled. */ break; } } diff --git a/src/hb-gpu-paint-fragment.hlsl b/src/hb-gpu-paint-fragment.hlsl index 10a46f9b8..637da1daa 100644 --- a/src/hb-gpu-paint-fragment.hlsl +++ b/src/hb-gpu-paint-fragment.hlsl @@ -31,6 +31,59 @@ */ +float4 _hb_gpu_stop_color (int stops_base, int i, float4 foreground, + out float offset) +{ + int4 a = hb_gpu_fetch (stops_base + i * 2); + offset = (float) a.r / 32767.0; + if ((a.g & 1) != 0) + return foreground; + int4 b = hb_gpu_fetch (stops_base + i * 2 + 1); + return (float4) b / 32767.0; +} + +float _hb_gpu_extend_t (float t, int extend) +{ + if (extend == 1) return t - floor (t); + if (extend == 2) { + float u = t - 2.0 * floor (t * 0.5); + return u > 1.0 ? 2.0 - u : u; + } + return clamp (t, 0.0, 1.0); +} + +float4 _hb_gpu_sample_linear (float2 renderCoord, int grad_base, + int stop_count, int extend, float4 foreground) +{ + int4 axis = hb_gpu_fetch (grad_base); + float2 p0 = float2 ((float) axis.r, (float) axis.g); + float2 p1 = float2 ((float) axis.b, (float) axis.a); + float2 d = p1 - p0; + float denom = dot (d, d); + if (denom < 1e-6) return float4 (0.0, 0.0, 0.0, 0.0); + float t = dot (renderCoord - p0, d) / denom; + t = _hb_gpu_extend_t (t, extend); + + int stops_base = grad_base + 1; + float off_prev; + float4 col_prev = _hb_gpu_stop_color (stops_base, 0, foreground, off_prev); + if (t <= off_prev) return col_prev; + for (int i = 1; i < stop_count; i++) + { + float off; + float4 col = _hb_gpu_stop_color (stops_base, i, foreground, off); + if (t <= off) + { + float span = off - off_prev; + float f = span > 1e-6 ? (t - off_prev) / span : 0.0; + return lerp (col_prev, col, f); + } + col_prev = col; + off_prev = off; + } + return col_prev; +} + float4 hb_gpu_paint (float2 renderCoord, uint glyphLoc_, float4 foreground) { /* fwidth once, at uniform control flow. */ @@ -48,28 +101,46 @@ 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 aux = 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); - float4 col = ((flags & 1) != 0) + float4 col = ((aux & 1) != 0) ? foreground : (float4) ct / 32767.0; float cov = _hb_gpu_draw_impl (renderCoord, pixelsPerEm, (uint) (base + payload)); - float4 src = col * cov; - /* SRC_OVER with premultiplied source. */ + float4 src = float4 (col.rgb * col.a, col.a) * cov; + acc = src + acc * (1.0 - src.a); + + cursor += 2; + } + else if (op_type == 1) /* LAYER_GRADIENT */ + { + int4 op2 = hb_gpu_fetch (cursor + 1); + int grad_payload = (op2.r << 16) | (op2.g & 0xffff); + int extend = op2.b; + int stop_count = op2.a; + + float4 col = float4 (0.0, 0.0, 0.0, 0.0); + if (aux == 0) + col = _hb_gpu_sample_linear (renderCoord, + base + grad_payload, + stop_count, extend, foreground); + + float cov = _hb_gpu_draw_impl (renderCoord, pixelsPerEm, + (uint) (base + payload)); + float4 src = float4 (col.rgb * col.a, col.a) * cov; acc = src + acc * (1.0 - src.a); cursor += 2; } else { - /* LAYER_GRADIENT, PUSH_GROUP, POP_GROUP not yet handled. */ + /* PUSH_GROUP, POP_GROUP not yet handled. */ break; } } diff --git a/src/hb-gpu-paint-fragment.msl b/src/hb-gpu-paint-fragment.msl index 9e4bbc7a2..cadb1f1cd 100644 --- a/src/hb-gpu-paint-fragment.msl +++ b/src/hb-gpu-paint-fragment.msl @@ -31,6 +31,63 @@ */ +/* Fetch the i'th stop (2 texels per stop), resolving is_foreground. */ +static float4 _hb_gpu_stop_color (device const short4* hb_gpu_atlas, + int stops_base, int i, float4 foreground, + thread float &offset) +{ + int4 a = int4 (hb_gpu_atlas[stops_base + i * 2]); + offset = float (a.r) / 32767.0; + if ((a.g & 1) != 0) + return foreground; + int4 b = int4 (hb_gpu_atlas[stops_base + i * 2 + 1]); + return float4 (b) / 32767.0; +} + +static float _hb_gpu_extend_t (float t, int extend) +{ + if (extend == 1) return t - floor (t); + if (extend == 2) { + float u = t - 2.0 * floor (t * 0.5); + return u > 1.0 ? 2.0 - u : u; + } + return clamp (t, 0.0, 1.0); +} + +static float4 _hb_gpu_sample_linear (float2 renderCoord, int grad_base, + int stop_count, int extend, + float4 foreground, + device const short4* hb_gpu_atlas) +{ + int4 axis = int4 (hb_gpu_atlas[grad_base]); + float2 p0 = float2 (float (axis.r), float (axis.g)); + float2 p1 = float2 (float (axis.b), float (axis.a)); + float2 d = p1 - p0; + float denom = dot (d, d); + if (denom < 1e-6) return float4 (0.0); + float t = dot (renderCoord - p0, d) / denom; + t = _hb_gpu_extend_t (t, extend); + + int stops_base = grad_base + 1; + float off_prev; + float4 col_prev = _hb_gpu_stop_color (hb_gpu_atlas, stops_base, 0, foreground, off_prev); + if (t <= off_prev) return col_prev; + for (int i = 1; i < stop_count; i++) + { + float off; + float4 col = _hb_gpu_stop_color (hb_gpu_atlas, stops_base, i, foreground, off); + if (t <= off) + { + float span = off - off_prev; + float f = span > 1e-6 ? (t - off_prev) / span : 0.0; + return mix (col_prev, col, f); + } + col_prev = col; + off_prev = off; + } + return col_prev; +} + float4 hb_gpu_paint (float2 renderCoord, uint glyphLoc_, float4 foreground, device const short4* hb_gpu_atlas) { @@ -49,28 +106,47 @@ 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 aux = 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]); - float4 col = ((flags & 1) != 0) + float4 col = ((aux & 1) != 0) ? foreground : float4 (ct) / 32767.0; float cov = _hb_gpu_draw_impl (renderCoord, pixelsPerEm, uint (base + payload), hb_gpu_atlas); - float4 src = col * cov; - /* SRC_OVER with premultiplied source. */ + float4 src = float4 (col.rgb * col.a, col.a) * cov; + acc = src + acc * (1.0 - src.a); + + cursor += 2; + } + else if (op_type == 1) /* LAYER_GRADIENT */ + { + int4 op2 = int4 (hb_gpu_atlas[cursor + 1]); + int grad_payload = (op2.r << 16) | (op2.g & 0xffff); + int extend = op2.b; + int stop_count = op2.a; + + float4 col = float4 (0.0); + if (aux == 0) + col = _hb_gpu_sample_linear (renderCoord, + base + grad_payload, + stop_count, extend, foreground, + hb_gpu_atlas); + + float cov = _hb_gpu_draw_impl (renderCoord, pixelsPerEm, + uint (base + payload), hb_gpu_atlas); + float4 src = float4 (col.rgb * col.a, col.a) * cov; acc = src + acc * (1.0 - src.a); cursor += 2; } else { - /* LAYER_GRADIENT, PUSH_GROUP, POP_GROUP not yet handled. */ + /* PUSH_GROUP, POP_GROUP not yet handled. */ break; } } diff --git a/src/hb-gpu-paint-fragment.wgsl b/src/hb-gpu-paint-fragment.wgsl index 81de92a46..2a10086dc 100644 --- a/src/hb-gpu-paint-fragment.wgsl +++ b/src/hb-gpu-paint-fragment.wgsl @@ -34,6 +34,64 @@ */ +fn _hb_gpu_stop_color (hb_gpu_atlas: ptr>, read>, + stops_base: i32, i: i32, foreground: vec4f, + offset: ptr) -> vec4f +{ + let a = hb_gpu_fetch (hb_gpu_atlas, stops_base + i * 2); + *offset = f32 (a.r) / 32767.0; + if ((a.g & 1) != 0) { + return foreground; + } + let b = hb_gpu_fetch (hb_gpu_atlas, stops_base + i * 2 + 1); + return vec4f (b) / 32767.0; +} + +fn _hb_gpu_extend_t (t: f32, extend: i32) -> f32 +{ + if (extend == 1) { return t - floor (t); } + if (extend == 2) { + let u = t - 2.0 * floor (t * 0.5); + if (u > 1.0) { return 2.0 - u; } + return u; + } + return clamp (t, 0.0, 1.0); +} + +fn _hb_gpu_sample_linear (renderCoord: vec2f, grad_base: i32, + stop_count: i32, extend: i32, foreground: vec4f, + hb_gpu_atlas: ptr>, read>) -> vec4f +{ + let axis = hb_gpu_fetch (hb_gpu_atlas, grad_base); + let p0 = vec2f (f32 (axis.r), f32 (axis.g)); + let p1 = vec2f (f32 (axis.b), f32 (axis.a)); + let d = p1 - p0; + let denom = dot (d, d); + if (denom < 1e-6) { return vec4f (0.0); } + var t = dot (renderCoord - p0, d) / denom; + t = _hb_gpu_extend_t (t, extend); + + let stops_base = grad_base + 1; + var off_prev: f32; + var col_prev = _hb_gpu_stop_color (hb_gpu_atlas, stops_base, 0, foreground, &off_prev); + if (t <= off_prev) { return col_prev; } + for (var i: i32 = 1; i < stop_count; i = i + 1) + { + var off: f32; + let col = _hb_gpu_stop_color (hb_gpu_atlas, stops_base, i, foreground, &off); + if (t <= off) + { + let span = off - off_prev; + var f: f32 = 0.0; + if (span > 1e-6) { f = (t - off_prev) / span; } + return mix (col_prev, col, f); + } + col_prev = col; + off_prev = off; + } + return col_prev; +} + fn hb_gpu_paint (renderCoord: vec2f, glyphLoc_: u32, foreground: vec4f, hb_gpu_atlas: ptr>, read>) -> vec4f { @@ -57,14 +115,14 @@ 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 aux = 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); var col: vec4f; - if ((flags & 1) != 0) { + if ((aux & 1) != 0) { col = foreground; } else { col = vec4f (ct) / 32767.0; @@ -72,13 +130,32 @@ fn hb_gpu_paint (renderCoord: vec2f, glyphLoc_: u32, foreground: vec4f, let cov = _hb_gpu_draw_impl (renderCoord, pixelsPerEm, u32 (base + payload), hb_gpu_atlas); - let src = col * cov; - /* SRC_OVER with premultiplied source. */ + let src = vec4f (col.rgb * col.a, col.a) * cov; + acc = src + acc * (1.0 - src.a); + + cursor = cursor + 2; + } else if (op_type == 1) { // LAYER_GRADIENT + let op2 = hb_gpu_fetch (hb_gpu_atlas, cursor + 1); + let grad_payload = (op2.r << 16) | (op2.g & 0xffff); + let extend = op2.b; + let stop_count = op2.a; + + var col = vec4f (0.0); + if (aux == 0) { + col = _hb_gpu_sample_linear (renderCoord, + base + grad_payload, + stop_count, extend, foreground, + hb_gpu_atlas); + } + + let cov = _hb_gpu_draw_impl (renderCoord, pixelsPerEm, + u32 (base + payload), hb_gpu_atlas); + let src = vec4f (col.rgb * col.a, col.a) * cov; acc = src + acc * (1.0 - src.a); cursor = cursor + 2; } else { - /* LAYER_GRADIENT, PUSH_GROUP, POP_GROUP not yet handled. */ + /* PUSH_GROUP, POP_GROUP not yet handled. */ break; } } diff --git a/src/hb-gpu-paint.cc b/src/hb-gpu-paint.cc index 6c8460733..4914e2e2b 100644 --- a/src/hb-gpu-paint.cc +++ b/src/hb-gpu-paint.cc @@ -81,27 +81,49 @@ hb_gpu_paint_pop_clip (hb_paint_funcs_t *funcs HB_UNUSED, c->pending_clip = false; } -static void -hb_gpu_paint_emit_solid (hb_gpu_paint_t *c, - hb_bool_t is_foreground, - hb_color_t color) +/* Quantize unsigned byte 0..255 to signed Q15 0..32767. */ +static inline int16_t +color_to_q15 (unsigned byte_value) { - if (unlikely (!c->pending_clip)) - return; + return (int16_t) ((byte_value * 32767u + 127u) / 255u); +} - /* Rasterize the clip-glyph outline into a Slug-format sub-blob. */ +static inline void +color_to_q15_rgba (hb_color_t color, int16_t out[4]) +{ + out[0] = color_to_q15 (hb_color_get_red (color)); + out[1] = color_to_q15 (hb_color_get_green (color)); + out[2] = color_to_q15 (hb_color_get_blue (color)); + out[3] = color_to_q15 (hb_color_get_alpha (color)); +} + +static inline int16_t +clamp_i16 (float v) +{ + if (v <= -32768.f) return -32768; + if (v >= 32767.f) return 32767; + return (int16_t) v; +} + +/* Rasterize the pending clip glyph's outline into a new sub-blob + * and accumulate its extents. Returns the sub_blob index (>= 0) + * on success, or -1 if the outline is empty / the layer should be + * skipped. Sets c->unsupported on hard failure. */ +static int +emit_clip_sub_blob (hb_gpu_paint_t *c) +{ if (unlikely (!c->scratch_draw)) { c->scratch_draw = hb_gpu_draw_create_or_fail (); if (unlikely (!c->scratch_draw)) { c->unsupported = true; - return; + return -1; } } hb_gpu_draw_clear (c->scratch_draw); if (!hb_gpu_draw_glyph (c->scratch_draw, c->pending_clip_font, c->pending_clip_glyph)) - return; /* Clip glyph has no outline -- skip the layer. */ + return -1; /* Clip glyph has no outline -- skip. */ hb_glyph_extents_t ext; hb_blob_t *blob = hb_gpu_draw_encode (c->scratch_draw, &ext); @@ -109,7 +131,7 @@ hb_gpu_paint_emit_solid (hb_gpu_paint_t *c, { hb_blob_destroy (blob); c->unsupported = true; - return; + return -1; } /* Accumulate extents: x_bearing/y_bearing are top-left, width @@ -123,7 +145,20 @@ hb_gpu_paint_emit_solid (hb_gpu_paint_t *c, c->ext_min_y = hb_min (c->ext_min_y, hb_min (y0, y1)); c->ext_max_y = hb_max (c->ext_max_y, hb_max (y0, y1)); - unsigned sub_blob_index = c->sub_blobs.length - 1; + return (int) (c->sub_blobs.length - 1); +} + +static void +hb_gpu_paint_emit_solid (hb_gpu_paint_t *c, + hb_bool_t is_foreground, + hb_color_t color) +{ + if (unlikely (!c->pending_clip)) + return; + + int clip_idx = emit_clip_sub_blob (c); + if (clip_idx < 0) + return; /* Emit LAYER_SOLID op: 2 texels (8 int16s). * texel 0: op_type, flags, payload_hi, payload_lo @@ -140,17 +175,145 @@ hb_gpu_paint_emit_solid (hb_gpu_paint_t *c, int16_t *o = &c->ops.arrayZ[c->ops.length - 8]; o[0] = HB_GPU_PAINT_OP_LAYER_SOLID; 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); - /* 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)); + o[2] = (int16_t) ((clip_idx >> 16) & 0xffff); + o[3] = (int16_t) (clip_idx & 0xffff); + color_to_q15_rgba (color, o + 4); + c->num_ops++; +} + +/* Reduce the 3-point anchor COLRv1 linear-gradient spec to a + * 2-point axis (the perpendicular foot of p1 onto the p0-p2 + * normal). Same derivation as hb_raster_paint's reduce_anchors. */ +static inline void +reduce_linear_anchors (float x0, float y0, + float x1, float y1, + float x2, float y2, + float *xx0, float *yy0, + float *xx1, float *yy1) +{ + float q2x = x2 - x0, q2y = y2 - y0; + float q1x = x1 - x0, q1y = y1 - y0; + float s = q2x * q2x + q2y * q2y; + if (s < 1e-6f) + { + *xx0 = x0; *yy0 = y0; + *xx1 = x1; *yy1 = y1; + return; + } + float k = (q2x * q1x + q2y * q1y) / s; + *xx0 = x0; + *yy0 = y0; + *xx1 = x1 - k * q2x; + *yy1 = y1 - k * q2y; +} + +/* Append a color line (@count stops) to @grad_data in the encoded + * stop format used by gradient ops: 2 texels per stop. + * texel a: (offset_q15, flags, _, _) with flags bit 0 = is_foreground + * texel b: (r_q15, g_q15, b_q15, a_q15) + * Returns false on allocation failure. */ +static bool +append_color_stops (hb_vector_t &grad_data, + const hb_color_stop_t *stops, + unsigned count) +{ + unsigned base = grad_data.length; + if (unlikely (!grad_data.resize (base + count * 8))) + return false; + int16_t *p = grad_data.arrayZ + base; + for (unsigned i = 0; i < count; i++) + { + float off = stops[i].offset; + if (off < -1.f) off = -1.f; + else if (off > 1.f) off = 1.f; + p[0] = (int16_t) (off * 32767.f); + p[1] = (int16_t) (stops[i].is_foreground ? 1 : 0); + p[2] = 0; + p[3] = 0; + color_to_q15_rgba (stops[i].color, p + 4); + p += 8; + } + return true; +} + +static void +hb_gpu_paint_emit_linear (hb_gpu_paint_t *c, + hb_color_line_t *color_line, + float x0, float y0, + float x1, float y1, + float x2, float y2) +{ + if (unlikely (!c->pending_clip)) + return; + + /* Resolve stops (triggers custom_palette_color, etc). */ + unsigned count = hb_color_line_get_color_stops (color_line, 0, nullptr, nullptr); + if (unlikely (!count)) + return; + hb_color_stop_t stack_stops[16]; + hb_color_stop_t *stops = stack_stops; + hb_color_stop_t *heap_stops = nullptr; + if (count > 16) + { + heap_stops = (hb_color_stop_t *) hb_malloc (count * sizeof (hb_color_stop_t)); + if (unlikely (!heap_stops)) { c->unsupported = true; return; } + stops = heap_stops; + } + unsigned got = count; + hb_color_line_get_color_stops (color_line, 0, &got, stops); + + hb_paint_extend_t extend = hb_color_line_get_extend (color_line); + + /* Emit clip sub-blob first. */ + int clip_idx = emit_clip_sub_blob (c); + if (clip_idx < 0) { hb_free (heap_stops); return; } + + /* Build gradient params sub-blob. + * texel 0: (lx0, ly0, lx1, ly1) in font units (i16) + * texels 1..N: color stops (2 texels each) + */ + hb_vector_t grad_data; + if (unlikely (!grad_data.resize (4))) + { c->unsupported = true; hb_free (heap_stops); return; } + + float lx0, ly0, lx1, ly1; + reduce_linear_anchors (x0, y0, x1, y1, x2, y2, &lx0, &ly0, &lx1, &ly1); + grad_data[0] = clamp_i16 (lx0); + grad_data[1] = clamp_i16 (ly0); + grad_data[2] = clamp_i16 (lx1); + grad_data[3] = clamp_i16 (ly1); + + if (unlikely (!append_color_stops (grad_data, stops, got))) + { c->unsupported = true; hb_free (heap_stops); return; } + hb_free (heap_stops); + + unsigned grad_bytes = grad_data.length * sizeof (int16_t); + hb_blob_t *grad_blob = hb_blob_create ((const char *) grad_data.arrayZ, + grad_bytes, HB_MEMORY_MODE_DUPLICATE, + nullptr, nullptr); + if (unlikely (!grad_blob || !c->sub_blobs.push (grad_blob))) + { + hb_blob_destroy (grad_blob); + c->unsupported = true; + return; + } + unsigned grad_idx = c->sub_blobs.length - 1; + + /* Emit LAYER_GRADIENT op: 2 texels. + * texel 0: (op_type, subtype, clip_payload_hi, clip_payload_lo) + * texel 1: (grad_payload_hi, grad_payload_lo, extend_mode, stop_count) + */ + if (unlikely (!c->ops.resize (c->ops.length + 8))) + { c->unsupported = true; return; } + int16_t *o = &c->ops.arrayZ[c->ops.length - 8]; + o[0] = HB_GPU_PAINT_OP_LAYER_GRADIENT; + o[1] = 0; /* subtype: 0 = linear */ + o[2] = (int16_t) ((clip_idx >> 16) & 0xffff); + o[3] = (int16_t) (clip_idx & 0xffff); + o[4] = (int16_t) ((grad_idx >> 16) & 0xffff); + o[5] = (int16_t) (grad_idx & 0xffff); + o[6] = (int16_t) extend; + o[7] = (int16_t) got; c->num_ops++; } @@ -186,12 +349,13 @@ static void hb_gpu_paint_linear_gradient (hb_paint_funcs_t *funcs HB_UNUSED, void *paint_data, hb_color_line_t *color_line, - float x0 HB_UNUSED, float y0 HB_UNUSED, - float x1 HB_UNUSED, float y1 HB_UNUSED, - float x2 HB_UNUSED, float y2 HB_UNUSED, + float x0, float y0, + float x1, float y1, + float x2, float y2, void *user_data HB_UNUSED) { - hb_gpu_paint_gradient_first_stop ((hb_gpu_paint_t *) paint_data, color_line); + hb_gpu_paint_emit_linear ((hb_gpu_paint_t *) paint_data, color_line, + x0, y0, x1, y1, x2, y2); } static void @@ -593,7 +757,6 @@ hb_gpu_paint_encode (hb_gpu_paint_t *paint, switch (op_type) { case HB_GPU_PAINT_OP_LAYER_SOLID: - case HB_GPU_PAINT_OP_LAYER_GRADIENT: { unsigned idx = ((uint16_t) ops_out[i + 2] << 16) | (uint16_t) ops_out[i + 3]; @@ -606,6 +769,25 @@ hb_gpu_paint_encode (hb_gpu_paint_t *paint, i += 8; /* 2 texels */ break; } + case HB_GPU_PAINT_OP_LAYER_GRADIENT: + { + /* Two sub-blob references: clip at [i+2,i+3], gradient + * params at [i+4,i+5]. */ + for (unsigned slot = 0; slot < 2; slot++) + { + unsigned p = i + 2 + slot * 2; + unsigned idx = ((uint16_t) ops_out[p] << 16) + | (uint16_t) ops_out[p + 1]; + if (idx < sub_offsets_count) + { + unsigned off = sub_offsets[idx]; + ops_out[p] = (int16_t) ((off >> 16) & 0xffff); + ops_out[p + 1] = (int16_t) (off & 0xffff); + } + } + i += 8; /* 2 texels */ + break; + } case HB_GPU_PAINT_OP_PUSH_GROUP: case HB_GPU_PAINT_OP_POP_GROUP: i += 4; /* 1 texel */