Framebuffer MRT bug and fix

Hi, :chimpanzee_amused:

I have found a bug that happens with MRT Framebuffers with same number of color attachments.

If we set a frame buffer frame1, then later frame2.

r.setFrameBuffer(frame1);
r.setFrameBuffer(frame2);

Frame2 buffer will not output to multiple targets. Only its first target will be rendered.

The culprit I believe is in the GLRenderer.setReadDrawBuffers method

if (context.boundDrawBuf != MRT_OFF + fb.getNumColorBuffers()) {
    intBuf16.clear();
    for (int i = 0; i < fb.getNumColorBuffers(); i++) {
        intBuf16.put(GLFbo.GL_COLOR_ATTACHMENT0_EXT + i);
    }

    intBuf16.flip();
    glext.glDrawBuffers(intBuf16);
    context.boundDrawBuf = MRT_OFF + fb.getNumColorBuffers();
}

Eg. the problem does not happen if one MRT frame buffer has 2 attachements and another one 3.
Thus deleteing the if statement fixes the problem.

I have put a patch on git here: Fix for MTR Framebuffers by TehLeo · Pull Request #554 · jMonkeyEngine/jmonkeyengine · GitHub

Merged.

By the way, if anyone is merging PRs, please make sure you merge into both master and the v3.1 branch if needed. We don’t want bugfixes to go only into master… If in doubt, you can ask the submitter or other devs if you are unsure if it should go into v3.1.

2 Likes

Okay, thanks for the info.