Krolyxon
Commit dbc22bf

Branches

Tags

Refs →

remagesrcrendershadersthumbs.wgsl

43 lines · 1.2 KB · WGSL

1// Instanced thumbnail quads sampled from atlas pages. Output is straight alpha.
2
3struct Globals {
4 viewport: vec2<f32>,
5 _pad: vec2<f32>,
6};
7
8@group(0) @binding(0) var<uniform> g: Globals;
9@group(0) @binding(1) var samp: sampler;
10@group(1) @binding(0) var page: texture_2d<f32>;
11
12struct Instance {
13 @location(0) rect: vec4<f32>, // x, y, w, h in physical px
14 @location(1) uv: vec4<f32>, // u, v, du, dv in the page
15 @location(2) params: vec4<f32>, // x: opacity
16};
17
18struct VOut {
19 @builtin(position) pos: vec4<f32>,
20 @location(0) uv: vec2<f32>,
21 @location(1) alpha: f32,
22};
23
24@vertex
25fn vs(@builtin(vertex_index) i: u32, inst: Instance) -> VOut {
26 var corners = array<vec2<f32>, 6>(
27 vec2(0.0, 0.0), vec2(1.0, 0.0), vec2(0.0, 1.0),
28 vec2(0.0, 1.0), vec2(1.0, 0.0), vec2(1.0, 1.0),
29 );
30 let c = corners[i];
31 let p = inst.rect.xy + c * inst.rect.zw;
32 var out: VOut;
33 out.pos = vec4(p.x / g.viewport.x * 2.0 - 1.0, 1.0 - p.y / g.viewport.y * 2.0, 0.0, 1.0);
34 out.uv = inst.uv.xy + c * inst.uv.zw;
35 out.alpha = inst.params.x;
36 return out;
37}
38
39@fragment
40fn fs(in: VOut) -> @location(0) vec4<f32> {
41 let c = textureSample(page, samp, in.uv);
42 return vec4(c.rgb, c.a * in.alpha);
43}