I have been trying to get a very simple pixel shader going that just applies diffuse lighting ( N dot L ) for a single directional light source. My pixel shader code is as follows:
cbuffer cb0 : register(b0)
{
float4 vCameraPos;
float4 vGlobalAmbient;
};
cbuffer cb1 : register(b1)
{
float4 vAmbient;
float4 vDiffuse;
float4 vSpecular;
int bDiffuseMap;
int bGlossMap;
int bNormalMap;
int bBumpMap;
};
cbuffer cb2 : register(b2)
{
float3 vLightColor;
float3 vLightDir;
int nLightType;
float SomeOtherFloat;
};
struct PS_INPUT
{
float4 vPosProj : SV_POSITION;
float2 vTexUV : TEXCOORD0;
float3 vNormWorld : TEXCOORD1;
float3 vPosWorld : TEXCOORD2;
};
// entry point
float4 main( PS_INPUT vert ) : SV_Target
{
float4 color;
color.a = vAmbient.a;
float3 lightVec = normalize( vLightDir );
float3 vectorToLight = normalize( vert.vPosWorld - lightVec );
float fDiffuse = saturate( dot( vert.vNormWorld, vectorToLight ) );
color.rgb = vLightColor * vAmbient.rgb;
color.rgb += vDiffuse.rgb * fDiffuse * vLightColor;
return color;
}
When I run this shader in my engine, the model it is rendering flashes between ambient-only and diffuse-lit very rapidly. I have determined that the problem is in the shader code, not my engine, and it is the statement that computes the fDiffuse variable. When I omit that line, and substitute a constant value for fDiffuse, the flashing goes away, but I am left with plain old ambient lighting.
Has anyone else seen this happening? Am I doing something wrong? plz help!