# The saturate function is not working in my pixel shader

I wrote a pixel shader for my game and when I tried to compile it an error occurred:

ID3DXEffectCompiler::CompileEffect: There was an error compiling expression


When I removed all the saturate functions from the shader it compiled without problems. Strangely the saturate function works for all techniques in this effect file but one.

This code worked for me:

float4 PixelShader(VertexToPixel PSIn) : COLOR0
{
return xAmbientColor * xAmbientIntensity;
}


And this didn't work:

float4 PixelShader(VertexToPixel PSIn) : COLOR0
{
return saturate(xAmbientColor * xAmbientIntensity);
}


-EDIT-

When I added some diffuse lighting the saturate function worked perfectly for the diffuse lighting calculation while it still didn't work for ambient lighting. This is really strange and I hope you can help me with this problem.

The result must be store in a separate variable before return like

float4 c = saturate(xAmbientColor * xAmbientIntensity);

return c;

• Why does that have to done? – user9790 Feb 9 '13 at 10:32
• This also didn't work when I used saturate, again it worked without saturate. – eagle999 Feb 9 '13 at 11:34

I solved my problem:

float4 PixelShader(VertexToPixel PSIN) : COLOR
{
//calculation of ambientColor and diffuseColor without using saturate
return saturate(diffuseColor + ambientColor);
}


I don't know why, but this worked in my case. I hope I helped someone who is having the same problem. I think XNA can't handle saturate for calculations using only effect parameters.