iced-yoda/wgpu/src/shader/image.wgsl

47 lines
1.3 KiB
WebGPU Shading Language
Raw Normal View History

2021-04-11 18:55:57 -07:00
struct Globals {
2022-07-02 22:29:49 +08:00
transform: mat4x4<f32>,
2021-04-11 18:55:57 -07:00
};
2022-07-02 15:39:42 +08:00
@group(0) @binding(0) var<uniform> globals: Globals;
@group(0) @binding(1) var u_sampler: sampler;
@group(1) @binding(0) var u_texture: texture_2d_array<f32>;
2021-04-11 18:55:57 -07:00
struct VertexInput {
2022-07-02 15:39:42 +08:00
@location(0) v_pos: vec2<f32>,
@location(1) pos: vec2<f32>,
@location(2) scale: vec2<f32>,
@location(3) atlas_pos: vec2<f32>,
@location(4) atlas_scale: vec2<f32>,
@location(5) layer: i32,
2021-04-11 18:55:57 -07:00
};
struct VertexOutput {
2022-07-02 15:39:42 +08:00
@builtin(position) position: vec4<f32>,
@location(0) uv: vec2<f32>,
@location(1) layer: f32, // this should be an i32, but naga currently reads that as requiring interpolation.
2021-04-11 18:55:57 -07:00
};
2022-07-02 15:39:42 +08:00
@vertex
2021-04-11 18:55:57 -07:00
fn vs_main(input: VertexInput) -> VertexOutput {
var out: VertexOutput;
2021-04-12 23:07:58 -07:00
out.uv = vec2<f32>(input.v_pos * input.atlas_scale + input.atlas_pos);
out.layer = f32(input.layer);
2021-04-11 18:55:57 -07:00
var transform: mat4x4<f32> = mat4x4<f32>(
vec4<f32>(input.scale.x, 0.0, 0.0, 0.0),
2021-04-12 23:07:58 -07:00
vec4<f32>(0.0, input.scale.y, 0.0, 0.0),
2021-04-11 18:55:57 -07:00
vec4<f32>(0.0, 0.0, 1.0, 0.0),
vec4<f32>(input.pos, 0.0, 1.0)
);
out.position = globals.transform * transform * vec4<f32>(input.v_pos, 0.0, 1.0);
return out;
}
2022-07-02 15:39:42 +08:00
@fragment
fn fs_main(input: VertexOutput) -> @location(0) vec4<f32> {
2021-04-12 23:07:58 -07:00
return textureSample(u_texture, u_sampler, input.uv, i32(input.layer));
2021-04-11 18:55:57 -07:00
}