2020-05-18 10:56:22 +02:00
|
|
|
#[vertex]
|
2019-08-27 20:27:35 +02:00
|
|
|
|
|
|
|
#version 450
|
|
|
|
|
2021-04-13 22:01:43 +02:00
|
|
|
#VERSION_DEFINES
|
2019-08-27 20:27:35 +02:00
|
|
|
|
2021-05-07 15:19:04 +02:00
|
|
|
#ifdef MULTIVIEW
|
|
|
|
#ifdef has_VK_KHR_multiview
|
|
|
|
#extension GL_EXT_multiview : enable
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2019-08-27 20:27:35 +02:00
|
|
|
layout(location = 0) out vec2 uv_interp;
|
|
|
|
|
|
|
|
void main() {
|
2019-11-05 12:01:00 +01:00
|
|
|
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));
|
2019-08-27 20:27:35 +02:00
|
|
|
uv_interp = base_arr[gl_VertexIndex];
|
2019-11-05 12:01:00 +01:00
|
|
|
gl_Position = vec4(uv_interp * 2.0 - 1.0, 0.0, 1.0);
|
2019-08-27 20:27:35 +02:00
|
|
|
}
|
|
|
|
|
2020-05-18 10:56:22 +02:00
|
|
|
#[fragment]
|
2019-08-27 20:27:35 +02:00
|
|
|
|
|
|
|
#version 450
|
|
|
|
|
2021-04-13 22:01:43 +02:00
|
|
|
#VERSION_DEFINES
|
2019-08-27 20:27:35 +02:00
|
|
|
|
2021-05-07 15:19:04 +02:00
|
|
|
#ifdef MULTIVIEW
|
|
|
|
#ifdef has_VK_KHR_multiview
|
|
|
|
#extension GL_EXT_multiview : enable
|
|
|
|
#define ViewIndex gl_ViewIndex
|
|
|
|
#else // has_VK_KHR_multiview
|
|
|
|
#define ViewIndex 0
|
|
|
|
#endif // has_VK_KHR_multiview
|
|
|
|
#endif //MULTIVIEW
|
|
|
|
|
2019-11-05 12:01:00 +01:00
|
|
|
layout(location = 0) in vec2 uv_interp;
|
2019-08-27 20:27:35 +02:00
|
|
|
|
2021-06-26 12:49:25 +02:00
|
|
|
#ifdef SUBPASS
|
|
|
|
layout(input_attachment_index = 0, set = 0, binding = 0) uniform subpassInput input_color;
|
2021-08-22 13:30:58 +02:00
|
|
|
#elif defined(MULTIVIEW)
|
2021-05-07 15:19:04 +02:00
|
|
|
layout(set = 0, binding = 0) uniform sampler2DArray source_color;
|
|
|
|
#else
|
2019-11-05 12:01:00 +01:00
|
|
|
layout(set = 0, binding = 0) uniform sampler2D source_color;
|
2021-05-07 15:19:04 +02:00
|
|
|
#endif
|
2021-08-22 13:30:58 +02:00
|
|
|
|
2019-11-05 12:01:00 +01:00
|
|
|
layout(set = 1, binding = 0) uniform sampler2D source_auto_exposure;
|
2022-04-29 09:10:54 +02:00
|
|
|
#ifdef MULTIVIEW
|
|
|
|
layout(set = 2, binding = 0) uniform sampler2DArray source_glow;
|
|
|
|
#else
|
2019-11-05 12:01:00 +01:00
|
|
|
layout(set = 2, binding = 0) uniform sampler2D source_glow;
|
2022-04-29 09:10:54 +02:00
|
|
|
#endif
|
2022-01-20 16:47:25 +01:00
|
|
|
layout(set = 2, binding = 1) uniform sampler2D glow_map;
|
2021-08-22 13:30:58 +02:00
|
|
|
|
2020-11-23 02:51:31 +01:00
|
|
|
#ifdef USE_1D_LUT
|
|
|
|
layout(set = 3, binding = 0) uniform sampler2D source_color_correction;
|
|
|
|
#else
|
|
|
|
layout(set = 3, binding = 0) uniform sampler3D source_color_correction;
|
|
|
|
#endif
|
2019-08-27 20:27:35 +02:00
|
|
|
|
2022-02-11 18:40:24 +01:00
|
|
|
layout(push_constant, std430) uniform Params {
|
2019-08-27 20:27:35 +02:00
|
|
|
vec3 bcs;
|
|
|
|
bool use_bcs;
|
|
|
|
|
|
|
|
bool use_glow;
|
|
|
|
bool use_auto_exposure;
|
|
|
|
bool use_color_correction;
|
|
|
|
uint tonemapper;
|
|
|
|
|
|
|
|
uvec2 glow_texture_size;
|
|
|
|
float glow_intensity;
|
2022-01-20 16:47:25 +01:00
|
|
|
float glow_map_strength;
|
2020-11-23 02:51:31 +01:00
|
|
|
|
2019-08-27 20:27:35 +02:00
|
|
|
uint glow_mode;
|
2020-09-20 00:02:32 +02:00
|
|
|
float glow_levels[7];
|
2019-08-27 20:27:35 +02:00
|
|
|
|
|
|
|
float exposure;
|
|
|
|
float white;
|
2022-08-01 01:20:24 +02:00
|
|
|
float auto_exposure_scale;
|
2021-07-26 13:31:15 +02:00
|
|
|
float luminance_multiplier;
|
2020-04-12 06:49:10 +02:00
|
|
|
|
|
|
|
vec2 pixel_size;
|
|
|
|
bool use_fxaa;
|
2020-04-20 23:34:47 +02:00
|
|
|
bool use_debanding;
|
2020-02-11 14:01:43 +01:00
|
|
|
}
|
|
|
|
params;
|
2019-08-27 20:27:35 +02:00
|
|
|
|
|
|
|
layout(location = 0) out vec4 frag_color;
|
|
|
|
|
|
|
|
#ifdef USE_GLOW_FILTER_BICUBIC
|
|
|
|
// w0, w1, w2, and w3 are the four cubic B-spline basis functions
|
|
|
|
float w0(float a) {
|
|
|
|
return (1.0f / 6.0f) * (a * (a * (-a + 3.0f) - 3.0f) + 1.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
float w1(float a) {
|
|
|
|
return (1.0f / 6.0f) * (a * a * (3.0f * a - 6.0f) + 4.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
float w2(float a) {
|
|
|
|
return (1.0f / 6.0f) * (a * (a * (-3.0f * a + 3.0f) + 3.0f) + 1.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
float w3(float a) {
|
|
|
|
return (1.0f / 6.0f) * (a * a * a);
|
|
|
|
}
|
|
|
|
|
|
|
|
// g0 and g1 are the two amplitude functions
|
|
|
|
float g0(float a) {
|
|
|
|
return w0(a) + w1(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
float g1(float a) {
|
|
|
|
return w2(a) + w3(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
// h0 and h1 are the two offset functions
|
|
|
|
float h0(float a) {
|
|
|
|
return -1.0f + w1(a) / (w0(a) + w1(a));
|
|
|
|
}
|
|
|
|
|
|
|
|
float h1(float a) {
|
|
|
|
return 1.0f + w3(a) / (w2(a) + w3(a));
|
|
|
|
}
|
|
|
|
|
2022-04-29 09:10:54 +02:00
|
|
|
#ifdef MULTIVIEW
|
|
|
|
vec4 texture2D_bicubic(sampler2DArray tex, vec2 uv, int p_lod) {
|
|
|
|
float lod = float(p_lod);
|
|
|
|
vec2 tex_size = vec2(params.glow_texture_size >> p_lod);
|
|
|
|
vec2 pixel_size = vec2(1.0f) / tex_size;
|
|
|
|
|
|
|
|
uv = uv * tex_size + vec2(0.5f);
|
|
|
|
|
|
|
|
vec2 iuv = floor(uv);
|
|
|
|
vec2 fuv = fract(uv);
|
|
|
|
|
|
|
|
float g0x = g0(fuv.x);
|
|
|
|
float g1x = g1(fuv.x);
|
|
|
|
float h0x = h0(fuv.x);
|
|
|
|
float h1x = h1(fuv.x);
|
|
|
|
float h0y = h0(fuv.y);
|
|
|
|
float h1y = h1(fuv.y);
|
|
|
|
|
|
|
|
vec3 p0 = vec3((vec2(iuv.x + h0x, iuv.y + h0y) - vec2(0.5f)) * pixel_size, ViewIndex);
|
|
|
|
vec3 p1 = vec3((vec2(iuv.x + h1x, iuv.y + h0y) - vec2(0.5f)) * pixel_size, ViewIndex);
|
|
|
|
vec3 p2 = vec3((vec2(iuv.x + h0x, iuv.y + h1y) - vec2(0.5f)) * pixel_size, ViewIndex);
|
|
|
|
vec3 p3 = vec3((vec2(iuv.x + h1x, iuv.y + h1y) - vec2(0.5f)) * pixel_size, ViewIndex);
|
|
|
|
|
|
|
|
return (g0(fuv.y) * (g0x * textureLod(tex, p0, lod) + g1x * textureLod(tex, p1, lod))) +
|
|
|
|
(g1(fuv.y) * (g0x * textureLod(tex, p2, lod) + g1x * textureLod(tex, p3, lod)));
|
|
|
|
}
|
|
|
|
|
|
|
|
#define GLOW_TEXTURE_SAMPLE(m_tex, m_uv, m_lod) texture2D_bicubic(m_tex, m_uv, m_lod)
|
|
|
|
#else // MULTIVIEW
|
|
|
|
|
2019-08-27 20:27:35 +02:00
|
|
|
vec4 texture2D_bicubic(sampler2D tex, vec2 uv, int p_lod) {
|
|
|
|
float lod = float(p_lod);
|
|
|
|
vec2 tex_size = vec2(params.glow_texture_size >> p_lod);
|
|
|
|
vec2 pixel_size = vec2(1.0f) / tex_size;
|
|
|
|
|
|
|
|
uv = uv * tex_size + vec2(0.5f);
|
|
|
|
|
|
|
|
vec2 iuv = floor(uv);
|
|
|
|
vec2 fuv = fract(uv);
|
|
|
|
|
|
|
|
float g0x = g0(fuv.x);
|
|
|
|
float g1x = g1(fuv.x);
|
|
|
|
float h0x = h0(fuv.x);
|
|
|
|
float h1x = h1(fuv.x);
|
|
|
|
float h0y = h0(fuv.y);
|
|
|
|
float h1y = h1(fuv.y);
|
|
|
|
|
|
|
|
vec2 p0 = (vec2(iuv.x + h0x, iuv.y + h0y) - vec2(0.5f)) * pixel_size;
|
|
|
|
vec2 p1 = (vec2(iuv.x + h1x, iuv.y + h0y) - vec2(0.5f)) * pixel_size;
|
|
|
|
vec2 p2 = (vec2(iuv.x + h0x, iuv.y + h1y) - vec2(0.5f)) * pixel_size;
|
|
|
|
vec2 p3 = (vec2(iuv.x + h1x, iuv.y + h1y) - vec2(0.5f)) * pixel_size;
|
|
|
|
|
|
|
|
return (g0(fuv.y) * (g0x * textureLod(tex, p0, lod) + g1x * textureLod(tex, p1, lod))) +
|
2021-10-28 15:19:35 +02:00
|
|
|
(g1(fuv.y) * (g0x * textureLod(tex, p2, lod) + g1x * textureLod(tex, p3, lod)));
|
2019-08-27 20:27:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#define GLOW_TEXTURE_SAMPLE(m_tex, m_uv, m_lod) texture2D_bicubic(m_tex, m_uv, m_lod)
|
2022-04-29 09:10:54 +02:00
|
|
|
#endif // !MULTIVIEW
|
2019-08-27 20:27:35 +02:00
|
|
|
|
2022-04-29 09:10:54 +02:00
|
|
|
#else // USE_GLOW_FILTER_BICUBIC
|
2019-08-27 20:27:35 +02:00
|
|
|
|
2022-04-29 09:10:54 +02:00
|
|
|
#ifdef MULTIVIEW
|
|
|
|
#define GLOW_TEXTURE_SAMPLE(m_tex, m_uv, m_lod) textureLod(m_tex, vec3(m_uv, ViewIndex), float(m_lod))
|
|
|
|
#else // MULTIVIEW
|
2019-08-27 20:27:35 +02:00
|
|
|
#define GLOW_TEXTURE_SAMPLE(m_tex, m_uv, m_lod) textureLod(m_tex, m_uv, float(m_lod))
|
2022-04-29 09:10:54 +02:00
|
|
|
#endif // !MULTIVIEW
|
2019-08-27 20:27:35 +02:00
|
|
|
|
2022-04-29 09:10:54 +02:00
|
|
|
#endif // !USE_GLOW_FILTER_BICUBIC
|
2019-08-27 20:27:35 +02:00
|
|
|
|
|
|
|
vec3 tonemap_filmic(vec3 color, float white) {
|
|
|
|
// exposure bias: input scale (color *= bias, white *= bias) to make the brightness consistent with other tonemappers
|
|
|
|
// also useful to scale the input to the range that the tonemapper is designed for (some require very high input values)
|
|
|
|
// has no effect on the curve's general shape or visual properties
|
|
|
|
const float exposure_bias = 2.0f;
|
|
|
|
const float A = 0.22f * exposure_bias * exposure_bias; // bias baked into constants for performance
|
|
|
|
const float B = 0.30f * exposure_bias;
|
|
|
|
const float C = 0.10f;
|
|
|
|
const float D = 0.20f;
|
|
|
|
const float E = 0.01f;
|
|
|
|
const float F = 0.30f;
|
|
|
|
|
|
|
|
vec3 color_tonemapped = ((color * (A * color + C * B) + D * E) / (color * (A * color + B) + D * F)) - E / F;
|
|
|
|
float white_tonemapped = ((white * (A * white + C * B) + D * E) / (white * (A * white + B) + D * F)) - E / F;
|
|
|
|
|
2019-09-14 05:37:42 +02:00
|
|
|
return color_tonemapped / white_tonemapped;
|
2019-08-27 20:27:35 +02:00
|
|
|
}
|
|
|
|
|
2021-09-07 22:06:24 +02:00
|
|
|
// Adapted from https://github.com/TheRealMJP/BakingLab/blob/master/BakingLab/ACES.hlsl
|
|
|
|
// (MIT License).
|
2019-08-27 20:27:35 +02:00
|
|
|
vec3 tonemap_aces(vec3 color, float white) {
|
2021-09-07 22:06:24 +02:00
|
|
|
const float exposure_bias = 1.8f;
|
|
|
|
const float A = 0.0245786f;
|
|
|
|
const float B = 0.000090537f;
|
|
|
|
const float C = 0.983729f;
|
|
|
|
const float D = 0.432951f;
|
|
|
|
const float E = 0.238081f;
|
|
|
|
|
|
|
|
// Exposure bias baked into transform to save shader instructions. Equivalent to `color *= exposure_bias`
|
|
|
|
const mat3 rgb_to_rrt = mat3(
|
|
|
|
vec3(0.59719f * exposure_bias, 0.35458f * exposure_bias, 0.04823f * exposure_bias),
|
|
|
|
vec3(0.07600f * exposure_bias, 0.90834f * exposure_bias, 0.01566f * exposure_bias),
|
|
|
|
vec3(0.02840f * exposure_bias, 0.13383f * exposure_bias, 0.83777f * exposure_bias));
|
|
|
|
|
|
|
|
const mat3 odt_to_rgb = mat3(
|
|
|
|
vec3(1.60475f, -0.53108f, -0.07367f),
|
|
|
|
vec3(-0.10208f, 1.10813f, -0.00605f),
|
|
|
|
vec3(-0.00327f, -0.07276f, 1.07602f));
|
|
|
|
|
|
|
|
color *= rgb_to_rrt;
|
|
|
|
vec3 color_tonemapped = (color * (color + A) - B) / (color * (C * color + D) + E);
|
|
|
|
color_tonemapped *= odt_to_rgb;
|
|
|
|
|
|
|
|
white *= exposure_bias;
|
|
|
|
float white_tonemapped = (white * (white + A) - B) / (white * (C * white + D) + E);
|
2019-08-27 20:27:35 +02:00
|
|
|
|
2019-09-14 05:37:42 +02:00
|
|
|
return color_tonemapped / white_tonemapped;
|
2019-08-27 20:27:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
vec3 tonemap_reinhard(vec3 color, float white) {
|
|
|
|
return (white * color + color) / (color * white + white);
|
|
|
|
}
|
|
|
|
|
2019-09-07 03:51:27 +02:00
|
|
|
vec3 linear_to_srgb(vec3 color) {
|
|
|
|
//if going to srgb, clamp from 0 to 1.
|
2019-11-05 12:01:00 +01:00
|
|
|
color = clamp(color, vec3(0.0), vec3(1.0));
|
2019-08-27 20:27:35 +02:00
|
|
|
const vec3 a = vec3(0.055f);
|
|
|
|
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)));
|
|
|
|
}
|
|
|
|
|
|
|
|
#define TONEMAPPER_LINEAR 0
|
|
|
|
#define TONEMAPPER_REINHARD 1
|
|
|
|
#define TONEMAPPER_FILMIC 2
|
|
|
|
#define TONEMAPPER_ACES 3
|
|
|
|
|
|
|
|
vec3 apply_tonemapping(vec3 color, float white) { // inputs are LINEAR, always outputs clamped [0;1] color
|
2021-09-07 22:06:24 +02:00
|
|
|
// Ensure color values passed to tonemappers are positive.
|
|
|
|
// They can be negative in the case of negative lights, which leads to undesired behavior.
|
2019-11-05 12:01:00 +01:00
|
|
|
if (params.tonemapper == TONEMAPPER_LINEAR) {
|
2019-08-27 20:27:35 +02:00
|
|
|
return color;
|
2019-11-05 12:01:00 +01:00
|
|
|
} else if (params.tonemapper == TONEMAPPER_REINHARD) {
|
2021-09-07 22:06:24 +02:00
|
|
|
return tonemap_reinhard(max(vec3(0.0f), color), white);
|
2019-11-05 12:01:00 +01:00
|
|
|
} else if (params.tonemapper == TONEMAPPER_FILMIC) {
|
2021-09-07 22:06:24 +02:00
|
|
|
return tonemap_filmic(max(vec3(0.0f), color), white);
|
2021-08-09 17:01:07 +02:00
|
|
|
} else { // TONEMAPPER_ACES
|
2021-09-07 22:06:24 +02:00
|
|
|
return tonemap_aces(max(vec3(0.0f), color), white);
|
2019-08-27 20:27:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-29 09:10:54 +02:00
|
|
|
#ifdef MULTIVIEW
|
|
|
|
vec3 gather_glow(sampler2DArray tex, vec2 uv) { // sample all selected glow levels, view is added to uv later
|
|
|
|
#else
|
2019-08-27 20:27:35 +02:00
|
|
|
vec3 gather_glow(sampler2D tex, vec2 uv) { // sample all selected glow levels
|
2022-04-29 09:10:54 +02:00
|
|
|
#endif // defined(MULTIVIEW)
|
2019-08-27 20:27:35 +02:00
|
|
|
vec3 glow = vec3(0.0f);
|
|
|
|
|
2020-09-20 00:02:32 +02:00
|
|
|
if (params.glow_levels[0] > 0.0001) {
|
|
|
|
glow += GLOW_TEXTURE_SAMPLE(tex, uv, 0).rgb * params.glow_levels[0];
|
2019-08-27 20:27:35 +02:00
|
|
|
}
|
|
|
|
|
2020-09-20 00:02:32 +02:00
|
|
|
if (params.glow_levels[1] > 0.0001) {
|
|
|
|
glow += GLOW_TEXTURE_SAMPLE(tex, uv, 1).rgb * params.glow_levels[1];
|
2019-08-27 20:27:35 +02:00
|
|
|
}
|
|
|
|
|
2020-09-20 00:02:32 +02:00
|
|
|
if (params.glow_levels[2] > 0.0001) {
|
|
|
|
glow += GLOW_TEXTURE_SAMPLE(tex, uv, 2).rgb * params.glow_levels[2];
|
2019-08-27 20:27:35 +02:00
|
|
|
}
|
|
|
|
|
2020-09-20 00:02:32 +02:00
|
|
|
if (params.glow_levels[3] > 0.0001) {
|
|
|
|
glow += GLOW_TEXTURE_SAMPLE(tex, uv, 3).rgb * params.glow_levels[3];
|
2019-08-27 20:27:35 +02:00
|
|
|
}
|
|
|
|
|
2020-09-20 00:02:32 +02:00
|
|
|
if (params.glow_levels[4] > 0.0001) {
|
|
|
|
glow += GLOW_TEXTURE_SAMPLE(tex, uv, 4).rgb * params.glow_levels[4];
|
2019-08-27 20:27:35 +02:00
|
|
|
}
|
|
|
|
|
2020-09-20 00:02:32 +02:00
|
|
|
if (params.glow_levels[5] > 0.0001) {
|
|
|
|
glow += GLOW_TEXTURE_SAMPLE(tex, uv, 5).rgb * params.glow_levels[5];
|
2019-08-27 20:27:35 +02:00
|
|
|
}
|
|
|
|
|
2020-09-20 00:02:32 +02:00
|
|
|
if (params.glow_levels[6] > 0.0001) {
|
|
|
|
glow += GLOW_TEXTURE_SAMPLE(tex, uv, 6).rgb * params.glow_levels[6];
|
2019-08-27 20:27:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return glow;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define GLOW_MODE_ADD 0
|
|
|
|
#define GLOW_MODE_SCREEN 1
|
|
|
|
#define GLOW_MODE_SOFTLIGHT 2
|
|
|
|
#define GLOW_MODE_REPLACE 3
|
2020-01-10 01:40:26 +01:00
|
|
|
#define GLOW_MODE_MIX 4
|
2019-08-27 20:27:35 +02:00
|
|
|
|
|
|
|
vec3 apply_glow(vec3 color, vec3 glow) { // apply glow using the selected blending mode
|
2019-11-05 12:01:00 +01:00
|
|
|
if (params.glow_mode == GLOW_MODE_ADD) {
|
2019-08-27 20:27:35 +02:00
|
|
|
return color + glow;
|
2019-11-05 12:01:00 +01:00
|
|
|
} else if (params.glow_mode == GLOW_MODE_SCREEN) {
|
2019-08-27 20:27:35 +02:00
|
|
|
//need color clamping
|
|
|
|
return max((color + glow) - (color * glow), vec3(0.0));
|
2019-11-05 12:01:00 +01:00
|
|
|
} else if (params.glow_mode == GLOW_MODE_SOFTLIGHT) {
|
2019-08-27 20:27:35 +02:00
|
|
|
//need color clamping
|
|
|
|
glow = glow * vec3(0.5f) + vec3(0.5f);
|
|
|
|
|
|
|
|
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)));
|
|
|
|
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)));
|
|
|
|
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)));
|
|
|
|
return color;
|
|
|
|
} else { //replace
|
|
|
|
return glow;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vec3 apply_bcs(vec3 color, vec3 bcs) {
|
|
|
|
color = mix(vec3(0.0f), color, bcs.x);
|
|
|
|
color = mix(vec3(0.5f), color, bcs.y);
|
|
|
|
color = mix(vec3(dot(vec3(1.0f), color) * 0.33333f), color, bcs.z);
|
|
|
|
|
|
|
|
return color;
|
|
|
|
}
|
2020-11-23 02:51:31 +01:00
|
|
|
#ifdef USE_1D_LUT
|
|
|
|
vec3 apply_color_correction(vec3 color) {
|
|
|
|
color.r = texture(source_color_correction, vec2(color.r, 0.0f)).r;
|
|
|
|
color.g = texture(source_color_correction, vec2(color.g, 0.0f)).g;
|
|
|
|
color.b = texture(source_color_correction, vec2(color.b, 0.0f)).b;
|
|
|
|
return color;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
vec3 apply_color_correction(vec3 color) {
|
|
|
|
return textureLod(source_color_correction, color, 0.0).rgb;
|
2019-08-27 20:27:35 +02:00
|
|
|
}
|
2020-11-23 02:51:31 +01:00
|
|
|
#endif
|
2019-08-27 20:27:35 +02:00
|
|
|
|
2021-06-26 12:49:25 +02:00
|
|
|
#ifndef SUBPASS
|
2020-04-12 06:49:10 +02:00
|
|
|
vec3 do_fxaa(vec3 color, float exposure, vec2 uv_interp) {
|
|
|
|
const float FXAA_REDUCE_MIN = (1.0 / 128.0);
|
|
|
|
const float FXAA_REDUCE_MUL = (1.0 / 8.0);
|
|
|
|
const float FXAA_SPAN_MAX = 8.0;
|
|
|
|
|
2021-05-07 15:19:04 +02:00
|
|
|
#ifdef MULTIVIEW
|
2022-09-26 20:47:22 +02:00
|
|
|
vec3 rgbNW = textureLod(source_color, vec3(uv_interp + vec2(-0.5, -0.5) * params.pixel_size, ViewIndex), 0.0).xyz * exposure * params.luminance_multiplier;
|
|
|
|
vec3 rgbNE = textureLod(source_color, vec3(uv_interp + vec2(0.5, -0.5) * params.pixel_size, ViewIndex), 0.0).xyz * exposure * params.luminance_multiplier;
|
|
|
|
vec3 rgbSW = textureLod(source_color, vec3(uv_interp + vec2(-0.5, 0.5) * params.pixel_size, ViewIndex), 0.0).xyz * exposure * params.luminance_multiplier;
|
|
|
|
vec3 rgbSE = textureLod(source_color, vec3(uv_interp + vec2(0.5, 0.5) * params.pixel_size, ViewIndex), 0.0).xyz * exposure * params.luminance_multiplier;
|
2021-05-07 15:19:04 +02:00
|
|
|
#else
|
2022-09-26 20:47:22 +02:00
|
|
|
vec3 rgbNW = textureLod(source_color, uv_interp + vec2(-0.5, -0.5) * params.pixel_size, 0.0).xyz * exposure * params.luminance_multiplier;
|
|
|
|
vec3 rgbNE = textureLod(source_color, uv_interp + vec2(0.5, -0.5) * params.pixel_size, 0.0).xyz * exposure * params.luminance_multiplier;
|
|
|
|
vec3 rgbSW = textureLod(source_color, uv_interp + vec2(-0.5, 0.5) * params.pixel_size, 0.0).xyz * exposure * params.luminance_multiplier;
|
|
|
|
vec3 rgbSE = textureLod(source_color, uv_interp + vec2(0.5, 0.5) * params.pixel_size, 0.0).xyz * exposure * params.luminance_multiplier;
|
2021-05-07 15:19:04 +02:00
|
|
|
#endif
|
2020-04-12 06:49:10 +02:00
|
|
|
vec3 rgbM = color;
|
|
|
|
vec3 luma = vec3(0.299, 0.587, 0.114);
|
|
|
|
float lumaNW = dot(rgbNW, luma);
|
|
|
|
float lumaNE = dot(rgbNE, luma);
|
|
|
|
float lumaSW = dot(rgbSW, luma);
|
|
|
|
float lumaSE = dot(rgbSE, luma);
|
|
|
|
float lumaM = dot(rgbM, luma);
|
|
|
|
float lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE)));
|
|
|
|
float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE)));
|
|
|
|
|
|
|
|
vec2 dir;
|
|
|
|
dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE));
|
|
|
|
dir.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE));
|
|
|
|
|
|
|
|
float dirReduce = max((lumaNW + lumaNE + lumaSW + lumaSE) *
|
2021-10-28 15:19:35 +02:00
|
|
|
(0.25 * FXAA_REDUCE_MUL),
|
2020-04-12 06:49:10 +02:00
|
|
|
FXAA_REDUCE_MIN);
|
|
|
|
|
|
|
|
float rcpDirMin = 1.0 / (min(abs(dir.x), abs(dir.y)) + dirReduce);
|
|
|
|
dir = min(vec2(FXAA_SPAN_MAX, FXAA_SPAN_MAX),
|
|
|
|
max(vec2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX),
|
|
|
|
dir * rcpDirMin)) *
|
2021-10-28 15:19:35 +02:00
|
|
|
params.pixel_size;
|
2020-04-12 06:49:10 +02:00
|
|
|
|
2021-05-07 15:19:04 +02:00
|
|
|
#ifdef MULTIVIEW
|
2021-07-26 13:31:15 +02:00
|
|
|
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;
|
|
|
|
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;
|
2021-05-07 15:19:04 +02:00
|
|
|
#else
|
2021-07-26 13:31:15 +02:00
|
|
|
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;
|
|
|
|
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;
|
2021-05-07 15:19:04 +02:00
|
|
|
#endif
|
2020-04-12 06:49:10 +02:00
|
|
|
|
|
|
|
float lumaB = dot(rgbB, luma);
|
2020-05-18 10:56:22 +02:00
|
|
|
if ((lumaB < lumaMin) || (lumaB > lumaMax)) {
|
2020-04-12 06:49:10 +02:00
|
|
|
return rgbA;
|
2020-05-18 10:56:22 +02:00
|
|
|
} else {
|
2020-04-12 06:49:10 +02:00
|
|
|
return rgbB;
|
2020-05-18 10:56:22 +02:00
|
|
|
}
|
2020-04-12 06:49:10 +02:00
|
|
|
}
|
2021-06-26 12:49:25 +02:00
|
|
|
#endif // !SUBPASS
|
2020-04-12 06:49:10 +02:00
|
|
|
|
2021-08-22 03:56:25 +02:00
|
|
|
// From https://alex.vlachos.com/graphics/Alex_Vlachos_Advanced_VR_Rendering_GDC2015.pdf
|
2020-04-20 23:34:47 +02:00
|
|
|
// and https://www.shadertoy.com/view/MslGR8 (5th one starting from the bottom)
|
|
|
|
// NOTE: `frag_coord` is in pixels (i.e. not normalized UV).
|
|
|
|
vec3 screen_space_dither(vec2 frag_coord) {
|
|
|
|
// Iestyn's RGB dither (7 asm instructions) from Portal 2 X360, slightly modified for VR.
|
|
|
|
vec3 dither = vec3(dot(vec2(171.0, 231.0), frag_coord));
|
|
|
|
dither.rgb = fract(dither.rgb / vec3(103.0, 71.0, 97.0));
|
|
|
|
|
2020-10-19 22:38:20 +02:00
|
|
|
// Subtract 0.5 to avoid slightly brightening the whole viewport.
|
|
|
|
return (dither.rgb - 0.5) / 255.0;
|
2020-04-20 23:34:47 +02:00
|
|
|
}
|
|
|
|
|
2019-08-27 20:27:35 +02:00
|
|
|
void main() {
|
2021-06-26 12:49:25 +02:00
|
|
|
#ifdef SUBPASS
|
|
|
|
// SUBPASS and MULTIVIEW can be combined but in that case we're already reading from the correct layer
|
2022-05-17 09:55:50 +02:00
|
|
|
vec4 color = subpassLoad(input_color);
|
2021-08-22 13:30:58 +02:00
|
|
|
#elif defined(MULTIVIEW)
|
2022-05-17 09:55:50 +02:00
|
|
|
vec4 color = textureLod(source_color, vec3(uv_interp, ViewIndex), 0.0f);
|
2021-05-07 15:19:04 +02:00
|
|
|
#else
|
2022-05-17 09:55:50 +02:00
|
|
|
vec4 color = textureLod(source_color, uv_interp, 0.0f);
|
2021-05-07 15:19:04 +02:00
|
|
|
#endif
|
2022-05-17 09:55:50 +02:00
|
|
|
color.rgb *= params.luminance_multiplier;
|
2019-08-27 20:27:35 +02:00
|
|
|
|
|
|
|
// Exposure
|
|
|
|
|
2020-04-12 06:49:10 +02:00
|
|
|
float exposure = params.exposure;
|
|
|
|
|
2021-06-26 12:49:25 +02:00
|
|
|
#ifndef SUBPASS
|
2019-08-27 20:27:35 +02:00
|
|
|
if (params.use_auto_exposure) {
|
2022-08-01 01:20:24 +02:00
|
|
|
exposure *= 1.0 / (texelFetch(source_auto_exposure, ivec2(0, 0), 0).r * params.luminance_multiplier / params.auto_exposure_scale);
|
2019-08-27 20:27:35 +02:00
|
|
|
}
|
2021-06-26 12:49:25 +02:00
|
|
|
#endif
|
2019-08-27 20:27:35 +02:00
|
|
|
|
2022-05-17 09:55:50 +02:00
|
|
|
color.rgb *= exposure;
|
2019-08-27 20:27:35 +02:00
|
|
|
|
|
|
|
// Early Tonemap & SRGB Conversion
|
2021-06-26 12:49:25 +02:00
|
|
|
#ifndef SUBPASS
|
2022-06-15 22:23:56 +02:00
|
|
|
if (params.use_fxaa) {
|
|
|
|
// FXAA must be performed before glow to preserve the "bleed" effect of glow.
|
|
|
|
color.rgb = do_fxaa(color.rgb, exposure, uv_interp);
|
|
|
|
}
|
|
|
|
|
2020-01-10 01:40:26 +01:00
|
|
|
if (params.use_glow && params.glow_mode == GLOW_MODE_MIX) {
|
2021-07-26 13:31:15 +02:00
|
|
|
vec3 glow = gather_glow(source_glow, uv_interp) * params.luminance_multiplier;
|
2022-01-20 16:47:25 +01:00
|
|
|
if (params.glow_map_strength > 0.001) {
|
|
|
|
glow = mix(glow, texture(glow_map, uv_interp).rgb * glow, params.glow_map_strength);
|
|
|
|
}
|
2020-02-11 14:01:43 +01:00
|
|
|
color.rgb = mix(color.rgb, glow, params.glow_intensity);
|
2020-01-10 01:40:26 +01:00
|
|
|
}
|
2021-06-26 12:49:25 +02:00
|
|
|
#endif
|
|
|
|
|
2022-05-17 09:55:50 +02:00
|
|
|
color.rgb = apply_tonemapping(color.rgb, params.white);
|
2019-08-27 20:27:35 +02:00
|
|
|
|
2022-05-17 09:55:50 +02:00
|
|
|
color.rgb = linear_to_srgb(color.rgb); // regular linear -> SRGB conversion
|
2019-08-27 20:27:35 +02:00
|
|
|
|
2021-06-26 12:49:25 +02:00
|
|
|
#ifndef SUBPASS
|
2019-08-27 20:27:35 +02:00
|
|
|
// Glow
|
2020-01-10 01:40:26 +01:00
|
|
|
if (params.use_glow && params.glow_mode != GLOW_MODE_MIX) {
|
2021-07-26 13:31:15 +02:00
|
|
|
vec3 glow = gather_glow(source_glow, uv_interp) * params.glow_intensity * params.luminance_multiplier;
|
2022-01-20 16:47:25 +01:00
|
|
|
if (params.glow_map_strength > 0.001) {
|
|
|
|
glow = mix(glow, texture(glow_map, uv_interp).rgb * glow, params.glow_map_strength);
|
|
|
|
}
|
2019-08-27 20:27:35 +02:00
|
|
|
|
|
|
|
// high dynamic range -> SRGB
|
|
|
|
glow = apply_tonemapping(glow, params.white);
|
|
|
|
glow = linear_to_srgb(glow);
|
|
|
|
|
2022-05-17 09:55:50 +02:00
|
|
|
color.rgb = apply_glow(color.rgb, glow);
|
2019-08-27 20:27:35 +02:00
|
|
|
}
|
2021-06-26 12:49:25 +02:00
|
|
|
#endif
|
2019-08-27 20:27:35 +02:00
|
|
|
|
|
|
|
// Additional effects
|
|
|
|
|
|
|
|
if (params.use_bcs) {
|
2022-05-17 09:55:50 +02:00
|
|
|
color.rgb = apply_bcs(color.rgb, params.bcs);
|
2019-08-27 20:27:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (params.use_color_correction) {
|
2022-05-17 09:55:50 +02:00
|
|
|
color.rgb = apply_color_correction(color.rgb);
|
2019-08-27 20:27:35 +02:00
|
|
|
}
|
|
|
|
|
2022-09-23 20:43:27 +02:00
|
|
|
if (params.use_debanding) {
|
|
|
|
// Debanding should be done at the end of tonemapping, but before writing to the LDR buffer.
|
|
|
|
// Otherwise, we're adding noise to an already-quantized image.
|
|
|
|
color.rgb += screen_space_dither(gl_FragCoord.xy);
|
|
|
|
}
|
|
|
|
|
2022-05-17 09:55:50 +02:00
|
|
|
frag_color = color;
|
2019-08-27 20:27:35 +02:00
|
|
|
}
|