1-
21$include vertex.glsl
32
43$define shader fragment
@@ -7,37 +6,38 @@ layout(binding = 0) uniform sampler2D texColor;
76
87layout (std140, binding = 2 ) uniform Tonemap
98{
10- vec4 tonemapFirst; // shoulderStrength, linearStrength, linearAngle, toeStrength
11- vec4 tonemapSecond; // toeNumerator, toeDenominator, white, tonemapEnabled
12- vec4 gammaParams; // gamma
9+ vec4 params; // gamma, tonemapEnabled
1310};
1411
1512out vec3 outColor;
1613
17- vec3 uncharted2Tonemap(vec3 x)
14+ // https://github.com/KhronosGroup/ToneMapping/blob/main/PBR_Neutral/pbrNeutral.glsl
15+ vec3 neutralToneMapping(vec3 color)
1816{
19- float shoulderStrength = tonemapFirst[0 ];
20- float linearStrength = tonemapFirst[1 ];
21- float linearAngle = tonemapFirst[2 ];
22- float toeStrength = tonemapFirst[3 ];
23- float toeNumerator = tonemapSecond[0 ];
24- float toeDenominator = tonemapSecond[1 ];
25- return ((x * (shoulderStrength * x + linearAngle * linearStrength) + toeStrength * toeNumerator) / (x * (shoulderStrength * x + linearStrength) + toeStrength * toeDenominator)) - toeNumerator / toeDenominator;
17+ const float startCompression = 0.8 - 0.04 ;
18+ const float desaturation = 0.15 ;
19+ float x = min (color.r, min (color.g, color.b));
20+ float offset = x < 0.08 ? x - 6.25 * x * x : 0.04 ;
21+ color -= offset;
22+ float peak = max (color.r, max (color.g, color.b));
23+ if (peak < startCompression) return color;
24+ const float d = 1 . - startCompression;
25+ float newPeak = 1 . - d * d / (peak + d - startCompression);
26+ color *= newPeak / peak;
27+ float g = 1 . - 1 . / (desaturation * (peak - newPeak) + 1 .);
28+ return mix (color, newPeak * vec3 (1 , 1 , 1 ), g);
2629}
2730
2831void main()
2932{
3033 vec3 color = texelFetch(texColor, ivec2 (gl_FragCoord .xy), 0 ).xyz;
3134
3235 // tone mapping
33- if (tonemapSecond[3 ] > 0.5 )
34- {
35- const float exposureBias = 2 ;
36- color = uncharted2Tonemap(color * exposureBias) / uncharted2Tonemap(vec3 (tonemapSecond[2 ]));
37- }
36+ if (params[1 ] > 0.5 )
37+ color = neutralToneMapping(color);
3838
3939 // gamma correction
40- color = pow (color, vec3 (gammaParams [0 ]));
40+ color = pow (color, vec3 (params [0 ]));
4141
4242 outColor = color;
4343}
0 commit comments