Sign in to follow this  
godmodder

Stencil blitting

Recommended Posts

Hi,

I've implemented the stencil-routed K-buffer technique to peel multiple layers of depth in one pass. So far I had a 6x increase in performance. One thing I wanted to optimize was this: instead of clearing the FBO every frame and then initializing it with the right stencil masked writes (8 fullscreen passes), I could just do this once and blit the result to the current FBO every frame.

To my amazement, this:
[code]glClearStencil(1); // initial value for samples in stencilbuffer is 1
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); // initialize the buffers
glDisable(GL_DEPTH_TEST); // no depth test => fragments on every depth layer affect buffers
glDepthMask(false); // no writes to the depth buffer
glColorMask(false, false, false, false); // no writes to the color buffer
glEnable(GL_STENCIL_TEST);
glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE); // keep replacing sample values until they all have different values
glEnable(GL_MULTISAMPLE);
glEnable(GL_SAMPLE_MASK); // enable rendering to specific samples
glSampleMaski(0, 0xffffffff); // we initially write to all samples

unsigned int sampleMask = 2; // we start with the second sample (first one filled by the clear)
for(unsigned int i = 0; i < 7; ++i) // TODO: dynamic 'numOfSamples-1'
{
glSampleMaski(0, sampleMask);
glStencilFunc(GL_ALWAYS, 2+i, 0xffffffff); // sample stencil value range in loop is [2, numOfSamples]
renderFullScreenTexture(0, false);
sampleMask <<= 1;
}

glSampleMaski(0, 0xffffffff);
glDisable(GL_SAMPLE_MASK);
glDisable(GL_MULTISAMPLE);
glColorMask(true, true, true, true);
glDepthMask(true);
glEnable(GL_DEPTH_TEST); // update the depth buffer[/code]

was [b]almost twice as fast[/b] as this:
[code]glBlitFramebuffer(0, 0, texWidth, texHeight,
0, 0, texWidth, texHeight,
GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT, GL_NEAREST);
glEnable(GL_STENCIL_TEST);[/code]

Why is the blitting so slow? I would have expected a performance increase, as I avoid 8 fullscreen passes with it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this