1
\$\begingroup\$

I have a bunch of point lights and directional lights. Instead of checking the light type in the fragment shader and then branch for either point light calculation or directional light calculation, is it more efficient to use two separate programs, one for point lights and one for directional lights?

(Using deferred shading in OpenGL 3.3)

\$\endgroup\$
  • \$\begingroup\$ I'm only interested in the use two programs in deferred shading \$\endgroup\$ – KaiserJohaan Dec 4 '13 at 12:17
  • \$\begingroup\$ Should be fast to write it both ways and profile it on your target hardware, if you want to be 100% sure which performs better, or whether there's a meaningful difference for this use case. \$\endgroup\$ – DMGregory Dec 4 '13 at 13:35
3
\$\begingroup\$
  • Branching incurs overhead on GPU if the threads take different branches, which leads to serialisation. Thus, if all of your pixels take the same conditional branch, the overhead is negligible.
  • In addition, if the condition is complex, it can increase the number of registers used per thread which can impact performance depending on how much is this increment.
  • You should also consider the choice in a code maintainability perspective. If the use of two separate programs makes code clearer, more maintainable and with no code replication I would suggest this approach if the overhead considered at the aforementioned items is acceptable.
\$\endgroup\$
  • \$\begingroup\$ The way I figure it is to simply pass a int uniform in which 1=point light 2=directional, and simply if (lightType==1) CalcPosLight() else CalcDirectionalLight(), so the branching would be negligble? \$\endgroup\$ – KaiserJohaan Dec 4 '13 at 12:59
  • \$\begingroup\$ I'm supposing that your deferred shading is the usual: light volumes are rendered to verify which pixels are affected by them. In this case my guess is that the overhead is indeed negligible, since all pixels affected by one light source will take the same conditional branch. \$\endgroup\$ – dsilva.vinicius Dec 4 '13 at 13:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.