summaryrefslogtreecommitdiffstats
path: root/assets/shaders/canvas.wgsl
blob: 8e8103eba9ca151e5795d30ae3d36482584a7e44 (plain) (blame)
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
40
41
42
43
44
45
46
47
48
49
50
51
struct VertexOutput {
    @builtin(position) clip_position: vec4<f32>,
    @location(0) uv: vec2<f32>
}

const QUAD_VERTICES = array<vec2<f32>,4>(
    vec2<f32>(0.0, 0.0),
    vec2<f32>(2.0, 0.0),
    vec2<f32>(0.0, 2.0),
    vec2<f32>(2.0, 2.0),
);

const QUAD_UVS = array<vec2<f32>,4>(
    vec2<f32>(0.0, 1.0),
    vec2<f32>(1.0, 1.0),
    vec2<f32>(0.0, 0.0),
    vec2<f32>(1.0, 0.0),
);

@group(0) @binding(0)
var canvas_texture: texture_2d<f32>;
@group(0) @binding(1)
var canvas_sampler: sampler;

@group(1) @binding(0)
var<uniform> rect: vec4<f32>;

@vertex
fn vs_main(
    @builtin(vertex_index) index: u32,
) -> VertexOutput {
    var out: VertexOutput;

    let model = QUAD_VERTICES[index];

    out.uv = QUAD_UVS[index];
    out.clip_position = vec4<f32>(
        -1.0 + (model.x * rect.z) + (2.0 * rect.x),
        -1.0 + (model.y * rect.w), 
        0.0, 
        1.0);

    return out;
}

@fragment
fn fs_main(in: VertexOutput)
-> @location(0) vec4<f32> 
{
    return textureSample(canvas_texture, canvas_sampler, in.uv);
}