Files
harfbuzz/src/hb-gpu-vertex.glsl
T
Behdad Esfahbod f587af3ec3 [gpu] Extract shared shader helpers into hb_gpu_shader_source() (#5921)
The draw-renderer fragment shaders defined utilities
(HB_GPU_UNITS_PER_EM, the hb_gpu_atlas uniform, hb_gpu_fetch) that
any future GPU renderer will also need.  The draw-renderer vertex
shader likewise contained only the generic hb_gpu_dilate helper.
If a paint renderer were added today and a caller linked both
fragment sources together, hb_gpu_fetch would be defined twice.

Move these shared helpers into a new family of sources and expose
them via a new public entry point:

  hb_gpu_shader_source (stage, lang);

The existing hb_gpu_draw_shader_source() now returns only the
draw-specific fragment code; it returns an empty string for the
vertex stage because all current vertex logic is shared.  Callers
concatenate the shared source first, then the draw-specific
source, then their own main():

  [#version, hb_gpu_shader_source (...), hb_gpu_draw_shader_source
  (...), main]

Additions:
 - src/hb-gpu-fragment.{glsl,msl,wgsl,hlsl} — shared fragment
 - src/hb-gpu-vertex.{glsl,msl,wgsl,hlsl}   — shared vertex,
   renamed from hb-gpu-draw-vertex.*
 - src/hb-gpu-shaders.cc — hb_gpu_shader_source() dispatcher

Removals:
 - src/hb-gpu-draw-vertex.{glsl,msl,wgsl,hlsl} — moved to common

Shared sources are Behdad-only copyright; the Eric Lengyel / Slug
attribution stays on the draw-specific fragment sources where the
Slug algorithm actually lives.

Updates: test/api/test-gpu.cc, the Metal/D3D11/WebGPU/GL demo
shaders, the hb-gpu SECTION documentation, meson.build,
CMakeLists.txt, gen-gpu-shader-artifacts.py decl_map,
harfbuzz-world.cc, and docs/harfbuzz-sections.txt.

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

71 lines
2.5 KiB
GLSL

/*
* 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.
*/
/* Requires GLSL 3.30 or GLSL ES 3.00. */
/* Dilate a glyph vertex by half a pixel on screen.
*
* position: object-space vertex position (modified in place)
* texcoord: em-space sample coordinates (modified in place)
* normal: object-space outward normal at this vertex
* jac: inverse of the 2x2 linear part of the em-to-object transform,
* stored row-major as (j00, j01, j10, j11). Maps object-space
* displacements back to em-space for texcoord adjustment.
* For simple scaling with y-flip (the common case):
* em-to-object = [[s, 0], [0, -s]]
* jac = (1/s, 0, 0, -1/s)
* m: model-view-projection matrix
* viewport: viewport size in pixels
*/
void hb_gpu_dilate (inout vec2 position, inout vec2 texcoord,
vec2 normal, vec4 jac,
mat4 m, vec2 viewport)
{
vec2 n = normalize (normal);
vec4 clipPos = m * vec4 (position, 0.0, 1.0);
vec4 clipN = m * vec4 (n, 0.0, 0.0);
float s = clipPos.w;
float t = clipN.w;
float u = (s * clipN.x - t * clipPos.x) * viewport.x;
float v = (s * clipN.y - t * clipPos.y) * viewport.y;
float s2 = s * s;
float st = s * t;
float uv = u * u + v * v;
float denom = uv - st * st;
float d = abs (denom) > 1.0 / 16777216.0
? s2 * (st + sqrt (uv)) / denom
: 0.0;
vec2 dPos = d * normal;
position += dPos;
texcoord += vec2 (dot (dPos, jac.xy), dot (dPos, jac.zw));
}