Speed of cos() and sin() function in GLSL shaders?

前端 未结 6 1885
别那么骄傲
别那么骄傲 2021-01-17 10:51

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

The GLSL Specification Document indi

6条回答
  •  悲&欢浪女
    2021-01-17 11:46

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

    No.

    Your compiler will do one of two things.

    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.

    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.

    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.

提交回复
热议问题