I am having problems using explicit multisampling when using multiple rendering targets in OpenGL.
I have 4 render targets (Position, Diffuse + opacity, Normal, Specular + exponent) that are rendered to during the initial geometry pass.
These are all non multisampled textures attached to a framebuffer object, and then set as the render targets using glDrawBuffers(). This works fine, and I can then sample these textures later to get the information needed for lighting calculations. Lovely.
I now want to try to remove some of the aliasing I am getting, so I started to implement explicit MSAA using multisampled textures. However, when I use multisampled textures as the render targets instead, only the first render target seems to be drawn to and the rest remain blank.
Other than changing how the textures are setup, bound and read in the shaders I haven't changed any other code.
Multisampled textures are attached to the framebuffer object using this code:
for (unsigned int i = 0; i < textureCount; ++i)
{
// Generate texture
GLuint texture;
glGenTextures(1, &texture);
// Bind multisample texture
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, texture);
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, msaaSamples, GL_RGBA, width, height, false);
// Set params
glTexParameterf(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// Attach to frame buffer
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D_MULTISAMPLE, texture, 0);
// Unbind
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, 0);
}
Multisampled depth/stencil texture is attached as well.
glGenTextures(1, &depth);
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, depth);
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, msaaSamples, GL_DEPTH24_STENCIL8, width, height, false);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D_MULTISAMPLE, depth, 0);
The framebuffer is then bound and draw buffers set
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
GLuint attachments[4] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3 };
glDrawBuffers(4, attachments);
I then draw the geometry as usual. However, when inspecting with apitrace it is clear that only the first color attachment is being drawn into. This is not the case when I use regular (non-multisampled textures).
All 4 draw buffers are definetly still being set however.