c++ - 如何在 OpenGL 中为 std430 布局正确填充和对齐数据

标签 c++ opengl glsl

在着色器存储对象中使用以下结构时,我无法使其工作:

着色器中的定义:

struct Object
{
    vec4                color;
    mat3                transform;
    float               depth;
    float               pObjIndex;
    //float align1;
    //float align2;
};

layout (std430, binding = 0) buffer Objects
{
  Object objects[];
};

据我了解,对象结构在 GLSL 中按其最大成员 vec4 对齐,即按 16 字节边界对齐。 (mat3 被视为 vec3 的 3 元素数组,并按 12 字节边界对齐)。 vec4、mat3 和 2 个 float 提供 72 个字节。当我用两个 float 将结构填充到最接近的 16 字节倍数 - 80 - 时,它无法正常工作,就像没有填充一样。

此结构布局的两种变体 - 填充有 2 个 float 和没有任何填充 - 是否不正确?

最佳答案

mat3 实际上是 3 个 vec4,因为每个 vec3 都是 3 个对齐到 16 字节的 float (或者至少它可能不会跨越 16 字节边界)

struct Object
{
    float[4]           color;
    float[4][3]         transform;
    float           depth;
    float           pObjIndex;
    //float align1;
    //float align2;
};

关于c++ - 如何在 OpenGL 中为 std430 布局正确填充和对齐数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23628259/

相关文章:

c++ - boost::bind 是否有 QPointer 特化

vb.net - OpenGL:glDrawElements 给出未处理的异常:System.AccessViolationException

opengl - 在 Cg 中访问 OpenGL 状态变量

java - FBO 纹理显示全黑

c++ - 右值和左值引用的重载函数

c++ - _Longlong 未在此范围内声明

c++ - OpenGL : GLSL float has low precision

ios - 在着色器修改器 SceneKit 中访问世界空间中的 z 坐标

c++ - 游戏菜单不工作 C++

c++ - 如何用glm旋转3D相机