[iortcw] 54/497: All: Rend2: Minor GLSL shader improvements

2017-09-08 Thread Simon McVittie
This is an automated email from the git hooks/post-receive script.

smcv pushed a commit to annotated tag 1.42d
in repository iortcw.

commit 588b86f4d1ba2285fd1bfef029188ace18d1df38
Author: m4n4t4...@gmail.com 

Date:   Tue Mar 4 04:55:13 2014 +

All: Rend2: Minor GLSL shader improvements
---
 MP/code/rend2/glsl/lightall_fp.glsl   | 18 --
 MP/code/rend2/glsl/shadowmask_fp.glsl |  9 +++--
 SP/code/rend2/glsl/lightall_fp.glsl   | 18 --
 SP/code/rend2/glsl/shadowmask_fp.glsl |  9 +++--
 4 files changed, 46 insertions(+), 8 deletions(-)

diff --git a/MP/code/rend2/glsl/lightall_fp.glsl 
b/MP/code/rend2/glsl/lightall_fp.glsl
index b983d6f..871c527 100644
--- a/MP/code/rend2/glsl/lightall_fp.glsl
+++ b/MP/code/rend2/glsl/lightall_fp.glsl
@@ -167,7 +167,7 @@ vec3 EnvironmentBRDF(float gloss, float NE, vec3 specular)
return clamp( a0 + specular * ( a1 - a0 ), 0.0, 1.0 );
   #elif 0
// from http://seblagarde.wordpress.com/2011/08/17/hello-world/
-   return mix(specular.rgb, max(specular.rgb, vec3(gloss)), 
CalcFresnel(NE));
+   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));
@@ -379,7 +379,7 @@ void main()
N.xy = texture2D(u_NormalMap, texCoords).rg - vec2(0.5);
 #endif
N.xy *= u_EnableTextures.x;
-   N.z = sqrt((0.25 - N.x * N.x) - N.y * N.y);
+   N.z = sqrt(clamp((0.25 - N.x * N.x) - N.y * N.y, 0.0, 1.0));
N = tangentToWorld * N;
   #else
N = var_Normal.xyz;
@@ -473,7 +473,21 @@ void main()
   #endif
 
gl_FragColor.rgb  = lightColor   * reflectance * 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);
diff --git a/MP/code/rend2/glsl/shadowmask_fp.glsl 
b/MP/code/rend2/glsl/shadowmask_fp.glsl
index 4bac5cc..b489fef 100644
--- a/MP/code/rend2/glsl/shadowmask_fp.glsl
+++ b/MP/code/rend2/glsl/shadowmask_fp.glsl
@@ -18,6 +18,10 @@ uniform vec4   u_ViewInfo; // zfar / znear, zfar
 varying vec2   var_DepthTex;
 varying vec3   var_ViewDir;
 
+// depth is GL_DEPTH_COMPONENT24
+// so the maximum error is 1.0 / 2^24
+#define DEPTH_MAX_ERROR 0.00059604644775390625
+
 // Input: It uses texture coords as the random number seed.
 // Output: Random number: [0,1), that is between 0.0 and 0.99... inclusive.
 // Author: Michael Pohoreski
@@ -39,7 +43,7 @@ float PCF(const sampler2D shadowmap, const vec2 st, const 
float dist)
 {
float mult;
float scale = 2.0 / r_shadowMapSize;
-   
+
 #if defined(USE_SHADOW_FILTER)
float r = random(var_DepthTex.xy);
float sinr = sin(r) * scale;
@@ -71,6 +75,7 @@ float PCF(const sampler2D shadowmap, const vec2 st, const 
float dist)
 float getLinearDepth(sampler2D depthMap, vec2 tex, float zFarDivZNear)
 {
float sampleZDivW = texture2D(depthMap, tex).r;
+   sampleZDivW -= DEPTH_MAX_ERROR;
return 1.0 / mix(zFarDivZNear, 1.0, sampleZDivW);
 }
 
@@ -81,7 +86,7 @@ void main()
float depth = getLinearDepth(u_ScreenDepthMap, var_DepthTex, 
u_ViewInfo.x);
float sampleZ = u_ViewInfo.y * depth;
 
-   vec4 biasPos = vec4(u_ViewOrigin + var_ViewDir * depth * 0.99, 1.0);
+   vec4 biasPos = vec4(u_ViewOrigin + var_ViewDir * (depth - 0.5 / 
u_ViewInfo.x), 1.0);

vec4 shadowpos = u_ShadowMvp * biasPos;

diff --git a/SP/code/rend2/glsl/lightall_fp.glsl 
b/SP/code/rend2/glsl/lightall_fp.glsl
index b983d6f..871c527 100644
--- a/SP/code/rend2/glsl/lightall_fp.glsl
+++ b/SP/code/rend2/glsl/lightall_fp.glsl
@@ -167,7 +167,7 @@ vec3 EnvironmentBRDF(float gloss, float NE, vec3 specular)
return clamp( a0 + specular * ( a1 - a0 ), 0.0, 1.0 );
   #elif 0
// from http://seblagarde.wordpress.com/2011/08/17/hello-world/
-   return mix(specular.rgb, max(specular.rgb, vec3(gloss)), 
CalcFresnel(NE));
+   return specular + 

[iortcw] 54/497: All: Rend2: Minor GLSL shader improvements

2016-09-21 Thread Simon McVittie
This is an automated email from the git hooks/post-receive script.

smcv pushed a commit to annotated tag 1.42d
in repository iortcw.

commit 588b86f4d1ba2285fd1bfef029188ace18d1df38
Author: m4n4t4...@gmail.com 

Date:   Tue Mar 4 04:55:13 2014 +

All: Rend2: Minor GLSL shader improvements
---
 MP/code/rend2/glsl/lightall_fp.glsl   | 18 --
 MP/code/rend2/glsl/shadowmask_fp.glsl |  9 +++--
 SP/code/rend2/glsl/lightall_fp.glsl   | 18 --
 SP/code/rend2/glsl/shadowmask_fp.glsl |  9 +++--
 4 files changed, 46 insertions(+), 8 deletions(-)

diff --git a/MP/code/rend2/glsl/lightall_fp.glsl 
b/MP/code/rend2/glsl/lightall_fp.glsl
index b983d6f..871c527 100644
--- a/MP/code/rend2/glsl/lightall_fp.glsl
+++ b/MP/code/rend2/glsl/lightall_fp.glsl
@@ -167,7 +167,7 @@ vec3 EnvironmentBRDF(float gloss, float NE, vec3 specular)
return clamp( a0 + specular * ( a1 - a0 ), 0.0, 1.0 );
   #elif 0
// from http://seblagarde.wordpress.com/2011/08/17/hello-world/
-   return mix(specular.rgb, max(specular.rgb, vec3(gloss)), 
CalcFresnel(NE));
+   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));
@@ -379,7 +379,7 @@ void main()
N.xy = texture2D(u_NormalMap, texCoords).rg - vec2(0.5);
 #endif
