In a RenderToTexture context, with several shaders involved, I need CPU access to one of the final render targets. Using glGetTexImage
works great, unfortunately it copies all texture data to client memory.
In order to avoid this copy, I am trying to implemnet a RTT using a TEXTURE_BUFFER
, hence attaching a separate Buffer Object to a texture, using that texture as part of the rendering, and then simply use glMapBuffer
to map the texture's datastore and make it accessible to the CPU.
Apart from the code, I would like to ask if such strategy may be right (nothing better crosses my mind) or if it goes against any specification.
I have tons of documents regarding TBOs as a way to setup textures, but nothing regarding the use of TBOs for reading the result of an RTT. I am just wondering if what I am trying to do is not legal in any way.
Unfortunately this approach won't work. A buffer texture is not really an oridinary 2D texture that can be used as render target or for 2D texture filtering, as described in this related question/answer pair. It is really just a buffer's data made accessible to a shader as a linear 1D integer-indexed array (disguised as a special kind of texture) and thus doesn't have much to do with an ordinary filtered 2D texture. It is much more similar to what OpenGL 4 later introduced as a shader storage buffer (but in a more cumbersome and read-only way, yet available on GL 3 hardware), or a uniform buffer object (but with a larger size and using a different memory region). So no, you cannot use a TBO as a render target in an FBO.
What might be an option for you would be a pixel buffer object (PBO). While this would still involve a copy from texture memory into an additional buffer, if used right you could still profit from stuff like asynchronous reading, special host-accessible memory and such things.