tonemap.glsl 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. #[vertex]
  2. #version 450
  3. #VERSION_DEFINES
  4. #ifdef USE_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. // old code, ARM driver bug on Mali-GXXx GPUs and Vulkan API 1.3.xxx
  12. // https://github.com/godotengine/godot/pull/92817#issuecomment-2168625982
  13. //vec2 base_arr[3] = vec2[](vec2(-1.0, -1.0), vec2(-1.0, 3.0), vec2(3.0, -1.0));
  14. //gl_Position = vec4(base_arr[gl_VertexIndex], 0.0, 1.0);
  15. //uv_interp = clamp(gl_Position.xy, vec2(0.0, 0.0), vec2(1.0, 1.0)) * 2.0; // saturate(x) * 2.0
  16. vec2 vertex_base;
  17. if (gl_VertexIndex == 0) {
  18. vertex_base = vec2(-1.0, -1.0);
  19. } else if (gl_VertexIndex == 1) {
  20. vertex_base = vec2(-1.0, 3.0);
  21. } else {
  22. vertex_base = vec2(3.0, -1.0);
  23. }
  24. gl_Position = vec4(vertex_base, 0.0, 1.0);
  25. uv_interp = clamp(vertex_base, vec2(0.0, 0.0), vec2(1.0, 1.0)) * 2.0; // saturate(x) * 2.0
  26. }
  27. #[fragment]
  28. #version 450
  29. #VERSION_DEFINES
  30. #ifdef USE_MULTIVIEW
  31. #ifdef has_VK_KHR_multiview
  32. #extension GL_EXT_multiview : enable
  33. #define ViewIndex gl_ViewIndex
  34. #else // has_VK_KHR_multiview
  35. #define ViewIndex 0
  36. #endif // has_VK_KHR_multiview
  37. #endif //USE_MULTIVIEW
  38. layout(location = 0) in vec2 uv_interp;
  39. #ifdef SUBPASS
  40. layout(input_attachment_index = 0, set = 0, binding = 0) uniform subpassInput input_color;
  41. #elif defined(USE_MULTIVIEW)
  42. layout(set = 0, binding = 0) uniform sampler2DArray source_color;
  43. #else
  44. layout(set = 0, binding = 0) uniform sampler2D source_color;
  45. #endif
  46. layout(set = 1, binding = 0) uniform sampler2D source_auto_exposure;
  47. #ifdef USE_MULTIVIEW
  48. layout(set = 2, binding = 0) uniform sampler2DArray source_glow;
  49. #else
  50. layout(set = 2, binding = 0) uniform sampler2D source_glow;
  51. #endif
  52. layout(set = 2, binding = 1) uniform sampler2D glow_map;
  53. #ifdef USE_1D_LUT
  54. layout(set = 3, binding = 0) uniform sampler2D source_color_correction;
  55. #else
  56. layout(set = 3, binding = 0) uniform sampler3D source_color_correction;
  57. #endif
  58. #define FLAG_USE_BCS (1 << 0)
  59. #define FLAG_USE_GLOW (1 << 1)
  60. #define FLAG_USE_AUTO_EXPOSURE (1 << 2)
  61. #define FLAG_USE_COLOR_CORRECTION (1 << 3)
  62. #define FLAG_USE_FXAA (1 << 4)
  63. #define FLAG_USE_DEBANDING (1 << 5)
  64. #define FLAG_CONVERT_TO_SRGB (1 << 6)
  65. layout(push_constant, std430) uniform Params {
  66. vec3 bcs;
  67. uint flags;
  68. vec2 pixel_size;
  69. uint tonemapper;
  70. uint pad;
  71. uvec2 glow_texture_size;
  72. float glow_intensity;
  73. float glow_map_strength;
  74. uint glow_mode;
  75. float glow_levels[7];
  76. float exposure;
  77. float white;
  78. float auto_exposure_scale;
  79. float luminance_multiplier;
  80. }
  81. params;
  82. layout(location = 0) out vec4 frag_color;
  83. #ifdef USE_GLOW_FILTER_BICUBIC
  84. // w0, w1, w2, and w3 are the four cubic B-spline basis functions
  85. float w0(float a) {
  86. return (1.0f / 6.0f) * (a * (a * (-a + 3.0f) - 3.0f) + 1.0f);
  87. }
  88. float w1(float a) {
  89. return (1.0f / 6.0f) * (a * a * (3.0f * a - 6.0f) + 4.0f);
  90. }
  91. float w2(float a) {
  92. return (1.0f / 6.0f) * (a * (a * (-3.0f * a + 3.0f) + 3.0f) + 1.0f);
  93. }
  94. float w3(float a) {
  95. return (1.0f / 6.0f) * (a * a * a);
  96. }
  97. // g0 and g1 are the two amplitude functions
  98. float g0(float a) {
  99. return w0(a) + w1(a);
  100. }
  101. float g1(float a) {
  102. return w2(a) + w3(a);
  103. }
  104. // h0 and h1 are the two offset functions
  105. float h0(float a) {
  106. return -1.0f + w1(a) / (w0(a) + w1(a));
  107. }
  108. float h1(float a) {
  109. return 1.0f + w3(a) / (w2(a) + w3(a));
  110. }
  111. #ifdef USE_MULTIVIEW
  112. vec4 texture2D_bicubic(sampler2DArray tex, vec2 uv, int p_lod) {
  113. float lod = float(p_lod);
  114. vec2 tex_size = vec2(params.glow_texture_size >> p_lod);
  115. vec2 pixel_size = vec2(1.0f) / tex_size;
  116. uv = uv * tex_size + vec2(0.5f);
  117. vec2 iuv = floor(uv);
  118. vec2 fuv = fract(uv);
  119. float g0x = g0(fuv.x);
  120. float g1x = g1(fuv.x);
  121. float h0x = h0(fuv.x);
  122. float h1x = h1(fuv.x);
  123. float h0y = h0(fuv.y);
  124. float h1y = h1(fuv.y);
  125. vec3 p0 = vec3((vec2(iuv.x + h0x, iuv.y + h0y) - vec2(0.5f)) * pixel_size, ViewIndex);
  126. vec3 p1 = vec3((vec2(iuv.x + h1x, iuv.y + h0y) - vec2(0.5f)) * pixel_size, ViewIndex);
  127. vec3 p2 = vec3((vec2(iuv.x + h0x, iuv.y + h1y) - vec2(0.5f)) * pixel_size, ViewIndex);
  128. vec3 p3 = vec3((vec2(iuv.x + h1x, iuv.y + h1y) - vec2(0.5f)) * pixel_size, ViewIndex);
  129. return (g0(fuv.y) * (g0x * textureLod(tex, p0, lod) + g1x * textureLod(tex, p1, lod))) +
  130. (g1(fuv.y) * (g0x * textureLod(tex, p2, lod) + g1x * textureLod(tex, p3, lod)));
  131. }
  132. #define GLOW_TEXTURE_SAMPLE(m_tex, m_uv, m_lod) texture2D_bicubic(m_tex, m_uv, m_lod)
  133. #else // USE_MULTIVIEW
  134. vec4 texture2D_bicubic(sampler2D tex, vec2 uv, int p_lod) {
  135. float lod = float(p_lod);
  136. vec2 tex_size = vec2(params.glow_texture_size >> p_lod);
  137. vec2 pixel_size = vec2(1.0f) / tex_size;
  138. uv = uv * tex_size + vec2(0.5f);
  139. vec2 iuv = floor(uv);
  140. vec2 fuv = fract(uv);
  141. float g0x = g0(fuv.x);
  142. float g1x = g1(fuv.x);
  143. float h0x = h0(fuv.x);
  144. float h1x = h1(fuv.x);
  145. float h0y = h0(fuv.y);
  146. float h1y = h1(fuv.y);
  147. vec2 p0 = (vec2(iuv.x + h0x, iuv.y + h0y) - vec2(0.5f)) * pixel_size;
  148. vec2 p1 = (vec2(iuv.x + h1x, iuv.y + h0y) - vec2(0.5f)) * pixel_size;
  149. vec2 p2 = (vec2(iuv.x + h0x, iuv.y + h1y) - vec2(0.5f)) * pixel_size;
  150. vec2 p3 = (vec2(iuv.x + h1x, iuv.y + h1y) - vec2(0.5f)) * pixel_size;
  151. return (g0(fuv.y) * (g0x * textureLod(tex, p0, lod) + g1x * textureLod(tex, p1, lod))) +
  152. (g1(fuv.y) * (g0x * textureLod(tex, p2, lod) + g1x * textureLod(tex, p3, lod)));
  153. }
  154. #define GLOW_TEXTURE_SAMPLE(m_tex, m_uv, m_lod) texture2D_bicubic(m_tex, m_uv, m_lod)
  155. #endif // !USE_MULTIVIEW
  156. #else // USE_GLOW_FILTER_BICUBIC
  157. #ifdef USE_MULTIVIEW
  158. #define GLOW_TEXTURE_SAMPLE(m_tex, m_uv, m_lod) textureLod(m_tex, vec3(m_uv, ViewIndex), float(m_lod))
  159. #else // USE_MULTIVIEW
  160. #define GLOW_TEXTURE_SAMPLE(m_tex, m_uv, m_lod) textureLod(m_tex, m_uv, float(m_lod))
  161. #endif // !USE_MULTIVIEW
  162. #endif // !USE_GLOW_FILTER_BICUBIC
  163. // Based on Reinhard's extended formula, see equation 4 in https://doi.org/cjbgrt
  164. vec3 tonemap_reinhard(vec3 color, float white) {
  165. float white_squared = white * white;
  166. vec3 white_squared_color = white_squared * color;
  167. // Equivalent to color * (1 + color / white_squared) / (1 + color)
  168. return (white_squared_color + color * color) / (white_squared_color + white_squared);
  169. }
  170. vec3 tonemap_filmic(vec3 color, float white) {
  171. // exposure bias: input scale (color *= bias, white *= bias) to make the brightness consistent with other tonemappers
  172. // also useful to scale the input to the range that the tonemapper is designed for (some require very high input values)
  173. // has no effect on the curve's general shape or visual properties
  174. const float exposure_bias = 2.0f;
  175. const float A = 0.22f * exposure_bias * exposure_bias; // bias baked into constants for performance
  176. const float B = 0.30f * exposure_bias;
  177. const float C = 0.10f;
  178. const float D = 0.20f;
  179. const float E = 0.01f;
  180. const float F = 0.30f;
  181. vec3 color_tonemapped = ((color * (A * color + C * B) + D * E) / (color * (A * color + B) + D * F)) - E / F;
  182. float white_tonemapped = ((white * (A * white + C * B) + D * E) / (white * (A * white + B) + D * F)) - E / F;
  183. return color_tonemapped / white_tonemapped;
  184. }
  185. // Adapted from https://github.com/TheRealMJP/BakingLab/blob/master/BakingLab/ACES.hlsl
  186. // (MIT License).
  187. vec3 tonemap_aces(vec3 color, float white) {
  188. const float exposure_bias = 1.8f;
  189. const float A = 0.0245786f;
  190. const float B = 0.000090537f;
  191. const float C = 0.983729f;
  192. const float D = 0.432951f;
  193. const float E = 0.238081f;
  194. // Exposure bias baked into transform to save shader instructions. Equivalent to `color *= exposure_bias`
  195. const mat3 rgb_to_rrt = mat3(
  196. vec3(0.59719f * exposure_bias, 0.35458f * exposure_bias, 0.04823f * exposure_bias),
  197. vec3(0.07600f * exposure_bias, 0.90834f * exposure_bias, 0.01566f * exposure_bias),
  198. vec3(0.02840f * exposure_bias, 0.13383f * exposure_bias, 0.83777f * exposure_bias));
  199. const mat3 odt_to_rgb = mat3(
  200. vec3(1.60475f, -0.53108f, -0.07367f),
  201. vec3(-0.10208f, 1.10813f, -0.00605f),
  202. vec3(-0.00327f, -0.07276f, 1.07602f));
  203. color *= rgb_to_rrt;
  204. vec3 color_tonemapped = (color * (color + A) - B) / (color * (C * color + D) + E);
  205. color_tonemapped *= odt_to_rgb;
  206. white *= exposure_bias;
  207. float white_tonemapped = (white * (white + A) - B) / (white * (C * white + D) + E);
  208. return color_tonemapped / white_tonemapped;
  209. }
  210. // Polynomial approximation of EaryChow's AgX sigmoid curve.
  211. // x must be within the range [0.0, 1.0]
  212. vec3 agx_contrast_approx(vec3 x) {
  213. // Generated with Excel trendline
  214. // Input data: Generated using python sigmoid with EaryChow's configuration and 57 steps
  215. // Additional padding values were added to give correct intersections at 0.0 and 1.0
  216. // 6th order, intercept of 0.0 to remove an operation and ensure intersection at 0.0
  217. vec3 x2 = x * x;
  218. vec3 x4 = x2 * x2;
  219. return 0.021 * x + 4.0111 * x2 - 25.682 * x2 * x + 70.359 * x4 - 74.778 * x4 * x + 27.069 * x4 * x2;
  220. }
  221. // This is an approximation and simplification of EaryChow's AgX implementation that is used by Blender.
  222. // This code is based off of the script that generates the AgX_Base_sRGB.cube LUT that Blender uses.
  223. // Source: https://github.com/EaryChow/AgX_LUT_Gen/blob/main/AgXBasesRGB.py
  224. vec3 tonemap_agx(vec3 color) {
  225. // Combined linear sRGB to linear Rec 2020 and Blender AgX inset matrices:
  226. const mat3 srgb_to_rec2020_agx_inset_matrix = mat3(
  227. 0.54490813676363087053, 0.14044005884001287035, 0.088827411851915368603,
  228. 0.37377945959812267119, 0.75410959864013760045, 0.17887712465043811023,
  229. 0.081384976686407536266, 0.10543358536857773485, 0.73224999956948382528);
  230. // Combined inverse AgX outset matrix and linear Rec 2020 to linear sRGB matrices.
  231. const mat3 agx_outset_rec2020_to_srgb_matrix = mat3(
  232. 1.9645509602733325934, -0.29932243390911083839, -0.16436833806080403409,
  233. -0.85585845117807513559, 1.3264510741502356555, -0.23822464068860595117,
  234. -0.10886710826831608324, -0.027084020983874825605, 1.402665347143271889);
  235. // LOG2_MIN = -10.0
  236. // LOG2_MAX = +6.5
  237. // MIDDLE_GRAY = 0.18
  238. const float min_ev = -12.4739311883324; // log2(pow(2, LOG2_MIN) * MIDDLE_GRAY)
  239. const float max_ev = 4.02606881166759; // log2(pow(2, LOG2_MAX) * MIDDLE_GRAY)
  240. // Large negative values in one channel and large positive values in other
  241. // channels can result in a colour that appears darker and more saturated than
  242. // desired after passing it through the inset matrix. For this reason, it is
  243. // best to prevent negative input values.
  244. // This is done before the Rec. 2020 transform to allow the Rec. 2020
  245. // transform to be combined with the AgX inset matrix. This results in a loss
  246. // of color information that could be correctly interpreted within the
  247. // Rec. 2020 color space as positive RGB values, but it is less common for Godot
  248. // to provide this function with negative sRGB values and therefore not worth
  249. // the performance cost of an additional matrix multiplication.
  250. // A value of 2e-10 intentionally introduces insignificant error to prevent
  251. // log2(0.0) after the inset matrix is applied; color will be >= 1e-10 after
  252. // the matrix transform.
  253. color = max(color, 2e-10);
  254. // Do AGX in rec2020 to match Blender and then apply inset matrix.
  255. color = srgb_to_rec2020_agx_inset_matrix * color;
  256. // Log2 space encoding.
  257. // Must be clamped because agx_contrast_approx may not work
  258. // well with values outside of the range [0.0, 1.0]
  259. color = clamp(log2(color), min_ev, max_ev);
  260. color = (color - min_ev) / (max_ev - min_ev);
  261. // Apply sigmoid function approximation.
  262. color = agx_contrast_approx(color);
  263. // Convert back to linear before applying outset matrix.
  264. color = pow(color, vec3(2.4));
  265. // Apply outset to make the result more chroma-laden and then go back to linear sRGB.
  266. color = agx_outset_rec2020_to_srgb_matrix * color;
  267. // Blender's lusRGB.compensate_low_side is too complex for this shader, so
  268. // simply return the color, even if it has negative components. These negative
  269. // components may be useful for subsequent color adjustments.
  270. return color;
  271. }
  272. vec3 linear_to_srgb(vec3 color) {
  273. //if going to srgb, clamp from 0 to 1.
  274. color = clamp(color, vec3(0.0), vec3(1.0));
  275. const vec3 a = vec3(0.055f);
  276. 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)));
  277. }
  278. #define TONEMAPPER_LINEAR 0
  279. #define TONEMAPPER_REINHARD 1
  280. #define TONEMAPPER_FILMIC 2
  281. #define TONEMAPPER_ACES 3
  282. #define TONEMAPPER_AGX 4
  283. vec3 apply_tonemapping(vec3 color, float white) { // inputs are LINEAR
  284. // Ensure color values passed to tonemappers are positive.
  285. // They can be negative in the case of negative lights, which leads to undesired behavior.
  286. if (params.tonemapper == TONEMAPPER_LINEAR) {
  287. return color;
  288. } else if (params.tonemapper == TONEMAPPER_REINHARD) {
  289. return tonemap_reinhard(max(vec3(0.0f), color), white);
  290. } else if (params.tonemapper == TONEMAPPER_FILMIC) {
  291. return tonemap_filmic(max(vec3(0.0f), color), white);
  292. } else if (params.tonemapper == TONEMAPPER_ACES) {
  293. return tonemap_aces(max(vec3(0.0f), color), white);
  294. } else { // TONEMAPPER_AGX
  295. return tonemap_agx(color);
  296. }
  297. }
  298. #ifdef USE_MULTIVIEW
  299. vec3 gather_glow(sampler2DArray tex, vec2 uv) { // sample all selected glow levels, view is added to uv later
  300. #else
  301. vec3 gather_glow(sampler2D tex, vec2 uv) { // sample all selected glow levels
  302. #endif // defined(USE_MULTIVIEW)
  303. vec3 glow = vec3(0.0f);
  304. if (params.glow_levels[0] > 0.0001) {
  305. glow += GLOW_TEXTURE_SAMPLE(tex, uv, 0).rgb * params.glow_levels[0];
  306. }
  307. if (params.glow_levels[1] > 0.0001) {
  308. glow += GLOW_TEXTURE_SAMPLE(tex, uv, 1).rgb * params.glow_levels[1];
  309. }
  310. if (params.glow_levels[2] > 0.0001) {
  311. glow += GLOW_TEXTURE_SAMPLE(tex, uv, 2).rgb * params.glow_levels[2];
  312. }
  313. if (params.glow_levels[3] > 0.0001) {
  314. glow += GLOW_TEXTURE_SAMPLE(tex, uv, 3).rgb * params.glow_levels[3];
  315. }
  316. if (params.glow_levels[4] > 0.0001) {
  317. glow += GLOW_TEXTURE_SAMPLE(tex, uv, 4).rgb * params.glow_levels[4];
  318. }
  319. if (params.glow_levels[5] > 0.0001) {
  320. glow += GLOW_TEXTURE_SAMPLE(tex, uv, 5).rgb * params.glow_levels[5];
  321. }
  322. if (params.glow_levels[6] > 0.0001) {
  323. glow += GLOW_TEXTURE_SAMPLE(tex, uv, 6).rgb * params.glow_levels[6];
  324. }
  325. return glow;
  326. }
  327. #define GLOW_MODE_ADD 0
  328. #define GLOW_MODE_SCREEN 1
  329. #define GLOW_MODE_SOFTLIGHT 2
  330. #define GLOW_MODE_REPLACE 3
  331. #define GLOW_MODE_MIX 4
  332. vec3 apply_glow(vec3 color, vec3 glow) { // apply glow using the selected blending mode
  333. if (params.glow_mode == GLOW_MODE_ADD) {
  334. return color + glow;
  335. } else if (params.glow_mode == GLOW_MODE_SCREEN) {
  336. // Needs color clamping.
  337. glow.rgb = clamp(glow.rgb, vec3(0.0f), vec3(1.0f));
  338. return max((color + glow) - (color * glow), vec3(0.0));
  339. } else if (params.glow_mode == GLOW_MODE_SOFTLIGHT) {
  340. // Needs color clamping.
  341. glow.rgb = clamp(glow.rgb, vec3(0.0f), vec3(1.0f));
  342. glow = glow * vec3(0.5f) + vec3(0.5f);
  343. 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)));
  344. 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)));
  345. 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)));
  346. return color;
  347. } else { //replace
  348. return glow;
  349. }
  350. }
  351. vec3 apply_bcs(vec3 color, vec3 bcs) {
  352. color = mix(vec3(0.0f), color, bcs.x);
  353. color = mix(vec3(0.5f), color, bcs.y);
  354. color = mix(vec3(dot(vec3(1.0f), color) * 0.33333f), color, bcs.z);
  355. return color;
  356. }
  357. #ifdef USE_1D_LUT
  358. vec3 apply_color_correction(vec3 color) {
  359. color.r = texture(source_color_correction, vec2(color.r, 0.0f)).r;
  360. color.g = texture(source_color_correction, vec2(color.g, 0.0f)).g;
  361. color.b = texture(source_color_correction, vec2(color.b, 0.0f)).b;
  362. return color;
  363. }
  364. #else
  365. vec3 apply_color_correction(vec3 color) {
  366. return textureLod(source_color_correction, color, 0.0).rgb;
  367. }
  368. #endif
  369. #ifndef SUBPASS
  370. vec3 do_fxaa(vec3 color, float exposure, vec2 uv_interp) {
  371. const float FXAA_REDUCE_MIN = (1.0 / 128.0);
  372. const float FXAA_REDUCE_MUL = (1.0 / 8.0);
  373. const float FXAA_SPAN_MAX = 8.0;
  374. #ifdef USE_MULTIVIEW
  375. vec3 rgbNW = textureLod(source_color, vec3(uv_interp + vec2(-0.5, -0.5) * params.pixel_size, ViewIndex), 0.0).xyz * exposure * params.luminance_multiplier;
  376. vec3 rgbNE = textureLod(source_color, vec3(uv_interp + vec2(0.5, -0.5) * params.pixel_size, ViewIndex), 0.0).xyz * exposure * params.luminance_multiplier;
  377. vec3 rgbSW = textureLod(source_color, vec3(uv_interp + vec2(-0.5, 0.5) * params.pixel_size, ViewIndex), 0.0).xyz * exposure * params.luminance_multiplier;
  378. vec3 rgbSE = textureLod(source_color, vec3(uv_interp + vec2(0.5, 0.5) * params.pixel_size, ViewIndex), 0.0).xyz * exposure * params.luminance_multiplier;
  379. #else
  380. vec3 rgbNW = textureLod(source_color, uv_interp + vec2(-0.5, -0.5) * params.pixel_size, 0.0).xyz * exposure * params.luminance_multiplier;
  381. vec3 rgbNE = textureLod(source_color, uv_interp + vec2(0.5, -0.5) * params.pixel_size, 0.0).xyz * exposure * params.luminance_multiplier;
  382. vec3 rgbSW = textureLod(source_color, uv_interp + vec2(-0.5, 0.5) * params.pixel_size, 0.0).xyz * exposure * params.luminance_multiplier;
  383. vec3 rgbSE = textureLod(source_color, uv_interp + vec2(0.5, 0.5) * params.pixel_size, 0.0).xyz * exposure * params.luminance_multiplier;
  384. #endif
  385. vec3 rgbM = color;
  386. vec3 luma = vec3(0.299, 0.587, 0.114);
  387. float lumaNW = dot(rgbNW, luma);
  388. float lumaNE = dot(rgbNE, luma);
  389. float lumaSW = dot(rgbSW, luma);
  390. float lumaSE = dot(rgbSE, luma);
  391. float lumaM = dot(rgbM, luma);
  392. float lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE)));
  393. float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE)));
  394. vec2 dir;
  395. dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE));
  396. dir.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE));
  397. float dirReduce = max((lumaNW + lumaNE + lumaSW + lumaSE) *
  398. (0.25 * FXAA_REDUCE_MUL),
  399. FXAA_REDUCE_MIN);
  400. float rcpDirMin = 1.0 / (min(abs(dir.x), abs(dir.y)) + dirReduce);
  401. dir = min(vec2(FXAA_SPAN_MAX, FXAA_SPAN_MAX),
  402. max(vec2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX),
  403. dir * rcpDirMin)) *
  404. params.pixel_size;
  405. #ifdef USE_MULTIVIEW
  406. 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;
  407. 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;
  408. #else
  409. 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;
  410. 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;
  411. #endif
  412. float lumaB = dot(rgbB, luma);
  413. if ((lumaB < lumaMin) || (lumaB > lumaMax)) {
  414. return rgbA;
  415. } else {
  416. return rgbB;
  417. }
  418. }
  419. #endif // !SUBPASS
  420. // From https://alex.vlachos.com/graphics/Alex_Vlachos_Advanced_VR_Rendering_GDC2015.pdf
  421. // and https://www.shadertoy.com/view/MslGR8 (5th one starting from the bottom)
  422. // NOTE: `frag_coord` is in pixels (i.e. not normalized UV).
  423. vec3 screen_space_dither(vec2 frag_coord) {
  424. // Iestyn's RGB dither (7 asm instructions) from Portal 2 X360, slightly modified for VR.
  425. vec3 dither = vec3(dot(vec2(171.0, 231.0), frag_coord));
  426. dither.rgb = fract(dither.rgb / vec3(103.0, 71.0, 97.0));
  427. // Subtract 0.5 to avoid slightly brightening the whole viewport.
  428. return (dither.rgb - 0.5) / 255.0;
  429. }
  430. void main() {
  431. #ifdef SUBPASS
  432. // SUBPASS and USE_MULTIVIEW can be combined but in that case we're already reading from the correct layer
  433. vec4 color = subpassLoad(input_color);
  434. #elif defined(USE_MULTIVIEW)
  435. vec4 color = textureLod(source_color, vec3(uv_interp, ViewIndex), 0.0f);
  436. #else
  437. vec4 color = textureLod(source_color, uv_interp, 0.0f);
  438. #endif
  439. color.rgb *= params.luminance_multiplier;
  440. // Exposure
  441. float exposure = params.exposure;
  442. #ifndef SUBPASS
  443. if (bool(params.flags & FLAG_USE_AUTO_EXPOSURE)) {
  444. exposure *= 1.0 / (texelFetch(source_auto_exposure, ivec2(0, 0), 0).r * params.luminance_multiplier / params.auto_exposure_scale);
  445. }
  446. #endif
  447. color.rgb *= exposure;
  448. // Early Tonemap & SRGB Conversion
  449. #ifndef SUBPASS
  450. if (bool(params.flags & FLAG_USE_FXAA)) {
  451. // FXAA must be performed before glow to preserve the "bleed" effect of glow.
  452. color.rgb = do_fxaa(color.rgb, exposure, uv_interp);
  453. }
  454. if (bool(params.flags & FLAG_USE_GLOW) && params.glow_mode == GLOW_MODE_MIX) {
  455. vec3 glow = gather_glow(source_glow, uv_interp) * params.luminance_multiplier;
  456. if (params.glow_map_strength > 0.001) {
  457. glow = mix(glow, texture(glow_map, uv_interp).rgb * glow, params.glow_map_strength);
  458. }
  459. color.rgb = mix(color.rgb, glow, params.glow_intensity);
  460. }
  461. #endif
  462. color.rgb = apply_tonemapping(color.rgb, params.white);
  463. if (bool(params.flags & FLAG_CONVERT_TO_SRGB)) {
  464. color.rgb = linear_to_srgb(color.rgb); // Regular linear -> SRGB conversion.
  465. }
  466. #ifndef SUBPASS
  467. // Glow
  468. if (bool(params.flags & FLAG_USE_GLOW) && params.glow_mode != GLOW_MODE_MIX) {
  469. vec3 glow = gather_glow(source_glow, uv_interp) * params.glow_intensity * params.luminance_multiplier;
  470. if (params.glow_map_strength > 0.001) {
  471. glow = mix(glow, texture(glow_map, uv_interp).rgb * glow, params.glow_map_strength);
  472. }
  473. // high dynamic range -> SRGB
  474. glow = apply_tonemapping(glow, params.white);
  475. if (bool(params.flags & FLAG_CONVERT_TO_SRGB)) {
  476. glow = linear_to_srgb(glow);
  477. }
  478. color.rgb = apply_glow(color.rgb, glow);
  479. }
  480. #endif
  481. // Additional effects
  482. if (bool(params.flags & FLAG_USE_BCS)) {
  483. color.rgb = apply_bcs(color.rgb, params.bcs);
  484. }
  485. if (bool(params.flags & FLAG_USE_COLOR_CORRECTION)) {
  486. color.rgb = apply_color_correction(color.rgb);
  487. }
  488. if (bool(params.flags & FLAG_USE_DEBANDING)) {
  489. // Debanding should be done at the end of tonemapping, but before writing to the LDR buffer.
  490. // Otherwise, we're adding noise to an already-quantized image.
  491. color.rgb += screen_space_dither(gl_FragCoord.xy);
  492. }
  493. frag_color = color;
  494. }