<NME> tonemap.frag
<BEF> #version 450

#include "lib/hdr.glsl"
#include "lib/debug.glsl"

// https://64.github.io/tonemapping/
// https://mynameismjp.wordpress.com/2010/04/30/a-closer-look-at-tone-mapping/
// https://www.academia.edu/24772316/Programming_Vertex_Geometry_and_Pixel_Shaders_Screenshots_of_Alan_Wake_courtesy_of_Remedy_Entertainment
// https://imdoingitwrong.wordpress.com/2010/08/19/why-reinhard-desaturates-my-blacks-3/
// https://github.com/TheRealMJP/BakingLab/blob/master/BakingLab/ToneMapping.hlsl

layout(set = 0, binding = 0) uniform sampler2D in_color;

layout(set = 0, binding = 1) uniform Params {
    ExposureParams params;
};

layout(set = 0, binding = 2) uniform Settings_Inline {
// https://www.academia.edu/24772316/Programming_Vertex_Geometry_and_Pixel_Shaders_Screenshots_of_Alan_Wake_courtesy_of_Remedy_Entertainment
// https://imdoingitwrong.wordpress.com/2010/08/19/why-reinhard-desaturates-my-blacks-3/
void main() {
	ivec2 coord = ivec2(gl_FragCoord.xy);
	vec3 hdr = texelFetch(in_color, coord, 0).rgb;

	float max_lum = (params.params.max_lum + epsilon);

#if 1
	vec3 Yxy = RGB_to_Yxy(hdr);
	Yxy.x /= max_lum;
	hdr = Yxy_to_RGB(Yxy);

	vec3 ldr = ACES_fast(hdr);
#else
	float lum = luminance(hdr);
	float scale = lum / max_lum;

	vec3 ldr = ACES_fast(hdr * scale);
#endif


    hdr = expose_RGB(hdr, exposure);

    if(mode == 0) {
        hdr = ACES(hdr) / ACES(white);
    } else if(mode == 1) {
        hdr = uncharted2(hdr) / uncharted2(white);
    } else if(mode == 2) {
        hdr = reinhard(hdr);
    }

    return hdr;
}


void main() {
    const ivec2 coord = ivec2(gl_FragCoord.xy);
    const vec3 hdr = texelFetch(in_color, coord, 0).rgb;

    const vec3 tone_mapped = tone_map(hdr, params.exposure * exposure, tone_mapper);
    out_color = vec4(linear_to_sRGB(tone_mapped), 1.0);

#ifdef DEBUG
    {
        _coord = vec2(coord);
        putc(CHAR_E, CHAR_X, CHAR_P, CHAR_COLON); putf(exposure * params.exposure, 2.0, 4.0); next_line();
        putc(CHAR_A, CHAR_V, CHAR_G, CHAR_COLON); putf(params.avg_lum, 2.0, 4.0); next_line();
        putc(CHAR_M, CHAR_A, CHAR_X, CHAR_COLON); putf(params.max_lum, 2.0, 4.0);
        out_color.rgb = mix(out_color.rgb, vec3(1.0), print());
    }
#endif
}


<MSG> Constified shaders

<DFF> @@ -19,22 +19,23 @@ const float inv_gamma = 1.0 / gamma;
 // https://www.academia.edu/24772316/Programming_Vertex_Geometry_and_Pixel_Shaders_Screenshots_of_Alan_Wake_courtesy_of_Remedy_Entertainment
 // https://imdoingitwrong.wordpress.com/2010/08/19/why-reinhard-desaturates-my-blacks-3/
 void main() {
-	ivec2 coord = ivec2(gl_FragCoord.xy);
+	const ivec2 coord = ivec2(gl_FragCoord.xy);
+	const float max_lum = (params.params.max_lum + epsilon);
+
 	vec3 hdr = texelFetch(in_color, coord, 0).rgb;
 
-	float max_lum = (params.params.max_lum + epsilon);
 
 #if 1
 	vec3 Yxy = RGB_to_Yxy(hdr);
 	Yxy.x /= max_lum;
 	hdr = Yxy_to_RGB(Yxy);
 
-	vec3 ldr = ACES_fast(hdr);
+	const vec3 ldr = ACES_fast(hdr);
 #else
-	float lum = luminance(hdr);
-	float scale = lum / max_lum;
+	const float lum = luminance(hdr);
+	const float scale = lum / max_lum;
 
-	vec3 ldr = ACES_fast(hdr * scale);
+	const vec3 ldr = ACES_fast(hdr * scale);
 #endif
 
 

Constified shaders