1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
struct VertexOutput {
@builtin(position) clip_position: vec4<f32>,
@location(0) uv: vec2<f32>,
}
@group(0) @binding(0)
var s_mask: sampler;
@group(0) @binding(1)
var t_mask: texture_2d<f32>;
@vertex
fn vs_main(
@builtin(vertex_index) in_vertex_index: u32,
) -> VertexOutput {
var out: VertexOutput;
// draw fullscreen triangle
let x = f32((in_vertex_index << 1u) & 2u);
let y = f32(in_vertex_index & 2u);
out.clip_position = vec4<f32>(x * 2.0 - 1.0, y * 2.0 - 1.0, 0.0, 1.0);
out.uv = vec2<f32>(x, 1.0-y);
return out;
}
@fragment
fn fs_mask(in: VertexOutput) {
let sample = textureSample(t_mask, s_mask, in.uv);
// We invert this check so that the mask will render objects in
// the center
if (sample.a > 0.1) {
discard;
}
}
@fragment
fn fs_colour(in: VertexOutput) -> @location(0) vec4<f32> {
let sample = textureSample(t_mask, s_mask, in.uv);
return sample;
}