我有一个 C++ 中的 PointLight 结构
struct PointLight
{
glm::vec4 position; // 16 bytes
glm::vec4 color; // 16 bytes
float intensity; // 4 bytes rounded to 16 bytes?
float range; // 4 bytes rounded to 16 bytes?
};
在 ssbo 中的用法:
layout(std430, binding = 3) buffer lightSSBO
{
PointLight pointLight[];
};
由于 vec4s,float 元素会四舍五入到 16 字节吗? std430 是否总是将数组中的元素舍入为最大元素的大小?
我需要向结构添加填充才能正确访问变量吗?
struct PointLight
{
glm::vec4 position; // 16 bytes
glm::vec4 color; // 16 bytes
float intensity; // 4 bytes
float range; // 4 bytes
float padding[2]; // padding to make struct multiple of 16 bytes, because
// largest element vec4 is 16 bytes
// total 48 bytes
};
是的。 (参见 https://registry.khronos.org/OpenGL/specs/gl/glspec46.core.pdf#page=166&zoom=100,168,308)来自规则 9 和 10 的
std430
:如果成员是 S 数组结构,结构的基本对齐方式为 N,其中
N 是其任何成员中最大的碱基对齐值。对于 std140
,这将向上舍入为 vec4
的基本对齐方式;此规则不适用于std430
。但是,由于您的情况中最大的元素是 vec4
,因此在这种情况下没有区别。