summaryrefslogtreecommitdiff
path: root/drivers/gles2/shaders/effect_blur.glsl
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/gles2/shaders/effect_blur.glsl')
-rw-r--r--drivers/gles2/shaders/effect_blur.glsl244
1 files changed, 132 insertions, 112 deletions
diff --git a/drivers/gles2/shaders/effect_blur.glsl b/drivers/gles2/shaders/effect_blur.glsl
index df79e89931..b28d78a6ca 100644
--- a/drivers/gles2/shaders/effect_blur.glsl
+++ b/drivers/gles2/shaders/effect_blur.glsl
@@ -1,11 +1,20 @@
/* 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;
#ifdef USE_BLUR_SECTION
@@ -16,7 +25,7 @@ uniform vec4 blur_section;
void main() {
uv_interp = uv_in;
- gl_Position = vertex_attrib;
+ gl_Position = vec4(vertex_attrib, 0.0, 1.0);
#ifdef USE_BLUR_SECTION
uv_interp = blur_section.xy + uv_interp * blur_section.zw;
@@ -27,29 +36,46 @@ void main() {
/* clang-format off */
[fragment]
-#if !defined(GLES_OVER_GL)
+// 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
+
+#ifdef GL_ARB_shader_texture_lod
+#extension GL_ARB_shader_texture_lod : enable
+#endif
+
+#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
+
+#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
-in vec2 uv_interp;
+varying vec2 uv_interp;
/* clang-format on */
uniform sampler2D source_color; //texunit:0
-#ifdef SSAO_MERGE
-uniform sampler2D source_ssao; //texunit:1
-#endif
-
uniform float lod;
uniform vec2 pixel_size;
-layout(location = 0) out vec4 frag_color;
-
-#ifdef SSAO_MERGE
-
-uniform vec4 ssao_color;
-
-#endif
-
#if defined(GLOW_GAUSSIAN_HORIZONTAL) || defined(GLOW_GAUSSIAN_VERTICAL)
uniform float glow_strength;
@@ -58,6 +84,7 @@ uniform float glow_strength;
#if defined(DOF_FAR_BLUR) || defined(DOF_NEAR_BLUR)
+#ifdef USE_GLES_OVER_GL
#ifdef DOF_QUALITY_LOW
const int dof_kernel_size = 5;
const int dof_kernel_from = 2;
@@ -76,6 +103,7 @@ const int dof_kernel_size = 21;
const int dof_kernel_from = 10;
const float dof_kernel[21] = float[](0.028174, 0.032676, 0.037311, 0.041944, 0.046421, 0.050582, 0.054261, 0.057307, 0.059587, 0.060998, 0.061476, 0.060998, 0.059587, 0.057307, 0.054261, 0.050582, 0.046421, 0.041944, 0.037311, 0.032676, 0.028174);
#endif
+#endif
uniform sampler2D dof_source_depth; //texunit:1
uniform float dof_begin;
@@ -83,24 +111,11 @@ uniform float dof_end;
uniform vec2 dof_dir;
uniform float dof_radius;
-#ifdef DOF_NEAR_BLUR_MERGE
-
-uniform sampler2D source_dof_original; //texunit:2
-#endif
-
#endif
#ifdef GLOW_FIRST_PASS
-uniform float exposure;
-uniform float white;
-
-#ifdef GLOW_USE_AUTO_EXPOSURE
-
-uniform highp sampler2D source_auto_exposure; //texunit:1
-uniform highp float auto_exposure_grey;
-
-#endif
+uniform highp float luminance_cap;
uniform float glow_bloom;
uniform float glow_hdr_threshold;
@@ -113,60 +128,95 @@ uniform float camera_z_near;
void main() {
-#ifdef GAUSSIAN_HORIZONTAL
+#ifdef GLOW_GAUSSIAN_HORIZONTAL
vec2 pix_size = pixel_size;
pix_size *= 0.5; //reading from larger buffer, so use more samples
- // sigma 2
- vec4 color = textureLod(source_color, uv_interp + vec2(0.0, 0.0) * pix_size, lod) * 0.214607;
- color += textureLod(source_color, uv_interp + vec2(1.0, 0.0) * pix_size, lod) * 0.189879;
- color += textureLod(source_color, uv_interp + vec2(2.0, 0.0) * pix_size, lod) * 0.131514;
- color += textureLod(source_color, uv_interp + vec2(3.0, 0.0) * pix_size, lod) * 0.071303;
- color += textureLod(source_color, uv_interp + vec2(-1.0, 0.0) * pix_size, lod) * 0.189879;
- color += textureLod(source_color, uv_interp + vec2(-2.0, 0.0) * pix_size, lod) * 0.131514;
- color += textureLod(source_color, uv_interp + vec2(-3.0, 0.0) * pix_size, lod) * 0.071303;
- frag_color = color;
+ vec4 color = texture2DLod(source_color, uv_interp + vec2(0.0, 0.0) * pix_size, lod) * 0.174938;
+ color += texture2DLod(source_color, uv_interp + vec2(1.0, 0.0) * pix_size, lod) * 0.165569;
+ color += texture2DLod(source_color, uv_interp + vec2(2.0, 0.0) * pix_size, lod) * 0.140367;
+ color += texture2DLod(source_color, uv_interp + vec2(3.0, 0.0) * pix_size, lod) * 0.106595;
+ color += texture2DLod(source_color, uv_interp + vec2(-1.0, 0.0) * pix_size, lod) * 0.165569;
+ color += texture2DLod(source_color, uv_interp + vec2(-2.0, 0.0) * pix_size, lod) * 0.140367;
+ color += texture2DLod(source_color, uv_interp + vec2(-3.0, 0.0) * pix_size, lod) * 0.106595;
+ color *= glow_strength;
+ gl_FragColor = color;
#endif
-#ifdef GAUSSIAN_VERTICAL
- vec4 color = textureLod(source_color, uv_interp + vec2(0.0, 0.0) * pixel_size, lod) * 0.38774;
- color += textureLod(source_color, uv_interp + vec2(0.0, 1.0) * pixel_size, lod) * 0.24477;
- color += textureLod(source_color, uv_interp + vec2(0.0, 2.0) * pixel_size, lod) * 0.06136;
- color += textureLod(source_color, uv_interp + vec2(0.0, -1.0) * pixel_size, lod) * 0.24477;
- color += textureLod(source_color, uv_interp + vec2(0.0, -2.0) * pixel_size, lod) * 0.06136;
- frag_color = color;
+#ifdef GLOW_GAUSSIAN_VERTICAL
+ vec4 color = texture2DLod(source_color, uv_interp + vec2(0.0, 0.0) * pixel_size, lod) * 0.288713;
+ color += texture2DLod(source_color, uv_interp + vec2(0.0, 1.0) * pixel_size, lod) * 0.233062;
+ color += texture2DLod(source_color, uv_interp + vec2(0.0, 2.0) * pixel_size, lod) * 0.122581;
+ color += texture2DLod(source_color, uv_interp + vec2(0.0, -1.0) * pixel_size, lod) * 0.233062;
+ color += texture2DLod(source_color, uv_interp + vec2(0.0, -2.0) * pixel_size, lod) * 0.122581;
+ color *= glow_strength;
+ gl_FragColor = color;
#endif
- //glow uses larger sigma for a more rounded blur effect
+#ifndef USE_GLES_OVER_GL
+#if defined(DOF_FAR_BLUR) || defined(DOF_NEAR_BLUR)
-#ifdef GLOW_GAUSSIAN_HORIZONTAL
- vec2 pix_size = pixel_size;
- pix_size *= 0.5; //reading from larger buffer, so use more samples
- vec4 color = textureLod(source_color, uv_interp + vec2(0.0, 0.0) * pix_size, lod) * 0.174938;
- color += textureLod(source_color, uv_interp + vec2(1.0, 0.0) * pix_size, lod) * 0.165569;
- color += textureLod(source_color, uv_interp + vec2(2.0, 0.0) * pix_size, lod) * 0.140367;
- color += textureLod(source_color, uv_interp + vec2(3.0, 0.0) * pix_size, lod) * 0.106595;
- color += textureLod(source_color, uv_interp + vec2(-1.0, 0.0) * pix_size, lod) * 0.165569;
- color += textureLod(source_color, uv_interp + vec2(-2.0, 0.0) * pix_size, lod) * 0.140367;
- color += textureLod(source_color, uv_interp + vec2(-3.0, 0.0) * pix_size, lod) * 0.106595;
- color *= glow_strength;
- frag_color = color;
+#ifdef DOF_QUALITY_LOW
+ const int dof_kernel_size = 5;
+ const int dof_kernel_from = 2;
+ float dof_kernel[5];
+ dof_kernel[0] = 0.153388;
+ dof_kernel[1] = 0.221461;
+ dof_kernel[2] = 0.250301;
+ dof_kernel[3] = 0.221461;
+ dof_kernel[4] = 0.153388;
#endif
-#ifdef GLOW_GAUSSIAN_VERTICAL
- vec4 color = textureLod(source_color, uv_interp + vec2(0.0, 0.0) * pixel_size, lod) * 0.288713;
- color += textureLod(source_color, uv_interp + vec2(0.0, 1.0) * pixel_size, lod) * 0.233062;
- color += textureLod(source_color, uv_interp + vec2(0.0, 2.0) * pixel_size, lod) * 0.122581;
- color += textureLod(source_color, uv_interp + vec2(0.0, -1.0) * pixel_size, lod) * 0.233062;
- color += textureLod(source_color, uv_interp + vec2(0.0, -2.0) * pixel_size, lod) * 0.122581;
- color *= glow_strength;
- frag_color = color;
+#ifdef DOF_QUALITY_MEDIUM
+ const int dof_kernel_size = 11;
+ const int dof_kernel_from = 5;
+ float dof_kernel[11];
+ dof_kernel[0] = 0.055037;
+ dof_kernel[1] = 0.072806;
+ dof_kernel[2] = 0.090506;
+ dof_kernel[3] = 0.105726;
+ dof_kernel[4] = 0.116061;
+ dof_kernel[5] = 0.119726;
+ dof_kernel[6] = 0.116061;
+ dof_kernel[7] = 0.105726;
+ dof_kernel[8] = 0.090506;
+ dof_kernel[9] = 0.072806;
+ dof_kernel[10] = 0.055037;
#endif
+#ifdef DOF_QUALITY_HIGH
+ const int dof_kernel_size = 21;
+ const int dof_kernel_from = 10;
+ float dof_kernel[21];
+ dof_kernel[0] = 0.028174;
+ dof_kernel[1] = 0.032676;
+ dof_kernel[2] = 0.037311;
+ dof_kernel[3] = 0.041944;
+ dof_kernel[4] = 0.046421;
+ dof_kernel[5] = 0.050582;
+ dof_kernel[6] = 0.054261;
+ dof_kernel[7] = 0.057307;
+ dof_kernel[8] = 0.059587;
+ dof_kernel[9] = 0.060998;
+ dof_kernel[10] = 0.061476;
+ dof_kernel[11] = 0.060998;
+ dof_kernel[12] = 0.059587;
+ dof_kernel[13] = 0.057307;
+ dof_kernel[14] = 0.054261;
+ dof_kernel[15] = 0.050582;
+ dof_kernel[16] = 0.046421;
+ dof_kernel[17] = 0.041944;
+ dof_kernel[18] = 0.037311;
+ dof_kernel[19] = 0.032676;
+ dof_kernel[20] = 0.028174;
+#endif
+#endif
+#endif //!USE_GLES_OVER_GL
+
#ifdef DOF_FAR_BLUR
vec4 color_accum = vec4(0.0);
- float depth = textureLod(dof_source_depth, uv_interp, 0.0).r;
+ float depth = texture2DLod(dof_source_depth, uv_interp, 0.0).r;
depth = depth * 2.0 - 1.0;
#ifdef USE_ORTHOGONAL_PROJECTION
depth = ((depth + (camera_z_far + camera_z_near) / (camera_z_far - camera_z_near)) * (camera_z_far - camera_z_near)) / 2.0;
@@ -184,17 +234,17 @@ void main() {
float tap_k = dof_kernel[i];
- float tap_depth = texture(dof_source_depth, tap_uv, 0.0).r;
+ float tap_depth = texture2D(dof_source_depth, tap_uv, 0.0).r;
tap_depth = tap_depth * 2.0 - 1.0;
#ifdef USE_ORTHOGONAL_PROJECTION
tap_depth = ((tap_depth + (camera_z_far + camera_z_near) / (camera_z_far - camera_z_near)) * (camera_z_far - camera_z_near)) / 2.0;
#else
tap_depth = 2.0 * camera_z_near * camera_z_far / (camera_z_far + camera_z_near - tap_depth * (camera_z_far - camera_z_near));
#endif
- float tap_amount = mix(smoothstep(dof_begin, dof_end, tap_depth), 1.0, int_ofs == 0);
+ float tap_amount = int_ofs == 0 ? 1.0 : smoothstep(dof_begin, dof_end, tap_depth);
tap_amount *= tap_amount * tap_amount; //prevent undesired glow effect
- vec4 tap_color = textureLod(source_color, tap_uv, 0.0) * tap_k;
+ vec4 tap_color = texture2DLod(source_color, tap_uv, 0.0) * tap_k;
k_accum += tap_k * tap_amount;
color_accum += tap_color * tap_amount;
@@ -204,7 +254,7 @@ void main() {
color_accum /= k_accum;
}
- frag_color = color_accum; ///k_accum;
+ gl_FragColor = color_accum; ///k_accum;
#endif
@@ -212,19 +262,19 @@ void main() {
vec4 color_accum = vec4(0.0);
- float max_accum = 0;
+ float max_accum = 0.0;
for (int i = 0; i < dof_kernel_size; i++) {
int int_ofs = i - dof_kernel_from;
vec2 tap_uv = uv_interp + dof_dir * float(int_ofs) * dof_radius;
- float ofs_influence = max(0.0, 1.0 - float(abs(int_ofs)) / float(dof_kernel_from));
+ float ofs_influence = max(0.0, 1.0 - abs(float(int_ofs)) / float(dof_kernel_from));
float tap_k = dof_kernel[i];
- vec4 tap_color = textureLod(source_color, tap_uv, 0.0);
+ vec4 tap_color = texture2DLod(source_color, tap_uv, 0.0);
- float tap_depth = texture(dof_source_depth, tap_uv, 0.0).r;
+ float tap_depth = texture2D(dof_source_depth, tap_uv, 0.0).r;
tap_depth = tap_depth * 2.0 - 1.0;
#ifdef USE_ORTHOGONAL_PROJECTION
tap_depth = ((tap_depth + (camera_z_far + camera_z_near) / (camera_z_far - camera_z_near)) * (camera_z_far - camera_z_near)) / 2.0;
@@ -247,46 +297,16 @@ void main() {
color_accum.a = max(color_accum.a, sqrt(max_accum));
-#ifdef DOF_NEAR_BLUR_MERGE
-
- vec4 original = textureLod(source_dof_original, uv_interp, 0.0);
- color_accum = mix(original, color_accum, color_accum.a);
-
-#endif
-
-#ifndef DOF_NEAR_FIRST_TAP
- //color_accum=vec4(vec3(color_accum.a),1.0);
-#endif
- frag_color = color_accum;
+ gl_FragColor = color_accum;
#endif
#ifdef GLOW_FIRST_PASS
-#ifdef GLOW_USE_AUTO_EXPOSURE
-
- frag_color /= texelFetch(source_auto_exposure, ivec2(0, 0), 0).r / auto_exposure_grey;
-#endif
- frag_color *= exposure;
-
- float luminance = max(frag_color.r, max(frag_color.g, frag_color.b));
+ float luminance = max(gl_FragColor.r, max(gl_FragColor.g, gl_FragColor.b));
float feedback = max(smoothstep(glow_hdr_threshold, glow_hdr_threshold + glow_hdr_scale, luminance), glow_bloom);
- frag_color *= feedback;
-
-#endif
-
-#ifdef SIMPLE_COPY
- vec4 color = textureLod(source_color, uv_interp, 0.0);
- frag_color = color;
-#endif
-
-#ifdef SSAO_MERGE
-
- vec4 color = textureLod(source_color, uv_interp, 0.0);
- float ssao = textureLod(source_ssao, uv_interp, 0.0).r;
-
- frag_color = vec4(mix(color.rgb, color.rgb * mix(ssao_color.rgb, vec3(1.0), ssao), color.a), 1.0);
+ gl_FragColor = min(gl_FragColor * feedback, vec4(luminance_cap));
#endif
}