Still wrapping my head around parallel processing and optimizing some code… from what I understand, it’s generally good practice to avoid conditionals and branching in shaders. No “if” statements and “for” loops whenever possible…
My question is this. Most fragment shader examples do something like
vec4 color = drawA( );
if (color.w > 0. ) color = drawB();
assuming color.w will only be 0 or 1, would it be more efficient to do something like this?
vec4 color = drawA();
color = color.w * drawB();
the first method seems like it wouldn’t be calling drawB() as much but it uses an if statement. The second one avoids the if statement, but it seems like it would have to compute more pixels and then erases them when multiplying by zero.
this leads to the next question. lets say instead of
drawAll( vec2 uv , int num){
for(int i = 0; i<3; i++){
if( i == num) color = drawA(uv);
if( i == num) color = drawB(uv);
if( i == num) color = drawC(uv);
}
}
what if I just offset the UV by the input num and have it draw out of frame unless the num matches the offset?
vec3 drawB(vec2 uv){
uv.y -= 1.; //-offset uv to match num
return vec3( vec3(0.5) ,1.);
}
vec3 drawC(vec2 uv){
uv.y -= 2.; //-offset uv to match num
return vec3(1.0);
}
drawAll( vec2 uv , int num){
uv.y += num; //offset uv to match draw offset
vec4 color = drawUVMask();
color.rgb += drawA(uv) * color.w; //mult by color.w or use an if statement?
color.rgb += drawB(uv)* color.w;
color.rgb += drawC(uv)* color.w;
}
this method would have to mask the draw functions somehow using one of the first two methods mentioned. I used the multiplication example just to show how these questions came about. Am I just worrying about conditionals too much?