glRenderbufferStorageMultisample
Allocates a renderbuffer with several samples per pixel, for MSAA in your own framebuffer.
Signature
void glRenderbufferStorageMultisample(GLenum target, GLsizei samples, GLenum internalformat, GLsizei width, GLsizei height);
How it works
Multisample anti-aliasing stores several coverage samples per pixel, so triangle edges get partial coverage instead of a hard step. The window's framebuffer can ask for it at creation (glfwWindowHint(GLFW_SAMPLES, 4)); an off-screen framebuffer, for HDR or post-processing, needs multisample attachments made with this function.
A multisample image cannot be sampled like a normal texture. When the frame is drawn, resolve it into a normal framebuffer with glBlitFramebuffer, which averages the samples of each pixel.
Parameters
Always GL_RENDERBUFFER (the bound one).
Samples per pixel: 4 is the usual choice, at most GL_MAX_SAMPLES.
GL_RGBA8, GL_RGBA16F (HDR), GL_DEPTH24_STENCIL8…
Width in pixels.
Height in pixels.
Tips & pitfalls
Every attachment of the framebuffer needs the same sample count, or it is incomplete (GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE).
Memory grows with the samples: a 4× 1080p RGBA16F target is about 66 MB.
Errors
When the call is invalid it does nothing and sets an error, which glGetError returns:
- GL_INVALID_VALUE
- samples is larger than GL_MAX_SAMPLES, or the size is too large.
Example
1glBindRenderbuffer(GL_RENDERBUFFER, msColor);
2glRenderbufferStorageMultisample(GL_RENDERBUFFER, 4, GL_RGBA16F, width, height);
3glBindRenderbuffer(GL_RENDERBUFFER, msDepth);
4glRenderbufferStorageMultisample(GL_RENDERBUFFER, 4, GL_DEPTH24_STENCIL8, width, height);