tonemap.glsl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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_FXAA
  37. uniform vec2 pixel_size;
  38. #endif
  39. #ifdef USE_COLOR_CORRECTION
  40. uniform sampler2D color_correction; //texunit:3
  41. #endif
  42. layout(location = 0) out vec4 frag_color;
  43. #ifdef USE_GLOW_FILTER_BICUBIC
  44. // w0, w1, w2, and w3 are the four cubic B-spline basis functions
  45. float w0(float a) {
  46. return (1.0f / 6.0f) * (a * (a * (-a + 3.0f) - 3.0f) + 1.0f);
  47. }
  48. float w1(float a) {
  49. return (1.0f / 6.0f) * (a * a * (3.0f * a - 6.0f) + 4.0f);
  50. }
  51. float w2(float a) {
  52. return (1.0f / 6.0f) * (a * (a * (-3.0f * a + 3.0f) + 3.0f) + 1.0f);
  53. }
  54. float w3(float a) {
  55. return (1.0f / 6.0f) * (a * a * a);
  56. }
  57. // g0 and g1 are the two amplitude functions
  58. float g0(float a) {
  59. return w0(a) + w1(a);
  60. }
  61. float g1(float a) {
  62. return w2(a) + w3(a);
  63. }
  64. // h0 and h1 are the two offset functions
  65. float h0(float a) {
  66. return -1.0f + w1(a) / (w0(a) + w1(a));
  67. }
  68. float h1(float a) {
  69. return 1.0f + w3(a) / (w2(a) + w3(a));
  70. }
  71. uniform ivec2 glow_texture_size;
  72. vec4 texture2D_bicubic(sampler2D tex, vec2 uv, int p_lod) {
  73. float lod = float(p_lod);
  74. vec2 tex_size = vec2(glow_texture_size >> p_lod);
  75. vec2 texel_size = vec2(1.0f) / tex_size;
  76. uv = uv * tex_size + vec2(0.5f);
  77. vec2 iuv = floor(uv);
  78. vec2 fuv = fract(uv);
  79. float g0x = g0(fuv.x);
  80. float g1x = g1(fuv.x);
  81. float h0x = h0(fuv.x);
  82. float h1x = h1(fuv.x);
  83. float h0y = h0(fuv.y);
  84. float h1y = h1(fuv.y);
  85. vec2 p0 = (vec2(iuv.x + h0x, iuv.y + h0y) - vec2(0.5f)) * texel_size;
  86. vec2 p1 = (vec2(iuv.x + h1x, iuv.y + h0y) - vec2(0.5f)) * texel_size;
  87. vec2 p2 = (vec2(iuv.x + h0x, iuv.y + h1y) - vec2(0.5f)) * texel_size;
  88. vec2 p3 = (vec2(iuv.x + h1x, iuv.y + h1y) - vec2(0.5f)) * texel_size;
  89. return (g0(fuv.y) * (g0x * textureLod(tex, p0, lod) + g1x * textureLod(tex, p1, lod))) +
  90. (g1(fuv.y) * (g0x * textureLod(tex, p2, lod) + g1x * textureLod(tex, p3, lod)));
  91. }
  92. #define GLOW_TEXTURE_SAMPLE(m_tex, m_uv, m_lod) texture2D_bicubic(m_tex, m_uv, m_lod)
  93. #else
  94. #define GLOW_TEXTURE_SAMPLE(m_tex, m_uv, m_lod) textureLod(m_tex, m_uv, float(m_lod))
  95. #endif
  96. vec3 tonemap_filmic(vec3 color, float white) {
  97. // exposure bias: input scale (color *= bias, white *= bias) to make the brightness consistent with other tonemappers
  98. // also useful to scale the input to the range that the tonemapper is designed for (some require very high input values)
  99. // has no effect on the curve's general shape or visual properties
  100. const float exposure_bias = 2.0f;
  101. const float A = 0.22f * exposure_bias * exposure_bias; // bias baked into constants for performance
  102. const float B = 0.30f * exposure_bias;
  103. const float C = 0.10f;
  104. const float D = 0.20f;
  105. const float E = 0.01f;
  106. const float F = 0.30f;
  107. vec3 color_tonemapped = ((color * (A * color + C * B) + D * E) / (color * (A * color + B) + D * F)) - E / F;
  108. float white_tonemapped = ((white * (A * white + C * B) + D * E) / (white * (A * white + B) + D * F)) - E / F;
  109. return clamp(color_tonemapped / white_tonemapped, vec3(0.0f), vec3(1.0f));
  110. }
  111. vec3 tonemap_aces(vec3 color, float white) {
  112. const float exposure_bias = 0.85f;
  113. const float A = 2.51f * exposure_bias * exposure_bias;
  114. const float B = 0.03f * exposure_bias;
  115. const float C = 2.43f * exposure_bias * exposure_bias;
  116. const float D = 0.59f * exposure_bias;
  117. const float E = 0.14f;
  118. vec3 color_tonemapped = (color * (A * color + B)) / (color * (C * color + D) + E);
  119. float white_tonemapped = (white * (A * white + B)) / (white * (C * white + D) + E);
  120. return clamp(color_tonemapped / white_tonemapped, vec3(0.0f), vec3(1.0f));
  121. }
  122. vec3 tonemap_reinhard(vec3 color, float white) {
  123. return clamp((white * color + color) / (color * white + white), vec3(0.0f), vec3(1.0f));
  124. }
  125. vec3 linear_to_srgb(vec3 color) { // convert linear rgb to srgb, assumes clamped input in range [0;1]
  126. const vec3 a = vec3(0.055f);
  127. 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)));
  128. }
  129. // inputs are LINEAR, If Linear tonemapping is selected no transform is performed else outputs are clamped [0, 1] color
  130. vec3 apply_tonemapping(vec3 color, float white) {
  131. #ifdef USE_REINHARD_TONEMAPPER
  132. return tonemap_reinhard(color, white);
  133. #endif
  134. #ifdef USE_FILMIC_TONEMAPPER
  135. return tonemap_filmic(color, white);
  136. #endif
  137. #ifdef USE_ACES_TONEMAPPER
  138. return tonemap_aces(color, white);
  139. #endif
  140. return color; // no other selected -> linear: no color transform applied
  141. }
  142. vec3 gather_glow(sampler2D tex, vec2 uv) { // sample all selected glow levels
  143. vec3 glow = vec3(0.0f);
  144. #ifdef USE_GLOW_LEVEL1
  145. glow += GLOW_TEXTURE_SAMPLE(tex, uv, 1).rgb;
  146. #endif
  147. #ifdef USE_GLOW_LEVEL2
  148. glow += GLOW_TEXTURE_SAMPLE(tex, uv, 2).rgb;
  149. #endif
  150. #ifdef USE_GLOW_LEVEL3
  151. glow += GLOW_TEXTURE_SAMPLE(tex, uv, 3).rgb;
  152. #endif
  153. #ifdef USE_GLOW_LEVEL4
  154. glow += GLOW_TEXTURE_SAMPLE(tex, uv, 4).rgb;
  155. #endif
  156. #ifdef USE_GLOW_LEVEL5
  157. glow += GLOW_TEXTURE_SAMPLE(tex, uv, 5).rgb;
  158. #endif
  159. #ifdef USE_GLOW_LEVEL6
  160. glow += GLOW_TEXTURE_SAMPLE(tex, uv, 6).rgb;
  161. #endif
  162. #ifdef USE_GLOW_LEVEL7
  163. glow += GLOW_TEXTURE_SAMPLE(tex, uv, 7).rgb;
  164. #endif
  165. return glow;
  166. }
  167. vec3 apply_glow(vec3 color, vec3 glow) { // apply glow using the selected blending mode
  168. #ifdef USE_GLOW_REPLACE
  169. color = glow;
  170. #endif
  171. #ifdef USE_GLOW_SCREEN
  172. //need color clamping
  173. color = clamp(color, vec3(0.0f), vec3(1.0f));
  174. color = max((color + glow) - (color * glow), vec3(0.0));
  175. #endif
  176. #ifdef USE_GLOW_SOFTLIGHT
  177. //need color clamping
  178. color = clamp(color, vec3(0.0f), vec3(1.0));
  179. glow = glow * vec3(0.5f) + vec3(0.5f);
  180. 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)));
  181. 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)));
  182. 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)));
  183. #endif
  184. #if !defined(USE_GLOW_SCREEN) && !defined(USE_GLOW_SOFTLIGHT) && !defined(USE_GLOW_REPLACE) // no other selected -> additive
  185. color += glow;
  186. #endif
  187. return color;
  188. }
  189. vec3 apply_bcs(vec3 color, vec3 bcs) {
  190. color = mix(vec3(0.0f), color, bcs.x);
  191. color = mix(vec3(0.5f), color, bcs.y);
  192. color = mix(vec3(dot(vec3(1.0f), color) * 0.33333f), color, bcs.z);
  193. return color;
  194. }
  195. vec3 apply_color_correction(vec3 color, sampler2D correction_tex) {
  196. color.r = texture(correction_tex, vec2(color.r, 0.0f)).r;
  197. color.g = texture(correction_tex, vec2(color.g, 0.0f)).g;
  198. color.b = texture(correction_tex, vec2(color.b, 0.0f)).b;
  199. return color;
  200. }
  201. vec3 apply_fxaa(vec3 color, float exposure, vec2 uv_interp, vec2 pixel_size) {
  202. const float FXAA_REDUCE_MIN = (1.0 / 128.0);
  203. const float FXAA_REDUCE_MUL = (1.0 / 8.0);
  204. const float FXAA_SPAN_MAX = 8.0;
  205. vec3 rgbNW = textureLod(source, uv_interp + vec2(-1.0, -1.0) * pixel_size, 0.0).xyz * exposure;
  206. vec3 rgbNE = textureLod(source, uv_interp + vec2(1.0, -1.0) * pixel_size, 0.0).xyz * exposure;
  207. vec3 rgbSW = textureLod(source, uv_interp + vec2(-1.0, 1.0) * pixel_size, 0.0).xyz * exposure;
  208. vec3 rgbSE = textureLod(source, uv_interp + vec2(1.0, 1.0) * pixel_size, 0.0).xyz * exposure;
  209. vec3 rgbM = color;
  210. vec3 luma = vec3(0.299, 0.587, 0.114);
  211. float lumaNW = dot(rgbNW, luma);
  212. float lumaNE = dot(rgbNE, luma);
  213. float lumaSW = dot(rgbSW, luma);
  214. float lumaSE = dot(rgbSE, luma);
  215. float lumaM = dot(rgbM, luma);
  216. float lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE)));
  217. float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE)));
  218. vec2 dir;
  219. dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE));
  220. dir.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE));
  221. float dirReduce = max((lumaNW + lumaNE + lumaSW + lumaSE) *
  222. (0.25 * FXAA_REDUCE_MUL),
  223. FXAA_REDUCE_MIN);
  224. float rcpDirMin = 1.0 / (min(abs(dir.x), abs(dir.y)) + dirReduce);
  225. dir = min(vec2(FXAA_SPAN_MAX, FXAA_SPAN_MAX),
  226. max(vec2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX),
  227. dir * rcpDirMin)) *
  228. pixel_size;
  229. vec3 rgbA = 0.5 * exposure * (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);
  230. vec3 rgbB = rgbA * 0.5 + 0.25 * exposure * (textureLod(source, uv_interp + dir * -0.5, 0.0).xyz + textureLod(source, uv_interp + dir * 0.5, 0.0).xyz);
  231. float lumaB = dot(rgbB, luma);
  232. if ((lumaB < lumaMin) || (lumaB > lumaMax)) {
  233. return rgbA;
  234. } else {
  235. return rgbB;
  236. }
  237. }
  238. // From http://alex.vlachos.com/graphics/Alex_Vlachos_Advanced_VR_Rendering_GDC2015.pdf
  239. // and https://www.shadertoy.com/view/MslGR8 (5th one starting from the bottom)
  240. // NOTE: `frag_coord` is in pixels (i.e. not normalized UV).
  241. vec3 screen_space_dither(vec2 frag_coord) {
  242. // Iestyn's RGB dither (7 asm instructions) from Portal 2 X360, slightly modified for VR.
  243. vec3 dither = vec3(dot(vec2(171.0, 231.0), frag_coord));
  244. dither.rgb = fract(dither.rgb / vec3(103.0, 71.0, 97.0));
  245. // Subtract 0.5 to avoid slightly brightening the whole viewport.
  246. return (dither.rgb - 0.5) / 255.0;
  247. }
  248. void main() {
  249. vec3 color = textureLod(source, uv_interp, 0.0f).rgb;
  250. // Exposure
  251. float full_exposure = exposure;
  252. #ifdef USE_AUTO_EXPOSURE
  253. full_exposure /= texelFetch(source_auto_exposure, ivec2(0, 0), 0).r / auto_exposure_grey;
  254. #endif
  255. color *= full_exposure;
  256. #ifdef USE_FXAA
  257. // FXAA must be applied before tonemapping.
  258. color = apply_fxaa(color, full_exposure, uv_interp, pixel_size);
  259. #endif
  260. #ifdef USE_DEBANDING
  261. // For best results, debanding should be done before tonemapping.
  262. // Otherwise, we're adding noise to an already-quantized image.
  263. color += screen_space_dither(gl_FragCoord.xy);
  264. #endif
  265. // Early Tonemap & SRGB Conversion; note that Linear tonemapping does not clamp to [0, 1]; some operations below expect a [0, 1] range and will clamp
  266. // Ensure color values are positive.
  267. // They can be negative in the case of negative lights, which leads to undesired behavior.
  268. color = apply_tonemapping(max(vec3(0.0), color), white);
  269. #ifdef KEEP_3D_LINEAR
  270. // leave color as is (-> don't convert to SRGB)
  271. #else
  272. //need color clamping
  273. color = clamp(color, vec3(0.0f), vec3(1.0f));
  274. color = linear_to_srgb(color); // regular linear -> SRGB conversion (needs clamped values)
  275. #endif
  276. // Glow
  277. #ifdef USING_GLOW
  278. vec3 glow = gather_glow(source_glow, uv_interp) * glow_intensity;
  279. // high dynamic range -> SRGB
  280. glow = apply_tonemapping(glow, white);
  281. glow = clamp(glow, vec3(0.0f), vec3(1.0f));
  282. glow = linear_to_srgb(glow);
  283. color = apply_glow(color, glow);
  284. #endif
  285. // Additional effects
  286. #ifdef USE_BCS
  287. color = apply_bcs(color, bcs);
  288. #endif
  289. #ifdef USE_COLOR_CORRECTION
  290. color = apply_color_correction(color, color_correction);
  291. #endif
  292. frag_color = vec4(color, 1.0f);
  293. }