[util/gpu] Extract demo shaders into .glsl files

demo_vertex_glsl and demo_fragment_glsl were inline R"glsl(...)"
string literals in util/gpu/demo-shader.cc.  Pull them into
dedicated util/gpu/demo-{vertex,fragment}.glsl source files with
their own copyright headers, and wire the existing
gen-gpu-shader-artifacts.py generator to stringize them into
demo-*-glsl.hh at build time.

Also simplifies gen-gpu-shader-artifacts.py: the decl_map had
grown to cover every shader and its entries were all trivial
filename-to-symbol derivations.  Replace the map with direct
derivation (hyphens -> underscores, strip .hh), so adding more
shaders anywhere -- library or demo -- needs no generator edit.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Behdad Esfahbod committed 2026-04-13 15:10:09 -06:00
1 parent f3fcd46ba9
commit 34a9f22f25
7 files changed
+160 -83

No files matched your search

+5 -19
View File
@@ -12,25 +12,11 @@ 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',
'hb-gpu-fragment-msl.hh': 'hb_gpu_fragment_msl',
'hb-gpu-vertex-msl.hh': 'hb_gpu_vertex_msl',
'hb-gpu-fragment-wgsl.hh': 'hb_gpu_fragment_wgsl',
'hb-gpu-vertex-wgsl.hh': 'hb_gpu_vertex_wgsl',
'hb-gpu-fragment-hlsl.hh': 'hb_gpu_fragment_hlsl',
'hb-gpu-vertex-hlsl.hh': 'hb_gpu_vertex_hlsl',
'hb-gpu-draw-fragment-glsl.hh': 'hb_gpu_draw_fragment_glsl',
'hb-gpu-draw-fragment-msl.hh': 'hb_gpu_draw_fragment_msl',
'hb-gpu-draw-fragment-wgsl.hh': 'hb_gpu_draw_fragment_wgsl',
'hb-gpu-draw-fragment-hlsl.hh': 'hb_gpu_draw_fragment_hlsl',
'hb-gpu-paint-fragment-glsl.hh': 'hb_gpu_paint_fragment_glsl',
}
decl = decl_map.get(hh)
if not decl:
sys.exit(f'Unknown shader output: {hh}')
# Decl name derives from the .hh filename: hyphens -> underscores,
# strip the .hh suffix. Example: hb-gpu-fragment-glsl.hh -> hb_gpu_fragment_glsl.
if not hh.endswith('.hh'):
sys.exit(f'Output must end with .hh: {hh}')
decl = hh[:-len('.hh')].replace('-', '_')
# Stringize the GLSL source into a C string literal
with open(INPUT, 'r') as f:
+39
View File
@@ -0,0 +1,39 @@
static const char *demo_fragment_glsl =
"/*\n"
" * Copyright 2026 Behdad Esfahbod. All Rights Reserved.\n"
" */\n"
"\n"
"uniform float u_gamma;\n"
"uniform float u_debug;\n"
"uniform float u_stem_darkening;\n"
"uniform vec4 u_foreground;\n"
"\n"
"in vec2 v_texcoord;\n"
"flat in uint v_glyphLoc;\n"
"\n"
"out vec4 fragColor;\n"
"\n"
"void main ()\n"
"{\n"
" /* The paint interpreter returns a premultiplied RGBA. For\n"
" * non-color glyphs the encoder synthesizes a single\n"
" * LAYER_SOLID with is_foreground=true, so the result is\n"
" * u_foreground * coverage -- same visual as the old draw\n"
" * path. For COLRv0 glyphs it returns the composited color. */\n"
" vec4 c = hb_gpu_paint (v_texcoord, v_glyphLoc, u_foreground);\n"
"\n"
" if (u_gamma != 1.0)\n"
" c.a = pow (c.a, u_gamma);\n"
"\n"
" if (u_debug > 0.0)\n"
" {\n"
" ivec2 counts = _hb_gpu_curve_counts (v_texcoord, v_glyphLoc);\n"
" float r = clamp (float (counts.x) / 8.0, 0.0, 1.0);\n"
" float g = clamp (float (counts.y) / 8.0, 0.0, 1.0);\n"
" fragColor = vec4 (r, g, c.a, max (max (r, g), c.a));\n"
" return;\n"
" }\n"
"\n"
" fragColor = c;\n"
"}\n"
;
+37
View File
@@ -0,0 +1,37 @@
/*
* Copyright 2026 Behdad Esfahbod. All Rights Reserved.
*/
uniform float u_gamma;
uniform float u_debug;
uniform float u_stem_darkening;
uniform vec4 u_foreground;
in vec2 v_texcoord;
flat in uint v_glyphLoc;
out vec4 fragColor;
void main ()
{
/* The paint interpreter returns a premultiplied RGBA. For
* non-color glyphs the encoder synthesizes a single
* LAYER_SOLID with is_foreground=true, so the result is
* u_foreground * coverage -- same visual as the old draw
* path. For COLRv0 glyphs it returns the composited color. */
vec4 c = hb_gpu_paint (v_texcoord, v_glyphLoc, u_foreground);
if (u_gamma != 1.0)
c.a = pow (c.a, u_gamma);
if (u_debug > 0.0)
{
ivec2 counts = _hb_gpu_curve_counts (v_texcoord, v_glyphLoc);
float r = clamp (float (counts.x) / 8.0, 0.0, 1.0);
float g = clamp (float (counts.y) / 8.0, 0.0, 1.0);
fragColor = vec4 (r, g, c.a, max (max (r, g), c.a));
return;
}
fragColor = c;
}
+2 -64
View File
@@ -4,70 +4,8 @@
#include "demo-shader.h"
static const char *demo_vertex_glsl = R"glsl(
uniform mat4 u_matViewProjection;
uniform vec2 u_viewport;
in vec2 a_position;
in vec2 a_texcoord;
in vec2 a_normal;
in float a_emPerPos;
in uint a_glyphLoc;
out vec2 v_texcoord;
flat out uint v_glyphLoc;
void main ()
{
vec2 pos = a_position;
vec2 tex = a_texcoord;
vec4 jac = vec4 (a_emPerPos, 0.0, 0.0, -a_emPerPos);
hb_gpu_dilate (pos, tex, a_normal, jac,
u_matViewProjection, u_viewport);
gl_Position = u_matViewProjection * vec4 (pos, 0.0, 1.0);
v_texcoord = tex;
v_glyphLoc = a_glyphLoc;
}
)glsl";
static const char *demo_fragment_glsl = R"glsl(
uniform float u_gamma;
uniform float u_debug;
uniform float u_stem_darkening;
uniform vec4 u_foreground;
in vec2 v_texcoord;
flat in uint v_glyphLoc;
out vec4 fragColor;
void main ()
{
/* The paint interpreter returns a premultiplied RGBA. For
* non-color glyphs the encoder synthesizes a single
* LAYER_SOLID with is_foreground=true, so the result is
* u_foreground * coverage -- same visual as the old draw
* path. For COLRv0 glyphs it returns the composited color. */
vec4 c = hb_gpu_paint (v_texcoord, v_glyphLoc, u_foreground);
if (u_gamma != 1.0)
c.a = pow (c.a, u_gamma);
if (u_debug > 0.0)
{
ivec2 counts = _hb_gpu_curve_counts (v_texcoord, v_glyphLoc);
float r = clamp (float (counts.x) / 8.0, 0.0, 1.0);
float g = clamp (float (counts.y) / 8.0, 0.0, 1.0);
fragColor = vec4 (r, g, c.a, max (max (r, g), c.a));
return;
}
fragColor = c;
}
)glsl";
#include "demo-vertex-glsl.hh"
#include "demo-fragment-glsl.hh"
+32
View File
@@ -0,0 +1,32 @@
static const char *demo_vertex_glsl =
"/*\n"
" * Copyright 2026 Behdad Esfahbod. All Rights Reserved.\n"
" */\n"
"\n"
"uniform mat4 u_matViewProjection;\n"
"uniform vec2 u_viewport;\n"
"\n"
"in vec2 a_position;\n"
"in vec2 a_texcoord;\n"
"in vec2 a_normal;\n"
"in float a_emPerPos;\n"
"in uint a_glyphLoc;\n"
"\n"
"out vec2 v_texcoord;\n"
"flat out uint v_glyphLoc;\n"
"\n"
"void main ()\n"
"{\n"
" vec2 pos = a_position;\n"
" vec2 tex = a_texcoord;\n"
"\n"
" vec4 jac = vec4 (a_emPerPos, 0.0, 0.0, -a_emPerPos);\n"
"\n"
" hb_gpu_dilate (pos, tex, a_normal, jac,\n"
" u_matViewProjection, u_viewport);\n"
"\n"
" gl_Position = u_matViewProjection * vec4 (pos, 0.0, 1.0);\n"
" v_texcoord = tex;\n"
" v_glyphLoc = a_glyphLoc;\n"
"}\n"
;
+30
View File
@@ -0,0 +1,30 @@
/*
* Copyright 2026 Behdad Esfahbod. All Rights Reserved.
*/
uniform mat4 u_matViewProjection;
uniform vec2 u_viewport;
in vec2 a_position;
in vec2 a_texcoord;
in vec2 a_normal;
in float a_emPerPos;
in uint a_glyphLoc;
out vec2 v_texcoord;
flat out uint v_glyphLoc;
void main ()
{
vec2 pos = a_position;
vec2 tex = a_texcoord;
vec4 jac = vec4 (a_emPerPos, 0.0, 0.0, -a_emPerPos);
hb_gpu_dilate (pos, tex, a_normal, jac,
u_matViewProjection, u_viewport);
gl_Position = u_matViewProjection * vec4 (pos, 0.0, 1.0);
v_texcoord = tex;
v_glyphLoc = a_glyphLoc;
}
+15
View File
@@ -21,6 +21,21 @@ gpu_demo_sources = files(
'trackball.hh',
)
demo_shader_sources = [
[ 'demo-vertex.glsl', 'demo-vertex-glsl.hh' ],
[ 'demo-fragment.glsl', 'demo-fragment-glsl.hh' ],
]
demo_shader_helper = find_program('../../src/gen-gpu-shader-artifacts.py')
foreach p: demo_shader_sources
gpu_demo_sources += custom_target('@0@'.format(p.get(1)),
build_by_default: true,
input: p.get(0),
output: p.get(1),
command: [ demo_shader_helper, '@OUTPUT@',
meson.current_source_dir(), '@INPUT@' ])
endforeach
if host_machine.system() == 'darwin'
gpu_demo_sources += files(
'demo-metal.mm',