如何在 GLSL 4.6 中设置统一结构
How to set a uniform struct in GLSL 4.6
我在一个结构中有一组相关的变量,每个结构成员都是基本类型。我声明了该结构类型的制服,并想在我的着色器中使用 foo.a
、foo.b
等。
struct Foo {
vec3 a;
vec4 b;
bool c;
mat3 d;
uint e;
};
layout(location = 1001) uniform Foo foo;
文档说 foo
占用 5 个统一位置,所以我假设我可以像这样简单地设置统一值?
unsigned int e = 1;
glUniform1i(1005, e); // e corresponds to uniform location 1005
// do sth with foo.e
我的问题是,我是否必须在此处明确查询统一位置?我需要担心内存布局和填充问题吗?我使用结构的目的是以更简洁的方式对这些变量进行分组,但我不想显式查询统一位置。
我知道如果变量对所有着色器程序都是全局的,那么使用具有 std140
布局的统一缓冲区对象会更容易。不过,由于UBOs在OpenGL中非常珍贵,我不想在这里浪费它,我宁愿保留UBOs作为光照输入。是否可以只使用原始统一结构? (precious我的意思是与宽松的制服相比,可用的UBO数量相对较少,显卡通常最多只支持15或16个UBO)
对于结构,统一位置是按顺序和升序排列的。见 4.4.3. Uniform Variable Layout Qualifiers:
Locations can be assigned to default-block uniform arrays and structures. The first inner-most scalar, vector or matrix member or element takes the specified location and the compiler assigns the next inner-most member or element the next incremental location value.
我在一个结构中有一组相关的变量,每个结构成员都是基本类型。我声明了该结构类型的制服,并想在我的着色器中使用 foo.a
、foo.b
等。
struct Foo {
vec3 a;
vec4 b;
bool c;
mat3 d;
uint e;
};
layout(location = 1001) uniform Foo foo;
文档说 foo
占用 5 个统一位置,所以我假设我可以像这样简单地设置统一值?
unsigned int e = 1;
glUniform1i(1005, e); // e corresponds to uniform location 1005
// do sth with foo.e
我的问题是,我是否必须在此处明确查询统一位置?我需要担心内存布局和填充问题吗?我使用结构的目的是以更简洁的方式对这些变量进行分组,但我不想显式查询统一位置。
我知道如果变量对所有着色器程序都是全局的,那么使用具有 std140
布局的统一缓冲区对象会更容易。不过,由于UBOs在OpenGL中非常珍贵,我不想在这里浪费它,我宁愿保留UBOs作为光照输入。是否可以只使用原始统一结构? (precious我的意思是与宽松的制服相比,可用的UBO数量相对较少,显卡通常最多只支持15或16个UBO)
对于结构,统一位置是按顺序和升序排列的。见 4.4.3. Uniform Variable Layout Qualifiers:
Locations can be assigned to default-block uniform arrays and structures. The first inner-most scalar, vector or matrix member or element takes the specified location and the compiler assigns the next inner-most member or element the next incremental location value.