tonemap.glsl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. #[vertex]
  2. #version 450
  3. VERSION_DEFINES
  4. layout(location = 0) out vec2 uv_interp;
  5. void main() {
  6. 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));
  7. uv_interp = base_arr[gl_VertexIndex];
  8. gl_Position = vec4(uv_interp * 2.0 - 1.0, 0.0, 1.0);
  9. }
  10. #[fragment]
  11. #version 450
  12. VERSION_DEFINES
  13. layout(location = 0) in vec2 uv_interp;
  14. layout(set = 0, binding = 0) uniform sampler2D source_color;
  15. layout(set = 1, binding = 0) uniform sampler2D source_auto_exposure;
  16. layout(set = 2, binding = 0) uniform sampler2D source_glow;
  17. #ifdef USE_1D_LUT
  18. layout(set = 3, binding = 0) uniform sampler2D source_color_correction;
  19. #else
  20. layout(set = 3, binding = 0) uniform sampler3D source_color_correction;
  21. #endif
  22. layout(push_constant, binding = 1, std430) uniform Params {
  23. vec3 bcs;
  24. bool use_bcs;
  25. bool use_glow;
  26. bool use_auto_exposure;
  27. bool use_color_correction;
  28. uint tonemapper;
  29. uvec2 glow_texture_size;
  30. float glow_intensity;
  31. uint pad3;
  32. uint glow_mode;
  33. float glow_levels[7];
  34. float exposure;
  35. float white;
  36. float auto_exposure_grey;
  37. uint pad2;
  38. vec2 pixel_size;
  39. bool use_fxaa;
  40. bool use_debanding;
  41. }
  42. params;
  43. layout(location = 0) out vec4 frag_color;
  44. #ifdef USE_GLOW_FILTER_BICUBIC
  45. // w0, w1, w2, and w3 are the four cubic B-spline basis functions
  46. float w0(float a) {
  47. return (1.0f / 6.0f) * (a * (a * (-a + 3.0f) - 3.0f) + 1.0f);
  48. }
  49. float w1(float a) {
  50. return (1.0f / 6.0f) * (a * a * (3.0f * a - 6.0f) + 4.0f);
  51. }
  52. float w2(float a) {
  53. return (1.0f / 6.0f) * (a * (a * (-3.0f * a + 3.0f) + 3.0f) + 1.0f);
  54. }
  55. float w3(float a) {
  56. return (1.0f / 6.0f) * (a * a * a);
  57. }
  58. // g0 and g1 are the two amplitude functions
  59. float g0(float a) {
  60. return w0(a) + w1(a);
  61. }
  62. float g1(float a) {
  63. return w2(a) + w3(a);
  64. }
  65. // h0 and h1 are the two offset functions
  66. float h0(float a) {
  67. return -1.0f + w1(a) / (w0(a) + w1(a));
  68. }
  69. float h1(float a) {
  70. return 1.0f + w3(a) / (w2(a) + w3(a));
  71. }
  72. vec4 texture2D_bicubic(sampler2D tex, vec2 uv, int p_lod) {
  73. float lod = float(p_lod);
  74. vec2 tex_size = vec2(params.glow_texture_size >> p_lod);
  75. vec2 pixel_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)) * pixel_size;
  86. vec2 p1 = (vec2(iuv.x + h1x, iuv.y + h0y) - vec2(0.5f)) * pixel_size;
  87. vec2 p2 = (vec2(iuv.x + h0x, iuv.y + h1y) - vec2(0.5f)) * pixel_size;
  88. vec2 p3 = (vec2(iuv.x + h1x, iuv.y + h1y) - vec2(0.5f)) * pixel_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 color_tonemapped / white_tonemapped;
  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 color_tonemapped / white_tonemapped;
  121. }
  122. vec3 tonemap_reinhard(vec3 color, float white) {
  123. // Ensure color values are positive.
  124. // They can be negative in the case of negative lights, which leads to undesired behavior.
  125. color = max(vec3(0.0), color);
  126. return (white * color + color) / (color * white + white);
  127. }
  128. vec3 linear_to_srgb(vec3 color) {
  129. //if going to srgb, clamp from 0 to 1.
  130. color = clamp(color, vec3(0.0), vec3(1.0));
  131. const vec3 a = vec3(0.055f);
  132. 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)));
  133. }
  134. #define TONEMAPPER_LINEAR 0
  135. #define TONEMAPPER_REINHARD 1
  136. #define TONEMAPPER_FILMIC 2
  137. #define TONEMAPPER_ACES 3
  138. vec3 apply_tonemapping(vec3 color, float white) { // inputs are LINEAR, always outputs clamped [0;1] color
  139. if (params.tonemapper == TONEMAPPER_LINEAR) {
  140. return color;
  141. } else if (params.tonemapper == TONEMAPPER_REINHARD) {
  142. return tonemap_reinhard(color, white);
  143. } else if (params.tonemapper == TONEMAPPER_FILMIC) {
  144. return tonemap_filmic(color, white);
  145. } else { //aces
  146. return tonemap_aces(color, white);
  147. }
  148. }
  149. vec3 gather_glow(sampler2D tex, vec2 uv) { // sample all selected glow levels
  150. vec3 glow = vec3(0.0f);
  151. if (params.glow_levels[0] > 0.0001) {
  152. glow += GLOW_TEXTURE_SAMPLE(tex, uv, 0).rgb * params.glow_levels[0];
  153. }
  154. if (params.glow_levels[1] > 0.0001) {
  155. glow += GLOW_TEXTURE_SAMPLE(tex, uv, 1).rgb * params.glow_levels[1];
  156. }
  157. if (params.glow_levels[2] > 0.0001) {
  158. glow += GLOW_TEXTURE_SAMPLE(tex, uv, 2).rgb * params.glow_levels[2];
  159. }
  160. if (params.glow_levels[3] > 0.0001) {
  161. glow += GLOW_TEXTURE_SAMPLE(tex, uv, 3).rgb * params.glow_levels[3];
  162. }
  163. if (params.glow_levels[4] > 0.0001) {
  164. glow += GLOW_TEXTURE_SAMPLE(tex, uv, 4).rgb * params.glow_levels[4];
  165. }
  166. if (params.glow_levels[5] > 0.0001) {
  167. glow += GLOW_TEXTURE_SAMPLE(tex, uv, 5).rgb * params.glow_levels[5];
  168. }
  169. if (params.glow_levels[6] > 0.0001) {
  170. glow += GLOW_TEXTURE_SAMPLE(tex, uv, 6).rgb * params.glow_levels[6];
  171. }
  172. return glow;
  173. }
  174. #define GLOW_MODE_ADD 0
  175. #define GLOW_MODE_SCREEN 1
  176. #define GLOW_MODE_SOFTLIGHT 2
  177. #define GLOW_MODE_REPLACE 3
  178. #define GLOW_MODE_MIX 4
  179. vec3 apply_glow(vec3 color, vec3 glow) { // apply glow using the selected blending mode
  180. if (params.glow_mode == GLOW_MODE_ADD) {
  181. return color + glow;
  182. } else if (params.glow_mode == GLOW_MODE_SCREEN) {
  183. //need color clamping
  184. return max((color + glow) - (color * glow), vec3(0.0));
  185. } else if (params.glow_mode == GLOW_MODE_SOFTLIGHT) {
  186. //need color clamping
  187. glow = glow * vec3(0.5f) + vec3(0.5f);
  188. 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)));
  189. 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)));
  190. 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)));
  191. return color;
  192. } else { //replace
  193. return glow;
  194. }
  195. }
  196. vec3 apply_bcs(vec3 color, vec3 bcs) {
  197. color = mix(vec3(0.0f), color, bcs.x);
  198. color = mix(vec3(0.5f), color, bcs.y);
  199. color = mix(vec3(dot(vec3(1.0f), color) * 0.33333f), color, bcs.z);
  200. return color;
  201. }
  202. #ifdef USE_1D_LUT
  203. vec3 apply_color_correction(vec3 color) {
  204. color.r = texture(source_color_correction, vec2(color.r, 0.0f)).r;
  205. color.g = texture(source_color_correction, vec2(color.g, 0.0f)).g;
  206. color.b = texture(source_color_correction, vec2(color.b, 0.0f)).b;
  207. return color;
  208. }
  209. #else
  210. vec3 apply_color_correction(vec3 color) {
  211. return textureLod(source_color_correction, color, 0.0).rgb;
  212. }
  213. #endif
  214. vec3 do_fxaa(vec3 color, float exposure, vec2 uv_interp) {
  215. const float FXAA_REDUCE_MIN = (1.0 / 128.0);
  216. const float FXAA_REDUCE_MUL = (1.0 / 8.0);
  217. const float FXAA_SPAN_MAX = 8.0;
  218. vec3 rgbNW = textureLod(source_color, uv_interp + vec2(-1.0, -1.0) * params.pixel_size, 0.0).xyz * exposure;
  219. vec3 rgbNE = textureLod(source_color, uv_interp + vec2(1.0, -1.0) * params.pixel_size, 0.0).xyz * exposure;
  220. vec3 rgbSW = textureLod(source_color, uv_interp + vec2(-1.0, 1.0) * params.pixel_size, 0.0).xyz * exposure;
  221. vec3 rgbSE = textureLod(source_color, uv_interp + vec2(1.0, 1.0) * params.pixel_size, 0.0).xyz * exposure;
  222. vec3 rgbM = color;
  223. vec3 luma = vec3(0.299, 0.587, 0.114);
  224. float lumaNW = dot(rgbNW, luma);
  225. float lumaNE = dot(rgbNE, luma);
  226. float lumaSW = dot(rgbSW, luma);
  227. float lumaSE = dot(rgbSE, luma);
  228. float lumaM = dot(rgbM, luma);
  229. float lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE)));
  230. float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE)));
  231. vec2 dir;
  232. dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE));
  233. dir.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE));
  234. float dirReduce = max((lumaNW + lumaNE + lumaSW + lumaSE) *
  235. (0.25 * FXAA_REDUCE_MUL),
  236. FXAA_REDUCE_MIN);
  237. float rcpDirMin = 1.0 / (min(abs(dir.x), abs(dir.y)) + dirReduce);
  238. dir = min(vec2(FXAA_SPAN_MAX, FXAA_SPAN_MAX),
  239. max(vec2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX),
  240. dir * rcpDirMin)) *
  241. params.pixel_size;
  242. 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);
  243. 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);
  244. float lumaB = dot(rgbB, luma);
  245. if ((lumaB < lumaMin) || (lumaB > lumaMax)) {
  246. return rgbA;
  247. } else {
  248. return rgbB;
  249. }
  250. }
  251. // From http://alex.vlachos.com/graphics/Alex_Vlachos_Advanced_VR_Rendering_GDC2015.pdf
  252. // and https://www.shadertoy.com/view/MslGR8 (5th one starting from the bottom)
  253. // NOTE: `frag_coord` is in pixels (i.e. not normalized UV).
  254. vec3 screen_space_dither(vec2 frag_coord) {
  255. // Iestyn's RGB dither (7 asm instructions) from Portal 2 X360, slightly modified for VR.
  256. vec3 dither = vec3(dot(vec2(171.0, 231.0), frag_coord));
  257. dither.rgb = fract(dither.rgb / vec3(103.0, 71.0, 97.0));
  258. // Subtract 0.5 to avoid slightly brightening the whole viewport.
  259. return (dither.rgb - 0.5) / 255.0;
  260. }
  261. void main() {
  262. vec3 color = textureLod(source_color, uv_interp, 0.0f).rgb;
  263. // Exposure
  264. float exposure = params.exposure;
  265. if (params.use_auto_exposure) {
  266. exposure *= 1.0 / (texelFetch(source_auto_exposure, ivec2(0, 0), 0).r / params.auto_exposure_grey);
  267. }
  268. color *= exposure;
  269. // Early Tonemap & SRGB Conversion
  270. if (params.use_glow && params.glow_mode == GLOW_MODE_MIX) {
  271. vec3 glow = gather_glow(source_glow, uv_interp);
  272. color.rgb = mix(color.rgb, glow, params.glow_intensity);
  273. }
  274. if (params.use_fxaa) {
  275. color = do_fxaa(color, exposure, uv_interp);
  276. }
  277. if (params.use_debanding) {
  278. // For best results, debanding should be done before tonemapping.
  279. // Otherwise, we're adding noise to an already-quantized image.
  280. color += screen_space_dither(gl_FragCoord.xy);
  281. }
  282. color = apply_tonemapping(color, params.white);
  283. color = linear_to_srgb(color); // regular linear -> SRGB conversion
  284. // Glow
  285. if (params.use_glow && params.glow_mode != GLOW_MODE_MIX) {
  286. vec3 glow = gather_glow(source_glow, uv_interp) * params.glow_intensity;
  287. // high dynamic range -> SRGB
  288. glow = apply_tonemapping(glow, params.white);
  289. glow = linear_to_srgb(glow);
  290. color = apply_glow(color, glow);
  291. }
  292. // Additional effects
  293. if (params.use_bcs) {
  294. color = apply_bcs(color, params.bcs);
  295. }
  296. if (params.use_color_correction) {
  297. color = apply_color_correction(color);
  298. }
  299. frag_color = vec4(color, 1.0f);
  300. }