<@include gpu/Config.slh@> <$VERSION_HEADER$> // Generated on Sat Oct 24 09:34:37 2015 // // toneMapping.frag // // Draw texture 0 fetched at texcoord.xy // // Created by Sam Gateau on 6/22/2015 // Copyright 2015 High Fidelity, Inc. // // Distributed under the Apache License, Version 2.0. // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // <@include render-utils/ShaderConstants.h@> struct ToneMappingParams { float _2powExp; int _toneCurve; }; const float GAMMA_22 = 2.2; const float INV_GAMMA_22 = 1.0 / 2.2; const int ToneCurveNone = 0; const int ToneCurveGamma22 = 1; const int ToneCurveReinhard = 2; const int ToneCurveFilmic = 3; LAYOUT(binding=RENDER_UTILS_BUFFER_TM_PARAMS) uniform toneMappingParamsBuffer { ToneMappingParams params; }; float getTwoPowExposure() { return params._2powExp; } int getToneCurve() { return params._toneCurve; } LAYOUT(binding=RENDER_UTILS_TEXTURE_TM_COLOR) uniform sampler2D colorMap; layout(location=0) in vec2 varTexCoord0; layout(location=0) out vec4 outFragColor; void main(void) { <@if HIFI_USE_MIRRORED@> vec4 fragColorRaw = texture(colorMap, vec2(1.0 - varTexCoord0.x, varTexCoord0.y)); <@else@> vec4 fragColorRaw = texture(colorMap, varTexCoord0); <@endif@> vec3 fragColor = fragColorRaw.xyz; vec3 srcColor = fragColor * getTwoPowExposure(); int toneCurve = getToneCurve(); vec3 tonedColor = srcColor; if (toneCurve == ToneCurveFilmic) { vec3 x = max(vec3(0.0), srcColor-0.004); tonedColor = pow((x * (6.2 * x + 0.5)) / (x * (6.2 * x + 1.7) + 0.06), vec3(GAMMA_22)); } else if (toneCurve == ToneCurveReinhard) { tonedColor = srcColor/(1.0 + srcColor); } else if (toneCurve == ToneCurveGamma22) { // We use glEnable(GL_FRAMEBUFFER_SRGB), which automatically converts textures from RGB to SRGB // when writing from an RGB framebuffer to an SRGB framebuffer (note that it doesn't do anything // when writing from an SRGB framebuffer to an RGB framebuffer). // Since the conversion happens automatically, we don't need to do anything in this shader } else { // toneCurve == ToneCurveNone // For debugging purposes, we may want to see what the colors look like before the automatic OpenGL // conversion mentioned above, so we undo it here tonedColor = pow(srcColor, vec3(GAMMA_22)); } outFragColor = vec4(tonedColor, 1.0); }