【问题标题】:GLSL, literal constant Input Layout QualifiersGLSL,文字常量输入布局限定符
【发布时间】:2016-04-04 16:22:38
【问题描述】:

我想知道我是否可能有这样的事情:

layout (location = attr.POSITION) in vec3 position;

例如Attr 是一个常量结构

const struct Attr
{
    int POSITION;
} attr = Attr(0);

我已经试过了,但它抱怨

着色器状态无效:0(34):错误 C0000:语法错误,意外 整数常量,期望标识符或模板标识符或类型 标记“”处的标识符

或者如果没有结构体,我可以使用其他东西来声明文字输入限定符,例如attr.POSITION

【问题讨论】:

    标签: opengl struct glsl constants


    【解决方案1】:

    GLSL 没有const struct 声明之类的东西。但是它确实具有编译时间常数值:

    const int position_loc = 0;
    

    constant expressions 的规则说,用常量表达式初始化的 const 限定变量本身就是一个常量表达式。

    并没有规定这样的 const 限定变量的类型必须是基本类型:

    struct Attr
    {
      int position;
    };
    const Attr attr = {1};
    

    由于attr 是使用包含常量表达式的初始化列表进行初始化的,所以attr 本身就是一个常量表达式。这意味着attr.position 也是一个常量表达式,是整数类型。

    这样的编译时整数常量表达式可以在layout qualifiers 中使用,但前提是您使用的是 GLSL 4.40 或 ARB_ehanced_layouts:

    layout(location = attr.position) in vec3 position;
    

    在该版本之前,您需要使用实际文字。这意味着你能做的最好的就是#define

    #define position_loc 1
    layout(location = position_loc) in vec3 position;
    

    现在就个人而言,我永远不会依赖这种结构内积分常数表达式的体操。很少有人依赖它们,因此驱动程序代码很少以这种方式进行测试。因此遇到驱动程序错误的可能性相当大。 #define 方法在实践中更有可能奏效。

    【讨论】:

    • 好的,我会坚持#define
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-09
    • 1970-01-01
    • 1970-01-01
    • 2012-09-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多