mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-24 00:11:14 +01:00
[util/gpu] Extract MSL/HLSL/WGSL demo shaders into own files
Matches the GLSL convention: demo-shader.{msl,hlsl,wgsl} live
alongside demo-fragment.glsl and get stringified by the same
gen-gpu-shader-artifacts.py helper into demo-shader-{msl,hlsl,
wgsl}.hh. No more giant inline R"msl(...)" / R"hlsl(...)" /
R"wgsl(...)" blobs in the .mm / .cc files.
Each language combines vertex and fragment entry points in one
source, as is idiomatic for MSL / HLSL / WGSL compilers; GLSL's
split (demo-vertex.glsl + demo-fragment.glsl) is preserved
because glCompileShader takes separate stages.
No functional change.
Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
5779d9315d
commit
1f813cfb86
10 files changed
+433
-208
No files matched your search
+2
-77
@@ -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<MTLLibrary> library = [r->device newLibraryWithSource:source
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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"
|
||||
;
|
||||
@@ -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"
|
||||
;
|
||||
@@ -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<uniform> u: Uniforms;\n"
|
||||
"@group(0) @binding(1) var<storage, read> hb_gpu_atlas: array<vec4<i32>>;\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"
|
||||
;
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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<uniform> u: Uniforms;
|
||||
@group(0) @binding(1) var<storage, read> hb_gpu_atlas: array<vec4<i32>>;
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -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')
|
||||
|
||||
@@ -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<uniform> u: Uniforms;
|
||||
@group(0) @binding(1) var<storage, read> hb_gpu_atlas: array<vec4<i32>>;
|
||||
|
||||
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;
|
||||
|
||||
Reference in new issue
Block a user