title: Efficiency of branching in shaders
body: <p>I understand that this question may seem somewhat ungrounded, but <em>if someone knows anything theoretical / has practical experience</em> on this topic, it would be great if you share it.</p>
  
  <p>I am attempting to optimize one of my old shaders, which uses a lot of texture lookups.</p>
  
  <p><em>I've got diffuse, normal, specular maps</em> for each of three possible mapping planes and for some faces which are near to the user I also have to apply mapping techniques, which also bring a lot of texture lookups (like <code>parallax occlusion mapping</code>).</p>
  
  <p>Profiling showed that texture lookups are the bottleneck of the shader and I am willing to remove some of them away. For <em>some cases of the input parameters</em> I already know that part of the texture lookups would be unnecessary and the <strong>obvious</strong> solution is to do something like <em>(pseudocode)</em>:</p>
  
  <pre><code>if (part_actually_needed) {
     perform lookups;
     perform other steps specific for THIS PART;
  }
  
  // All other parts.
  </code></pre>
  
  <p><em>Now - here comes the question.</em></p>
  
  <p>I do not remember exactly (that's why I stated the question might be <strong>ungrounded</strong>), but in some paper I recently read <em>(unfortunately, can't remember the name)</em> something similar to the following was stated:</p>
  
  <blockquote>
    <p>The <em>performance</em> of the presented
    technique depends on how efficient
    the <strong><em>HARDWARE-BASED CONDITIONAL
    BRANCHING</em></strong>  is implemented.</p>
  </blockquote>
  
  <p>I remembered this kind of statement right before I was about to start refactoring a big number of shaders and implement that <em><code>if</code>-based optimization</em> I was talking about.</p>
  
  <p>So - <em>right before I start doing that</em> - does someone know something about the efficiency of the branching in shaders? Why could branching give a severe performance penalty in shaders? </p>
  
  <p><strong><em>And is it even possible that I could only worsen the actual performance with the <code>if</code>-based branching?</em></strong></p>
  
  <hr>
  
  <p>You might say - <strong>try and see.</strong> Yes, that's what I'm going to do if nobody here is helps me :)</p>
  
  <p>But still, what in the <code>if</code> case may be effective for new GPU's could be a nightmare for a bit older ones. <em>And that kind of issue is very hard to forecast unless you have a lot of different GPU's (that's not my case)</em></p>
  
  <p><em>So, if anyone knows something about that or has benchmarking experience for these kinds of shaders, I would really appreciate your help.</em></p>
  
  <hr>
  
  <p>Few remaining brain cells that are actually working keep telling me that branching on the GPU's might be far not as effective as branching for the CPU (which usually has extremely efficient ways of branch predictions and eliminating cache misses) simply because it's a GPU (or that could be hard / impossible to implement on the GPU).</p>
  
  <p><em>Unfortunately I am not sure if this statement has anything in common with the real situation...</em></p>