From c128210adf8a5e57c007a613c9c7b0a03a5fe1b4 Mon Sep 17 00:00:00 2001 From: Supakorn 'Jamie' Rassameemasmuang Date: Sun, 11 Feb 2024 02:00:30 -0700 Subject: [PATCH] VK: Add gamma conversion from linear to srgb in fragment shader. --- base/shaders/fragment.glsl | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/base/shaders/fragment.glsl b/base/shaders/fragment.glsl index e76d5e23a..5c842cfce 100644 --- a/base/shaders/fragment.glsl +++ b/base/shaders/fragment.glsl @@ -95,6 +95,21 @@ float Roughness; vec3 normal; +const float gamma=2.2; +const float invGamma=1.0/gamma; + +/** + * @brief Converts linear color (measuring photon count) to srgb (what our brain thinks + * is the brightness + * example linearToPerceptual(vec3(0.5)) is approximately vec3(0.729) + */ +vec3 linearToPerceptual(vec3 inColor) +{ + // an actual 0.5 brightness (half amount of photons) would + // look brighter than what our eyes think is "half" light + return pow(inColor, vec3(invGamma)); +} + #ifdef USE_IBL layout(binding=11) uniform sampler2D diffuseSampler; @@ -267,6 +282,14 @@ void main() { #endif /*USE_IBL*/ #endif /*NORMAL*/ + // for reasons, the swapchain/FXAA shader expects a "perceptual" color, + // while all of our calculations have been linear (i.e. by measuring photon counts) + // (e.g. our 0.5 is much much brighter than what swap chain/monitor thinks 0.5 is) + // need to give the output image the color our brain perceives with the same photon count + // as the original pixel + vec3 outColorInPerceptualSpace=linearToPerceptual(outColor.rgb); + outColor=vec4(outColorInPerceptualSpace,outColor.a); + #ifndef WIDTH // TODO DO NOT DO THE DEPTH COMPARISON WHEN NO TRANSPARENT OBJECTS! uint pixel=uint(gl_FragCoord.y)*push.constants[1]+uint(gl_FragCoord.x); #if defined(TRANSPARENT) || (!defined(HAVE_INTERLOCK) && !defined(OPAQUE))