First of all: appologies for a long post - but I wanted to include as much detail as possible to avoid needless question/answer posts if i’d just thought about what I was writing in the first place.
I’m trying to add some sort of anti aliasing for transparent objects to my deferred engine. At the moment the engine has a separate forward pass phase to blend transparent objects like grass. Another possible rendering path is to render the grass objects as part of the GBuffer phase with Alpha Test enabled.
Having read some nVidia documents on AA, CSAA looks like a nice and easy way to introduce multisampling and will propably serve me well for the grass objects. Now I wish to add anti aliasing to this part of the engine - the grass - using MSAA or CSAA or Transparency Anti Aliasing.
I’m aware that GL 3.2 now supports multisample textures - but to use this I’d have to rewrite my existing shader library and I’d prefer not to do that as I have a mostly legacy GL 2.1 engine.
What I have done is create a wrapper around the Gbuffer phase of the engine - and switch to using Multisample renderbuffers instead of the current implementation’s MRT render textures.
To do this I created an additional MRT GBuffer containing 3 multisample RenderBuffers (not textures) and used this new framebuffer when rendering the GBuffer phase. I then added an FBO BLIT at the end to ‘resolve’ each attachment into the original MRT GBuffer - so as far as the rest of the application is concerned there is no multisampling, just the usual MRT I have always had containing a Depth/Stencil texture and Colour,Normal and Position texture attachments. This is nice as there are a lot of dependancies upon the depth buffer and colour texture attachments (for example post processing).
Although I have sucessfully created the MRT renderbuffers for Multisample - it did not create a depth WITH stencil render buffer (combined). I did not get any GL errors or even a framebuffer incomplete, but a query to return the number of stencil bits gave 0. The stencil is a cruital element of the framebuffer for my engine, as the lighting depends upon it- and this is the bit giving me the most problems.
My Engine is tested on GForce 8 and Radeon 4850. Both can not give me a depth/stencil renderbuffer, yet alone a multisampled depth/stencil buffer.
Here’s my code for the ARB Framebuffer - create a depth Renderbuffer
glGenRenderbuffers( 1, @FrameBuffers[bufferID].FBDepth ); // allocate shared Depth render target.
glBindRenderbuffer( GL_RENDERBUFFER, FrameBuffers[bufferID].FBDepth );
if samples > 0 then
begin
if (CoverageSamples >0 ) AND (NVMultiSampleCoverage) then glRenderBufferStorageMultiSampleCoverageNV (GL_RENDERBUFFER,CoverageSamples, samples,depthBuffer.InternalFormat,width,height)
else glRenderBufferStorageMultiSample (GL_RENDERBUFFER, samples,depthBuffer.InternalFormat, width,height);
end else glRenderbufferStorage( GL_RENDERBUFFER, depthBuffer.InternalFormat, Width , Height );
end;
//Tested on both ATI and nVidia - use the DepthBuffer ID and attach to FBO. Do not use: GL_DEPTH_STENCIL_ATTACHMENT
glFramebufferRenderbuffer(GL_FRAMEBUFFER,GL_DEPTH_ATTACHMENT,GL_RENDERBUFFER, FrameBuffers[bufferID].FBDepth);
Here’s the Stencil Renderbuffer creation - i.e. resuse the depth buffer as the stencil
glFramebufferRenderbuffer(GL_FRAMEBUFFER,GL_STENCIL_ATTACHMENT,GL_RENDERBUFFER, FrameBuffers[bufferID].FBdepth);
glGetRenderbufferParameteriv (GL_RENDERBUFFER,GL_RENDERBUFFER_STENCIL_SIZE, @depthBuffer.GLAllocatedStencilSize);
My Depth format is Depth24_Stencil8, so I expect Depth and Stencil to share the same renderbuffer. As I said, creating a separate stencil render buffer with the code below give ‘Frame buffer unsupported’
glGenRenderbuffers( 1, @FrameBuffers[bufferID].FBstencil ); // allocate shared stencil render target.
glBindRenderbuffer(GL_RENDERBUFFER, FrameBuffers[bufferID].FBstencil);
glRenderbufferStorage(GL_RENDERBUFFER, GL_STENCIL_INDEX8, width , height);
if (fbDepthStencil in Template.flags) then glFramebufferRenderbuffer(GL_FRAMEBUFFER,GL_DEPTH_STENCIL_ATTACHMENT,GL_RENDERBUFFER, FrameBuffers[bufferID].FBstencil)
else glFramebufferRenderbuffer(GL_FRAMEBUFFER,GL_STENCIL_ATTACHMENT,GL_RENDERBUFFER, FrameBuffers[bufferID].FBstencil);
glGetRenderbufferParameteriv (GL_RENDERBUFFER,GL_RENDERBUFFER_STENCIL_SIZE, @depthBuffer.GLAllocatedStencilSize);
This is what i need help with:
-
Creating an ARB Multisample Render buffer for Depth/stencil (D24_S8). Am I attaching/creating the stencil renderbuffer incorrectly?
-
Coverage Sample Anti Aliasing (CSAA). Is this only available by using RenderbufferStorageMultisampleCoverageNV - ie not avaiable using the ARB framebuffer Multisample RenderbufferStorage.
-
MSAA (ARB_MultiSampling and now part of GL 1.3+). If CSAA is being used, do we still need to glEnable (GL_MULTISAMPLE) or is this only for MSAA. Do I need this enable state at all?
-
If CSAA is not part of the ‘core’ GL 3.0+, then why are there references to GL_SAMPLE_TO_COVERAGE,GL_SAMLE_VALUE and SampleCoverage mentioned in the spec on page 197, section 4.1 ‘PER-FRAGMENT OPERATIONS’ of the GL3.3_Core_20100311_withchanges.pdf ?
-
How to actually ‘turn on’ CSAA if RenderbufferStorageMultisampleCoverageNV is used.
Do we need to glEnable (GL_SAMPLE_TO_COVERAGE) and glEnable (GL_SAMPLE_VALUE) -
How is the value of SAMPLE_COVERAGE_VALUE as set by the function gl_SampleCoverage used to control the anti aliasing?
I guess what I’m after here guys as a little help clearing up how to implement MSAA, CSAA and/or Transparency Anti Aliasing. The examples in nVidia SDK 9.52 don’t really help - the best visual example is a Transparency AA demo - but this is for DX7 and the source does not help. Another good demo id the nVidia ‘Froggy AA’ demo. Although it enumerates every single FBO format imagineable (sRGB, 16 bit float/fixed, etc) it does not include any Depth/Stencil formats. I wonder why?
Anyone with a working example with key source snippets they can share?