本文介绍了OpenGL / GLSL - 统一块数据值不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的着色器有一个统一的块如下:

My shader has a uniform block as such:

layout (std140) uniform LightSourceBlock
{
    vec3 test;
    vec3 color;
} LightSources;

这个块的数据应该来自一个缓冲区对象, p>

The data for this block is supposed to come from a buffer object which is created like so:

GLuint buffer;
glGenBuffers(1,&buffer);

GLfloat data[6] = {
    0,0,0,
    0,0,1
};
glBindBuffer(GL_UNIFORM_BUFFER,buffer);
glBufferData(GL_UNIFORM_BUFFER,sizeof(data),&data[0],GL_DYNAMIC_DRAW);

缓冲区在渲染之前链接到统一块:

The buffer is linked to the uniform block before rendering:

unsigned int locLightSourceBlock = glGetUniformBlockIndex(program,"LightSourceBlock");
glUniformBlockBinding(program,locLightSourceBlock,8);
glBindBufferBase(GL_UNIFORM_BUFFER,8,buffer);



从我的理解,这应该是在着色器块中设置'color' ,1),但是我得到的值是(0,1,0)。

From my understanding this should be setting 'color' inside the block in the shader to (0,0,1), but the value I'm getting instead is (0,1,0).

如果我从块中删除'test'变量,

If I remove the 'test' variable from the block and only bind the three floats (0,0,1) to the shader, it works as intended.

发生了什么事?

推荐答案

正如你为你的UBO指定 layout(std140),你必须遵守那里定义的海藻规则。该布局首先在 OpenGL 3.2核心规范中指定(在核心中) a>,第2.11.4节标准统一块布局中的统一变量:

As you did specify layout (std140) for your UBO, you must obey the alginment rules defined there. That layout was first specified (in core) in the OpenGL 3.2 core spec, section 2.11.4 "Uniform Variables" in subsection "Standard Uniform Block Layout":


  1. 成员是消耗N个基本机器单元的标量,基本对齐是N。

  2. 如果成员是具有消耗N个基本机器单元的组件的两分量或四分量向量,

  3. 如果成员是具有消耗N个基本机器单位的组件的三分量向量,则基准对齐为4N。

  4. 如果成员是标量或向量数组,则根据规则(1),(2),基准对齐和数组跨度被设置为匹配单个数组
    元素的基准对齐)和(3),并向上舍入到vec4的
    基本对齐。该数组在末尾可能有填充;

  5. 如果成员是一个列主矩阵,则该数组后面的成员的
    基本偏移被向上取整为基数对齐的
    的下一个倍数。 C列和R行,根据规则(4),矩阵与具有
    R个分量的C列向量的数组相同地存储。

  6. 如果成员是根据规则(4),具有C列和R行的S列主矩阵的阵列与具有R分量的SC列
    向量的行相同地存储。

  7. 如果成员是具有C列和R行的行主矩阵,则根据规则(4),矩阵被存储为与具有C
    个分量的R个行向量的数组相同。

  8. 如果成员是具有C列和R行的S行主矩阵的数组,则矩阵与具有C个分量的SR行
    向量的行相同地存储,根据

  9. 如果成员是结构,则结构的基准线对齐为N,其中N是其任何
    成员的最大基准线对齐值,并向上舍入到vec4的基本对齐。该子结构的
    个体成员然后被赋予偏移量
    ,以递归方式应用该组规则,其中子结构的
    第一成员的基本偏移量等于
    的结构。结构可以在末端具有填充;

  10. 如果成员是一个结构体,则结构体的基本对齐结构的下一个多个结构体的基础
    偏移量。

  1. If the member is a scalar consuming N basic machine units, the base alignment is N.
  2. If the member is a two- or four-component vector with components consuming N basic machine units, the base alignment is 2N or 4N, respectively.
  3. If the member is a three-component vector with components consuming N basic machine units, the base alignment is 4N.
  4. If the member is an array of scalars or vectors, the base alignment and array stride are set to match the base alignment of a single array element, according to rules (1), (2), and (3), and rounded up to the base alignment of a vec4. The array may have padding at the end; the base offset of the member following the array is rounded up to the next multiple of the base alignment.
  5. If the member is a column-major matrix with C columns and R rows, the matrix is stored identically to an array of C column vectors with R components each, according to rule (4).
  6. If the member is an array of S column-major matrices with C columns and R rows, the matrix is stored identically to a row of S C column vectors with R components each, according to rule (4).
  7. If the member is a row-major matrix with C columns and R rows, the matrix is stored identically to an array of R row vectors with C components each, according to rule (4).
  8. If the member is an array of S row-major matrices with C columns and R rows, the matrix is stored identically to a row of S R row vectors with C components each, according to rule (4).
  9. If the member is a structure, the base alignment of the structure is N, where N is the largest base alignment value of any of its members, and rounded up to the base alignment of a vec4. The individual members of this substructure are then assigned offsets by applying this set of rules recursively, where the base offset of the first member of the sub-structure is equal to the aligned offset of the structure. The structure may have padding at the end; the base offset of the member following the sub-structure is rounded up to the next multiple of the base alignment of the structure.
  10. If the member is an array of S structures, the S elements of the array are laid out in order, according to rule (9).



$ b的数组,按照规则(9) $ b

对于您的情况,第3点适用。所以,你需要在第二个向量开始前填充另一个float。

For your case, point 3 applies. So, you need to pad another float before the second vector begins.

这篇关于OpenGL / GLSL - 统一块数据值不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!