opengl - 如何在SSBO中设置数组对齐方式

标签 opengl glsl shader

在计算着色器中,我有一个粒子结构:

struct Particle {
    float delta;
    float start_posx;
    float start_posy;
    float start_posz;
    float restart;
    uint max_textures;
    float max_lifetime;
};

现在我想在 SSBO 中使用这个结构:

layout (std430, binding = 12) buffer particleBlock {
    Particle particles[];
};

我假设 ssbo 中的粒子是紧密堆积的,因为所有结构成员的大小均为 4。如果我将对齐设置为 16:

layout (std430, binding = 12) buffer particleBlock {
    layout(align = 16) Particle particles[];
};

粒子[1]会在偏移量32处吗?我不确定这种对齐方式如何适用于数组,这似乎对我不起作用。

最佳答案

单个数组元素无法通过 align 限定符对齐。只能对齐数组的开头。

GLSL - The OpenGL Shading Language 4.6; 4.4.5. Uniform and Shader Storage Block Layout Qualifiers; page 85 :

The align qualifier makes the start of each block member have a minimum byte alignment. It does not affect the internal layout within each member, which will still follow the std140 or std430 rules.
...
When align is applied to an array, it affects only the start of the array, not the array’s internal stride.

如果您使用 std140 限定符,则结构体 Particle 的数组的步幅将为 32,因为步幅向上舍入为基数的倍数vec4 的对齐方式,即 16。

请注意,std140std430 标准之间的主要区别是,在以下情况下,数组元素的跨度不会四舍五入为 16 字节的倍数:标准430。

如果结构体Particle的大小应为32字节,那么您可以在末尾添加一个虚拟成员:

struct Particle {
    float delta;        //  0
    float start_posx;   //  4
    float start_posy;   //  8
    float start_posz;   // 12
    float restart;      // 16
    uint  max_textures; // 20
    float max_lifetime; // 24
    float dummy;        // 28: dummy member for the alignment
};

关于opengl - 如何在SSBO中设置数组对齐方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52328129/

相关文章:

python - glGetAttribLocation 导致无效操作

opengl - glsl sampler2DShadow 和 shadow2D 说明

c++ - GLSL 构建错误

无法使纹理透明

windows - wglext - 扩展未安装在 OpenGL 上下文中

c++ - 现代 OpenGL 投影 View 模型转换不起作用

c++ - glutSpecialFunc 只捕获一些键

opengl - 开放着色器语言和 GLSL 之间的主要区别是什么

shader - 如何使用shader绘制FBO lwjgl

c++ - 使用顶点着色器确定线方向