Files
harfbuzz/perf
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
..
2024-10-15 14:58:13 -06:00
2024-10-15 14:58:13 -06:00
2024-10-15 14:58:13 -06:00
2025-07-26 04:02:41 -06:00
2025-03-07 14:01:57 -07:00

Building and Running

Benchmarks are implemented using Google Benchmark.

To build the benchmarks in this directory you need to set the benchmark option while configuring the build with meson:

meson build -Dbenchmark=enabled --buildtype=debugoptimized

The default build type is debugoptimized, which is good enough for benchmarking, but you can also get the fastest mode with release build type:

meson build -Dbenchmark=enabled --buildtype=release

You should, of course, enable features you want to benchmark, like -Dfreetype, -Dfontations, -Dcoretext, etc.

Then build a specific benchmark binaries with ninja, eg.:

ninja -Cbuild perf/benchmark-set

or just build the whole project:

ninja -Cbuild

Finally, to run one of the benchmarks:

./build/perf/benchmark-set

It's possible to filter the benchmarks being run and customize the output via flags to the benchmark binary. See the Google Benchmark User Guide for more details.

The most useful benchmark is benchmark-font. You can provide custom fonts to it too. For example, to run only the "paint" benchmarks, against a given font, you can do:

./build/perf/benchmark-font NotoColorEmoji-Regular.ttf --benchmark_filter="paint"

Some useful options are: --benchmark_repetitions=5 to run the benchmark 5 times, --benchmark_min_time=.1s to run the benchmark for at least .1 seconds (defaults to .5s), and --benchmark_filter=... to filter the benchmarks by regular expression.

To compare before/after benchmarks, you need to save the benchmark results in files for both runs. Use --benchmark_out=results.json to output the results in JSON format. Then you can use:

./subprojects/benchmark-1.8.4/tools/compare.py benchmarks before.json after.json

Substitute your version of benchmark instead of 1.8.4.

Profiling

If you like to disable optimizations and enable frame pointers for better profiling output, you can do so with the following meson command:

CXXFLAGS="-fno-omit-frame-pointer" meson --reconfigure build -Dbenchmark=enabled --buildtype=debug
ninja -Cbuild

However, this will slow down the benchmarks significantly and might give you inaccurate information as to where to optimize. It's better to profile the debugoptimized build (the default).

Then run the benchmark with perf:

perf record -g build/perf/benchmark-subset --benchmark_filter="BM_subset_codepoints/subset_notocjk/100000" --benchmark_repetitions=5

You probably want to filter to a specific benchmark of interest and set the number of repititions high enough to get a good sampling of profile data.

Finally view the profile with:

perf report

Another useful perf tool is the perf stat command, which can give you a quick overview of the performance of a benchmark, as well as stalled cycles, cache misses, and mispredicted branches.