tonemap.glsl 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. #[vertex]
  2. #version 450
  3. #VERSION_DEFINES
  4. #ifdef MULTIVIEW
  5. #ifdef has_VK_KHR_multiview
  6. #extension GL_EXT_multiview : enable
  7. #endif
  8. #endif
  9. layout(location = 0) out vec2 uv_interp;
  10. void main() {
  11. vec2 base_arr[4] = vec2[](vec2(0.0, 0.0), vec2(0.0, 1.0), vec2(1.0, 1.0), vec2(1.0, 0.0));
  12. uv_interp = base_arr[gl_VertexIndex];
  13. gl_Position = vec4(uv_interp * 2.0 - 1.0, 0.0, 1.0);
  14. }
  15. #[fragment]
  16. #version 450
  17. #VERSION_DEFINES
  18. #ifdef MULTIVIEW
  19. #ifdef has_VK_KHR_multiview
  20. #extension GL_EXT_multiview : enable
  21. #define ViewIndex gl_ViewIndex
  22. #else // has_VK_KHR_multiview
  23. #define ViewIndex 0
  24. #endif // has_VK_KHR_multiview
  25. #endif //MULTIVIEW
  26. layout(location = 0) in vec2 uv_interp;
  27. #ifdef SUBPASS
  28. layout(input_attachment_index = 0, set = 0, binding = 0) uniform subpassInput input_color;
  29. #elif defined(MULTIVIEW)
  30. layout(set = 0, binding = 0) uniform sampler2DArray source_color;
  31. #else
  32. layout(set = 0, binding = 0) uniform sampler2D source_color;
  33. #endif
  34. layout(set = 1, binding = 0) uniform sampler2D source_auto_exposure;
  35. #ifdef MULTIVIEW
  36. layout(set = 2, binding = 0) uniform sampler2DArray source_glow;
  37. #else
  38. layout(set = 2, binding = 0) uniform sampler2D source_glow;
  39. #endif
  40. layout(set = 2, binding = 1) uniform sampler2D glow_map;
  41. #ifdef USE_1D_LUT
  42. layout(set = 3, binding = 0) uniform sampler2D source_color_correction;
  43. #else
  44. layout(set = 3, binding = 0) uniform sampler3D source_color_correction;
  45. #endif
  46. layout(push_constant, std430) uniform Params {
  47. vec3 bcs;
  48. bool use_bcs;
  49. bool use_glow;
  50. bool use_auto_exposure;
  51. bool use_color_correction;
  52. uint tonemapper;
  53. uvec2 glow_texture_size;
  54. float glow_intensity;
  55. float glow_map_strength;
  56. uint glow_mode;
  57. float glow_levels[7];
  58. float exposure;
  59. float white;
  60. float auto_exposure_scale;
  61. float luminance_multiplier;
  62. vec2 pixel_size;
  63. bool use_fxaa;
  64. bool use_debanding;
  65. }
  66. params;
  67. layout(location = 0) out vec4 frag_color;
  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.0f / 6.0f) * (a * (a * (-a + 3.0f) - 3.0f) + 1.0f);
  72. }
  73. float w1(float a) {
  74. return (1.0f / 6.0f) * (a * a * (3.0f * a - 6.0f) + 4.0f);
  75. }
  76. float w2(float a) {
  77. return (1.0f / 6.0f) * (a * (a * (-3.0f * a + 3.0f) + 3.0f) + 1.0f);
  78. }
  79. float w3(float a) {
  80. return (1.0f / 6.0f) * (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.0f + w1(a) / (w0(a) + w1(a));
  92. }
  93. float h1(float a) {
  94. return 1.0f + w3(a) / (w2(a) + w3(a));
  95. }
  96. #ifdef MULTIVIEW
  97. vec4 texture2D_bicubic(sampler2DArray tex, vec2 uv, int p_lod) {
  98. float lod = float(p_lod);
  99. vec2 tex_size = vec2(params.glow_texture_size >> p_lod);
  100. vec2 pixel_size = vec2(1.0f) / tex_size;
  101. uv = uv * tex_size + vec2(0.5f);
  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. vec3 p0 = vec3((vec2(iuv.x + h0x, iuv.y + h0y) - vec2(0.5f)) * pixel_size, ViewIndex);
  111. vec3 p1 = vec3((vec2(iuv.x + h1x, iuv.y + h0y) - vec2(0.5f)) * pixel_size, ViewIndex);
  112. vec3 p2 = vec3((vec2(iuv.x + h0x, iuv.y + h1y) - vec2(0.5f)) * pixel_size, ViewIndex);
  113. vec3 p3 = vec3((vec2(iuv.x + h1x, iuv.y + h1y) - vec2(0.5f)) * pixel_size, ViewIndex);
  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) texture2D_bicubic(m_tex, m_uv, m_lod)
  118. #else // MULTIVIEW
  119. vec4 texture2D_bicubic(sampler2D tex, vec2 uv, int p_lod) {
  120. float lod = float(p_lod);
  121. vec2 tex_size = vec2(params.glow_texture_size >> p_lod);
  122. vec2 pixel_size = vec2(1.0f) / tex_size;
  123. uv = uv * tex_size + vec2(0.5f);
  124. vec2 iuv = floor(uv);
  125. vec2 fuv = fract(uv);
  126. float g0x = g0(fuv.x);
  127. float g1x = g1(fuv.x);
  128. float h0x = h0(fuv.x);
  129. float h1x = h1(fuv.x);
  130. float h0y = h0(fuv.y);
  131. float h1y = h1(fuv.y);
  132. vec2 p0 = (vec2(iuv.x + h0x, iuv.y + h0y) - vec2(0.5f)) * pixel_size;
  133. vec2 p1 = (vec2(iuv.x + h1x, iuv.y + h0y) - vec2(0.5f)) * pixel_size;
  134. vec2 p2 = (vec2(iuv.x + h0x, iuv.y + h1y) - vec2(0.5f)) * pixel_size;
  135. vec2 p3 = (vec2(iuv.x + h1x, iuv.y + h1y) - vec2(0.5f)) * pixel_size;
  136. return (g0(fuv.y) * (g0x * textureLod(tex, p0, lod) + g1x * textureLod(tex, p1, lod))) +
  137. (g1(fuv.y) * (g0x * textureLod(tex, p2, lod) + g1x * textureLod(tex, p3, lod)));
  138. }
  139. #define GLOW_TEXTURE_SAMPLE(m_tex, m_uv, m_lod) texture2D_bicubic(m_tex, m_uv, m_lod)
  140. #endif // !MULTIVIEW
  141. #else // USE_GLOW_FILTER_BICUBIC
  142. #ifdef MULTIVIEW
  143. #define GLOW_TEXTURE_SAMPLE(m_tex, m_uv, m_lod) textureLod(m_tex, vec3(m_uv, ViewIndex), float(m_lod))
  144. #else // MULTIVIEW
  145. #define GLOW_TEXTURE_SAMPLE(m_tex, m_uv, m_lod) textureLod(m_tex, m_uv, float(m_lod))
  146. #endif // !MULTIVIEW
  147. #endif // !USE_GLOW_FILTER_BICUBIC
  148. vec3 tonemap_filmic(vec3 color, float white) {
  149. // exposure bias: input scale (color *= bias, white *= bias) to make the brightness consistent with other tonemappers
  150. // also useful to scale the input to the range that the tonemapper is designed for (some require very high input values)
  151. // has no effect on the curve's general shape or visual properties
  152. const float exposure_bias = 2.0f;
  153. const float A = 0.22f * exposure_bias * exposure_bias; // bias baked into constants for performance
  154. const float B = 0.30f * exposure_bias;
  155. const float C = 0.10f;
  156. const float D = 0.20f;
  157. const float E = 0.01f;
  158. const float F = 0.30f;
  159. vec3 color_tonemapped = ((color * (A * color + C * B) + D * E) / (color * (A * color + B) + D * F)) - E / F;
  160. float white_tonemapped = ((white * (A * white + C * B) + D * E) / (white * (A * white + B) + D * F)) - E / F;
  161. return color_tonemapped / white_tonemapped;
  162. }
  163. // Adapted from https://github.com/TheRealMJP/BakingLab/blob/master/BakingLab/ACES.hlsl
  164. // (MIT License).
  165. vec3 tonemap_aces(vec3 color, float white) {
  166. const float exposure_bias = 1.8f;
  167. const float A = 0.0245786f;
  168. const float B = 0.000090537f;
  169. const float C = 0.983729f;
  170. const float D = 0.432951f;
  171. const float E = 0.238081f;
  172. // Exposure bias baked into transform to save shader instructions. Equivalent to `color *= exposure_bias`
  173. const mat3 rgb_to_rrt = mat3(
  174. vec3(0.59719f * exposure_bias, 0.35458f * exposure_bias, 0.04823f * exposure_bias),
  175. vec3(0.07600f * exposure_bias, 0.90834f * exposure_bias, 0.01566f * exposure_bias),
  176. vec3(0.02840f * exposure_bias, 0.13383f * exposure_bias, 0.83777f * exposure_bias));
  177. const mat3 odt_to_rgb = mat3(
  178. vec3(1.60475f, -0.53108f, -0.07367f),
  179. vec3(-0.10208f, 1.10813f, -0.00605f),
  180. vec3(-0.00327f, -0.07276f, 1.07602f));
  181. color *= rgb_to_rrt;
  182. vec3 color_tonemapped = (color * (color + A) - B) / (color * (C * color + D) + E);
  183. color_tonemapped *= odt_to_rgb;
  184. white *= exposure_bias;
  185. float white_tonemapped = (white * (white + A) - B) / (white * (C * white + D) + E);
  186. return color_tonemapped / white_tonemapped;
  187. }
  188. vec3 tonemap_reinhard(vec3 color, float white) {
  189. return (white * color + color) / (color * white + white);
  190. }
  191. vec3 linear_to_srgb(vec3 color) {
  192. //if going to srgb, clamp from 0 to 1.
  193. color = clamp(color, vec3(0.0), vec3(1.0));
  194. const vec3 a = vec3(0.055f);
  195. 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)));
  196. }
  197. #define TONEMAPPER_LINEAR 0
  198. #define TONEMAPPER_REINHARD 1
  199. #define TONEMAPPER_FILMIC 2
  200. #define TONEMAPPER_ACES 3
  201. vec3 apply_tonemapping(vec3 color, float white) { // inputs are LINEAR, always outputs clamped [0;1] color
  202. // Ensure color values passed to tonemappers are positive.
  203. // They can be negative in the case of negative lights, which leads to undesired behavior.
  204. if (params.tonemapper == TONEMAPPER_LINEAR) {
  205. return color;
  206. } else if (params.tonemapper == TONEMAPPER_REINHARD) {
  207. return tonemap_reinhard(max(vec3(0.0f), color), white);
  208. } else if (params.tonemapper == TONEMAPPER_FILMIC) {
  209. return tonemap_filmic(max(vec3(0.0f), color), white);
  210. } else { // TONEMAPPER_ACES
  211. return tonemap_aces(max(vec3(0.0f), color), white);
  212. }
  213. }
  214. #ifdef MULTIVIEW
  215. vec3 gather_glow(sampler2DArray tex, vec2 uv) { // sample all selected glow levels, view is added to uv later
  216. #else
  217. vec3 gather_glow(sampler2D tex, vec2 uv) { // sample all selected glow levels
  218. #endif // defined(MULTIVIEW)
  219. vec3 glow = vec3(0.0f);
  220. if (params.glow_levels[0] > 0.0001) {
  221. glow += GLOW_TEXTURE_SAMPLE(tex, uv, 0).rgb * params.glow_levels[0];
  222. }
  223. if (params.glow_levels[1] > 0.0001) {
  224. glow += GLOW_TEXTURE_SAMPLE(tex, uv, 1).rgb * params.glow_levels[1];
  225. }
  226. if (params.glow_levels[2] > 0.0001) {
  227. glow += GLOW_TEXTURE_SAMPLE(tex, uv, 2).rgb * params.glow_levels[2];
  228. }
  229. if (params.glow_levels[3] > 0.0001) {
  230. glow += GLOW_TEXTURE_SAMPLE(tex, uv, 3).rgb * params.glow_levels[3];
  231. }
  232. if (params.glow_levels[4] > 0.0001) {
  233. glow += GLOW_TEXTURE_SAMPLE(tex, uv, 4).rgb * params.glow_levels[4];
  234. }
  235. if (params.glow_levels[5] > 0.0001) {
  236. glow += GLOW_TEXTURE_SAMPLE(tex, uv, 5).rgb * params.glow_levels[5];
  237. }
  238. if (params.glow_levels[6] > 0.0001) {
  239. glow += GLOW_TEXTURE_SAMPLE(tex, uv, 6).rgb * params.glow_levels[6];
  240. }
  241. return glow;
  242. }
  243. #define GLOW_MODE_ADD 0
  244. #define GLOW_MODE_SCREEN 1
  245. #define GLOW_MODE_SOFTLIGHT 2
  246. #define GLOW_MODE_REPLACE 3
  247. #define GLOW_MODE_MIX 4
  248. vec3 apply_glow(vec3 color, vec3 glow) { // apply glow using the selected blending mode
  249. if (params.glow_mode == GLOW_MODE_ADD) {
  250. return color + glow;
  251. } else if (params.glow_mode == GLOW_MODE_SCREEN) {
  252. //need color clamping
  253. return max((color + glow) - (color * glow), vec3(0.0));
  254. } else if (params.glow_mode == GLOW_MODE_SOFTLIGHT) {
  255. //need color clamping
  256. glow = glow * vec3(0.5f) + vec3(0.5f);
  257. 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)));
  258. 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)));
  259. 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)));
  260. return color;
  261. } else { //replace
  262. return glow;
  263. }
  264. }
  265. vec3 apply_bcs(vec3 color, vec3 bcs) {
  266. color = mix(vec3(0.0f), color, bcs.x);
  267. color = mix(vec3(0.5f), color, bcs.y);
  268. color = mix(vec3(dot(vec3(1.0f), color) * 0.33333f), color, bcs.z);
  269. return color;
  270. }
  271. #ifdef USE_1D_LUT
  272. vec3 apply_color_correction(vec3 color) {
  273. color.r = texture(source_color_correction, vec2(color.r, 0.0f)).r;
  274. color.g = texture(source_color_correction, vec2(color.g, 0.0f)).g;
  275. color.b = texture(source_color_correction, vec2(color.b, 0.0f)).b;
  276. return color;
  277. }
  278. #else
  279. vec3 apply_color_correction(vec3 color) {
  280. return textureLod(source_color_correction, color, 0.0).rgb;
  281. }
  282. #endif
  283. #ifndef SUBPASS
  284. vec3 do_fxaa(vec3 color, float exposure, vec2 uv_interp) {
  285. const float FXAA_REDUCE_MIN = (1.0 / 128.0);
  286. const float FXAA_REDUCE_MUL = (1.0 / 8.0);
  287. const float FXAA_SPAN_MAX = 8.0;
  288. #ifdef MULTIVIEW
  289. vec3 rgbNW = textureLod(source_color, vec3(uv_interp + vec2(-0.5, -0.5) * params.pixel_size, ViewIndex), 0.0).xyz * exposure * params.luminance_multiplier;
  290. vec3 rgbNE = textureLod(source_color, vec3(uv_interp + vec2(0.5, -0.5) * params.pixel_size, ViewIndex), 0.0).xyz * exposure * params.luminance_multiplier;
  291. vec3 rgbSW = textureLod(source_color, vec3(uv_interp + vec2(-0.5, 0.5) * params.pixel_size, ViewIndex), 0.0).xyz * exposure * params.luminance_multiplier;
  292. vec3 rgbSE = textureLod(source_color, vec3(uv_interp + vec2(0.5, 0.5) * params.pixel_size, ViewIndex), 0.0).xyz * exposure * params.luminance_multiplier;
  293. #else
  294. vec3 rgbNW = textureLod(source_color, uv_interp + vec2(-0.5, -0.5) * params.pixel_size, 0.0).xyz * exposure * params.luminance_multiplier;
  295. vec3 rgbNE = textureLod(source_color, uv_interp + vec2(0.5, -0.5) * params.pixel_size, 0.0).xyz * exposure * params.luminance_multiplier;
  296. vec3 rgbSW = textureLod(source_color, uv_interp + vec2(-0.5, 0.5) * params.pixel_size, 0.0).xyz * exposure * params.luminance_multiplier;
  297. vec3 rgbSE = textureLod(source_color, uv_interp + vec2(0.5, 0.5) * params.pixel_size, 0.0).xyz * exposure * params.luminance_multiplier;
  298. #endif
  299. vec3 rgbM = color;
  300. vec3 luma = vec3(0.299, 0.587, 0.114);
  301. float lumaNW = dot(rgbNW, luma);
  302. float lumaNE = dot(rgbNE, luma);
  303. float lumaSW = dot(rgbSW, luma);
  304. float lumaSE = dot(rgbSE, luma);
  305. float lumaM = dot(rgbM, luma);
  306. float lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE)));
  307. float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE)));
  308. vec2 dir;
  309. dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE));
  310. dir.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE));
  311. float dirReduce = max((lumaNW + lumaNE + lumaSW + lumaSE) *
  312. (0.25 * FXAA_REDUCE_MUL),
  313. FXAA_REDUCE_MIN);
  314. float rcpDirMin = 1.0 / (min(abs(dir.x), abs(dir.y)) + dirReduce);
  315. dir = min(vec2(FXAA_SPAN_MAX, FXAA_SPAN_MAX),
  316. max(vec2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX),
  317. dir * rcpDirMin)) *
  318. params.pixel_size;
  319. #ifdef MULTIVIEW
  320. vec3 rgbA = 0.5 * exposure * (textureLod(source_color, vec3(uv_interp + dir * (1.0 / 3.0 - 0.5), ViewIndex), 0.0).xyz + textureLod(source_color, vec3(uv_interp + dir * (2.0 / 3.0 - 0.5), ViewIndex), 0.0).xyz) * params.luminance_multiplier;
  321. vec3 rgbB = rgbA * 0.5 + 0.25 * exposure * (textureLod(source_color, vec3(uv_interp + dir * -0.5, ViewIndex), 0.0).xyz + textureLod(source_color, vec3(uv_interp + dir * 0.5, ViewIndex), 0.0).xyz) * params.luminance_multiplier;
  322. #else
  323. vec3 rgbA = 0.5 * exposure * (textureLod(source_color, uv_interp + dir * (1.0 / 3.0 - 0.5), 0.0).xyz + textureLod(source_color, uv_interp + dir * (2.0 / 3.0 - 0.5), 0.0).xyz) * params.luminance_multiplier;
  324. vec3 rgbB = rgbA * 0.5 + 0.25 * exposure * (textureLod(source_color, uv_interp + dir * -0.5, 0.0).xyz + textureLod(source_color, uv_interp + dir * 0.5, 0.0).xyz) * params.luminance_multiplier;
  325. #endif
  326. float lumaB = dot(rgbB, luma);
  327. if ((lumaB < lumaMin) || (lumaB > lumaMax)) {
  328. return rgbA;
  329. } else {
  330. return rgbB;
  331. }
  332. }
  333. #endif // !SUBPASS
  334. // From https://alex.vlachos.com/graphics/Alex_Vlachos_Advanced_VR_Rendering_GDC2015.pdf
  335. // and https://www.shadertoy.com/view/MslGR8 (5th one starting from the bottom)
  336. // NOTE: `frag_coord` is in pixels (i.e. not normalized UV).
  337. vec3 screen_space_dither(vec2 frag_coord) {
  338. // Iestyn's RGB dither (7 asm instructions) from Portal 2 X360, slightly modified for VR.
  339. vec3 dither = vec3(dot(vec2(171.0, 231.0), frag_coord));
  340. dither.rgb = fract(dither.rgb / vec3(103.0, 71.0, 97.0));
  341. // Subtract 0.5 to avoid slightly brightening the whole viewport.
  342. return (dither.rgb - 0.5) / 255.0;
  343. }
  344. void main() {
  345. #ifdef SUBPASS
  346. // SUBPASS and MULTIVIEW can be combined but in that case we're already reading from the correct layer
  347. vec4 color = subpassLoad(input_color);
  348. #elif defined(MULTIVIEW)
  349. vec4 color = textureLod(source_color, vec3(uv_interp, ViewIndex), 0.0f);
  350. #else
  351. vec4 color = textureLod(source_color, uv_interp, 0.0f);
  352. #endif
  353. color.rgb *= params.luminance_multiplier;
  354. // Exposure
  355. float exposure = params.exposure;
  356. #ifndef SUBPASS
  357. if (params.use_auto_exposure) {
  358. exposure *= 1.0 / (texelFetch(source_auto_exposure, ivec2(0, 0), 0).r * params.luminance_multiplier / params.auto_exposure_scale);
  359. }
  360. #endif
  361. color.rgb *= exposure;
  362. // Early Tonemap & SRGB Conversion
  363. #ifndef SUBPASS
  364. if (params.use_fxaa) {
  365. // FXAA must be performed before glow to preserve the "bleed" effect of glow.
  366. color.rgb = do_fxaa(color.rgb, exposure, uv_interp);
  367. }
  368. if (params.use_glow && params.glow_mode == GLOW_MODE_MIX) {
  369. vec3 glow = gather_glow(source_glow, uv_interp) * params.luminance_multiplier;
  370. if (params.glow_map_strength > 0.001) {
  371. glow = mix(glow, texture(glow_map, uv_interp).rgb * glow, params.glow_map_strength);
  372. }
  373. color.rgb = mix(color.rgb, glow, params.glow_intensity);
  374. }
  375. #endif
  376. color.rgb = apply_tonemapping(color.rgb, params.white);
  377. color.rgb = linear_to_srgb(color.rgb); // regular linear -> SRGB conversion
  378. #ifndef SUBPASS
  379. // Glow
  380. if (params.use_glow && params.glow_mode != GLOW_MODE_MIX) {
  381. vec3 glow = gather_glow(source_glow, uv_interp) * params.glow_intensity * params.luminance_multiplier;
  382. if (params.glow_map_strength > 0.001) {
  383. glow = mix(glow, texture(glow_map, uv_interp).rgb * glow, params.glow_map_strength);
  384. }
  385. // high dynamic range -> SRGB
  386. glow = apply_tonemapping(glow, params.white);
  387. glow = linear_to_srgb(glow);
  388. color.rgb = apply_glow(color.rgb, glow);
  389. }
  390. #endif
  391. // Additional effects
  392. if (params.use_bcs) {
  393. color.rgb = apply_bcs(color.rgb, params.bcs);
  394. }
  395. if (params.use_color_correction) {
  396. color.rgb = apply_color_correction(color.rgb);
  397. }
  398. if (params.use_debanding) {
  399. // Debanding should be done at the end of tonemapping, but before writing to the LDR buffer.
  400. // Otherwise, we're adding noise to an already-quantized image.
  401. color.rgb += screen_space_dither(gl_FragCoord.xy);
  402. }
  403. frag_color = color;
  404. }