我想知道我是否会有这样的事情:
layout (location = attr.POSITION) in vec3 position;
例如Attr
是一个常数结构
const struct Attr
{
int POSITION;
} attr = Attr(0);
我已经尝试了,但它抱怨
着色器状态无效:0(34):错误C0000:语法错误,意外 整数常量,期望标识符或模板标识符或类型 令牌“”
的标识符
或者如果结构没有办法,我可以使用别的东西来声明文字输入限定符,例如attr.POSITION
吗?
答案 0 :(得分:2)
GLSL has no such thing as a const struct
declaration. It does however have compile time constant values:
const int position_loc = 0;
The rules for constant expressions say that a const-qualified variable which is initialized with a constant expression is itself a constant expression.
And there ain't no rule that says that the type of such a const-qualified variable must be a basic type:
struct Attr
{
int position;
};
const Attr attr = {1};
Since attr
is initialized with an initialization list containing constant expressions, attr
is itself a constant expression. Which means that attr.position
is an constant expression too, one of integral type.
And such a compile-time integral constant expression can be used in layout qualifiers, but only if you're using GLSL 4.40 or ARB_ehanced_layouts:
layout(location = attr.position) in vec3 position;
Before that version, you'd be required to use an actual literal. Which means the best you could do would be a #define
:
#define position_loc 1
layout(location = position_loc) in vec3 position;
Now personally, I would never rely on such integral-constant-expression-within-struct gymnastics. Few people rely on them, so driver code rarely gets tested in this fashion. So the likelihood of encountering a driver bug is fairly large. The #define
method is far more likely to work in practice.