diff options
author | Juan Linietsky <reduzio@gmail.com> | 2016-12-10 01:13:20 -0300 |
---|---|---|
committer | Juan Linietsky <reduzio@gmail.com> | 2016-12-10 01:13:20 -0300 |
commit | 22a90e8f2acce60f92958788a52b3f0bdb1a0cdf (patch) | |
tree | f6e0a7282992c9adf3e1c929d2462ae921bcd461 /drivers/gles3/shaders | |
parent | 18ebd22000478dffc91255e89b9845f74b05b606 (diff) |
DOF blur, near and far fields..
Diffstat (limited to 'drivers/gles3/shaders')
-rw-r--r-- | drivers/gles3/shaders/effect_blur.glsl | 177 | ||||
-rw-r--r-- | drivers/gles3/shaders/tonemap.glsl | 99 |
2 files changed, 249 insertions, 27 deletions
diff --git a/drivers/gles3/shaders/effect_blur.glsl b/drivers/gles3/shaders/effect_blur.glsl index 589af64d44..89afa12f60 100644 --- a/drivers/gles3/shaders/effect_blur.glsl +++ b/drivers/gles3/shaders/effect_blur.glsl @@ -41,6 +41,40 @@ uniform float glow_strength; #endif +#if defined(DOF_FAR_BLUR) || defined (DOF_NEAR_BLUR) + +#ifdef DOF_QUALITY_LOW +const int dof_kernel_size=5; +const int dof_kernel_from=2; +const float dof_kernel[5] = float[] (0.153388,0.221461,0.250301,0.221461,0.153388); +#endif + +#ifdef DOF_QUALITY_MEDIUM +const int dof_kernel_size=11; +const int dof_kernel_from=5; +const float dof_kernel[11] = float[] (0.055037,0.072806,0.090506,0.105726,0.116061,0.119726,0.116061,0.105726,0.090506,0.072806,0.055037); + +#endif + +#ifdef DOF_QUALITY_HIGH +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 + +uniform sampler2D dof_source_depth; //texunit:1 +uniform float dof_begin; +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 @@ -60,16 +94,23 @@ uniform float glow_hdr_scale; #endif +uniform float camera_z_far; +uniform float camera_z_near; + void main() { #ifdef GAUSSIAN_HORIZONTAL - vec4 color =textureLod( source_color, uv_interp+vec2( 0.0, 0.0)*pixel_size,lod )*0.38774; - color+=textureLod( source_color, uv_interp+vec2( 1.0, 0.0)*pixel_size,lod )*0.24477; - color+=textureLod( source_color, uv_interp+vec2( 2.0, 0.0)*pixel_size,lod )*0.06136; - color+=textureLod( source_color, uv_interp+vec2(-1.0, 0.0)*pixel_size,lod )*0.24477; - color+=textureLod( source_color, uv_interp+vec2(-2.0, 0.0)*pixel_size,lod )*0.06136; + 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.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.157305; + 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.157305; + color+=textureLod( source_color, uv_interp+vec2(-3.0, 0.0)*pix_size,lod )*0.071303; frag_color = color; #endif @@ -82,32 +123,126 @@ void main() { frag_color = color; #endif - +//glow uses larger sigma for a more rounded blur effect #ifdef GLOW_GAUSSIAN_HORIZONTAL - vec4 color =textureLod( source_color, uv_interp+vec2( 0.0, 0.0)*pixel_size,lod )*0.174938; - color+=textureLod( source_color, uv_interp+vec2( 1.0, 0.0)*pixel_size,lod )*0.165569; - color+=textureLod( source_color, uv_interp+vec2( 2.0, 0.0)*pixel_size,lod )*0.140367; - color+=textureLod( source_color, uv_interp+vec2( 3.0, 0.0)*pixel_size,lod )*0.106595; - color+=textureLod( source_color, uv_interp+vec2(-1.0, 0.0)*pixel_size,lod )*0.165569; - color+=textureLod( source_color, uv_interp+vec2(-2.0, 0.0)*pixel_size,lod )*0.140367; - color+=textureLod( source_color, uv_interp+vec2(-3.0, 0.0)*pixel_size,lod )*0.165569; + 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; #endif #ifdef GLOW_GAUSSIAN_VERTICAL - vec4 color =textureLod( source_color, uv_interp+vec2(0.0, 0.0)*pixel_size,lod )*0.174938; - color+=textureLod( source_color, uv_interp+vec2(0.0, 1.0)*pixel_size,lod )*0.165569; - color+=textureLod( source_color, uv_interp+vec2(0.0, 2.0)*pixel_size,lod )*0.140367; - color+=textureLod( source_color, uv_interp+vec2(0.0, 3.0)*pixel_size,lod )*0.106595; - color+=textureLod( source_color, uv_interp+vec2(0.0,-1.0)*pixel_size,lod )*0.165569; - color+=textureLod( source_color, uv_interp+vec2(0.0,-2.0)*pixel_size,lod )*0.140367; - color+=textureLod( source_color, uv_interp+vec2(0.0,-3.0)*pixel_size,lod )*0.165569; + 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; #endif +#ifdef DOF_FAR_BLUR + + vec4 color_accum = vec4(0.0); + + float depth = textureLod( dof_source_depth, uv_interp, 0.0).r; + depth = depth * 2.0 - 1.0; + depth = 2.0 * camera_z_near * camera_z_far / (camera_z_far + camera_z_near - depth * (camera_z_far - camera_z_near)); + + float amount = smoothstep(dof_begin,dof_end,depth); + float k_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) * amount * dof_radius; + + float tap_k = dof_kernel[i]; + + float tap_depth = texture( dof_source_depth, tap_uv, 0.0).r; + tap_depth = tap_depth * 2.0 - 1.0; + tap_depth = 2.0 * camera_z_near * camera_z_far / (camera_z_far + camera_z_near - tap_depth * (camera_z_far - camera_z_near)); + + float tap_amount = mix(smoothstep(dof_begin,dof_end,tap_depth),1.0,int_ofs==0); + tap_amount*=tap_amount*tap_amount; //prevent undesired glow effect + + vec4 tap_color = textureLod( source_color, tap_uv, 0.0) * tap_k; + + k_accum+=tap_k*tap_amount; + color_accum+=tap_color*tap_amount; + + + } + + if (k_accum>0.0) { + color_accum/=k_accum; + } + + frag_color = color_accum;///k_accum; + +#endif + +#ifdef DOF_NEAR_BLUR + + vec4 color_accum = vec4(0.0); + + float max_accum=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 tap_k = dof_kernel[i]; + + vec4 tap_color = textureLod( source_color, tap_uv, 0.0); + + float tap_depth = texture( dof_source_depth, tap_uv, 0.0).r; + tap_depth = tap_depth * 2.0 - 1.0; + tap_depth = 2.0 * camera_z_near * camera_z_far / (camera_z_far + camera_z_near - tap_depth * (camera_z_far - camera_z_near)); + float tap_amount = 1.0-smoothstep(dof_end,dof_begin,tap_depth); + tap_amount*=tap_amount*tap_amount; //prevent undesired glow effect + +#ifdef DOF_NEAR_FIRST_TAP + + tap_color.a= 1.0-smoothstep(dof_end,dof_begin,tap_depth); + +#endif + + max_accum=max(max_accum,tap_amount*ofs_influence); + + color_accum+=tap_color*tap_k; + + } + + 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; + +#endif + + + #ifdef GLOW_FIRST_PASS #ifdef GLOW_USE_AUTO_EXPOSURE @@ -137,5 +272,7 @@ void main() { frag_color = vec4( mix(color.rgb,color.rgb*mix(ssao_color.rgb,vec3(1.0),ssao),color.a), 1.0 ); #endif + + } diff --git a/drivers/gles3/shaders/tonemap.glsl b/drivers/gles3/shaders/tonemap.glsl index 8ee51e9d0c..8f7e0c7be3 100644 --- a/drivers/gles3/shaders/tonemap.glsl +++ b/drivers/gles3/shaders/tonemap.glsl @@ -41,6 +41,90 @@ uniform highp float glow_intensity; 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.0/6.0)*(a*(a*(-a + 3.0) - 3.0) + 1.0); +} + +float w1(float a) +{ + return (1.0/6.0)*(a*a*(3.0*a - 6.0) + 4.0); +} + +float w2(float a) +{ + return (1.0/6.0)*(a*(a*(-3.0*a + 3.0) + 3.0) + 1.0); +} + +float w3(float a) +{ + return (1.0/6.0)*(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.0 + w1(a) / (w0(a) + w1(a)); +} + +float h1(float a) +{ + return 1.0 + 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 =1.0/tex_size; + uv = uv*tex_size + 0.5; + 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) - 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; + + 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 + +#define GLOW_TEXTURE_SAMPLE(m_tex,m_uv,m_lod) textureLod(m_tex,m_uv,float(m_lod)) + +#endif + void main() { @@ -60,31 +144,32 @@ void main() { vec3 glow = vec3(0.0); #ifdef USE_GLOW_LEVEL1 - glow+=textureLod(source_glow,uv_interp,1.0).rgb; + + glow+=GLOW_TEXTURE_SAMPLE(source_glow,uv_interp,1).rgb; #endif #ifdef USE_GLOW_LEVEL2 - glow+=textureLod(source_glow,uv_interp,2.0).rgb; + glow+=GLOW_TEXTURE_SAMPLE(source_glow,uv_interp,2).rgb; #endif #ifdef USE_GLOW_LEVEL3 - glow+=textureLod(source_glow,uv_interp,3.0).rgb; + glow+=GLOW_TEXTURE_SAMPLE(source_glow,uv_interp,3).rgb; #endif #ifdef USE_GLOW_LEVEL4 - glow+=textureLod(source_glow,uv_interp,4.0).rgb; + glow+=GLOW_TEXTURE_SAMPLE(source_glow,uv_interp,4).rgb; #endif #ifdef USE_GLOW_LEVEL5 - glow+=textureLod(source_glow,uv_interp,5.0).rgb; + glow+=GLOW_TEXTURE_SAMPLE(source_glow,uv_interp,5).rgb; #endif #ifdef USE_GLOW_LEVEL6 - glow+=textureLod(source_glow,uv_interp,6.0).rgb; + glow+=GLOW_TEXTURE_SAMPLE(source_glow,uv_interp,6).rgb; #endif #ifdef USE_GLOW_LEVEL7 - glow+=textureLod(source_glow,uv_interp,7.0).rgb; + glow+=GLOW_TEXTURE_SAMPLE(source_glow,uv_interp,7).rgb; #endif |