-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathcameraEffects_fp.glsl
More file actions
145 lines (114 loc) · 4.13 KB
/
cameraEffects_fp.glsl
File metadata and controls
145 lines (114 loc) · 4.13 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
===========================================================================
Copyright (C) 2009-2010 Robert Beckebans <trebor_7@users.sourceforge.net>
This file is part of XreaL source code.
XreaL source code is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.
XreaL source code is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with XreaL source code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
/* cameraEffects_fp.glsl */
#define CAMERAEFFECTS_GLSL
uniform sampler2D u_CurrentMap;
#if defined(r_colorGrading)
uniform sampler3D u_ColorMap3D;
#endif
uniform vec4 u_ColorModulate;
uniform float u_GlobalLightFactor; // 1 / tr.identityLight
uniform float u_InverseGamma;
uniform bool u_SRGB;
void convertToSRGB(inout vec3 color) {
#if defined(r_accurateSRGB)
float threshold = 0.0031308f;
bvec3 cutoff = lessThan(color, vec3(threshold));
vec3 low = vec3(12.92f) * color;
vec3 high = vec3(1.055f) * pow(color, vec3(1.0f / 2.4f)) - vec3(0.055f);
#if __VERSION__ > 120
color = mix(high, low, cutoff);
#else
color = mix(high, low, vec3(cutoff));
#endif
#else
float inverse = 0.4545454f; // 1 / 2.2
color = pow(color, vec3(inverse));
#endif
}
uniform float u_Exposure;
// Tone mapping is not available when high-precision float framebuffer isn't enabled or supported.
#if defined(r_highPrecisionRendering) && defined(HAVE_ARB_texture_float)
uniform uint u_ViewWidth;
uniform uint u_ViewHeight;
uniform bool u_Tonemap;
#if defined(ADAPTIVE_EXPOSURE_AVAILABLE)
uniform bool u_TonemapAdaptiveExposure;
#endif
/* x: contrast
y: highlightsCompressionSpeed
z: shoulderClip
w: highlightsCompression */
uniform vec4 u_TonemapParms;
#if defined(ADAPTIVE_EXPOSURE_AVAILABLE)
uniform vec4 u_TonemapParms2;
#endif
vec3 TonemapLottes( vec3 color ) {
// Lottes 2016, "Advanced Techniques and Optimization of HDR Color Pipelines"
return pow( color, vec3( u_TonemapParms[0] ) )
/ ( pow( color, vec3( u_TonemapParms[0] * u_TonemapParms[1] ) ) * u_TonemapParms[2] + u_TonemapParms[3] );
}
#if defined(ADAPTIVE_EXPOSURE_AVAILABLE)
layout(std140, binding = BIND_LUMINANCE) uniform ub_LuminanceUBO {
uint luminanceU;
};
float GetAverageLuminance( const in uint luminance ) {
return float( luminanceU ) / ( u_TonemapParms2[1] * u_ViewWidth * u_ViewHeight );
}
#endif
#endif
DECLARE_OUTPUT(vec4)
void main() {
#insert material_fp
// calculate the screen texcoord in the 0.0 to 1.0 range
vec2 st = gl_FragCoord.st / r_FBufSize;
vec4 color = texture2D(u_CurrentMap, st);
color *= u_GlobalLightFactor;
#if defined(r_highPrecisionRendering) && defined(HAVE_ARB_texture_float)
if( u_Tonemap ) {
#if defined(ADAPTIVE_EXPOSURE_AVAILABLE)
if ( u_TonemapAdaptiveExposure ) {
const float l = GetAverageLuminance( luminanceU ) - 8;
color.rgb *= clamp( 0.18f / exp2( l * 0.8f + 0.1f ), 0.0f, 2.0f );
}
#endif
color.rgb *= u_Exposure;
color.rgb = TonemapLottes( color.rgb );
}
else
#endif
{
color.rgb *= u_Exposure;
}
color.rgb = clamp( color.rgb, vec3( 0.0f ), vec3( 1.0f ) );
if ( u_SRGB )
{
convertToSRGB( color.rgb );
}
#if defined(r_colorGrading)
// apply color grading
vec3 colCoord = color.rgb * 15.0 / 16.0 + 0.5 / 16.0;
colCoord.z *= 0.25;
color.rgb = u_ColorModulate.x * texture3D(u_ColorMap3D, colCoord).rgb;
color.rgb += u_ColorModulate.y * texture3D(u_ColorMap3D, colCoord + vec3(0.0, 0.0, 0.25)).rgb;
color.rgb += u_ColorModulate.z * texture3D(u_ColorMap3D, colCoord + vec3(0.0, 0.0, 0.50)).rgb;
color.rgb += u_ColorModulate.w * texture3D(u_ColorMap3D, colCoord + vec3(0.0, 0.0, 0.75)).rgb;
#endif
color.xyz = pow(color.xyz, vec3(u_InverseGamma));
outputColor = color;
}