I'm trying to optimize my terrain shader for my XNA game as it seems to consume a lot of ressources. It takes around 10 to 20 FPS on my computer, and my terrain is 512*512 vertices, so the PixelShader is called a lot of times.
I've seen that branching is using some ressources, and I have 3/4 conditions in my shaders. What could I do to bypass them? Are triadic operators more efficient than conditions?
For instance:
float a = (b == x) ? c : d;
or
float a;
if(b == x)
a = c;
else
c = d;
I'm using also multiple times the functions lerp and clamp, should it be more efficient to use arithmetic operations instead?
Here's the less efficient part of my code:
float fog;
if(FogWaterActivated && input.WorldPosition.y-0.1 < FogWaterHeight)
{
if(!IsUnderWater)
fog = clamp(input.Depth*0.005*(FogWaterHeight - input.WorldPosition.y), 0, 1);
else
fog = clamp(input.Depth*0.02, 0, 1);
return float4(lerp(lerp( output * light, FogColorWater, fog), ShoreColor, shore), 1);
}
else
{
fog = clamp((input.Depth*0.01 - FogStart) / (FogEnd - FogStart), 0, 0.8);
return float4(lerp(lerp( output * light, FogColor, fog), ShoreColor, shore), 1);
}
Thanks!
if
statements in a shader I wrote, and I got around20fps
. Then I removed theif
statements and used an alpha map, and got60fps
easy. I would try replacing myif
statements with maybe something like an alpha map, and see if that helps. (Or try commenting theif
statements out to see if there's a big difference.) – davidsbroFogWaterActivated
andIsUnderWater
global shader parameters? If so you could remove the conditionals and implement your different code paths as different shader techniques. – Cole Campbellfloat4(lerp(lerp( output * light, FogColorWater, fog), ShoreColor, shore), 1);
Do you think there's some kind of a trick to use only one for the same effect? – Adrien Neveu