GLSL : branching is pretty much expensive, isn't it?

Started by
6 comments, last by Ingrater 15 years, 10 months ago
In my fragment program I used an if/else branches to skip 8 dot products depending on some values. But I was surprised to see a huge gain in performance when I removed the if/else branches so the dot products are always performed... Should I use branching only skip very expensive computations? I have a fairly old hardware , GeForce 6600. Does this apply to new videocards too? What about ATI videocards?
Advertisement
Yes :(

NVidia says that the recommended maximum use for dynamic branching should be a little portion of pixel such 18x18 or something similar...

I've an 8500 video NVidia card.
Wait, branching == if/else statements(to put it short)?!

Yeah, if/else statements don't always seem to be so fast on the gpu.
Branching is not so expensive, but different fragments in a group (usually 3x3 groups) need to be coherent between each them, as threads that processes them need to take the same branch in the fragment group. If not, both branches are executed anyway, and the correct one taken for each fragment, which is expensive and might affect performance.

Also, a GeForce 8 might speed up, as better branching was implemented. nVidia was sucky in branching before the GeForce 8 came out.
Branching can be very slow or very fast, depending on how you use it. As has been said, coherence between nearby fragments is very important to get it fast. And the number of branching should obviously be kept to a minimum.

Now, you can gain a lot from branching, if you use it to skip very expensive but seldom used code parts. Skipping a dot product, however, doesn't make a lot of sense, because a dot product is a very, very fast operation. So you introduce a lot of overhead while gaining very little from skipping an already highly optimized operation.

Oh and on the subject of branching - keep in mind that mipmapped texture access within a conditional branch is very often undefined, unless you manually supply the derivatives !
Just one question

Is it better a product without sense (let's say color*1.0) that a branching to avoid it? I mean, their cost on the gpu?

Thnaks.
I've seen things you people wouldn't believe. Attack ships on fire off the shoulder of Orion. I watched C-beams glitter in the dark near the Tannhauser gate. All those moments will be lost in time, like tears in rain. Time to die.
The single product is a very cheap operation and it doesn’t make sense to use branching (that can have a huge impact on performance) to avoid it.
In the Nvidia cascades demo secrets paper, nvidia even avoids braching in their parallax occlusion shader. Tested around with this, and I came to the conclusion that you only should use brachning to avoid very expensive computations, elsewhere avoid it.
http://3d.benjamin-thaut.de

This topic is closed to new replies.

Advertisement