@group(0)
@binding(0)
var hdr_image: texture_2d<f32>;
@group(0)
@binding(1)
var hdr_sampler: sampler;
var<push_constant> tonemapping_mode: u32;
// Taken from learn-wgpu example for hdr.
struct VertexOutput {
@location(0) uv: vec2<f32>,
@builtin(position) clip_position: vec4<f32>,
};
@fragment
fn fs_main(vs: VertexOutput) -> @location(0) vec4<f32> {
let color_with_a: vec4<f32> = textureSample(hdr_image, hdr_sampler, vs.uv);
var color: vec3<f32> = color_with_a.rgb;
if tonemapping_mode == 1u{
color = aces_tone_map(color);
}
return vec4(color, color_with_a.a);
}
// Maps HDR values to linear values
// Based on http://www.oscars.org/science-technology/sci-tech-projects/aces
fn aces_tone_map(hdr: vec3<f32>) -> vec3<f32> {
let m1 = mat3x3(
0.59719, 0.07600, 0.02840,
0.35458, 0.90834, 0.13383,
0.04823, 0.01566, 0.83777,
);
let m2 = mat3x3(
1.60475, -0.10208, -0.00327,
-0.53108, 1.10813, -0.07276,
-0.07367, -0.00605, 1.07602,
);
let v = m1 * hdr;
let a = v * (v + 0.0245786) - 0.000090537;
let b = v * (0.983729 * v + 0.4329510) + 0.238081;
return clamp(m2 * (a / b), vec3(0.0), vec3(1.0));
}