Stencil buffer/FBO strange behaviour

Hello,

I have a rendering stage where a scene is rendered to a framebuffer object with a stencil/depth buffer enabled. The resulting stencil mask is kept, but the colourbuffer bit is cleared. A second scene is then rendered with the stencil test enabled so that only parts of the second scene not overlapping a part of the first scene are rendered (performing an XOR of the two scenes). I realise this can be done with a glLogicOp, but I need to use the stencil buffer. I then render the result of the first scene XOR the second scene later on via the texture attached to the framebuffer object. The problem I have is that on my Mac everything works perfectly except for 1 time in 10 or so when nothing gets rendered. On Linux, nothing at all gets rendered and the texture attached to the XOR FBO is just black (empty).

Here is an example of my code:


GLvoid XOR()
{
	glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, xor_fb );

	glPushAttrib( GL_VIEWPORT_BIT );
	glViewport( 0, 0, WIDTH, HEIGHT );
	
	glEnable( GL_STENCIL_TEST );
	glClear( GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | 
			GL_DEPTH_BUFFER_BIT );
	glMatrixMode( GL_MODELVIEW );

	glPushMatrix();
	{
		glStencilFunc( GL_ALWAYS, 1, 1 );
		glStencilOp( GL_REPLACE, GL_REPLACE, GL_REPLACE );

		glBindTexture( GL_TEXTURE_2D, scene1_tex );
		// Use a custom shader that just does some colour keying.
		glUseProgramObjectARB( shader_prog );
		glActiveTexture( GL_TEXTURE0 );
		
		glPushMatrix();
		{
			// Render first scene.
			glCallList( quad_face );
		}
		glPopMatrix();
		
		// Disable shader program.
		glUseProgramObjectARB( 0 );
		glBindTexture( GL_TEXTURE_2D, 0 );
		glClear( GL_COLOR_BUFFER_BIT );
		glStencilFunc( GL_EQUAL, 0, 0 );

		glPushMatrix();
		{		
			// Render second scene
		}
		glPopMatrix();
	}
	glPopMatrix();

	glDisable( GL_STENCIL_TEST );
		
	glPopAttrib();
	
	glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 );
}

I thought it could be a depthbuffer issue, but I tried clearing the depth buffer after rendering the first scene (along with the colourbuffer) and that seems to stop the stencil test from working.

Thanks for your time,

-Ross