Coolant
Coolant

Reputation: 448

bool condition opengl es 2.0 shader

Since it is recommended to not use condition in shader, which is better for a boolean uniform :

A. Create different shaders for different values of a boolean uniform ?

B. Create one shader and just use if-else in the code like this :

uniform bool uValue;
if (uValue) {
// code
} else {
// code
}

I have read somewhere that for uniform bool value, the driver will compile multiple shaders so that we don't have to bother creating multiple shaders. But I can't verify this.

Thanks!

Upvotes: 0

Views: 756

Answers (2)

Strings
Strings

Reputation: 1683

We all know that drivers change very quickly in GPU programming.

If your condition is fairly evenly balanced then there probably isn't a definitive right or wrong answer. It will depend on the hardware, the version of the drivers, and possible future mechanisms that the card itself uses to create parallel batches.

If your condition is more one sided, then there might be a real benefit using an if condition in one shader, or having two shaders and switching. Testing the load on the graphics card while it is processing real data is the only way to really answer this.

If this is identified as your bottleneck point and is worth the time investment, then perhaps include both, and choose at runtime. But remember there is no point in optimizing code if it won't make your shader faster. If you code delivers all of the requested visual features and you are still processor bound, then you have done your job.

Equally optimizing if statements, when you are fetch bound, doesn't make any sense. So keep all of your optimization until you have reached as many of the visual features as you can, then optimize, which might get you one more feature, then optimize again.

Upvotes: 0

rickster
rickster

Reputation: 126117

Which approach is more performant depends on a lot of other things:

  • How many conditions are you switching on?
  • How many times per frame are you switching?
  • How much computation happens on either side of your conditional?
  • What are the other performance constraints in your situation? Memory usage? Power consumption? Client-to-GPU bandwidth?

Try both options and test with Instruments to see which performs better in your case.

Upvotes: 2

Related Questions