tonemap.glsl 15 KB

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