Implement texture atlas for fonts

This commit is contained in:
Dynamitos committed 2026-10-21 21:21:49 +02:00
1 parent 17dc1e4d88
commit 22a90fe07c
14 files changed
+388 -50

No files matched your search

+11 -6
View File
@@ -10,8 +10,10 @@ struct RenderElementStyle
float opacity;
float z;
uint textureIndex;
uint pad0;
uint pad1;
// UV rect for textured quads (glyphs: their rect in the font atlas)
float2 uvOffset;
float2 uvSize;
float2 pad;
};
struct UIParameter
@@ -69,12 +71,15 @@ float4 fragmentMain(
if(style.textureIndex == -1) {
return float4(style.color, style.opacity);
}
// Glyph quads: the texture is R8_UNORM (coverage in the R channel, A baked to 1.0).
// Reading it as a Texture2D gives (R=coverage, G=0, B=0, A=1) — so the alpha we want
// is in .r, NOT .a (using .a would make every pixel opaque -> the "black cutout").
// Glyph quads: the texture is the font's per-size atlas (R8_UNORM, coverage in the R
// channel, A baked to 1.0). Reading it as a Texture2D gives (R=coverage, G=0, B=0, A=1)
// — so the alpha we want is in .r, NOT .a (using .a would make every pixel opaque).
// Output the pure text color + coverage as alpha; the pipeline's SRC_ALPHA blend then
// applies the coverage exactly once (do NOT premultiply here or it'd be squared).
float4 tex = pParams.textures[style.textureIndex].Sample(pParams.sampler, texCoords.xy);
// The UV rect (style.uvOffset/uvSize) selects the glyph inside the atlas; plain
// textured UI quads use the full-rect default (0,0,1,1).
float2 uv = style.uvOffset + texCoords * style.uvSize;
float4 tex = pParams.textures[style.textureIndex].Sample(pParams.sampler, uv);
float coverage = tex.r;
return float4(style.color, coverage * style.opacity);
}