tonemap_inc.glsl 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. // Based on Reinhard's extended formula, see equation 4 in https://doi.org/cjbgrt
  26. vec3 tonemap_reinhard(vec3 color, float p_white) {
  27. float white_squared = p_white * p_white;
  28. vec3 white_squared_color = white_squared * color;
  29. // Equivalent to color * (1 + color / white_squared) / (1 + color)
  30. return (white_squared_color + color * color) / (white_squared_color + white_squared);
  31. }
  32. vec3 tonemap_filmic(vec3 color, float p_white) {
  33. // exposure bias: input scale (color *= bias, white *= bias) to make the brightness consistent with other tonemappers
  34. // also useful to scale the input to the range that the tonemapper is designed for (some require very high input values)
  35. // has no effect on the curve's general shape or visual properties
  36. const float exposure_bias = 2.0f;
  37. const float A = 0.22f * exposure_bias * exposure_bias; // bias baked into constants for performance
  38. const float B = 0.30f * exposure_bias;
  39. const float C = 0.10f;
  40. const float D = 0.20f;
  41. const float E = 0.01f;
  42. const float F = 0.30f;
  43. vec3 color_tonemapped = ((color * (A * color + C * B) + D * E) / (color * (A * color + B) + D * F)) - E / F;
  44. float p_white_tonemapped = ((p_white * (A * p_white + C * B) + D * E) / (p_white * (A * p_white + B) + D * F)) - E / F;
  45. return color_tonemapped / p_white_tonemapped;
  46. }
  47. // Adapted from https://github.com/TheRealMJP/BakingLab/blob/master/BakingLab/ACES.hlsl
  48. // (MIT License).
  49. vec3 tonemap_aces(vec3 color, float p_white) {
  50. const float exposure_bias = 1.8f;
  51. const float A = 0.0245786f;
  52. const float B = 0.000090537f;
  53. const float C = 0.983729f;
  54. const float D = 0.432951f;
  55. const float E = 0.238081f;
  56. // Exposure bias baked into transform to save shader instructions. Equivalent to `color *= exposure_bias`
  57. const mat3 rgb_to_rrt = mat3(
  58. vec3(0.59719f * exposure_bias, 0.35458f * exposure_bias, 0.04823f * exposure_bias),
  59. vec3(0.07600f * exposure_bias, 0.90834f * exposure_bias, 0.01566f * exposure_bias),
  60. vec3(0.02840f * exposure_bias, 0.13383f * exposure_bias, 0.83777f * exposure_bias));
  61. const mat3 odt_to_rgb = mat3(
  62. vec3(1.60475f, -0.53108f, -0.07367f),
  63. vec3(-0.10208f, 1.10813f, -0.00605f),
  64. vec3(-0.00327f, -0.07276f, 1.07602f));
  65. color *= rgb_to_rrt;
  66. vec3 color_tonemapped = (color * (color + A) - B) / (color * (C * color + D) + E);
  67. color_tonemapped *= odt_to_rgb;
  68. p_white *= exposure_bias;
  69. float p_white_tonemapped = (p_white * (p_white + A) - B) / (p_white * (C * p_white + D) + E);
  70. return color_tonemapped / p_white_tonemapped;
  71. }
  72. // Polynomial approximation of EaryChow's AgX sigmoid curve.
  73. // x must be within the range [0.0, 1.0]
  74. vec3 agx_contrast_approx(vec3 x) {
  75. // Generated with Excel trendline
  76. // Input data: Generated using python sigmoid with EaryChow's configuration and 57 steps
  77. // Additional padding values were added to give correct intersections at 0.0 and 1.0
  78. // 6th order, intercept of 0.0 to remove an operation and ensure intersection at 0.0
  79. vec3 x2 = x * x;
  80. vec3 x4 = x2 * x2;
  81. return 0.021 * x + 4.0111 * x2 - 25.682 * x2 * x + 70.359 * x4 - 74.778 * x4 * x + 27.069 * x4 * x2;
  82. }
  83. // This is an approximation and simplification of EaryChow's AgX implementation that is used by Blender.
  84. // This code is based off of the script that generates the AgX_Base_sRGB.cube LUT that Blender uses.
  85. // Source: https://github.com/EaryChow/AgX_LUT_Gen/blob/main/AgXBasesRGB.py
  86. vec3 tonemap_agx(vec3 color) {
  87. // Combined linear sRGB to linear Rec 2020 and Blender AgX inset matrices:
  88. const mat3 srgb_to_rec2020_agx_inset_matrix = mat3(
  89. 0.54490813676363087053, 0.14044005884001287035, 0.088827411851915368603,
  90. 0.37377945959812267119, 0.75410959864013760045, 0.17887712465043811023,
  91. 0.081384976686407536266, 0.10543358536857773485, 0.73224999956948382528);
  92. // Combined inverse AgX outset matrix and linear Rec 2020 to linear sRGB matrices.
  93. const mat3 agx_outset_rec2020_to_srgb_matrix = mat3(
  94. 1.9645509602733325934, -0.29932243390911083839, -0.16436833806080403409,
  95. -0.85585845117807513559, 1.3264510741502356555, -0.23822464068860595117,
  96. -0.10886710826831608324, -0.027084020983874825605, 1.402665347143271889);
  97. // LOG2_MIN = -10.0
  98. // LOG2_MAX = +6.5
  99. // MIDDLE_GRAY = 0.18
  100. const float min_ev = -12.4739311883324; // log2(pow(2, LOG2_MIN) * MIDDLE_GRAY)
  101. const float max_ev = 4.02606881166759; // log2(pow(2, LOG2_MAX) * MIDDLE_GRAY)
  102. // Large negative values in one channel and large positive values in other
  103. // channels can result in a colour that appears darker and more saturated than
  104. // desired after passing it through the inset matrix. For this reason, it is
  105. // best to prevent negative input values.
  106. // This is done before the Rec. 2020 transform to allow the Rec. 2020
  107. // transform to be combined with the AgX inset matrix. This results in a loss
  108. // of color information that could be correctly interpreted within the
  109. // Rec. 2020 color space as positive RGB values, but it is less common for Godot
  110. // to provide this function with negative sRGB values and therefore not worth
  111. // the performance cost of an additional matrix multiplication.
  112. // A value of 2e-10 intentionally introduces insignificant error to prevent
  113. // log2(0.0) after the inset matrix is applied; color will be >= 1e-10 after
  114. // the matrix transform.
  115. color = max(color, 2e-10);
  116. // Do AGX in rec2020 to match Blender and then apply inset matrix.
  117. color = srgb_to_rec2020_agx_inset_matrix * color;
  118. // Log2 space encoding.
  119. // Must be clamped because agx_contrast_approx may not work
  120. // well with values outside of the range [0.0, 1.0]
  121. color = clamp(log2(color), min_ev, max_ev);
  122. color = (color - min_ev) / (max_ev - min_ev);
  123. // Apply sigmoid function approximation.
  124. color = agx_contrast_approx(color);
  125. // Convert back to linear before applying outset matrix.
  126. color = pow(color, vec3(2.4));
  127. // Apply outset to make the result more chroma-laden and then go back to linear sRGB.
  128. color = agx_outset_rec2020_to_srgb_matrix * color;
  129. // Blender's lusRGB.compensate_low_side is too complex for this shader, so
  130. // simply return the color, even if it has negative components. These negative
  131. // components may be useful for subsequent color adjustments.
  132. return color;
  133. }
  134. #define TONEMAPPER_LINEAR 0
  135. #define TONEMAPPER_REINHARD 1
  136. #define TONEMAPPER_FILMIC 2
  137. #define TONEMAPPER_ACES 3
  138. #define TONEMAPPER_AGX 4
  139. vec3 apply_tonemapping(vec3 color, float p_white) { // inputs are LINEAR
  140. // Ensure color values passed to tonemappers are positive.
  141. // They can be negative in the case of negative lights, which leads to undesired behavior.
  142. if (tonemapper == TONEMAPPER_LINEAR) {
  143. return color;
  144. } else if (tonemapper == TONEMAPPER_REINHARD) {
  145. return tonemap_reinhard(max(vec3(0.0f), color), p_white);
  146. } else if (tonemapper == TONEMAPPER_FILMIC) {
  147. return tonemap_filmic(max(vec3(0.0f), color), p_white);
  148. } else if (tonemapper == TONEMAPPER_ACES) {
  149. return tonemap_aces(max(vec3(0.0f), color), p_white);
  150. } else { // TONEMAPPER_AGX
  151. return tonemap_agx(color);
  152. }
  153. }
  154. #endif // APPLY_TONEMAPPING