LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: smokethepot on May 16, 2018, 16:47:19

Title: MSAA in Frame Buffer Objects
Post by: smokethepot on May 16, 2018, 16:47:19
I was reading up on MSAA in FBOs and found out that you can create multi-sampled renderbuffers in a frame buffer and then to display them, blit them either to the screen or to some other framebuffer which is not multi-sampled. In the case of blitting it to another framebuffer sample code would look like,

Code: [Select]
public void blitToFBO(FBO fbo){
      GL30.glBindFramebuffer(GL30.GL_READ_FRAMEBUFFER, frameBuffer);
      GL30.glBindFramebuffer(GL30.GL_DRAW_FRAMEBUFFER, fbo.frameBuffer);
      GL30.glBlitFramebuffer(0, 0, width, height, 0, 0, fbo.width, fbo.height, GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT, GL11.GL_NEAREST);
       unbindFrameBuffer();
}
,if I'm not mistaken.

This got me thinking, can we create a single frame buffer with a multisampled render buffer and a color attachment and then internally blit the render buffer to the color attachment?
I've written some possible code for it:
Code: [Select]
GL30.glBindFramebuffer(GL30.GL_READ_FRAMEBUFFER, frameBuffer);
GL30.glBindFramebuffer(GL30.GL_DRAW_FRAMEBUFFER, frameBuffer);
GL11.glReadBuffer(GL30.GL_RENDERBUFFER);
GL11.glDrawBuffer(GL30.GL_COLOR_ATTACHMENT0);
GL30.glBlitFramebuffer(0, 0, width, height, 0, 0, width, height, GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT, GL11.GL_NEAREST);


The only problem is that nowhere in the documentation does it state that GL_RENDERBUFFER is an acceptable parameter for the glReadBuffer(int) method, which means that it most probably isn't. If it is possible, can someone provide some alternative code to make it work. If it's not possible, can someone provide some explaination as to where am I going wrong in my idea.

Thanks in advance.
Title: Re: MSAA in Frame Buffer Objects
Post by: KaiHH on May 16, 2018, 17:21:26
You cannot have a non-multisampled color attachment and multisampled color attachment (be they either textures or renderbuffers) at the same time in the same FBO. You will get a GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE error on glCheckFramebufferStatus().
Also, using GL_RENDERBUFFER as read or draw buffer does not make any sense. It's like using GL_TEXTURE_2D for it. With a FBO you have to tell which attachment(s) of the FBO you actually want to read/write, which are depth, stencil, depth+stencil or one of the color attachments. And those attachments can either be backed by a renderbuffer or a texture, which is specified with either glFramebufferRenderbuffer(..., attachment, ...)  or glFramebufferTexture*D(..., attachment, ...) or any of their multisampled variants.
Title: Re: MSAA in Frame Buffer Objects
Post by: smokethepot on May 17, 2018, 03:29:34
Thanks, KaiHH. Your answer actually ended up clarifying a lot for me. What if I am using an FBO which has multiple color attachments? Can I, then, have one multisampled color attachment and one non-multisampled color attachment?
Title: Re: MSAA in Frame Buffer Objects
Post by: KaiHH on May 17, 2018, 07:55:33
You cannot have a non-multisampled color attachment and multisampled color attachment (be they either textures or renderbuffers) at the same time in the same FBO.
Title: Re: MSAA in Frame Buffer Objects
Post by: smokethepot on May 17, 2018, 15:45:27
Ok, thanks for clarification.