Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 10 of 10

Thread: Binding multiple textures works, but not multiple framebuffer texture attachments

  1. #1
    Junior Member Newbie
    Join Date
    Jun 2018
    Posts
    6

    Binding multiple textures works, but not multiple framebuffer texture attachments

    Hi All.

    I want to bind multiple texture attachments. It does work when I bind multiple textures, but just not the texture attachments. I checked the texture attachments and they draw if I bind just one, but binding multiple of them only draws one.

    This is my framebuffer and texture attachment code:

    Code :
    glGenFramebuffers(1, fbo);
        glGenTextures(1, tao);
        glBindFramebuffer(GL_FRAMEBUFFER, *fbo);
        glBindTexture(GL_TEXTURE_2D, *tao);
     
        glTexImage2D(GL_TEXTURE_2D, 0, (GLint)GL_RGBA32F,
                     width,
                     height,
                     0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
     
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (GLint)GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, (GLint)GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, (GLint)GL_MIRRORED_REPEAT);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, (GLint)GL_MIRRORED_REPEAT);
        glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, GL_TEXTURE_2D, *tao, 0);
        glBindTexture(GL_TEXTURE_2D, 0); // unbind
     
        if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
        {
            std::cout << "Framebuffer bad." << std::endl;
        }

    And this is the uniform & drawing code:

    Code :
    glUseProgram(blend_program); glCheckError();
     
                GLuint tex0 = glGetUniformLocation(blend_program, "tex0");
                glUniform1i(tex0, 0);
     
                GLuint tex1 = glGetUniformLocation(blend_program, "tex1");
                glUniform1i(tex1, 1);
     
                glActiveTexture(GL_TEXTURE0); glCheckError();
                glBindTexture(GL_TEXTURE_2D, tao[0]); glCheckError();
     
                glActiveTexture(GL_TEXTURE1); glCheckError();
                glBindTexture(GL_TEXTURE_2D, tao[1]); glCheckError();
     
                glDrawArrays(GL_TRIANGLE_FAN, 0, 4); glCheckError();

    The code draws only the first texture attachment. However if I bound an actual texture (as opposed to a texture attachment) in GL_TEXTURE1, it draws both images.

    This is being implemented in an after effects plugin. The exact same code works in a standalone app but not in AE, so I'm quite puzzled. Any info/hints would be appreciated. Thanks.

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,975
    Quote Originally Posted by Jamesel View Post
    I want to bind multiple texture attachments. It does work when I bind multiple textures, but just not the texture attachments. I checked the texture attachments and they draw if I bind just one, but binding multiple of them only draws one.
    What is this "texture attachment" you're referring to?

    Quote Originally Posted by Jamesel View Post
    This is my framebuffer and texture attachment code:
    Code :
        glGenTextures(1, tao);

    And this is the uniform & drawing code:

    Code :
                glActiveTexture(GL_TEXTURE0); glCheckError();
                glBindTexture(GL_TEXTURE_2D, tao[0]); glCheckError();
     
                glActiveTexture(GL_TEXTURE1); glCheckError();
                glBindTexture(GL_TEXTURE_2D, tao[1]); glCheckError();
    The glGenTextures() call generates a single texture name which is stored in tao[0]. Unless set by some other code which you aren't showing, tao[1] is undefined.

  3. #3
    Junior Member Newbie
    Join Date
    Jun 2018
    Posts
    6
    Quote Originally Posted by GClements View Post
    What is this "texture attachment" you're referring to?


    The glGenTextures() call generates a single texture name which is stored in tao[0]. Unless set by some other code which you aren't showing, tao[1] is undefined.
    Sorry I didn't show it. The tao array is initialised as follows:

    Code :
    for(int i = 0; i < numBuffers; i++)
    {
        genFramebufferTexAttach(&fbo[i], &tao[i], GL_COLOR_ATTACHMENT0, widthL, heightL);
    }

    Code :
    void genFramebufferTexAttach(gl::GLuint *fbo,
                                 gl::GLuint *tao,
                                 gl::GLenum attachment,
                                 int width,
                                 int height)
    {
        glGenFramebuffers(1, fbo);
        glGenTextures(1, tao);
        glBindFramebuffer(GL_FRAMEBUFFER, *fbo);
        glBindTexture(GL_TEXTURE_2D, *tao);
     
        glTexImage2D(GL_TEXTURE_2D, 0, (GLint)GL_RGBA32F,
                     width,
                     height,
                     0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
     
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (GLint)GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, (GLint)GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, (GLint)GL_MIRRORED_REPEAT);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, (GLint)GL_MIRRORED_REPEAT);
        glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, GL_TEXTURE_2D, *tao, 0);
        glBindTexture(GL_TEXTURE_2D, 0); // unbind
     
        if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
        {
            std::cout << "Framebuffer bad." << std::endl;
        }
     
        glCheckError();
    }

    For some reason the binding of multiple textures likes an actual texture2D, but not the tao's.

  4. #4
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,975
    Well, there's nothing wrong with what you've shown there.

    Are either of the textures still attached to the bound framebuffer during rendering? That will produce undefined behaviour (but won't generate an error).

  5. #5
    Senior Member OpenGL Lord
    Join Date
    May 2009
    Posts
    6,055
    Quote Originally Posted by Jamesel View Post
    Sorry I didn't show it. The tao array is initialised as follows:

    Code :
    for(int i = 0; i < numBuffers; i++)
    {
        genFramebufferTexAttach(&fbo[i], &tao[i], GL_COLOR_ATTACHMENT0, widthL, heightL);
    }
    Notice how you're attaching different images to the same attachment. That should (presumably) be `GL_COLOR_ATTACHMENT0 + i`.

  6. #6
    Junior Member Newbie
    Join Date
    Jun 2018
    Posts
    6
    Quote Originally Posted by Alfonse Reinheart View Post
    Notice how you're attaching different images to the same attachment. That should (presumably) be `GL_COLOR_ATTACHMENT0 + i`.
    Is that so? I'm creating 6 framebuffers and 6 texture attachments, so each framebuffer has one attachment, that's why I placed each texture as color attachment 0.

  7. #7
    Junior Member Newbie
    Join Date
    Jun 2018
    Posts
    6
    Quote Originally Posted by GClements View Post
    Well, there's nothing wrong with what you've shown there.

    Are either of the textures still attached to the bound framebuffer during rendering? That will produce undefined behaviour (but won't generate an error).
    Yes I thought that's how texture attachments work, the texture attachment should be attached to a framebuffer and then when you draw, it will draw the image into the texture attachment of the currently bound framebuffer.

  8. #8
    Senior Member OpenGL Lord
    Join Date
    May 2009
    Posts
    6,055
    Quote Originally Posted by Jamesel View Post
    Is that so? I'm creating 6 framebuffers and 6 texture attachments, so each framebuffer has one attachment, that's why I placed each texture as color attachment 0.
    Then I guess I don't understand how you analogize what you're doing with "binding multiple textures". That phrase usually implies "at the same time", not merely switching from one to another. So I assumed that when you wanted to attach 6 textures, it would be to the same FBO.

    Overall, it's not clear exactly what you're trying to accomplish, besides creating 6 FBOs. That is, what are you trying to do with those FBOs that isn't working?

  9. #9
    Junior Member Newbie
    Join Date
    Jun 2018
    Posts
    6
    Quote Originally Posted by Alfonse Reinheart View Post
    Then I guess I don't understand how you analogize what you're doing with "binding multiple textures". That phrase usually implies "at the same time", not merely switching from one to another. So I assumed that when you wanted to attach 6 textures, it would be to the same FBO.

    Overall, it's not clear exactly what you're trying to accomplish, besides creating 6 FBOs. That is, what are you trying to do with those FBOs that isn't working?
    Sorry about that, I'm not great at explaining. I want to add 6 buffers together in a frag shader, so I need them all bound at once. I can achieve this if the 6 are actual textures, but because they're texture attachments to framebuffers it's not working for some reason and I'm trying to find out that reason.

  10. #10
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,975
    Quote Originally Posted by Jamesel View Post
    Sorry about that, I'm not great at explaining. I want to add 6 buffers together in a frag shader, so I need them all bound at once. I can achieve this if the 6 are actual textures, but because they're texture attachments to framebuffers it's not working for some reason and I'm trying to find out that reason.
    Aside from a feedback loop (reading from a texture which is bound to the current framebuffer, which seems unlikely in this case), the other potential issues are just coding issues arising from the increased complexity. Make sure you aren't accidentally deleting the texture, clearing it, or overwriting the variable containing its name (handle).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •