Browse Source

Shader code cleanup.

Lasse Öörni 14 years ago
parent
commit
d0cdfd6f1e

+ 1 - 1
Engine/Graphics/OpenGL/OGLShaderProgram.cpp

@@ -154,7 +154,7 @@ bool ShaderProgram::Link()
     }
     }
     
     
     // Query the vertex attribute bindings
     // Query the vertex attribute bindings
-    attributeLocations_[0] = glGetAttribLocation(object_, "iPosition");
+    attributeLocations_[0] = glGetAttribLocation(object_, "iPos");
     attributeLocations_[1] = glGetAttribLocation(object_, "iNormal");
     attributeLocations_[1] = glGetAttribLocation(object_, "iNormal");
     attributeLocations_[2] = glGetAttribLocation(object_, "iColor");
     attributeLocations_[2] = glGetAttribLocation(object_, "iColor");
     attributeLocations_[3] = glGetAttribLocation(object_, "iTexCoord");
     attributeLocations_[3] = glGetAttribLocation(object_, "iTexCoord");

+ 4 - 1
SourceAssets/GLSLShaders/Basic.vert

@@ -10,7 +10,10 @@
 
 
 void main()
 void main()
 {
 {
-    GetPosition(iPosition, gl_Position);
+    mat4 modelMatrix = iModelMatrix;
+    vec3 worldPos = GetWorldPos(modelMatrix);
+    gl_Position = GetClipPos(worldPos);
+    
     #ifdef DIFFMAP
     #ifdef DIFFMAP
         vTexCoord = iTexCoord;
         vTexCoord = iTexCoord;
     #endif
     #endif

+ 1 - 9
SourceAssets/GLSLShaders/Forward.frag

@@ -69,15 +69,7 @@ void main()
 
 
         #ifdef SHADOW
         #ifdef SHADOW
             #if defined(DIRLIGHT)
             #if defined(DIRLIGHT)
-                vec4 shadowPos;
-                if (vLightVec.w < cShadowSplits.x)
-                    shadowPos = vShadowPos[0];
-                else if (vLightVec.w < cShadowSplits.y)
-                    shadowPos = vShadowPos[1];
-                else if (vLightVec.w < cShadowSplits.z)
-                    shadowPos = vShadowPos[2];
-                else
-                    shadowPos = vShadowPos[3];
+                vec4 shadowPos = GetDirShadowPos(vShadowPos, vLightVec.w);
                 diff *= min(GetShadow(shadowPos) + GetShadowFade(vLightVec.w), 1.0);
                 diff *= min(GetShadow(shadowPos) + GetShadowFade(vLightVec.w), 1.0);
             #elif defined(SPOTLIGHT)
             #elif defined(SPOTLIGHT)
                 diff *= GetShadow(vShadowPos);
                 diff *= GetShadow(vShadowPos);

+ 19 - 40
SourceAssets/GLSLShaders/Forward.vert

@@ -32,7 +32,10 @@ varying vec4 vLightVec;
 
 
 void main()
 void main()
 {
 {
-    vec4 pos;
+    mat4 modelMatrix = iModelMatrix;
+    vec3 worldPos = GetWorldPos(modelMatrix);
+    gl_Position = GetClipPos(worldPos);
+    vTexCoord = GetTexCoord(iTexCoord);
 
 
     #ifndef UNLIT
     #ifndef UNLIT
 
 
@@ -42,29 +45,14 @@ void main()
             vec3 vBitangent;
             vec3 vBitangent;
         #endif
         #endif
     
     
-        #if defined(SKINNED)
-            #ifndef NORMALMAP
-                pos = GetPositionNormalSkinned(iPosition, iNormal, iBlendWeights, iBlendIndices, gl_Position, vNormal);
-            #else
-                pos = GetPositionNormalTangentSkinned(iPosition, iNormal, iTangent, iBlendWeights, iBlendIndices, gl_Position, vNormal, vTangent);
-            #endif
-        #elif defined(BILLBOARD)
-            pos = GetPositionBillboard(iPosition, iTexCoord2, gl_Position);
-            vNormal = vec3(-cCameraRot[2][0], -cCameraRot[2][1], -cCameraRot[2][2]);
-        #else
-            #ifndef NORMALMAP
-                pos = GetPositionNormal(iPosition, iNormal, gl_Position, vNormal);
-            #else
-                pos = GetPositionNormalTangent(iPosition, iNormal, iTangent, gl_Position, vNormal, vTangent);
-            #endif
-        #endif
-    
-        vec3 worldPos = pos.xyz - cCameraPos;
-    
+        vNormal = GetWorldNormal(modelMatrix);
+        vec3 centeredWorldPos = worldPos - cCameraPos;
+        vec4 projWorldPos = vec4(worldPos, 1.0);
+
         #if defined(DIRLIGHT)
         #if defined(DIRLIGHT)
             vLightVec = vec4(cLightDir, GetDepth(gl_Position));
             vLightVec = vec4(cLightDir, GetDepth(gl_Position));
         #elif defined(LIGHT)
         #elif defined(LIGHT)
-            vLightVec = vec4((cLightPos - worldPos) * cLightAtten, GetDepth(gl_Position));
+            vLightVec = vec4((cLightPos - centeredWorldPos) * cLightAtten, GetDepth(gl_Position));
         #else
         #else
             vLightVec = vec4(0.0, 0.0, 0.0, GetDepth(gl_Position));
             vLightVec = vec4(0.0, 0.0, 0.0, GetDepth(gl_Position));
         #endif
         #endif
@@ -72,20 +60,20 @@ void main()
         #ifdef SHADOW
         #ifdef SHADOW
             // Shadow projection: transform from world space to shadow space
             // Shadow projection: transform from world space to shadow space
             #if defined(DIRLIGHT)
             #if defined(DIRLIGHT)
-                vShadowPos[0] = cShadowProj[0] * pos;
-                vShadowPos[1] = cShadowProj[1] * pos;
-                vShadowPos[2] = cShadowProj[2] * pos;
-                vShadowPos[3] = cShadowProj[3] * pos;
+                vShadowPos[0] = cShadowProj[0] * projWorldPos;
+                vShadowPos[1] = cShadowProj[1] * projWorldPos;
+                vShadowPos[2] = cShadowProj[2] * projWorldPos;
+                vShadowPos[3] = cShadowProj[3] * projWorldPos;
             #elif defined(SPOTLIGHT)
             #elif defined(SPOTLIGHT)
-                vShadowPos = cShadowProj[0] * pos;
+                vShadowPos = cShadowProj[0] * projWorldPos;
             #else
             #else
-                vWorldLightVec = worldPos - cLightPos;
+                vWorldLightVec = centeredWorldPos - cLightPos;
             #endif
             #endif
         #endif
         #endif
     
     
         #ifdef SPOTLIGHT
         #ifdef SPOTLIGHT
             // Spotlight projection: transform from world space to projector texture coordinates
             // Spotlight projection: transform from world space to projector texture coordinates
-            vSpotPos = cSpotProj * pos;
+            vSpotPos = cSpotProj * projWorldPos;
         #endif
         #endif
     
     
         #ifdef POINTLIGHT
         #ifdef POINTLIGHT
@@ -93,28 +81,21 @@ void main()
         #endif
         #endif
 
 
         #ifdef NORMALMAP
         #ifdef NORMALMAP
+            vTangent = GetWorldTangent(modelMatrix);
             vBitangent = cross(vTangent, vNormal) * iTangent.w;
             vBitangent = cross(vTangent, vNormal) * iTangent.w;
             mat3 tbn = mat3(vTangent, vBitangent, vNormal);
             mat3 tbn = mat3(vTangent, vBitangent, vNormal);
             #ifdef LIGHT
             #ifdef LIGHT
                 vLightVec.xyz = vLightVec.xyz * tbn;
                 vLightVec.xyz = vLightVec.xyz * tbn;
             #endif
             #endif
             #ifdef SPECULAR
             #ifdef SPECULAR
-                vEyeVec = -worldPos * tbn;
+                vEyeVec = -centeredWorldPos * tbn;
             #endif
             #endif
         #elif defined(SPECULAR)
         #elif defined(SPECULAR)
-            vEyeVec = -worldPos;
+            vEyeVec = -centeredWorldPos;
         #endif
         #endif
         
         
     #else
     #else
     
     
-        #if defined(SKINNED)
-            pos = GetPositionSkinned(iPosition, iBlendWeights, iBlendIndices, gl_Position);
-        #elif defined(BILLBOARD)
-            pos = GetPositionBillboard(iPosition, iTexCoord2, gl_Position);
-        #else
-            pos = GetPosition(iPosition, gl_Position);
-        #endif
-
         vLightVec = vec4(0.0, 0.0, 0.0, GetDepth(gl_Position));
         vLightVec = vec4(0.0, 0.0, 0.0, GetDepth(gl_Position));
 
 
     #endif
     #endif
@@ -122,6 +103,4 @@ void main()
     #ifdef VERTEXCOLOR
     #ifdef VERTEXCOLOR
         vColor = iColor;
         vColor = iColor;
     #endif
     #endif
-
-    vTexCoord = GetTexCoord(iTexCoord);
 }
 }

+ 1 - 1
SourceAssets/GLSLShaders/Forward.xml

@@ -21,7 +21,7 @@
             <exclude name="Spot" />
             <exclude name="Spot" />
         </option>
         </option>
         <option name="Spec" define="SPECULAR" />
         <option name="Spec" define="SPECULAR" />
-        <option name="Shadow" define="SHADOW" />
+        <option name="Shadow" define="SHADOW" require="LIGHT" />
         <option name="Unlit" define="UNLIT">
         <option name="Unlit" define="UNLIT">
             <exclude name="Normal" />
             <exclude name="Normal" />
             <exclude name="Dir" />
             <exclude name="Dir" />

+ 12 - 0
SourceAssets/GLSLShaders/Lighting.frag

@@ -75,3 +75,15 @@ float GetShadowFade(float depth)
 {
 {
     return clamp((depth - cShadowFade.x) * cShadowFade.y, 0.0, 1.0);
     return clamp((depth - cShadowFade.x) * cShadowFade.y, 0.0, 1.0);
 }
 }
+
+vec4 GetDirShadowPos(const vec4 shadowPos[4], float depth)
+{
+    if (depth < cShadowSplits.x)
+        return shadowPos[0];
+    else if (depth < cShadowSplits.y)
+        return shadowPos[1];
+    else if (depth < cShadowSplits.z)
+        return shadowPos[2];
+    else
+        return shadowPos[3];
+}

+ 3 - 5
SourceAssets/GLSLShaders/Shadow.vert

@@ -7,11 +7,9 @@
 
 
 void main()
 void main()
 {
 {
-    #if defined(SKINNED)
-        GetPositionSkinned(iPosition, iBlendWeights, iBlendIndices, gl_Position);
-    #else
-        GetPosition(iPosition, gl_Position);
-    #endif
+    mat4 modelMatrix = iModelMatrix;
+    vec3 worldPos = GetWorldPos(modelMatrix);
+    gl_Position = GetClipPos(worldPos);
 
 
     #ifdef ALPHAMASK
     #ifdef ALPHAMASK
         vTexCoord = GetTexCoord(iTexCoord);
         vTexCoord = GetTexCoord(iTexCoord);

+ 5 - 2
SourceAssets/GLSLShaders/Skybox.vert

@@ -5,7 +5,10 @@ varying vec3 vTexCoord;
 
 
 void main()
 void main()
 {
 {
-    GetPosition(iPosition, gl_Position);
+    mat4 modelMatrix = iModelMatrix;
+    vec3 worldPos = GetWorldPos(modelMatrix);
+    gl_Position = GetClipPos(worldPos);
+
     gl_Position.z = gl_Position.w;
     gl_Position.z = gl_Position.w;
-    vTexCoord = iPosition.xyz;
+    vTexCoord = iPos.xyz;
 }
 }

+ 3 - 1
SourceAssets/GLSLShaders/Stencil.vert

@@ -3,5 +3,7 @@
 
 
 void main()
 void main()
 {
 {
-    GetPosition(iPosition, gl_Position);
+    mat4 modelMatrix = iModelMatrix;
+    vec3 worldPos = GetWorldPos(modelMatrix);
+    gl_Position = GetClipPos(worldPos);
 }
 }

+ 38 - 47
SourceAssets/GLSLShaders/Transform.vert

@@ -1,4 +1,4 @@
-attribute vec4 iPosition;
+attribute vec4 iPos;
 attribute vec3 iNormal;
 attribute vec3 iNormal;
 attribute vec4 iColor;
 attribute vec4 iColor;
 attribute vec2 iTexCoord;
 attribute vec2 iTexCoord;
@@ -29,69 +29,60 @@ vec2 GetTexCoord(vec2 texCoord)
     return vec2(dot(texCoord, cUOffset.xy) + cUOffset.w, dot(texCoord, cVOffset.xy) + cVOffset.w);
     return vec2(dot(texCoord, cUOffset.xy) + cUOffset.w, dot(texCoord, cVOffset.xy) + cVOffset.w);
 }
 }
 
 
-float GetDepth(vec4 clipPos)
+vec4 GetClipPos(vec3 worldPos)
 {
 {
-    return dot(clipPos.zw, cDepthMode.zw);
+    return cViewProj * vec4(worldPos, 1.0);
 }
 }
 
 
-vec4 GetPosition(vec4 pos, out vec4 oPos)
+float GetDepth(vec4 clipPos)
 {
 {
-    vec4 worldPos = cModel * pos;
-    oPos = cViewProj * worldPos;
-    return worldPos;
+    return dot(clipPos.zw, cDepthMode.zw);
 }
 }
 
 
-vec4 GetPositionBillboard(vec4 pos, vec2 size, out vec4 oPos)
+vec3 GetBillboardPos(vec4 iPos, vec2 iSize)
 {
 {
-    vec4 worldPos = vec4(pos.xyz + size.x * cViewRightVector + size.y * cViewUpVector, 1.0);
-    oPos = cViewProj * worldPos;
-    return worldPos;
+    return vec3(iPos.xyz + iSize.x * cViewRightVector + iSize.y * cViewUpVector);
 }
 }
 
 
-vec4 GetPositionSkinned(vec4 pos, vec4 blendWeights, vec4 blendIndices, out vec4 oPos)
+vec3 GetBillboardNormal()
 {
 {
-    mat4 skinMatrix = GetSkinMatrix(blendWeights, blendIndices);
-    vec4 worldPos = pos * skinMatrix;
-    oPos = cViewProj * worldPos;
-    return worldPos;
+    return vec3(-cCameraRot[2][0], -cCameraRot[2][1], -cCameraRot[2][2]);
 }
 }
 
 
-vec4 GetPositionNormal(vec4 pos, vec3 normal, out vec4 oPos, out vec3 oNormal)
-{
-    mat3 normalMatrix = GetNormalMatrix(cModel);
-    vec4 worldPos = cModel * pos;
-    oPos = cViewProj * worldPos;
-    oNormal = normalize(normalMatrix * normal);
-    return worldPos;
-}
+#ifdef SKINNED
+    #define iModelMatrix GetSkinMatrix(iBlendWeights, iBlendIndices)
+#else
+    #define iModelMatrix cModel
+#endif
 
 
-vec4 GetPositionNormalSkinned(vec4 pos, vec3 normal, vec4 blendWeights, vec4 blendIndices, out vec4 oPos, out vec3 oNormal)
+vec3 GetWorldPos(mat4 modelMatrix)
 {
 {
-    mat4 skinMatrix = GetSkinMatrix(blendWeights, blendIndices);
-    mat3 normalMatrix = GetNormalMatrix(skinMatrix);
-    vec4 worldPos = pos * skinMatrix;
-    oPos = cViewProj * worldPos;
-    oNormal = normalize(normal * normalMatrix);
-    return worldPos;
+    #if defined(BILLBOARD)
+        return GetBillboardPos(iPos, iTexCoord2);
+    #elif defined(SKINNED)
+        return (iPos * modelMatrix).xyz;
+    #else
+        return (modelMatrix * iPos).xyz;
+    #endif
 }
 }
 
 
-vec4 GetPositionNormalTangent(vec4 pos, vec3 normal, vec4 tangent, out vec4 oPos, out vec3 oNormal, out vec3 oTangent)
+vec3 GetWorldNormal(mat4 modelMatrix)
 {
 {
-    mat3 normalMatrix = GetNormalMatrix(cModel);
-    vec4 worldPos = cModel * pos;
-    oPos = cViewProj * worldPos;
-    oNormal = normalize(normalMatrix * normal);
-    oTangent = normalize(normalMatrix * tangent.xyz);
-    return worldPos;
+    #if defined(BILLBOARD)
+        return GetBillboardNormal();
+    #elif defined(SKINNED)
+        return normalize(iNormal * GetNormalMatrix(modelMatrix));
+    #else
+        return normalize(GetNormalMatrix(modelMatrix) * iNormal);
+    #endif
 }
 }
 
 
-vec4 GetPositionNormalTangentSkinned(vec4 pos, vec3 normal, vec4 tangent, vec4 blendWeights, vec4 blendIndices, out vec4 oPos, out vec3 oNormal, out vec3 oTangent)
-{
-    mat4 skinMatrix = GetSkinMatrix(blendWeights, blendIndices);
-    mat3 normalMatrix = GetNormalMatrix(skinMatrix);
-    vec4 worldPos = pos * skinMatrix;
-    oPos = cViewProj * worldPos;
-    oNormal = normalize(normal * normalMatrix);
-    oTangent = normalize(tangent.xyz * normalMatrix);
-    return worldPos;
+vec3 GetWorldTangent(mat4 modelMatrix)
+{   
+    mat3 normalMatrix = GetNormalMatrix(modelMatrix);
+    #ifdef SKINNED
+        return normalize(iTangent.xyz * normalMatrix);
+    #else
+        return normalize(normalMatrix * iTangent.xyz);
+    #endif
 }
 }

+ 4 - 1
SourceAssets/HLSLShaders/Basic.hlsl

@@ -15,7 +15,10 @@ void VS(float4 iPos : POSITION,
     #endif
     #endif
     out float4 oPos : POSITION)
     out float4 oPos : POSITION)
 {
 {
-    GetPosition(iPos, oPos);
+    float4x3 modelMatrix = iModelMatrix;
+    float3 worldPos = GetWorldPos(modelMatrix);
+    oPos = GetClipPos(worldPos);
+
     #ifdef VERTEXCOLOR
     #ifdef VERTEXCOLOR
         oColor = iColor;
         oColor = iColor;
     #endif
     #endif

+ 26 - 63
SourceAssets/HLSLShaders/Forward.hlsl

@@ -34,7 +34,7 @@ void VS(float4 iPos : POSITION,
             #elif defined(SPOTLIGHT)
             #elif defined(SPOTLIGHT)
                 out float4 oShadowPos : TEXCOORD4,
                 out float4 oShadowPos : TEXCOORD4,
             #else
             #else
-                out float3 oWorldLightVec : TEXCOORD4,
+                out float3 oShadowPos : TEXCOORD4,
             #endif
             #endif
         #endif
         #endif
         #ifdef SPOTLIGHT
         #ifdef SPOTLIGHT
@@ -51,7 +51,10 @@ void VS(float4 iPos : POSITION,
     out float4 oPos : POSITION,
     out float4 oPos : POSITION,
     out float2 oTexCoord : TEXCOORD0)
     out float2 oTexCoord : TEXCOORD0)
 {
 {
-    float4 pos;
+    float4x3 modelMatrix = iModelMatrix;
+    float3 worldPos = GetWorldPos(modelMatrix);
+    oPos = GetClipPos(worldPos);
+    oTexCoord = GetTexCoord(iTexCoord);
 
 
     #ifndef UNLIT
     #ifndef UNLIT
 
 
@@ -60,36 +63,15 @@ void VS(float4 iPos : POSITION,
             float3 oTangent;
             float3 oTangent;
             float3 oBitangent;
             float3 oBitangent;
         #endif
         #endif
-    
-        #if defined(SKINNED)
-            #ifndef NORMALMAP
-                pos = GetPositionNormalSkinned(iPos, iNormal, iBlendWeights, iBlendIndices, oPos, oNormal);
-            #else
-                pos = GetPositionNormalTangentSkinned(iPos, iNormal, iTangent, iBlendWeights, iBlendIndices, oPos, oNormal, oTangent);
-            #endif
-        #elif defined(INSTANCED)
-            #ifndef NORMALMAP
-                pos = GetPositionNormalInstanced(iPos, iNormal, iModelInstance, oPos, oNormal);
-            #else
-                pos = GetPositionNormalTangentInstanced(iPos, iNormal, iTangent, iModelInstance, oPos, oNormal, oTangent);
-            #endif
-        #elif defined(BILLBOARD)
-            pos = GetPositionBillboard(iPos, iSize, oPos);
-            oNormal = float3(-cCameraRot[2][0], -cCameraRot[2][1], -cCameraRot[2][2]);
-        #else
-            #ifndef NORMALMAP
-                pos = GetPositionNormal(iPos, iNormal, oPos, oNormal);
-            #else
-                pos = GetPositionNormalTangent(iPos, iNormal, iTangent, oPos, oNormal, oTangent);
-            #endif
-        #endif
-    
-        float3 worldPos = pos.xyz - cCameraPos;
-    
+
+        oNormal = GetWorldNormal(modelMatrix);
+        float3 centeredWorldPos = worldPos - cCameraPos;
+        float4 projWorldPos = float4(worldPos, 1.0);
+
         #ifdef DIRLIGHT
         #ifdef DIRLIGHT
             oLightVec = float4(cLightDir, GetDepth(oPos));
             oLightVec = float4(cLightDir, GetDepth(oPos));
         #elif defined(LIGHT)
         #elif defined(LIGHT)
-            oLightVec = float4((cLightPos - worldPos) * cLightAtten, GetDepth(oPos));
+            oLightVec = float4((cLightPos - centeredWorldPos) * cLightAtten, GetDepth(oPos));
         #else
         #else
             oLightVec = float4(0.0, 0.0, 0.0, GetDepth(oPos));
             oLightVec = float4(0.0, 0.0, 0.0, GetDepth(oPos));
         #endif
         #endif
@@ -97,51 +79,42 @@ void VS(float4 iPos : POSITION,
         #ifdef SHADOW
         #ifdef SHADOW
             // Shadow projection: transform from world space to shadow space
             // Shadow projection: transform from world space to shadow space
             #if defined(DIRLIGHT)
             #if defined(DIRLIGHT)
-                oShadowPos[0] = mul(pos, cShadowProj[0]);
-                oShadowPos[1] = mul(pos, cShadowProj[1]);
-                oShadowPos[2] = mul(pos, cShadowProj[2]);
-                oShadowPos[3] = mul(pos, cShadowProj[3]);
+                oShadowPos[0] = mul(projWorldPos, cShadowProj[0]);
+                oShadowPos[1] = mul(projWorldPos, cShadowProj[1]);
+                oShadowPos[2] = mul(projWorldPos, cShadowProj[2]);
+                oShadowPos[3] = mul(projWorldPos, cShadowProj[3]);
             #elif defined(SPOTLIGHT)
             #elif defined(SPOTLIGHT)
-                oShadowPos = mul(pos, cShadowProj[0]);
+                oShadowPos = mul(projWorldPos, cShadowProj[0]);
             #else
             #else
-                oWorldLightVec = worldPos - cLightPos;
+                oShadowPos = centeredWorldPos - cLightPos;
             #endif
             #endif
         #endif
         #endif
     
     
         #ifdef SPOTLIGHT
         #ifdef SPOTLIGHT
             // Spotlight projection: transform from world space to projector texture coordinates
             // Spotlight projection: transform from world space to projector texture coordinates
-            oSpotPos = mul(pos, cSpotProj);
+            oSpotPos = mul(projWorldPos, cSpotProj);
         #endif
         #endif
-    
+
         #ifdef POINTLIGHT
         #ifdef POINTLIGHT
             oCubeMaskVec = mul(oLightVec.xyz, cLightVecRot);
             oCubeMaskVec = mul(oLightVec.xyz, cLightVecRot);
         #endif
         #endif
     
     
         #ifdef NORMALMAP
         #ifdef NORMALMAP
+            oTangent = GetWorldTangent(modelMatrix);
             oBitangent = cross(oTangent, oNormal) * iTangent.w;
             oBitangent = cross(oTangent, oNormal) * iTangent.w;
             float3x3 tbn = float3x3(oTangent, oBitangent, oNormal);
             float3x3 tbn = float3x3(oTangent, oBitangent, oNormal);
             #ifdef LIGHT
             #ifdef LIGHT
                 oLightVec.xyz = mul(tbn, oLightVec.xyz);
                 oLightVec.xyz = mul(tbn, oLightVec.xyz);
             #endif
             #endif
             #ifdef SPECULAR
             #ifdef SPECULAR
-                oEyeVec = mul(tbn, -worldPos);
+                oEyeVec = mul(tbn, -centeredWorldPos);
             #endif
             #endif
         #elif defined(SPECULAR)
         #elif defined(SPECULAR)
-            oEyeVec = -worldPos;
+            oEyeVec = -centeredWorldPos;
         #endif
         #endif
 
 
     #else
     #else
-    
-        #if defined(SKINNED)
-            pos = GetPositionSkinned(iPos, iBlendWeights, iBlendIndices, oPos);
-        #elif defined(INSTANCED)
-            pos = GetPositionInstanced(iPos, iModelInstance, oPos);
-        #elif defined(BILLBOARD)
-            pos = GetPositionBillboard(iPos, iSize, oPos);
-        #else
-            pos = GetPosition(iPos, oPos);
-        #endif
-        
+
         oLightVec = float4(0.0, 0.0, 0.0, GetDepth(oPos));
         oLightVec = float4(0.0, 0.0, 0.0, GetDepth(oPos));
 
 
     #endif
     #endif
@@ -149,8 +122,6 @@ void VS(float4 iPos : POSITION,
     #ifdef VERTEXCOLOR
     #ifdef VERTEXCOLOR
         oColor = iColor;
         oColor = iColor;
     #endif
     #endif
-
-    oTexCoord = GetTexCoord(iTexCoord);
 }
 }
 
 
 void PS(float2 iTexCoord : TEXCOORD0,
 void PS(float2 iTexCoord : TEXCOORD0,
@@ -167,7 +138,7 @@ void PS(float2 iTexCoord : TEXCOORD0,
         #elif defined(SPOTLIGHT)
         #elif defined(SPOTLIGHT)
             float4 iShadowPos : TEXCOORD4,
             float4 iShadowPos : TEXCOORD4,
         #else
         #else
-            float3 iWorldLightVec : TEXCOORD4,
+            float3 iShadowPos : TEXCOORD4,
         #endif
         #endif
     #endif
     #endif
     #ifdef SPOTLIGHT
     #ifdef SPOTLIGHT
@@ -216,20 +187,12 @@ void PS(float2 iTexCoord : TEXCOORD0,
 
 
         #ifdef SHADOW
         #ifdef SHADOW
             #if defined(DIRLIGHT)
             #if defined(DIRLIGHT)
-                float4 shadowPos;
-                if (iLightVec.w < cShadowSplits.x)
-                    shadowPos = iShadowPos[0];
-                else if (iLightVec.w < cShadowSplits.y)
-                    shadowPos = iShadowPos[1];
-                else if (iLightVec.w < cShadowSplits.z)
-                    shadowPos = iShadowPos[2];
-                else
-                    shadowPos = iShadowPos[3];
+                float4 shadowPos = GetDirShadowPos(iShadowPos, iLightVec.w);
                 diff *= saturate(GetShadow(shadowPos) + GetShadowFade(iLightVec.w));
                 diff *= saturate(GetShadow(shadowPos) + GetShadowFade(iLightVec.w));
             #elif defined(SPOTLIGHT)
             #elif defined(SPOTLIGHT)
                 diff *= GetShadow(iShadowPos);
                 diff *= GetShadow(iShadowPos);
             #else
             #else
-                diff *= GetCubeShadow(iWorldLightVec);
+                diff *= GetCubeShadow(iShadowPos);
             #endif
             #endif
         #endif
         #endif
 
 

+ 1 - 1
SourceAssets/HLSLShaders/Forward.xml

@@ -21,7 +21,7 @@
             <exclude name="Spot" />
             <exclude name="Spot" />
         </option>
         </option>
         <option name="Spec" define="SPECULAR" />
         <option name="Spec" define="SPECULAR" />
-        <option name="Shadow" define="SHADOW" />
+        <option name="Shadow" define="SHADOW" require="LIGHT" />
         <option name="Unlit" define="UNLIT">
         <option name="Unlit" define="UNLIT">
             <exclude name="Normal" />
             <exclude name="Normal" />
             <exclude name="Dir" />
             <exclude name="Dir" />

+ 13 - 0
SourceAssets/HLSLShaders/Lighting.hlsl

@@ -111,3 +111,16 @@ float GetShadowFade(float depth)
 {
 {
     return saturate((depth - cShadowFade.x) * cShadowFade.y);
     return saturate((depth - cShadowFade.x) * cShadowFade.y);
 }
 }
+
+float4 GetDirShadowPos(const float4 iShadowPos[4], float depth)
+{
+    if (depth < cShadowSplits.x)
+        return iShadowPos[0];
+    else if (depth < cShadowSplits.y)
+        return iShadowPos[1];
+    else if (depth < cShadowSplits.z)
+        return iShadowPos[2];
+    else
+        return iShadowPos[3];
+}
+

+ 3 - 7
SourceAssets/HLSLShaders/Shadow.hlsl

@@ -19,13 +19,9 @@ void VS(float4 iPos : POSITION,
     #endif
     #endif
     out float4 oPos : POSITION)
     out float4 oPos : POSITION)
 {
 {
-    #if defined(SKINNED)
-        GetPositionSkinned(iPos, iBlendWeights, iBlendIndices, oPos);
-    #elif defined(INSTANCED)
-        GetPositionInstanced(iPos, iModelInstance, oPos);
-    #else
-        GetPosition(iPos, oPos);
-    #endif
+    float4x3 modelMatrix = iModelMatrix;
+    float3 worldPos = GetWorldPos(modelMatrix);
+    oPos = GetClipPos(worldPos);
 
 
     #ifdef ALPHAMASK
     #ifdef ALPHAMASK
         oTexCoord = GetTexCoord(iTexCoord);
         oTexCoord = GetTexCoord(iTexCoord);

+ 4 - 1
SourceAssets/HLSLShaders/Skybox.hlsl

@@ -6,7 +6,10 @@ void VS(float4 iPos : POSITION,
     out float4 oPos : POSITION,
     out float4 oPos : POSITION,
     out float3 oTexCoord : TEXCOORD0)
     out float3 oTexCoord : TEXCOORD0)
 {
 {
-    GetPosition(iPos, oPos);
+    float4x3 modelMatrix = iModelMatrix;
+    float3 worldPos = GetWorldPos(modelMatrix);
+    oPos = GetClipPos(worldPos);
+    
     oPos.z = oPos.w;
     oPos.z = oPos.w;
     oTexCoord = iPos.xyz;
     oTexCoord = iPos.xyz;
 }
 }

+ 3 - 1
SourceAssets/HLSLShaders/Stencil.hlsl

@@ -4,7 +4,9 @@
 void VS(float4 iPos : POSITION,
 void VS(float4 iPos : POSITION,
     out float4 oPos : POSITION)
     out float4 oPos : POSITION)
 {
 {
-    GetPosition(iPos, oPos);
+    float4x3 modelMatrix = iModelMatrix;
+    float3 worldPos = GetWorldPos(modelMatrix);
+    oPos = GetClipPos(worldPos);
 }
 }
 
 
 void PS(out float4 oColor : COLOR0)
 void PS(out float4 oColor : COLOR0)

+ 26 - 71
SourceAssets/HLSLShaders/Transform.hlsl

@@ -11,89 +11,44 @@ float2 GetTexCoord(float2 iTexCoord)
     return float2(dot(iTexCoord, cUOffset.xy) + cUOffset.w, dot(iTexCoord, cVOffset.xy) + cVOffset.w);
     return float2(dot(iTexCoord, cUOffset.xy) + cUOffset.w, dot(iTexCoord, cVOffset.xy) + cVOffset.w);
 };
 };
 
 
-float GetDepth(float4 clipPos)
-{
-    return dot(clipPos.zw, cDepthMode.zw);
-}
-
-float4 GetPosition(float4 iPos, out float4 oPos)
-{
-    float4 worldPos = float4(mul(iPos, cModel), 1.0);
-    oPos = mul(worldPos, cViewProj);
-    return worldPos;
-}
-
-float4 GetPositionBillboard(float4 iPos, float2 iSize, out float4 oPos)
-{
-    float4 worldPos = float4(iPos.xyz + iSize.x * cViewRightVector + iSize.y * cViewUpVector, 1.0);
-    oPos = mul(worldPos, cViewProj);
-    return worldPos;
-}
-
-float4 GetPositionSkinned(float4 iPos, float4 iBlendWeights, int4 iBlendIndices, out float4 oPos)
+float4 GetClipPos(float3 worldPos)
 {
 {
-    float4x3 skinMatrix = GetSkinMatrix(iBlendWeights, iBlendIndices);
-    float4 worldPos = float4(mul(iPos, skinMatrix), 1.0);
-    oPos = mul(worldPos, cViewProj);
-    return worldPos;
+    return mul(float4(worldPos, 1.0), cViewProj);
 }
 }
 
 
-float4 GetPositionInstanced(float4 iPos, float4x3 iModel, out float4 oPos)
+float GetDepth(float4 clipPos)
 {
 {
-    float4 worldPos = float4(mul(iPos, iModel), 1.0);
-    oPos = mul(worldPos, cViewProj);
-    return worldPos;
+    return dot(clipPos.zw, cDepthMode.zw);
 }
 }
 
 
-float4 GetPositionNormal(float4 iPos, float3 iNormal, out float4 oPos, out float3 oNormal)
+float3 GetBillboardPos(float4 iPos, float2 iSize)
 {
 {
-    float4 worldPos = float4(mul(iPos, cModel), 1.0);
-    oPos = mul(worldPos, cViewProj);
-    oNormal = normalize(mul(iNormal, (float3x3)cModel));
-    return worldPos;
+    return float3(iPos.xyz + iSize.x * cViewRightVector + iSize.y * cViewUpVector);
 }
 }
 
 
-float4 GetPositionNormalSkinned(float4 iPos, float3 iNormal, float4 iBlendWeights, int4 iBlendIndices, out float4 oPos, out float3 oNormal)
+float3 GetBillboardNormal()
 {
 {
-    float4x3 skinMatrix = GetSkinMatrix(iBlendWeights, iBlendIndices);
-    float4 worldPos = float4(mul(iPos, skinMatrix), 1.0);
-    oPos = mul(worldPos, cViewProj);
-    oNormal = normalize(mul(iNormal, (float3x3)skinMatrix));
-    return worldPos;
+    return float3(-cCameraRot[2][0], -cCameraRot[2][1], -cCameraRot[2][2]);
 }
 }
 
 
-float4 GetPositionNormalInstanced(float4 iPos, float3 iNormal, float4x3 iModel, out float4 oPos, out float3 oNormal)
-{
-    float4 worldPos = float4(mul(iPos, iModel), 1.0);
-    oPos = mul(worldPos, cViewProj);
-    oNormal = normalize(mul(iNormal, (float3x3)iModel));
-    return worldPos;
-}
+#if defined(SKINNED)
+    #define iModelMatrix GetSkinMatrix(iBlendWeights, iBlendIndices);
+#elif defined(INSTANCED)
+    #define iModelMatrix iModelInstance
+#else
+    #define iModelMatrix cModel
+#endif
 
 
-float4 GetPositionNormalTangent(float4 iPos, float3 iNormal, float4 iTangent, out float4 oPos, out float3 oNormal, out float3 oTangent)
-{
-    float4 worldPos = float4(mul(iPos, cModel), 1.0);
-    oPos = mul(worldPos, cViewProj);
-    oNormal = normalize(mul(iNormal, (float3x3)cModel));
-    oTangent = normalize(mul(iTangent.xyz, (float3x3)cModel));
-    return worldPos;
-}
+#ifdef BILLBOARD
+    #define GetWorldPos(modelMatrix) GetBillboardPos(iPos, iSize)
+#else
+    #define GetWorldPos(modelMatrix) mul(iPos, modelMatrix)
+#endif
 
 
-float4 GetPositionNormalTangentSkinned(float4 iPos, float3 iNormal, float4 iTangent, float4 iBlendWeights, int4 iBlendIndices, out float4 oPos, out float3 oNormal, out float3 oTangent)
-{
-    float4x3 skinMatrix = GetSkinMatrix(iBlendWeights, iBlendIndices);
-    float4 worldPos = float4(mul(iPos, skinMatrix), 1.0);
-    oPos = mul(worldPos, cViewProj);
-    oNormal = normalize(mul(iNormal, (float3x3)skinMatrix));
-    oTangent = normalize(mul(iTangent.xyz, (float3x3)skinMatrix));
-    return worldPos;
-}
+#ifdef BILLBOARD
+    #define GetWorldNormal(modelMatrix) GetBillboardNormal()
+#else
+    #define GetWorldNormal(modelMatrix) normalize(mul(iNormal, (float3x3)modelMatrix))
+#endif
 
 
-float4 GetPositionNormalTangentInstanced(float4 iPos, float3 iNormal, float4 iTangent, float4x3 iModel, out float4 oPos, out float3 oNormal, out float3 oTangent)
-{
-    float4 worldPos = float4(mul(iPos, iModel), 1.0);
-    oPos = mul(worldPos, cViewProj);
-    oNormal = normalize(mul(iNormal, (float3x3)iModel));
-    oTangent = normalize(mul(iTangent.xyz, (float3x3)iModel));
-    return worldPos;
-}
+#define GetWorldTangent(modelMatrix) normalize(mul(iTangent.xyz, (float3x3)modelMatrix))