Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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);


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.

share|improve this question

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

float4 c = saturate(xAmbientColor * xAmbientIntensity);

return c;

share|improve this answer
Why does that have to done? – flai 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
up vote 0 down vote accepted

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.