Render to Depth Texture using FBOs is Slow

Hi,

I’ve been having an issue when trying to use frame buffer objects to render to depth textures. For some reason it takes about a second or two for this to occur. For the purposes of my project, I need to use multiple 2D depth textures. At first I tried to use one FBO and call glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, depthTextures[i], 0); each time I need to use a different depth texture. Since this was slow I next created one FBO for each depth texture so that I wasn’t calling this function each frame. Unfortunately this didn’t work.

Here’s my initialization code:


glBindTexture(GL_TEXTURE_2D, depthTextures[i]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);	// Need to reconsider this may want to check for wrapping in shader take care of there.
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);	// Need to reconsider this
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // Since this is linear, the color texture will look anti-aliased
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
			
// Also set up the depth comparison modes
glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_INTENSITY);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, texW, 1, 0, GL_DEPTH_COMPONENT, GL_FLOAT, (void*)NULL); // Just save space for it.

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, depthFrameBuffer[i]);
			glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, depthTextures[i], 0);
glDrawBuffer(GL_FALSE);
glReadBuffer(GL_FALSE);

GLenum fboStatus = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
if (fboStatus != GL_FRAMEBUFFER_COMPLETE_EXT )
{
	fprintf(stderr, "FBO ERROR (0x%x)!
", (fboStatus));
}

And here’s where I attempt to render to the textures:


glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, depthFrameBuffer[quadrant]);

glViewport(0, 0, 1, texH);
glClear(GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glOrtho(0.0, 1.0, 0.0, texH, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);

// ... drawing code ...

glFlush();
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

I’ve been searching the net for a while and I have been unable to find any reason behind this. My card is a NVIDIA Geforce 7600 GT in case that’s of interest. Any ideas would be greatly appreciated.

Thanks.

At a quick glance, you don’t have a square (to the power of 2) depth texture, it’s texW by 1 pixel.

Also when you call glViewport and glOrtho your setting it be 1 by texH pixels.

Regards
elFarto

Thanks for the suggestion. I changed it from 1 to 2, but unfortunately it didn’t help. I also checked to make sure texW was a power of 2 as well and it’s 1024 so that should be ok. The funny thing is that when I used these textures before without using FBOs it worked fine. I want to use the FBOs because using glCopyTexture is too slow.

Thanks again though.
animeaholic

Did you try GL_NEAREST ?
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

I gave the GL_NEAREST suggestion a try, but it didn’t help either. One thing that I have noticed is that after switching to the FBO and doing all of my draw commands it only hangs when I either return to the window’s Framebuffer or call something like glCheckFramebufferStatusEXT. I’m assuming that when I call these there’s some kind of forced completion since when I call glFinish it also hangs for a couple of seconds before completing.

Any ideas?
animeaholic

Well, I took another look at the non power of 2 texture issue and decided just to use a normal size texture (1024x1024 instead of 1x1024) and that ended up getting rid of the slowness. I was using glExpert a little bit and it turns out that using the 1xN or even the 2xN was causing the driver to revert to software based rendering. As a result the process was significantly slower.

Thanks for all of the help
animeaholic