struct VertexOutput {
@builtin(position) clip_position: vec4<f32>,
@location(0) tex_coords: vec2<f32>,
};
struct Uniforms {
rect: vec4<f32>,
is_bgra: u32,
opacity: f32,
};
@group(0) @binding(2)
var<uniform> uniforms: Uniforms;
@vertex
fn vs_main(@builtin(vertex_index) in_vertex_index: u32) -> VertexOutput {
let vertices = array<vec2<f32>, 6>(
vec2<f32>(-1.0, -1.0),
vec2<f32>(1.0, -1.0),
vec2<f32>(1.0, 1.0),
vec2<f32>(-1.0, -1.0),
vec2<f32>(1.0, 1.0),
vec2<f32>(-1.0, 1.0),
);
let tex_coords = array<vec2<f32>, 6>(
vec2<f32>(0.0, 1.0),
vec2<f32>(1.0, 1.0),
vec2<f32>(1.0, 0.0),
vec2<f32>(0.0, 1.0),
vec2<f32>(1.0, 0.0),
vec2<f32>(0.0, 0.0),
);
var out: VertexOutput;
let pos = vertices[in_vertex_index] * uniforms.rect.zw + uniforms.rect.xy;
out.clip_position = vec4<f32>(pos, 0.0, 1.0);
out.tex_coords = tex_coords[in_vertex_index];
return out;
}
@group(0) @binding(0)
var t_diffuse: texture_2d<f32>;
@group(0) @binding(1)
var s_diffuse: sampler;
@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
var color = textureSample(t_diffuse, s_diffuse, in.tex_coords);
if uniforms.is_bgra == 1u {
color = color.bgra;
}
let alpha = color.a * uniforms.opacity;
let rgb = color.rgb * uniforms.opacity;
return vec4<f32>(rgb, alpha);
}