Files
harfbuzz/perf/benchmark-gpu.cc
T
Behdad Esfahbod dfd5d5b3cb [gpu] Paint: port to HLSL/WGSL; add _hb_gpu_draw_impl; fix WGSL
This commit unblocks end-to-end paint rendering on the WebGPU
demo, adds the HLSL port, and avoids losing MSAA or stem
darkening along the way.

### Shared: _hb_gpu_draw_impl

hb_gpu_draw internally did fwidth(renderCoord) + MSAA.  That works
fine in the single-call-per-fragment case but not when called from
inside a non-uniform branch (e.g. an op-stream LAYER_SOLID
conditional in the paint fragment shader) -- WGSL rejects fwidth
from non-uniform control flow.

Factor the MSAA-aware body into _hb_gpu_draw_impl, which takes a
caller-supplied pixelsPerEm.  hb_gpu_draw is now a thin wrapper
that computes pixelsPerEm via fwidth and delegates.  The paint
fragment shader in every language now computes pixelsPerEm once
at the top of hb_gpu_paint and calls _hb_gpu_draw_impl per layer
-- MSAA preserved, no redundant fwidth calls per layer.

In WGSL, _hb_gpu_draw_impl also inlines the ppem computation from
pixelsPerEm rather than recurring into hb_gpu_ppem (which itself
calls fwidth).

### Paint shaders

- hb-gpu-paint-fragment.hlsl (new): HLSL transcription.  Assumes
  a caller-declared StructuredBuffer<float4> hb_gpu_palette at
  register(t1).
- hb-gpu-paint-fragment.wgsl: switched from hb_gpu_draw to
  _hb_gpu_draw_impl; comment updated.
- hb-gpu-paint-fragment.msl, .glsl: same switch, cached
  pixelsPerEm at entry.

hb_gpu_paint_shader_source() now returns content for all four
languages.

### Demos

- util/gpu/demo-fragment.glsl: restores stem darkening (dropped in
  the first paint wiring commit); applies to the premultiplied
  RGBA by scaling both colour and alpha.  The desktop GL demo
  regenerates demo-fragment-glsl.hh from this source.
- util/gpu/web/hb-gpu-web-webgpu.cc: WGSL fs_main concatenates the
  paint source, binds a zero-filled palette storage buffer at
  @group(0) @binding(2), and restores stem darkening.  fwidth
  hoisted outside the stem-darkening conditional so WGSL's
  derivative-uniformity rule is happy.
- util/gpu/demo-renderer-d3d11.cc: HLSL ps_main switches to
  hb_gpu_paint.  Adds a second StructuredBuffer<float4>
  hb_gpu_palette at t1, zero-filled for now, bound alongside the
  atlas SRV.

### Misc

- perf/benchmark-gpu.cc: fix stale reset/encode(draw) calls.

Measured build (local Emscripten):
  WebGL   index.wasm  : 443638  181640 gz
  WebGPU  webgpu.wasm : 461626  188400 gz

COLRv0 rendering (BungeeTint, Noto Color Emoji, etc.) now works
end-to-end on WebGL and WebGPU with the correct palette.  Metal
and D3D11 demos should likewise compile against the new shader
flow; real palette upload cross-backend remains a TODO.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-13 15:52:21 -06:00

131 lines
3.7 KiB
C++

/*
* 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.
*
* Author(s): Behdad Esfahbod
*/
#include "hb-benchmark.hh"
#include <hb-gpu.h>
#include <glib.h>
#define SUBSET_FONT_BASE_PATH "test/subset/data/fonts/"
static struct test_input_t
{
const char *font_path;
} default_tests[] =
{
{SUBSET_FONT_BASE_PATH "Roboto-Regular.ttf"},
{SUBSET_FONT_BASE_PATH "SourceSansPro-Regular.otf"},
{SUBSET_FONT_BASE_PATH "AdobeVFPrototype.otf"},
{SUBSET_FONT_BASE_PATH "Comfortaa-Regular-new.ttf"},
{SUBSET_FONT_BASE_PATH "NotoNastaliqUrdu-Regular.ttf"},
{SUBSET_FONT_BASE_PATH "NotoSerifMyanmar-Regular.otf"},
};
static test_input_t *tests = default_tests;
static unsigned num_tests = sizeof (default_tests) / sizeof (default_tests[0]);
static void BM_GpuEncode (benchmark::State &state,
const test_input_t &test_input)
{
hb_face_t *face = hb_benchmark_face_create_from_file_or_fail (test_input.font_path, 0);
if (!face)
{
state.SkipWithError ("Failed to open font file");
return;
}
hb_font_t *font = hb_font_create (face);
unsigned num_glyphs = hb_face_get_glyph_count (face);
hb_gpu_draw_t *draw = hb_gpu_draw_create_or_fail ();
assert (draw);
for (auto _ : state)
for (unsigned gid = 0; gid < num_glyphs; ++gid)
{
hb_gpu_draw_clear (draw);
hb_gpu_draw_glyph (draw, font, gid);
hb_blob_t *blob = hb_gpu_draw_encode (draw, nullptr);
hb_gpu_draw_recycle_blob (draw, blob);
}
hb_gpu_draw_destroy (draw);
hb_font_destroy (font);
hb_face_destroy (face);
}
static const char *font_file = nullptr;
static GOptionEntry entries[] =
{
{"font-file", 0, 0, G_OPTION_ARG_STRING, &font_file, "Font file-path to benchmark", "FONTFILE"},
{nullptr}
};
int main (int argc, char **argv)
{
benchmark::Initialize (&argc, argv);
GOptionContext *context = g_option_context_new ("");
g_option_context_set_summary (context, "Benchmark GPU glyph encoding");
g_option_context_add_main_entries (context, entries, nullptr);
g_option_context_parse (context, &argc, &argv, nullptr);
g_option_context_free (context);
argc--;
argv++;
if (!font_file && argc)
{
font_file = *argv;
argc--;
argv++;
}
if (font_file)
{
num_tests = 1;
tests = (test_input_t *) calloc (num_tests, sizeof (test_input_t));
tests[0].font_path = font_file;
}
for (unsigned i = 0; i < num_tests; i++)
{
auto &test_input = tests[i];
char name[1024] = "BM_GpuEncode/";
const char *p = strrchr (test_input.font_path, '/');
strcat (name, p ? p + 1 : test_input.font_path);
benchmark::RegisterBenchmark (name, BM_GpuEncode, test_input)
->Unit (benchmark::kMillisecond);
}
benchmark::RunSpecifiedBenchmarks ();
benchmark::Shutdown ();
if (tests != default_tests)
free (tests);
}