shader if else performance

How expensive are if statements inside a fragment shader? How do they compare performance wise against a lerp?

I think it depends on the hardware but in general they are really bad. That’s because the GPU is a pipeline processor. It works best with a fix set of instructions on a large amount of data which is called SIMD(single instruction multiple data). See the Advantages / Disadvantages sections. Any kind of branching is inefficient because the pipeline has to be rebuild. Modern GPUs have a great branch prediction and can hold multiple pipeline versions but in most cases a lerp instruction is better :wink:

Hi - In your opinion how evil is a fragment program like this that has 2 returns based on a conditional? Will the GPU have to process both outputs?

float4 frag (vertex_output IN): COLOR {
if ( sunColor.z < 0.15){
		float4 stars = tex2D( starTexture,IN.uv.xy );
		stars *= 1 - saturate(sunColor.z * 4);
		return stars + IN.color * float4(Saturate(sunColor,0,1.5,1,0,0),1);
return IN.color * float4(Saturate(sunColor,0,1.5,1,0,0),1);

Hey I have a question to this Topic.

I have something in my head like a pixelbased multimaterial.
I want to seperate phongshader and labertshader by an pixelbased if calculation to save math operations.
for example one part of the model use Nomalmaps and specular and an otherpart dont use it.
i think to use vertex/fracment with an lerp Funktion.
in the end it isnt specular normal some more expensive per Pixel math.

is it better to lerp between this or make some per Pixel if calculation to save not visible math or just use a different lighting model?

thanks… my english isnt the best but i hope u get the Point.

Thanks Robin