tonemap_inc.glsl 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. layout(std140) uniform TonemapData { //ubo:0
  2. float exposure;
  3. float white;
  4. int tonemapper;
  5. int pad;
  6. int pad2;
  7. float brightness;
  8. float contrast;
  9. float saturation;
  10. };
  11. // This expects 0-1 range input.
  12. vec3 linear_to_srgb(vec3 color) {
  13. //color = clamp(color, vec3(0.0), vec3(1.0));
  14. //const vec3 a = vec3(0.055f);
  15. //return mix((vec3(1.0f) + a) * pow(color.rgb, vec3(1.0f / 2.4f)) - a, 12.92f * color.rgb, lessThan(color.rgb, vec3(0.0031308f)));
  16. // Approximation from http://chilliant.blogspot.com/2012/08/srgb-approximations-for-hlsl.html
  17. return max(vec3(1.055) * pow(color, vec3(0.416666667)) - vec3(0.055), vec3(0.0));
  18. }
  19. // This expects 0-1 range input, outside that range it behaves poorly.
  20. vec3 srgb_to_linear(vec3 color) {
  21. // Approximation from http://chilliant.blogspot.com/2012/08/srgb-approximations-for-hlsl.html
  22. return color * (color * (color * 0.305306011 + 0.682171111) + 0.012522878);
  23. }
  24. #ifdef APPLY_TONEMAPPING
  25. vec3 tonemap_filmic(vec3 color, float p_white) {
  26. // exposure bias: input scale (color *= bias, white *= bias) to make the brightness consistent with other tonemappers
  27. // also useful to scale the input to the range that the tonemapper is designed for (some require very high input values)
  28. // has no effect on the curve's general shape or visual properties
  29. const float exposure_bias = 2.0f;
  30. const float A = 0.22f * exposure_bias * exposure_bias; // bias baked into constants for performance
  31. const float B = 0.30f * exposure_bias;
  32. const float C = 0.10f;
  33. const float D = 0.20f;
  34. const float E = 0.01f;
  35. const float F = 0.30f;
  36. vec3 color_tonemapped = ((color * (A * color + C * B) + D * E) / (color * (A * color + B) + D * F)) - E / F;
  37. float p_white_tonemapped = ((p_white * (A * p_white + C * B) + D * E) / (p_white * (A * p_white + B) + D * F)) - E / F;
  38. return color_tonemapped / p_white_tonemapped;
  39. }
  40. // Adapted from https://github.com/TheRealMJP/BakingLab/blob/master/BakingLab/ACES.hlsl
  41. // (MIT License).
  42. vec3 tonemap_aces(vec3 color, float p_white) {
  43. const float exposure_bias = 1.8f;
  44. const float A = 0.0245786f;
  45. const float B = 0.000090537f;
  46. const float C = 0.983729f;
  47. const float D = 0.432951f;
  48. const float E = 0.238081f;
  49. // Exposure bias baked into transform to save shader instructions. Equivalent to `color *= exposure_bias`
  50. const mat3 rgb_to_rrt = mat3(
  51. vec3(0.59719f * exposure_bias, 0.35458f * exposure_bias, 0.04823f * exposure_bias),
  52. vec3(0.07600f * exposure_bias, 0.90834f * exposure_bias, 0.01566f * exposure_bias),
  53. vec3(0.02840f * exposure_bias, 0.13383f * exposure_bias, 0.83777f * exposure_bias));
  54. const mat3 odt_to_rgb = mat3(
  55. vec3(1.60475f, -0.53108f, -0.07367f),
  56. vec3(-0.10208f, 1.10813f, -0.00605f),
  57. vec3(-0.00327f, -0.07276f, 1.07602f));
  58. color *= rgb_to_rrt;
  59. vec3 color_tonemapped = (color * (color + A) - B) / (color * (C * color + D) + E);
  60. color_tonemapped *= odt_to_rgb;
  61. p_white *= exposure_bias;
  62. float p_white_tonemapped = (p_white * (p_white + A) - B) / (p_white * (C * p_white + D) + E);
  63. return color_tonemapped / p_white_tonemapped;
  64. }
  65. // Based on Reinhard's extended formula, see equation 4 in https://doi.org/cjbgrt
  66. vec3 tonemap_reinhard(vec3 color, float p_white) {
  67. float white_squared = p_white * p_white;
  68. vec3 white_squared_color = white_squared * color;
  69. // Equivalent to color * (1 + color / white_squared) / (1 + color)
  70. return (white_squared_color + color * color) / (white_squared_color + white_squared);
  71. }
  72. #define TONEMAPPER_LINEAR 0
  73. #define TONEMAPPER_REINHARD 1
  74. #define TONEMAPPER_FILMIC 2
  75. #define TONEMAPPER_ACES 3
  76. vec3 apply_tonemapping(vec3 color, float p_white) { // inputs are LINEAR
  77. // Ensure color values passed to tonemappers are positive.
  78. // They can be negative in the case of negative lights, which leads to undesired behavior.
  79. if (tonemapper == TONEMAPPER_LINEAR) {
  80. return color;
  81. } else if (tonemapper == TONEMAPPER_REINHARD) {
  82. return tonemap_reinhard(max(vec3(0.0f), color), p_white);
  83. } else if (tonemapper == TONEMAPPER_FILMIC) {
  84. return tonemap_filmic(max(vec3(0.0f), color), p_white);
  85. } else { // TONEMAPPER_ACES
  86. return tonemap_aces(max(vec3(0.0f), color), p_white);
  87. }
  88. }
  89. #endif // APPLY_TONEMAPPING