From d5f07d72d045ba9563ea7bbfbebc8eaaff8f1a30 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 13 Apr 2026 15:27:41 -0600 Subject: [PATCH] [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>, read>, hb_gpu_palette: ptr, 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) --- src/hb-gpu-paint-fragment-wgsl.hh | 84 +++++++++++++++++++++++++++++++ src/hb-gpu-paint-fragment.wgsl | 82 ++++++++++++++++++++++++++++++ src/hb-gpu-paint.cc | 3 +- src/meson.build | 1 + util/gpu/web/gl-stub.h | 3 +- util/gpu/web/hb-gpu-web-webgpu.cc | 40 +++++++++------ 6 files changed, 196 insertions(+), 17 deletions(-) create mode 100644 src/hb-gpu-paint-fragment-wgsl.hh create mode 100644 src/hb-gpu-paint-fragment.wgsl diff --git a/src/hb-gpu-paint-fragment-wgsl.hh b/src/hb-gpu-paint-fragment-wgsl.hh new file mode 100644 index 000000000..a33e98d6b --- /dev/null +++ b/src/hb-gpu-paint-fragment-wgsl.hh @@ -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>, read>,\n" +" hb_gpu_palette: ptr, 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" +; diff --git a/src/hb-gpu-paint-fragment.wgsl b/src/hb-gpu-paint-fragment.wgsl new file mode 100644 index 000000000..aacb312e7 --- /dev/null +++ b/src/hb-gpu-paint-fragment.wgsl @@ -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>, read>, + hb_gpu_palette: ptr, 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; +} diff --git a/src/hb-gpu-paint.cc b/src/hb-gpu-paint.cc index 024f116f7..4f6c6a760 100644 --- a/src/hb-gpu-paint.cc +++ b/src/hb-gpu-paint.cc @@ -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; } diff --git a/src/meson.build b/src/meson.build index 9cb55ffe3..2c8a2616e 100644 --- a/src/meson.build +++ b/src/meson.build @@ -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') diff --git a/util/gpu/web/gl-stub.h b/util/gpu/web/gl-stub.h index efb5bbf6e..f6ee5d78e 100644 --- a/util/gpu/web/gl-stub.h +++ b/util/gpu/web/gl-stub.h @@ -48,8 +48,9 @@ static inline void glTexParameteri (GLenum, GLenum, GLint) {} static inline void glTexImage2D (GLenum, GLint, GLint, GLsizei, GLsizei, GLint, GLenum, GLenum, const void *) {} static inline void glTexSubImage2D (GLenum, GLint, GLint, GLint, GLsizei, GLsizei, GLenum, GLenum, const void *) {} static inline void glTexBuffer (GLenum, GLenum, GLuint) {} -static inline GLint glGetUniformLocation (GLuint, const GLchar *) { return 0; } +static inline GLint glGetUniformLocation (GLuint, const GLchar *) { return -1; } static inline void glUniform1i (GLint, GLint) {} +static inline void glUniform4fv (GLint, GLsizei, const GLfloat *) {} static inline GLenum glGetError () { return 0; } /* GLFW type and constant stubs */ diff --git a/util/gpu/web/hb-gpu-web-webgpu.cc b/util/gpu/web/hb-gpu-web-webgpu.cc index b7f723875..7867fdbb6 100644 --- a/util/gpu/web/hb-gpu-web-webgpu.cc +++ b/util/gpu/web/hb-gpu-web-webgpu.cc @@ -72,6 +72,7 @@ struct Uniforms { @group(0) @binding(0) var u: Uniforms; @group(0) @binding(1) var hb_gpu_atlas: array>; +@group(0) @binding(2) var hb_gpu_palette: array; struct VertexInput { @location(0) position: vec2f, @@ -103,26 +104,21 @@ struct VertexOutput { } @fragment fn fs_main (in: VertexOutput) -> @location(0) vec4f { - var coverage = hb_gpu_draw (in.texcoord, in.glyphLoc, &hb_gpu_atlas); - - if (u.stem_darkening > 0.0) { - coverage = hb_gpu_stem_darken (coverage, - dot (u.foreground.rgb, vec3f (1.0 / 3.0)), - 1.0 / max (fwidth (in.texcoord).x, fwidth (in.texcoord).y)); - } + var c = hb_gpu_paint (in.texcoord, in.glyphLoc, u.foreground, + &hb_gpu_atlas, &hb_gpu_palette); if (u.gamma != 1.0) { - coverage = pow (coverage, u.gamma); + c.a = pow (c.a, u.gamma); } if (u.debug > 0.0) { let counts = _hb_gpu_curve_counts (in.texcoord, in.glyphLoc, &hb_gpu_atlas); let r = clamp (f32 (counts.x) / 8.0, 0.0, 1.0); let g = clamp (f32 (counts.y) / 8.0, 0.0, 1.0); - return vec4f (r, g, coverage, max (max (r, g), coverage)); + return vec4f (r, g, c.a, max (max (r, g), c.a)); } - return vec4f (u.foreground.rgb, u.foreground.a * coverage); + return c; } )wgsl"; @@ -191,6 +187,7 @@ static WGPUQueue g_queue; static WGPUSurface g_surface; static WGPURenderPipeline g_pipeline; static WGPUBuffer g_uniform_buf; +static WGPUBuffer g_palette_buf; static WGPUBuffer g_vertex_buf; static unsigned g_vertex_buf_capacity; static WGPUBindGroup g_bind_group; @@ -831,13 +828,15 @@ static void create_pipeline () { std::string wgsl; - wgsl += hb_gpu_shader_source (HB_GPU_SHADER_STAGE_VERTEX, HB_GPU_SHADER_LANG_WGSL); + wgsl += hb_gpu_shader_source (HB_GPU_SHADER_STAGE_VERTEX, HB_GPU_SHADER_LANG_WGSL); wgsl += "\n"; wgsl += hb_gpu_draw_shader_source (HB_GPU_SHADER_STAGE_VERTEX, HB_GPU_SHADER_LANG_WGSL); wgsl += "\n"; - wgsl += hb_gpu_shader_source (HB_GPU_SHADER_STAGE_FRAGMENT, HB_GPU_SHADER_LANG_WGSL); + wgsl += hb_gpu_shader_source (HB_GPU_SHADER_STAGE_FRAGMENT, HB_GPU_SHADER_LANG_WGSL); wgsl += "\n"; - wgsl += hb_gpu_draw_shader_source (HB_GPU_SHADER_STAGE_FRAGMENT, HB_GPU_SHADER_LANG_WGSL); + wgsl += hb_gpu_draw_shader_source (HB_GPU_SHADER_STAGE_FRAGMENT, HB_GPU_SHADER_LANG_WGSL); + wgsl += "\n"; + wgsl += hb_gpu_paint_shader_source (HB_GPU_SHADER_STAGE_FRAGMENT, HB_GPU_SHADER_LANG_WGSL); wgsl += "\n"; wgsl += wgsl_demo_shader; @@ -909,17 +908,20 @@ create_pipeline () static void create_bind_group () { - WGPUBindGroupEntry entries[2] = {}; + WGPUBindGroupEntry entries[3] = {}; entries[0].binding = 0; entries[0].buffer = g_uniform_buf; entries[0].size = sizeof (Uniforms); entries[1].binding = 1; entries[1].buffer = atlas.buf; entries[1].size = atlas.capacity * 4 * sizeof (int32_t); + entries[2].binding = 2; + entries[2].buffer = g_palette_buf; + entries[2].size = 256 * 4 * sizeof (float); WGPUBindGroupDescriptor bgDesc = {}; bgDesc.layout = wgpuRenderPipelineGetBindGroupLayout (g_pipeline, 0); - bgDesc.entryCount = 2; + bgDesc.entryCount = 3; bgDesc.entries = entries; if (g_bind_group) @@ -963,6 +965,14 @@ init_demo () uniDesc.usage = WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst; g_uniform_buf = wgpuDeviceCreateBuffer (g_device, &uniDesc); + /* Palette storage buffer: 256 vec4f, zero-filled for now. */ + WGPUBufferDescriptor palDesc = {}; + palDesc.size = 256 * 4 * sizeof (float); + palDesc.usage = WGPUBufferUsage_Storage | WGPUBufferUsage_CopyDst; + g_palette_buf = wgpuDeviceCreateBuffer (g_device, &palDesc); + float zero_palette[256 * 4] = {}; + wgpuQueueWriteBuffer (g_queue, g_palette_buf, 0, zero_palette, sizeof (zero_palette)); + /* Create pipeline and bind group */ create_pipeline ();