Bladeren bron

Optimized deferred shadowed directional light shader in both HLSL & GLSL.

Lasse Öörni 14 jaren geleden
bovenliggende
commit
a4aed67509

+ 1 - 2
SourceAssets/GLSLShaders/LightVolume.frag

@@ -62,8 +62,7 @@ void main()
 
 
     #ifdef SHADOW
     #ifdef SHADOW
         #if defined(DIRLIGHT)
         #if defined(DIRLIGHT)
-            mat4 shadowMatrix = GetDirShadowMatrix(depth, cLightMatricesPS);
-            vec4 shadowPos = shadowMatrix * projWorldPos;
+            vec4 shadowPos = GetDirShadowPosDeferred(cLightMatricesPS, projWorldPos, depth);
             diff *= min(GetShadow(shadowPos) + GetShadowFade(depth), 1.0);
             diff *= min(GetShadow(shadowPos) + GetShadowFade(depth), 1.0);
         #elif defined(SPOTLIGHT)
         #elif defined(SPOTLIGHT)
             vec4 shadowPos = cLightMatricesPS[1] * projWorldPos;
             vec4 shadowPos = cLightMatricesPS[1] * projWorldPos;

+ 5 - 5
SourceAssets/GLSLShaders/Lighting.frag

@@ -94,16 +94,16 @@ vec4 GetDirShadowPos(const vec4 shadowPos[4], float depth)
         return shadowPos[3];
         return shadowPos[3];
 }
 }
 
 
-mat4 GetDirShadowMatrix(float depth, const mat4 matrices[4])
+vec4 GetDirShadowPosDeferred(const mat4 shadowMatrix[4], vec4 projWorldPos, float depth)
 {
 {
     if (depth < cShadowSplits.x)
     if (depth < cShadowSplits.x)
-        return matrices[0];
+        return shadowMatrix[0] * projWorldPos;
     else if (depth < cShadowSplits.y)
     else if (depth < cShadowSplits.y)
-        return matrices[1];
+        return shadowMatrix[1] * projWorldPos;
     else if (depth < cShadowSplits.z)
     else if (depth < cShadowSplits.z)
-        return matrices[2];
+        return shadowMatrix[2] * projWorldPos;
     else
     else
-        return matrices[3];
+        return shadowMatrix[3] * projWorldPos;
 }
 }
 #endif
 #endif
 
 

+ 1 - 2
SourceAssets/HLSLShaders/LightVolume.hlsl

@@ -92,8 +92,7 @@ void PS(
 
 
     #ifdef SHADOW
     #ifdef SHADOW
         #if defined(DIRLIGHT)
         #if defined(DIRLIGHT)
-            float4x4 shadowMatrix = GetDirShadowMatrix(depth, cLightMatricesPS);
-            float4 shadowPos = mul(projWorldPos, shadowMatrix);
+            float4 shadowPos = GetDirShadowPosDeferred(cLightMatricesPS, projWorldPos, depth);
             diff *= saturate(GetShadow(shadowPos) + GetShadowFade(depth));
             diff *= saturate(GetShadow(shadowPos) + GetShadowFade(depth));
         #elif defined(SPOTLIGHT)
         #elif defined(SPOTLIGHT)
             float4 shadowPos = mul(projWorldPos, cLightMatricesPS[1]);
             float4 shadowPos = mul(projWorldPos, cLightMatricesPS[1]);

+ 9 - 9
SourceAssets/HLSLShaders/Lighting.hlsl

@@ -165,26 +165,26 @@ float4 GetDirShadowPos(const float4 iShadowPos[4], float depth)
 }
 }
 
 
 #ifdef SM3
 #ifdef SM3
-float4x4 GetDirShadowMatrix(float depth, const float4x4 matrices[4])
+float4 GetDirShadowPosDeferred(const float4x4 matrices[4], float4 projWorldPos, float depth)
 {
 {
     if (depth < cShadowSplits.x)
     if (depth < cShadowSplits.x)
-        return matrices[0];
+        return mul(projWorldPos, matrices[0]);
     else if (depth < cShadowSplits.y)
     else if (depth < cShadowSplits.y)
-        return matrices[1];
+        return mul(projWorldPos, matrices[1]);
     else if (depth < cShadowSplits.z)
     else if (depth < cShadowSplits.z)
-        return matrices[2];
+        return mul(projWorldPos, matrices[2]);
     else
     else
-        return matrices[3];
+        return mul(projWorldPos, matrices[3]);
 }
 }
 #else
 #else
-float4x4 GetDirShadowMatrix(float depth, const float4x4 matrices[3])
+float4 GetDirShadowPosDeferred(const float4x4 matrices[3], float4 projWorldPos, float depth)
 {
 {
     if (depth < cShadowSplits.x)
     if (depth < cShadowSplits.x)
-        return matrices[0];
+        return mul(projWorldPos, matrices[0]);
     else if (depth < cShadowSplits.y)
     else if (depth < cShadowSplits.y)
-        return matrices[1];
+        return mul(projWorldPos, matrices[1]);
     else
     else
-        return matrices[2];
+        return mul(projWorldPos, matrices[2]);
 }
 }
 #endif
 #endif
 #endif
 #endif