Starting to add cascading shadow maps

This commit is contained in:
2025-07-14 21:10:07 +02:00
parent cc22f10565
commit 9bd1ca1b09
18 changed files with 282 additions and 172 deletions
+21 -3
View File
@@ -3,6 +3,18 @@ import LightEnv;
import MaterialParameter;
import MATERIAL_FILE_NAME;
const static uint64_t NUM_CASCADES = 4;
/*struct ShadowMappingData
{
Texture2DArray shadowMaps[NUM_CASCADES];
ConstantBuffer<float4x4> lightSpaceMatrices[NUM_CASCADES];
ConstantBuffer<float[NUM_CASCADES]> cascadeSplits;
};
ParameterBlock<ShadowMappingData> pShadowMapping;
*/
struct LightCullingData
{
RWStructuredBuffer<uint> lightIndexList;
@@ -30,7 +42,13 @@ float4 fragmentMain(in FragmentParameter params) : SV_Target
float3 result = float3(0, 0, 0);
for(int i = 0; i < pLightEnv.numDirectionalLights; ++i)
{
float4 lightSpacePos = mul(biasMat, mul(pLightEnv.directionalLights[i].lightSpaceMatrix, float4(params.position_WS, 1)));
/*uint cascadeIndex = 0;
for (uint c = 0; c < NUM_CASCADES - 1; ++c) {
if (params.position_VS.z < pShadowMapping.cascadeSplits[c]) {
cascadeIndex = c + 1;
}
}
float4 lightSpacePos = mul(biasMat, mul(pShadowMapping.lightSpaceMatrices[i], float4(params.position_WS, 1)));
float4 shadowCoord = lightSpacePos / lightSpacePos.w;
int2 texDim;
pLightEnv.shadowMap.GetDimensions(texDim.x, texDim.y);
@@ -55,9 +73,9 @@ float4 fragmentMain(in FragmentParameter params) : SV_Target
shadowFactor += shadow;
count++;
}
}
}*/
result += (shadowFactor / count) * pLightEnv.directionalLights[i].illuminate(lightingParams, brdf);
result += /*(shadowFactor / count) **/ pLightEnv.directionalLights[i].illuminate(lightingParams, brdf);
}
for (uint i = 0; i < pLightEnv.numPointLights; ++i)
{
-2
View File
@@ -10,7 +10,6 @@ struct DirectionalLight : ILightEnv
{
float4 color;
float4 direction;
float4x4 lightSpaceMatrix;
float3 illuminate<B:IBRDF>(LightingParameter params, B brdf)
{
@@ -369,4 +368,3 @@ struct CookTorrance : IBRDF
}
[mutating] void setNormal(float3 n) { normal = n; }
};
+8 -4
View File
@@ -63,7 +63,8 @@ struct FragmentParameter
float3 normal_WS : NORMALWS;
float3 tangent_WS : TANGENTWS;
float3 biTangent_WS : BITANGENTWS;
float3 position_WS : POSITIONWS;
float3 position_WS : POSITIONWS;
float3 position_VS : POSITIONVS;
float3 vertexColor : COLOR;
float4 texCoords0 : TEXCOORDS0;
float4 texCoords1 : TEXCOORDS1;
@@ -109,7 +110,8 @@ struct FragmentParameter
result.normal_WS = f0.normal_WS * barycentricCoords.x + f1.normal_WS * barycentricCoords.y + f2.normal_WS * barycentricCoords.z;
result.tangent_WS = f0.tangent_WS * barycentricCoords.x + f1.tangent_WS * barycentricCoords.y + f2.tangent_WS * barycentricCoords.z;
result.biTangent_WS = f0.biTangent_WS * barycentricCoords.x + f1.biTangent_WS * barycentricCoords.y + f2.biTangent_WS * barycentricCoords.z;
result.position_WS = f0.position_WS * barycentricCoords.x + f1.position_WS * barycentricCoords.y + f2.position_WS * barycentricCoords.z;
result.position_WS = f0.position_WS * barycentricCoords.x + f1.position_WS * barycentricCoords.y + f2.position_WS * barycentricCoords.z;
result.position_VS = f0.position_VS * barycentricCoords.x + f1.position_VS * barycentricCoords.y + f2.position_VS * barycentricCoords.z;
result.vertexColor = f0.vertexColor * barycentricCoords.x + f1.vertexColor * barycentricCoords.y + f2.vertexColor * barycentricCoords.z;
result.texCoords0 = f0.texCoords0 * barycentricCoords.x + f1.texCoords0 * barycentricCoords.y + f2.texCoords0 * barycentricCoords.z;
result.texCoords1 = f0.texCoords1 * barycentricCoords.x + f1.texCoords1 * barycentricCoords.y + f2.texCoords1 * barycentricCoords.z;
@@ -135,7 +137,8 @@ struct VertexAttributes
FragmentParameter getParameter(float4x4 transformMatrix, float4x4 inverseTransformMatrix)
{
float4 modelPos = float4(position_MS, 1);
float4 worldPos = mul(transformMatrix, modelPos);
float4 worldPos = mul(transformMatrix, modelPos);
float4 viewPos = mul(pViewParams.viewMatrix, worldPos);
float4 clipPos = mul(pViewParams.viewProjectionMatrix, worldPos);
FragmentParameter result;
result.position_CS = clipPos;
@@ -145,7 +148,8 @@ struct VertexAttributes
result.biTangent_WS = mul(transformMatrix, float4(biTangent_MS, 0)).xyz;
result.normal_WS = mul(transformMatrix, float4(normal_MS, 0)).xyz;
result.vertexColor = vertexColor;
result.position_WS = worldPos.xyz;
result.position_WS = worldPos.xyz;
result.position_VS = viewPos.xyz;
result.texCoords0 = float4(texCoords[0], texCoords[1]);
result.texCoords1 = float4(texCoords[2], texCoords[3]);
result.texCoords2 = float4(texCoords[4], texCoords[5]);