diff --git a/src/gen-gpu-shader-artifacts.py b/src/gen-gpu-shader-artifacts.py index 9b1af65d4..6a7f7f903 100755 --- a/src/gen-gpu-shader-artifacts.py +++ b/src/gen-gpu-shader-artifacts.py @@ -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: diff --git a/util/gpu/demo-fragment-glsl.hh b/util/gpu/demo-fragment-glsl.hh new file mode 100644 index 000000000..39c2dcfcf --- /dev/null +++ b/util/gpu/demo-fragment-glsl.hh @@ -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" +; diff --git a/util/gpu/demo-fragment.glsl b/util/gpu/demo-fragment.glsl new file mode 100644 index 000000000..bcd52b01f --- /dev/null +++ b/util/gpu/demo-fragment.glsl @@ -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; +} diff --git a/util/gpu/demo-shader.cc b/util/gpu/demo-shader.cc index 9bbb1c368..d739103cd 100644 --- a/util/gpu/demo-shader.cc +++ b/util/gpu/demo-shader.cc @@ -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" diff --git a/util/gpu/demo-vertex-glsl.hh b/util/gpu/demo-vertex-glsl.hh new file mode 100644 index 000000000..8f8260e60 --- /dev/null +++ b/util/gpu/demo-vertex-glsl.hh @@ -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" +; diff --git a/util/gpu/demo-vertex.glsl b/util/gpu/demo-vertex.glsl new file mode 100644 index 000000000..687b7c340 --- /dev/null +++ b/util/gpu/demo-vertex.glsl @@ -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; +} diff --git a/util/gpu/meson.build b/util/gpu/meson.build index 62f751bd8..920bb73c5 100644 --- a/util/gpu/meson.build +++ b/util/gpu/meson.build @@ -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',