OpenGL ES 2.x:如何丢弃深度缓冲区 glDiscardFramebufferEXT?

发布于 2024-12-05 01:45:27 字数 2347 浏览 0 评论 0原文

我读过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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

望笑 2024-12-12 01:45:27

您对 glDiscardFramebufferEXT(GL_FRAMEBUFFER,1,discards) 的调用表示您仅丢弃 1 个帧缓冲区附件,但是您的丢弃数组包含两个:GL_DEPTH_ATTACHMENTGL_COLOR_ATTACHMENT0< /代码>。

尝试将其更改为:

glDiscardFramebufferEXT(GL_FRAMEBUFFER, 2, discards);

事实上,您说您在绘制周期结束时丢弃这些帧缓冲区附件,但就在[m_context PresentRenderbuffer:GL_RENDERBUFFER];之前。您正在丢弃呈现渲染缓冲区所需的颜色渲染缓冲区附件 - 也许尝试仅丢弃深度附件,因为此时不再需要它。

您只需初始化缓冲区一次,不需要每个绘制周期。 glDiscardFramebufferEXT() 实际上并不会删除您的帧缓冲区附件 - 它只是向 API 发出一个提示,表明在丢弃命令完成后,在该绘制周期中不需要渲染缓冲区的内容。来自苹果的 iOS 版 OpenGL ES 编程指南

丢弃操作由EXT_discard_framebuffer定义
扩展并可在 iOS 4.0 及更高版本上使用。丢弃操作
当您的应用程序在早期版本上运行时应省略
iOS 的,但只要它们可用就包含在内。弃牌是
OpenGL ES 的性能提示;它告诉 OpenGL ES 的内容
在之后,您的应用程序不再使用一个或多个渲染缓冲区
丢弃命令完成。通过向 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 and GL_COLOR_ATTACHMENT0.

Try changing it to:

glDiscardFramebufferEXT(GL_FRAMEBUFFER, 2, discards);

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:

A discard operation is defined by the EXT_discard_framebuffer
extension and is available on iOS 4.0 and later. Discard operations
should be omitted when your application is running on earlier versions
of ioS, but included whenever they are available. A discard is a
performance hint to OpenGL ES; it tells OpenGL ES that the contents of
one or more renderbuffers are not used by your application after the
discard command completes. By hinting to OpenGL ES that your
application does not need the contents of a renderbuffer, the data in
the buffers can be discarded or expensive tasks to keep the contents
of those buffers updated can be avoided.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文