// Shared mask shader: ShapeMask (mode=0), LumaMask (mode=1), AlphaMatte (mode=2).
struct VertexOutput {
@builtin(position) position: vec4<f32>,
@location(0) uv: vec2<f32>,
}
@vertex
fn vs_main(@builtin(vertex_index) vertex_idx: u32) -> VertexOutput {
var positions = array<vec2<f32>, 6>(
vec2<f32>(-1.0, 1.0), vec2<f32>( 1.0, 1.0), vec2<f32>(-1.0, -1.0),
vec2<f32>(-1.0, -1.0), vec2<f32>( 1.0, 1.0), vec2<f32>( 1.0, -1.0),
);
var uvs = array<vec2<f32>, 6>(
vec2<f32>(0.0, 0.0), vec2<f32>(1.0, 0.0), vec2<f32>(0.0, 1.0),
vec2<f32>(0.0, 1.0), vec2<f32>(1.0, 0.0), vec2<f32>(1.0, 1.0),
);
var out: VertexOutput;
out.position = vec4<f32>(positions[vertex_idx], 0.0, 1.0);
out.uv = uvs[vertex_idx];
return out;
}
// ── Bindings ──────────────────────────────────────────────────────────────────
@group(0) @binding(0) var tex_base: texture_2d<f32>; // base / foreground
@group(0) @binding(1) var tex_mask: texture_2d<f32>; // mask / background
@group(0) @binding(2) var tex_sampler: sampler;
@group(0) @binding(3) var<uniform> u: MaskUniforms;
struct MaskUniforms {
// 0 = ShapeMask, 1 = LumaMask, 2 = AlphaMatte
mode: u32,
_pad0: u32,
_pad1: u32,
_pad2: u32,
}
// ── Fragment ──────────────────────────────────────────────────────────────────
@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
let base = textureSample(tex_base, tex_sampler, in.uv);
let mask = textureSample(tex_mask, tex_sampler, in.uv);
var out: vec4<f32>;
switch u.mode {
// ShapeMask: apply mask's alpha as a hard threshold.
case 0u {
let alpha = select(0.0, 1.0, mask.a > 0.004);
out = vec4<f32>(base.rgb, base.a * alpha);
}
// LumaMask: use mask's BT.709 luma as opacity.
case 1u {
let luma = dot(mask.rgb, vec3<f32>(0.2126, 0.7152, 0.0722));
out = vec4<f32>(base.rgb, base.a * luma);
}
// AlphaMatte: Porter-Duff src-over (base = foreground, mask = background).
case 2u {
let alpha = base.a;
let out_rgb = base.rgb * alpha + mask.rgb * (1.0 - alpha);
let out_a = alpha + mask.a * (1.0 - alpha);
out = vec4<f32>(out_rgb, out_a);
}
default {
out = base;
}
}
return out;
}