计算着色器中的数组值无效?
我使用一个缓冲区来传递我的 C++ 结构
struct Node {
Node(int size, glm::ivec3 position);
bool isEmpty();
int getSubIndex(const glm::ivec3& vec);
void divide(std::vector<Node> &nodes);
void setColor(glm::vec4 color);
int getSubNodeIndex(const glm::ivec3& vec);
int getSubNodeIndex(int subIndex);
glm::ivec4 position;
glm::vec4 color;
int halfSize;
int sub;
int leaf;
};
在着色器中它看起来像这样
struct Node {
vec4 position;
vec4 color;
int data[3];
};
layout(std430, binding=4) readonly buffer Octree_data {
Node nodes[];
};
在计算过程中,我发现在数组的所有元素中(第一个元素除外)都有不正确的数据(很可能被替换)我可能会犯什么错误?
I use a buffer to which I pass my C++ structures
struct Node {
Node(int size, glm::ivec3 position);
bool isEmpty();
int getSubIndex(const glm::ivec3& vec);
void divide(std::vector<Node> &nodes);
void setColor(glm::vec4 color);
int getSubNodeIndex(const glm::ivec3& vec);
int getSubNodeIndex(int subIndex);
glm::ivec4 position;
glm::vec4 color;
int halfSize;
int sub;
int leaf;
};
In the shader it looks like this
struct Node {
vec4 position;
vec4 color;
int data[3];
};
layout(std430, binding=4) readonly buffer Octree_data {
Node nodes[];
};
In the process of calculations, I find out that in all elements of the array (except for the first element) there are incorrect data (most likely displaced) in what could I make a mistake?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Node
结构所需的std430
对齐方式是 16 字节。这是因为它包含 16 字节对齐类型(vec4
和ivec4
)。因此,Node
数组中的每个数组元素都必须从 16 字节边界开始。因此,nodes
的数组步长必须为 48。Node
结构的 C++ 对齐方式可能是 4 个字节。这是因为 在 C++ 布局中,您的Node
结构不需要更高的对齐方式。 GLM 的 4 元素向量类型是 4 字节对齐的(或者更确切地说,它们是浮点对齐的,几乎总是 4 字节)。这意味着sizeof(Node)
将为 44 字节,数组步长也是 44 字节。如果您希望 C++ 结构体匹配 GLSL 所需的布局,则需要正确对齐它:
The
std430
required alignment for yourNode
structure is 16-bytes. This is because it contains a 16-byte-aligned type (vec4
andivec4
). Therefore, every array element in theNode
array will have to start at a 16-byte boundary. So the array stride fornodes
will have to be 48.The C++ alignment of your
Node
structure is probably 4 bytes. This is because nothing in C++'s layout for yourNode
structure requires higher alignment. GLM's 4-element vector types are 4-byte aligned (or rather, they'refloat
aligned, which is almost always 4 bytes). This means that thesizeof(Node)
will be 44 bytes, as will the array stride.If you want your C++ struct to match the GLSL required layout, you need to align it properly: