From dfd5d5b3cb5692c488fd4ab9f2a3e504146c1f44 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 13 Apr 2026 15:52:21 -0600 Subject: [PATCH] [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 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 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) --- perf/benchmark-gpu.cc | 4 +- src/hb-gpu-draw-fragment-glsl.hh | 16 ++++-- src/hb-gpu-draw-fragment-hlsl.hh | 15 ++++-- src/hb-gpu-draw-fragment-msl.hh | 19 +++++-- src/hb-gpu-draw-fragment-wgsl.hh | 26 +++++++--- src/hb-gpu-draw-fragment.glsl | 16 ++++-- src/hb-gpu-draw-fragment.hlsl | 15 ++++-- src/hb-gpu-draw-fragment.msl | 19 +++++-- src/hb-gpu-draw-fragment.wgsl | 26 +++++++--- src/hb-gpu-paint-fragment-glsl.hh | 8 ++- src/hb-gpu-paint-fragment-hlsl.hh | 86 +++++++++++++++++++++++++++++++ src/hb-gpu-paint-fragment-msl.hh | 7 ++- src/hb-gpu-paint-fragment-wgsl.hh | 11 +++- src/hb-gpu-paint-fragment.glsl | 8 ++- src/hb-gpu-paint-fragment.hlsl | 84 ++++++++++++++++++++++++++++++ src/hb-gpu-paint-fragment.msl | 7 ++- src/hb-gpu-paint-fragment.wgsl | 11 +++- src/hb-gpu-paint.cc | 3 +- src/meson.build | 1 + util/gpu/demo-fragment-glsl.hh | 10 ++++ util/gpu/demo-fragment.glsl | 10 ++++ util/gpu/demo-renderer-d3d11.cc | 53 +++++++++++++------ util/gpu/web/hb-gpu-web-webgpu.cc | 12 +++++ 23 files changed, 401 insertions(+), 66 deletions(-) create mode 100644 src/hb-gpu-paint-fragment-hlsl.hh create mode 100644 src/hb-gpu-paint-fragment.hlsl diff --git a/perf/benchmark-gpu.cc b/perf/benchmark-gpu.cc index 5b39c5c6e..546ba1bf2 100644 --- a/perf/benchmark-gpu.cc +++ b/perf/benchmark-gpu.cc @@ -66,9 +66,9 @@ static void BM_GpuEncode (benchmark::State &state, for (auto _ : state) for (unsigned gid = 0; gid < num_glyphs; ++gid) { - hb_gpu_draw_reset (draw); + hb_gpu_draw_clear (draw); hb_gpu_draw_glyph (draw, font, gid); - hb_blob_t *blob = hb_gpu_draw_encode (draw); + hb_blob_t *blob = hb_gpu_draw_encode (draw, nullptr); hb_gpu_draw_recycle_blob (draw, blob); } diff --git a/src/hb-gpu-draw-fragment-glsl.hh b/src/hb-gpu-draw-fragment-glsl.hh index 92d4d962d..5bcfad908 100644 --- a/src/hb-gpu-draw-fragment-glsl.hh +++ b/src/hb-gpu-draw-fragment-glsl.hh @@ -266,11 +266,12 @@ static const char *hb_gpu_draw_fragment_glsl = " * renderCoord: em-space sample position\n" " * glyphLoc: texel offset of glyph blob in atlas\n" " */\n" -"float hb_gpu_draw (vec2 renderCoord, uint glyphLoc_)\n" +"/* The MSAA-aware implementation. Caller supplies pixelsPerEm so\n" +" * this function can be invoked from non-uniform control flow (for\n" +" * example inside an op-stream branch in hb-gpu-paint-fragment.wgsl,\n" +" * where WGSL would otherwise reject an fwidth call). */\n" +"float _hb_gpu_draw_impl (vec2 renderCoord, vec2 pixelsPerEm, uint glyphLoc_)\n" "{\n" -" vec2 emsPerPixel = fwidth (renderCoord);\n" -" vec2 pixelsPerEm = 1.0 / emsPerPixel;\n" -"\n" " float c = _hb_gpu_draw_single (renderCoord, pixelsPerEm, glyphLoc_);\n" "\n" "#ifndef HB_GPU_NO_MSAA\n" @@ -278,6 +279,7 @@ static const char *hb_gpu_draw_fragment_glsl = "\n" " if (ppem < 16.0)\n" " {\n" +" vec2 emsPerPixel = 1.0 / pixelsPerEm;\n" " vec2 d = emsPerPixel * (1.0 / 3.0);\n" " float msaa = 0.25 *\n" " (_hb_gpu_draw_single (renderCoord + vec2 (-d.x, -d.y), pixelsPerEm, glyphLoc_) +\n" @@ -292,6 +294,12 @@ static const char *hb_gpu_draw_fragment_glsl = " return c;\n" "}\n" "\n" +"float hb_gpu_draw (vec2 renderCoord, uint glyphLoc_)\n" +"{\n" +" vec2 pixelsPerEm = 1.0 / fwidth (renderCoord);\n" +" return _hb_gpu_draw_impl (renderCoord, pixelsPerEm, glyphLoc_);\n" +"}\n" +"\n" "/* Stem darkening for small sizes.\n" " *\n" " * coverage: output of hb_gpu_draw\n" diff --git a/src/hb-gpu-draw-fragment-hlsl.hh b/src/hb-gpu-draw-fragment-hlsl.hh index fd3236d42..579ba7816 100644 --- a/src/hb-gpu-draw-fragment-hlsl.hh +++ b/src/hb-gpu-draw-fragment-hlsl.hh @@ -266,11 +266,11 @@ static const char *hb_gpu_draw_fragment_hlsl = " * renderCoord: em-space sample position\n" " * glyphLoc: texel offset of glyph blob in atlas\n" " */\n" -"float hb_gpu_draw (float2 renderCoord, uint glyphLoc_)\n" +"/* The MSAA-aware implementation. Caller supplies pixelsPerEm so\n" +" * this function can be invoked from non-uniform control flow (for\n" +" * example from a paint op-stream branch). */\n" +"float _hb_gpu_draw_impl (float2 renderCoord, float2 pixelsPerEm, uint glyphLoc_)\n" "{\n" -" float2 emsPerPixel = fwidth (renderCoord);\n" -" float2 pixelsPerEm = 1.0 / emsPerPixel;\n" -"\n" " float c = _hb_gpu_draw_single (renderCoord, pixelsPerEm, glyphLoc_);\n" "\n" "#ifndef HB_GPU_NO_MSAA\n" @@ -278,6 +278,7 @@ static const char *hb_gpu_draw_fragment_hlsl = "\n" " if (ppem < 16.0)\n" " {\n" +" float2 emsPerPixel = 1.0 / pixelsPerEm;\n" " float2 d = emsPerPixel * (1.0 / 3.0);\n" " float msaa = 0.25 *\n" " (_hb_gpu_draw_single (renderCoord + float2 (-d.x, -d.y), pixelsPerEm, glyphLoc_) +\n" @@ -292,6 +293,12 @@ static const char *hb_gpu_draw_fragment_hlsl = " return c;\n" "}\n" "\n" +"float hb_gpu_draw (float2 renderCoord, uint glyphLoc_)\n" +"{\n" +" float2 pixelsPerEm = 1.0 / fwidth (renderCoord);\n" +" return _hb_gpu_draw_impl (renderCoord, pixelsPerEm, glyphLoc_);\n" +"}\n" +"\n" "/* Stem darkening for small sizes.\n" " *\n" " * coverage: output of hb_gpu_draw\n" diff --git a/src/hb-gpu-draw-fragment-msl.hh b/src/hb-gpu-draw-fragment-msl.hh index a780a12cd..450153ca8 100644 --- a/src/hb-gpu-draw-fragment-msl.hh +++ b/src/hb-gpu-draw-fragment-msl.hh @@ -272,12 +272,13 @@ static const char *hb_gpu_draw_fragment_msl = " * glyphLoc: texel offset of glyph blob in atlas\n" " * hb_gpu_atlas: device pointer to the atlas buffer\n" " */\n" -"float hb_gpu_draw (float2 renderCoord, uint glyphLoc_,\n" -" device const short4* hb_gpu_atlas)\n" +"/* The MSAA-aware implementation. Caller supplies pixelsPerEm so\n" +" * this function can be invoked from non-uniform control flow (for\n" +" * example from a paint op-stream branch where a recomputed fwidth\n" +" * would be rejected by strict derivative-uniformity rules). */\n" +"float _hb_gpu_draw_impl (float2 renderCoord, float2 pixelsPerEm, uint glyphLoc_,\n" +" device const short4* hb_gpu_atlas)\n" "{\n" -" float2 emsPerPixel = fwidth (renderCoord);\n" -" float2 pixelsPerEm = 1.0 / emsPerPixel;\n" -"\n" " float c = _hb_gpu_draw_single (renderCoord, pixelsPerEm, glyphLoc_, hb_gpu_atlas);\n" "\n" "#ifndef HB_GPU_NO_MSAA\n" @@ -285,6 +286,7 @@ static const char *hb_gpu_draw_fragment_msl = "\n" " if (ppem < 16.0)\n" " {\n" +" float2 emsPerPixel = 1.0 / pixelsPerEm;\n" " float2 d = emsPerPixel * (1.0 / 3.0);\n" " float msaa = 0.25 *\n" " (_hb_gpu_draw_single (renderCoord + float2 (-d.x, -d.y), pixelsPerEm, glyphLoc_, hb_gpu_atlas) +\n" @@ -299,6 +301,13 @@ static const char *hb_gpu_draw_fragment_msl = " return c;\n" "}\n" "\n" +"float hb_gpu_draw (float2 renderCoord, uint glyphLoc_,\n" +" device const short4* hb_gpu_atlas)\n" +"{\n" +" float2 pixelsPerEm = 1.0 / fwidth (renderCoord);\n" +" return _hb_gpu_draw_impl (renderCoord, pixelsPerEm, glyphLoc_, hb_gpu_atlas);\n" +"}\n" +"\n" "/* Stem darkening for small sizes.\n" " *\n" " * coverage: output of hb_gpu_draw\n" diff --git a/src/hb-gpu-draw-fragment-wgsl.hh b/src/hb-gpu-draw-fragment-wgsl.hh index d91a47947..685da2e76 100644 --- a/src/hb-gpu-draw-fragment-wgsl.hh +++ b/src/hb-gpu-draw-fragment-wgsl.hh @@ -281,18 +281,25 @@ static const char *hb_gpu_draw_fragment_wgsl = " * glyphLoc: texel offset of glyph blob in atlas\n" " * hb_gpu_atlas: storage buffer pointer to the atlas\n" " */\n" -"fn hb_gpu_draw (renderCoord: vec2f, glyphLoc_: u32,\n" -" hb_gpu_atlas: ptr>, read>) -> f32\n" +"/* The MSAA-aware implementation. Caller supplies pixelsPerEm so\n" +" * this function can be invoked from non-uniform control flow (for\n" +" * example inside an op-stream branch in hb-gpu-paint-fragment.wgsl,\n" +" * where WGSL would otherwise reject an fwidth call). */\n" +"fn _hb_gpu_draw_impl (renderCoord: vec2f, pixelsPerEm: vec2f, glyphLoc_: u32,\n" +" hb_gpu_atlas: ptr>, read>) -> f32\n" "{\n" -" let emsPerPixel = fwidth (renderCoord);\n" -" let pixelsPerEm = 1.0 / emsPerPixel;\n" -"\n" " var c = _hb_gpu_draw_single (renderCoord, pixelsPerEm, glyphLoc_, hb_gpu_atlas);\n" "\n" -" let ppem = hb_gpu_ppem (renderCoord, glyphLoc_, hb_gpu_atlas);\n" +" /* Inline ppem from pixelsPerEm so we don't re-enter hb_gpu_ppem\n" +" * (which would call fwidth again -- rejected by WGSL when this\n" +" * function is invoked from non-uniform control flow). */\n" +" let gi_pp = _hb_gpu_decode_glyph (renderCoord, glyphLoc_, hb_gpu_atlas);\n" +" let ppem = min (gi_pp.scale.x, gi_pp.scale.y) *\n" +" min (pixelsPerEm.x, pixelsPerEm.y);\n" "\n" " if (ppem < 16.0)\n" " {\n" +" let emsPerPixel = 1.0 / pixelsPerEm;\n" " let d = emsPerPixel * (1.0 / 3.0);\n" " let msaa = 0.25 *\n" " (_hb_gpu_draw_single (renderCoord + vec2f (-d.x, -d.y), pixelsPerEm, glyphLoc_, hb_gpu_atlas) +\n" @@ -306,6 +313,13 @@ static const char *hb_gpu_draw_fragment_wgsl = " return c;\n" "}\n" "\n" +"fn hb_gpu_draw (renderCoord: vec2f, glyphLoc_: u32,\n" +" hb_gpu_atlas: ptr>, read>) -> f32\n" +"{\n" +" let pixelsPerEm = 1.0 / fwidth (renderCoord);\n" +" return _hb_gpu_draw_impl (renderCoord, pixelsPerEm, glyphLoc_, hb_gpu_atlas);\n" +"}\n" +"\n" "/* Stem darkening for small sizes.\n" " *\n" " * coverage: output of hb_gpu_draw\n" diff --git a/src/hb-gpu-draw-fragment.glsl b/src/hb-gpu-draw-fragment.glsl index c72400295..dc567dd46 100644 --- a/src/hb-gpu-draw-fragment.glsl +++ b/src/hb-gpu-draw-fragment.glsl @@ -265,11 +265,12 @@ float _hb_gpu_draw_single (vec2 renderCoord, vec2 pixelsPerEm, uint glyphLoc_) * renderCoord: em-space sample position * glyphLoc: texel offset of glyph blob in atlas */ -float hb_gpu_draw (vec2 renderCoord, uint glyphLoc_) +/* The MSAA-aware implementation. Caller supplies pixelsPerEm so + * this function can be invoked from non-uniform control flow (for + * example inside an op-stream branch in hb-gpu-paint-fragment.wgsl, + * where WGSL would otherwise reject an fwidth call). */ +float _hb_gpu_draw_impl (vec2 renderCoord, vec2 pixelsPerEm, uint glyphLoc_) { - vec2 emsPerPixel = fwidth (renderCoord); - vec2 pixelsPerEm = 1.0 / emsPerPixel; - float c = _hb_gpu_draw_single (renderCoord, pixelsPerEm, glyphLoc_); #ifndef HB_GPU_NO_MSAA @@ -277,6 +278,7 @@ float hb_gpu_draw (vec2 renderCoord, uint glyphLoc_) if (ppem < 16.0) { + vec2 emsPerPixel = 1.0 / pixelsPerEm; vec2 d = emsPerPixel * (1.0 / 3.0); float msaa = 0.25 * (_hb_gpu_draw_single (renderCoord + vec2 (-d.x, -d.y), pixelsPerEm, glyphLoc_) + @@ -291,6 +293,12 @@ float hb_gpu_draw (vec2 renderCoord, uint glyphLoc_) return c; } +float hb_gpu_draw (vec2 renderCoord, uint glyphLoc_) +{ + vec2 pixelsPerEm = 1.0 / fwidth (renderCoord); + return _hb_gpu_draw_impl (renderCoord, pixelsPerEm, glyphLoc_); +} + /* Stem darkening for small sizes. * * coverage: output of hb_gpu_draw diff --git a/src/hb-gpu-draw-fragment.hlsl b/src/hb-gpu-draw-fragment.hlsl index e02f0634d..27563d580 100644 --- a/src/hb-gpu-draw-fragment.hlsl +++ b/src/hb-gpu-draw-fragment.hlsl @@ -265,11 +265,11 @@ float _hb_gpu_draw_single (float2 renderCoord, float2 pixelsPerEm, uint glyphLoc * renderCoord: em-space sample position * glyphLoc: texel offset of glyph blob in atlas */ -float hb_gpu_draw (float2 renderCoord, uint glyphLoc_) +/* The MSAA-aware implementation. Caller supplies pixelsPerEm so + * this function can be invoked from non-uniform control flow (for + * example from a paint op-stream branch). */ +float _hb_gpu_draw_impl (float2 renderCoord, float2 pixelsPerEm, uint glyphLoc_) { - float2 emsPerPixel = fwidth (renderCoord); - float2 pixelsPerEm = 1.0 / emsPerPixel; - float c = _hb_gpu_draw_single (renderCoord, pixelsPerEm, glyphLoc_); #ifndef HB_GPU_NO_MSAA @@ -277,6 +277,7 @@ float hb_gpu_draw (float2 renderCoord, uint glyphLoc_) if (ppem < 16.0) { + float2 emsPerPixel = 1.0 / pixelsPerEm; float2 d = emsPerPixel * (1.0 / 3.0); float msaa = 0.25 * (_hb_gpu_draw_single (renderCoord + float2 (-d.x, -d.y), pixelsPerEm, glyphLoc_) + @@ -291,6 +292,12 @@ float hb_gpu_draw (float2 renderCoord, uint glyphLoc_) return c; } +float hb_gpu_draw (float2 renderCoord, uint glyphLoc_) +{ + float2 pixelsPerEm = 1.0 / fwidth (renderCoord); + return _hb_gpu_draw_impl (renderCoord, pixelsPerEm, glyphLoc_); +} + /* Stem darkening for small sizes. * * coverage: output of hb_gpu_draw diff --git a/src/hb-gpu-draw-fragment.msl b/src/hb-gpu-draw-fragment.msl index 642dcff80..701ccffa9 100644 --- a/src/hb-gpu-draw-fragment.msl +++ b/src/hb-gpu-draw-fragment.msl @@ -271,12 +271,13 @@ float _hb_gpu_draw_single (float2 renderCoord, float2 pixelsPerEm, uint glyphLoc * glyphLoc: texel offset of glyph blob in atlas * hb_gpu_atlas: device pointer to the atlas buffer */ -float hb_gpu_draw (float2 renderCoord, uint glyphLoc_, - device const short4* hb_gpu_atlas) +/* The MSAA-aware implementation. Caller supplies pixelsPerEm so + * this function can be invoked from non-uniform control flow (for + * example from a paint op-stream branch where a recomputed fwidth + * would be rejected by strict derivative-uniformity rules). */ +float _hb_gpu_draw_impl (float2 renderCoord, float2 pixelsPerEm, uint glyphLoc_, + device const short4* hb_gpu_atlas) { - float2 emsPerPixel = fwidth (renderCoord); - float2 pixelsPerEm = 1.0 / emsPerPixel; - float c = _hb_gpu_draw_single (renderCoord, pixelsPerEm, glyphLoc_, hb_gpu_atlas); #ifndef HB_GPU_NO_MSAA @@ -284,6 +285,7 @@ float hb_gpu_draw (float2 renderCoord, uint glyphLoc_, if (ppem < 16.0) { + float2 emsPerPixel = 1.0 / pixelsPerEm; float2 d = emsPerPixel * (1.0 / 3.0); float msaa = 0.25 * (_hb_gpu_draw_single (renderCoord + float2 (-d.x, -d.y), pixelsPerEm, glyphLoc_, hb_gpu_atlas) + @@ -298,6 +300,13 @@ float hb_gpu_draw (float2 renderCoord, uint glyphLoc_, return c; } +float hb_gpu_draw (float2 renderCoord, uint glyphLoc_, + device const short4* hb_gpu_atlas) +{ + float2 pixelsPerEm = 1.0 / fwidth (renderCoord); + return _hb_gpu_draw_impl (renderCoord, pixelsPerEm, glyphLoc_, hb_gpu_atlas); +} + /* Stem darkening for small sizes. * * coverage: output of hb_gpu_draw diff --git a/src/hb-gpu-draw-fragment.wgsl b/src/hb-gpu-draw-fragment.wgsl index 852019d4a..c62567864 100644 --- a/src/hb-gpu-draw-fragment.wgsl +++ b/src/hb-gpu-draw-fragment.wgsl @@ -280,18 +280,25 @@ fn _hb_gpu_draw_single (renderCoord: vec2f, pixelsPerEm: vec2f, glyphLoc_: u32, * glyphLoc: texel offset of glyph blob in atlas * hb_gpu_atlas: storage buffer pointer to the atlas */ -fn hb_gpu_draw (renderCoord: vec2f, glyphLoc_: u32, - hb_gpu_atlas: ptr>, read>) -> f32 +/* The MSAA-aware implementation. Caller supplies pixelsPerEm so + * this function can be invoked from non-uniform control flow (for + * example inside an op-stream branch in hb-gpu-paint-fragment.wgsl, + * where WGSL would otherwise reject an fwidth call). */ +fn _hb_gpu_draw_impl (renderCoord: vec2f, pixelsPerEm: vec2f, glyphLoc_: u32, + hb_gpu_atlas: ptr>, read>) -> f32 { - let emsPerPixel = fwidth (renderCoord); - let pixelsPerEm = 1.0 / emsPerPixel; - var c = _hb_gpu_draw_single (renderCoord, pixelsPerEm, glyphLoc_, hb_gpu_atlas); - let ppem = hb_gpu_ppem (renderCoord, glyphLoc_, hb_gpu_atlas); + /* Inline ppem from pixelsPerEm so we don't re-enter hb_gpu_ppem + * (which would call fwidth again -- rejected by WGSL when this + * function is invoked from non-uniform control flow). */ + let gi_pp = _hb_gpu_decode_glyph (renderCoord, glyphLoc_, hb_gpu_atlas); + let ppem = min (gi_pp.scale.x, gi_pp.scale.y) * + min (pixelsPerEm.x, pixelsPerEm.y); if (ppem < 16.0) { + let emsPerPixel = 1.0 / pixelsPerEm; let d = emsPerPixel * (1.0 / 3.0); let msaa = 0.25 * (_hb_gpu_draw_single (renderCoord + vec2f (-d.x, -d.y), pixelsPerEm, glyphLoc_, hb_gpu_atlas) + @@ -305,6 +312,13 @@ fn hb_gpu_draw (renderCoord: vec2f, glyphLoc_: u32, return c; } +fn hb_gpu_draw (renderCoord: vec2f, glyphLoc_: u32, + hb_gpu_atlas: ptr>, read>) -> f32 +{ + let pixelsPerEm = 1.0 / fwidth (renderCoord); + return _hb_gpu_draw_impl (renderCoord, pixelsPerEm, glyphLoc_, hb_gpu_atlas); +} + /* Stem darkening for small sizes. * * coverage: output of hb_gpu_draw diff --git a/src/hb-gpu-paint-fragment-glsl.hh b/src/hb-gpu-paint-fragment-glsl.hh index d58f47d35..ef66d52ff 100644 --- a/src/hb-gpu-paint-fragment-glsl.hh +++ b/src/hb-gpu-paint-fragment-glsl.hh @@ -50,6 +50,11 @@ static const char *hb_gpu_paint_fragment_glsl = " */\n" "vec4 hb_gpu_paint (vec2 renderCoord, uint glyphLoc, vec4 foreground)\n" "{\n" +" /* fwidth once, at uniform control flow: every per-layer\n" +" * coverage sample below uses this pre-computed pixelsPerEm via\n" +" * _hb_gpu_draw_impl. */\n" +" vec2 pixelsPerEm = 1.0 / fwidth (renderCoord);\n" +"\n" " int base = int (glyphLoc);\n" " ivec4 h0 = hb_gpu_fetch (base); /* (num_ops, _, _, _) */\n" " ivec4 h2 = hb_gpu_fetch (base + 2); /* (ops_offset, _, _, _) */\n" @@ -78,7 +83,8 @@ static const char *hb_gpu_paint_fragment_glsl = "\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 (renderCoord, uint (base + payload));\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" " acc = src + acc * (1.0 - src.a);\n" diff --git a/src/hb-gpu-paint-fragment-hlsl.hh b/src/hb-gpu-paint-fragment-hlsl.hh new file mode 100644 index 000000000..4040347d1 --- /dev/null +++ b/src/hb-gpu-paint-fragment-hlsl.hh @@ -0,0 +1,86 @@ +static const char *hb_gpu_paint_fragment_hlsl = +"/*\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 (HLSL).\n" +" *\n" +" * Assumes the shared fragment helpers (hb-gpu-fragment.hlsl) and\n" +" * the draw-renderer fragment helpers (hb-gpu-draw-fragment.hlsl)\n" +" * are prepended to this source. The caller must also declare the\n" +" * palette buffer before this source, e.g.:\n" +" *\n" +" * StructuredBuffer hb_gpu_palette : register(t1);\n" +" */\n" +"\n" +"\n" +"float4 hb_gpu_paint (float2 renderCoord, uint glyphLoc_, float4 foreground)\n" +"{\n" +" /* fwidth once, at uniform control flow. */\n" +" float2 pixelsPerEm = 1.0 / fwidth (renderCoord);\n" +"\n" +" int base = (int) glyphLoc_;\n" +" int4 h0 = hb_gpu_fetch (base); /* (num_ops, _, _, _) */\n" +" int4 h2 = hb_gpu_fetch (base + 2); /* (ops_offset, _, _, _) */\n" +" int num_ops = h0.r;\n" +" int cursor = base + h2.r;\n" +"\n" +" float4 acc = float4 (0.0, 0.0, 0.0, 0.0);\n" +"\n" +" for (int i = 0; i < num_ops; i++)\n" +" {\n" +" int4 op = hb_gpu_fetch (cursor);\n" +" int op_type = op.r;\n" +" int payload = (op.b << 16) | (op.a & 0xffff);\n" +"\n" +" if (op_type == 0) /* LAYER_SOLID */\n" +" {\n" +" int4 ct = hb_gpu_fetch (cursor + 1);\n" +" int palette_index = ct.r;\n" +" int flags = ct.g;\n" +" float alpha = (float) ct.b / 32767.0;\n" +"\n" +" float4 col = ((flags & 1) != 0)\n" +" ? foreground\n" +" : hb_gpu_palette[palette_index];\n" +" col.a *= alpha;\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" +" 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" +" break;\n" +" }\n" +" }\n" +"\n" +" return acc;\n" +"}\n" +; diff --git a/src/hb-gpu-paint-fragment-msl.hh b/src/hb-gpu-paint-fragment-msl.hh index 0c2a06ac9..c7e0fa77c 100644 --- a/src/hb-gpu-paint-fragment-msl.hh +++ b/src/hb-gpu-paint-fragment-msl.hh @@ -39,6 +39,9 @@ static const char *hb_gpu_paint_fragment_msl = " device const short4* hb_gpu_atlas,\n" " constant float4* hb_gpu_palette)\n" "{\n" +" /* fwidth once, at uniform control flow. */\n" +" float2 pixelsPerEm = 1.0 / fwidth (renderCoord);\n" +"\n" " int base = int (glyphLoc_);\n" " int4 h0 = int4 (hb_gpu_atlas[base]); /* (num_ops, _, _, _) */\n" " int4 h2 = int4 (hb_gpu_atlas[base + 2]); /* (ops_offset, _, _, _) */\n" @@ -65,8 +68,8 @@ static const char *hb_gpu_paint_fragment_msl = " : hb_gpu_palette[palette_index];\n" " col.a *= alpha;\n" "\n" -" float cov = hb_gpu_draw (renderCoord, uint (base + payload),\n" -" hb_gpu_atlas);\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" " acc = src + acc * (1.0 - src.a);\n" diff --git a/src/hb-gpu-paint-fragment-wgsl.hh b/src/hb-gpu-paint-fragment-wgsl.hh index a33e98d6b..996c9815e 100644 --- a/src/hb-gpu-paint-fragment-wgsl.hh +++ b/src/hb-gpu-paint-fragment-wgsl.hh @@ -39,6 +39,14 @@ static const char *hb_gpu_paint_fragment_wgsl = " hb_gpu_atlas: ptr>, read>,\n" " hb_gpu_palette: ptr, read>) -> vec4f\n" "{\n" +" /* Compute pixelsPerEm once here at uniform control flow. WGSL\n" +" * rejects fwidth inside a loop-conditional branch, so we call\n" +" * _hb_gpu_draw_impl (the MSAA-aware implementation that takes a\n" +" * pre-computed pixelsPerEm) instead of the top-level\n" +" * hb_gpu_draw() which would re-call fwidth. */\n" +" let emsPerPixel = fwidth (renderCoord);\n" +" let pixelsPerEm = 1.0 / emsPerPixel;\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" @@ -67,7 +75,8 @@ static const char *hb_gpu_paint_fragment_wgsl = " }\n" " col.a = col.a * alpha;\n" "\n" -" let cov = hb_gpu_draw (renderCoord, u32 (base + payload), hb_gpu_atlas);\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" " acc = src + acc * (1.0 - src.a);\n" diff --git a/src/hb-gpu-paint-fragment.glsl b/src/hb-gpu-paint-fragment.glsl index 9667ce61d..4e945ad0d 100644 --- a/src/hb-gpu-paint-fragment.glsl +++ b/src/hb-gpu-paint-fragment.glsl @@ -49,6 +49,11 @@ uniform vec4 hb_gpu_palette[HB_GPU_PALETTE_SIZE]; */ vec4 hb_gpu_paint (vec2 renderCoord, uint glyphLoc, vec4 foreground) { + /* fwidth once, at uniform control flow: every per-layer + * coverage sample below uses this pre-computed pixelsPerEm via + * _hb_gpu_draw_impl. */ + vec2 pixelsPerEm = 1.0 / fwidth (renderCoord); + int base = int (glyphLoc); ivec4 h0 = hb_gpu_fetch (base); /* (num_ops, _, _, _) */ ivec4 h2 = hb_gpu_fetch (base + 2); /* (ops_offset, _, _, _) */ @@ -77,7 +82,8 @@ vec4 hb_gpu_paint (vec2 renderCoord, uint glyphLoc, vec4 foreground) /* payload is a blob-relative texel offset; sub-blobs live * inside the paint blob at atlas offset `base`. */ - float cov = hb_gpu_draw (renderCoord, uint (base + payload)); + float cov = _hb_gpu_draw_impl (renderCoord, pixelsPerEm, + uint (base + payload)); vec4 src = col * cov; /* SRC_OVER with premultiplied source. */ acc = src + acc * (1.0 - src.a); diff --git a/src/hb-gpu-paint-fragment.hlsl b/src/hb-gpu-paint-fragment.hlsl new file mode 100644 index 000000000..5222b4d73 --- /dev/null +++ b/src/hb-gpu-paint-fragment.hlsl @@ -0,0 +1,84 @@ +/* + * 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 (HLSL). + * + * Assumes the shared fragment helpers (hb-gpu-fragment.hlsl) and + * the draw-renderer fragment helpers (hb-gpu-draw-fragment.hlsl) + * are prepended to this source. The caller must also declare the + * palette buffer before this source, e.g.: + * + * StructuredBuffer hb_gpu_palette : register(t1); + */ + + +float4 hb_gpu_paint (float2 renderCoord, uint glyphLoc_, float4 foreground) +{ + /* fwidth once, at uniform control flow. */ + float2 pixelsPerEm = 1.0 / fwidth (renderCoord); + + int base = (int) glyphLoc_; + int4 h0 = hb_gpu_fetch (base); /* (num_ops, _, _, _) */ + int4 h2 = hb_gpu_fetch (base + 2); /* (ops_offset, _, _, _) */ + int num_ops = h0.r; + int cursor = base + h2.r; + + float4 acc = float4 (0.0, 0.0, 0.0, 0.0); + + for (int i = 0; i < num_ops; i++) + { + int4 op = hb_gpu_fetch (cursor); + int op_type = op.r; + int payload = (op.b << 16) | (op.a & 0xffff); + + if (op_type == 0) /* LAYER_SOLID */ + { + int4 ct = hb_gpu_fetch (cursor + 1); + int palette_index = ct.r; + int flags = ct.g; + float alpha = (float) ct.b / 32767.0; + + float4 col = ((flags & 1) != 0) + ? foreground + : hb_gpu_palette[palette_index]; + col.a *= alpha; + + float cov = _hb_gpu_draw_impl (renderCoord, pixelsPerEm, + (uint) (base + payload)); + float4 src = col * cov; + /* SRC_OVER with premultiplied source. */ + acc = src + acc * (1.0 - src.a); + + cursor += 2; + } + else + { + /* LAYER_GRADIENT, PUSH_GROUP, POP_GROUP not yet handled. */ + break; + } + } + + return acc; +} diff --git a/src/hb-gpu-paint-fragment.msl b/src/hb-gpu-paint-fragment.msl index 3f4caf7d4..345ce32bb 100644 --- a/src/hb-gpu-paint-fragment.msl +++ b/src/hb-gpu-paint-fragment.msl @@ -38,6 +38,9 @@ float4 hb_gpu_paint (float2 renderCoord, uint glyphLoc_, float4 foreground, device const short4* hb_gpu_atlas, constant float4* hb_gpu_palette) { + /* fwidth once, at uniform control flow. */ + float2 pixelsPerEm = 1.0 / fwidth (renderCoord); + int base = int (glyphLoc_); int4 h0 = int4 (hb_gpu_atlas[base]); /* (num_ops, _, _, _) */ int4 h2 = int4 (hb_gpu_atlas[base + 2]); /* (ops_offset, _, _, _) */ @@ -64,8 +67,8 @@ float4 hb_gpu_paint (float2 renderCoord, uint glyphLoc_, float4 foreground, : hb_gpu_palette[palette_index]; col.a *= alpha; - float cov = hb_gpu_draw (renderCoord, uint (base + payload), - hb_gpu_atlas); + float cov = _hb_gpu_draw_impl (renderCoord, pixelsPerEm, + uint (base + payload), hb_gpu_atlas); float4 src = col * cov; /* SRC_OVER with premultiplied source. */ acc = src + acc * (1.0 - src.a); diff --git a/src/hb-gpu-paint-fragment.wgsl b/src/hb-gpu-paint-fragment.wgsl index aacb312e7..1c4a3f92e 100644 --- a/src/hb-gpu-paint-fragment.wgsl +++ b/src/hb-gpu-paint-fragment.wgsl @@ -38,6 +38,14 @@ fn hb_gpu_paint (renderCoord: vec2f, glyphLoc_: u32, foreground: vec4f, hb_gpu_atlas: ptr>, read>, hb_gpu_palette: ptr, read>) -> vec4f { + /* Compute pixelsPerEm once here at uniform control flow. WGSL + * rejects fwidth inside a loop-conditional branch, so we call + * _hb_gpu_draw_impl (the MSAA-aware implementation that takes a + * pre-computed pixelsPerEm) instead of the top-level + * hb_gpu_draw() which would re-call fwidth. */ + let emsPerPixel = fwidth (renderCoord); + let pixelsPerEm = 1.0 / emsPerPixel; + 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, _, _, _) @@ -66,7 +74,8 @@ fn hb_gpu_paint (renderCoord: vec2f, glyphLoc_: u32, foreground: vec4f, } col.a = col.a * alpha; - let cov = hb_gpu_draw (renderCoord, u32 (base + payload), hb_gpu_atlas); + let cov = _hb_gpu_draw_impl (renderCoord, pixelsPerEm, + u32 (base + payload), hb_gpu_atlas); let src = col * cov; /* SRC_OVER with premultiplied source. */ acc = src + acc * (1.0 - src.a); diff --git a/src/hb-gpu-paint.cc b/src/hb-gpu-paint.cc index 4f6c6a760..5f1173b45 100644 --- a/src/hb-gpu-paint.cc +++ b/src/hb-gpu-paint.cc @@ -564,6 +564,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" +#include "hb-gpu-paint-fragment-hlsl.hh" const char * hb_gpu_paint_shader_source (hb_gpu_shader_stage_t stage, @@ -575,7 +576,7 @@ hb_gpu_paint_shader_source (hb_gpu_shader_stage_t stage, 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: return hb_gpu_paint_fragment_wgsl; - case HB_GPU_SHADER_LANG_HLSL: return ""; + case HB_GPU_SHADER_LANG_HLSL: return hb_gpu_paint_fragment_hlsl; default: return nullptr; } case HB_GPU_SHADER_STAGE_VERTEX: diff --git a/src/meson.build b/src/meson.build index 2c8a2616e..3f7a01c31 100644 --- a/src/meson.build +++ b/src/meson.build @@ -840,6 +840,7 @@ hb_gpu_shader_sources = [ [ '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-paint-fragment.hlsl', 'hb-gpu-paint-fragment-hlsl.hh' ], ] hb_gpu_shader_helper = find_program('gen-gpu-shader-artifacts.py') diff --git a/util/gpu/demo-fragment-glsl.hh b/util/gpu/demo-fragment-glsl.hh index 39c2dcfcf..0cb4a8064 100644 --- a/util/gpu/demo-fragment-glsl.hh +++ b/util/gpu/demo-fragment-glsl.hh @@ -22,6 +22,16 @@ static const char *demo_fragment_glsl = " * path. For COLRv0 glyphs it returns the composited color. */\n" " vec4 c = hb_gpu_paint (v_texcoord, v_glyphLoc, u_foreground);\n" "\n" +" /* Stem darkening: scale the premultiplied value by the\n" +" * alpha-channel darkening ratio so RGB and A stay in lockstep. */\n" +" if (u_stem_darkening > 0.0 && c.a > 0.0)\n" +" {\n" +" float darkened = hb_gpu_stem_darken (c.a,\n" +" dot (u_foreground.rgb, vec3 (1.0 / 3.0)),\n" +" 1.0 / max (fwidth (v_texcoord).x, fwidth (v_texcoord).y));\n" +" c *= darkened / c.a;\n" +" }\n" +"\n" " if (u_gamma != 1.0)\n" " c.a = pow (c.a, u_gamma);\n" "\n" diff --git a/util/gpu/demo-fragment.glsl b/util/gpu/demo-fragment.glsl index bcd52b01f..0f10915d5 100644 --- a/util/gpu/demo-fragment.glsl +++ b/util/gpu/demo-fragment.glsl @@ -21,6 +21,16 @@ void main () * path. For COLRv0 glyphs it returns the composited color. */ vec4 c = hb_gpu_paint (v_texcoord, v_glyphLoc, u_foreground); + /* Stem darkening: scale the premultiplied value by the + * alpha-channel darkening ratio so RGB and A stay in lockstep. */ + if (u_stem_darkening > 0.0 && c.a > 0.0) + { + float darkened = hb_gpu_stem_darken (c.a, + dot (u_foreground.rgb, vec3 (1.0 / 3.0)), + 1.0 / max (fwidth (v_texcoord).x, fwidth (v_texcoord).y)); + c *= darkened / c.a; + } + if (u_gamma != 1.0) c.a = pow (c.a, u_gamma); diff --git a/util/gpu/demo-renderer-d3d11.cc b/util/gpu/demo-renderer-d3d11.cc index 38956bb5c..f56ca0518 100644 --- a/util/gpu/demo-renderer-d3d11.cc +++ b/util/gpu/demo-renderer-d3d11.cc @@ -45,6 +45,10 @@ struct demo_renderer_d3d11_t : demo_renderer_t bool dirty; } atlas; + /* Palette (zero-filled for now; real upload is a TODO). */ + ID3D11Buffer *palette_buf; + ID3D11ShaderResourceView *palette_srv; + /* State */ float bg_r, bg_g, bg_b, bg_a; float fg_r, fg_g, fg_b, fg_a; @@ -123,30 +127,25 @@ PSInput vs_main (VSInput input) { } float4 ps_main (PSInput input) : SV_Target { - float coverage = hb_gpu_draw (input.texcoord, input.glyphLoc); - if (stem_darkening > 0.0) { - float2 fw = fwidth (input.texcoord); - float ppem = 1.0 / max (fw.x, fw.y); - float sf = smoothstep (8.0, 48.0, ppem); - bool light = dot (foreground.rgb, float3 (1,1,1)) > 1.5; - float se = light ? lerp (1.4, 1.0, sf) : lerp (0.7, 1.0, sf); - coverage = pow (coverage, se); - } + float4 c = hb_gpu_paint (input.texcoord, input.glyphLoc, foreground); if (gamma != 1.0) - coverage = pow (coverage, gamma); - return float4 (foreground.rgb, foreground.a * coverage); + c.a = pow (c.a, gamma); + return c; } )hlsl"; std::string full; - full += "StructuredBuffer hb_gpu_atlas : register(t0);\n"; - full += hb_gpu_shader_source (HB_GPU_SHADER_STAGE_VERTEX, HB_GPU_SHADER_LANG_HLSL); + full += "StructuredBuffer hb_gpu_atlas : register(t0);\n"; + full += "StructuredBuffer hb_gpu_palette : register(t1);\n"; + full += hb_gpu_shader_source (HB_GPU_SHADER_STAGE_VERTEX, HB_GPU_SHADER_LANG_HLSL); full += "\n"; - full += hb_gpu_draw_shader_source (HB_GPU_SHADER_STAGE_VERTEX, HB_GPU_SHADER_LANG_HLSL); + full += hb_gpu_draw_shader_source (HB_GPU_SHADER_STAGE_VERTEX, HB_GPU_SHADER_LANG_HLSL); full += "\n"; - full += hb_gpu_shader_source (HB_GPU_SHADER_STAGE_FRAGMENT, HB_GPU_SHADER_LANG_HLSL); + full += hb_gpu_shader_source (HB_GPU_SHADER_STAGE_FRAGMENT, HB_GPU_SHADER_LANG_HLSL); full += "\n"; - full += hb_gpu_draw_shader_source (HB_GPU_SHADER_STAGE_FRAGMENT, HB_GPU_SHADER_LANG_HLSL); + full += hb_gpu_draw_shader_source (HB_GPU_SHADER_STAGE_FRAGMENT, HB_GPU_SHADER_LANG_HLSL); + full += "\n"; + full += hb_gpu_paint_shader_source (HB_GPU_SHADER_STAGE_FRAGMENT, HB_GPU_SHADER_LANG_HLSL); full += "\n"; full += hlsl_demo; @@ -205,6 +204,25 @@ float4 ps_main (PSInput input) : SV_Target { device->CreateShaderResourceView (atlas.srv_buf, &srvd, &atlas.srv); } + /* Palette: 256 float4, zero-filled for now. */ + { + D3D11_BUFFER_DESC pbd = {}; + pbd.ByteWidth = 256 * 4 * sizeof (float); + pbd.Usage = D3D11_USAGE_IMMUTABLE; + pbd.BindFlags = D3D11_BIND_SHADER_RESOURCE; + pbd.MiscFlags = D3D11_RESOURCE_MISC_BUFFER_STRUCTURED; + pbd.StructureByteStride = 16; + float zero[256 * 4] = {}; + D3D11_SUBRESOURCE_DATA init = { zero, 0, 0 }; + device->CreateBuffer (&pbd, &init, &palette_buf); + + D3D11_SHADER_RESOURCE_VIEW_DESC srvd = {}; + srvd.Format = DXGI_FORMAT_UNKNOWN; + srvd.ViewDimension = D3D11_SRV_DIMENSION_BUFFER; + srvd.Buffer.NumElements = 256; + device->CreateShaderResourceView (palette_buf, &srvd, &palette_srv); + } + /* Atlas backend callbacks */ demo_atlas_backend_t backend = {}; backend.ctx = this; @@ -349,7 +367,8 @@ float4 ps_main (PSInput input) : SV_Target { ctx->PSSetShader (ps, nullptr, 0); ctx->VSSetConstantBuffers (0, 1, &cbuf); ctx->PSSetConstantBuffers (0, 1, &cbuf); - ctx->PSSetShaderResources (0, 1, &atlas.srv); + ID3D11ShaderResourceView *srvs[2] = { atlas.srv, palette_srv }; + ctx->PSSetShaderResources (0, 2, srvs); ctx->OMSetBlendState (blend_state, nullptr, 0xffffffff); UINT stride = sizeof (glyph_vertex_t), offset = 0; ctx->IASetVertexBuffers (0, 1, &vbuf, &stride, &offset); diff --git a/util/gpu/web/hb-gpu-web-webgpu.cc b/util/gpu/web/hb-gpu-web-webgpu.cc index 7867fdbb6..905d42a11 100644 --- a/util/gpu/web/hb-gpu-web-webgpu.cc +++ b/util/gpu/web/hb-gpu-web-webgpu.cc @@ -104,9 +104,21 @@ struct VertexOutput { } @fragment fn fs_main (in: VertexOutput) -> @location(0) vec4f { + /* Compute ppem up front so fwidth is called at uniform control + * flow, before the non-uniform branches below. */ + let fw = fwidth (in.texcoord); + let ppem = 1.0 / max (fw.x, fw.y); + var c = hb_gpu_paint (in.texcoord, in.glyphLoc, u.foreground, &hb_gpu_atlas, &hb_gpu_palette); + if (u.stem_darkening > 0.0 && c.a > 0.0) { + let darkened = hb_gpu_stem_darken (c.a, + dot (u.foreground.rgb, vec3f (1.0 / 3.0)), + ppem); + c = c * (darkened / c.a); + } + if (u.gamma != 1.0) { c.a = pow (c.a, u.gamma); }