// What yakui painted, put on the frame.
//
// yakui hands over vertices with positions already in its own frame space --
// (0, 0) top-left to (1, 1) bottom-right -- and colours in linear light. Two
// fragment paths: `fs_main` for geometry and images, whose texture is a
// colour (a flat quad samples a white texel); `fs_text` for lettering, whose
// texture is coverage in one channel. Both write premultiplied alpha, which
// is what the pipelines blend with.
struct VertexInput {
@location(0) position: vec2<f32>,
@location(1) texcoord: vec2<f32>,
@location(2) color: vec4<f32>,
};
struct VertexOutput {
@builtin(position) position: vec4<f32>,
@location(0) texcoord: vec2<f32>,
@location(1) color: vec4<f32>,
};
@group(0) @binding(0) var color_texture: texture_2d<f32>;
@group(0) @binding(1) var color_sampler: sampler;
@vertex
fn vs_main(in: VertexInput) -> VertexOutput {
var out: VertexOutput;
// yakui's (0, 0) top-left to (1, 1) bottom-right becomes clip space's
// (-1, 1) top-left to (1, -1) bottom-right.
let adjusted = in.position * vec2<f32>(2.0, -2.0) + vec2<f32>(-1.0, 1.0);
out.position = vec4<f32>(adjusted, 0.0, 1.0);
out.texcoord = in.texcoord;
out.color = in.color;
return out;
}
@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
// The texture is premultiplied on upload; the vertex colour is not, so
// its alpha is applied here.
var color = textureSample(color_texture, color_sampler, in.texcoord);
color *= in.color.a;
return in.color * color;
}
@fragment
fn fs_text(in: VertexOutput) -> @location(0) vec4<f32> {
let coverage = textureSample(color_texture, color_sampler, in.texcoord).r;
let alpha = coverage * in.color.a;
return vec4<f32>(in.color.rgb * alpha, alpha);
}