[gpu] Port paint fragment shader to WGSL; wire WebGPU demo

WGSL transcription of the paint interpreter.  Takes the atlas and
palette as explicit storage-buffer pointer parameters (matching
the WGSL draw helper's calling convention):

  fn hb_gpu_paint (renderCoord: vec2f, glyphLoc: u32,
		   foreground: vec4f,
		   hb_gpu_atlas: ptr<storage, array<vec4<i32>>, read>,
		   hb_gpu_palette: ptr<storage, array<vec4f>, read>) -> vec4f

hb_gpu_paint_shader_source() now returns real content for
HB_GPU_SHADER_LANG_WGSL fragment; HLSL still stubs to "".

util/gpu/web/hb-gpu-web-webgpu.cc:
  - Concats the paint source alongside the draw source into the
    WGSL shader module.
  - Declares @group(0) @binding(2) for hb_gpu_palette.
  - Allocates a 256 * vec4f storage buffer (zero-filled for now;
    real palette upload is TODO), binds it at entry index 2.
  - Fragment main switches from hb_gpu_draw to hb_gpu_paint.

util/gpu/web/gl-stub.h: adds glUniform4fv stub (demo-font.cc now
calls it for the GL palette path) and makes
glGetUniformLocation return -1 so the WebGPU build's
demo_font_upload_palette short-circuits.

Wasm sizes:
  WebGL   index.wasm  : 443628  181597 gz
  WebGPU  webgpu.wasm : 461110  188045 gz

Non-color text should render through WebGPU.  COLRv0 color glyphs
come up transparent until the cross-backend palette upload lands.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Behdad Esfahbod committed 2026-04-13 15:27:41 -06:00
1 parent 5e2b77d2ea
commit d5f07d72d0
6 files changed
+196 -17

No files matched your search

+84
View File
@@ -0,0 +1,84 @@
static const char *hb_gpu_paint_fragment_wgsl =
"/*\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 (WGSL).\n"
" *\n"
" * Assumes the shared fragment helpers (hb-gpu-fragment.wgsl) and\n"
" * 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"
" */\n"
"\n"
"\n"
"fn hb_gpu_paint (renderCoord: vec2f, glyphLoc_: u32, foreground: vec4f,\n"
" hb_gpu_atlas: ptr<storage, array<vec4<i32>>, read>,\n"
" hb_gpu_palette: ptr<storage, array<vec4f>, read>) -> vec4f\n"
"{\n"
" let base = i32 (glyphLoc_);\n"
" let h0 = hb_gpu_fetch (hb_gpu_atlas, base); // (num_ops, _, _, _)\n"
" let h2 = hb_gpu_fetch (hb_gpu_atlas, base + 2); // (ops_offset, _, _, _)\n"
" let num_ops = h0.r;\n"
" var cursor = base + h2.r;\n"
"\n"
" var acc = vec4f (0.0);\n"
"\n"
" for (var i: i32 = 0; i < num_ops; i = i + 1)\n"
" {\n"
" let op = hb_gpu_fetch (hb_gpu_atlas, cursor);\n"
" let op_type = op.r;\n"
" let payload = (op.b << 16) | (op.a & 0xffff);\n"
"\n"
" if (op_type == 0) { // LAYER_SOLID\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"
" }\n"
" col.a = col.a * alpha;\n"
"\n"
" let cov = hb_gpu_draw (renderCoord, u32 (base + payload), hb_gpu_atlas);\n"
" let src = col * cov;\n"
" /* SRC_OVER with premultiplied source. */\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"
" break;\n"
" }\n"
" }\n"
"\n"
" return acc;\n"
"}\n"
;
+82
View File
@@ -0,0 +1,82 @@
/*
* 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 (WGSL).
*
* Assumes the shared fragment helpers (hb-gpu-fragment.wgsl) and
* 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.
*/
fn hb_gpu_paint (renderCoord: vec2f, glyphLoc_: u32, foreground: vec4f,
hb_gpu_atlas: ptr<storage, array<vec4<i32>>, read>,
hb_gpu_palette: ptr<storage, array<vec4f>, read>) -> vec4f
{
let base = i32 (glyphLoc_);
let h0 = hb_gpu_fetch (hb_gpu_atlas, base); // (num_ops, _, _, _)
let h2 = hb_gpu_fetch (hb_gpu_atlas, base + 2); // (ops_offset, _, _, _)
let num_ops = h0.r;
var cursor = base + h2.r;
var acc = vec4f (0.0);
for (var i: i32 = 0; i < num_ops; i = i + 1)
{
let op = hb_gpu_fetch (hb_gpu_atlas, cursor);
let op_type = op.r;
let payload = (op.b << 16) | (op.a & 0xffff);
if (op_type == 0) { // LAYER_SOLID
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.a = col.a * alpha;
let cov = hb_gpu_draw (renderCoord, u32 (base + payload), hb_gpu_atlas);
let src = col * cov;
/* SRC_OVER with premultiplied source. */
acc = src + acc * (1.0 - src.a);
cursor = cursor + 2;
} else {
/* LAYER_GRADIENT, PUSH_GROUP, POP_GROUP not yet handled. */
break;
}
}
return acc;
}
+2 -1
View File
@@ -563,6 +563,7 @@ hb_gpu_paint_recycle_blob (hb_gpu_paint_t *paint HB_UNUSED,
**/
#include "hb-gpu-paint-fragment-glsl.hh"
#include "hb-gpu-paint-fragment-msl.hh"
#include "hb-gpu-paint-fragment-wgsl.hh"
const char *
hb_gpu_paint_shader_source (hb_gpu_shader_stage_t stage,
@@ -573,7 +574,7 @@ hb_gpu_paint_shader_source (hb_gpu_shader_stage_t stage,
switch (lang) {
case HB_GPU_SHADER_LANG_GLSL: return hb_gpu_paint_fragment_glsl;
case HB_GPU_SHADER_LANG_MSL: return hb_gpu_paint_fragment_msl;
case HB_GPU_SHADER_LANG_WGSL:
case HB_GPU_SHADER_LANG_WGSL: return hb_gpu_paint_fragment_wgsl;
case HB_GPU_SHADER_LANG_HLSL: return "";
default: return nullptr;
}
+1
View File
@@ -839,6 +839,7 @@ hb_gpu_shader_sources = [
[ '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-paint-fragment.wgsl', 'hb-gpu-paint-fragment-wgsl.hh' ],
]
hb_gpu_shader_helper = find_program('gen-gpu-shader-artifacts.py')