【问题标题】:Not able to understand alignment rules of OpenGL SSBOs [duplicate]无法理解 OpenGL SSBO 的对齐规则 [重复]
【发布时间】:2020-05-18 21:24:07
【问题描述】:

我在 glsl SSBO 中有以下结构数组:

struct node {

  vec3 min, max;

  int hitNext;   
  int missNext;    
  int firstTri, numTris;
};
layout(std430, binding = 3) readonly buffer Nodes {
  node[] nodes;
};

我正在向他们传递一个字节缓冲区,我在其中存储了以下格式的数据:

4 bytes min.x, 4 bytes min.y, 4 bytes min.z, 4 bytes 1.0f,
4 bytes max.x, 4 bytes max.y, 4 bytes max.z, 4 bytes 1.0f,
4 bytes hitNext, 4 bytes missNext, 4 bytes firstTrue, 4 bytes numTris

但是 glsl 没有正确读取数据。如果我将结构定义中的vec3 更改为vec4,它将完美运行。但为什么会这样呢?根据 OpenGL 规范,这不是对齐方式吗?:

struct node {               //Base alignment    Aligned Offset
  vec3 min;                 //  16                  0

  vec3 max;                 //  16                  16

  int hitNext;              //  4                   32

  int missNext;             //  4                   36

  int firstTri, numTris;    //  4                   40
                            //  4                   44
};
layout(std430, binding = 3) readonly buffer Nodes {
  node[] nodes;             //  16                  48
};

但是这种对齐方式与我传入的数据相匹配。为什么这不起作用但vec4 起作用?

【问题讨论】:

    标签: opengl glsl


    【解决方案1】:
    struct node {               //Base alignment    Aligned Offset   
      vec3 min;                 //  16                  0
      vec3 max;                 //  16                  16
      int hitNext;              //  4                   32
    

    不,规范不是这么说的。 vec3 可能只需要 16 个字节的对齐,但它不需要消耗 16 个字节。由于它后面的元素的基本对齐方式仅为 4,因此我们最终得到了 28 的对齐偏移量 hitNext

    这只是this question 的另一个实例。

    【讨论】:

      猜你喜欢
      • 2021-02-11
      • 2018-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-02
      相关资源
      最近更新 更多