79 lines
1.8 KiB
WebGPU Shading Language
79 lines
1.8 KiB
WebGPU Shading Language
// Vertex shader
|
|
|
|
struct VertexOutput {
|
|
@builtin(position) clip_position: vec4<f32>,
|
|
@location(0) tex_coords: vec2<f32>,
|
|
};
|
|
|
|
@vertex
|
|
fn vs_main(
|
|
@builtin(vertex_index) in_vertex_index: u32,
|
|
) -> VertexOutput {
|
|
var out: VertexOutput;
|
|
var x: f32;
|
|
var y: f32;
|
|
switch in_vertex_index {
|
|
case 0u, 3u: {
|
|
x = -1.0;
|
|
y = 1.0;
|
|
}
|
|
case 1u: {
|
|
x = -1.0;
|
|
y = -1.0;
|
|
}
|
|
case 2u, 4u: {
|
|
x = 1.0;
|
|
y = -1.0;
|
|
}
|
|
default: {
|
|
x = 1.0;
|
|
y = 1.0;
|
|
}
|
|
}
|
|
out.clip_position = vec4<f32>(x, y, 0.0, 1.0);
|
|
out.tex_coords = vec2<f32>((x + 1.0) / 2.0, (1.0 - y) / 2.0);
|
|
return out;
|
|
}
|
|
|
|
// Fragment shader
|
|
@group(0) @binding(0)
|
|
var u_texture: texture_2d<f32>;
|
|
@group(0) @binding(1)
|
|
var u_sampler: sampler;
|
|
|
|
struct Colors {
|
|
left: vec4<f32>,
|
|
right: vec4<f32>,
|
|
};
|
|
@group(0) @binding(2)
|
|
var<uniform> colors: Colors;
|
|
|
|
@fragment
|
|
fn fs_lefteye(in: VertexOutput) -> @location(0) vec4<f32> {
|
|
let brt = textureSample(u_texture, u_sampler, in.tex_coords);
|
|
return colors.left * brt[0];
|
|
}
|
|
|
|
@fragment
|
|
fn fs_righteye(in: VertexOutput) -> @location(0) vec4<f32> {
|
|
let brt = textureSample(u_texture, u_sampler, in.tex_coords);
|
|
return colors.left * brt[1];
|
|
}
|
|
|
|
@fragment
|
|
fn fs_anaglyph(in: VertexOutput) -> @location(0) vec4<f32> {
|
|
let brt = textureSample(u_texture, u_sampler, in.tex_coords);
|
|
return colors.left * brt[0] + colors.right * brt[1];
|
|
}
|
|
|
|
@fragment
|
|
fn fs_sidebyside(in: VertexOutput) -> @location(0) vec4<f32> {
|
|
var point = in.tex_coords;
|
|
point.x = (point.x * 2.0) % 1.0;
|
|
let brt = textureSample(u_texture, u_sampler, point);
|
|
if in.tex_coords.x < 0.5 {
|
|
return colors.left * brt[0];
|
|
} else {
|
|
return colors.left * brt[1];
|
|
};
|
|
} |