OpenGL ES 2.x:如何丢弃深度缓冲区 glDiscardFramebufferEXT?
我读过iOS OpenGL ES逻辑缓冲区加载,可以通过以下方式实现性能增益在每个绘制周期后“丢弃”深度缓冲区。我尝试了这个,但这是因为我的游戏引擎不再渲染。当我尝试渲染下一个周期时,出现 glError 1286 或 GL_INVALID_FRAMEBUFFER_OPERATION_EXT。
我感觉如果我要丢弃它,我需要在每个周期初始化或设置深度缓冲区,但我似乎找不到任何有关此的信息。这是我初始化深度缓冲区的方式(实际上是所有缓冲区):
// ---- GENERAL INIT ---- //
// Extract width and height.
int bufferWidth, bufferHeight;
glGetRenderbufferParameteriv(GL_RENDERBUFFER,
GL_RENDERBUFFER_WIDTH, &bufferWidth);
glGetRenderbufferParameteriv(GL_RENDERBUFFER,
GL_RENDERBUFFER_HEIGHT, &bufferHeight);
// Create a depth buffer that has the same size as the color buffer.
glGenRenderbuffers(1, &m_depthRenderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, m_depthRenderbuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24_OES, GAMESTATE->GetViewportSize().x, GAMESTATE->GetViewportSize().y);
// Create the framebuffer object.
GLuint framebuffer;
glGenFramebuffers(1, &framebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
GL_RENDERBUFFER, m_colorRenderbuffer);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
GL_RENDERBUFFER, m_depthRenderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, m_colorRenderbuffer);
这是我试图在每个绘制周期结束时丢弃深度缓冲区的方法:
// Discard the depth buffer
const GLenum discards[] = {GL_DEPTH_ATTACHMENT, GL_COLOR_ATTACHMENT0};
glBindFramebuffer(GL_FRAMEBUFFER, m_depthRenderbuffer);
glDiscardFramebufferEXT(GL_FRAMEBUFFER,1,discards);
我在所有绘制调用之后立即调用它。 ..
[m_context presentRenderbuffer:GL_RENDERBUFFER];
有什么想法吗?有人可以向我指出任何信息吗?我尝试阅读Apple关于该主题的指南(我在其中得到了最初的想法),http://developer.apple.com/library/ios/#documentation/3DDrawing/Conceptual/OpenGLES_ProgrammingGuide/WorkingwithEAGLContexts/WorkingwithEAGLContexts.html,但它似乎不太适合我。
I read iOS OpenGL ES Logical Buffer Loads that a performance gain can be reached by "discarding" your depth buffer after each draw cycle. I try this, but it's as my game engine is not rendering any longer. I am getting an glError 1286, or GL_INVALID_FRAMEBUFFER_OPERATION_EXT, when I try to render the next cycle.
I get the feeling I need to initialize or setup the depth buffer each cycle if I'm going to discard it, but I can't seem to find any information on this. Here is how I init the depth buffer (all buffers, actually):
// ---- GENERAL INIT ---- //
// Extract width and height.
int bufferWidth, bufferHeight;
glGetRenderbufferParameteriv(GL_RENDERBUFFER,
GL_RENDERBUFFER_WIDTH, &bufferWidth);
glGetRenderbufferParameteriv(GL_RENDERBUFFER,
GL_RENDERBUFFER_HEIGHT, &bufferHeight);
// Create a depth buffer that has the same size as the color buffer.
glGenRenderbuffers(1, &m_depthRenderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, m_depthRenderbuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24_OES, GAMESTATE->GetViewportSize().x, GAMESTATE->GetViewportSize().y);
// Create the framebuffer object.
GLuint framebuffer;
glGenFramebuffers(1, &framebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
GL_RENDERBUFFER, m_colorRenderbuffer);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
GL_RENDERBUFFER, m_depthRenderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, m_colorRenderbuffer);
And here is what I'm trying to do to discard the depth buffer at the end of each draw cycle:
// Discard the depth buffer
const GLenum discards[] = {GL_DEPTH_ATTACHMENT, GL_COLOR_ATTACHMENT0};
glBindFramebuffer(GL_FRAMEBUFFER, m_depthRenderbuffer);
glDiscardFramebufferEXT(GL_FRAMEBUFFER,1,discards);
I call that immediately following all of my draw calls and...
[m_context presentRenderbuffer:GL_RENDERBUFFER];
Any ideas? Any info someone could point me to? I tried reading through Apple's guide on the subject (where I got the original idea), http://developer.apple.com/library/ios/#documentation/3DDrawing/Conceptual/OpenGLES_ProgrammingGuide/WorkingwithEAGLContexts/WorkingwithEAGLContexts.html, but it doesn't seem to work quite right for me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您对
glDiscardFramebufferEXT(GL_FRAMEBUFFER,1,discards)
的调用表示您仅丢弃 1 个帧缓冲区附件,但是您的丢弃数组包含两个:GL_DEPTH_ATTACHMENT
和GL_COLOR_ATTACHMENT0< /代码>。
尝试将其更改为:
事实上,您说您在绘制周期结束时丢弃这些帧缓冲区附件,但就在
[m_context PresentRenderbuffer:GL_RENDERBUFFER];
之前。您正在丢弃呈现渲染缓冲区所需的颜色渲染缓冲区附件 - 也许尝试仅丢弃深度附件,因为此时不再需要它。您只需初始化缓冲区一次,不需要每个绘制周期。
glDiscardFramebufferEXT()
实际上并不会删除您的帧缓冲区附件 - 它只是向 API 发出一个提示,表明在丢弃命令完成后,在该绘制周期中不需要渲染缓冲区的内容。来自苹果的 iOS 版 OpenGL ES 编程指南:Your call to
glDiscardFramebufferEXT(GL_FRAMEBUFFER,1,discards)
is saying that you are discarding just 1 framebuffer attachment, however your discards array includes two:GL_DEPTH_ATTACHMENT
andGL_COLOR_ATTACHMENT0
.Try changing it to:
In fact, you say that you are discarding these framebuffer attachments at the end of the draw cycle, but directly before
[m_context presentRenderbuffer:GL_RENDERBUFFER];
. You are discarding the colour renderbuffer attachment that you need in order to present the renderbuffer - perhaps try just discarding the depth attachment, as this is no longer needed at this point.You only need to initialise your buffers once, not every draw cycle. The
glDiscardFramebufferEXT()
doesn't actually delete your framebuffer attachment - it is simply a hint to the API to say that the contents of the renderbuffer are not needed in that draw cycle after the discard command completes. From Apple's OpenGL ES Programming Guide for iOS: