Constant float values in GLSL shaders - any reason to use uniforms?

前端 未结 2 1874
天涯浪人
天涯浪人 2021-02-13 00:43

I\'m looking at the source of an OpenGL application that uses shaders. One particular shader looks like this:

uniform float someConstantValue;
void main()
{
             


        
2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-13 01:31

    First off, the performance difference between using a uniform or a constant is probably negligible. Secondly, just because a value is always constant in nature doesn't mean that you will always want it be constant in your program. Programmers will often tweak physical values to produce the best looking result, even when that doesn't match reality. For instance, the acceleration due to gravity is often increased in certain types of games to make them more fast paced.

    If you don't want to have to set the uniform in your code you could provide a default value in GLSL:

    uniform float someConstantValue = 12.5;
    

    That said, there is no reason not to use const for something like pi where there would be little value in modifying it....

提交回复
热议问题