problem connected with shader

Dec 1, 2010 at 8:36 AM

hello statusunknown,

                           I have written a directional light shadow shader . Initially it was working fine, later i  have added few lines of code for better shadow effect but it is giving me a compilation error which is

 "Custom tool error (121) Program is too complex, there are more active values than registers ". I have pasted the code below  

                          

#include "Environment.fx.h"
float3 lightDirection;
float3 diffuseColor;
float3 specularColor;
float3 cameraPosition;
float4x4 InvertViewProjection;
float lightIntensity ,specularIntensity;
float2 g_vShadowMapSize;
float4x4 lightViewProjection[3];
 
texture colorMap;
texture normalMap;
texture depthMap;
texture shadow1;
texture shadow2;
texture shadow3;
 
 
 
sampler shadowSampler1 = sampler_state
{
Texture = (shadow1);
AddressU =BORDER;
AddressV = BORDER ;
MinFilter = POINT ;
MagFilter = POINT;
MipFilter = POINT ;
BorderColor = 0xFFFFFFFF;
};
sampler shadowSampler2 = sampler_state
{
Texture = (shadow2);
AddressU =BORDER;
AddressV = BORDER ;
MinFilter = POINT ;
MagFilter = POINT;
MipFilter = POINT ;
BorderColor = 0xFFFFFFFF;
};
sampler shadowSampler3 = sampler_state
{
Texture = (shadow3);
AddressU =BORDER;
AddressV = BORDER ;
MinFilter = POINT ;
MagFilter = POINT;
MipFilter = POINT ;
BorderColor = 0xFFFFFFFF;
};
sampler depthSampler = sampler_state
{
Texture = (depthMap);
AddressU = CLAMP;
AddressV = CLAMP;
MagFilter = POINT;
MinFilter = POINT;
Mipfilter = POINT;
};
sampler normalSampler = sampler_state
{
Texture = (normalMap);
AddressU = CLAMP;
AddressV = CLAMP;
MagFilter = POINT;
MinFilter = POINT;
Mipfilter = POINT;
};
 
struct VertexShaderInput
{
float3 Position : POSITION0;
float2 TexCoord : TEXCOORD0;
};
struct VertexShaderOutput
{
float4 Position : POSITION0;
float2 TexCoord : TEXCOORD0;
};
float2 halfPixel;
VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
VertexShaderOutput output;
output.Position = float4(input.Position,1);
output.TexCoord = input.TexCoord - halfPixel;
return output;
}
float CalcShadowTermPCF(float fLightDepth, float2 vShadowTexCoord,sampler shadowSampler,float BIAS)
{
float fShadowTerm = 0.0f;
float2 offset = float2(1.0f/g_vShadowMapSize.x,1.0f/g_vShadowMapSize.y);
 
float fSamples = 0.0f;
float2 texCoord[9];
texCoord[0] = vShadowTexCoord;
texCoord[1] = vShadowTexCoord + float2(offset.x, 0);
texCoord[2] = vShadowTexCoord + float2(-offset.x, 0);
texCoord[3] = vShadowTexCoord + float2(0, offset.y);
texCoord[4] = vShadowTexCoord + float2(0, -offset.y);
 
texCoord[5] = vShadowTexCoord + float2(-offset.x, -offset.y);
texCoord[6] = vShadowTexCoord + float2(-offset.x, offset.y);
texCoord[7] = vShadowTexCoord + float2(offset.x, -offset.y);
texCoord[8] = vShadowTexCoord + float2(offset.x, offset.y);
 
for(int i=0;i<9;i++)
{
fSamples += (tex2D(shadowSampler, texCoord[i]).x + BIAS < fLightDepth) ? 0.0f: 1.0f;
}
fShadowTerm = (fSamples)/9.0f;
return fShadowTerm ;
 
 
}
 
 
float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
 
 
float4 normalData = tex2D(normalSampler,input.TexCoord);
float3 normal = 2.0f * normalData.xyz - 1.0f;
float specularPower = normalData.a *100.0f;
 
 
float4 depthVal = tex2D(depthSampler,input.TexCoord);
//compute screen-space position
float4 position;
position.x = input.TexCoord.x * 2.0f - 1.0f;
position.y = -(input.TexCoord.y * 2.0f - 1.0f);
position.z = depthVal.r;
position.w = 1.0f;
position = mul(position, InvertViewProjection);
position = position/position.w;
float distFromCamera = length(position.xyz - cameraPosition);
 
