-
1、原因:
如果你的程序中包含了多个着色器,而且这些着色器使用了相同的Uniform变量,你就不得不为每个着色器分别管理这些变量。Uniform变量的location是在程序链接的时候产生的,因此Uniform变量的location会随着着色器的不同而发生变化。因此,这些Uniform变量的数据必须重新产生,然后应用到新的location上。
而Uniform Block正是为了使在着色器间共享Uniform数据变得更加容易而设计的。有了Uniform Block,我们可以创建一个缓冲区对象来存储这些Uniform变量的值,然后将缓冲区对象绑定到Uniform Block。当着色器程序改变的时候,只需要将同样的缓冲区对重新绑定到在新的着色器中与之相关的Block即可。 -
2、定义:
uniform projViewM{
mat4 projection;
mat4 viewMatrix;
vec4 color2;
float radius1;
};
- 3、填充:
对于每一个uniform块,都有一个“索引值”(index),这个索引值我们可以在OpenGL ES中获得,并把它与一个具体的UBO关联起来。
GLint blockIndex = glGetUniformBlockIndex(nProgramHandler, "BlobSettings");
GLint nBlockDataSize = 0;
glGetActiveUniformBlockiv(nProgramHandler, blockIndex, GL_UNIFORM_BLOCK_DATA_SIZE, &nBlockDataSize);
glGenBuffers(1, &m_nUBO);
glBindBuffer(GL_UNIFORM_BUFFER, m_nUBO);
glBufferData(GL_UNIFORM_BUFFER, nBlockDataSize, NULL, GL_DYNAMIC_DRAW);
glBindBufferRange(GL_UNIFORM_BUFFER, 0, m_nUBO, 0, nBlockDataSize);
glUniformBlockBinding(nProgramHandler, blockIndex, 0);
glBindBuffer(GL_UNIFORM_BUFFER, NULL);
示例程序:
//顶点着色器A
attribute vec3 aPos;
uniform projViewM{
mat4 projection;
mat4 viewMatrix;
};
uniform mat4 modelMatrix;
void main(){
gl_Position = projection * viewMatrix * modelMatrix * vec4(aPos,1.0);
}
//片段着色器A
void main(){
gl_FragColor = vec4(0,1,0,1.0);
}
//顶点着色器B
attribute vec3 aPos;
uniform projViewM{
mat4 projection;
mat4 viewMatrix;
};
uniform mat4 modelMatrix;
void main(){
gl_Position = projection * viewMatrix * modelMatrix * vec4(aPos,1.0);
}
//片段着色器B
void main(){
gl_FragColor = vec4(0,0,1,1.0);
}
从上实力着色器程序中,可以看到有相同的projection和viewMatrix,并且值是相同的,所以可以定义成uniform块。 使用unifrom块定义变量后,可以在着色器间共享uniform变量。
- 3、着色器程序数据填充
- (void)setUpUniformBlock{
//获取索引块 在着色z器中的位置值索引 对应所有的着色器程序都需要定位,因为不同着色器可能值不一样
GLuint skyBoxID = glGetUniformBlockIndex(self.skyBoxProgram, "projViewM");
GLuint blackBoxID = glGetUniformBlockIndex(self.myProgram, "projViewM");
//a将uniform块绑定到一个特定的绑定点中
glUniformBlockBinding(self.skyBoxProgram, skyBoxID, 0);
glUniformBlockBinding(self.myProgram, blackBoxID, 0);
GLuint projViewM;
glGenBuffers(1, &projViewM);
glBindBuffer(GL_UNIFORM_BUFFER, projViewM);
//获取uniform块的尺寸大小
GLint blockSize ;
glGetActiveUniformBlockiv(self.skyBoxProgram, skyBoxID, GL_UNIFORM_BLOCK_DATA_SIZE, &blockSize);
KSMatrix4 identify;
ksMatrixLoadIdentity(&identify);
glBufferData(GL_UNIFORM_BUFFER, blockSize, NULL, GL_DYNAMIC_DRAW);
glBindBuffer(GL_UNIFORM_BUFFER, 0);
//b绑定Uniform缓冲对象到相同的绑定点上 (与a对应)
//目标、 绑定索引点 、uniform缓冲对象、偏移量、大小参数 可以绑定uniform对象的特定部分到绑定点
glBindBufferRange(GL_UNIFORM_BUFFER, 0, projViewM, 0, blockSize);
//或者 下面的函数
//目标、 绑定索引点 、uniform缓冲对象
//glBindBufferBase(GL_UNIFORM_BUFFER, 0, projViewM);
ksPerspective(&identify, 45, (_viewWidth*1.0)/(_viewHeight*1.0), 0.1, 100);
glBindBuffer(GL_UNIFORM_BUFFER, projViewM);
//更新缓冲数据
glBufferSubData(GL_UNIFORM_BUFFER, 0, blockSize/2, &identify.m[0][0]);
glBindBuffer(GL_UNIFORM_BUFFER, 0);
//更新剩余部分缓冲
KSMatrix4 viewM;
ksMatrixLoadIdentity(&viewM);
glBindBuffer(GL_UNIFORM_BUFFER, projViewM);
glBufferSubData(GL_UNIFORM_BUFFER, blockSize/2, blockSize/2, &viewM.m[0][0]);
glBindBuffer(GL_UNIFORM_BUFFER, 0);
}
- 4、绘制
- (void)draw{
glBindFramebuffer(GL_FRAMEBUFFER, _myColorFrameBuffer);
glViewport(0, 0, _viewWidth,_viewHeight);
glUseProgram(self.myProgram);
GLuint modelMID = glGetUniformLocation(self.myProgram, "modelMatrix");
KSMatrix4 modeMatrix;
ksMatrixLoadIdentity(&modeMatrix);
ksTranslate(&modeMatrix,1, 0, -10);
ksRotate(&modeMatrix, 30, 1, 1, 0);
glUniformMatrix4fv(modelMID, 1, GL_FALSE, &modeMatrix.m[0][0]);
glBindVertexArray(self.blackBoxVAO);
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);
[ZKLodaShader glCheckError];
glUseProgram(self.skyBoxProgram);
GLuint skyModelMID = glGetUniformLocation(self.skyBoxProgram, "modelMatrix");
KSMatrix4 skymodeMatrix;
ksMatrixLoadIdentity(&skymodeMatrix);
ksTranslate(&skymodeMatrix, -1, 0, -10);
ksRotate(&skymodeMatrix, 30, 1, 1, 0);
glUniformMatrix4fv(skyModelMID, 1, GL_FALSE, &skymodeMatrix.m[0][0]);
glBindVertexArray(self.VAO);
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);
[ZKLodaShader glCheckError];
if (_myColorRenderBuffer) {
glBindRenderbuffer(GL_RENDERBUFFER, _myColorRenderBuffer);
}
//绘制
[self.myContext presentRenderbuffer:GL_RENDERBUFFER];
glDeleteVertexArrays(1,&_VAO);
glDeleteVertexArrays(1, &_blackBoxVAO);
}
-
5、效果如下:
Snip20200322_7.png
网友评论