Search code examples
framebufferopengl-4

OpenGL DSA and FBO


I upgraded my FBO code to use DSA (Direct Access State) features from OpenGL 4.5.

All is fine, but I still need to use glBindFramebuffer() before drawing. Is there something I missed ?

I was thinking about this call before drawing to my FBO.

glNamedFramebufferDrawBuffer(m_FBO, GL_COLOR_ATTACHMENT0);

Then use this one to revert back to default framebuffer.

glDrawBuffer(GL_BACK);

But it doesn't work. Should I still use glBindFramebuffer() ? And finally what is the use of glNamedFramebufferDrawBuffer() if so ?

I hardly found clear topics about this.


Solution

  • glNamedFramebufferDrawBuffer does not bind the frambuffer to the target. It only specifies the color buffer for a named framebuffer object.

    See OpenGL 4.6 core profile specification - 17.4.1 Selecting Buffers for Writing, p. 513:

    17.4.1 Selecting Buffers for Writing

    The first such operation is controlling the color buffers into which each of the fragment color values is written. This is accomplished with either DrawBuffer or DrawBuffers commands described below. The set of buffers of a framebuffer object to which fragment color zero is written is controlled with the commands

    void DrawBuffer( enum buf );
    void NamedFramebufferDrawBuffer( uint framebuffer, enum buf );
    

    For DrawBuffer, the framebuffer object is that bound to the DRAW_FRAMEBUFFER binding. For NamedFramebufferDrawBuffer, framebuffer is zero or the name of a framebuffer object. If framebuffer is zero, then the default draw framebuffer is affected.

    See OpenGL 4.6 core profile specification - 9.2 Binding and Managing Framebuffer Objects, p. 297:

    A framebuffer object is created by binding a name returned by GenFramebuffers (see below) to DRAW_FRAMEBUFFER or READ_FRAMEBUFFER. The binding is effected by calling

    void BindFramebuffer( enum target, uint framebuffer );
    

    with target set to the desired framebuffer target and framebuffer set to the framebuffer object name. The resulting framebuffer object is a new state vector ...