tonemap.glsl 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /* clang-format off */
  2. [vertex]
  3. layout(location = 0) in highp vec4 vertex_attrib;
  4. /* clang-format on */
  5. layout(location = 4) in vec2 uv_in;
  6. out vec2 uv_interp;
  7. void main() {
  8. gl_Position = vertex_attrib;
  9. uv_interp = uv_in;
  10. #ifdef V_FLIP
  11. uv_interp.y = 1.0f - uv_interp.y;
  12. #endif
  13. }
  14. /* clang-format off */
  15. [fragment]
  16. #if !defined(GLES_OVER_GL)
  17. precision mediump float;
  18. #endif
  19. /* clang-format on */
  20. in vec2 uv_interp;
  21. uniform highp sampler2D source; //texunit:0
  22. uniform float exposure;
  23. uniform float white;
  24. #ifdef USE_AUTO_EXPOSURE
  25. uniform highp sampler2D source_auto_exposure; //texunit:1
  26. uniform highp float auto_exposure_grey;
  27. #endif
  28. #if defined(USE_GLOW_LEVEL1) || defined(USE_GLOW_LEVEL2) || defined(USE_GLOW_LEVEL3) || defined(USE_GLOW_LEVEL4) || defined(USE_GLOW_LEVEL5) || defined(USE_GLOW_LEVEL6) || defined(USE_GLOW_LEVEL7)
  29. #define USING_GLOW // only use glow when at least one glow level is selected
  30. uniform highp sampler2D source_glow; //texunit:2
  31. uniform highp float glow_intensity;
  32. #endif
  33. #ifdef USE_BCS
  34. uniform vec3 bcs;
  35. #endif
  36. #ifdef USE_COLOR_CORRECTION
  37. uniform sampler2D color_correction; //texunit:3
  38. #endif
  39. layout(location = 0) out vec4 frag_color;
  40. #ifdef USE_GLOW_FILTER_BICUBIC
  41. // w0, w1, w2, and w3 are the four cubic B-spline basis functions
  42. float w0(float a) {
  43. return (1.0f / 6.0f) * (a * (a * (-a + 3.0f) - 3.0f) + 1.0f);
  44. }
  45. float w1(float a) {
  46. return (1.0f / 6.0f) * (a * a * (3.0f * a - 6.0f) + 4.0f);
  47. }
  48. float w2(float a) {
  49. return (1.0f / 6.0f) * (a * (a * (-3.0f * a + 3.0f) + 3.0f) + 1.0f);
  50. }
  51. float w3(float a) {
  52. return (1.0f / 6.0f) * (a * a * a);
  53. }
  54. // g0 and g1 are the two amplitude functions
  55. float g0(float a) {
  56. return w0(a) + w1(a);
  57. }
  58. float g1(float a) {
  59. return w2(a) + w3(a);
  60. }
  61. // h0 and h1 are the two offset functions
  62. float h0(float a) {
  63. return -1.0f + w1(a) / (w0(a) + w1(a));
  64. }
  65. float h1(float a) {
  66. return 1.0f + w3(a) / (w2(a) + w3(a));
  67. }
  68. uniform ivec2 glow_texture_size;
  69. vec4 texture2D_bicubic(sampler2D tex, vec2 uv, int p_lod) {
  70. float lod = float(p_lod);
  71. vec2 tex_size = vec2(glow_texture_size >> p_lod);
  72. vec2 pixel_size = vec2(1.0f) / tex_size;
  73. uv = uv * tex_size + vec2(0.5f);
  74. vec2 iuv = floor(uv);
  75. vec2 fuv = fract(uv);
  76. float g0x = g0(fuv.x);
  77. float g1x = g1(fuv.x);
  78. float h0x = h0(fuv.x);
  79. float h1x = h1(fuv.x);
  80. float h0y = h0(fuv.y);
  81. float h1y = h1(fuv.y);
  82. vec2 p0 = (vec2(iuv.x + h0x, iuv.y + h0y) - vec2(0.5f)) * pixel_size;
  83. vec2 p1 = (vec2(iuv.x + h1x, iuv.y + h0y) - vec2(0.5f)) * pixel_size;
  84. vec2 p2 = (vec2(iuv.x + h0x, iuv.y + h1y) - vec2(0.5f)) * pixel_size;
  85. vec2 p3 = (vec2(iuv.x + h1x, iuv.y + h1y) - vec2(0.5f)) * pixel_size;
  86. return (g0(fuv.y) * (g0x * textureLod(tex, p0, lod) + g1x * textureLod(tex, p1, lod))) +
  87. (g1(fuv.y) * (g0x * textureLod(tex, p2, lod) + g1x * textureLod(tex, p3, lod)));
  88. }
  89. #define GLOW_TEXTURE_SAMPLE(m_tex, m_uv, m_lod) texture2D_bicubic(m_tex, m_uv, m_lod)
  90. #else
  91. #define GLOW_TEXTURE_SAMPLE(m_tex, m_uv, m_lod) textureLod(m_tex, m_uv, float(m_lod))
  92. #endif
  93. vec3 tonemap_filmic(vec3 color, float white) {
  94. // exposure bias: input scale (color *= bias, white *= bias) to make the brightness consistent with other tonemappers
  95. // also useful to scale the input to the range that the tonemapper is designed for (some require very high input values)
  96. // has no effect on the curve's general shape or visual properties
  97. const float exposure_bias = 2.0f;
  98. const float A = 0.22f * exposure_bias * exposure_bias; // bias baked into constants for performance
  99. const float B = 0.30f * exposure_bias;
  100. const float C = 0.10f;
  101. const float D = 0.20f;
  102. const float E = 0.01f;
  103. const float F = 0.30f;
  104. vec3 color_tonemapped = ((color * (A * color + C * B) + D * E) / (color * (A * color + B) + D * F)) - E / F;
  105. float white_tonemapped = ((white * (A * white + C * B) + D * E) / (white * (A * white + B) + D * F)) - E / F;
  106. return clamp(color_tonemapped / white_tonemapped, vec3(0.0f), vec3(1.0f));
  107. }
  108. vec3 tonemap_aces(vec3 color, float white) {
  109. const float exposure_bias = 0.85f;
  110. const float A = 2.51f * exposure_bias * exposure_bias;
  111. const float B = 0.03f * exposure_bias;
  112. const float C = 2.43f * exposure_bias * exposure_bias;
  113. const float D = 0.59f * exposure_bias;
  114. const float E = 0.14f;
  115. vec3 color_tonemapped = (color * (A * color + B)) / (color * (C * color + D) + E);
  116. float white_tonemapped = (white * (A * white + B)) / (white * (C * white + D) + E);
  117. return clamp(color_tonemapped / white_tonemapped, vec3(0.0f), vec3(1.0f));
  118. }
  119. vec3 tonemap_reinhard(vec3 color, float white) {
  120. return clamp((white * color + color) / (color * white + white), vec3(0.0f), vec3(1.0f));
  121. }
  122. vec3 linear_to_srgb(vec3 color) { // convert linear rgb to srgb, assumes clamped input in range [0;1]
  123. const vec3 a = vec3(0.055f);
  124. 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)));
  125. }
  126. vec3 apply_tonemapping(vec3 color, float white) { // inputs are LINEAR, always outputs clamped [0;1] color
  127. #ifdef USE_REINHARD_TONEMAPPER
  128. return tonemap_reinhard(color, white);
  129. #endif
  130. #ifdef USE_FILMIC_TONEMAPPER
  131. return tonemap_filmic(color, white);
  132. #endif
  133. #ifdef USE_ACES_TONEMAPPER
  134. return tonemap_aces(color, white);
  135. #endif
  136. return clamp(color, vec3(0.0f), vec3(1.0f)); // no other selected -> linear
  137. }
  138. vec3 gather_glow(sampler2D tex, vec2 uv) { // sample all selected glow levels
  139. vec3 glow = vec3(0.0f);
  140. #ifdef USE_GLOW_LEVEL1
  141. glow += GLOW_TEXTURE_SAMPLE(tex, uv, 1).rgb;
  142. #endif
  143. #ifdef USE_GLOW_LEVEL2
  144. glow += GLOW_TEXTURE_SAMPLE(tex, uv, 2).rgb;
  145. #endif
  146. #ifdef USE_GLOW_LEVEL3
  147. glow += GLOW_TEXTURE_SAMPLE(tex, uv, 3).rgb;
  148. #endif
  149. #ifdef USE_GLOW_LEVEL4
  150. glow += GLOW_TEXTURE_SAMPLE(tex, uv, 4).rgb;
  151. #endif
  152. #ifdef USE_GLOW_LEVEL5
  153. glow += GLOW_TEXTURE_SAMPLE(tex, uv, 5).rgb;
  154. #endif
  155. #ifdef USE_GLOW_LEVEL6
  156. glow += GLOW_TEXTURE_SAMPLE(tex, uv, 6).rgb;
  157. #endif
  158. #ifdef USE_GLOW_LEVEL7
  159. glow += GLOW_TEXTURE_SAMPLE(tex, uv, 7).rgb;
  160. #endif
  161. return glow;
  162. }
  163. vec3 apply_glow(vec3 color, vec3 glow) { // apply glow using the selected blending mode
  164. #ifdef USE_GLOW_REPLACE
  165. color = glow;
  166. #endif
  167. #ifdef USE_GLOW_SCREEN
  168. color = max((color + glow) - (color * glow), vec3(0.0));
  169. #endif
  170. #ifdef USE_GLOW_SOFTLIGHT
  171. glow = glow * vec3(0.5f) + vec3(0.5f);
  172. color.r = (glow.r <= 0.5f) ? (color.r - (1.0f - 2.0f * glow.r) * color.r * (1.0f - color.r)) : (((glow.r > 0.5f) && (color.r <= 0.25f)) ? (color.r + (2.0f * glow.r - 1.0f) * (4.0f * color.r * (4.0f * color.r + 1.0f) * (color.r - 1.0f) + 7.0f * color.r)) : (color.r + (2.0f * glow.r - 1.0f) * (sqrt(color.r) - color.r)));
  173. color.g = (glow.g <= 0.5f) ? (color.g - (1.0f - 2.0f * glow.g) * color.g * (1.0f - color.g)) : (((glow.g > 0.5f) && (color.g <= 0.25f)) ? (color.g + (2.0f * glow.g - 1.0f) * (4.0f * color.g * (4.0f * color.g + 1.0f) * (color.g - 1.0f) + 7.0f * color.g)) : (color.g + (2.0f * glow.g - 1.0f) * (sqrt(color.g) - color.g)));
  174. color.b = (glow.b <= 0.5f) ? (color.b - (1.0f - 2.0f * glow.b) * color.b * (1.0f - color.b)) : (((glow.b > 0.5f) && (color.b <= 0.25f)) ? (color.b + (2.0f * glow.b - 1.0f) * (4.0f * color.b * (4.0f * color.b + 1.0f) * (color.b - 1.0f) + 7.0f * color.b)) : (color.b + (2.0f * glow.b - 1.0f) * (sqrt(color.b) - color.b)));
  175. #endif
  176. #if !defined(USE_GLOW_SCREEN) && !defined(USE_GLOW_SOFTLIGHT) && !defined(USE_GLOW_REPLACE) // no other selected -> additive
  177. color += glow;
  178. #endif
  179. return color;
  180. }
  181. vec3 apply_bcs(vec3 color, vec3 bcs) {
  182. color = mix(vec3(0.0f), color, bcs.x);
  183. color = mix(vec3(0.5f), color, bcs.y);
  184. color = mix(vec3(dot(vec3(1.0f), color) * 0.33333f), color, bcs.z);
  185. return color;
  186. }
  187. vec3 apply_color_correction(vec3 color, sampler2D correction_tex) {
  188. color.r = texture(correction_tex, vec2(color.r, 0.0f)).r;
  189. color.g = texture(correction_tex, vec2(color.g, 0.0f)).g;
  190. color.b = texture(correction_tex, vec2(color.b, 0.0f)).b;
  191. return color;
  192. }
  193. void main() {
  194. vec3 color = textureLod(source, uv_interp, 0.0f).rgb;
  195. // Exposure
  196. #ifdef USE_AUTO_EXPOSURE
  197. color /= texelFetch(source_auto_exposure, ivec2(0, 0), 0).r / auto_exposure_grey;
  198. #endif
  199. color *= exposure;
  200. // Early Tonemap & SRGB Conversion
  201. color = apply_tonemapping(color, white);
  202. #ifdef KEEP_3D_LINEAR
  203. // leave color as is (-> don't convert to SRGB)
  204. #else
  205. color = linear_to_srgb(color); // regular linear -> SRGB conversion
  206. #endif
  207. // Glow
  208. #ifdef USING_GLOW
  209. vec3 glow = gather_glow(source_glow, uv_interp) * glow_intensity;
  210. // high dynamic range -> SRGB
  211. glow = apply_tonemapping(glow, white);
  212. glow = linear_to_srgb(glow);
  213. color = apply_glow(color, glow);
  214. #endif
  215. // Additional effects
  216. #ifdef USE_BCS
  217. color = apply_bcs(color, bcs);
  218. #endif
  219. #ifdef USE_COLOR_CORRECTION
  220. color = apply_color_correction(color, color_correction);
  221. #endif
  222. frag_color = vec4(color, 1.0f);
  223. }