• Advertisement
Sign in to follow this  

Stencil blitting

This topic is 2323 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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:
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


was almost twice as fast as this:
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);


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
Advertisement
Sign in to follow this  

  • Advertisement