render.wgsl42 lines · 1.0 KB · raw
1override encode_to_srgb: bool = false;
2
3struct VertexInput {
4    @location(0) position: vec2f,
5    @location(1) texture_coords: vec2f,
6}
7
8struct VertexOutput {
9    @builtin(position) position: vec4f,
10    @location(0) @interpolate(perspective, sample) texture_coords: vec2f,
11}
12
13@group(0) @binding(0)
14var texture_in: texture_2d<f32>;
15@group(0) @binding(1)
16var sampler_in: sampler;
17
18@vertex
19fn vs_main(input: VertexInput) -> VertexOutput {
20    var out: VertexOutput;
21
22    out.position = vec4f(input.position, 0.0, 1.0);
23    out.texture_coords = input.texture_coords;
24    return out;
25}
26
27@fragment
28fn fs_main(input: VertexOutput) -> @location(0) vec4f {
29    var pixel = textureSample(texture_in, sampler_in, input.texture_coords);
30
31    if encode_to_srgb {
32        // https://en.wikipedia.org/wiki/SRGB#Transfer_function_(%22gamma%22)
33        let srgb = select(
34            1.055 * pow(pixel.rgb, vec3f(1.0 / 2.4)) - 0.055,
35            12.92 * pixel.rgb,
36            pixel.rgb <= vec3f(0.0031308),
37        );
38        pixel = vec4f(srgb, pixel.a);
39    }
40
41    return pixel;
42}