tonemap_inc.glsl 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #ifdef USE_BCS
  2. uniform vec3 bcs;
  3. #endif
  4. #ifdef USE_COLOR_CORRECTION
  5. #ifdef USE_1D_LUT
  6. uniform sampler2D source_color_correction; //texunit:-1
  7. #else
  8. uniform sampler3D source_color_correction; //texunit:-1
  9. #endif
  10. #endif
  11. layout(std140) uniform TonemapData { //ubo:0
  12. float exposure;
  13. float white;
  14. int tonemapper;
  15. int pad;
  16. };
  17. vec3 apply_bcs(vec3 color, vec3 bcs) {
  18. color = mix(vec3(0.0), color, bcs.x);
  19. color = mix(vec3(0.5), color, bcs.y);
  20. color = mix(vec3(dot(vec3(1.0), color) * 0.33333), color, bcs.z);
  21. return color;
  22. }
  23. #ifdef USE_COLOR_CORRECTION
  24. #ifdef USE_1D_LUT
  25. vec3 apply_color_correction(vec3 color) {
  26. color.r = texture(source_color_correction, vec2(color.r, 0.0f)).r;
  27. color.g = texture(source_color_correction, vec2(color.g, 0.0f)).g;
  28. color.b = texture(source_color_correction, vec2(color.b, 0.0f)).b;
  29. return color;
  30. }
  31. #else
  32. vec3 apply_color_correction(vec3 color) {
  33. return textureLod(source_color_correction, color, 0.0).rgb;
  34. }
  35. #endif
  36. #endif
  37. vec3 tonemap_filmic(vec3 color, float p_white) {
  38. // exposure bias: input scale (color *= bias, white *= bias) to make the brightness consistent with other tonemappers
  39. // also useful to scale the input to the range that the tonemapper is designed for (some require very high input values)
  40. // has no effect on the curve's general shape or visual properties
  41. const float exposure_bias = 2.0f;
  42. const float A = 0.22f * exposure_bias * exposure_bias; // bias baked into constants for performance
  43. const float B = 0.30f * exposure_bias;
  44. const float C = 0.10f;
  45. const float D = 0.20f;
  46. const float E = 0.01f;
  47. const float F = 0.30f;
  48. vec3 color_tonemapped = ((color * (A * color + C * B) + D * E) / (color * (A * color + B) + D * F)) - E / F;
  49. float p_white_tonemapped = ((p_white * (A * p_white + C * B) + D * E) / (p_white * (A * p_white + B) + D * F)) - E / F;
  50. return color_tonemapped / p_white_tonemapped;
  51. }
  52. // Adapted from https://github.com/TheRealMJP/BakingLab/blob/master/BakingLab/ACES.hlsl
  53. // (MIT License).
  54. vec3 tonemap_aces(vec3 color, float p_white) {
  55. const float exposure_bias = 1.8f;
  56. const float A = 0.0245786f;
  57. const float B = 0.000090537f;
  58. const float C = 0.983729f;
  59. const float D = 0.432951f;
  60. const float E = 0.238081f;
  61. // Exposure bias baked into transform to save shader instructions. Equivalent to `color *= exposure_bias`
  62. const mat3 rgb_to_rrt = mat3(
  63. vec3(0.59719f * exposure_bias, 0.35458f * exposure_bias, 0.04823f * exposure_bias),
  64. vec3(0.07600f * exposure_bias, 0.90834f * exposure_bias, 0.01566f * exposure_bias),
  65. vec3(0.02840f * exposure_bias, 0.13383f * exposure_bias, 0.83777f * exposure_bias));
  66. const mat3 odt_to_rgb = mat3(
  67. vec3(1.60475f, -0.53108f, -0.07367f),
  68. vec3(-0.10208f, 1.10813f, -0.00605f),
  69. vec3(-0.00327f, -0.07276f, 1.07602f));
  70. color *= rgb_to_rrt;
  71. vec3 color_tonemapped = (color * (color + A) - B) / (color * (C * color + D) + E);
  72. color_tonemapped *= odt_to_rgb;
  73. p_white *= exposure_bias;
  74. float p_white_tonemapped = (p_white * (p_white + A) - B) / (p_white * (C * p_white + D) + E);
  75. return color_tonemapped / p_white_tonemapped;
  76. }
  77. vec3 tonemap_reinhard(vec3 color, float p_white) {
  78. return (p_white * color + color) / (color * p_white + p_white);
  79. }
  80. // This expects 0-1 range input.
  81. vec3 linear_to_srgb(vec3 color) {
  82. //color = clamp(color, vec3(0.0), vec3(1.0));
  83. //const vec3 a = vec3(0.055f);
  84. //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)));
  85. // Approximation from http://chilliant.blogspot.com/2012/08/srgb-approximations-for-hlsl.html
  86. return max(vec3(1.055) * pow(color, vec3(0.416666667)) - vec3(0.055), vec3(0.0));
  87. }
  88. // This expects 0-1 range input, outside that range it behaves poorly.
  89. vec3 srgb_to_linear(vec3 color) {
  90. // Approximation from http://chilliant.blogspot.com/2012/08/srgb-approximations-for-hlsl.html
  91. return color * (color * (color * 0.305306011 + 0.682171111) + 0.012522878);
  92. }
  93. #define TONEMAPPER_LINEAR 0
  94. #define TONEMAPPER_REINHARD 1
  95. #define TONEMAPPER_FILMIC 2
  96. #define TONEMAPPER_ACES 3
  97. vec3 apply_tonemapping(vec3 color, float p_white) { // inputs are LINEAR, always outputs clamped [0;1] color
  98. // Ensure color values passed to tonemappers are positive.
  99. // They can be negative in the case of negative lights, which leads to undesired behavior.
  100. if (tonemapper == TONEMAPPER_LINEAR) {
  101. return color;
  102. } else if (tonemapper == TONEMAPPER_REINHARD) {
  103. return tonemap_reinhard(max(vec3(0.0f), color), p_white);
  104. } else if (tonemapper == TONEMAPPER_FILMIC) {
  105. return tonemap_filmic(max(vec3(0.0f), color), p_white);
  106. } else { // TONEMAPPER_ACES
  107. return tonemap_aces(max(vec3(0.0f), color), p_white);
  108. }
  109. }