Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have a software that performs volume rendering through a raycasting approach. The actual raycasting shader writes the raycasted volume depth into a framebuffer object, through gl_FragDepth, that I bind before calling the shader.

The problem I have is that I would like to use this depth in another shader that I call later on. I figured out that the only way to do that is to bind the framebuffer once the raycasting has finished, read the depthmap through something like glReadPixels(0, 0, m_winSize.x , m_winSize.y, GL_DEPTH_COMPONENT, GL_FLOAT, pixels); and write it to a 2D texture as usual glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, m_winSize.x, m_winSize.y, 0, GL_DEPTH_COMPONENT, GL_FLOAT, pixels) and then pass this 2D texture that contains a simple depth map to the other shader.

However, I am not entirely sure that what I do is the proper way to do this. Is there anyway to pass the framebuffer that I fill up in my raycasting shader to the other shader?

share|improve this question
Even if Nathan's answer wouldn't work, what got you to subsitute glReadPixels and glTexImage2D for glCopyTexImage2D? –  Christian Rau Nov 11 '11 at 21:32
add comment

1 Answer

up vote 4 down vote accepted

You should be able to create the depth buffer as a texture rather than a renderbuffer, and bind the texture directly to the FBO's depth attachment. It's the same way you'd do render-to-texture.

The way you're doing it now is definitely not efficient, since it's copying the depth buffer from VRAM to main memory, and then right back to VRAM. It also forces the CPU to stall waiting for the GPU to finish rendering before it can do these copies.

share|improve this answer
Thanks Nathan, that's what I was planning to do. As it is work related, I will try this on Monday. So far, looks like the correct answer :) –  Dan Nov 11 '11 at 22:54
The problem is that the first raycasting shader already uses a FBO to write the result. Is there anyway to bind two FBO's at the same time while running a shader? I presume you there is not.. –  Dan Nov 16 '11 at 9:28
OK, I think it is possible gamedev.net/page/resources/_/technical/opengl/… –  Dan Nov 16 '11 at 9:37
I finally got it working, as I said in my previous comment through multiple FBO writing, and I has sped up a LOT. Before my application was running at 20fps, now it does it at 80fps. Yayy! –  Dan Nov 16 '11 at 10:25
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.