GLSL,文字常量输入布局限定符

GLSL, literal constant Input Layout Qualifiers

我想知道我是否可能有这样的东西:

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

其中例如 Attr 是常量结构

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

我已经试过了,但它抱怨

Shader status invalid: 0(34) : error C0000: syntax error, unexpected integer constant, expecting identifier or template identifier or type identifier at token ""

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

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 方法在实践中更有可能起作用。