GLSL着色器中cos()和sin()函数的速度? [英] Speed of cos() and sin() function in GLSL shaders?

查看:722
本文介绍了GLSL着色器中cos()和sin()函数的速度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Open GL Shader Language sin()cos()的速度信息感兴趣.

I'm interested in information about the speed of sin() and cos() in Open GL Shader Language.

GLSL规范文档表示:

内置函数基本上分为三类:

The built-in functions basically fall into three categories:

  • ...
  • ...
  • 它们表示一种操作图形硬件可能会在某个时候加速.三角函数属于 类别.
  • ...
  • ...
  • They represent an operation graphics hardware is likely to accelerate at some point. The trigonometry functions fall into this category.

正如已经指出的那样,对诸如sin()cos()之类的单个操作的时钟周期进行计数并不能真正说明整个性能问题.

As has been pointed out, counting clock cycles of individual operations like sin() and cos() doesn't really tell the whole performance story.

因此,为了澄清我的问题,我真正感兴趣的是,是否有必要针对常见情况优化sin()cos()调用.

So to clarify my question, what I'm really interested in is whether it's worthwhile to optimize away sin() and cos() calls for common cases.

例如,在我的应用程序中,参数通常为0.这样的话是否有意义:

For example, in my application it'll be very common for the argument to be 0. So does something like this make sense:

float sina, cosa;

if ( rotation == 0 )
{
   sina = 0;
   cosa = 1;
}
else
{
   sina = sin( rotation );
   cosa = cos( rotation );
}

还是GLSL编译器或sin()cos()实现对我来说像这样的优化?

Or will the GLSL compiler or the sin() and cos() implementations take care of optimizations like that for me?

推荐答案

例如,在我的应用程序中,参数通常为0是很常见的.

For example, in my application it'll be very common for the argument to be 0. So does something like this make sense:

否.

您的编译器将执行以下两项操作之一.

Your compiler will do one of two things.

  1. 它将发出实际的条件分支.在最好的情况下,如果0是局部一致的值(这样,着色器组通常会同时命中0或非零),那么您可能会获得更好的性能.
  2. 它将评估条件的双方,并且只存储其中正确的一方的结果.在这种情况下,您一无所获.
  1. It will issue an actual conditional branch. In the best possible case, if 0 is a value that is coherent locally (such that groups of shaders will often hit 0 or non-zero together), then you might get improved performance.
  2. It will evaluate both sides of the condition, and only store the result for the correct one of them. In which case, you've gained nothing.

通常,使用条件逻辑在这样的小型性能周围跳舞并不是一个好主意.它必须真的很大才能值钱,例如discard之类的东西.

In general, it's not a good idea to use conditional logic to dance around small performance like this. It needs to be really big to be worthwhile, like a discard or something.

此外,请注意,浮点对等不太可能起作用.除非您实际上将均匀包含0.0的均匀或顶点属性传递给着色器,否则就不会这样.即使在0到非零之间进行插值,也可能永远不会为任何片段精确地产生0.

Also, do note that floating-point equivalence is not likely to work. Not unless you actually pass a uniform or vertex attribute containing exactly 0.0 to the shader. Even interpolating between 0 and non-zero will likely never produce exactly 0 for any fragment.

这篇关于GLSL着色器中cos()和sin()函数的速度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