ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Branch in GPU

2022-05-31 11:03:55  阅读:208  来源: 互联网

标签:branching uniform hardware will Branch GPU wavefront compiler


What is it about shaders that even potentially makes if statements performance problems?
It has to do with how shaders get executed and where GPUs get their massive computing performance from.

Separate shader invocations are usually executed in parallel,
executing the same instructions at the same time.
They're simply executing them on different sets of input values; they share uniforms, but they have different internal registers.
One term for a group of shaders all executing the same sequence of operations is wavefront.

The potential problem with any form of conditional branching is that it can screw all that up.
It causes different invocations within the wavefront to have to execute different sequences of code.
That is a very expensive process, whereby a new wavefront has to be created, data copied over to it, etc.

Unless... it doesn't.

For example, if the condition is one that is taken by every invocation in the wavefront,(选择同一分支) then no runtime divergence is needed.
As such, the cost of the if is just the cost of checking a condition.

So, let's say you have a conditional branch, and let's assume that
all of the invocations in the wavefront will take the same branch.
There are three possibilities for the nature of the expression in that condition:

  • Compile-time static. The conditional expression is entirely based off of compile-time constants.
    As such, you know from looking at the code which branches will be taken.
    Pretty much any compiler handles this as part of basic optimization.
  • Statically uniform branching. The condition is based off of expressions involving things
    which are known at compile-time to be constant (specifically, constants and uniform values).
    But the value of the expression will not be known at compile-time.
    So the compiler can statically be certain that wavefronts will never be broken by this if,
    but the compiler cannot know which branch will be taken.
  • Dynamic branching. The conditional expression contains terms other than constants and uniforms.
    Here, a compiler cannot tell a priori if a wavefront will be broken up or not.
    Whether that will need to happen depends on the runtime evaluation of the condition expression.

Different hardware can handle different branching types without divergence.

Also, even if a condition is taken by different wavefronts, the compiler could restructure the code to not require actual branching.
You gave a fine example:

output = input*enable + input2*(1-enable); 

is functionally equivalent to the if statement.
A compiler could detect that an if is being used to set a variable, and thus execute both sides.
This is frequently done for cases of dynamic conditions where the bodies of the branches are small.

Pretty much all hardware can handle var = bool ? val1 : val2 without having to diverge. This was possible way back in 2002.

Since this is very hardware-dependent, it... depends on the hardware. There are however certain epochs of hardware that can be looked at:

Desktop, Pre-D3D10

There, it's kinda the wild west. NVIDIA's compiler for such hardware was notorious for
detecting such conditions and actually recompiling your shader whenever you changed uniforms that affected such conditions.

In general, this era is where about 80% of the "never use if statements" comes from.
But even here, it's not necessarily true.

You can expect optimization of static branching.
You can hope that statically uniform branching won't cause any additional slowdown
(though the fact that NVIDIA thought recompilation would be faster than executing it
makes it unlikely at least for their hardware).
But dynamic branching is going to cost you something, even if all of the invocations take the same branch.

Compilers of this era do their best to optimize shaders so that simple conditions can be executed simply.
For example, your output = input*enable + input2*(1-enable); is something that a decent compiler
could generate from your equivalent if statement.

Desktop, Post-D3D10

Hardware of this era is generally capable of handling statically uniform branches statements with little slowdown.
For dynamic branching, you may or may not encounter slowdown.

Desktop, D3D11+

Hardware of this era is pretty much guaranteed to be able to handle dynamically uniform conditions with little performance issues.
Indeed, it doesn't even have to be dynamically uniform;
so long as all of the invocations within the same wavefront take the same path, you won't see any significant performance loss.

Note that some hardware from the previous epoch probably could do this as well. But this is the one where it's almost certain to be true.

Mobile, ES 2.0

Welcome back to the wild west. Though unlike Pre-D3D10 desktop,
this is mainly due to the huge variance of ES 2.0-caliber hardware.
There's such a huge amount of stuff that can handle ES 2.0, and they all work very differently from each other.

Static branching will likely be optimized.
But whether you get good performance from statically uniform branching is very hardware-dependent.

Mobile, ES 3.0+

Hardware here is rather more mature and capable than ES 2.0.
As such, you can expect ·statically uniform branches· to execute reasonably well.
And some hardware can probably handle dynamic branches the way modern desktop hardware does.

https://stackoverflow.com/questions/37827216/do-conditional-statements-slow-down-shaders

标签:branching,uniform,hardware,will,Branch,GPU,wavefront,compiler
来源: https://www.cnblogs.com/Searchor/p/16329645.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有