2018-08-27 07:31:48 +02:00
|
|
|
/* clang-format off */
|
2016-12-07 21:49:52 +01:00
|
|
|
[vertex]
|
|
|
|
|
2018-08-24 13:42:18 +02:00
|
|
|
layout(location = 0) in highp vec4 vertex_attrib;
|
2018-08-27 07:31:48 +02:00
|
|
|
/* clang-format on */
|
2018-08-24 13:42:18 +02:00
|
|
|
layout(location = 4) in vec2 uv_in;
|
2016-12-08 13:48:38 +01:00
|
|
|
|
|
|
|
out vec2 uv_interp;
|
|
|
|
|
2018-08-24 13:42:18 +02:00
|
|
|
void main() {
|
2016-12-07 21:49:52 +01:00
|
|
|
gl_Position = vertex_attrib;
|
2018-07-12 17:10:00 +02:00
|
|
|
|
2016-12-08 13:48:38 +01:00
|
|
|
uv_interp = uv_in;
|
2016-12-07 21:49:52 +01:00
|
|
|
|
2018-08-24 13:42:18 +02:00
|
|
|
#ifdef V_FLIP
|
|
|
|
uv_interp.y = 1.0f - uv_interp.y;
|
|
|
|
#endif
|
2016-12-07 21:49:52 +01:00
|
|
|
}
|
|
|
|
|
2018-08-27 07:31:48 +02:00
|
|
|
/* clang-format off */
|
2016-12-07 21:49:52 +01:00
|
|
|
[fragment]
|
|
|
|
|
2017-07-22 19:07:38 +02:00
|
|
|
#if !defined(GLES_OVER_GL)
|
2018-08-24 13:42:18 +02:00
|
|
|
precision mediump float;
|
2017-07-22 19:07:38 +02:00
|
|
|
#endif
|
2018-08-27 07:31:48 +02:00
|
|
|
/* clang-format on */
|
2017-07-22 19:07:38 +02:00
|
|
|
|
2016-12-08 13:48:38 +01:00
|
|
|
in vec2 uv_interp;
|
|
|
|
|
2016-12-07 21:49:52 +01:00
|
|
|
uniform highp sampler2D source; //texunit:0
|
|
|
|
|
|
|
|
uniform float exposure;
|
|
|
|
uniform float white;
|
|
|
|
|
|
|
|
#ifdef USE_AUTO_EXPOSURE
|
2018-08-24 13:42:18 +02:00
|
|
|
uniform highp sampler2D source_auto_exposure; //texunit:1
|
|
|
|
uniform highp float auto_exposure_grey;
|
2016-12-07 21:49:52 +01:00
|
|
|
#endif
|
|
|
|
|
2016-12-08 13:48:38 +01:00
|
|
|
#if defined(USE_GLOW_LEVEL1) || defined(USE_GLOW_LEVEL2) || defined(USE_GLOW_LEVEL3) || defined(USE_GLOW_LEVEL4) || defined(USE_GLOW_LEVEL5) || defined(USE_GLOW_LEVEL6) || defined(USE_GLOW_LEVEL7)
|
2018-08-24 13:42:18 +02:00
|
|
|
#define USING_GLOW // only use glow when at least one glow level is selected
|
2016-12-08 13:48:38 +01:00
|
|
|
|
2018-08-24 13:42:18 +02:00
|
|
|
uniform highp sampler2D source_glow; //texunit:2
|
|
|
|
uniform highp float glow_intensity;
|
2016-12-08 13:48:38 +01:00
|
|
|
#endif
|
2016-12-07 21:49:52 +01:00
|
|
|
|
2017-06-06 03:33:01 +02:00
|
|
|
#ifdef USE_BCS
|
2018-08-24 13:42:18 +02:00
|
|
|
uniform vec3 bcs;
|
2017-06-06 03:33:01 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef USE_COLOR_CORRECTION
|
2018-08-24 13:42:18 +02:00
|
|
|
uniform sampler2D color_correction; //texunit:3
|
2017-06-06 03:33:01 +02:00
|
|
|
#endif
|
|
|
|
|
2018-08-24 13:42:18 +02:00
|
|
|
layout(location = 0) out vec4 frag_color;
|
2016-12-07 21:49:52 +01:00
|
|
|
|
2016-12-10 05:13:20 +01:00
|
|
|
#ifdef USE_GLOW_FILTER_BICUBIC
|
2018-08-24 13:42:18 +02:00
|
|
|
// 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));
|
|
|
|
}
|
|
|
|
|
|
|
|
uniform ivec2 glow_texture_size;
|
|
|
|
|
|
|
|
vec4 texture2D_bicubic(sampler2D tex, vec2 uv, int p_lod) {
|
|
|
|
float lod = float(p_lod);
|
|
|
|
vec2 tex_size = vec2(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;
|
|
|
|
|
2018-08-27 07:31:48 +02:00
|
|
|
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)));
|
2018-08-24 13:42:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#define GLOW_TEXTURE_SAMPLE(m_tex, m_uv, m_lod) texture2D_bicubic(m_tex, m_uv, m_lod)
|
2016-12-10 05:13:20 +01:00
|
|
|
#else
|
2018-08-24 13:42:18 +02:00
|
|
|
#define GLOW_TEXTURE_SAMPLE(m_tex, m_uv, m_lod) textureLod(m_tex, m_uv, float(m_lod))
|
2016-12-10 05:13:20 +01:00
|
|
|
#endif
|
|
|
|
|
2018-08-24 13:42:18 +02:00
|
|
|
vec3 tonemap_filmic(vec3 color, float white) {
|
2019-02-13 09:23:29 +01:00
|
|
|
// exposure bias: input scale (color *= bias, white *= bias) to make the brightness consistent with other tonemappers
|
2018-08-26 12:29:20 +02:00
|
|
|
// 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;
|
2018-07-12 17:10:00 +02:00
|
|
|
const float C = 0.10f;
|
|
|
|
const float D = 0.20f;
|
2018-08-26 12:29:20 +02:00
|
|
|
const float E = 0.01f;
|
2018-07-12 17:10:00 +02:00
|
|
|
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;
|
|
|
|
|
|
|
|
return clamp(color_tonemapped / white_tonemapped, vec3(0.0f), vec3(1.0f));
|
|
|
|
}
|
2016-12-07 21:49:52 +01:00
|
|
|
|
2018-08-24 13:42:18 +02:00
|
|
|
vec3 tonemap_aces(vec3 color, float white) {
|
2018-08-26 12:29:20 +02:00
|
|
|
const float exposure_bias = 0.85f;
|
|
|
|
const float A = 2.51f * exposure_bias * exposure_bias;
|
|
|
|
const float B = 0.03f * exposure_bias;
|
|
|
|
const float C = 2.43f * exposure_bias * exposure_bias;
|
|
|
|
const float D = 0.59f * exposure_bias;
|
2018-07-12 17:10:00 +02:00
|
|
|
const float E = 0.14f;
|
2017-07-26 05:39:41 +02:00
|
|
|
|
2018-07-12 17:10:00 +02:00
|
|
|
vec3 color_tonemapped = (color * (A * color + B)) / (color * (C * color + D) + E);
|
|
|
|
float white_tonemapped = (white * (A * white + B)) / (white * (C * white + D) + E);
|
2017-07-26 05:39:41 +02:00
|
|
|
|
2018-07-12 17:10:00 +02:00
|
|
|
return clamp(color_tonemapped / white_tonemapped, vec3(0.0f), vec3(1.0f));
|
2017-07-26 05:39:41 +02:00
|
|
|
}
|
|
|
|
|
2018-08-26 12:29:20 +02:00
|
|
|
vec3 tonemap_reinhard(vec3 color, float white) {
|
|
|
|
return clamp((white * color + color) / (color * white + white), vec3(0.0f), vec3(1.0f));
|
2017-07-26 05:39:41 +02:00
|
|
|
}
|
|
|
|
|
2018-08-24 13:42:18 +02:00
|
|
|
vec3 linear_to_srgb(vec3 color) { // convert linear rgb to srgb, assumes clamped input in range [0;1]
|
2018-07-12 17:10:00 +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)));
|
2017-07-26 05:39:41 +02:00
|
|
|
}
|
|
|
|
|
2018-08-24 13:42:18 +02:00
|
|
|
vec3 apply_tonemapping(vec3 color, float white) { // inputs are LINEAR, always outputs clamped [0;1] color
|
2018-08-26 12:29:20 +02:00
|
|
|
#ifdef USE_REINHARD_TONEMAPPER
|
|
|
|
return tonemap_reinhard(color, white);
|
2018-08-24 13:42:18 +02:00
|
|
|
#endif
|
2016-12-08 13:48:38 +01:00
|
|
|
|
2018-08-24 13:42:18 +02:00
|
|
|
#ifdef USE_FILMIC_TONEMAPPER
|
|
|
|
return tonemap_filmic(color, white);
|
|
|
|
#endif
|
2016-12-08 13:48:38 +01:00
|
|
|
|
2018-08-24 13:42:18 +02:00
|
|
|
#ifdef USE_ACES_TONEMAPPER
|
|
|
|
return tonemap_aces(color, white);
|
|
|
|
#endif
|
2016-12-08 13:48:38 +01:00
|
|
|
|
2018-09-13 03:38:39 +02:00
|
|
|
return clamp(color, vec3(0.0f), vec3(1.0f)); // no other selected -> linear
|
2018-07-12 17:10:00 +02:00
|
|
|
}
|
2016-12-08 13:48:38 +01:00
|
|
|
|
2018-08-24 13:42:18 +02:00
|
|
|
vec3 gather_glow(sampler2D tex, vec2 uv) { // sample all selected glow levels
|
2018-07-12 17:10:00 +02:00
|
|
|
vec3 glow = vec3(0.0f);
|
2017-07-26 05:39:41 +02:00
|
|
|
|
2018-08-24 13:42:18 +02:00
|
|
|
#ifdef USE_GLOW_LEVEL1
|
|
|
|
glow += GLOW_TEXTURE_SAMPLE(tex, uv, 1).rgb;
|
|
|
|
#endif
|
2016-12-08 13:48:38 +01:00
|
|
|
|
2018-08-24 13:42:18 +02:00
|
|
|
#ifdef USE_GLOW_LEVEL2
|
|
|
|
glow += GLOW_TEXTURE_SAMPLE(tex, uv, 2).rgb;
|
|
|
|
#endif
|
2016-12-08 13:48:38 +01:00
|
|
|
|
2018-08-24 13:42:18 +02:00
|
|
|
#ifdef USE_GLOW_LEVEL3
|
|
|
|
glow += GLOW_TEXTURE_SAMPLE(tex, uv, 3).rgb;
|
|
|
|
#endif
|
2016-12-08 13:48:38 +01:00
|
|
|
|
2018-08-24 13:42:18 +02:00
|
|
|
#ifdef USE_GLOW_LEVEL4
|
|
|
|
glow += GLOW_TEXTURE_SAMPLE(tex, uv, 4).rgb;
|
|
|
|
#endif
|
2017-07-26 05:39:41 +02:00
|
|
|
|
2018-08-24 13:42:18 +02:00
|
|
|
#ifdef USE_GLOW_LEVEL5
|
|
|
|
glow += GLOW_TEXTURE_SAMPLE(tex, uv, 5).rgb;
|
|
|
|
#endif
|
2016-12-08 13:48:38 +01:00
|
|
|
|
2018-08-24 13:42:18 +02:00
|
|
|
#ifdef USE_GLOW_LEVEL6
|
|
|
|
glow += GLOW_TEXTURE_SAMPLE(tex, uv, 6).rgb;
|
|
|
|
#endif
|
2016-12-08 13:48:38 +01:00
|
|
|
|
2018-08-24 13:42:18 +02:00
|
|
|
#ifdef USE_GLOW_LEVEL7
|
|
|
|
glow += GLOW_TEXTURE_SAMPLE(tex, uv, 7).rgb;
|
|
|
|
#endif
|
2016-12-08 13:48:38 +01:00
|
|
|
|
2018-07-12 17:10:00 +02:00
|
|
|
return glow;
|
|
|
|
}
|
2016-12-08 13:48:38 +01:00
|
|
|
|
2018-08-24 13:42:18 +02:00
|
|
|
vec3 apply_glow(vec3 color, vec3 glow) { // apply glow using the selected blending mode
|
|
|
|
#ifdef USE_GLOW_REPLACE
|
|
|
|
color = glow;
|
|
|
|
#endif
|
2016-12-08 13:48:38 +01:00
|
|
|
|
2018-08-24 13:42:18 +02:00
|
|
|
#ifdef USE_GLOW_SCREEN
|
|
|
|
color = max((color + glow) - (color * glow), vec3(0.0));
|
|
|
|
#endif
|
2016-12-08 13:48:38 +01:00
|
|
|
|
2018-08-24 13:42:18 +02:00
|
|
|
#ifdef USE_GLOW_SOFTLIGHT
|
|
|
|
glow = glow * vec3(0.5f) + vec3(0.5f);
|
2016-12-08 13:48:38 +01:00
|
|
|
|
2018-08-24 13:42:18 +02:00
|
|
|
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)));
|
|
|
|
#endif
|
2016-12-08 13:48:38 +01:00
|
|
|
|
2018-08-24 13:42:18 +02:00
|
|
|
#if !defined(USE_GLOW_SCREEN) && !defined(USE_GLOW_SOFTLIGHT) && !defined(USE_GLOW_REPLACE) // no other selected -> additive
|
|
|
|
color += glow;
|
|
|
|
#endif
|
2016-12-08 13:48:38 +01:00
|
|
|
|
2018-07-12 17:10:00 +02:00
|
|
|
return color;
|
|
|
|
}
|
2016-12-07 21:49:52 +01:00
|
|
|
|
2018-08-24 13:42:18 +02:00
|
|
|
vec3 apply_bcs(vec3 color, vec3 bcs) {
|
2018-07-12 17:10:00 +02:00
|
|
|
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);
|
2016-12-07 21:49:52 +01:00
|
|
|
|
2018-07-12 17:10:00 +02:00
|
|
|
return color;
|
|
|
|
}
|
2016-12-07 21:49:52 +01:00
|
|
|
|
2018-08-24 13:42:18 +02:00
|
|
|
vec3 apply_color_correction(vec3 color, sampler2D correction_tex) {
|
2018-07-12 17:10:00 +02:00
|
|
|
color.r = texture(correction_tex, vec2(color.r, 0.0f)).r;
|
|
|
|
color.g = texture(correction_tex, vec2(color.g, 0.0f)).g;
|
|
|
|
color.b = texture(correction_tex, vec2(color.b, 0.0f)).b;
|
2016-12-07 21:49:52 +01:00
|
|
|
|
2018-07-12 17:10:00 +02:00
|
|
|
return color;
|
|
|
|
}
|
2016-12-07 21:49:52 +01:00
|
|
|
|
2018-08-24 13:42:18 +02:00
|
|
|
void main() {
|
2018-07-12 17:10:00 +02:00
|
|
|
vec3 color = textureLod(source, uv_interp, 0.0f).rgb;
|
2016-12-07 21:49:52 +01:00
|
|
|
|
2018-07-12 17:10:00 +02:00
|
|
|
// Exposure
|
2016-12-07 21:49:52 +01:00
|
|
|
|
2018-08-24 13:42:18 +02:00
|
|
|
#ifdef USE_AUTO_EXPOSURE
|
|
|
|
color /= texelFetch(source_auto_exposure, ivec2(0, 0), 0).r / auto_exposure_grey;
|
|
|
|
#endif
|
2016-12-07 21:49:52 +01:00
|
|
|
|
2018-07-12 17:10:00 +02:00
|
|
|
color *= exposure;
|
2016-12-07 21:49:52 +01:00
|
|
|
|
2018-07-12 17:10:00 +02:00
|
|
|
// Early Tonemap & SRGB Conversion
|
2017-07-26 05:39:41 +02:00
|
|
|
|
2018-07-12 17:10:00 +02:00
|
|
|
color = apply_tonemapping(color, white);
|
2016-12-07 21:49:52 +01:00
|
|
|
|
2018-08-24 13:42:18 +02:00
|
|
|
#ifdef KEEP_3D_LINEAR
|
|
|
|
// leave color as is (-> don't convert to SRGB)
|
|
|
|
#else
|
|
|
|
color = linear_to_srgb(color); // regular linear -> SRGB conversion
|
|
|
|
#endif
|
2016-12-07 21:49:52 +01:00
|
|
|
|
2018-07-12 17:10:00 +02:00
|
|
|
// Glow
|
2016-12-07 21:49:52 +01:00
|
|
|
|
2018-08-24 13:42:18 +02:00
|
|
|
#ifdef USING_GLOW
|
|
|
|
vec3 glow = gather_glow(source_glow, uv_interp) * glow_intensity;
|
2017-06-06 03:33:01 +02:00
|
|
|
|
2018-08-24 13:42:18 +02:00
|
|
|
// high dynamic range -> SRGB
|
|
|
|
glow = apply_tonemapping(glow, white);
|
|
|
|
glow = linear_to_srgb(glow);
|
2017-06-06 03:33:01 +02:00
|
|
|
|
2018-08-24 13:42:18 +02:00
|
|
|
color = apply_glow(color, glow);
|
|
|
|
#endif
|
2017-06-06 03:33:01 +02:00
|
|
|
|
2018-07-12 17:10:00 +02:00
|
|
|
// Additional effects
|
2017-06-06 03:33:01 +02:00
|
|
|
|
2018-08-24 13:42:18 +02:00
|
|
|
#ifdef USE_BCS
|
|
|
|
color = apply_bcs(color, bcs);
|
|
|
|
#endif
|
2016-12-07 21:49:52 +01:00
|
|
|
|
2018-08-24 13:42:18 +02:00
|
|
|
#ifdef USE_COLOR_CORRECTION
|
|
|
|
color = apply_color_correction(color, color_correction);
|
|
|
|
#endif
|
2016-12-07 21:49:52 +01:00
|
|
|
|
2018-07-12 17:10:00 +02:00
|
|
|
frag_color = vec4(color, 1.0f);
|
2016-12-07 21:49:52 +01:00
|
|
|
}
|