diff --git a/util/gpu/demo-metal.mm b/util/gpu/demo-metal.mm index bcd5643fd..efca65857 100644 --- a/util/gpu/demo-metal.mm +++ b/util/gpu/demo-metal.mm @@ -16,82 +16,7 @@ #define TEXEL_SIZE 8 /* sizeof short4 texel */ -static const char *demo_metal_shader_source = R"msl( - -struct Uniforms { - float4x4 matViewProjection; - float2 viewport; - float gamma; - float stem_darkening; - float4 foreground; - float debug; -}; - -struct VertexIn { - float2 position [[attribute(0)]]; - float2 texcoord [[attribute(1)]]; - float2 normal [[attribute(2)]]; - float emPerPos [[attribute(3)]]; - uint glyphLoc [[attribute(4)]]; -}; - -struct VertexOut { - float4 position [[position]]; - float2 texcoord; - uint glyphLoc [[flat]]; -}; - -vertex VertexOut vertex_main(VertexIn in [[stage_in]], - constant Uniforms& uniforms [[buffer(1)]]) { - float2 pos = in.position; - float2 tex = in.texcoord; - float4 jac = float4(in.emPerPos, 0.0, 0.0, -in.emPerPos); - - hb_gpu_dilate(pos, tex, in.normal, jac, - uniforms.matViewProjection, uniforms.viewport); - - VertexOut out; - out.position = uniforms.matViewProjection * float4(pos, 0.0, 1.0); - out.texcoord = tex; - out.glyphLoc = in.glyphLoc; - return out; -} - -fragment float4 fragment_main(VertexOut in [[stage_in]], - constant Uniforms& uniforms [[buffer(1)]], - device const short4* atlas [[buffer(0)]]) { -#ifdef HB_GPU_DEMO_DRAW - float cov = hb_gpu_draw(in.texcoord, in.glyphLoc, atlas); - float4 c = float4(uniforms.foreground.rgb * uniforms.foreground.a, - uniforms.foreground.a) * cov; -#else - /* Paint interpreter returns premultiplied RGBA. */ - float4 c = hb_gpu_paint(in.texcoord, in.glyphLoc, uniforms.foreground, - atlas); -#endif - - if (uniforms.stem_darkening > 0.0 && c.a > 0.0) { - float2 fw = fwidth(in.texcoord); - float ppem = 1.0 / max(fw.x, fw.y); - float brightness = dot(c.rgb, float3(1.0 / 3.0)) / c.a; - float darkened = hb_gpu_stem_darken(c.a, brightness, ppem); - c *= darkened / c.a; - } - - if (uniforms.gamma != 1.0) - c.a = pow(c.a, uniforms.gamma); - - if (uniforms.debug > 0.0) { - int2 counts = _hb_gpu_curve_counts(in.texcoord, in.glyphLoc, atlas); - float r = clamp(float(counts.x) / 8.0, 0.0, 1.0); - float g = clamp(float(counts.y) / 8.0, 0.0, 1.0); - return float4(r, g, c.a, max(max(r, g), c.a)); - } - - return c; -} - -)msl"; +#include "demo-shader-msl.hh" struct demo_renderer_metal_t : demo_renderer_t @@ -423,7 +348,7 @@ demo_renderer_create_metal (GLFWwindow *window, bool draw_only) NSString *source = [NSString stringWithFormat:@"%@%s%s%s%s%s", preamble, vert_common, vert_src, frag_common, frag_mode, - demo_metal_shader_source]; + demo_shader_msl]; NSError *error = nil; id library = [r->device newLibraryWithSource:source diff --git a/util/gpu/demo-renderer-d3d11.cc b/util/gpu/demo-renderer-d3d11.cc index e34008ab0..c29ec6255 100644 --- a/util/gpu/demo-renderer-d3d11.cc +++ b/util/gpu/demo-renderer-d3d11.cc @@ -87,64 +87,7 @@ struct demo_renderer_d3d11_t : demo_renderer_t &scd, &swapchain, &device, nullptr, &ctx); /* Compile shaders */ - static const char *hlsl_demo = R"hlsl( -cbuffer Uniforms : register(b0) { - float4x4 mvp; - float2 viewport; - float gamma; - float stem_darkening; - float4 foreground; - float debug; - float3 _pad; -}; - -struct VSInput { - float2 position : POSITION; - float2 texcoord : TEXCOORD0; - float2 normal : NORMAL; - float emPerPos : TEXCOORD1; - uint glyphLoc : TEXCOORD2; -}; - -struct PSInput { - float4 pos : SV_Position; - float2 texcoord : TEXCOORD0; - nointerpolation uint glyphLoc : TEXCOORD1; -}; - -PSInput vs_main (VSInput input) { - float2 pos = input.position; - float2 tex = input.texcoord; - float4 jac = float4 (input.emPerPos, 0.0, 0.0, -input.emPerPos); - hb_gpu_dilate (pos, tex, input.normal, jac, mvp, viewport); - PSInput output; - output.pos = mul (mvp, float4 (pos, 0.0, 1.0)); - output.texcoord = tex; - output.glyphLoc = input.glyphLoc; - return output; -} - -float4 ps_main (PSInput input) : SV_Target { -#ifdef HB_GPU_DEMO_DRAW - float cov = hb_gpu_draw (input.texcoord, input.glyphLoc); - float4 c = float4 (foreground.rgb * foreground.a, foreground.a) * cov; -#else - float4 c = hb_gpu_paint (input.texcoord, input.glyphLoc, foreground); -#endif - - if (stem_darkening > 0.0 && c.a > 0.0) { - float2 fw = fwidth (input.texcoord); - float ppem = 1.0 / max (fw.x, fw.y); - float brightness = dot (c.rgb, float3 (1.0/3.0, 1.0/3.0, 1.0/3.0)) / c.a; - float darkened = hb_gpu_stem_darken (c.a, brightness, ppem); - c *= darkened / c.a; - } - - if (gamma != 1.0) - c.a = pow (c.a, gamma); - return c; -} -)hlsl"; + #include "demo-shader-hlsl.hh" std::string full; if (draw_only) @@ -161,7 +104,7 @@ float4 ps_main (PSInput input) : SV_Target { else full += hb_gpu_paint_shader_source (HB_GPU_SHADER_STAGE_FRAGMENT, HB_GPU_SHADER_LANG_HLSL); full += "\n"; - full += hlsl_demo; + full += demo_shader_hlsl; auto compile = [&](const char *entry, const char *target) -> ID3DBlob* { ID3DBlob *blob = nullptr, *errors = nullptr; diff --git a/util/gpu/demo-shader-hlsl.hh b/util/gpu/demo-shader-hlsl.hh new file mode 100644 index 000000000..4467f7f97 --- /dev/null +++ b/util/gpu/demo-shader-hlsl.hh @@ -0,0 +1,62 @@ +static const char *demo_shader_hlsl = +"/*\n" +" * Copyright 2026 Behdad Esfahbod. All Rights Reserved.\n" +" */\n" +"\n" +"cbuffer Uniforms : register(b0) {\n" +" float4x4 mvp;\n" +" float2 viewport;\n" +" float gamma;\n" +" float stem_darkening;\n" +" float4 foreground;\n" +" float debug;\n" +" float3 _pad;\n" +"};\n" +"\n" +"struct VSInput {\n" +" float2 position : POSITION;\n" +" float2 texcoord : TEXCOORD0;\n" +" float2 normal : NORMAL;\n" +" float emPerPos : TEXCOORD1;\n" +" uint glyphLoc : TEXCOORD2;\n" +"};\n" +"\n" +"struct PSInput {\n" +" float4 pos : SV_Position;\n" +" float2 texcoord : TEXCOORD0;\n" +" nointerpolation uint glyphLoc : TEXCOORD1;\n" +"};\n" +"\n" +"PSInput vs_main (VSInput input) {\n" +" float2 pos = input.position;\n" +" float2 tex = input.texcoord;\n" +" float4 jac = float4 (input.emPerPos, 0.0, 0.0, -input.emPerPos);\n" +" hb_gpu_dilate (pos, tex, input.normal, jac, mvp, viewport);\n" +" PSInput output;\n" +" output.pos = mul (mvp, float4 (pos, 0.0, 1.0));\n" +" output.texcoord = tex;\n" +" output.glyphLoc = input.glyphLoc;\n" +" return output;\n" +"}\n" +"\n" +"float4 ps_main (PSInput input) : SV_Target {\n" +"#ifdef HB_GPU_DEMO_DRAW\n" +" float cov = hb_gpu_draw (input.texcoord, input.glyphLoc);\n" +" float4 c = float4 (foreground.rgb * foreground.a, foreground.a) * cov;\n" +"#else\n" +" float4 c = hb_gpu_paint (input.texcoord, input.glyphLoc, foreground);\n" +"#endif\n" +"\n" +" if (stem_darkening > 0.0 && c.a > 0.0) {\n" +" float2 fw = fwidth (input.texcoord);\n" +" float ppem = 1.0 / max (fw.x, fw.y);\n" +" float brightness = dot (c.rgb, float3 (1.0/3.0, 1.0/3.0, 1.0/3.0)) / c.a;\n" +" float darkened = hb_gpu_stem_darken (c.a, brightness, ppem);\n" +" c *= darkened / c.a;\n" +" }\n" +"\n" +" if (gamma != 1.0)\n" +" c.a = pow (c.a, gamma);\n" +" return c;\n" +"}\n" +; diff --git a/util/gpu/demo-shader-msl.hh b/util/gpu/demo-shader-msl.hh new file mode 100644 index 000000000..63e0b3a07 --- /dev/null +++ b/util/gpu/demo-shader-msl.hh @@ -0,0 +1,78 @@ +static const char *demo_shader_msl = +"/*\n" +" * Copyright 2026 Behdad Esfahbod. All Rights Reserved.\n" +" */\n" +"\n" +"struct Uniforms {\n" +" float4x4 matViewProjection;\n" +" float2 viewport;\n" +" float gamma;\n" +" float stem_darkening;\n" +" float4 foreground;\n" +" float debug;\n" +"};\n" +"\n" +"struct VertexIn {\n" +" float2 position [[attribute(0)]];\n" +" float2 texcoord [[attribute(1)]];\n" +" float2 normal [[attribute(2)]];\n" +" float emPerPos [[attribute(3)]];\n" +" uint glyphLoc [[attribute(4)]];\n" +"};\n" +"\n" +"struct VertexOut {\n" +" float4 position [[position]];\n" +" float2 texcoord;\n" +" uint glyphLoc [[flat]];\n" +"};\n" +"\n" +"vertex VertexOut vertex_main(VertexIn in [[stage_in]],\n" +" constant Uniforms& uniforms [[buffer(1)]]) {\n" +" float2 pos = in.position;\n" +" float2 tex = in.texcoord;\n" +" float4 jac = float4(in.emPerPos, 0.0, 0.0, -in.emPerPos);\n" +"\n" +" hb_gpu_dilate(pos, tex, in.normal, jac,\n" +" uniforms.matViewProjection, uniforms.viewport);\n" +"\n" +" VertexOut out;\n" +" out.position = uniforms.matViewProjection * float4(pos, 0.0, 1.0);\n" +" out.texcoord = tex;\n" +" out.glyphLoc = in.glyphLoc;\n" +" return out;\n" +"}\n" +"\n" +"fragment float4 fragment_main(VertexOut in [[stage_in]],\n" +" constant Uniforms& uniforms [[buffer(1)]],\n" +" device const short4* atlas [[buffer(0)]]) {\n" +"#ifdef HB_GPU_DEMO_DRAW\n" +" float cov = hb_gpu_draw(in.texcoord, in.glyphLoc, atlas);\n" +" float4 c = float4(uniforms.foreground.rgb * uniforms.foreground.a,\n" +" uniforms.foreground.a) * cov;\n" +"#else\n" +" /* Paint interpreter returns premultiplied RGBA. */\n" +" float4 c = hb_gpu_paint(in.texcoord, in.glyphLoc, uniforms.foreground,\n" +" atlas);\n" +"#endif\n" +"\n" +" if (uniforms.stem_darkening > 0.0 && c.a > 0.0) {\n" +" float2 fw = fwidth(in.texcoord);\n" +" float ppem = 1.0 / max(fw.x, fw.y);\n" +" float brightness = dot(c.rgb, float3(1.0 / 3.0)) / c.a;\n" +" float darkened = hb_gpu_stem_darken(c.a, brightness, ppem);\n" +" c *= darkened / c.a;\n" +" }\n" +"\n" +" if (uniforms.gamma != 1.0)\n" +" c.a = pow(c.a, uniforms.gamma);\n" +"\n" +" if (uniforms.debug > 0.0) {\n" +" int2 counts = _hb_gpu_curve_counts(in.texcoord, in.glyphLoc, atlas);\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" +" return float4(r, g, c.a, max(max(r, g), c.a));\n" +" }\n" +"\n" +" return c;\n" +"}\n" +; diff --git a/util/gpu/demo-shader-wgsl.hh b/util/gpu/demo-shader-wgsl.hh new file mode 100644 index 000000000..ada3dc5db --- /dev/null +++ b/util/gpu/demo-shader-wgsl.hh @@ -0,0 +1,75 @@ +static const char *demo_shader_wgsl = +"/*\n" +" * Copyright 2026 Behdad Esfahbod. All Rights Reserved.\n" +" */\n" +"\n" +"struct Uniforms {\n" +" mvp: mat4x4f,\n" +" viewport: vec2f,\n" +" gamma: f32,\n" +" stem_darkening: f32,\n" +" foreground: vec4f,\n" +" debug: f32,\n" +"};\n" +"\n" +"@group(0) @binding(0) var u: Uniforms;\n" +"@group(0) @binding(1) var hb_gpu_atlas: array>;\n" +"\n" +"struct VertexInput {\n" +" @location(0) position: vec2f,\n" +" @location(1) texcoord: vec2f,\n" +" @location(2) normal: vec2f,\n" +" @location(3) emPerPos: f32,\n" +" @location(4) glyphLoc: u32,\n" +"};\n" +"\n" +"struct VertexOutput {\n" +" @builtin(position) clip_position: vec4f,\n" +" @location(0) texcoord: vec2f,\n" +" @location(1) @interpolate(flat) glyphLoc: u32,\n" +"};\n" +"\n" +"@vertex fn vs_main (in: VertexInput) -> VertexOutput {\n" +" var pos = in.position;\n" +" var tc = in.texcoord;\n" +" let jac = vec4f (in.emPerPos, 0.0, 0.0, -in.emPerPos);\n" +" let result = hb_gpu_dilate (pos, tc, in.normal, jac, u.mvp, u.viewport);\n" +" pos = result[0];\n" +" tc = result[1];\n" +"\n" +" var out: VertexOutput;\n" +" out.clip_position = u.mvp * vec4f (pos, 0.0, 1.0);\n" +" out.texcoord = tc;\n" +" out.glyphLoc = in.glyphLoc;\n" +" return out;\n" +"}\n" +"\n" +"@fragment fn fs_main (in: VertexOutput) -> @location(0) vec4f {\n" +" /* Compute ppem up front so fwidth is called at uniform control\n" +" * flow, before the non-uniform branches below. */\n" +" let fw = fwidth (in.texcoord);\n" +" let ppem = 1.0 / max (fw.x, fw.y);\n" +"\n" +" var c = hb_gpu_paint (in.texcoord, in.glyphLoc, u.foreground,\n" +" &hb_gpu_atlas);\n" +"\n" +" if (u.stem_darkening > 0.0 && c.a > 0.0) {\n" +" let brightness = dot (c.rgb, vec3f (1.0 / 3.0)) / c.a;\n" +" let darkened = hb_gpu_stem_darken (c.a, brightness, ppem);\n" +" c = c * (darkened / c.a);\n" +" }\n" +"\n" +" if (u.gamma != 1.0) {\n" +" c.a = pow (c.a, u.gamma);\n" +" }\n" +"\n" +" if (u.debug > 0.0) {\n" +" let counts = _hb_gpu_curve_counts (in.texcoord, in.glyphLoc, &hb_gpu_atlas);\n" +" let r = clamp (f32 (counts.x) / 8.0, 0.0, 1.0);\n" +" let g = clamp (f32 (counts.y) / 8.0, 0.0, 1.0);\n" +" return vec4f (r, g, c.a, max (max (r, g), c.a));\n" +" }\n" +"\n" +" return c;\n" +"}\n" +; diff --git a/util/gpu/demo-shader.hlsl b/util/gpu/demo-shader.hlsl new file mode 100644 index 000000000..d9a69ebe9 --- /dev/null +++ b/util/gpu/demo-shader.hlsl @@ -0,0 +1,60 @@ +/* + * Copyright 2026 Behdad Esfahbod. All Rights Reserved. + */ + +cbuffer Uniforms : register(b0) { + float4x4 mvp; + float2 viewport; + float gamma; + float stem_darkening; + float4 foreground; + float debug; + float3 _pad; +}; + +struct VSInput { + float2 position : POSITION; + float2 texcoord : TEXCOORD0; + float2 normal : NORMAL; + float emPerPos : TEXCOORD1; + uint glyphLoc : TEXCOORD2; +}; + +struct PSInput { + float4 pos : SV_Position; + float2 texcoord : TEXCOORD0; + nointerpolation uint glyphLoc : TEXCOORD1; +}; + +PSInput vs_main (VSInput input) { + float2 pos = input.position; + float2 tex = input.texcoord; + float4 jac = float4 (input.emPerPos, 0.0, 0.0, -input.emPerPos); + hb_gpu_dilate (pos, tex, input.normal, jac, mvp, viewport); + PSInput output; + output.pos = mul (mvp, float4 (pos, 0.0, 1.0)); + output.texcoord = tex; + output.glyphLoc = input.glyphLoc; + return output; +} + +float4 ps_main (PSInput input) : SV_Target { +#ifdef HB_GPU_DEMO_DRAW + float cov = hb_gpu_draw (input.texcoord, input.glyphLoc); + float4 c = float4 (foreground.rgb * foreground.a, foreground.a) * cov; +#else + float4 c = hb_gpu_paint (input.texcoord, input.glyphLoc, foreground); +#endif + + if (stem_darkening > 0.0 && c.a > 0.0) { + float2 fw = fwidth (input.texcoord); + float ppem = 1.0 / max (fw.x, fw.y); + float brightness = dot (c.rgb, float3 (1.0/3.0, 1.0/3.0, 1.0/3.0)) / c.a; + float darkened = hb_gpu_stem_darken (c.a, brightness, ppem); + c *= darkened / c.a; + } + + if (gamma != 1.0) + c.a = pow (c.a, gamma); + return c; +} diff --git a/util/gpu/demo-shader.msl b/util/gpu/demo-shader.msl new file mode 100644 index 000000000..d5e7d7217 --- /dev/null +++ b/util/gpu/demo-shader.msl @@ -0,0 +1,76 @@ +/* + * Copyright 2026 Behdad Esfahbod. All Rights Reserved. + */ + +struct Uniforms { + float4x4 matViewProjection; + float2 viewport; + float gamma; + float stem_darkening; + float4 foreground; + float debug; +}; + +struct VertexIn { + float2 position [[attribute(0)]]; + float2 texcoord [[attribute(1)]]; + float2 normal [[attribute(2)]]; + float emPerPos [[attribute(3)]]; + uint glyphLoc [[attribute(4)]]; +}; + +struct VertexOut { + float4 position [[position]]; + float2 texcoord; + uint glyphLoc [[flat]]; +}; + +vertex VertexOut vertex_main(VertexIn in [[stage_in]], + constant Uniforms& uniforms [[buffer(1)]]) { + float2 pos = in.position; + float2 tex = in.texcoord; + float4 jac = float4(in.emPerPos, 0.0, 0.0, -in.emPerPos); + + hb_gpu_dilate(pos, tex, in.normal, jac, + uniforms.matViewProjection, uniforms.viewport); + + VertexOut out; + out.position = uniforms.matViewProjection * float4(pos, 0.0, 1.0); + out.texcoord = tex; + out.glyphLoc = in.glyphLoc; + return out; +} + +fragment float4 fragment_main(VertexOut in [[stage_in]], + constant Uniforms& uniforms [[buffer(1)]], + device const short4* atlas [[buffer(0)]]) { +#ifdef HB_GPU_DEMO_DRAW + float cov = hb_gpu_draw(in.texcoord, in.glyphLoc, atlas); + float4 c = float4(uniforms.foreground.rgb * uniforms.foreground.a, + uniforms.foreground.a) * cov; +#else + /* Paint interpreter returns premultiplied RGBA. */ + float4 c = hb_gpu_paint(in.texcoord, in.glyphLoc, uniforms.foreground, + atlas); +#endif + + if (uniforms.stem_darkening > 0.0 && c.a > 0.0) { + float2 fw = fwidth(in.texcoord); + float ppem = 1.0 / max(fw.x, fw.y); + float brightness = dot(c.rgb, float3(1.0 / 3.0)) / c.a; + float darkened = hb_gpu_stem_darken(c.a, brightness, ppem); + c *= darkened / c.a; + } + + if (uniforms.gamma != 1.0) + c.a = pow(c.a, uniforms.gamma); + + if (uniforms.debug > 0.0) { + int2 counts = _hb_gpu_curve_counts(in.texcoord, in.glyphLoc, atlas); + float r = clamp(float(counts.x) / 8.0, 0.0, 1.0); + float g = clamp(float(counts.y) / 8.0, 0.0, 1.0); + return float4(r, g, c.a, max(max(r, g), c.a)); + } + + return c; +} diff --git a/util/gpu/demo-shader.wgsl b/util/gpu/demo-shader.wgsl new file mode 100644 index 000000000..8cc96ce77 --- /dev/null +++ b/util/gpu/demo-shader.wgsl @@ -0,0 +1,73 @@ +/* + * Copyright 2026 Behdad Esfahbod. All Rights Reserved. + */ + +struct Uniforms { + mvp: mat4x4f, + viewport: vec2f, + gamma: f32, + stem_darkening: f32, + foreground: vec4f, + debug: f32, +}; + +@group(0) @binding(0) var u: Uniforms; +@group(0) @binding(1) var hb_gpu_atlas: array>; + +struct VertexInput { + @location(0) position: vec2f, + @location(1) texcoord: vec2f, + @location(2) normal: vec2f, + @location(3) emPerPos: f32, + @location(4) glyphLoc: u32, +}; + +struct VertexOutput { + @builtin(position) clip_position: vec4f, + @location(0) texcoord: vec2f, + @location(1) @interpolate(flat) glyphLoc: u32, +}; + +@vertex fn vs_main (in: VertexInput) -> VertexOutput { + var pos = in.position; + var tc = in.texcoord; + let jac = vec4f (in.emPerPos, 0.0, 0.0, -in.emPerPos); + let result = hb_gpu_dilate (pos, tc, in.normal, jac, u.mvp, u.viewport); + pos = result[0]; + tc = result[1]; + + var out: VertexOutput; + out.clip_position = u.mvp * vec4f (pos, 0.0, 1.0); + out.texcoord = tc; + out.glyphLoc = in.glyphLoc; + return out; +} + +@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); + + if (u.stem_darkening > 0.0 && c.a > 0.0) { + let brightness = dot (c.rgb, vec3f (1.0 / 3.0)) / c.a; + let darkened = hb_gpu_stem_darken (c.a, brightness, ppem); + c = c * (darkened / c.a); + } + + if (u.gamma != 1.0) { + c.a = pow (c.a, u.gamma); + } + + if (u.debug > 0.0) { + let counts = _hb_gpu_curve_counts (in.texcoord, in.glyphLoc, &hb_gpu_atlas); + let r = clamp (f32 (counts.x) / 8.0, 0.0, 1.0); + let g = clamp (f32 (counts.y) / 8.0, 0.0, 1.0); + return vec4f (r, g, c.a, max (max (r, g), c.a)); + } + + return c; +} diff --git a/util/gpu/meson.build b/util/gpu/meson.build index 920bb73c5..18b6f8675 100644 --- a/util/gpu/meson.build +++ b/util/gpu/meson.build @@ -24,6 +24,9 @@ gpu_demo_sources = files( demo_shader_sources = [ [ 'demo-vertex.glsl', 'demo-vertex-glsl.hh' ], [ 'demo-fragment.glsl', 'demo-fragment-glsl.hh' ], + [ 'demo-shader.msl', 'demo-shader-msl.hh' ], + [ 'demo-shader.hlsl', 'demo-shader-hlsl.hh' ], + [ 'demo-shader.wgsl', 'demo-shader-wgsl.hh' ], ] demo_shader_helper = find_program('../../src/gen-gpu-shader-artifacts.py') diff --git a/util/gpu/web/hb-gpu-web-webgpu.cc b/util/gpu/web/hb-gpu-web-webgpu.cc index 4bb993867..4a2b78e32 100644 --- a/util/gpu/web/hb-gpu-web-webgpu.cc +++ b/util/gpu/web/hb-gpu-web-webgpu.cc @@ -60,77 +60,7 @@ /* ---- WGSL demo shader ---- */ -static const char *wgsl_demo_shader = R"wgsl( -struct Uniforms { - mvp: mat4x4f, - viewport: vec2f, - gamma: f32, - stem_darkening: f32, - foreground: vec4f, - debug: f32, -}; - -@group(0) @binding(0) var u: Uniforms; -@group(0) @binding(1) var hb_gpu_atlas: array>; - -struct VertexInput { - @location(0) position: vec2f, - @location(1) texcoord: vec2f, - @location(2) normal: vec2f, - @location(3) emPerPos: f32, - @location(4) glyphLoc: u32, -}; - -struct VertexOutput { - @builtin(position) clip_position: vec4f, - @location(0) texcoord: vec2f, - @location(1) @interpolate(flat) glyphLoc: u32, -}; - -@vertex fn vs_main (in: VertexInput) -> VertexOutput { - var pos = in.position; - var tc = in.texcoord; - let jac = vec4f (in.emPerPos, 0.0, 0.0, -in.emPerPos); - let result = hb_gpu_dilate (pos, tc, in.normal, jac, u.mvp, u.viewport); - pos = result[0]; - tc = result[1]; - - var out: VertexOutput; - out.clip_position = u.mvp * vec4f (pos, 0.0, 1.0); - out.texcoord = tc; - out.glyphLoc = in.glyphLoc; - return out; -} - -@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); - - if (u.stem_darkening > 0.0 && c.a > 0.0) { - let brightness = dot (c.rgb, vec3f (1.0 / 3.0)) / c.a; - let darkened = hb_gpu_stem_darken (c.a, brightness, ppem); - c = c * (darkened / c.a); - } - - if (u.gamma != 1.0) { - c.a = pow (c.a, u.gamma); - } - - if (u.debug > 0.0) { - let counts = _hb_gpu_curve_counts (in.texcoord, in.glyphLoc, &hb_gpu_atlas); - let r = clamp (f32 (counts.x) / 8.0, 0.0, 1.0); - let g = clamp (f32 (counts.y) / 8.0, 0.0, 1.0); - return vec4f (r, g, c.a, max (max (r, g), c.a)); - } - - return c; -} -)wgsl"; +#include "../demo-shader-wgsl.hh" /* ---- Uniform buffer layout (must match WGSL struct) ---- */ @@ -849,7 +779,7 @@ create_pipeline () wgsl += "\n"; wgsl += hb_gpu_paint_shader_source (HB_GPU_SHADER_STAGE_FRAGMENT, HB_GPU_SHADER_LANG_WGSL); wgsl += "\n"; - wgsl += wgsl_demo_shader; + wgsl += demo_shader_wgsl; WGPUShaderSourceWGSL wgslSrc = {}; wgslSrc.chain.sType = WGPUSType_ShaderSourceWGSL;