tonemap.glsl 9.8 KB

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