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:


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:

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.

What is this “texture attachment” you’re referring to?

The glGenTextures() call generates a single texture name which is stored in [var]tao[0][/var]. Unless set by some other code which you aren’t showing, [var]tao[1][/var] is undefined.

[QUOTE=GClements;1292862]What is this “texture attachment” you’re referring to?

The glGenTextures() call generates a single texture name which is stored in [var]tao[0][/var]. Unless set by some other code which you aren’t showing, [var]tao[1][/var] is undefined.[/QUOTE]

Sorry I didn’t show it. The tao array is initialised as follows:

for(int i = 0; i < numBuffers; i++)
{
    genFramebufferTexAttach(&fbo[i], &tao[i], GL_COLOR_ATTACHMENT0, widthL, heightL);
}
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.

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).

[QUOTE=Jamesel;1292867]Sorry I didn’t show it. The tao array is initialised as follows:

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

[/quote]

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.

[QUOTE=GClements;1292870]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).[/QUOTE]

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.

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?

[QUOTE=Alfonse Reinheart;1292881]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?[/QUOTE]

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).