diff options
author | clayjohn <claynjohn@gmail.com> | 2019-08-31 15:03:55 -0700 |
---|---|---|
committer | clayjohn <claynjohn@gmail.com> | 2019-09-30 08:04:31 -0700 |
commit | 82f63633d187013cbcd0f3b74549ad0453a879e0 (patch) | |
tree | 6064f1bc93e61e009f83e8e79977b2b34def9891 /drivers/gles2/shaders/tonemap.glsl | |
parent | c9e1aced53929159f3deaba21df258c991e7ef7f (diff) |
Implement DOF blur, Glow, and BCS in GLES2
Diffstat (limited to 'drivers/gles2/shaders/tonemap.glsl')
-rw-r--r-- | drivers/gles2/shaders/tonemap.glsl | 296 |
1 files changed, 140 insertions, 156 deletions
diff --git a/drivers/gles2/shaders/tonemap.glsl b/drivers/gles2/shaders/tonemap.glsl index eae3b5a1ca..585d821626 100644 --- a/drivers/gles2/shaders/tonemap.glsl +++ b/drivers/gles2/shaders/tonemap.glsl @@ -1,66 +1,103 @@ /* clang-format off */ [vertex] -layout(location = 0) in highp vec4 vertex_attrib; +#ifdef USE_GLES_OVER_GL +#define lowp +#define mediump +#define highp +#else +precision highp float; +precision highp int; +#endif + +attribute vec2 vertex_attrib; // attrib:0 /* clang-format on */ -layout(location = 4) in vec2 uv_in; +attribute vec2 uv_in; // attrib:4 -out vec2 uv_interp; +varying vec2 uv_interp; void main() { + gl_Position = vec4(vertex_attrib, 0.0, 1.0); - gl_Position = vertex_attrib; uv_interp = uv_in; -#ifdef V_FLIP - uv_interp.y = 1.0 - uv_interp.y; -#endif } /* clang-format off */ [fragment] -#if !defined(GLES_OVER_GL) -precision mediump float; + +// texture2DLodEXT and textureCubeLodEXT are fragment shader specific. +// Do not copy these defines in the vertex section. +#ifndef USE_GLES_OVER_GL +#ifdef GL_EXT_shader_texture_lod +#extension GL_EXT_shader_texture_lod : enable +#define texture2DLod(img, coord, lod) texture2DLodEXT(img, coord, lod) +#define textureCubeLod(img, coord, lod) textureCubeLodEXT(img, coord, lod) #endif +#endif // !USE_GLES_OVER_GL -in vec2 uv_interp; -/* clang-format on */ +#ifdef GL_ARB_shader_texture_lod +#extension GL_ARB_shader_texture_lod : enable +#endif -uniform highp sampler2D source; //texunit:0 +#if !defined(GL_EXT_shader_texture_lod) && !defined(GL_ARB_shader_texture_lod) +#define texture2DLod(img, coord, lod) texture2D(img, coord, lod) +#define textureCubeLod(img, coord, lod) textureCube(img, coord, lod) +#endif -uniform float exposure; -uniform float white; +// Allows the use of bitshift operators for bicubic upscale +#ifdef GL_EXT_gpu_shader4 +#extension GL_EXT_gpu_shader4 : enable +#endif -#ifdef USE_AUTO_EXPOSURE +#ifdef USE_GLES_OVER_GL +#define lowp +#define mediump +#define highp +#else +#if defined(USE_HIGHP_PRECISION) +precision highp float; +precision highp int; +#else +precision mediump float; +precision mediump int; +#endif +#endif -uniform highp sampler2D source_auto_exposure; //texunit:1 -uniform highp float auto_exposure_grey; +#include "stdlib.glsl" -#endif +varying vec2 uv_interp; +/* clang-format on */ -#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) +uniform highp sampler2D source; //texunit:0 -uniform highp sampler2D source_glow; //texunit:2 +#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) +#define USING_GLOW // only use glow when at least one glow level is selected + +#ifdef USE_MULTI_TEXTURE_GLOW +uniform highp sampler2D source_glow1; //texunit:1 +uniform highp sampler2D source_glow2; //texunit:2 +uniform highp sampler2D source_glow3; //texunit:3 +uniform highp sampler2D source_glow4; //texunit:4 +uniform highp sampler2D source_glow5; //texunit:5 +uniform highp sampler2D source_glow6; //texunit:6 +uniform highp sampler2D source_glow7; //texunit:7 +#else +uniform highp sampler2D source_glow; //texunit:1 +#endif uniform highp float glow_intensity; - #endif #ifdef USE_BCS - uniform vec3 bcs; - #endif #ifdef USE_COLOR_CORRECTION - -uniform sampler2D color_correction; //texunit:3 - +uniform sampler2D color_correction; //texunit:2 #endif -layout(location = 0) out vec4 frag_color; - +#ifdef GL_EXT_gpu_shader4 #ifdef USE_GLOW_FILTER_BICUBIC - // w0, w1, w2, and w3 are the four cubic B-spline basis functions float w0(float a) { return (1.0 / 6.0) * (a * (a * (-a + 3.0) - 3.0) + 1.0); @@ -101,8 +138,10 @@ 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 = 1.0 / tex_size; - uv = uv * tex_size + 0.5; + vec2 pixel_size = vec2(1.0) / tex_size; + + uv = uv * tex_size + vec2(0.5); + vec2 iuv = floor(uv); vec2 fuv = fract(uv); @@ -113,73 +152,97 @@ vec4 texture2D_bicubic(sampler2D tex, vec2 uv, int p_lod) { float h0y = h0(fuv.y); float h1y = h1(fuv.y); - vec2 p0 = (vec2(iuv.x + h0x, iuv.y + h0y) - 0.5) * pixel_size; - vec2 p1 = (vec2(iuv.x + h1x, iuv.y + h0y) - 0.5) * pixel_size; - vec2 p2 = (vec2(iuv.x + h0x, iuv.y + h1y) - 0.5) * pixel_size; - vec2 p3 = (vec2(iuv.x + h1x, iuv.y + h1y) - 0.5) * pixel_size; + vec2 p0 = (vec2(iuv.x + h0x, iuv.y + h0y) - vec2(0.5)) * pixel_size; + vec2 p1 = (vec2(iuv.x + h1x, iuv.y + h0y) - vec2(0.5)) * pixel_size; + vec2 p2 = (vec2(iuv.x + h0x, iuv.y + h1y) - vec2(0.5)) * pixel_size; + vec2 p3 = (vec2(iuv.x + h1x, iuv.y + h1y) - vec2(0.5)) * pixel_size; - 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))); + return (g0(fuv.y) * (g0x * texture2DLod(tex, p0, lod) + g1x * texture2DLod(tex, p1, lod))) + + (g1(fuv.y) * (g0x * texture2DLod(tex, p2, lod) + g1x * texture2DLod(tex, p3, lod))); } #define GLOW_TEXTURE_SAMPLE(m_tex, m_uv, m_lod) texture2D_bicubic(m_tex, m_uv, m_lod) +#else //!USE_GLOW_FILTER_BICUBIC +#define GLOW_TEXTURE_SAMPLE(m_tex, m_uv, m_lod) texture2DLod(m_tex, m_uv, float(m_lod)) +#endif //USE_GLOW_FILTER_BICUBIC -#else +#else //!GL_EXT_gpu_shader4 +#define GLOW_TEXTURE_SAMPLE(m_tex, m_uv, m_lod) texture2DLod(m_tex, m_uv, float(m_lod)) +#endif //GL_EXT_gpu_shader4 -#define GLOW_TEXTURE_SAMPLE(m_tex, m_uv, m_lod) textureLod(m_tex, m_uv, float(m_lod)) +vec3 apply_glow(vec3 color, vec3 glow) { // apply glow using the selected blending mode +#ifdef USE_GLOW_REPLACE + color = glow; +#endif +#ifdef USE_GLOW_SCREEN + color = max((color + glow) - (color * glow), vec3(0.0)); #endif -vec3 tonemap_filmic(vec3 color, float white) { +#ifdef USE_GLOW_SOFTLIGHT + glow = glow * vec3(0.5) + vec3(0.5); - float A = 0.15; - float B = 0.50; - float C = 0.10; - float D = 0.20; - float E = 0.02; - float F = 0.30; - float W = 11.2; + color.r = (glow.r <= 0.5) ? (color.r - (1.0 - 2.0 * glow.r) * color.r * (1.0 - color.r)) : (((glow.r > 0.5) && (color.r <= 0.25)) ? (color.r + (2.0 * glow.r - 1.0) * (4.0 * color.r * (4.0 * color.r + 1.0) * (color.r - 1.0) + 7.0 * color.r)) : (color.r + (2.0 * glow.r - 1.0) * (sqrt(color.r) - color.r))); + color.g = (glow.g <= 0.5) ? (color.g - (1.0 - 2.0 * glow.g) * color.g * (1.0 - color.g)) : (((glow.g > 0.5) && (color.g <= 0.25)) ? (color.g + (2.0 * glow.g - 1.0) * (4.0 * color.g * (4.0 * color.g + 1.0) * (color.g - 1.0) + 7.0 * color.g)) : (color.g + (2.0 * glow.g - 1.0) * (sqrt(color.g) - color.g))); + color.b = (glow.b <= 0.5) ? (color.b - (1.0 - 2.0 * glow.b) * color.b * (1.0 - color.b)) : (((glow.b > 0.5) && (color.b <= 0.25)) ? (color.b + (2.0 * glow.b - 1.0) * (4.0 * color.b * (4.0 * color.b + 1.0) * (color.b - 1.0) + 7.0 * color.b)) : (color.b + (2.0 * glow.b - 1.0) * (sqrt(color.b) - color.b))); +#endif - vec3 coltn = ((color * (A * color + C * B) + D * E) / (color * (A * color + B) + D * F)) - E / F; - float whitetn = ((white * (A * white + C * B) + D * E) / (white * (A * white + B) + D * F)) - E / F; +#if !defined(USE_GLOW_SCREEN) && !defined(USE_GLOW_SOFTLIGHT) && !defined(USE_GLOW_REPLACE) // no other selected -> additive + color += glow; +#endif - return coltn / whitetn; + return color; } -vec3 tonemap_aces(vec3 color) { - float a = 2.51f; - float b = 0.03f; - float c = 2.43f; - float d = 0.59f; - float e = 0.14f; - return color = clamp((color * (a * color + b)) / (color * (c * color + d) + e), vec3(0.0), vec3(1.0)); +vec3 apply_bcs(vec3 color, vec3 bcs) { + color = mix(vec3(0.0), color, bcs.x); + color = mix(vec3(0.5), color, bcs.y); + color = mix(vec3(dot(vec3(1.0), color) * 0.33333), color, bcs.z); + + return color; } -vec3 tonemap_reindhart(vec3 color, float white) { +vec3 apply_color_correction(vec3 color, sampler2D correction_tex) { + color.r = texture2D(correction_tex, vec2(color.r, 0.0)).r; + color.g = texture2D(correction_tex, vec2(color.g, 0.0)).g; + color.b = texture2D(correction_tex, vec2(color.b, 0.0)).b; - return (color * (1.0 + (color / (white)))) / (1.0 + color); + return color; } void main() { + vec3 color = texture2DLod(source, uv_interp, 0.0).rgb; - vec4 color = textureLod(source, uv_interp, 0.0); + // Glow -#ifdef USE_AUTO_EXPOSURE - - color /= texelFetch(source_auto_exposure, ivec2(0, 0), 0).r / auto_exposure_grey; +#ifdef USING_GLOW + vec3 glow = vec3(0.0); +#ifdef USE_MULTI_TEXTURE_GLOW +#ifdef USE_GLOW_LEVEL1 + glow += GLOW_TEXTURE_SAMPLE(source_glow1, uv_interp, 0).rgb; +#ifdef USE_GLOW_LEVEL2 + glow += GLOW_TEXTURE_SAMPLE(source_glow2, uv_interp, 0).rgb; +#ifdef USE_GLOW_LEVEL3 + glow += GLOW_TEXTURE_SAMPLE(source_glow3, uv_interp, 0).rgb; +#ifdef USE_GLOW_LEVEL4 + glow += GLOW_TEXTURE_SAMPLE(source_glow4, uv_interp, 0).rgb; +#ifdef USE_GLOW_LEVEL5 + glow += GLOW_TEXTURE_SAMPLE(source_glow5, uv_interp, 0).rgb; +#ifdef USE_GLOW_LEVEL6 + glow += GLOW_TEXTURE_SAMPLE(source_glow6, uv_interp, 0).rgb; +#ifdef USE_GLOW_LEVEL7 + glow += GLOW_TEXTURE_SAMPLE(source_glow7, uv_interp, 0).rgb; +#endif +#endif +#endif +#endif +#endif #endif - - color *= exposure; - -#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) -#define USING_GLOW #endif -#if defined(USING_GLOW) - vec3 glow = vec3(0.0); +#else #ifdef USE_GLOW_LEVEL1 - glow += GLOW_TEXTURE_SAMPLE(source_glow, uv_interp, 1).rgb; #endif @@ -206,100 +269,21 @@ void main() { #ifdef USE_GLOW_LEVEL7 glow += GLOW_TEXTURE_SAMPLE(source_glow, uv_interp, 7).rgb; #endif +#endif //USE_MULTI_TEXTURE_GLOW glow *= glow_intensity; - -#endif - -#ifdef USE_REINDHART_TONEMAPPER - - color.rgb = tonemap_reindhart(color.rgb, white); - -#if defined(USING_GLOW) - glow = tonemap_reindhart(glow, white); -#endif - + color = apply_glow(color, glow); #endif -#ifdef USE_FILMIC_TONEMAPPER - - color.rgb = tonemap_filmic(color.rgb, white); - -#if defined(USING_GLOW) - glow = tonemap_filmic(glow, white); -#endif - -#endif - -#ifdef USE_ACES_TONEMAPPER - - color.rgb = tonemap_aces(color.rgb); - -#if defined(USING_GLOW) - glow = tonemap_aces(glow); -#endif - -#endif - - //regular Linear -> SRGB conversion - vec3 a = vec3(0.055); - color.rgb = mix((vec3(1.0) + a) * pow(color.rgb, vec3(1.0 / 2.4)) - a, 12.92 * color.rgb, lessThan(color.rgb, vec3(0.0031308))); - -#if defined(USING_GLOW) - glow = mix((vec3(1.0) + a) * pow(glow, vec3(1.0 / 2.4)) - a, 12.92 * glow, lessThan(glow, vec3(0.0031308))); -#endif - - //glow needs to be added in SRGB space (together with image space effects) - - color.rgb = clamp(color.rgb, 0.0, 1.0); - -#if defined(USING_GLOW) - glow = clamp(glow, 0.0, 1.0); -#endif - -#ifdef USE_GLOW_REPLACE - - color.rgb = glow; - -#endif - -#ifdef USE_GLOW_SCREEN - - color.rgb = max((color.rgb + glow) - (color.rgb * glow), vec3(0.0)); - -#endif - -#ifdef USE_GLOW_SOFTLIGHT - - { - - glow = (glow * 0.5) + 0.5; - color.r = (glow.r <= 0.5) ? (color.r - (1.0 - 2.0 * glow.r) * color.r * (1.0 - color.r)) : (((glow.r > 0.5) && (color.r <= 0.25)) ? (color.r + (2.0 * glow.r - 1.0) * (4.0 * color.r * (4.0 * color.r + 1.0) * (color.r - 1.0) + 7.0 * color.r)) : (color.r + (2.0 * glow.r - 1.0) * (sqrt(color.r) - color.r))); - color.g = (glow.g <= 0.5) ? (color.g - (1.0 - 2.0 * glow.g) * color.g * (1.0 - color.g)) : (((glow.g > 0.5) && (color.g <= 0.25)) ? (color.g + (2.0 * glow.g - 1.0) * (4.0 * color.g * (4.0 * color.g + 1.0) * (color.g - 1.0) + 7.0 * color.g)) : (color.g + (2.0 * glow.g - 1.0) * (sqrt(color.g) - color.g))); - color.b = (glow.b <= 0.5) ? (color.b - (1.0 - 2.0 * glow.b) * color.b * (1.0 - color.b)) : (((glow.b > 0.5) && (color.b <= 0.25)) ? (color.b + (2.0 * glow.b - 1.0) * (4.0 * color.b * (4.0 * color.b + 1.0) * (color.b - 1.0) + 7.0 * color.b)) : (color.b + (2.0 * glow.b - 1.0) * (sqrt(color.b) - color.b))); - } - -#endif - -#if defined(USING_GLOW) && !defined(USE_GLOW_SCREEN) && !defined(USE_GLOW_SOFTLIGHT) && !defined(USE_GLOW_REPLACE) - //additive - color.rgb += glow; -#endif + // Additional effects #ifdef USE_BCS - - color.rgb = mix(vec3(0.0), color.rgb, bcs.x); - color.rgb = mix(vec3(0.5), color.rgb, bcs.y); - color.rgb = mix(vec3(dot(vec3(1.0), color.rgb) * 0.33333), color.rgb, bcs.z); - + color = apply_bcs(color, bcs); #endif #ifdef USE_COLOR_CORRECTION - - color.r = texture(color_correction, vec2(color.r, 0.0)).r; - color.g = texture(color_correction, vec2(color.g, 0.0)).g; - color.b = texture(color_correction, vec2(color.b, 0.0)).b; + color = apply_color_correction(color, color_correction); #endif - frag_color = vec4(color.rgb, 1.0); + gl_FragColor = vec4(color, 1.0); } |