Hi all,
Now that I've got my shadowmapping implementation working, I noticed I'm doing quite some ‘ifs’/ branching in my shader. In general I know it's best to minimize those and where possible replace with multiplication with 1 generic formula instead.
Any idea if/ how I could apply that better here?
// spot lights
[unroll]
for(uint sl=0;sl<gPerObject.NrSpotLightsAff;++sl)
{
uint lightId = gPerObject.SpotLightsAff[sl/4][sl%4];
float3 projColor = float3(1.0f, 1.0f, 1.0f);
if(gPerScene.LightSources[lightId].ProjectedTex)
{
uint texIndex = gPerScene.LightSources[lightId].LengthOrProjTexIndex;
float2 projUV = pin.SpotLightSpacePos[sl].xy / pin.SpotLightSpacePos[sl].w;
float2 projUVnorm = projUV * float2(0.5, -0.5) + 0.5;
projColor = gLightMap.Sample(gTexSampler, float3(projUVnorm, texIndex)).rgb;
}
float shadowFactor = 1.0f;
if(gPerScene.LightSources[lightId].CastShadows)
{
float4 lightSpacePos = pin.SpotLightSpacePos[sl];
lightSpacePos.xyz /= lightSpacePos.w;
if(LightInRange(lightSpacePos))
{
shadowFactor = GetShadowFactor(lightSpacePos, gPerScene.LightSources[lightId].SpotShadowMapIndex);
}
}
CalcSpotLight(gPerScene.LightSources[lightId], pin.PosW, toEye, lightSetup, projColor, shadowFactor);
}
And:
void CalcSpotLight(LightSource pSpotLight, float3 pPos, float3 pToEye, inout LightSetup pLightSetup, float3 pProjColor, float pShadowFactor)
{
if(!any(pSpotLight.ColorIntensity.rgb)) return;
if(pShadowFactor == 0.0f) return;
Any input is appreciated.