zoul
zoul

Reputation: 104065

Should I clean OpenGL state when working with renderbuffers and framebuffers?

I am writing a simple OOP wrapper around OpenGL ES. While writing the render- and framebuffer I have to bind the buffer in order to work with it:

- (void) setupSomething
{
    …
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, myBufferID);
    …
}

Now what if this setup code is called in a context where there’s already some other render buffer bound? My simple version mentioned above would have the nasty side effect of switching the current buffer, which sounds quite fragile. I figured I should write the code more defensively:

- (void) setupSomething
{
    // Store current state
    GLint previousRenderBuffer = 0;
    glGetIntegerv(GL_RENDERBUFFER_BINDING_OES, &previousRenderBuffer);
    // Do whatever I want to do
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, myBufferID);
    …
    // Restore previous state
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, previousRenderBuffer);
}

My questions are: is it really necessary/wise/customary to save the previous state like this, and if yes, is there some kind of glPushSomething that would do it for me?

Upvotes: 1

Views: 169

Answers (1)

kerim
kerim

Reputation: 2512

Working with a graphics api like OpenGL it's usually a good idea to minimize the number of api calls. Some calls can be quite expensive - I'm not sure about glBindRenderBuffer though - it could be as cheap as just storing one int. But it could be some complex state switching operation. You'd better handle it youself - using your own 'stack' of buffers(there's not glPushAttrib or something like this for render buffers in OpenGL ES) or, better in my humble opinion, avoid those situations - always make sure you finish your work with render buffer you have bound before passing to another buffer.

Upvotes: 1

Related Questions