klyff 0.1.3

Text rendering library for games with MSDF support
Documentation
struct VertexInput {
    @location(0) uvw: vec3f,
    @location(1) screen_pos: vec2f,
}

struct VertexOutput {
    @builtin(position) clip_position: vec4f,
    @location(0) uvw: vec3f,
}

struct Uniforms {
    /// Viewport dimensions in pixels, used to convert pixel coords to NDC.
    screen_size: vec2<f32>,
}

@group(0) @binding(0) var atlas: texture_2d_array<f32>;
@group(0) @binding(1) var atlas_sampler: sampler;
@group(1) @binding(0) var<uniform> uniforms: Uniforms;

@vertex
fn vs_main(in: VertexInput) -> VertexOutput {
    var out: VertexOutput;
    let ndc = (in.screen_pos / uniforms.screen_size) * 2.0 - 1.0;
    out.clip_position = vec4<f32>(ndc.x, -ndc.y, 0.0, 1.0);
    out.uvw = in.uvw;
    return out;
}

@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
    let layer = i32(in.uvw.z);
    let sampled = textureSample(atlas, atlas_sampler, in.uvw.xy, layer);
    // Glyph bitmaps in the atlas (Rgba8Unorm) are sRGB-encoded color values.
    // The render target is sRGB and re-encodes on store, so we must decode
    // to linear here. Gamma 2.2 matches the convention in `Color::from_srgb8`.
    let linear_rgb = pow(sampled.rgb, vec3<f32>(2.2));
    return vec4<f32>(linear_rgb, sampled.a);
}