diff options
author | Juan Linietsky <reduzio@gmail.com> | 2014-06-16 10:22:26 -0300 |
---|---|---|
committer | Juan Linietsky <reduzio@gmail.com> | 2014-06-16 10:22:26 -0300 |
commit | 703004f830f39adcde9b9565f1aa49d1b10e8d27 (patch) | |
tree | b8fd669af9dee07177ff658f0ebca83aff836598 /drivers/gles2/shaders | |
parent | 64e83bfd1404ea593f0c79b478d196a3fcde42a8 (diff) |
More 3D Work
-=-=-=-=-=-
-ESM Shadow Mapping for softer and less glitchy shadows
-HDR Pipeline (convert to Linear on texture import, convert to SRGB at the end)
-Fix to xml parse bug
Diffstat (limited to 'drivers/gles2/shaders')
-rw-r--r-- | drivers/gles2/shaders/copy.glsl | 157 | ||||
-rw-r--r-- | drivers/gles2/shaders/material.glsl | 61 |
2 files changed, 204 insertions, 14 deletions
diff --git a/drivers/gles2/shaders/copy.glsl b/drivers/gles2/shaders/copy.glsl index 2f1b349618..6d78c69e50 100644 --- a/drivers/gles2/shaders/copy.glsl +++ b/drivers/gles2/shaders/copy.glsl @@ -54,8 +54,12 @@ varying vec3 cube_interp; uniform samplerCube source_cube; #else varying vec2 uv_interp; +#ifdef HIGHP_SOURCE +uniform highp sampler2D source; +#else uniform sampler2D source; #endif +#endif varying vec2 uv2_interp; #ifdef USE_GLOW @@ -83,12 +87,6 @@ uniform vec3 bcs; #endif -#ifdef USE_GAMMA - -uniform float gamma; - -#endif - #ifdef USE_GLOW_COPY uniform float bloom; @@ -96,7 +94,7 @@ uniform float bloom_treshold; #endif -#if defined(BLUR_V_PASS) || defined(BLUR_H_PASS) || defined(USE_HDR_REDUCE) +#if defined(SHADOW_BLUR_V_PASS) || defined(SHADOW_BLUR_H_PASS) || defined(BLUR_V_PASS) || defined(BLUR_H_PASS) || defined(USE_HDR_REDUCE) uniform vec2 pixel_size; uniform float pixel_scale; @@ -133,6 +131,17 @@ uniform float custom_alpha; void main() { //vec4 color = color_interp; +#ifdef USE_HIGHP_SOURCE + +#ifdef USE_CUBEMAP + highp vec4 color = textureCube( source_cube, normalize(cube_interp) ); + +#else + highp vec4 color = texture2D( source, uv_interp ); +#endif + +#else + #ifdef USE_CUBEMAP vec4 color = textureCube( source_cube, normalize(cube_interp) ); @@ -141,6 +150,8 @@ void main() { #endif +#endif + #ifdef USE_FXAA #define FXAA_REDUCE_MIN (1.0/ 128.0) @@ -226,17 +237,103 @@ void main() { #endif +#ifdef SHADOW_BLUR_V_PASS + +#ifdef USE_RGBA_DEPTH + +#define VEC42DEPTH(m_vec4) dot(m_vec4,vec4(1.0 / (256.0 * 256.0 * 256.0),1.0 / (256.0 * 256.0),1.0 / 256.0,1)) + + highp float depth = VEC42DEPTH(color)*0.383; + depth+=VEC42DEPTH(texture2D(source,uv_interp+vec2(0.0,pixel_size.y*-3.0)*pixel_scale))*0.006; + depth+=VEC42DEPTH(texture2D(source,uv_interp+vec2(0.0,pixel_size.y*-2.0)*pixel_scale))*0.061; + depth+=VEC42DEPTH(texture2D(source,uv_interp+vec2(0.0,pixel_size.y*-1.0)*pixel_scale))*0.242; + depth+=VEC42DEPTH(texture2D(source,uv_interp+vec2(0.0,pixel_size.y*1.0)*pixel_scale))*0.242; + depth+=VEC42DEPTH(texture2D(source,uv_interp+vec2(0.0,pixel_size.y*2.0)*pixel_scale))*0.061; + depth+=VEC42DEPTH(texture2D(source,uv_interp+vec2(0.0,pixel_size.y*3.0)*pixel_scale))*0.006; + highp vec4 comp = fract(depth * vec4(256.0 * 256.0 * 256.0, 256.0 * 256.0, 256.0, 1.0)); + comp -= comp.xxyz * vec4(0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0); + color=comp; + +#else + + highp float depth = color.r*0.383; + depth+=texture2D(source,uv_interp+vec2(0.0,pixel_size.y*-3.0)*pixel_scale).r*0.006; + depth+=texture2D(source,uv_interp+vec2(0.0,pixel_size.y*-2.0)*pixel_scale).r*0.061; + depth+=texture2D(source,uv_interp+vec2(0.0,pixel_size.y*-1.0)*pixel_scale).r*0.242; + depth+=texture2D(source,uv_interp+vec2(0.0,pixel_size.y*1.0)*pixel_scale).r*0.242; + depth+=texture2D(source,uv_interp+vec2(0.0,pixel_size.y*2.0)*pixel_scale).r*0.061; + depth+=texture2D(source,uv_interp+vec2(0.0,pixel_size.y*3.0)*pixel_scale).r*0.006; + +#ifdef USE_GLES_OVER_GL + gl_FragDepth = depth; + +#else + gl_FragDepthEXT = depth; +#endif + + return; +#endif + +#endif + +#ifdef SHADOW_BLUR_H_PASS + + +#ifdef USE_RGBA_DEPTH + +#define VEC42DEPTH(m_vec4) dot(m_vec4,vec4(1.0 / (256.0 * 256.0 * 256.0),1.0 / (256.0 * 256.0),1.0 / 256.0,1)) + + highp float depth = VEC42DEPTH(color)*0.383; + depth+=VEC42DEPTH(texture2D(source,uv_interp+vec2(pixel_size.x*-3.0,0.0)*pixel_scale))*0.006; + depth+=VEC42DEPTH(texture2D(source,uv_interp+vec2(pixel_size.x*-2.0,0.0)*pixel_scale))*0.061; + depth+=VEC42DEPTH(texture2D(source,uv_interp+vec2(pixel_size.x*-1.0,0.0)*pixel_scale))*0.242; + depth+=VEC42DEPTH(texture2D(source,uv_interp+vec2(pixel_size.x*1.0,0.0)*pixel_scale))*0.242; + depth+=VEC42DEPTH(texture2D(source,uv_interp+vec2(pixel_size.x*2.0,0.0)*pixel_scale))*0.061; + depth+=VEC42DEPTH(texture2D(source,uv_interp+vec2(pixel_size.x*3.0,0.0)*pixel_scale))*0.006; + highp vec4 comp = fract(depth * vec4(256.0 * 256.0 * 256.0, 256.0 * 256.0, 256.0, 1.0)); + comp -= comp.xxyz * vec4(0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0); + color=comp; +#else + + + highp float depth = color.r*0.383; + depth+=texture2D(source,uv_interp+vec2(pixel_size.x*-3.0,0.0)*pixel_scale).r*0.006; + depth+=texture2D(source,uv_interp+vec2(pixel_size.x*-2.0,0.0)*pixel_scale).r*0.061; + depth+=texture2D(source,uv_interp+vec2(pixel_size.x*-1.0,0.0)*pixel_scale).r*0.242; + depth+=texture2D(source,uv_interp+vec2(pixel_size.x*1.0,0.0)*pixel_scale).r*0.242; + depth+=texture2D(source,uv_interp+vec2(pixel_size.x*2.0,0.0)*pixel_scale).r*0.061; + depth+=texture2D(source,uv_interp+vec2(pixel_size.x*3.0,0.0)*pixel_scale).r*0.006; + +#ifdef USE_GLES_OVER_GL + gl_FragDepth = depth; +#else + gl_FragDepthEXT = depth; +#endif + + return; + +#endif + +#endif + #ifdef USE_HDR + +#ifdef USE_8BIT_HDR highp vec4 _mult = vec4(1.0 / (256.0 * 256.0 * 256.0),1.0 / (256.0 * 256.0),1.0 / 256.0,1); highp float hdr_lum = dot(texture2D( hdr_source, vec2(0.0) ), _mult ); color.rgb*=LUM_RANGE; hdr_lum*=LUM_RANGE; //restore to full range +#else + highp float hdr_lum = texture2D( hdr_source, vec2(0.0) ).r; +#endif + highp float tone_scale = tonemap_exposure / hdr_lum; //only linear supported color.rgb*=tone_scale; #endif + #ifdef USE_GLOW_COPY highp vec3 glowcol = color.rgb*color.a+step(bloom_treshold,dot(vec3(0.3333,0.3333,0.3333),color.rgb))*bloom*color.rgb; @@ -281,10 +378,15 @@ void main() { #endif -#ifdef USE_GAMMA - - color.rgb = pow(color.rgb,gamma); +#ifdef USE_SRGB + { //i have my doubts about how fast this is + color.rgb = min(color.rgb,vec3(1.0)); //clamp just in case + vec3 S1 = sqrt(color.rgb); + vec3 S2 = sqrt(S1); + vec3 S3 = sqrt(S2); + color.rgb = 0.662002687 * S1 + 0.684122060 * S2 - 0.323583601 * S3 - 0.225411470 * color.rgb; + } #endif @@ -292,13 +394,20 @@ void main() { //highp float lum = dot(color.rgb,highp vec3(1.0/3.0,1.0/3.0,1.0/3.0)); highp float lum = max(color.r,max(color.g,color.b)); +#ifdef USE_8BIT_HDR highp vec4 comp = fract(lum * vec4(256.0 * 256.0 * 256.0, 256.0 * 256.0, 256.0, 1.0)); comp -= comp.xxyz * vec4(0.0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0); color=comp; +#else + color.rgb=vec3(lum); +#endif + + #endif #ifdef USE_HDR_REDUCE +#ifdef USE_8BIT_HDR highp vec4 _multcv = vec4(1.0 / (256.0 * 256.0 * 256.0),1.0 / (256.0 * 256.0),1.0 / 256.0, 1.0); highp float lum_accum = dot(color,_multcv ); lum_accum += dot(texture2D( source, uv_interp+vec2(-pixel_size.x,-pixel_size.y) ),_multcv ); @@ -310,16 +419,42 @@ void main() { lum_accum += dot(texture2D( source, uv_interp+vec2(0.0,pixel_size.y) ),_multcv ); lum_accum += dot(texture2D( source, uv_interp+vec2(pixel_size.x,pixel_size.y) ),_multcv ); lum_accum/=9.0; +#else + + highp float lum_accum = color.r; + lum_accum += texture2D( source, uv_interp+vec2(-pixel_size.x,-pixel_size.y) ).r; + lum_accum += texture2D( source, uv_interp+vec2(0.0,-pixel_size.y) ).r; + lum_accum += texture2D( source, uv_interp+vec2(pixel_size.x,-pixel_size.y) ).r; + lum_accum += texture2D( source, uv_interp+vec2(-pixel_size.x,0.0) ).r; + lum_accum += texture2D( source, uv_interp+vec2(pixel_size.x,0.0) ).r; + lum_accum += texture2D( source, uv_interp+vec2(-pixel_size.x,pixel_size.y) ).r; + lum_accum += texture2D( source, uv_interp+vec2(0.0,pixel_size.y) ).r; + lum_accum += texture2D( source, uv_interp+vec2(pixel_size.x,pixel_size.y) ).r; + lum_accum/=9.0; + +#endif #ifdef USE_HDR_STORE +#ifdef USE_8BIT_HDR highp float vd_lum = dot(texture2D( source_vd_lum, vec2(0.0) ), _multcv ); lum_accum = clamp( vd_lum + (lum_accum-vd_lum)*hdr_time_delta*hdr_exp_adj_speed,min_luminance*(1.0/LUM_RANGE),max_luminance*(1.0/LUM_RANGE)); +#else + highp float vd_lum=texture2D( source_vd_lum, vec2(0.0) ); + lum_accum = clamp( vd_lum + (lum_accum-vd_lum)*hdr_time_delta*hdr_exp_adj_speed,min_luminance,max_luminance); +#endif + #endif +#ifdef USE_8BIT_HDR highp vec4 comp = fract(lum_accum * vec4(256.0 * 256.0 * 256.0, 256.0 * 256.0, 256.0, 1.0)); comp -= comp.xxyz * vec4(0.0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0); color=comp; +#else + color.rgb=vec3(lum_accum); +#endif + + #endif #ifdef USE_RGBE @@ -338,6 +473,8 @@ void main() { #ifdef USE_CUSTOM_ALPHA color.a=custom_alpha; #endif + + gl_FragColor = color; } diff --git a/drivers/gles2/shaders/material.glsl b/drivers/gles2/shaders/material.glsl index 7ed59ae9cd..3aed9820ed 100644 --- a/drivers/gles2/shaders/material.glsl +++ b/drivers/gles2/shaders/material.glsl @@ -342,7 +342,7 @@ VERTEX_SHADER_CODE #ifdef USE_FOG - fog_interp.a = pow( clamp( (-vertex_interp.z-fog_params.x)/(fog_params.y-fog_params.x), 0.0, 1.0 ), fog_params.z ); + fog_interp.a = pow( clamp( (length(vertex_interp)-fog_params.x)/(fog_params.y-fog_params.x), 0.0, 1.0 ), fog_params.z ); fog_interp.rgb = mix( fog_color_begin, fog_color_end, fog_interp.a ); #endif @@ -666,9 +666,14 @@ float SAMPLE_SHADOW_TEX( highp vec2 coord, highp float refdepth) { #ifdef USE_SHADOW_ESM +uniform float esm_multiplier; float SAMPLE_SHADOW_TEX(vec2 p_uv,float p_depth) { +#if defined (USE_DEPTH_SHADOWS) + //these only are used if interpolation exists + highp float occluder = SHADOW_DEPTH(shadow_texture, p_uv); +#else vec2 unnormalized = p_uv/shadow_texel_size; vec2 fractional = fract(unnormalized); unnormalized = floor(unnormalized); @@ -681,7 +686,8 @@ float SAMPLE_SHADOW_TEX(vec2 p_uv,float p_depth) { highp float occluder = (exponent.w + (exponent.x - exponent.w) * fractional.y); occluder = occluder + ((exponent.z + (exponent.y - exponent.z) * fractional.y) - occluder)*fractional.x; - return clamp(exp(28.0 * ( occluder - p_depth )),0.0,1.0); +#endif + return clamp(exp(esm_multiplier* ( occluder - p_depth )),0.0,1.0); } @@ -818,7 +824,7 @@ FRAGMENT_SHADER_CODE vec3 col_up=texture2D(ambient_octree_tex,octant_uv).rgb; octant_uv.y+=ambient_octree_pix_size.y*2.0; vec3 col_down=texture2D(ambient_octree_tex,octant_uv).rgb; - ambientmap_color=mix(col_down,col_up,1.0-sub.z); + ambientmap_color=mix(col_up,col_down,sub.z); ambientmap_color*=diffuse.rgb; @@ -866,6 +872,15 @@ FRAGMENT_SHADER_CODE vec2 pssm_coord; float pssm_z; +#if defined(LIGHT_USE_PSSM) && defined(USE_SHADOW_ESM) +#define USE_PSSM_BLEND + float pssm_blend; + vec2 pssm_coord_2; + float pssm_z_2; + vec3 light_pssm_split_inv = 1.0/light_pssm_split; + float w_inv = 1.0/gl_FragCoord.w; +#endif + #ifdef LIGHT_USE_PSSM4 @@ -874,10 +889,21 @@ FRAGMENT_SHADER_CODE if (gl_FragCoord.w > light_pssm_split.x) { pssm_coord=shadow_coord.xy; pssm_z=shadow_coord.z; +#if defined(USE_PSSM_BLEND) + pssm_coord_2=shadow_coord2.xy; + pssm_z_2=shadow_coord2.z; + pssm_blend=smoothstep(0.0,light_pssm_split_inv.x,w_inv); +#endif } else { pssm_coord=shadow_coord2.xy; pssm_z=shadow_coord2.z; +#if defined(USE_PSSM_BLEND) + pssm_coord_2=shadow_coord3.xy; + pssm_z_2=shadow_coord3.z; + pssm_blend=smoothstep(light_pssm_split_inv.x,light_pssm_split_inv.y,w_inv); +#endif + } } else { @@ -885,9 +911,21 @@ FRAGMENT_SHADER_CODE if (gl_FragCoord.w > light_pssm_split.z) { pssm_coord=shadow_coord3.xy; pssm_z=shadow_coord3.z; +#if defined(USE_PSSM_BLEND) + pssm_coord_2=shadow_coord4.xy; + pssm_z_2=shadow_coord4.z; + pssm_blend=smoothstep(light_pssm_split_inv.y,light_pssm_split_inv.z,w_inv); +#endif + } else { pssm_coord=shadow_coord4.xy; pssm_z=shadow_coord4.z; +#if defined(USE_PSSM_BLEND) + pssm_coord_2=shadow_coord4.xy; + pssm_z_2=shadow_coord4.z; + pssm_blend=0.0; +#endif + } } @@ -896,16 +934,31 @@ FRAGMENT_SHADER_CODE if (gl_FragCoord.w > light_pssm_split.x) { pssm_coord=shadow_coord.xy; pssm_z=shadow_coord.z; +#if defined(USE_PSSM_BLEND) + pssm_coord_2=shadow_coord2.xy; + pssm_z_2=shadow_coord2.z; + pssm_blend=smoothstep(0.0,light_pssm_split_inv.x,w_inv); +#endif } else { pssm_coord=shadow_coord2.xy; pssm_z=shadow_coord2.z; +#if defined(USE_PSSM_BLEND) + pssm_coord_2=shadow_coord2.xy; + pssm_z_2=shadow_coord2.z; + pssm_blend=0.0; +#endif + } #endif //one one sample shadow_attenuation=SAMPLE_SHADOW_TEX(pssm_coord,pssm_z); +#if defined(USE_PSSM_BLEND) + shadow_attenuation=mix(shadow_attenuation,SAMPLE_SHADOW_TEX(pssm_coord_2,pssm_z_2),pssm_blend); +#endif + #endif @@ -1054,7 +1107,7 @@ FRAGMENT_SHADER_CODE diffuse.a=glow; #endif -#ifdef USE_HDR +#ifdef USE_8BIT_HDR diffuse.rgb*=0.25; #endif |