-
Notifications
You must be signed in to change notification settings - Fork 0
/
tonemapping.frag.glsl
65 lines (50 loc) · 1.73 KB
/
tonemapping.frag.glsl
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
63
64
65
#version 300 es
precision highp float;
precision highp int;
precision highp usampler2D;
uniform sampler2D u_source;
uniform float u_gamma;
uniform float u_exposure;
uniform float u_whitePoint;
uniform float u_acesC;
uniform float u_acesS;
uniform int u_tonemappingMode;
uniform float u_ditherStrength;
in vec2 v_position; // TexCoords
layout(location = 0) out vec4 outColor;
@import ./_utils;
@import ./_dither;
@import ./_tonemappers;
@import ./_colorGrading;
const int TONEMAP_LINEAR = 0;
const int TONEMAP_REINHARD = 1;
const int TONEMAP_U2 = 2;
const int TONEMAP_PHOTOGRAPHIC = 3;
const int TONEMAP_ACES = 4;
vec3 doTonemapping(int tonemapMode, vec3 hdrColor) {
switch (tonemapMode) {
case TONEMAP_U2: return Uncharted2Tonemap(hdrColor);
case TONEMAP_LINEAR: return tonemapLinear(hdrColor);
case TONEMAP_PHOTOGRAPHIC: return tonemapPhotographic(hdrColor);
case TONEMAP_REINHARD: return tonemapReinhard(hdrColor);
default:
case TONEMAP_ACES: return tonemapACES(hdrColor);
}
}
void main() {
vec2 pixelTS = to_0_1(v_position);
vec3 colorHDR = texture(u_source, pixelTS).rgb;
// do dithering to break up banding
colorHDR = doDither(colorHDR, u_ditherStrength);
// color grade raw HDR
// In old days we used LUTs for this, but LUTs require conversion to LDR.
// Since HDR displays are now available, we do color grading in HDR,
// skipping LDR conversion. This, and also cause we can.
vec3 colorAfterColorGrading = colorCorrectAll(colorHDR);
outColor.rgb = saturate(
doTonemapping(u_tonemappingMode, colorAfterColorGrading)
);
float luma = toLuma_fromLinear(outColor.rgb);
// just SOME gamma, does not matter exact. We need to convert into SOME perceptual space
outColor.a = doGamma(luma, u_gamma);
}