Tonemapping.glsl 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright (C) 2009-2017, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #ifndef ANKI_SHADERS_TONEMAP_GLSL
  6. #define ANKI_SHADERS_TONEMAP_GLSL
  7. #include "shaders/Common.glsl"
  8. // A tick to compute log of base 10
  9. float log10(in float x)
  10. {
  11. return log(x) / log(10.0);
  12. }
  13. float computeLuminance(in vec3 color)
  14. {
  15. return max(dot(vec3(0.30, 0.59, 0.11), color), EPSILON);
  16. }
  17. vec3 computeExposedColor(in vec3 color, in float avgLum, in float threshold)
  18. {
  19. float keyValue = 1.03 - (2.0 / (2.0 + log10(avgLum + 1.0)));
  20. float linearExposure = (keyValue / avgLum);
  21. float exposure = log2(linearExposure);
  22. exposure -= threshold;
  23. return exp2(exposure) * color;
  24. }
  25. // Reinhard operator
  26. vec3 tonemapReinhard(in vec3 color, in float saturation)
  27. {
  28. float lum = computeLuminance(color);
  29. float toneMappedLuminance = lum / (lum + 1.0);
  30. return toneMappedLuminance * pow(color / lum, vec3(saturation));
  31. }
  32. // Uncharted 2 operator
  33. vec3 tonemapUncharted2(in vec3 color)
  34. {
  35. const float A = 0.15;
  36. const float B = 0.50;
  37. const float C = 0.10;
  38. const float D = 0.20;
  39. const float E = 0.02;
  40. const float F = 0.30;
  41. return ((color * (A * color + C * B) + D * E) / (color * (A * color + B) + D * F)) - E / F;
  42. }
  43. vec3 tonemap(in vec3 color, in float avgLum, in float threshold)
  44. {
  45. vec3 c = computeExposedColor(color, avgLum, threshold);
  46. // float saturation = clamp(avgLum, 0.0, 1.0);
  47. float saturation = 1.0;
  48. return tonemapReinhard(c, saturation);
  49. // return tonemapUncharted2(c);
  50. }
  51. #endif