Stencil buffers behaving strangely with FBO

I’m using GL_DEPTH_STENCIL_EXT, so that isn’t the problem.

What is the problem is that my stencil buffers aren’t working as they’re supposed to—unless I do a glReadPixels on GL_STENCIL_INDEX after creating them. Then they work fine.

For example. You’d think that if I bound a Cg program designed to mask out part of an image, and then called drawQuad() (which simply runs the program on every texel of the stencil buffer), the stencil would exist, and subsequent calls to anything would be masked appropriately (according to the stencil settings).

But that’s not what’s happening. I’m using an occlusion query to count how many fragments get through:

glClearStencil(1);
glClear(GL_STENCIL_BUFFER_BIT);

glStencilFunc(GL_ALWAYS, 0x1, 0x1);
glStencilOp(GL_DECR, GL_DECR, GL_DECR);

n = drawQuad();

//glReadPixels(0,0,width,height,GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, stencilbuffer[0]);

glStencilFunc(GL_EQUAL, 0x1, 0x1);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);

n = drawQuad();
glReadPixels(0,0,width,height,GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, stencilbuffer[0]);

Clearly, the first drawQuad should be setting the stencil buffer to 0 for all passing fragments, so the second drawQuad should report 0 passed fragments.

But it doesn’t. It reports the exact same number. UNLESS I uncomment that glReadPixels----then it works as expected.

Any idea what’s going on?

I’m willing to try switching to early-z to do this if I have to, but it’s frustrating when something as reasonable-sounding as a stencil buffer completely fails to work.