I know that branching is an expensive operation on GPU (not as much as it used to be, but still).
The most common situation where I use branching is when I have both textured and non-textured models/objects rendered with single shader (switching between shaders is also quite expensive and it only differs in 1-2 small parts).
For example, here’s some pixel shader for rendering 2D objects:
constbuffer constBufferPerObject{
float4 textureCoordORColor; //hasTexture is true => it's texCoord(x,y), color otherwise
bool hasTexture;
};
...
float4 PS(VS_OUTPUT input) : SV_TARGET{
if(hasTexture){
//input.TexCoord was calculated in VS (it's meaningless if hasTexture is false)
return ObjTexture.Sample(ObjSamplerState, input.TexCoord);
}else{
return textureCoordORColor;
}
}
What would be a smart way to re-write the PS
in order to avoid branching here?