mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-30 10:04:24 +01:00
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>
77 lines
2.2 KiB
Plaintext
77 lines
2.2 KiB
Plaintext
/*
|
|
* 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;
|
|
}
|