tonemap.glsl 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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.0 - 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. in vec2 uv_interp;
  20. /* clang-format on */
  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. uniform highp sampler2D source_glow; //texunit:2
  30. uniform highp float glow_intensity;
  31. #endif
  32. #ifdef USE_BCS
  33. uniform vec3 bcs;
  34. #endif
  35. #ifdef USE_COLOR_CORRECTION
  36. uniform sampler2D color_correction; //texunit:3
  37. #endif
  38. layout(location = 0) out vec4 frag_color;
  39. #ifdef USE_GLOW_FILTER_BICUBIC
  40. // w0, w1, w2, and w3 are the four cubic B-spline basis functions
  41. float w0(float a) {
  42. return (1.0 / 6.0) * (a * (a * (-a + 3.0) - 3.0) + 1.0);
  43. }
  44. float w1(float a) {
  45. return (1.0 / 6.0) * (a * a * (3.0 * a - 6.0) + 4.0);
  46. }
  47. float w2(float a) {
  48. return (1.0 / 6.0) * (a * (a * (-3.0 * a + 3.0) + 3.0) + 1.0);
  49. }
  50. float w3(float a) {
  51. return (1.0 / 6.0) * (a * a * a);
  52. }
  53. // g0 and g1 are the two amplitude functions
  54. float g0(float a) {
  55. return w0(a) + w1(a);
  56. }
  57. float g1(float a) {
  58. return w2(a) + w3(a);
  59. }
  60. // h0 and h1 are the two offset functions
  61. float h0(float a) {
  62. return -1.0 + w1(a) / (w0(a) + w1(a));
  63. }
  64. float h1(float a) {
  65. return 1.0 + w3(a) / (w2(a) + w3(a));
  66. }
  67. uniform ivec2 glow_texture_size;
  68. vec4 texture2D_bicubic(sampler2D tex, vec2 uv, int p_lod) {
  69. float lod = float(p_lod);
  70. vec2 tex_size = vec2(glow_texture_size >> p_lod);
  71. vec2 pixel_size = 1.0 / tex_size;
  72. uv = uv * tex_size + 0.5;
  73. vec2 iuv = floor(uv);
  74. vec2 fuv = fract(uv);
  75. float g0x = g0(fuv.x);
  76. float g1x = g1(fuv.x);
  77. float h0x = h0(fuv.x);
  78. float h1x = h1(fuv.x);
  79. float h0y = h0(fuv.y);
  80. float h1y = h1(fuv.y);
  81. vec2 p0 = (vec2(iuv.x + h0x, iuv.y + h0y) - 0.5) * pixel_size;
  82. vec2 p1 = (vec2(iuv.x + h1x, iuv.y + h0y) - 0.5) * pixel_size;
  83. vec2 p2 = (vec2(iuv.x + h0x, iuv.y + h1y) - 0.5) * pixel_size;
  84. vec2 p3 = (vec2(iuv.x + h1x, iuv.y + h1y) - 0.5) * pixel_size;
  85. return (g0(fuv.y) * (g0x * textureLod(tex, p0, lod) + g1x * textureLod(tex, p1, lod))) +
  86. (g1(fuv.y) * (g0x * textureLod(tex, p2, lod) + g1x * textureLod(tex, p3, lod)));
  87. }
  88. #define GLOW_TEXTURE_SAMPLE(m_tex, m_uv, m_lod) texture2D_bicubic(m_tex, m_uv, m_lod)
  89. #else
  90. #define GLOW_TEXTURE_SAMPLE(m_tex, m_uv, m_lod) textureLod(m_tex, m_uv, float(m_lod))
  91. #endif
  92. vec3 tonemap_filmic(vec3 color, float white) {
  93. float A = 0.15;
  94. float B = 0.50;
  95. float C = 0.10;
  96. float D = 0.20;
  97. float E = 0.02;
  98. float F = 0.30;
  99. float W = 11.2;
  100. vec3 coltn = ((color * (A * color + C * B) + D * E) / (color * (A * color + B) + D * F)) - E / F;
  101. float whitetn = ((white * (A * white + C * B) + D * E) / (white * (A * white + B) + D * F)) - E / F;
  102. return coltn / whitetn;
  103. }
  104. vec3 tonemap_aces(vec3 color) {
  105. float a = 2.51f;
  106. float b = 0.03f;
  107. float c = 2.43f;
  108. float d = 0.59f;
  109. float e = 0.14f;
  110. return color = clamp((color * (a * color + b)) / (color * (c * color + d) + e), vec3(0.0), vec3(1.0));
  111. }
  112. vec3 tonemap_reindhart(vec3 color, float white) {
  113. return (color * (1.0 + (color / (white)))) / (1.0 + color);
  114. }
  115. void main() {
  116. vec4 color = textureLod(source, uv_interp, 0.0);
  117. #ifdef USE_AUTO_EXPOSURE
  118. color /= texelFetch(source_auto_exposure, ivec2(0, 0), 0).r / auto_exposure_grey;
  119. #endif
  120. color *= exposure;
  121. #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)
  122. #define USING_GLOW
  123. #endif
  124. #if defined(USING_GLOW)
  125. vec3 glow = vec3(0.0);
  126. #ifdef USE_GLOW_LEVEL1
  127. glow += GLOW_TEXTURE_SAMPLE(source_glow, uv_interp, 1).rgb;
  128. #endif
  129. #ifdef USE_GLOW_LEVEL2
  130. glow += GLOW_TEXTURE_SAMPLE(source_glow, uv_interp, 2).rgb;
  131. #endif
  132. #ifdef USE_GLOW_LEVEL3
  133. glow += GLOW_TEXTURE_SAMPLE(source_glow, uv_interp, 3).rgb;
  134. #endif
  135. #ifdef USE_GLOW_LEVEL4
  136. glow += GLOW_TEXTURE_SAMPLE(source_glow, uv_interp, 4).rgb;
  137. #endif
  138. #ifdef USE_GLOW_LEVEL5
  139. glow += GLOW_TEXTURE_SAMPLE(source_glow, uv_interp, 5).rgb;
  140. #endif
  141. #ifdef USE_GLOW_LEVEL6
  142. glow += GLOW_TEXTURE_SAMPLE(source_glow, uv_interp, 6).rgb;
  143. #endif
  144. #ifdef USE_GLOW_LEVEL7
  145. glow += GLOW_TEXTURE_SAMPLE(source_glow, uv_interp, 7).rgb;
  146. #endif
  147. glow *= glow_intensity;
  148. #endif
  149. #ifdef USE_REINDHART_TONEMAPPER
  150. color.rgb = tonemap_reindhart(color.rgb, white);
  151. #if defined(USING_GLOW)
  152. glow = tonemap_reindhart(glow, white);
  153. #endif
  154. #endif
  155. #ifdef USE_FILMIC_TONEMAPPER
  156. color.rgb = tonemap_filmic(color.rgb, white);
  157. #if defined(USING_GLOW)
  158. glow = tonemap_filmic(glow, white);
  159. #endif
  160. #endif
  161. #ifdef USE_ACES_TONEMAPPER
  162. color.rgb = tonemap_aces(color.rgb);
  163. #if defined(USING_GLOW)
  164. glow = tonemap_aces(glow);
  165. #endif
  166. #endif
  167. //regular Linear -> SRGB conversion
  168. vec3 a = vec3(0.055);
  169. color.rgb = mix((vec3(1.0) + a) * pow(color.rgb, vec3(1.0 / 2.4)) - a, 12.92 * color.rgb, lessThan(color.rgb, vec3(0.0031308)));
  170. #if defined(USING_GLOW)
  171. glow = mix((vec3(1.0) + a) * pow(glow, vec3(1.0 / 2.4)) - a, 12.92 * glow, lessThan(glow, vec3(0.0031308)));
  172. #endif
  173. //glow needs to be added in SRGB space (together with image space effects)
  174. color.rgb = clamp(color.rgb, 0.0, 1.0);
  175. #if defined(USING_GLOW)
  176. glow = clamp(glow, 0.0, 1.0);
  177. #endif
  178. #ifdef USE_GLOW_REPLACE
  179. color.rgb = glow;
  180. #endif
  181. #ifdef USE_GLOW_SCREEN
  182. color.rgb = max((color.rgb + glow) - (color.rgb * glow), vec3(0.0));
  183. #endif
  184. #ifdef USE_GLOW_SOFTLIGHT
  185. {
  186. glow = (glow * 0.5) + 0.5;
  187. color.r = (glow.r <= 0.5) ? (color.r - (1.0 - 2.0 * glow.r) * color.r * (1.0 - color.r)) : (((glow.r > 0.5) && (color.r <= 0.25)) ? (color.r + (2.0 * glow.r - 1.0) * (4.0 * color.r * (4.0 * color.r + 1.0) * (color.r - 1.0) + 7.0 * color.r)) : (color.r + (2.0 * glow.r - 1.0) * (sqrt(color.r) - color.r)));
  188. color.g = (glow.g <= 0.5) ? (color.g - (1.0 - 2.0 * glow.g) * color.g * (1.0 - color.g)) : (((glow.g > 0.5) && (color.g <= 0.25)) ? (color.g + (2.0 * glow.g - 1.0) * (4.0 * color.g * (4.0 * color.g + 1.0) * (color.g - 1.0) + 7.0 * color.g)) : (color.g + (2.0 * glow.g - 1.0) * (sqrt(color.g) - color.g)));
  189. color.b = (glow.b <= 0.5) ? (color.b - (1.0 - 2.0 * glow.b) * color.b * (1.0 - color.b)) : (((glow.b > 0.5) && (color.b <= 0.25)) ? (color.b + (2.0 * glow.b - 1.0) * (4.0 * color.b * (4.0 * color.b + 1.0) * (color.b - 1.0) + 7.0 * color.b)) : (color.b + (2.0 * glow.b - 1.0) * (sqrt(color.b) - color.b)));
  190. }
  191. #endif
  192. #if defined(USING_GLOW) && !defined(USE_GLOW_SCREEN) && !defined(USE_GLOW_SOFTLIGHT) && !defined(USE_GLOW_REPLACE)
  193. //additive
  194. color.rgb += glow;
  195. #endif
  196. #ifdef USE_BCS
  197. color.rgb = mix(vec3(0.0), color.rgb, bcs.x);
  198. color.rgb = mix(vec3(0.5), color.rgb, bcs.y);
  199. color.rgb = mix(vec3(dot(vec3(1.0), color.rgb) * 0.33333), color.rgb, bcs.z);
  200. #endif
  201. #ifdef USE_COLOR_CORRECTION
  202. color.r = texture(color_correction, vec2(color.r, 0.0)).r;
  203. color.g = texture(color_correction, vec2(color.g, 0.0)).g;
  204. color.b = texture(color_correction, vec2(color.b, 0.0)).b;
  205. #endif
  206. frag_color = vec4(color.rgb, 1.0);
  207. }