前文 《OpenGL ES 多目标渲染(MRT)》中我们了解了利用 MRT 技术可以一次渲染到多个缓冲区,本文将利用帧缓冲区位块传送实现高性能缓冲区之间的像素拷贝。
OpenGL ES 帧缓冲区位块传送
帧缓冲区位块传送(Blit)也是 OpenGL ES 3.0 的新特性,主要用于帧缓冲区之间的像素拷贝,性能高且使用方便,可以指定缓冲区任意矩形区域的像素拷贝。
帧缓冲区位块传送(Blit)api
本文的绘制流程是,新建一个 FBO 绑定纹理作为颜色附着,然后绑定该帧缓冲区进行一次离屏渲染,最后绑定渲染到屏幕的缓冲区作为渲染缓冲区,从新的帧缓冲区中拷贝像素。
渲染代码:
代码语言:javascript复制//首先获取当前默认帧缓冲区的 id
GLint defaultFrameBuffer = GL_NONE;
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &defaultFrameBuffer);
//绑定我们新创建的帧缓冲区进行渲染
glBindFramebuffer(GL_FRAMEBUFFER, m_FBO);
glViewport ( 0, 0, m_RenderImage.width, m_RenderImage.height);
glClear(GL_COLOR_BUFFER_BIT);
glDrawBuffers(ATTACHMENT_NUM, attachments);
glUseProgram (m_ProgramObj);
glBindVertexArray(m_VaoId);
UpdateMVPMatrix(m_MVPMatrix, 0, m_AngleY, (float)screenW / screenH);
glUniformMatrix4fv(m_MVPMatLoc, 1, GL_FALSE, &m_MVPMatrix[0][0]);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_TextureId);
glUniform1i(m_SamplerLoc, 0);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, (const void *)0);
//绑定默认的帧缓冲区对象,将像素从新建的帧缓冲区拷贝到当前默认的帧缓冲区
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, defaultFrameBuffer);
glViewport ( 0, 0, m_SurfaceWidth, m_SurfaceHeight);
glClear(GL_COLOR_BUFFER_BIT);
//位块传送
BlitTextures();
进行帧缓冲区间位块传之前,需要指定好源帧缓冲区 GL_READ_FRAMEBUFFER 和目标帧缓冲区 GL_DRAW_FRAMEBUFFER,下面代码实现是将四个颜色附着对应的缓冲区像素,分别拷贝到当前渲染缓冲区中的 1/4 矩形区域内:
代码语言:javascript复制void FBOBlitSample::BlitTextures() {
//很重要,指定源帧缓冲区
glBindFramebuffer(GL_READ_FRAMEBUFFER, m_FBO);
glReadBuffer(GL_COLOR_ATTACHMENT0);
glBlitFramebuffer(0, 0, m_RenderImage.width, m_RenderImage.height,
0, 0, m_SurfaceWidth/2, m_SurfaceHeight/2,
GL_COLOR_BUFFER_BIT, GL_LINEAR);
glReadBuffer(GL_COLOR_ATTACHMENT1);
glBlitFramebuffer(0, 0, m_RenderImage.width, m_RenderImage.height,
m_SurfaceWidth/2, 0, m_SurfaceWidth, m_SurfaceHeight/2,
GL_COLOR_BUFFER_BIT, GL_LINEAR);
glReadBuffer(GL_COLOR_ATTACHMENT2);
glBlitFramebuffer(0, 0, m_RenderImage.width, m_RenderImage.height,
0, m_SurfaceHeight/2, m_SurfaceWidth/2, m_SurfaceHeight,
GL_COLOR_BUFFER_BIT, GL_LINEAR);
glReadBuffer(GL_COLOR_ATTACHMENT3);
glBlitFramebuffer(0, 0, m_RenderImage.width, m_RenderImage.height,
m_SurfaceWidth/2, m_SurfaceHeight/2, m_SurfaceWidth, m_SurfaceHeight,
GL_COLOR_BUFFER_BIT, GL_LINEAR);
}
最后呈现的效果是将四张图像渲染到一个屏幕上。
帧缓冲区位块传送(Blit)
-- END --