// Perspective-correct quad blit. Each vertex carries (u/w, v/w, 1/w);
// linear interpolation of those three quantities is the textbook
// projective interpolation pattern, and the fragment shader recovers
// (u, v) by dividing xy / z.
struct Viewport {
size: vec2<f32>,
_pad: vec2<f32>,
};
@group(0) @binding(0) var<uniform> view: Viewport;
@group(0) @binding(1) var src_tex: texture_2d<f32>;
@group(0) @binding(2) var src_samp: sampler;
struct VertexIn {
@location(0) pos: vec2<f32>,
@location(1) uvw: vec3<f32>,
@location(2) params: vec4<f32>,
};
struct VertexOut {
@builtin(position) clip: vec4<f32>,
// `linear` opts out of clip-space perspective division; the host
// already encoded the homography weight into `uvw`.
@location(0) @interpolate(linear) uvw: vec3<f32>,
@location(1) @interpolate(flat) params: vec4<f32>,
};
@vertex
fn vs_main(in: VertexIn) -> VertexOut {
let ndc = vec2<f32>(
(in.pos.x / view.size.x) * 2.0 - 1.0,
1.0 - (in.pos.y / view.size.y) * 2.0,
);
var out: VertexOut;
out.clip = vec4<f32>(ndc, 0.0, 1.0);
out.uvw = in.uvw;
out.params = in.params;
return out;
}
@fragment
fn fs_main(v: VertexOut) -> @location(0) vec4<f32> {
let uv = v.uvw.xy / v.uvw.z;
let c = textureSample(src_tex, src_samp, uv);
var coverage = 1.0;
if (v.params.y > 0.0) {
let size = v.params.zw;
let half = size * 0.5;
let radius = min(v.params.y, min(half.x, half.y));
let q = abs(uv * size - half) - (half - vec2<f32>(radius));
let distance = length(max(q, vec2<f32>(0.0))) + min(max(q.x, q.y), 0.0) - radius;
coverage = clamp(0.5 - distance / max(fwidth(distance), 0.001), 0.0, 1.0);
}
let alpha = c.a * v.params.x * coverage;
return vec4<f32>(c.rgb * alpha, alpha);
}