Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Historically speaking GPUs have supported per-fragment instructions such as <code>MIN</code> and <code>MAX</code> for much longer than they have supported arbitrary conditional branching. One example of this in desktop OpenGL is the <code>GL_ARB_fragment_program</code> extension (now superseded by GLSL) which explicitly states that it doesn't support branching, but it does provide instructions for <code>MIN</code> and <code>MAX</code> as well as some other conditional instructions.</p> <p>I'd be pretty confident that all GPUs will still have dedicated hardware for these operations given how common <code>min()</code>, <code>max()</code> and <code>clamp()</code> are in shaders. This isn't guaranteed by the specification because an implementation can optimize code however it sees fit, but in the real world you should use GLSL's built-in functions rather than rolling your own.</p> <p>The only exception would be if your conditional was being used to avoid a large amount of additional fragment processing. At some point the cost of a branch will be less than the cost of running all the code in the branch, but the balance here will be very hardware dependent and you'd have to benchmark to see if it actually helps in your application on its target hardware. Here's the kind of thing I mean:</p> <pre><code>void main() { vec3 N = ...; vec3 L = ...; float NDotL = dot(N, L); if (NDotL &gt; 0.0) { // Lots of very intensive code for an awesome shadowing algorithm that we // want to avoid wasting time on if the fragment is facing away from the light } } </code></pre> <p>Just clamping <code>NDotL</code> to 0-1 and then always processing the shadow code on every fragment only to multiply through your final shadow term by <code>NDotL</code> is a lot of wasted effort if <code>NDotL</code> was originally &lt;= 0, and we can theoretically avoid this overhead with a branch. The reason this kind of thing is not always a performance win is that it is very dependent on how the hardware implements shader branching.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload