tonemap.glsl 18 KB

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