//----------Position WRT to light
float shadowTerm =1.0f ;
//float3 occlusion = float3(1.0f,1.0f,1.0f);
float3 ambient = float3(0.0f,0.0f,0.0f);
 
 
 
if(distFromCamera <= 220.0f)
{
float4 lightScreenPosition[3];
 
float3 shadowUV[3] ;
 
//float2 n = mul(normal,lightViewProjection[0]).xy;
 
for(int i=0;i<3;i++)
{
 
lightScreenPosition[i] = mul(position,lightViewProjection[i]);
lightScreenPosition[i] /=lightScreenPosition[i].w ;
 
//------Sample Shadow Map
 
shadowUV[i].xz = (lightScreenPosition[i].xz/ 2.0f + 0.5f) ;
shadowUV[i].y = (-lightScreenPosition[i].y / 2.0f + 0.5f );
 
//Adding these line is showing error  
.if(normalData.a >=1.0f )
shadowUV[i].x = shadowUV[i].x + normal.x ;
 
}
 
//-----Shadow Condition
float shadowTerm1 = CalcShadowTermPCF(lightScreenPosition[0].z,float2(shadowUV[0].x,shadowUV[0].y),shadowSampler1,0.001f);
float shadowTerm2 = CalcShadowTermPCF(lightScreenPosition[1].z,float2(shadowUV[1].x,shadowUV[1].y),shadowSampler2,0.003f);
float shadowTerm3 = CalcShadowTermPCF(lightScreenPosition[2].z,float2(shadowUV[2].x,shadowUV[2].y),shadowSampler3,0.003f);
 
 
bool s1 = all(abs(shadowUV[0] - 0.5f) <= 0.5f);
bool s2 = all(abs(shadowUV[1] - 0.5f) <= 0.5f);
bool s3 = all(abs(shadowUV[2] - 0.5f) <= 0.5f);
bool exit = false ;
 
 
if(!exit && s1 )
{
shadowTerm= shadowTerm1;
exit = true ;
}
else
{
if(!exit && s2 )
{
shadowTerm= shadowTerm2 ;
exit = true ;
}
else
{
if(!exit)
{
shadowTerm= shadowTerm3 ;
exit = true ;
}
}
}
}
 
//surface-to-light vector
float3 lightVector = -normalize(lightDirection);
 
//Compute spherical Light component
 
ambient = SampleSH(normal,17.0f);
//occlusion = float3(1.0f,1.0f,1.0f);//tex2D(OcclusionSampler , input.TexCoord);
//compute diffuse light
float NdL = max(0.2f,dot(normal,lightVector)) ;
float diffuseIntensity = NdL * lightIntensity;
float toonIntensity = 1.0f;
 
float3 diffuseLight = diffuseIntensity * toonIntensity * diffuseColor.rgb ;
 
 
 
 
//Compute Specular Light
 
float3 reflectionVector =normalize(reflect(lightVector, normal)); // needed to be checked
float3 directionToCamera = normalize( position - cameraPosition );
float specularity = specularIntensity * pow( saturate(dot(reflectionVector, directionToCamera)), specularPower);
float3 specularLight = specularColor*specularity;
 
float4 color = float4((shadowTerm*diffuseLight.rgb+specularLight.rgb*shadowTerm+ambient),1.0f);
 
return color;
}
technique Technique1
{
pass Pass0
{
VertexShader = compile vs_3_0 VertexShaderFunction();
PixelShader = compile ps_3_0 PixelShaderFunction();
}
} 
 

                     So i have highlighted the part of the code which triggers the error in RED.  Is there any limits on the number 
of instructions in your custom shader tool for PS_3_0 ???? Looking forward for help