Tonemapping.glsl 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright (C) 2009-2016, Panagiotis Christopoulos Charitos.
  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. #pragma anki 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)
  42. / (color * (A * color + B) + D * F)) - E / F;
  43. }
  44. vec3 tonemap(in vec3 color, in float avgLum, in float threshold)
  45. {
  46. vec3 c = computeExposedColor(color, avgLum, threshold);
  47. //float saturation = clamp(avgLum, 0.0, 1.0);
  48. float saturation = 1.0;
  49. return tonemapReinhard(c, saturation);
  50. //return tonemapUncharted2(c);
  51. }
  52. #endif