N.xy *= u_EnableTextures.x;
-   N.z = sqrt((0.25 - N.x * N.x) - N.y * N.y);
+   N.z = sqrt(clamp((0.25 - N.x * N.x) - N.y * N.y, 0.0, 1.0));
N = tangentToWorld * N;
   #else
N = var_Normal.xyz;
@@ -473,7 +473,21 @@ void main()
   #endif
 
gl_FragColor.rgb  = lightColor   * reflectance * 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);
diff --git a/MP/code/rend2/glsl/shadowmask_fp.glsl 
b/MP/code/rend2/glsl/shadowmask_fp.glsl
index 4bac5cc..b489fef 100644
--- a/MP/code/rend2/glsl/shadowmask_fp.glsl
+++ b/MP/code/rend2/glsl/shadowmask_fp.glsl
@@ -18,6 +18,10 @@ uniform vec4   u_ViewInfo; // zfar / znear, zfar
 varying vec2   var_DepthTex;
 varying vec3   var_ViewDir;
 
+// depth is GL_DEPTH_COMPONENT24
+// so the maximum error is 1.0 / 2^24
+#define DEPTH_MAX_ERROR 0.00059604644775390625
+
 // Input: It uses texture coords as the random number seed.
 // Output: Random number: [0,1), that is between 0.0 and 0.99... inclusive.
 // Author: Michael Pohoreski
@@ -39,7 +43,7 @@ float PCF(const sampler2D shadowmap, const vec2 st, const 
float dist)
 {
float mult;
float scale = 2.0 / r_shadowMapSize;
-   
+
 #if defined(USE_SHADOW_FILTER)
float r = random(var_DepthTex.xy);
float sinr = sin(r) * scale;
@@ -71,6 +75,7 @@ float PCF(const sampler2D shadowmap, const vec2 st, const 
float dist)
 float getLinearDepth(sampler2D depthMap, vec2 tex, float zFarDivZNear)
 {
float sampleZDivW = texture2D(depthMap, tex).r;
+   sampleZDivW -= DEPTH_MAX_ERROR;
return 1.0 / mix(zFarDivZNear, 1.0, sampleZDivW);
 }
 
@@ -81,7 +86,7 @@ void main()
float depth = getLinearDepth(u_ScreenDepthMap, var_DepthTex, 
u_ViewInfo.x);
float sampleZ = u_ViewInfo.y * depth;
 
-   vec4 biasPos = vec4(u_ViewOrigin + var_ViewDir * depth * 0.99, 1.0);
+   vec4 biasPos = vec4(u_ViewOrigin + var_ViewDir * (depth - 0.5 / 
u_ViewInfo.x), 1.0);

vec4 shadowpos = u_ShadowMvp * biasPos;

diff --git a/SP/code/rend2/glsl/lightall_fp.glsl 
b/SP/code/rend2/glsl/lightall_fp.glsl
index b983d6f..871c527 100644
--- a/SP/code/rend2/glsl/lightall_fp.glsl
+++ b/SP/code/rend2/glsl/lightall_fp.glsl
@@ -167,7 +167,7 @@ vec3 EnvironmentBRDF(float gloss, float NE, vec3 specular)
return clamp( a0 + specular * ( a1 - a0 ), 0.0, 1.0 );
   #elif 0
// from http://seblagarde.wordpress.com/2011/08/17/hello-world/
-   return mix(specular.rgb, max(specular.rgb, vec3(gloss)), 
CalcFresnel(NE));
+   return specular +