From e8a31f6a628ca309d1c5119d6d52c391e225445d Mon Sep 17 00:00:00 2001 From: SmileTheory Date: Thu, 3 Dec 2015 02:44:33 -0800 Subject: OpenGL2: Remove some rendering options for simplicity. --- src/renderergl2/glsl/lightall_fp.glsl | 224 ++++++---------------------------- 1 file changed, 35 insertions(+), 189 deletions(-) (limited to 'src/renderergl2/glsl') diff --git a/src/renderergl2/glsl/lightall_fp.glsl b/src/renderergl2/glsl/lightall_fp.glsl index d1182781..c0d2bc80 100644 --- a/src/renderergl2/glsl/lightall_fp.glsl +++ b/src/renderergl2/glsl/lightall_fp.glsl @@ -150,156 +150,35 @@ float RayIntersectDisplaceMap(vec2 dp, vec2 ds, sampler2D normalMap) } #endif -vec3 CalcDiffuse(vec3 diffuseAlbedo, vec3 N, vec3 L, vec3 E, float NE, float NL, float shininess) +vec3 CalcDiffuse(vec3 diffuseAlbedo, float EH, float NH, float r) { - #if defined(USE_OREN_NAYAR) || defined(USE_TRIACE_OREN_NAYAR) - float gamma = dot(E, L) - NE * NL; - float B = 2.22222 + 0.1 * shininess; - - #if defined(USE_OREN_NAYAR) - float A = 1.0 - 1.0 / (2.0 + 0.33 * shininess); - gamma = clamp(gamma, 0.0, 1.0); - #endif - - #if defined(USE_TRIACE_OREN_NAYAR) - float A = 1.0 - 1.0 / (2.0 + 0.65 * shininess); - - if (gamma >= 0.0) - #endif - { - B = max(B * max(NL, NE), EPSILON); - } - - return diffuseAlbedo * (A + gamma / B); - #else - return diffuseAlbedo; - #endif -} - -vec3 EnvironmentBRDF(float gloss, float NE, vec3 specular) -{ - #if 1 - // from http://blog.selfshadow.com/publications/s2013-shading-course/lazarov/s2013_pbs_black_ops_2_notes.pdf - vec4 t = vec4( 1.0/0.96, 0.475, (0.0275 - 0.25 * 0.04)/0.96,0.25 ) * gloss; - t += vec4( 0.0, 0.0, (0.015 - 0.75 * 0.04)/0.96,0.75 ); - float a0 = t.x * min( t.y, exp2( -9.28 * NE ) ) + t.z; - float a1 = t.w; - return clamp( a0 + specular * ( a1 - a0 ), 0.0, 1.0 ); - #elif 0 - // from http://seblagarde.wordpress.com/2011/08/17/hello-world/ - return specular + CalcFresnel(NE) * clamp(vec3(gloss) - specular, 0.0, 1.0); - #else - // from http://advances.realtimerendering.com/s2011/Lazarov-Physically-Based-Lighting-in-Black-Ops%20%28Siggraph%202011%20Advances%20in%20Real-Time%20Rendering%20Course%29.pptx - return mix(specular.rgb, vec3(1.0), CalcFresnel(NE) / (4.0 - 3.0 * gloss)); - #endif -} - -float CalcBlinn(float NH, float shininess) -{ -#if defined(USE_BLINN) || defined(USE_BLINN_FRESNEL) - // Normalized Blinn-Phong - float norm = shininess * 0.125 + 1.0; -#elif defined(USE_MCAULEY) - // Cook-Torrance as done by Stephen McAuley - // http://blog.selfshadow.com/publications/s2012-shading-course/mcauley/s2012_pbs_farcry3_notes_v2.pdf - float norm = shininess * 0.25 + 0.125; -#elif defined(USE_GOTANDA) - // Neumann-Neumann as done by Yoshiharu Gotanda - // http://research.tri-ace.com/Data/s2012_beyond_CourseNotes.pdf - float norm = shininess * 0.124858 + 0.269182; -#elif defined(USE_LAZAROV) - // Cook-Torrance as done by Dimitar Lazarov - // http://blog.selfshadow.com/publications/s2013-shading-course/lazarov/s2013_pbs_black_ops_2_notes.pdf - float norm = shininess * 0.125 + 0.25; -#else - float norm = 1.0; -#endif - -#if 0 - // from http://seblagarde.wordpress.com/2012/06/03/spherical-gaussien-approximation-for-blinn-phong-phong-and-fresnel/ - float a = shininess + 0.775; - return norm * exp(a * NH - a); -#else - return norm * pow(NH, shininess); -#endif -} - -float CalcGGX(float NH, float gloss) -{ - // from http://blog.selfshadow.com/publications/s2013-shading-course/karis/s2013_pbs_epic_notes_v2.pdf - float a_sq = exp2(gloss * -13.0 + 1.0); - float d = ((NH * NH) * (a_sq - 1.0) + 1.0); - return a_sq / (d * d); -} - -float CalcFresnel(float EH) -{ -#if 1 - // From http://blog.selfshadow.com/publications/s2013-shading-course/lazarov/s2013_pbs_black_ops_2_notes.pdf - // not accurate, but fast - return exp2(-10.0 * EH); -#elif 0 - // From http://seblagarde.wordpress.com/2012/06/03/spherical-gaussien-approximation-for-blinn-phong-phong-and-fresnel/ - return exp2((-5.55473 * EH - 6.98316) * EH); -#elif 0 - float blend = 1.0 - EH; - float blend2 = blend * blend; - blend *= blend2 * blend2; - - return blend; +#if defined(USE_BURLEY) + // modified from https://disney-animation.s3.amazonaws.com/library/s2012_pbs_disney_brdf_notes_v2.pdf + float fd90 = -0.5 + EH * EH * r; + float burley = 1.0 + fd90 * 0.04 / NH; + burley *= burley; + return diffuseAlbedo * burley; #else - return pow(1.0 - EH, 5.0); + return diffuseAlbedo; #endif } -float CalcVisibility(float NH, float NL, float NE, float EH, float gloss) +vec3 EnvironmentBRDF(float r, float NE, vec3 specular) { -#if defined(USE_GOTANDA) - // Neumann-Neumann as done by Yoshiharu Gotanda - // http://research.tri-ace.com/Data/s2012_beyond_CourseNotes.pdf - return 1.0 / max(max(NL, NE), EPSILON); -#elif defined(USE_LAZAROV) - // Cook-Torrance as done by Dimitar Lazarov - // http://blog.selfshadow.com/publications/s2013-shading-course/lazarov/s2013_pbs_black_ops_2_notes.pdf - float k = min(1.0, gloss + 0.545); - return 1.0 / (k * (EH * EH - 1.0) + 1.0); -#elif defined(USE_GGX) - float roughness = exp2(gloss * -6.5); - - // Modified from http://blog.selfshadow.com/publications/s2013-shading-course/karis/s2013_pbs_epic_notes_v2.pdf - // NL, NE in numerator factored out from cook-torrance - float k = roughness + 1.0; - k *= k * 0.125; - - float k2 = 1.0 - k; - - float invGeo1 = NL * k2 + k; - float invGeo2 = NE * k2 + k; - - return 1.0 / (invGeo1 * invGeo2); -#else - return 1.0; -#endif + // from http://community.arm.com/servlet/JiveServlet/download/96891546-19496/siggraph2015-mmg-renaldas-slides.pdf + float v = 1.0 - max(r, NE); + v *= v * v; + return vec3(v) + specular; } - -vec3 CalcSpecular(vec3 specular, float NH, float NL, float NE, float EH, float gloss, float shininess) +vec3 CalcSpecular(vec3 specular, float NH, float NL, float NE, float EH, float r) { -#if defined(USE_GGX) - float distrib = CalcGGX(NH, gloss); -#else - float distrib = CalcBlinn(NH, shininess); -#endif - -#if defined(USE_BLINN) - vec3 fSpecular = specular; -#else - vec3 fSpecular = mix(specular, vec3(1.0), CalcFresnel(EH)); -#endif - - float vis = CalcVisibility(NH, NL, NE, EH, gloss); - - return fSpecular * (distrib * vis); + // from http://community.arm.com/servlet/JiveServlet/download/96891546-19496/siggraph2015-mmg-renaldas-slides.pdf + float rr = r*r; + float rrrr = rr*rr; + float d = (NH * NH) * (rrrr - 1.0) + 1.0; + float v = (EH * EH) * (r + 0.5); + return specular * (rrrr / (4.0 * d * d * v)); } @@ -421,14 +300,7 @@ void main() shadowValue *= float(dot(var_Normal.xyz, var_PrimaryLightDir.xyz) > 0.0); #if defined(SHADOWMAP_MODULATE) - //vec3 shadowColor = min(u_PrimaryLightAmbient, lightColor); - vec3 shadowColor = u_PrimaryLightAmbient * lightColor; - - #if 0 - // Only shadow when the world light is parallel to the primary light - shadowValue = 1.0 + (shadowValue - 1.0) * clamp(dot(L, var_PrimaryLightDir.xyz), 0.0, 1.0); - #endif - lightColor = mix(shadowColor, lightColor, shadowValue); + lightColor *= shadowValue * (1.0 - u_PrimaryLightAmbient.r) + u_PrimaryLightAmbient.r; #endif #endif @@ -465,65 +337,39 @@ void main() #if defined(r_materialGamma) diffuse.rgb = pow(diffuse.rgb, vec3(r_materialGamma)); + #if !defined(SPECULAR_IS_METALLIC) specular.rgb = pow(specular.rgb, vec3(r_materialGamma)); + #endif #endif float gloss = specular.a; - float shininess = exp2(gloss * 13.0); + float r = exp2(-3.0 * gloss); #if defined(SPECULAR_IS_METALLIC) - // diffuse is actually base color, and red of specular is metallicness - float metallic = specular.r; + // diffuse is actually base color, and green of specular is metallicness + float metallic = specular.g; - specular.rgb = (0.96 * metallic) * diffuse.rgb + vec3(0.04); + specular.rgb = metallic * diffuse.rgb + vec3(0.04 - 0.04 * metallic); diffuse.rgb *= 1.0 - metallic; #else // adjust diffuse by specular reflectance, to maintain energy conservation diffuse.rgb *= vec3(1.0) - specular.rgb; #endif - reflectance = CalcDiffuse(diffuse.rgb, N, L, E, NE, NL, shininess); - - #if defined(r_deluxeSpecular) || defined(USE_LIGHT_VECTOR) - float adjGloss = gloss; - float adjShininess = shininess; - - #if !defined(USE_LIGHT_VECTOR) - adjGloss *= r_deluxeSpecular; - adjShininess = exp2(adjGloss * 13.0); - #endif - - H = normalize(L + E); - + reflectance = CalcDiffuse(diffuse.rgb, EH, NH, r); + #if defined(USE_SHADOWMAP) && defined(SHADOWMAP_MODULATE) + // bit of a hack, with modulated shadowmaps, add specular to sunlight + H = normalize(var_PrimaryLightDir.xyz + E); EH = clamp(dot(E, H), 0.0, 1.0); NH = clamp(dot(N, H), 0.0, 1.0); - - #if !defined(USE_LIGHT_VECTOR) - reflectance += CalcSpecular(specular.rgb, NH, NL, NE, EH, adjGloss, adjShininess) * r_deluxeSpecular; - #else - reflectance += CalcSpecular(specular.rgb, NH, NL, NE, EH, adjGloss, adjShininess); - #endif + reflectance += shadowValue * CalcSpecular(specular.rgb, NH, NL, NE, EH, r); #endif gl_FragColor.rgb = lightColor * reflectance * (attenuation * NL); - -#if 0 - vec3 aSpecular = EnvironmentBRDF(gloss, NE, specular.rgb); - - // do ambient as two hemisphere lights, one straight up one straight down - float hemiDiffuseUp = N.z * 0.5 + 0.5; - float hemiDiffuseDown = 1.0 - hemiDiffuseUp; - float hemiSpecularUp = mix(hemiDiffuseUp, float(N.z >= 0.0), gloss); - float hemiSpecularDown = 1.0 - hemiSpecularUp; - - gl_FragColor.rgb += ambientColor * 0.75 * (diffuse.rgb * hemiDiffuseUp + aSpecular * hemiSpecularUp); - gl_FragColor.rgb += ambientColor * 0.25 * (diffuse.rgb * hemiDiffuseDown + aSpecular * hemiSpecularDown); -#else gl_FragColor.rgb += ambientColor * (diffuse.rgb + specular.rgb); -#endif #if defined(USE_CUBEMAP) - reflectance = EnvironmentBRDF(gloss, NE, specular.rgb); + reflectance = EnvironmentBRDF(r, NE, specular.rgb); vec3 R = reflect(E, N); @@ -565,8 +411,8 @@ void main() EH2 = clamp(dot(E, H2), 0.0, 1.0); NH2 = clamp(dot(N, H2), 0.0, 1.0); - reflectance = CalcDiffuse(diffuse.rgb, N, L2, E, NE, NL2, shininess); - reflectance += CalcSpecular(specular.rgb, NH2, NL2, NE, EH2, gloss, shininess); + reflectance = CalcDiffuse(diffuse.rgb, EH2, NH2, r); + reflectance += CalcSpecular(specular.rgb, NH2, NL2, NE, EH2, r); lightColor = u_PrimaryLightColor * var_Color.rgb; -- cgit From e3ac1437c4e4df764cc3279324fab7e3ee032e2a Mon Sep 17 00:00:00 2001 From: SmileTheory Date: Tue, 8 Dec 2015 21:23:50 -0800 Subject: OpenGL2: Add r_glossIsRoughness. --- src/renderergl2/glsl/lightall_fp.glsl | 44 +++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 18 deletions(-) (limited to 'src/renderergl2/glsl') diff --git a/src/renderergl2/glsl/lightall_fp.glsl b/src/renderergl2/glsl/lightall_fp.glsl index c0d2bc80..6c8977e1 100644 --- a/src/renderergl2/glsl/lightall_fp.glsl +++ b/src/renderergl2/glsl/lightall_fp.glsl @@ -150,11 +150,11 @@ float RayIntersectDisplaceMap(vec2 dp, vec2 ds, sampler2D normalMap) } #endif -vec3 CalcDiffuse(vec3 diffuseAlbedo, float EH, float NH, float r) +vec3 CalcDiffuse(vec3 diffuseAlbedo, float EH, float NH, float roughness) { #if defined(USE_BURLEY) // modified from https://disney-animation.s3.amazonaws.com/library/s2012_pbs_disney_brdf_notes_v2.pdf - float fd90 = -0.5 + EH * EH * r; + float fd90 = -0.5 + EH * EH * roughness; float burley = 1.0 + fd90 * 0.04 / NH; burley *= burley; return diffuseAlbedo * burley; @@ -163,21 +163,21 @@ vec3 CalcDiffuse(vec3 diffuseAlbedo, float EH, float NH, float r) #endif } -vec3 EnvironmentBRDF(float r, float NE, vec3 specular) +vec3 EnvironmentBRDF(float roughness, float NE, vec3 specular) { // from http://community.arm.com/servlet/JiveServlet/download/96891546-19496/siggraph2015-mmg-renaldas-slides.pdf - float v = 1.0 - max(r, NE); + float v = 1.0 - max(roughness, NE); v *= v * v; return vec3(v) + specular; } -vec3 CalcSpecular(vec3 specular, float NH, float NL, float NE, float EH, float r) +vec3 CalcSpecular(vec3 specular, float NH, float NL, float NE, float EH, float roughness) { // from http://community.arm.com/servlet/JiveServlet/download/96891546-19496/siggraph2015-mmg-renaldas-slides.pdf - float rr = r*r; + float rr = roughness*roughness; float rrrr = rr*rr; float d = (NH * NH) * (rrrr - 1.0) + 1.0; - float v = (EH * EH) * (r + 0.5); + float v = (EH * EH) * (roughness + 0.5); return specular * (rrrr / (4.0 * d * d * v)); } @@ -276,6 +276,11 @@ void main() attenuation = 1.0; #endif + #if defined(r_lightGamma) + lightColor = pow(lightColor, vec3(r_lightGamma)); + ambientColor = pow(ambientColor, vec3(r_lightGamma)); + #endif + #if defined(USE_NORMALMAP) #if defined(SWIZZLE_NORMALMAP) N.xy = texture2D(u_NormalMap, texCoords).ag - vec2(0.5); @@ -304,11 +309,6 @@ void main() #endif #endif - #if defined(r_lightGamma) - lightColor = pow(lightColor, vec3(r_lightGamma)); - ambientColor = pow(ambientColor, vec3(r_lightGamma)); - #endif - #if defined(USE_LIGHTMAP) || defined(USE_LIGHT_VERTEX) ambientColor = lightColor; float surfNL = clamp(dot(var_Normal.xyz, L), 0.0, 1.0); @@ -343,7 +343,11 @@ void main() #endif float gloss = specular.a; - float r = exp2(-3.0 * gloss); + #if defined(GLOSS_IS_ROUGHNESS) + float roughness = gloss; + #else + float roughness = exp2(-3.0 * gloss); + #endif #if defined(SPECULAR_IS_METALLIC) // diffuse is actually base color, and green of specular is metallicness @@ -356,20 +360,20 @@ void main() diffuse.rgb *= vec3(1.0) - specular.rgb; #endif - reflectance = CalcDiffuse(diffuse.rgb, EH, NH, r); + reflectance = CalcDiffuse(diffuse.rgb, EH, NH, roughness); #if defined(USE_SHADOWMAP) && defined(SHADOWMAP_MODULATE) // bit of a hack, with modulated shadowmaps, add specular to sunlight H = normalize(var_PrimaryLightDir.xyz + E); EH = clamp(dot(E, H), 0.0, 1.0); NH = clamp(dot(N, H), 0.0, 1.0); - reflectance += shadowValue * CalcSpecular(specular.rgb, NH, NL, NE, EH, r); + reflectance += shadowValue * CalcSpecular(specular.rgb, NH, NL, NE, EH, roughness); #endif gl_FragColor.rgb = lightColor * reflectance * (attenuation * NL); gl_FragColor.rgb += ambientColor * (diffuse.rgb + specular.rgb); #if defined(USE_CUBEMAP) - reflectance = EnvironmentBRDF(r, NE, specular.rgb); + reflectance = EnvironmentBRDF(roughness, NE, specular.rgb); vec3 R = reflect(E, N); @@ -377,7 +381,11 @@ void main() // from http://seblagarde.wordpress.com/2012/09/29/image-based-lighting-approaches-and-parallax-corrected-cubemap/ vec3 parallax = u_CubeMapInfo.xyz + u_CubeMapInfo.w * viewDir; + #if defined(GLOSS_IS_ROUGHNESS) + vec3 cubeLightColor = textureCubeLod(u_CubeMap, R + parallax, roughness).rgb * u_EnableTextures.w; + #else vec3 cubeLightColor = textureCubeLod(u_CubeMap, R + parallax, 7.0 - gloss * 7.0).rgb * u_EnableTextures.w; + #endif // normalize cubemap based on lowest mip (~diffuse) // multiplying cubemap values by lighting below depends on either this or the cubemap being normalized at generation @@ -411,8 +419,8 @@ void main() EH2 = clamp(dot(E, H2), 0.0, 1.0); NH2 = clamp(dot(N, H2), 0.0, 1.0); - reflectance = CalcDiffuse(diffuse.rgb, EH2, NH2, r); - reflectance += CalcSpecular(specular.rgb, NH2, NL2, NE, EH2, r); + reflectance = CalcDiffuse(diffuse.rgb, EH2, NH2, roughness); + reflectance += CalcSpecular(specular.rgb, NH2, NL2, NE, EH2, roughness); lightColor = u_PrimaryLightColor * var_Color.rgb; -- cgit From c9cf340a4eb8d81b44e8c02b167b9ab6319167fd Mon Sep 17 00:00:00 2001 From: SmileTheory Date: Wed, 9 Dec 2015 03:42:12 -0800 Subject: OpenGL2: Forgot a multiply. --- src/renderergl2/glsl/lightall_fp.glsl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/renderergl2/glsl') diff --git a/src/renderergl2/glsl/lightall_fp.glsl b/src/renderergl2/glsl/lightall_fp.glsl index 6c8977e1..9330b1f8 100644 --- a/src/renderergl2/glsl/lightall_fp.glsl +++ b/src/renderergl2/glsl/lightall_fp.glsl @@ -382,7 +382,7 @@ void main() vec3 parallax = u_CubeMapInfo.xyz + u_CubeMapInfo.w * viewDir; #if defined(GLOSS_IS_ROUGHNESS) - vec3 cubeLightColor = textureCubeLod(u_CubeMap, R + parallax, roughness).rgb * u_EnableTextures.w; + vec3 cubeLightColor = textureCubeLod(u_CubeMap, R + parallax, 7.0 * roughness).rgb * u_EnableTextures.w; #else vec3 cubeLightColor = textureCubeLod(u_CubeMap, R + parallax, 7.0 - gloss * 7.0).rgb * u_EnableTextures.w; #endif -- cgit From 9cdedf11eff894db8f9f572ab52eae37fdacf473 Mon Sep 17 00:00:00 2001 From: SmileTheory Date: Wed, 9 Dec 2015 05:25:58 -0800 Subject: OpenGL2: Use correct sunlight color for sunlight specular. --- src/renderergl2/glsl/lightall_fp.glsl | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) (limited to 'src/renderergl2/glsl') diff --git a/src/renderergl2/glsl/lightall_fp.glsl b/src/renderergl2/glsl/lightall_fp.glsl index 9330b1f8..17dc9fca 100644 --- a/src/renderergl2/glsl/lightall_fp.glsl +++ b/src/renderergl2/glsl/lightall_fp.glsl @@ -361,13 +361,6 @@ void main() #endif reflectance = CalcDiffuse(diffuse.rgb, EH, NH, roughness); - #if defined(USE_SHADOWMAP) && defined(SHADOWMAP_MODULATE) - // bit of a hack, with modulated shadowmaps, add specular to sunlight - H = normalize(var_PrimaryLightDir.xyz + E); - EH = clamp(dot(E, H), 0.0, 1.0); - NH = clamp(dot(N, H), 0.0, 1.0); - reflectance += shadowValue * CalcSpecular(specular.rgb, NH, NL, NE, EH, roughness); - #endif gl_FragColor.rgb = lightColor * reflectance * (attenuation * NL); gl_FragColor.rgb += ambientColor * (diffuse.rgb + specular.rgb); @@ -403,7 +396,7 @@ void main() gl_FragColor.rgb += cubeLightColor * reflectance; #endif - #if defined(USE_PRIMARY_LIGHT) + #if defined(USE_PRIMARY_LIGHT) || defined(SHADOWMAP_MODULATE) vec3 L2, H2; float NL2, EH2, NH2; @@ -419,10 +412,14 @@ void main() EH2 = clamp(dot(E, H2), 0.0, 1.0); NH2 = clamp(dot(N, H2), 0.0, 1.0); - reflectance = CalcDiffuse(diffuse.rgb, EH2, NH2, roughness); - reflectance += CalcSpecular(specular.rgb, NH2, NL2, NE, EH2, roughness); + reflectance = CalcSpecular(specular.rgb, NH2, NL2, NE, EH2, roughness); + + // bit of a hack, with modulated shadowmaps, ignore diffuse + #if !defined(SHADOWMAP_MODULATE) + reflectance += CalcDiffuse(diffuse.rgb, EH2, NH2, roughness); + #endif - lightColor = u_PrimaryLightColor * var_Color.rgb; + lightColor = u_PrimaryLightColor; #if defined(r_lightGamma) lightColor = pow(lightColor, vec3(r_lightGamma)); -- cgit From ac7296d42f36891d13d7cd475f5ce46fa6ef3d63 Mon Sep 17 00:00:00 2001 From: SmileTheory Date: Fri, 15 Jan 2016 02:46:30 -0800 Subject: OpenGL2: Remove specular ambient. --- src/renderergl2/glsl/lightall_fp.glsl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/renderergl2/glsl') diff --git a/src/renderergl2/glsl/lightall_fp.glsl b/src/renderergl2/glsl/lightall_fp.glsl index 17dc9fca..b8f3985f 100644 --- a/src/renderergl2/glsl/lightall_fp.glsl +++ b/src/renderergl2/glsl/lightall_fp.glsl @@ -363,7 +363,7 @@ void main() reflectance = CalcDiffuse(diffuse.rgb, EH, NH, roughness); gl_FragColor.rgb = lightColor * reflectance * (attenuation * NL); - gl_FragColor.rgb += ambientColor * (diffuse.rgb + specular.rgb); + gl_FragColor.rgb += ambientColor * diffuse.rgb; #if defined(USE_CUBEMAP) reflectance = EnvironmentBRDF(roughness, NE, specular.rgb); -- cgit From d5ba7bb70ef327c8856165475b58e946c8fbd8ce Mon Sep 17 00:00:00 2001 From: SmileTheory Date: Mon, 1 Feb 2016 21:37:23 -0800 Subject: OpenGL2: Merge several cvars into r_pbr. r_glossIsRoughness, r_specularIsMetallic, r_framebufferGamma, r_tonemapGamma, r_materialGamma, r_lightGamma --- src/renderergl2/glsl/calclevels4x_fp.glsl | 4 +-- src/renderergl2/glsl/lightall_fp.glsl | 48 ++++++++++++------------------- src/renderergl2/glsl/tonemap_fp.glsl | 8 +++--- 3 files changed, 25 insertions(+), 35 deletions(-) (limited to 'src/renderergl2/glsl') diff --git a/src/renderergl2/glsl/calclevels4x_fp.glsl b/src/renderergl2/glsl/calclevels4x_fp.glsl index 1de59e9f..0d298b62 100644 --- a/src/renderergl2/glsl/calclevels4x_fp.glsl +++ b/src/renderergl2/glsl/calclevels4x_fp.glsl @@ -14,8 +14,8 @@ vec3 GetValues(vec2 offset, vec3 current) #ifdef FIRST_PASS - #if defined(r_framebufferGamma) - minAvgMax = pow(minAvgMax, vec3(r_framebufferGamma)); + #if defined(USE_PBR) + minAvgMax = pow(minAvgMax, vec3(2.2)); #endif float lumi = max(dot(LUMINANCE_VECTOR, minAvgMax), 0.000001); diff --git a/src/renderergl2/glsl/lightall_fp.glsl b/src/renderergl2/glsl/lightall_fp.glsl index b8f3985f..eb8ba900 100644 --- a/src/renderergl2/glsl/lightall_fp.glsl +++ b/src/renderergl2/glsl/lightall_fp.glsl @@ -276,9 +276,9 @@ void main() attenuation = 1.0; #endif - #if defined(r_lightGamma) - lightColor = pow(lightColor, vec3(r_lightGamma)); - ambientColor = pow(ambientColor, vec3(r_lightGamma)); + #if defined(USE_PBR) + lightColor = pow(lightColor, vec3(2.2)); + ambientColor = pow(ambientColor, vec3(2.2)); #endif #if defined(USE_NORMALMAP) @@ -319,7 +319,7 @@ void main() // Recover any unused light as ambient, in case attenuation is over 4x or // light is below the surface - ambientColor = clamp(ambientColor - lightColor * surfNL, 0.0, 1.0); + ambientColor = max(ambientColor - lightColor * surfNL, vec3(0.0)); #endif vec3 reflectance; @@ -335,21 +335,18 @@ void main() specular *= u_SpecularScale; - #if defined(r_materialGamma) - diffuse.rgb = pow(diffuse.rgb, vec3(r_materialGamma)); - #if !defined(SPECULAR_IS_METALLIC) - specular.rgb = pow(specular.rgb, vec3(r_materialGamma)); - #endif + #if defined(USE_PBR) + diffuse.rgb = pow(diffuse.rgb, vec3(2.2)); #endif float gloss = specular.a; - #if defined(GLOSS_IS_ROUGHNESS) - float roughness = gloss; + #if defined(USE_PBR) + float roughness = 1.0 - specular.r; #else float roughness = exp2(-3.0 * gloss); #endif - #if defined(SPECULAR_IS_METALLIC) + #if defined(USE_PBR) // diffuse is actually base color, and green of specular is metallicness float metallic = specular.g; @@ -374,7 +371,7 @@ void main() // from http://seblagarde.wordpress.com/2012/09/29/image-based-lighting-approaches-and-parallax-corrected-cubemap/ vec3 parallax = u_CubeMapInfo.xyz + u_CubeMapInfo.w * viewDir; - #if defined(GLOSS_IS_ROUGHNESS) + #if defined(USE_PBR) vec3 cubeLightColor = textureCubeLod(u_CubeMap, R + parallax, 7.0 * roughness).rgb * u_EnableTextures.w; #else vec3 cubeLightColor = textureCubeLod(u_CubeMap, R + parallax, 7.0 - gloss * 7.0).rgb * u_EnableTextures.w; @@ -385,8 +382,8 @@ void main() //vec3 cubeLightDiffuse = max(textureCubeLod(u_CubeMap, N, 6.0).rgb, 0.5 / 255.0); //cubeLightColor /= dot(cubeLightDiffuse, vec3(0.2125, 0.7154, 0.0721)); - #if defined(r_framebufferGamma) - cubeLightColor = pow(cubeLightColor, vec3(r_framebufferGamma)); + #if defined(USE_PBR) + cubeLightColor = pow(cubeLightColor, vec3(2.2)); #endif // multiply cubemap values by lighting @@ -421,8 +418,8 @@ void main() lightColor = u_PrimaryLightColor; - #if defined(r_lightGamma) - lightColor = pow(lightColor, vec3(r_lightGamma)); + #if defined(USE_PBR) + lightColor = pow(lightColor, vec3(2.2)); #endif #if defined(USE_SHADOWMAP) @@ -434,6 +431,11 @@ void main() gl_FragColor.rgb += lightColor * reflectance * NL2; #endif + + #if defined(USE_PBR) + gl_FragColor.rgb = pow(gl_FragColor.rgb, vec3(1.0 / 2.2)); + #endif + #else lightColor = var_Color.rgb; @@ -441,21 +443,9 @@ void main() lightColor *= lightmapColor.rgb; #endif - #if defined(r_lightGamma) - lightColor = pow(lightColor, vec3(r_lightGamma)); - #endif - - #if defined(r_materialGamma) - diffuse.rgb = pow(diffuse.rgb, vec3(r_materialGamma)); - #endif - gl_FragColor.rgb = diffuse.rgb * lightColor; #endif -#if defined(r_framebufferGamma) - gl_FragColor.rgb = pow(gl_FragColor.rgb, vec3(1.0 / r_framebufferGamma)); -#endif - gl_FragColor.a = diffuse.a * var_Color.a; } diff --git a/src/renderergl2/glsl/tonemap_fp.glsl b/src/renderergl2/glsl/tonemap_fp.glsl index 1368c5bd..5d8841d6 100644 --- a/src/renderergl2/glsl/tonemap_fp.glsl +++ b/src/renderergl2/glsl/tonemap_fp.glsl @@ -28,8 +28,8 @@ void main() { vec4 color = texture2D(u_TextureMap, var_TexCoords) * u_Color; -#if defined(r_framebufferGamma) - color.rgb = pow(color.rgb, vec3(r_framebufferGamma)); +#if defined(USE_PBR) + color.rgb = pow(color.rgb, vec3(2.2)); #endif vec3 minAvgMax = texture2D(u_LevelsMap, var_TexCoords).rgb; @@ -46,8 +46,8 @@ void main() color.rgb = clamp(color.rgb * var_InvWhite, 0.0, 1.0); -#if defined(r_tonemapGamma) - color.rgb = pow(color.rgb, vec3(1.0 / r_tonemapGamma)); +#if defined(USE_PBR) + color.rgb = pow(color.rgb, vec3(1.0 / 2.2)); #endif gl_FragColor = color; -- cgit From fdf6b74c210f7cc1fd54472308fc61c9341c4de1 Mon Sep 17 00:00:00 2001 From: SmileTheory Date: Wed, 17 Feb 2016 20:06:18 -0800 Subject: OpenGL2: Shader optimization, and add dither to tonemap. --- src/renderergl2/glsl/calclevels4x_fp.glsl | 2 +- src/renderergl2/glsl/lightall_fp.glsl | 106 +++++++++++------------------- src/renderergl2/glsl/lightall_vp.glsl | 21 ++++-- src/renderergl2/glsl/tonemap_fp.glsl | 7 +- 4 files changed, 63 insertions(+), 73 deletions(-) (limited to 'src/renderergl2/glsl') diff --git a/src/renderergl2/glsl/calclevels4x_fp.glsl b/src/renderergl2/glsl/calclevels4x_fp.glsl index 0d298b62..8246c4b3 100644 --- a/src/renderergl2/glsl/calclevels4x_fp.glsl +++ b/src/renderergl2/glsl/calclevels4x_fp.glsl @@ -15,7 +15,7 @@ vec3 GetValues(vec2 offset, vec3 current) #ifdef FIRST_PASS #if defined(USE_PBR) - minAvgMax = pow(minAvgMax, vec3(2.2)); + minAvgMax *= minAvgMax; #endif float lumi = max(dot(LUMINANCE_VECTOR, minAvgMax), 0.000001); diff --git a/src/renderergl2/glsl/lightall_fp.glsl b/src/renderergl2/glsl/lightall_fp.glsl index eb8ba900..6a0a5155 100644 --- a/src/renderergl2/glsl/lightall_fp.glsl +++ b/src/renderergl2/glsl/lightall_fp.glsl @@ -29,11 +29,6 @@ uniform samplerCube u_CubeMap; uniform vec4 u_EnableTextures; #endif -#if defined(USE_LIGHT_VECTOR) && !defined(USE_FAST_LIGHT) -uniform vec3 u_DirectedLight; -uniform vec3 u_AmbientLight; -#endif - #if defined(USE_PRIMARY_LIGHT) || defined(USE_SHADOWMAP) uniform vec3 u_PrimaryLightColor; uniform vec3 u_PrimaryLightAmbient; @@ -53,6 +48,9 @@ uniform vec4 u_CubeMapInfo; varying vec4 var_TexCoords; varying vec4 var_Color; +#if (defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)) +varying vec4 var_ColorAmbient; +#endif #if (defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)) #if defined(USE_VERT_TANGENT_SPACE) @@ -150,7 +148,7 @@ float RayIntersectDisplaceMap(vec2 dp, vec2 ds, sampler2D normalMap) } #endif -vec3 CalcDiffuse(vec3 diffuseAlbedo, float EH, float NH, float roughness) +vec3 CalcDiffuse(vec3 diffuseAlbedo, float NH, float EH, float roughness) { #if defined(USE_BURLEY) // modified from https://disney-animation.s3.amazonaws.com/library/s2012_pbs_disney_brdf_notes_v2.pdf @@ -171,7 +169,7 @@ vec3 EnvironmentBRDF(float roughness, float NE, vec3 specular) return vec3(v) + specular; } -vec3 CalcSpecular(vec3 specular, float NH, float NL, float NE, float EH, float roughness) +vec3 CalcSpecular(vec3 specular, float NH, float EH, float roughness) { // from http://community.arm.com/servlet/JiveServlet/download/96891546-19496/siggraph2015-mmg-renaldas-slides.pdf float rr = roughness*roughness; @@ -220,7 +218,7 @@ mat3 cotangent_frame( vec3 N, vec3 p, vec2 uv ) void main() { - vec3 viewDir, lightColor, ambientColor; + vec3 viewDir, lightColor, ambientColor, reflectance; vec3 L, N, E, H; float NL, NH, NE, EH, attenuation; @@ -232,21 +230,20 @@ void main() mat3 tangentToWorld = cotangent_frame(var_Normal, -var_ViewDir, var_TexCoords.xy); viewDir = var_ViewDir; #endif - E = normalize(viewDir); - - L = var_LightDir.xyz; - #if defined(USE_DELUXEMAP) - L += (texture2D(u_DeluxeMap, var_TexCoords.zw).xyz - vec3(0.5)) * u_EnableTextures.y; - #endif - float sqrLightDist = dot(L, L); #endif + lightColor = var_Color.rgb; + #if defined(USE_LIGHTMAP) vec4 lightmapColor = texture2D(u_LightMap, var_TexCoords.zw); #if defined(RGBM_LIGHTMAP) lightmapColor.rgb *= lightmapColor.a; #endif + #if defined(USE_PBR) && !defined(USE_FAST_LIGHT) + lightmapColor.rgb *= lightmapColor.rgb; + #endif + lightColor *= lightmapColor.rgb; #endif vec2 texCoords = var_TexCoords.xy; @@ -262,23 +259,17 @@ void main() vec4 diffuse = texture2D(u_DiffuseMap, texCoords); #if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT) - #if defined(USE_LIGHTMAP) - lightColor = lightmapColor.rgb * var_Color.rgb; - ambientColor = vec3(0.0); - attenuation = 1.0; - #elif defined(USE_LIGHT_VECTOR) - lightColor = u_DirectedLight * var_Color.rgb; - ambientColor = u_AmbientLight * var_Color.rgb; - attenuation = CalcLightAttenuation(float(var_LightDir.w > 0.0), var_LightDir.w / sqrLightDist); - #elif defined(USE_LIGHT_VERTEX) - lightColor = var_Color.rgb; - ambientColor = vec3(0.0); - attenuation = 1.0; + L = var_LightDir.xyz; + #if defined(USE_DELUXEMAP) + L += (texture2D(u_DeluxeMap, var_TexCoords.zw).xyz - vec3(0.5)) * u_EnableTextures.y; #endif + float sqrLightDist = dot(L, L); + L /= sqrt(sqrLightDist); - #if defined(USE_PBR) - lightColor = pow(lightColor, vec3(2.2)); - ambientColor = pow(ambientColor, vec3(2.2)); + #if defined(USE_LIGHT_VECTOR) + attenuation = CalcLightAttenuation(float(var_LightDir.w > 0.0), var_LightDir.w / sqrLightDist); + #else + attenuation = 1.0; #endif #if defined(USE_NORMALMAP) @@ -295,21 +286,20 @@ void main() #endif N = normalize(N); - L /= sqrt(sqrLightDist); #if defined(USE_SHADOWMAP) vec2 shadowTex = gl_FragCoord.xy * r_FBufScale; float shadowValue = texture2D(u_ShadowMap, shadowTex).r; // surfaces not facing the light are always shadowed - shadowValue *= float(dot(var_Normal.xyz, var_PrimaryLightDir.xyz) > 0.0); + shadowValue *= clamp(dot(var_Normal.xyz, var_PrimaryLightDir.xyz), 0.0, 1.0); #if defined(SHADOWMAP_MODULATE) lightColor *= shadowValue * (1.0 - u_PrimaryLightAmbient.r) + u_PrimaryLightAmbient.r; #endif #endif - #if defined(USE_LIGHTMAP) || defined(USE_LIGHT_VERTEX) + #if !defined(USE_LIGHT_VECTOR) ambientColor = lightColor; float surfNL = clamp(dot(var_Normal.xyz, L), 0.0, 1.0); @@ -320,9 +310,9 @@ void main() // Recover any unused light as ambient, in case attenuation is over 4x or // light is below the surface ambientColor = max(ambientColor - lightColor * surfNL, vec3(0.0)); + #else + ambientColor = var_ColorAmbient.rgb; #endif - - vec3 reflectance; NL = clamp(dot(N, L), 0.0, 1.0); NE = clamp(dot(N, E), 0.0, 1.0); @@ -332,32 +322,30 @@ void main() #else vec4 specular = vec4(1.0); #endif - specular *= u_SpecularScale; #if defined(USE_PBR) - diffuse.rgb = pow(diffuse.rgb, vec3(2.2)); + diffuse.rgb *= diffuse.rgb; #endif - float gloss = specular.a; #if defined(USE_PBR) + // diffuse rgb is base color + // specular red is smoothness + // specular green is metallicness float roughness = 1.0 - specular.r; + specular.rgb = specular.g * diffuse.rgb + vec3(0.04 - 0.04 * specular.g); + diffuse.rgb *= 1.0 - specular.g; #else - float roughness = exp2(-3.0 * gloss); - #endif + // diffuse rgb is diffuse + // specular rgb is specular reflectance at normal incidence + // specular alpha is gloss + float roughness = exp2(-3.0 * specular.a); - #if defined(USE_PBR) - // diffuse is actually base color, and green of specular is metallicness - float metallic = specular.g; - - specular.rgb = metallic * diffuse.rgb + vec3(0.04 - 0.04 * metallic); - diffuse.rgb *= 1.0 - metallic; - #else // adjust diffuse by specular reflectance, to maintain energy conservation diffuse.rgb *= vec3(1.0) - specular.rgb; #endif - reflectance = CalcDiffuse(diffuse.rgb, EH, NH, roughness); + reflectance = CalcDiffuse(diffuse.rgb, NH, EH, roughness); gl_FragColor.rgb = lightColor * reflectance * (attenuation * NL); gl_FragColor.rgb += ambientColor * diffuse.rgb; @@ -371,11 +359,7 @@ void main() // from http://seblagarde.wordpress.com/2012/09/29/image-based-lighting-approaches-and-parallax-corrected-cubemap/ vec3 parallax = u_CubeMapInfo.xyz + u_CubeMapInfo.w * viewDir; - #if defined(USE_PBR) vec3 cubeLightColor = textureCubeLod(u_CubeMap, R + parallax, 7.0 * roughness).rgb * u_EnableTextures.w; - #else - vec3 cubeLightColor = textureCubeLod(u_CubeMap, R + parallax, 7.0 - gloss * 7.0).rgb * u_EnableTextures.w; - #endif // normalize cubemap based on lowest mip (~diffuse) // multiplying cubemap values by lighting below depends on either this or the cubemap being normalized at generation @@ -383,7 +367,7 @@ void main() //cubeLightColor /= dot(cubeLightDiffuse, vec3(0.2125, 0.7154, 0.0721)); #if defined(USE_PBR) - cubeLightColor = pow(cubeLightColor, vec3(2.2)); + cubeLightColor *= cubeLightColor; #endif // multiply cubemap values by lighting @@ -404,24 +388,19 @@ void main() //L2 /= sqrt(sqrLightDist); NL2 = clamp(dot(N, L2), 0.0, 1.0); - H2 = normalize(L2 + E); EH2 = clamp(dot(E, H2), 0.0, 1.0); NH2 = clamp(dot(N, H2), 0.0, 1.0); - reflectance = CalcSpecular(specular.rgb, NH2, NL2, NE, EH2, roughness); + reflectance = CalcSpecular(specular.rgb, NH2, EH2, roughness); // bit of a hack, with modulated shadowmaps, ignore diffuse #if !defined(SHADOWMAP_MODULATE) - reflectance += CalcDiffuse(diffuse.rgb, EH2, NH2, roughness); + reflectance += CalcDiffuse(diffuse.rgb, NH2, EH2, roughness); #endif lightColor = u_PrimaryLightColor; - #if defined(USE_PBR) - lightColor = pow(lightColor, vec3(2.2)); - #endif - #if defined(USE_SHADOWMAP) lightColor *= shadowValue; #endif @@ -433,15 +412,10 @@ void main() #endif #if defined(USE_PBR) - gl_FragColor.rgb = pow(gl_FragColor.rgb, vec3(1.0 / 2.2)); + gl_FragColor.rgb = sqrt(gl_FragColor.rgb); #endif #else - lightColor = var_Color.rgb; - - #if defined(USE_LIGHTMAP) - lightColor *= lightmapColor.rgb; - #endif gl_FragColor.rgb = diffuse.rgb * lightColor; diff --git a/src/renderergl2/glsl/lightall_vp.glsl b/src/renderergl2/glsl/lightall_vp.glsl index 59051d7c..783885e9 100644 --- a/src/renderergl2/glsl/lightall_vp.glsl +++ b/src/renderergl2/glsl/lightall_vp.glsl @@ -57,10 +57,8 @@ uniform float u_VertexLerp; #if defined(USE_LIGHT_VECTOR) uniform vec4 u_LightOrigin; uniform float u_LightRadius; - #if defined(USE_FAST_LIGHT) uniform vec3 u_DirectedLight; uniform vec3 u_AmbientLight; - #endif #endif #if defined(USE_PRIMARY_LIGHT) || defined(USE_SHADOWMAP) @@ -71,6 +69,9 @@ uniform float u_PrimaryLightRadius; varying vec4 var_TexCoords; varying vec4 var_Color; +#if defined(USE_LIGHT_VECTOR) && !defined(USE_FAST_LIGHT) +varying vec4 var_ColorAmbient; +#endif #if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT) #if defined(USE_VERT_TANGENT_SPACE) @@ -208,12 +209,24 @@ void main() var_Color = u_VertColor * attr_Color + u_BaseColor; -#if defined(USE_LIGHT_VECTOR) && defined(USE_FAST_LIGHT) +#if defined(USE_LIGHT_VECTOR) + #if defined(USE_FAST_LIGHT) float sqrLightDist = dot(L, L); - float attenuation = CalcLightAttenuation(u_LightOrigin.w, u_LightRadius * u_LightRadius / sqrLightDist); float NL = clamp(dot(normalize(normal), L) / sqrt(sqrLightDist), 0.0, 1.0); + float attenuation = CalcLightAttenuation(u_LightOrigin.w, u_LightRadius * u_LightRadius / sqrLightDist); var_Color.rgb *= u_DirectedLight * (attenuation * NL) + u_AmbientLight; + #else + var_ColorAmbient.rgb = u_AmbientLight * var_Color.rgb; + var_Color.rgb *= u_DirectedLight; + #if defined(USE_PBR) + var_ColorAmbient.rgb *= var_ColorAmbient.rgb; + #endif + #endif +#endif + +#if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT) && defined(USE_PBR) + var_Color.rgb *= var_Color.rgb; #endif #if defined(USE_PRIMARY_LIGHT) || defined(USE_SHADOWMAP) diff --git a/src/renderergl2/glsl/tonemap_fp.glsl b/src/renderergl2/glsl/tonemap_fp.glsl index 5d8841d6..9e24e24a 100644 --- a/src/renderergl2/glsl/tonemap_fp.glsl +++ b/src/renderergl2/glsl/tonemap_fp.glsl @@ -29,7 +29,7 @@ void main() vec4 color = texture2D(u_TextureMap, var_TexCoords) * u_Color; #if defined(USE_PBR) - color.rgb = pow(color.rgb, vec3(2.2)); + color.rgb *= color.rgb; #endif vec3 minAvgMax = texture2D(u_LevelsMap, var_TexCoords).rgb; @@ -47,8 +47,11 @@ void main() color.rgb = clamp(color.rgb * var_InvWhite, 0.0, 1.0); #if defined(USE_PBR) - color.rgb = pow(color.rgb, vec3(1.0 / 2.2)); + color.rgb = sqrt(color.rgb); #endif + // add a bit of dither to reduce banding + color.rgb += vec3(1.0/510.0 * mod(gl_FragCoord.x + gl_FragCoord.y, 2.0) - 1.0/1020.0); + gl_FragColor = color; } -- cgit From 5d1223366351d7ed9ee537d8f4245ab29b550d2c Mon Sep 17 00:00:00 2001 From: SmileTheory Date: Mon, 7 Mar 2016 02:27:03 -0800 Subject: OpenGL2: Add r_shadowBlur. --- src/renderergl2/glsl/depthblur_fp.glsl | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) (limited to 'src/renderergl2/glsl') diff --git a/src/renderergl2/glsl/depthblur_fp.glsl b/src/renderergl2/glsl/depthblur_fp.glsl index 93895b4e..15f7be27 100644 --- a/src/renderergl2/glsl/depthblur_fp.glsl +++ b/src/renderergl2/glsl/depthblur_fp.glsl @@ -1,13 +1,13 @@ uniform sampler2D u_ScreenImageMap; uniform sampler2D u_ScreenDepthMap; -uniform vec4 u_ViewInfo; // zfar / znear, zfar +uniform vec4 u_ViewInfo; // zfar / znear, zfar, 1/width, 1/height varying vec2 var_ScreenTex; //float gauss[5] = float[5](0.30, 0.23, 0.097, 0.024, 0.0033); float gauss[4] = float[4](0.40, 0.24, 0.054, 0.0044); //float gauss[3] = float[3](0.60, 0.19, 0.0066); -#define GAUSS_SIZE 4 +#define BLUR_SIZE 4 float getLinearDepth(sampler2D depthMap, const vec2 tex, const float zFarDivZNear) { @@ -17,7 +17,7 @@ float getLinearDepth(sampler2D depthMap, const vec2 tex, const float zFarDivZNea vec4 depthGaussian1D(sampler2D imageMap, sampler2D depthMap, vec2 tex, float zFarDivZNear, float zFar) { - float scale = 1.0 / 256.0; + vec2 scale = u_ViewInfo.zw; #if defined(USE_HORIZONTAL_BLUR) vec2 direction = vec2(1.0, 0.0) * scale; @@ -27,22 +27,32 @@ vec4 depthGaussian1D(sampler2D imageMap, sampler2D depthMap, vec2 tex, float zFa float depthCenter = zFar * getLinearDepth(depthMap, tex, zFarDivZNear); vec2 centerSlope = vec2(dFdx(depthCenter), dFdy(depthCenter)) / vec2(dFdx(tex.x), dFdy(tex.y)); - + +#if defined(USE_GAUSS) vec4 result = texture2D(imageMap, tex) * gauss[0]; float total = gauss[0]; +#else + vec4 result = texture2D(imageMap, tex); + float total = 1.0; +#endif int i, j; for (i = 0; i < 2; i++) { - for (j = 1; j < GAUSS_SIZE; j++) + for (j = 1; j < BLUR_SIZE; j++) { vec2 offset = direction * j; float depthSample = zFar * getLinearDepth(depthMap, tex + offset, zFarDivZNear); float depthExpected = depthCenter + dot(centerSlope, offset); if(abs(depthSample - depthExpected) < 5.0) { +#if defined(USE_GAUSS) result += texture2D(imageMap, tex + offset) * gauss[j]; total += gauss[j]; +#else + result += texture2D(imageMap, tex + offset); + total += 1.0; +#endif } } -- cgit From 7c858dafb7941be2ea6522a2a267252abf2d4fe7 Mon Sep 17 00:00:00 2001 From: SmileTheory Date: Mon, 7 Mar 2016 03:30:16 -0800 Subject: OpenGL2: Add r_glossType. --- src/renderergl2/glsl/lightall_fp.glsl | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'src/renderergl2/glsl') diff --git a/src/renderergl2/glsl/lightall_fp.glsl b/src/renderergl2/glsl/lightall_fp.glsl index 6a0a5155..8edbebda 100644 --- a/src/renderergl2/glsl/lightall_fp.glsl +++ b/src/renderergl2/glsl/lightall_fp.glsl @@ -332,19 +332,29 @@ void main() // diffuse rgb is base color // specular red is smoothness // specular green is metallicness - float roughness = 1.0 - specular.r; + float gloss = specular.r; specular.rgb = specular.g * diffuse.rgb + vec3(0.04 - 0.04 * specular.g); diffuse.rgb *= 1.0 - specular.g; #else // diffuse rgb is diffuse // specular rgb is specular reflectance at normal incidence // specular alpha is gloss - float roughness = exp2(-3.0 * specular.a); + float gloss = specular.a; // adjust diffuse by specular reflectance, to maintain energy conservation diffuse.rgb *= vec3(1.0) - specular.rgb; #endif + #if defined(GLOSS_IS_GLOSS) + float roughness = exp2(-3.0 * gloss); + #elif defined(GLOSS_IS_SMOOTHNESS) + float roughness = 1.0 - gloss; + #elif defined(GLOSS_IS_ROUGHNESS) + float roughness = gloss; + #elif defined(GLOSS_IS_SHININESS) + float roughness = pow(2.0 / (8190.0 * gloss + 2.0), 0.25); + #endif + reflectance = CalcDiffuse(diffuse.rgb, NH, EH, roughness); gl_FragColor.rgb = lightColor * reflectance * (attenuation * NL); -- cgit From c1ec25db8442cacf87df39d1e8e54b819b83a047 Mon Sep 17 00:00:00 2001 From: SmileTheory Date: Tue, 8 Mar 2016 18:30:51 -0800 Subject: OpenGL2: Speedup for SSAO & blur shaders, fix sunlight normals in lightall. --- src/renderergl2/glsl/depthblur_fp.glsl | 46 ++++++++++++++--------------- src/renderergl2/glsl/lightall_fp.glsl | 4 +-- src/renderergl2/glsl/ssao_fp.glsl | 54 +++++++++++++++++----------------- 3 files changed, 52 insertions(+), 52 deletions(-) (limited to 'src/renderergl2/glsl') diff --git a/src/renderergl2/glsl/depthblur_fp.glsl b/src/renderergl2/glsl/depthblur_fp.glsl index 15f7be27..60a261c5 100644 --- a/src/renderergl2/glsl/depthblur_fp.glsl +++ b/src/renderergl2/glsl/depthblur_fp.glsl @@ -11,22 +11,23 @@ float gauss[4] = float[4](0.40, 0.24, 0.054, 0.0044); float getLinearDepth(sampler2D depthMap, const vec2 tex, const float zFarDivZNear) { - float sampleZDivW = texture2D(depthMap, tex).r; - return 1.0 / mix(zFarDivZNear, 1.0, sampleZDivW); + float sampleZDivW = texture2D(depthMap, tex).r; + return 1.0 / mix(zFarDivZNear, 1.0, sampleZDivW); } -vec4 depthGaussian1D(sampler2D imageMap, sampler2D depthMap, vec2 tex, float zFarDivZNear, float zFar) +vec4 depthGaussian1D(sampler2D imageMap, sampler2D depthMap, vec2 tex, float zFarDivZNear, float zFar, vec2 scale) { - vec2 scale = u_ViewInfo.zw; + float depthCenter = getLinearDepth(depthMap, tex, zFarDivZNear); + //scale /= zFarDivZNear * depthCenter; + //int blurSteps = int(float(BLUR_SIZE) / (zFarDivZNear * depthCenter)); #if defined(USE_HORIZONTAL_BLUR) - vec2 direction = vec2(1.0, 0.0) * scale; + vec2 direction = vec2(scale.x, 0.0); #else // if defined(USE_VERTICAL_BLUR) - vec2 direction = vec2(0.0, 1.0) * scale; + vec2 direction = vec2(0.0, scale.y); #endif - - float depthCenter = zFar * getLinearDepth(depthMap, tex, zFarDivZNear); - vec2 centerSlope = vec2(dFdx(depthCenter), dFdy(depthCenter)) / vec2(dFdx(tex.x), dFdy(tex.y)); + + vec2 slope = vec2(dFdx(depthCenter), dFdy(depthCenter)) / vec2(dFdx(tex.x), dFdy(tex.y)); #if defined(USE_GAUSS) vec4 result = texture2D(imageMap, tex) * gauss[0]; @@ -36,33 +37,32 @@ vec4 depthGaussian1D(sampler2D imageMap, sampler2D depthMap, vec2 tex, float zFa float total = 1.0; #endif + float zLimit = 5.0 / zFar; int i, j; for (i = 0; i < 2; i++) { for (j = 1; j < BLUR_SIZE; j++) { vec2 offset = direction * j; - float depthSample = zFar * getLinearDepth(depthMap, tex + offset, zFarDivZNear); - float depthExpected = depthCenter + dot(centerSlope, offset); - if(abs(depthSample - depthExpected) < 5.0) - { + float depthSample = getLinearDepth(depthMap, tex + offset, zFarDivZNear); + float depthExpected = depthCenter + dot(slope, offset); + float useSample = float(abs(depthSample - depthExpected) < zLimit); #if defined(USE_GAUSS) - result += texture2D(imageMap, tex + offset) * gauss[j]; - total += gauss[j]; + result += texture2D(imageMap, tex + offset) * (gauss[j] * useSample); + total += gauss[j] * useSample; #else - result += texture2D(imageMap, tex + offset); - total += 1.0; + result += texture2D(imageMap, tex + offset) * useSample; + total += useSample; #endif - } } - + direction = -direction; - } - + } + return result / total; } void main() -{ - gl_FragColor = depthGaussian1D(u_ScreenImageMap, u_ScreenDepthMap, var_ScreenTex, u_ViewInfo.x, u_ViewInfo.y); +{ + gl_FragColor = depthGaussian1D(u_ScreenImageMap, u_ScreenDepthMap, var_ScreenTex, u_ViewInfo.x, u_ViewInfo.y, u_ViewInfo.zw); } diff --git a/src/renderergl2/glsl/lightall_fp.glsl b/src/renderergl2/glsl/lightall_fp.glsl index 8edbebda..f16298a3 100644 --- a/src/renderergl2/glsl/lightall_fp.glsl +++ b/src/renderergl2/glsl/lightall_fp.glsl @@ -292,7 +292,7 @@ void main() float shadowValue = texture2D(u_ShadowMap, shadowTex).r; // surfaces not facing the light are always shadowed - shadowValue *= clamp(dot(var_Normal.xyz, var_PrimaryLightDir.xyz), 0.0, 1.0); + shadowValue *= clamp(dot(N, var_PrimaryLightDir.xyz), 0.0, 1.0); #if defined(SHADOWMAP_MODULATE) lightColor *= shadowValue * (1.0 - u_PrimaryLightAmbient.r) + u_PrimaryLightAmbient.r; @@ -330,7 +330,7 @@ void main() #if defined(USE_PBR) // diffuse rgb is base color - // specular red is smoothness + // specular red is gloss // specular green is metallicness float gloss = specular.r; specular.rgb = specular.g * diffuse.rgb + vec3(0.04 - 0.04 * specular.g); diff --git a/src/renderergl2/glsl/ssao_fp.glsl b/src/renderergl2/glsl/ssao_fp.glsl index 6263284c..84f18cb7 100644 --- a/src/renderergl2/glsl/ssao_fp.glsl +++ b/src/renderergl2/glsl/ssao_fp.glsl @@ -1,6 +1,6 @@ uniform sampler2D u_ScreenDepthMap; -uniform vec4 u_ViewInfo; // zfar / znear, zfar +uniform vec4 u_ViewInfo; // zfar / znear, zfar, 1/width, 1/height varying vec2 var_ScreenTex; @@ -11,6 +11,7 @@ vec2(0.5784913, -0.002528916), vec2(0.192888, 0.4064181), vec2(-0.6335801, -0.5247476), vec2(-0.5579782, 0.7491854), vec2(0.7320465, 0.6317794) ); +#define NUM_SAMPLES 3 // Input: It uses texture coords as the random number seed. // Output: Random number: [0,1), that is between 0.0 and 0.999999... inclusive. @@ -39,48 +40,47 @@ mat2 randomRotation( const vec2 p ) float getLinearDepth(sampler2D depthMap, const vec2 tex, const float zFarDivZNear) { - float sampleZDivW = texture2D(depthMap, tex).r; - return 1.0 / mix(zFarDivZNear, 1.0, sampleZDivW); + float sampleZDivW = texture2D(depthMap, tex).r; + return 1.0 / mix(zFarDivZNear, 1.0, sampleZDivW); } -float ambientOcclusion(sampler2D depthMap, const vec2 tex, const float zFarDivZNear, const float zFar) +float ambientOcclusion(sampler2D depthMap, const vec2 tex, const float zFarDivZNear, const float zFar, const vec2 scale) { float result = 0; - float sampleZ = zFar * getLinearDepth(depthMap, tex, zFarDivZNear); + float sampleZ = getLinearDepth(depthMap, tex, zFarDivZNear); + float scaleZ = zFarDivZNear * sampleZ; - vec2 expectedSlope = vec2(dFdx(sampleZ), dFdy(sampleZ)) / vec2(dFdx(tex.x), dFdy(tex.y)); - - if (length(expectedSlope) > 5000.0) + vec2 slope = vec2(dFdx(sampleZ), dFdy(sampleZ)) / vec2(dFdx(tex.x), dFdy(tex.y)); + + if (length(slope) * zFar > 5000.0) return 1.0; - - vec2 offsetScale = vec2(3.0 / sampleZ); - + + vec2 offsetScale = vec2(scale * 1024.0 / scaleZ); + mat2 rmat = randomRotation(tex); - + + float invZFar = 1.0 / zFar; + float zLimit = 20.0 * invZFar; int i; - for (i = 0; i < 3; i++) + for (i = 0; i < NUM_SAMPLES; i++) { vec2 offset = rmat * poissonDisc[i] * offsetScale; - float sampleZ2 = zFar * getLinearDepth(depthMap, tex + offset, zFarDivZNear); - - if (abs(sampleZ - sampleZ2) > 20.0) - result += 1.0; - else - { - float expectedZ = sampleZ + dot(expectedSlope, offset); - result += step(expectedZ - 1.0, sampleZ2); - } + float sampleDiff = getLinearDepth(depthMap, tex + offset, zFarDivZNear) - sampleZ; + + bool s1 = abs(sampleDiff) > zLimit; + bool s2 = sampleDiff + invZFar > dot(slope, offset); + result += float(s1 || s2); } - - result *= 0.33333; - + + result *= 1.0 / float(NUM_SAMPLES); + return result; } void main() { - float result = ambientOcclusion(u_ScreenDepthMap, var_ScreenTex, u_ViewInfo.x, u_ViewInfo.y); - + float result = ambientOcclusion(u_ScreenDepthMap, var_ScreenTex, u_ViewInfo.x, u_ViewInfo.y, u_ViewInfo.zw); + gl_FragColor = vec4(vec3(result), 1.0); } -- cgit From 46cb9a5112fecbdf152bccf692d89bfca564e987 Mon Sep 17 00:00:00 2001 From: SmileTheory Date: Thu, 10 Mar 2016 03:44:21 -0800 Subject: OpenGL2: Fixes to depth blur and ssao. --- src/renderergl2/glsl/depthblur_fp.glsl | 11 +++++++---- src/renderergl2/glsl/ssao_fp.glsl | 5 +++-- 2 files changed, 10 insertions(+), 6 deletions(-) (limited to 'src/renderergl2/glsl') diff --git a/src/renderergl2/glsl/depthblur_fp.glsl b/src/renderergl2/glsl/depthblur_fp.glsl index 60a261c5..a5b264fd 100644 --- a/src/renderergl2/glsl/depthblur_fp.glsl +++ b/src/renderergl2/glsl/depthblur_fp.glsl @@ -4,6 +4,7 @@ uniform sampler2D u_ScreenDepthMap; uniform vec4 u_ViewInfo; // zfar / znear, zfar, 1/width, 1/height varying vec2 var_ScreenTex; +//float gauss[8] = float[8](0.17, 0.17, 0.16, 0.14, 0.12, 0.1, 0.08, 0.06); //float gauss[5] = float[5](0.30, 0.23, 0.097, 0.024, 0.0033); float gauss[4] = float[4](0.40, 0.24, 0.054, 0.0044); //float gauss[3] = float[3](0.60, 0.19, 0.0066); @@ -11,15 +12,17 @@ float gauss[4] = float[4](0.40, 0.24, 0.054, 0.0044); float getLinearDepth(sampler2D depthMap, const vec2 tex, const float zFarDivZNear) { - float sampleZDivW = texture2D(depthMap, tex).r; + // depth is upside down? + float sampleZDivW = texture2D(depthMap, vec2(tex.x, 1.0 - tex.y)).r; return 1.0 / mix(zFarDivZNear, 1.0, sampleZDivW); } vec4 depthGaussian1D(sampler2D imageMap, sampler2D depthMap, vec2 tex, float zFarDivZNear, float zFar, vec2 scale) { float depthCenter = getLinearDepth(depthMap, tex, zFarDivZNear); - //scale /= zFarDivZNear * depthCenter; - //int blurSteps = int(float(BLUR_SIZE) / (zFarDivZNear * depthCenter)); + + // enable for less blurring for farther objects + //scale /= min(zFarDivZNear * depthCenter / 32.0, 2.0); #if defined(USE_HORIZONTAL_BLUR) vec2 direction = vec2(scale.x, 0.0); @@ -64,5 +67,5 @@ vec4 depthGaussian1D(sampler2D imageMap, sampler2D depthMap, vec2 tex, float zFa void main() { - gl_FragColor = depthGaussian1D(u_ScreenImageMap, u_ScreenDepthMap, var_ScreenTex, u_ViewInfo.x, u_ViewInfo.y, u_ViewInfo.zw); + gl_FragColor = depthGaussian1D(u_ScreenImageMap, u_ScreenDepthMap, var_ScreenTex, u_ViewInfo.x, u_ViewInfo.y, u_ViewInfo.wz); } diff --git a/src/renderergl2/glsl/ssao_fp.glsl b/src/renderergl2/glsl/ssao_fp.glsl index 84f18cb7..1714c2be 100644 --- a/src/renderergl2/glsl/ssao_fp.glsl +++ b/src/renderergl2/glsl/ssao_fp.glsl @@ -40,7 +40,8 @@ mat2 randomRotation( const vec2 p ) float getLinearDepth(sampler2D depthMap, const vec2 tex, const float zFarDivZNear) { - float sampleZDivW = texture2D(depthMap, tex).r; + // depth is upside down? + float sampleZDivW = texture2D(depthMap, vec2(tex.x, 1.0 - tex.y)).r; return 1.0 / mix(zFarDivZNear, 1.0, sampleZDivW); } @@ -80,7 +81,7 @@ float ambientOcclusion(sampler2D depthMap, const vec2 tex, const float zFarDivZN void main() { - float result = ambientOcclusion(u_ScreenDepthMap, var_ScreenTex, u_ViewInfo.x, u_ViewInfo.y, u_ViewInfo.zw); + float result = ambientOcclusion(u_ScreenDepthMap, var_ScreenTex, u_ViewInfo.x, u_ViewInfo.y, u_ViewInfo.wz); gl_FragColor = vec4(vec3(result), 1.0); } -- cgit From e4c921a3b24d7631af8d119a5175321b6eb1c1f7 Mon Sep 17 00:00:00 2001 From: SmileTheory Date: Fri, 11 Mar 2016 04:37:50 -0800 Subject: OpenGL2: More ssao/depth blur improvements. --- src/renderergl2/glsl/depthblur_fp.glsl | 18 +++++++++++------- src/renderergl2/glsl/depthblur_vp.glsl | 5 ++++- src/renderergl2/glsl/ssao_fp.glsl | 3 +-- 3 files changed, 16 insertions(+), 10 deletions(-) (limited to 'src/renderergl2/glsl') diff --git a/src/renderergl2/glsl/depthblur_fp.glsl b/src/renderergl2/glsl/depthblur_fp.glsl index a5b264fd..9685f6d7 100644 --- a/src/renderergl2/glsl/depthblur_fp.glsl +++ b/src/renderergl2/glsl/depthblur_fp.glsl @@ -9,11 +9,11 @@ varying vec2 var_ScreenTex; float gauss[4] = float[4](0.40, 0.24, 0.054, 0.0044); //float gauss[3] = float[3](0.60, 0.19, 0.0066); #define BLUR_SIZE 4 +//#define USE_GAUSS float getLinearDepth(sampler2D depthMap, const vec2 tex, const float zFarDivZNear) { - // depth is upside down? - float sampleZDivW = texture2D(depthMap, vec2(tex.x, 1.0 - tex.y)).r; + float sampleZDivW = texture2D(depthMap, tex).r; return 1.0 / mix(zFarDivZNear, 1.0, sampleZDivW); } @@ -22,12 +22,14 @@ vec4 depthGaussian1D(sampler2D imageMap, sampler2D depthMap, vec2 tex, float zFa float depthCenter = getLinearDepth(depthMap, tex, zFarDivZNear); // enable for less blurring for farther objects - //scale /= min(zFarDivZNear * depthCenter / 32.0, 2.0); + scale /= clamp(zFarDivZNear * depthCenter / 32.0, 1.0, 2.0); #if defined(USE_HORIZONTAL_BLUR) - vec2 direction = vec2(scale.x, 0.0); + vec2 direction = vec2(scale.x * 2.0, 0.0); + vec2 nudge = vec2(0.0, scale.y * 0.5); #else // if defined(USE_VERTICAL_BLUR) - vec2 direction = vec2(0.0, scale.y); + vec2 direction = vec2(0.0, scale.y * 2.0); + vec2 nudge = vec2(scale.x * 0.5, 0.0); #endif vec2 slope = vec2(dFdx(depthCenter), dFdy(depthCenter)) / vec2(dFdx(tex.x), dFdy(tex.y)); @@ -46,7 +48,7 @@ vec4 depthGaussian1D(sampler2D imageMap, sampler2D depthMap, vec2 tex, float zFa { for (j = 1; j < BLUR_SIZE; j++) { - vec2 offset = direction * j; + vec2 offset = direction * (float(j) - 0.25) + nudge; float depthSample = getLinearDepth(depthMap, tex + offset, zFarDivZNear); float depthExpected = depthCenter + dot(slope, offset); float useSample = float(abs(depthSample - depthExpected) < zLimit); @@ -57,9 +59,11 @@ vec4 depthGaussian1D(sampler2D imageMap, sampler2D depthMap, vec2 tex, float zFa result += texture2D(imageMap, tex + offset) * useSample; total += useSample; #endif + nudge = -nudge; } direction = -direction; + nudge = -nudge; } return result / total; @@ -67,5 +71,5 @@ vec4 depthGaussian1D(sampler2D imageMap, sampler2D depthMap, vec2 tex, float zFa void main() { - gl_FragColor = depthGaussian1D(u_ScreenImageMap, u_ScreenDepthMap, var_ScreenTex, u_ViewInfo.x, u_ViewInfo.y, u_ViewInfo.wz); + gl_FragColor = depthGaussian1D(u_ScreenImageMap, u_ScreenDepthMap, var_ScreenTex, u_ViewInfo.x, u_ViewInfo.y, u_ViewInfo.zw); } diff --git a/src/renderergl2/glsl/depthblur_vp.glsl b/src/renderergl2/glsl/depthblur_vp.glsl index 9c46a79f..ba0b6c56 100644 --- a/src/renderergl2/glsl/depthblur_vp.glsl +++ b/src/renderergl2/glsl/depthblur_vp.glsl @@ -1,12 +1,15 @@ attribute vec4 attr_Position; attribute vec4 attr_TexCoord0; +uniform vec4 u_ViewInfo; // zfar / znear, zfar, 1/width, 1/height + varying vec2 var_ScreenTex; void main() { gl_Position = attr_Position; - var_ScreenTex = attr_TexCoord0.xy; + var_ScreenTex = (floor(attr_TexCoord0.xy * (1.0 / u_ViewInfo.zw - vec2(1.0))) + vec2(0.5)) * u_ViewInfo.zw; + //vec2 screenCoords = gl_Position.xy / gl_Position.w; //var_ScreenTex = screenCoords * 0.5 + 0.5; } diff --git a/src/renderergl2/glsl/ssao_fp.glsl b/src/renderergl2/glsl/ssao_fp.glsl index 1714c2be..93f61859 100644 --- a/src/renderergl2/glsl/ssao_fp.glsl +++ b/src/renderergl2/glsl/ssao_fp.glsl @@ -40,8 +40,7 @@ mat2 randomRotation( const vec2 p ) float getLinearDepth(sampler2D depthMap, const vec2 tex, const float zFarDivZNear) { - // depth is upside down? - float sampleZDivW = texture2D(depthMap, vec2(tex.x, 1.0 - tex.y)).r; + float sampleZDivW = texture2D(depthMap, tex).r; return 1.0 / mix(zFarDivZNear, 1.0, sampleZDivW); } -- cgit From b74cc1c42c5bfb2a0a17fc3c083a5a488c159d0d Mon Sep 17 00:00:00 2001 From: SmileTheory Date: Tue, 15 Mar 2016 05:20:25 -0700 Subject: OpenGL2: Fix horribly broken metallic shader. --- src/renderergl2/glsl/lightall_fp.glsl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src/renderergl2/glsl') diff --git a/src/renderergl2/glsl/lightall_fp.glsl b/src/renderergl2/glsl/lightall_fp.glsl index f16298a3..64cae3fa 100644 --- a/src/renderergl2/glsl/lightall_fp.glsl +++ b/src/renderergl2/glsl/lightall_fp.glsl @@ -333,8 +333,9 @@ void main() // specular red is gloss // specular green is metallicness float gloss = specular.r; - specular.rgb = specular.g * diffuse.rgb + vec3(0.04 - 0.04 * specular.g); - diffuse.rgb *= 1.0 - specular.g; + float metal = specular.g; + specular.rgb = metal * diffuse.rgb + vec3(0.04 - 0.04 * metal); + diffuse.rgb *= 1.0 - metal; #else // diffuse rgb is diffuse // specular rgb is specular reflectance at normal incidence -- cgit From e1c7b8c9bd19f0e4d9b763d1f30c175e49c5397d Mon Sep 17 00:00:00 2001 From: SmileTheory Date: Tue, 5 Apr 2016 02:37:05 -0700 Subject: OpenGL2: Some FBO cleanup, and add non-depth blur to blur shader. --- src/renderergl2/glsl/depthblur_fp.glsl | 17 ++++++++++++----- src/renderergl2/glsl/depthblur_vp.glsl | 3 ++- 2 files changed, 14 insertions(+), 6 deletions(-) (limited to 'src/renderergl2/glsl') diff --git a/src/renderergl2/glsl/depthblur_fp.glsl b/src/renderergl2/glsl/depthblur_fp.glsl index 9685f6d7..d71b3487 100644 --- a/src/renderergl2/glsl/depthblur_fp.glsl +++ b/src/renderergl2/glsl/depthblur_fp.glsl @@ -9,7 +9,10 @@ varying vec2 var_ScreenTex; float gauss[4] = float[4](0.40, 0.24, 0.054, 0.0044); //float gauss[3] = float[3](0.60, 0.19, 0.0066); #define BLUR_SIZE 4 + +#if !defined(USE_DEPTH) //#define USE_GAUSS +#endif float getLinearDepth(sampler2D depthMap, const vec2 tex, const float zFarDivZNear) { @@ -19,21 +22,21 @@ float getLinearDepth(sampler2D depthMap, const vec2 tex, const float zFarDivZNea vec4 depthGaussian1D(sampler2D imageMap, sampler2D depthMap, vec2 tex, float zFarDivZNear, float zFar, vec2 scale) { - float depthCenter = getLinearDepth(depthMap, tex, zFarDivZNear); - // enable for less blurring for farther objects +#if defined(USE_DEPTH) + float depthCenter = getLinearDepth(depthMap, tex, zFarDivZNear); + vec2 slope = vec2(dFdx(depthCenter), dFdy(depthCenter)) / vec2(dFdx(tex.x), dFdy(tex.y)); scale /= clamp(zFarDivZNear * depthCenter / 32.0, 1.0, 2.0); +#endif #if defined(USE_HORIZONTAL_BLUR) vec2 direction = vec2(scale.x * 2.0, 0.0); vec2 nudge = vec2(0.0, scale.y * 0.5); #else // if defined(USE_VERTICAL_BLUR) vec2 direction = vec2(0.0, scale.y * 2.0); - vec2 nudge = vec2(scale.x * 0.5, 0.0); + vec2 nudge = vec2(-scale.x * 0.5, 0.0); #endif - vec2 slope = vec2(dFdx(depthCenter), dFdy(depthCenter)) / vec2(dFdx(tex.x), dFdy(tex.y)); - #if defined(USE_GAUSS) vec4 result = texture2D(imageMap, tex) * gauss[0]; float total = gauss[0]; @@ -49,9 +52,13 @@ vec4 depthGaussian1D(sampler2D imageMap, sampler2D depthMap, vec2 tex, float zFa for (j = 1; j < BLUR_SIZE; j++) { vec2 offset = direction * (float(j) - 0.25) + nudge; +#if defined(USE_DEPTH) float depthSample = getLinearDepth(depthMap, tex + offset, zFarDivZNear); float depthExpected = depthCenter + dot(slope, offset); float useSample = float(abs(depthSample - depthExpected) < zLimit); +#else + float useSample = 1.0; +#endif #if defined(USE_GAUSS) result += texture2D(imageMap, tex + offset) * (gauss[j] * useSample); total += gauss[j] * useSample; diff --git a/src/renderergl2/glsl/depthblur_vp.glsl b/src/renderergl2/glsl/depthblur_vp.glsl index ba0b6c56..9c47660c 100644 --- a/src/renderergl2/glsl/depthblur_vp.glsl +++ b/src/renderergl2/glsl/depthblur_vp.glsl @@ -8,7 +8,8 @@ varying vec2 var_ScreenTex; void main() { gl_Position = attr_Position; - var_ScreenTex = (floor(attr_TexCoord0.xy * (1.0 / u_ViewInfo.zw - vec2(1.0))) + vec2(0.5)) * u_ViewInfo.zw; + vec2 wh = vec2(1.0) / u_ViewInfo.zw - vec2(1.0); + var_ScreenTex = (floor(attr_TexCoord0.xy * wh) + vec2(0.5)) * u_ViewInfo.zw; //vec2 screenCoords = gl_Position.xy / gl_Position.w; //var_ScreenTex = screenCoords * 0.5 + 0.5; -- cgit