aboutsummaryrefslogtreecommitdiffstatsci
path: root/shaders/tone_mapping.frag
blob: 5c4d752354e847408771e87f1470611ec4e51836 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#version 450
#extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_420pack : enable

layout (set = 0, binding = 0) uniform UBO {
    mat4 projection;
    mat4 model;
    mat4 view;
    vec4 camPos;    
    float exposure;
    float gamma;    
} ubo;

layout (input_attachment_index = 0, set = 1, binding = 4) uniform subpassInputMS samplerHDR;
layout (set = 0, binding = 8) uniform sampler2D samplerUI;

layout (location = 0) in vec2 inUV;
layout (location = 0) out vec4 outColor;

vec3 Uncharted2Tonemap(vec3 color)
{
    const float A = 0.15;
    const float B = 0.50;
    const float C = 0.10;
    const float D = 0.20;
    const float E = 0.02;
    const float F = 0.30;
    const float W = 11.2;
    return ((color*(A*color+C*B)+D*E)/(color*(A*color+B)+D*F))-E/F;
}

vec3 tonemap(vec3 color)
{
    vec3 outcol = Uncharted2Tonemap(color.rgb * ubo.exposure);
    outcol = outcol * (1.0f / Uncharted2Tonemap(vec3(11.2f)));  
    return pow(outcol, vec3(1.0f / ubo.gamma));
}

vec3 SRGBtoLINEAR(vec3 srgbIn)
{
    #ifdef MANUAL_SRGB
    #ifdef SRGB_FAST_APPROXIMATION
    return pow(srgbIn.xyz,vec3(2.2));
    #else //SRGB_FAST_APPROXIMATION
    vec3 bLess = step(vec3(0.04045),srgbIn.xyz);
    return linOut = mix( srgbIn.xyz/vec3(12.92), pow((srgbIn.xyz+vec3(0.055))/vec3(1.055),vec3(2.4)), bLess );
    #endif //SRGB_FAST_APPROXIMATION    
    #else //MANUAL_SRGB
    return srgbIn;
    #endif //MANUAL_SRGB
}

void main() 
{    
    vec4 hdrColor = subpassLoad(samplerHDR, gl_SampleID);
    vec4 ui = texture(samplerUI, inUV);
    outColor = (hdrColor.a - ui.a) * vec4(SRGBtoLINEAR(tonemap(hdrColor.rgb)), 1.0) + ui;    
    /*
    vec3 mapped = vec3(1.0) - exp(-hdrColor.rgb * ubo.exposure);        
    mapped = pow(mapped, vec3(1.0 / ubo.gamma));
    outColor = vec4(mapped, hdrColor.a);*/
}