简体   繁体   English

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

[英]OpenGL ES 2.x: How to Discard Depth Buffer glDiscardFramebufferEXT?

I read iOS OpenGL ES Logical Buffer Loads that a performance gain can be reached by "discarding" your depth buffer after each draw cycle. 我阅读了iOS OpenGL ES逻辑缓冲区负载 ,可以通过在每个绘制周期后“丢弃”深度缓冲区来达到性能提升。 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. 当我尝试渲染下一个循环时,出现glError 1286或GL_INVALID_FRAMEBUFFER_OPERATION_EXT。

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. 我尝试通读有关该主题的Apple指南(从中得到了最初的想法), http://developer.apple.com/library/ios/#documentation/3DDrawing/Conceptual/OpenGLES_ProgrammingGuide/WorkingwithEAGLContexts/WorkingwithEAGLContexts.html ,但是没有对于我来说似乎工作不正确。

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 . 您对glDiscardFramebufferEXT(GL_FRAMEBUFFER,1,discards)调用是说您仅丢弃1个帧缓冲区附件,但是您的discards数组包括两个: GL_DEPTH_ATTACHMENTGL_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]; 实际上,您说要在绘制周期结束时丢弃这些帧缓冲区附件,但要在[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. 您将丢弃呈现呈现缓冲区所需的彩色renderbuffer附件-也许尝试仅丢弃depth附件,因为此时不再需要此附件。

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. glDiscardFramebufferEXT()实际上并不会删除您的帧缓冲区附件-这只是向API发出的提示,表明丢弃命令完成后在该绘制周期中不需要渲染缓冲区的内容。 From Apple's OpenGL ES Programming Guide for iOS : 摘自Apple的iOS OpenGL ES编程指南

A discard operation is defined by the EXT_discard_framebuffer extension and is available on iOS 4.0 and later. 丢弃操作由EXT_discard_framebuffer扩展定义,并且在iOS 4.0及更高版本上可用。 Discard operations should be omitted when your application is running on earlier versions of ioS, but included whenever they are available. 当您的应用程序在ioS的早期版本上运行时,丢弃操作应该被省略,但是只要有可用的话,丢弃操作就应该包括在内。 A discard is a performance hint to OpenGL ES; 丢弃是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. 它告诉OpenGL ES,在完成drop命令之后,您的应用程序未使用一个或多个渲染缓冲区的内容。 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. 通过向OpenGL ES提示您的应用程序不需要渲染缓冲区的内容,可以丢弃缓冲区中的数据,或者可以避免执行昂贵的任务来保持这些缓冲区的内容更新。

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

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