tonemap.glsl 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /* clang-format off */
  2. [vertex]
  3. #ifdef USE_GLES_OVER_GL
  4. #define lowp
  5. #define mediump
  6. #define highp
  7. #else
  8. precision highp float;
  9. precision highp int;
  10. #endif
  11. layout(location = 0) in vec2 vertex_attrib;
  12. /* clang-format on */
  13. layout(location = 4) in vec2 uv_in;
  14. out vec2 uv_interp;
  15. void main() {
  16. gl_Position = vec4(vertex_attrib, 0.0, 1.0);
  17. uv_interp = uv_in;
  18. }
  19. /* clang-format off */
  20. [fragment]
  21. #ifdef USE_GLES_OVER_GL
  22. #define lowp
  23. #define mediump
  24. #define highp
  25. #else
  26. #if defined(USE_HIGHP_PRECISION)
  27. precision highp float;
  28. precision highp int;
  29. #else
  30. precision mediump float;
  31. precision mediump int;
  32. #endif
  33. #endif
  34. in vec2 uv_interp;
  35. /* clang-format on */
  36. layout(location = 0) out vec4 frag_color;
  37. uniform highp sampler2D source; //texunit:0
  38. #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)
  39. #define USING_GLOW // only use glow when at least one glow level is selected
  40. #ifdef USE_MULTI_TEXTURE_GLOW
  41. uniform highp sampler2D source_glow1; //texunit:2
  42. uniform highp sampler2D source_glow2; //texunit:3
  43. uniform highp sampler2D source_glow3; //texunit:4
  44. uniform highp sampler2D source_glow4; //texunit:5
  45. uniform highp sampler2D source_glow5; //texunit:6
  46. uniform highp sampler2D source_glow6; //texunit:7
  47. #ifdef USE_GLOW_LEVEL7
  48. uniform highp sampler2D source_glow7; //texunit:8
  49. #endif
  50. #else
  51. uniform highp sampler2D source_glow; //texunit:2
  52. #endif
  53. uniform highp float glow_intensity;
  54. #endif
  55. #ifdef USE_BCS
  56. uniform vec3 bcs;
  57. #endif
  58. #ifdef USE_FXAA
  59. uniform vec2 pixel_size;
  60. #endif
  61. #ifdef USE_COLOR_CORRECTION
  62. uniform sampler2D color_correction; //texunit:1
  63. #endif
  64. #ifdef USE_GLOW_FILTER_BICUBIC
  65. // w0, w1, w2, and w3 are the four cubic B-spline basis functions
  66. float w0(float a) {
  67. return (1.0 / 6.0) * (a * (a * (-a + 3.0) - 3.0) + 1.0);
  68. }
  69. float w1(float a) {
  70. return (1.0 / 6.0) * (a * a * (3.0 * a - 6.0) + 4.0);
  71. }
  72. float w2(float a) {
  73. return (1.0 / 6.0) * (a * (a * (-3.0 * a + 3.0) + 3.0) + 1.0);
  74. }
  75. float w3(float a) {
  76. return (1.0 / 6.0) * (a * a * a);
  77. }
  78. // g0 and g1 are the two amplitude functions
  79. float g0(float a) {
  80. return w0(a) + w1(a);
  81. }
  82. float g1(float a) {
  83. return w2(a) + w3(a);
  84. }
  85. // h0 and h1 are the two offset functions
  86. float h0(float a) {
  87. return -1.0 + w1(a) / (w0(a) + w1(a));
  88. }
  89. float h1(float a) {
  90. return 1.0 + w3(a) / (w2(a) + w3(a));
  91. }
  92. uniform ivec2 glow_texture_size;
  93. vec4 texture_bicubic(sampler2D tex, vec2 uv, int p_lod) {
  94. float lod = float(p_lod);
  95. vec2 tex_size = vec2(glow_texture_size >> p_lod);
  96. vec2 texel_size = vec2(1.0) / tex_size;
  97. uv = uv * tex_size + vec2(0.5);
  98. vec2 iuv = floor(uv);
  99. vec2 fuv = fract(uv);
  100. float g0x = g0(fuv.x);
  101. float g1x = g1(fuv.x);
  102. float h0x = h0(fuv.x);
  103. float h1x = h1(fuv.x);
  104. float h0y = h0(fuv.y);
  105. float h1y = h1(fuv.y);
  106. vec2 p0 = (vec2(iuv.x + h0x, iuv.y + h0y) - vec2(0.5)) * texel_size;
  107. vec2 p1 = (vec2(iuv.x + h1x, iuv.y + h0y) - vec2(0.5)) * texel_size;
  108. vec2 p2 = (vec2(iuv.x + h0x, iuv.y + h1y) - vec2(0.5)) * texel_size;
  109. vec2 p3 = (vec2(iuv.x + h1x, iuv.y + h1y) - vec2(0.5)) * texel_size;
  110. return (g0(fuv.y) * (g0x * textureLod(tex, p0, lod) + g1x * textureLod(tex, p1, lod))) +
  111. (g1(fuv.y) * (g0x * textureLod(tex, p2, lod) + g1x * textureLod(tex, p3, lod)));
  112. }
  113. #define GLOW_TEXTURE_SAMPLE(m_tex, m_uv, m_lod) texture_bicubic(m_tex, m_uv, m_lod)
  114. #else //!USE_GLOW_FILTER_BICUBIC
  115. #define GLOW_TEXTURE_SAMPLE(m_tex, m_uv, m_lod) textureLod(m_tex, m_uv, float(m_lod))
  116. #endif //USE_GLOW_FILTER_BICUBIC
  117. vec3 apply_glow(vec3 color, vec3 glow) { // apply glow using the selected blending mode
  118. #ifdef USE_GLOW_REPLACE
  119. color = glow;
  120. #endif
  121. #ifdef USE_GLOW_SCREEN
  122. color = max((color + glow) - (color * glow), vec3(0.0));
  123. #endif
  124. #ifdef USE_GLOW_SOFTLIGHT
  125. glow = glow * vec3(0.5) + vec3(0.5);
  126. 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)));
  127. 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)));
  128. 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)));
  129. #endif
  130. #if !defined(USE_GLOW_SCREEN) && !defined(USE_GLOW_SOFTLIGHT) && !defined(USE_GLOW_REPLACE) // no other selected -> additive
  131. color += glow;
  132. #endif
  133. return color;
  134. }
  135. vec3 apply_bcs(vec3 color, vec3 bcs) {
  136. color = mix(vec3(0.0), color, bcs.x);
  137. color = mix(vec3(0.5), color, bcs.y);
  138. color = mix(vec3(dot(vec3(1.0), color) * 0.33333), color, bcs.z);
  139. return color;
  140. }
  141. vec3 apply_color_correction(vec3 color, sampler2D correction_tex) {
  142. color.r = texture(correction_tex, vec2(color.r, 0.0)).r;
  143. color.g = texture(correction_tex, vec2(color.g, 0.0)).g;
  144. color.b = texture(correction_tex, vec2(color.b, 0.0)).b;
  145. return color;
  146. }
  147. vec3 apply_fxaa(vec3 color, vec2 uv_interp, vec2 pixel_size) {
  148. const float FXAA_REDUCE_MIN = (1.0 / 128.0);
  149. const float FXAA_REDUCE_MUL = (1.0 / 8.0);
  150. const float FXAA_SPAN_MAX = 8.0;
  151. vec3 rgbNW = textureLod(source, uv_interp + vec2(-1.0, -1.0) * pixel_size, 0.0).xyz;
  152. vec3 rgbNE = textureLod(source, uv_interp + vec2(1.0, -1.0) * pixel_size, 0.0).xyz;
  153. vec3 rgbSW = textureLod(source, uv_interp + vec2(-1.0, 1.0) * pixel_size, 0.0).xyz;
  154. vec3 rgbSE = textureLod(source, uv_interp + vec2(1.0, 1.0) * pixel_size, 0.0).xyz;
  155. vec3 rgbM = color;
  156. vec3 luma = vec3(0.299, 0.587, 0.114);
  157. float lumaNW = dot(rgbNW, luma);
  158. float lumaNE = dot(rgbNE, luma);
  159. float lumaSW = dot(rgbSW, luma);
  160. float lumaSE = dot(rgbSE, luma);
  161. float lumaM = dot(rgbM, luma);
  162. float lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE)));
  163. float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE)));
  164. vec2 dir;
  165. dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE));
  166. dir.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE));
  167. float dirReduce = max((lumaNW + lumaNE + lumaSW + lumaSE) *
  168. (0.25 * FXAA_REDUCE_MUL),
  169. FXAA_REDUCE_MIN);
  170. float rcpDirMin = 1.0 / (min(abs(dir.x), abs(dir.y)) + dirReduce);
  171. dir = min(vec2(FXAA_SPAN_MAX, FXAA_SPAN_MAX),
  172. max(vec2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX),
  173. dir * rcpDirMin)) *
  174. pixel_size;
  175. vec3 rgbA = 0.5 * (textureLod(source, uv_interp + dir * (1.0 / 3.0 - 0.5), 0.0).xyz + textureLod(source, uv_interp + dir * (2.0 / 3.0 - 0.5), 0.0).xyz);
  176. vec3 rgbB = rgbA * 0.5 + 0.25 * (textureLod(source, uv_interp + dir * -0.5, 0.0).xyz + textureLod(source, uv_interp + dir * 0.5, 0.0).xyz);
  177. float lumaB = dot(rgbB, luma);
  178. if ((lumaB < lumaMin) || (lumaB > lumaMax)) {
  179. return rgbA;
  180. } else {
  181. return rgbB;
  182. }
  183. }
  184. void main() {
  185. vec3 color = textureLod(source, uv_interp, 0.0).rgb;
  186. #ifdef USE_FXAA
  187. color = apply_fxaa(color, uv_interp, pixel_size);
  188. #endif
  189. // Glow
  190. #ifdef USING_GLOW
  191. vec3 glow = vec3(0.0);
  192. #ifdef USE_MULTI_TEXTURE_GLOW
  193. #ifdef USE_GLOW_LEVEL1
  194. glow += GLOW_TEXTURE_SAMPLE(source_glow1, uv_interp, 0).rgb;
  195. #ifdef USE_GLOW_LEVEL2
  196. glow += GLOW_TEXTURE_SAMPLE(source_glow2, uv_interp, 0).rgb;
  197. #ifdef USE_GLOW_LEVEL3
  198. glow += GLOW_TEXTURE_SAMPLE(source_glow3, uv_interp, 0).rgb;
  199. #ifdef USE_GLOW_LEVEL4
  200. glow += GLOW_TEXTURE_SAMPLE(source_glow4, uv_interp, 0).rgb;
  201. #ifdef USE_GLOW_LEVEL5
  202. glow += GLOW_TEXTURE_SAMPLE(source_glow5, uv_interp, 0).rgb;
  203. #ifdef USE_GLOW_LEVEL6
  204. glow += GLOW_TEXTURE_SAMPLE(source_glow6, uv_interp, 0).rgb;
  205. #ifdef USE_GLOW_LEVEL7
  206. glow += GLOW_TEXTURE_SAMPLE(source_glow7, uv_interp, 0).rgb;
  207. #endif
  208. #endif
  209. #endif
  210. #endif
  211. #endif
  212. #endif
  213. #endif
  214. #else
  215. #ifdef USE_GLOW_LEVEL1
  216. glow += GLOW_TEXTURE_SAMPLE(source_glow, uv_interp, 1).rgb;
  217. #endif
  218. #ifdef USE_GLOW_LEVEL2
  219. glow += GLOW_TEXTURE_SAMPLE(source_glow, uv_interp, 2).rgb;
  220. #endif
  221. #ifdef USE_GLOW_LEVEL3
  222. glow += GLOW_TEXTURE_SAMPLE(source_glow, uv_interp, 3).rgb;
  223. #endif
  224. #ifdef USE_GLOW_LEVEL4
  225. glow += GLOW_TEXTURE_SAMPLE(source_glow, uv_interp, 4).rgb;
  226. #endif
  227. #ifdef USE_GLOW_LEVEL5
  228. glow += GLOW_TEXTURE_SAMPLE(source_glow, uv_interp, 5).rgb;
  229. #endif
  230. #ifdef USE_GLOW_LEVEL6
  231. glow += GLOW_TEXTURE_SAMPLE(source_glow, uv_interp, 6).rgb;
  232. #endif
  233. #ifdef USE_GLOW_LEVEL7
  234. glow += GLOW_TEXTURE_SAMPLE(source_glow, uv_interp, 7).rgb;
  235. #endif
  236. #endif //USE_MULTI_TEXTURE_GLOW
  237. glow *= glow_intensity;
  238. color = apply_glow(color, glow);
  239. #endif
  240. // Additional effects
  241. #ifdef USE_BCS
  242. color = apply_bcs(color, bcs);
  243. #endif
  244. #ifdef USE_COLOR_CORRECTION
  245. color = apply_color_correction(color, color_correction);
  246. #endif
  247. frag_color = vec4(color, 1.0);
  248. }