简体   繁体   English

OpenGL / GLSL - 统一块数据值不正确

[英]OpenGL / GLSL - Uniform block data values incorrect

My shader has a uniform block as such: 我的着色器有一个统一的块:

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

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);

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). 根据我的理解,这应该将着色器中块内的“颜色”设置为(0,0,1),但我得到的值是(0,1,0)。

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. 如果我从块中删除'test'变量并仅将三个浮点数(0,0,1)绑定到着色器,它将按预期工作。

What's going on? 这是怎么回事?

As you did specify layout (std140) for your UBO, you must obey the alginment rules defined there. 正如您为UBO指定layout (std140) ,您必须遵守那里定义的alginment规则。 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": 该布局首先在OpenGL 3.2核心规范中指定(核心),第2.11.4节“统一变量”在“标准统一块布局”小节中:

  1. If the member is a scalar consuming N basic machine units, the base alignment is N. 如果该成员是消耗N个基本机器单元的标量,则基本对齐为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. 如果该成员是具有消耗N个基本机器单元的组件的双组分或四组分向量,则基础对齐分别为2N或4N。
  3. If the member is a three-component vector with components consuming N basic machine units, the base alignment is 4N. 如果该成员是具有消耗N个基本机器单元的组件的三分量向量,则基本对齐为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. 如果成员是标量或向量的数组,则根据规则(1),(2)和(3)将基本对齐和数组步长设置为匹配单个数组元素的基本对齐,并向上舍入到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). 如果该成员是具有C列和R行的列主矩阵,则根据规则(4),矩阵与C列向量的数组相同地存储,每个R列组件具有R个组件。
  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 SC column vectors with R components each, according to rule (4). 如果该成员是具有C列和R行的S列主要矩阵的数组,则根据规则(4),矩阵与SC列向量的行相同地存储,每个R列具有R个分量。
  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). 如果该成员是具有C列和R行的行主矩阵,则根据规则(4),矩阵与R行向量的数组相同地存储,每个R行向量具有C个分量。
  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 SR row vectors with C components each, according to rule (4). 如果该成员是具有C列和R行的S行主矩阵的数组,则根据规则(4),矩阵与每行具有C分量的SR行向量的行相同地存储。
  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. 如果该成员是结构,则该结构的基本对齐是N,其中N是其任何成员的最大基本对齐值,并向上舍入到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). 如果成员是S结构的数组,则根据规则(9),按顺序排列数组的S元素。

For your case, point 3 applies. 对于您的情况,第3点适用。 So, you need to pad another float before the second vector begins. 因此,您需要在第二个向量开始之前填充另一个浮点数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM