2022-05-12 07:02:28 +00:00
|
|
|
// Vertex shader bindings
|
|
|
|
|
|
|
|
struct VertexOutput {
|
2022-07-03 13:43:39 +00:00
|
|
|
@location(0) tex_coord: vec2<f32>,
|
2022-09-24 15:53:11 +00:00
|
|
|
@location(1) color: vec4<f32>, // gamma 0-1
|
2022-07-03 13:43:39 +00:00
|
|
|
@builtin(position) position: vec4<f32>,
|
2022-05-12 07:02:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Locals {
|
2022-07-03 13:43:39 +00:00
|
|
|
screen_size: vec2<f32>,
|
|
|
|
// Uniform buffers need to be at least 16 bytes in WebGL.
|
|
|
|
// See https://github.com/gfx-rs/wgpu/issues/2072
|
|
|
|
_padding: vec2<u32>,
|
2022-05-12 07:02:28 +00:00
|
|
|
};
|
2022-07-03 13:43:39 +00:00
|
|
|
@group(0) @binding(0) var<uniform> r_locals: Locals;
|
2022-05-12 07:02:28 +00:00
|
|
|
|
2022-09-24 15:53:11 +00:00
|
|
|
// 0-1 linear from 0-1 sRGB gamma
|
|
|
|
fn linear_from_gamma_rgb(srgb: vec3<f32>) -> vec3<f32> {
|
|
|
|
let cutoff = srgb < vec3<f32>(0.04045);
|
|
|
|
let lower = srgb / vec3<f32>(12.92);
|
|
|
|
let higher = pow((srgb + vec3<f32>(0.055)) / vec3<f32>(1.055), vec3<f32>(2.4));
|
2022-05-12 07:02:28 +00:00
|
|
|
return select(higher, lower, cutoff);
|
|
|
|
}
|
|
|
|
|
2022-09-24 15:53:11 +00:00
|
|
|
// 0-1 sRGB gamma from 0-1 linear
|
|
|
|
fn gamma_from_linear_rgb(rgb: vec3<f32>) -> vec3<f32> {
|
|
|
|
let cutoff = rgb < vec3<f32>(0.0031308);
|
|
|
|
let lower = rgb * vec3<f32>(12.92);
|
|
|
|
let higher = vec3<f32>(1.055) * pow(rgb, vec3<f32>(1.0 / 2.4)) - vec3<f32>(0.055);
|
|
|
|
return select(higher, lower, cutoff);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 0-1 sRGBA gamma from 0-1 linear
|
|
|
|
fn gamma_from_linear_rgba(linear_rgba: vec4<f32>) -> vec4<f32> {
|
|
|
|
return vec4<f32>(gamma_from_linear_rgb(linear_rgba.rgb), linear_rgba.a);
|
|
|
|
}
|
|
|
|
|
|
|
|
// [u8; 4] SRGB as u32 -> [r, g, b, a] in 0.-1
|
2022-06-11 11:52:06 +00:00
|
|
|
fn unpack_color(color: u32) -> vec4<f32> {
|
|
|
|
return vec4<f32>(
|
|
|
|
f32(color & 255u),
|
|
|
|
f32((color >> 8u) & 255u),
|
|
|
|
f32((color >> 16u) & 255u),
|
|
|
|
f32((color >> 24u) & 255u),
|
2022-09-24 15:53:11 +00:00
|
|
|
) / 255.0;
|
2022-06-11 11:52:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn position_from_screen(screen_pos: vec2<f32>) -> vec4<f32> {
|
|
|
|
return vec4<f32>(
|
|
|
|
2.0 * screen_pos.x / r_locals.screen_size.x - 1.0,
|
|
|
|
1.0 - 2.0 * screen_pos.y / r_locals.screen_size.y,
|
|
|
|
0.0,
|
|
|
|
1.0,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-07-03 13:43:39 +00:00
|
|
|
@vertex
|
2022-05-12 07:02:28 +00:00
|
|
|
fn vs_main(
|
2022-07-03 13:43:39 +00:00
|
|
|
@location(0) a_pos: vec2<f32>,
|
|
|
|
@location(1) a_tex_coord: vec2<f32>,
|
|
|
|
@location(2) a_color: u32,
|
2022-05-12 07:02:28 +00:00
|
|
|
) -> VertexOutput {
|
|
|
|
var out: VertexOutput;
|
|
|
|
out.tex_coord = a_tex_coord;
|
2022-09-24 15:53:11 +00:00
|
|
|
out.color = unpack_color(a_color);
|
2022-06-11 11:52:06 +00:00
|
|
|
out.position = position_from_screen(a_pos);
|
2022-05-12 07:02:28 +00:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fragment shader bindings
|
|
|
|
|
2022-07-03 13:43:39 +00:00
|
|
|
@group(1) @binding(0) var r_tex_color: texture_2d<f32>;
|
|
|
|
@group(1) @binding(1) var r_tex_sampler: sampler;
|
2022-05-12 07:02:28 +00:00
|
|
|
|
2022-07-03 13:43:39 +00:00
|
|
|
@fragment
|
2022-09-24 15:53:11 +00:00
|
|
|
fn fs_main_linear_framebuffer(in: VertexOutput) -> @location(0) vec4<f32> {
|
|
|
|
// We always have an sRGB aware texture at the moment.
|
|
|
|
let tex_linear = textureSample(r_tex_color, r_tex_sampler, in.tex_coord);
|
|
|
|
let tex_gamma = gamma_from_linear_rgba(tex_linear);
|
|
|
|
let out_color_gamma = in.color * tex_gamma;
|
|
|
|
return vec4<f32>(linear_from_gamma_rgb(out_color_gamma.rgb), out_color_gamma.a);
|
|
|
|
}
|
|
|
|
|
|
|
|
@fragment
|
|
|
|
fn fs_main_gamma_framebuffer(in: VertexOutput) -> @location(0) vec4<f32> {
|
|
|
|
// We always have an sRGB aware texture at the moment.
|
|
|
|
let tex_linear = textureSample(r_tex_color, r_tex_sampler, in.tex_coord);
|
|
|
|
let tex_gamma = gamma_from_linear_rgba(tex_linear);
|
|
|
|
let out_color_gamma = in.color * tex_gamma;
|
|
|
|
return out_color_gamma;
|
2022-05-12 07:02:28 +00:00
|
|
|
}
|