I am having trouble using glGetTexImage with a depth texture of an FBO. (OGL 4.2)

I am having trouble using glGetTexImage with a depth texture of an FBO. (Using an OpenGL 4.2 context)

My FBO is set up like so:


GLfloat border[] ={ 1.0f, 0.0f, 0.0f, 0.0f};

// depth texture setup
m_DepthTexture.Create(dbFormat,m_Width,m_Height);
m_TextureID = m_DepthTexture.GetTextureID();
m_DepthTexture.SetFilterMode(GL_NEAREST,GL_NEAREST);
m_DepthTexture.SetWrapMode(GL_CLAMP_TO_BORDER,GL_CLAMP_TO_BORDER);

glBindTexture(GL_TEXTURE_2D, m_TextureID);
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, border);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE,
                GL_COMPARE_REF_TO_TEXTURE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LESS);

// Assign the depth map to texture unit 0
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_TextureID);

// Create and set up the FBO
glGenFramebuffers(1,&m_FBOID);
glBindFramebuffer(GL_FRAMEBUFFER, m_FBOID);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
                       GL_TEXTURE_2D, m_TextureID, 0);
GLenum drawBuffers[] = {GL_NONE};
glDrawBuffers(1, drawBuffers);

GLenum status = glCheckFramebufferStatus(GL_DRAW_FRAMEBUFFER);

glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
glBindTexture(GL_TEXTURE_2D,0);

if(status != GL_FRAMEBUFFER_COMPLETE)
{ return false; }

I draw to it like so:


glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_FBOID);
 
// draw some things
 
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);

And I attempt to copy the depth texture to system memory like so:


glBindTexture(GL_TEXTURE_2D, m_TextureID);
glGetTexImage(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, GL_FLOAT,
                       m_DepthImage.data);
glCheckErrorRetB();
glBindTexture(GL_TEXTURE_2D, 0);

The function glGetTexImage throws an error: invalid operation;

This same code works in another project i have, but not in this project, so I am sure it has to do with some internal state that is being incorrectly set.

Any ideas as to why it might be behaving so?

Are you quite sure about that? You need to call glGetError() before the glGetTexImage() call to clear any pending errors. Otherwise, you don’t actually know that it’s the glGetTexImage() call that’s generating the error.

Is a buffer bound to the GL_PIXEL_PACK_BUFFER target? If not, that excludes many of the reasons why glGetTexImage() can generate a GL_INVALID_OPERATION error.

Is m_TextureID being overwritten with a different texture ID? it’s an error to read GL_DEPTH_COMPONENT from a non-depth-format texture.

Also, why are you binding the texture to a texture unit after creating it? It’s an error (although not necessarily one which will be reported via glGetError()) to execute a draw call while a texture level is attached to the current framebuffer if that level can be accessed by the current shader program. In general, any textures attached to the current framebuffer shouldn’t be bound to a texture unit. In the case where you want to read from other levels of the texture, you need to ensure that the base and maximum levels are set to exclude the level which is attached to the framebuffer.