Retrieve the Stencil Buffer?

Is there an easy way to copy the contents of the stencil buffer to main memory?

You can copy the depth or frame buffer with glCopyTexImage2D(… GL_RGBA/GL_DEPTH_COMPONENT, …) but the spec says nothing about stencil?

Is it possible? Or do I have to clear the framebuffer, set the stencil, draw a black quad and then copy the framebuffer or something every time instead?

glReadPixels() with GL_STENCIL_INDEX or GL_DEPTH_STENCIL as ‘format’ parameter and an appropriate type (GL_UNSIGNED_INT or UNSIGNED_INT_24_8) copies the stencil values to main memory.

Be aware that you cannot use glReadPixels() on multisampled FBOs.
Paragraph 4.3. of the OpenGL specs tells you everything you need to know :wink: