constants - glsl 编译时除法 const

标签 constants glsl

此 GLSL 代码会根据“oneSixth”和“twoThirds”创建编译时常量吗?

// GLSL TESSELLATION EVALUATION SHADER
#version 410

layout (isolines, equal_spacing) in;
in vec4 tessColor[];
out vec4 pointColor;

const float oneSixth = 1. / 6.;
const float twoThirds = 2. / 3.;

void main ()
{
    float s2 = gl_TessCoord.s * gl_TessCoord.s;
    float s3 = s2 * gl_TessCoord.s;
    float w0 = oneSixth - .5 * gl_TessCoord.s + .5 * s2 - oneSixth * s3;
    float w1 = twoThirds - s2 + .5 * s3;
    float w2 = oneSixth + .5 * gl_TessCoord.s + .5 * s2 - .5 * s3;
    float w3 = oneSixth * s3;

    gl_Position = w0 * gl_in[0].gl_Position + w1 * gl_in[1].gl_Position +
            w2 * gl_in[2].gl_Position + w3 * gl_in[3].gl_Position;
    pointColor = w0 * tessColor[0] + w1 * tessColor[1] +
            w2 * tessColor[2] + w3 * tessColor[3];
}

我的一位同事认为这段代码效率低下,并说我应该对除法进行硬编码,否则它会在运行时发生。

const float oneSixth = .1666666667;
const float twoThirds = .6666666667;

我是 GLSL 的新手,但我怀疑这是否必要。有什么想法吗?是否依赖于供应商?

最佳答案

它会在编译时发生。无需像这样对琐碎的事情进行硬编码。但是,GLSL 规范中并未提及这一点。

关于constants - glsl 编译时除法 const,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10434903/

相关文章:

ios - 如何传递非插值数据 OpenGL ES (GLSL)

c++ - 在 OpenGL/GLSL 中绘制镶嵌的 LineLoops

ios - 在 iOS 上使用 OpenGL ES 2.0 进行实例化绘图

c++ - 确保const指针成员变量的constness

c++ - 可以将互斥锁功能标记为 const

ios - iOS 中的 Sampler3D

opengl - 聚光灯阴影贴图的计算偏差

C++ 对非 Const 对象的 Const 引用与对非 Const 对象的非 Const 引用之间的区别

c++ - 使用 const 参数调用 map::find

c++ - 在 C/C++ 中按位指定浮点常量值