问题描述
我想这是纯C ++问题和OpenGL问题之间的一种交叉.我有一个统一的缓冲区,并在其中分配了sizeof(ShaderData)个字节的空间.我在着色器的GPU端使用std140布局.
I suppose this is a kind-of cross between a pure C++ question and an OpenGL question. I have a uniform buffer and am allocating space in it of sizeof(ShaderData) bytes. I'm using std140 layout on the GPU side in the shader.
根据std140规则,我需要在结构的各个位置添加填充以确保矢量正确对齐.下面的结构是一个示例(针对我的观点):
According to std140 rules, I need to add padding in various places in my structure to make sure things like vectors are aligned correctly. The structure below is an example (for my light):
struct ShaderData {
float Light_Intensity;
float _pad1[3]; // align following vec3 on 4N boundary
Math::Vec3f Light_Position;
float _pad2; // align following vec4 on 4N boundary
Math::Colour4f Light_Ambient;
Math::Colour4f Light_Diffuse;
Math::Colour4f Light_Specular;
float Light_AttenuationMin;
float Light_AttenuationMax;
} MyShaderData;
这是人们通常在C ++中执行操作的方式吗?还是有一些特殊的关键字或实用注释来对齐结构CPU端的各个元素,使它们显得有些整洁?
Is this the way people generally do things in C++, or are there special keywords or pragmas for aligning individual elements of a structure CPU side that are a little tidier?
推荐答案
否,这样只会浪费空间.您必须根据std140规则找到优化的布局.
No, in this way you just waste space. You have to find the optimized layout according to std140 rules.
- 一个
float
需要4个字节,并且已对齐4个字节 - 一个
vec3
需要12个字节,并且已对齐16个字节 - 一个
vec4
需要16个字节,并且已对齐16个字节
- a
float
needs 4 bytes and it's 4 bytes aligned - a
vec3
needs 12 bytes and it's 16 bytes aligned - a
vec4
needs 16 bytes and it's 16 bytes aligned
这意味着您可以为您的结构找到更好的布局:
This means that you can find a better layout for your struct:
float Light_Intensity; X
float _pad1[3]; XXX
Math::Vec3f Light_Position; XXX
float _pad2; X
您可以看到您浪费了4个字节,更糟糕的是,您只能执行以下操作:
As you can see you are wasting 4 bytes and what's worse is the fact that you can just do something like:
Math::Vec3f Light_Position XXX
float Light_Intensity; X
使其对齐并且无需浪费字节.之所以可行,是因为vec3
将与16个字节的边界对齐,而float
将仍与4个字节的边界对齐.
To have it aligned and without the need to waste a byte. This works because vec3
will be aligned to 16 bytes boundaries while the float
will be still aligned to 4 bytes boundaries.
这篇关于将结构与std140,CPU端对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!