[gpu] Commit stringized shader headers; auto-regenerate like ragel

Pre-generate hb-gpu-{fragment,vertex}-glsl.hh from the .glsl
sources so the library builds without Python/stringize at
compile time.

When stringize is found, a build rule regenerates them from
the .glsl sources and copies back to the source tree, matching
the ragel artifact pattern.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Behdad EsfahbodandClaude Opus 4.6 committed 2026-03-27 23:37:52 -06:00
1 parent 2af0d9787f
commit b1ad797e50
8 files changed
+378 -19

No files matched your search

+46
View File
@@ -0,0 +1,46 @@
#!/usr/bin/env python3
"This tool is intended to be used from meson"
import os, sys, shutil
if len(sys.argv) < 4:
sys.exit(__doc__)
OUTPUT = sys.argv[1]
CURRENT_SOURCE_DIR = sys.argv[2]
INPUT = sys.argv[3]
hh = os.path.basename(OUTPUT)
decl_map = {
'hb-gpu-fragment-glsl.hh': 'hb_gpu_fragment_glsl',
'hb-gpu-vertex-glsl.hh': 'hb_gpu_vertex_glsl',
}
decl = decl_map.get(hh)
if not decl:
sys.exit(f'Unknown shader output: {hh}')
# Stringize the GLSL source into a C string literal
with open(INPUT, 'r') as f:
lines = f.read().splitlines()
parts = [f'static const char *{decl} =\n']
for line in lines:
line = line.replace('\\', '\\\\').replace('"', '\\"')
parts.append(f'"{line}\\n"\n')
parts.append(';\n')
content = ''.join(parts)
with open(OUTPUT, 'w') as f:
f.write(content)
# Copy back to source tree; if read-only, verify committed copy matches.
src_copy = os.path.join(CURRENT_SOURCE_DIR, hh)
try:
shutil.copyfile(OUTPUT, src_copy)
except OSError:
import filecmp
if not filecmp.cmp(OUTPUT, src_copy, shallow=False):
sys.exit(f'{src_copy} is out of date; regenerate with a writable source tree')
+9 -2
View File
@@ -23,5 +23,12 @@ ret = subprocess.Popen (ragel.split() + ['-e', '-F1', '-o', hh, rl], cwd=outdir)
if ret:
sys.exit (ret)
# copy it also to src/
shutil.copyfile (os.path.join (outdir, hh), os.path.join (CURRENT_SOURCE_DIR, hh))
# copy it also to src/; if read-only, verify committed copy matches.
generated = os.path.join (outdir, hh)
src_copy = os.path.join (CURRENT_SOURCE_DIR, hh)
try:
shutil.copyfile (generated, src_copy)
except OSError:
import filecmp
if not filecmp.cmp (generated, src_copy, shallow=False):
sys.exit (f'{src_copy} is out of date; regenerate with a writable source tree')
+230
View File
@@ -0,0 +1,230 @@
static const char *hb_gpu_fragment_glsl =
"/*\n"
" * Copyright (C) 2026 Behdad Esfahbod\n"
" * Copyright (C) 2017 Eric Lengyel\n"
" *\n"
" * Based on the Slug algorithm by Eric Lengyel:\n"
" * https://github.com/EricLengyel/Slug\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"
"/* Requires GLSL 3.30 */\n"
"\n"
"\n"
"#ifndef HB_GPU_UNITS_PER_EM\n"
"#define HB_GPU_UNITS_PER_EM 4\n"
"#endif\n"
"\n"
"#define HB_GPU_INV_UNITS float(1.0 / float(HB_GPU_UNITS_PER_EM))\n"
"\n"
"\n"
"uniform isamplerBuffer hb_gpu_atlas;\n"
"\n"
"\n"
"uint _hb_gpu_calc_root_code (float y1, float y2, float y3)\n"
"{\n"
" uint i1 = floatBitsToUint (y1) >> 31U;\n"
" uint i2 = floatBitsToUint (y2) >> 30U;\n"
" uint i3 = floatBitsToUint (y3) >> 29U;\n"
"\n"
" uint shift = (i2 & 2U) | (i1 & ~2U);\n"
" shift = (i3 & 4U) | (shift & ~4U);\n"
"\n"
" return (0x2E74U >> shift) & 0x0101U;\n"
"}\n"
"\n"
"vec2 _hb_gpu_solve_horiz_poly (vec4 p12, vec2 p3)\n"
"{\n"
" vec2 a = p12.xy - p12.zw * 2.0 + p3;\n"
" vec2 b = p12.xy - p12.zw;\n"
" float ra = 1.0 / a.y;\n"
" float rb = 0.5 / b.y;\n"
"\n"
" float d = sqrt (max (b.y * b.y - a.y * p12.y, 0.0));\n"
" float t1 = (b.y - d) * ra;\n"
" float t2 = (b.y + d) * ra;\n"
"\n"
" if (abs (a.y) < 1.0 / 65536.0)\n"
" t1 = t2 = p12.y * rb;\n"
"\n"
" return vec2 ((a.x * t1 - b.x * 2.0) * t1 + p12.x,\n"
" (a.x * t2 - b.x * 2.0) * t2 + p12.x);\n"
"}\n"
"\n"
"vec2 _hb_gpu_solve_vert_poly (vec4 p12, vec2 p3)\n"
"{\n"
" vec2 a = p12.xy - p12.zw * 2.0 + p3;\n"
" vec2 b = p12.xy - p12.zw;\n"
" float ra = 1.0 / a.x;\n"
" float rb = 0.5 / b.x;\n"
"\n"
" float d = sqrt (max (b.x * b.x - a.x * p12.x, 0.0));\n"
" float t1 = (b.x - d) * ra;\n"
" float t2 = (b.x + d) * ra;\n"
"\n"
" if (abs (a.x) < 1.0 / 65536.0)\n"
" t1 = t2 = p12.x * rb;\n"
"\n"
" return vec2 ((a.y * t1 - b.y * 2.0) * t1 + p12.y,\n"
" (a.y * t2 - b.y * 2.0) * t2 + p12.y);\n"
"}\n"
"\n"
"float _hb_gpu_calc_coverage (float xcov, float ycov, float xwgt, float ywgt)\n"
"{\n"
" float coverage = max (abs (xcov * xwgt + ycov * ywgt) /\n"
" max (xwgt + ywgt, 1.0 / 65536.0),\n"
" min (abs (xcov), abs (ycov)));\n"
"\n"
" return clamp (coverage, 0.0, 1.0);\n"
"}\n"
"\n"
"/* Render a glyph and return its coverage in [0, 1].\n"
" *\n"
" * Requires the hb_gpu_atlas uniform to be bound to the glyph atlas buffer.\n"
" *\n"
" * renderCoord: em-space sample position (interpolated from vertex shader)\n"
" * glyphLoc: offset into hb_gpu_atlas for this glyph's encoded blob\n"
" */\n"
"float hb_gpu_render (vec2 renderCoord, uint glyphLoc_)\n"
"{\n"
" vec2 emsPerPixel = fwidth (renderCoord);\n"
" vec2 pixelsPerEm = 1.0 / emsPerPixel;\n"
"\n"
" int glyphLoc = int (glyphLoc_);\n"
"\n"
" /* Read blob header */\n"
" ivec4 header0 = texelFetch (hb_gpu_atlas, glyphLoc);\n"
" ivec4 header1 = texelFetch (hb_gpu_atlas, glyphLoc + 1);\n"
" vec4 ext = vec4 (header0) * HB_GPU_INV_UNITS; /* min_x, min_y, max_x, max_y */\n"
" int numHBands = header1.r;\n"
" int numVBands = header1.g;\n"
"\n"
" /* Compute band transform from extents */\n"
" vec2 extSize = ext.zw - ext.xy; /* (width, height) */\n"
" vec2 bandScale = vec2 (float (numVBands), float (numHBands)) / max (extSize, vec2 (1.0 / 65536.0));\n"
" vec2 bandOffset = -ext.xy * bandScale;\n"
"\n"
" ivec2 bandIndex = clamp (ivec2 (renderCoord * bandScale + bandOffset),\n"
" ivec2 (0, 0),\n"
" ivec2 (numVBands - 1, numHBands - 1));\n"
"\n"
" /* Skip past header (2 texels) */\n"
" int bandBase = glyphLoc + 2;\n"
"\n"
" float xcov = 0.0;\n"
" float xwgt = 0.0;\n"
"\n"
" ivec4 hbandData = texelFetch (hb_gpu_atlas, bandBase + bandIndex.y);\n"
" int hCurveCount = hbandData.r;\n"
" /* Symmetric: choose rightward (desc) or leftward (asc) sort */\n"
" float hSplit = float (hbandData.a) * HB_GPU_INV_UNITS;\n"
" bool hLeftRay = (renderCoord.x < hSplit);\n"
" int hDataOffset = (hLeftRay ? hbandData.b : hbandData.g) + 32768;\n"
"\n"
" for (int ci = 0; ci < hCurveCount; ci++)\n"
" {\n"
" int curveOffset = texelFetch (hb_gpu_atlas, glyphLoc + hDataOffset + ci).r + 32768;\n"
"\n"
" ivec4 raw12 = texelFetch (hb_gpu_atlas, glyphLoc + curveOffset);\n"
" ivec4 raw3 = texelFetch (hb_gpu_atlas, glyphLoc + curveOffset + 1);\n"
"\n"
" vec4 p12 = vec4 (raw12) * HB_GPU_INV_UNITS - vec4 (renderCoord, renderCoord);\n"
" vec2 p3 = vec2 (raw3.rg) * HB_GPU_INV_UNITS - renderCoord;\n"
"\n"
" if (hLeftRay) {\n"
" if (min (min (p12.x, p12.z), p3.x) * pixelsPerEm.x > 0.5) break;\n"
" } else {\n"
" if (max (max (p12.x, p12.z), p3.x) * pixelsPerEm.x < -0.5) break;\n"
" }\n"
"\n"
" uint code = _hb_gpu_calc_root_code (p12.y, p12.w, p3.y);\n"
" if (code != 0U)\n"
" {\n"
" vec2 r = _hb_gpu_solve_horiz_poly (p12, p3) * pixelsPerEm.x;\n"
" /* For leftward ray: saturate(0.5 - r) counts coverage from the left */\n"
" vec2 cov = hLeftRay ? clamp (vec2 (0.5) - r, 0.0, 1.0)\n"
" : clamp (r + vec2 (0.5), 0.0, 1.0);\n"
"\n"
" if ((code & 1U) != 0U)\n"
" {\n"
" xcov += cov.x;\n"
" xwgt = max (xwgt, clamp (1.0 - abs (r.x) * 2.0, 0.0, 1.0));\n"
" }\n"
"\n"
" if (code > 1U)\n"
" {\n"
" xcov -= cov.y;\n"
" xwgt = max (xwgt, clamp (1.0 - abs (r.y) * 2.0, 0.0, 1.0));\n"
" }\n"
" }\n"
" }\n"
"\n"
" float ycov = 0.0;\n"
" float ywgt = 0.0;\n"
"\n"
" ivec4 vbandData = texelFetch (hb_gpu_atlas, bandBase + numHBands + bandIndex.x);\n"
" int vCurveCount = vbandData.r;\n"
" float vSplit = float (vbandData.a) * HB_GPU_INV_UNITS;\n"
" bool vLeftRay = (renderCoord.y < vSplit);\n"
" int vDataOffset = (vLeftRay ? vbandData.b : vbandData.g) + 32768;\n"
"\n"
" for (int ci = 0; ci < vCurveCount; ci++)\n"
" {\n"
" int curveOffset = texelFetch (hb_gpu_atlas, glyphLoc + vDataOffset + ci).r + 32768;\n"
"\n"
" ivec4 raw12 = texelFetch (hb_gpu_atlas, glyphLoc + curveOffset);\n"
" ivec4 raw3 = texelFetch (hb_gpu_atlas, glyphLoc + curveOffset + 1);\n"
"\n"
" vec4 p12 = vec4 (raw12) * HB_GPU_INV_UNITS - vec4 (renderCoord, renderCoord);\n"
" vec2 p3 = vec2 (raw3.rg) * HB_GPU_INV_UNITS - renderCoord;\n"
"\n"
" if (vLeftRay) {\n"
" if (min (min (p12.y, p12.w), p3.y) * pixelsPerEm.y > 0.5) break;\n"
" } else {\n"
" if (max (max (p12.y, p12.w), p3.y) * pixelsPerEm.y < -0.5) break;\n"
" }\n"
"\n"
" uint code = _hb_gpu_calc_root_code (p12.x, p12.z, p3.x);\n"
" if (code != 0U)\n"
" {\n"
" vec2 r = _hb_gpu_solve_vert_poly (p12, p3) * pixelsPerEm.y;\n"
" vec2 cov = vLeftRay ? clamp (vec2 (0.5) - r, 0.0, 1.0)\n"
" : clamp (r + vec2 (0.5), 0.0, 1.0);\n"
"\n"
" if ((code & 1U) != 0U)\n"
" {\n"
" ycov -= cov.x;\n"
" ywgt = max (ywgt, clamp (1.0 - abs (r.x) * 2.0, 0.0, 1.0));\n"
" }\n"
"\n"
" if (code > 1U)\n"
" {\n"
" ycov += cov.y;\n"
" ywgt = max (ywgt, clamp (1.0 - abs (r.y) * 2.0, 0.0, 1.0));\n"
" }\n"
" }\n"
" }\n"
"\n"
" return _hb_gpu_calc_coverage (xcov, ycov, xwgt, ywgt);\n"
"}\n"
;
+2 -2
View File
@@ -28,8 +28,8 @@
#include "hb-gpu.h"
#include "hb-gpu-fragment-glsl.h"
#include "hb-gpu-vertex-glsl.h"
#include "hb-gpu-fragment-glsl.hh"
#include "hb-gpu-vertex-glsl.hh"
/**
+76
View File
@@ -0,0 +1,76 @@
static const char *hb_gpu_vertex_glsl =
"/*\n"
" * Copyright (C) 2026 Behdad Esfahbod\n"
" * Copyright (C) 2017 Eric Lengyel\n"
" *\n"
" * Based on the Slug algorithm by Eric Lengyel:\n"
" * https://github.com/EricLengyel/Slug\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"
"/* Requires GLSL 3.30 */\n"
"\n"
"\n"
"/* Dilate a glyph vertex by half a pixel on screen.\n"
" *\n"
" * position: object-space vertex position (modified in place)\n"
" * texcoord: em-space sample coordinates (modified in place)\n"
" * normal: object-space outward normal at this vertex\n"
" * jac: inverse of the 2x2 linear part of the em-to-object transform,\n"
" * stored row-major as (j00, j01, j10, j11). Maps object-space\n"
" * displacements back to em-space for texcoord adjustment.\n"
" * For simple scaling with y-flip (the common case):\n"
" * em-to-object = [[s, 0], [0, -s]]\n"
" * jac = (1/s, 0, 0, -1/s)\n"
" * m: model-view-projection matrix\n"
" * viewport: viewport size in pixels\n"
" */\n"
"void hb_gpu_dilate (inout vec2 position, inout vec2 texcoord,\n"
" vec2 normal, vec4 jac,\n"
" mat4 m, vec2 viewport)\n"
"{\n"
" vec2 n = normalize (normal);\n"
"\n"
" vec4 clipPos = m * vec4 (position, 0.0, 1.0);\n"
" vec4 clipN = m * vec4 (n, 0.0, 0.0);\n"
"\n"
" float s = clipPos.w;\n"
" float t = clipN.w;\n"
"\n"
" float u = (s * clipN.x - t * clipPos.x) * viewport.x;\n"
" float v = (s * clipN.y - t * clipPos.y) * viewport.y;\n"
"\n"
" float s2 = s * s;\n"
" float st = s * t;\n"
" float uv = u * u + v * v;\n"
"\n"
" float denom = uv - st * st;\n"
" float d = abs (denom) > 1.0 / 16777216.0\n"
" ? s2 * (st + sqrt (uv)) / denom\n"
" : 0.0;\n"
"\n"
" vec2 dPos = d * normal;\n"
" position += dPos;\n"
" texcoord += vec2 (dot (dPos, jac.xy), dot (dPos, jac.zw));\n"
"}\n"
;
+14 -14
View File
@@ -850,23 +850,21 @@ if not get_option('vector').disabled()
)
endif
hb_gpu_shaders = [
[ 'hb-gpu-fragment.glsl', 'hb-gpu-fragment-glsl.h', 'hb_gpu_fragment_glsl' ],
[ 'hb-gpu-vertex.glsl', 'hb-gpu-vertex-glsl.h', 'hb_gpu_vertex_glsl' ],
hb_gpu_glsl_sources = [
[ 'hb-gpu-fragment.glsl', 'hb-gpu-fragment-glsl.hh', 'hb_gpu_fragment_glsl' ],
[ 'hb-gpu-vertex.glsl', 'hb-gpu-vertex-glsl.hh', 'hb_gpu_vertex_glsl' ],
]
hb_gpu_stringize = find_program('stringize')
hb_gpu_shader_sources = []
foreach p: hb_gpu_shaders
hb_gpu_shader_helper = find_program('gen-gpu-shader-artifacts.py')
foreach p: hb_gpu_glsl_sources
glsl = p.get(0)
header = p.get(1)
decl = p.get(2)
hb_gpu_shader_sources += custom_target('@0@ shader'.format(glsl),
hh = p.get(1)
custom_target('@0@'.format(hh),
build_by_default: true,
input: glsl,
output: header,
capture: true,
command: [ hb_gpu_stringize, 'static const char *' + decl, '@INPUT@' ])
output: hh,
command: [ hb_gpu_shader_helper, '@OUTPUT@',
meson.current_source_dir(), '@INPUT@' ])
endforeach
hb_gpu_sources = [
@@ -875,6 +873,8 @@ hb_gpu_sources = [
'hb-gpu-draw.hh',
'hb-gpu-cu2qu.hh',
'hb-gpu-shaders.cc',
'hb-gpu-fragment-glsl.hh',
'hb-gpu-vertex-glsl.hh',
'hb-static.cc',
]
@@ -887,7 +887,7 @@ if not get_option('gpu').disabled()
gpu_deps = [m_dep]
libharfbuzz_gpu = library('harfbuzz-gpu',
hb_gpu_sources + hb_gpu_shader_sources,
hb_gpu_sources,
include_directories: incconfig,
dependencies: gpu_deps,
link_with: [libharfbuzz],
+1 -1
View File
@@ -24,7 +24,7 @@ gpu_demo_shaders = [
[ 'default-text.txt', 'default-text.h', 'default_text' ],
]
gpu_stringize = find_program('../../src/stringize')
gpu_stringize = find_program('stringize')
gpu_demo_generated = []
File renamed without changes.