reading data back from a fragment shader

Hi all, I have a simple shader which I want to use as a lookup table from my code running on the CPU and I’m not sure what the best way to go about it is.

I’ve come across two functions: glGetTexImage and glReadPixels, but not sure which is the best way… and what exactly do I need to do? So far I can set the shader as active and render a quad with the shader on screen… but should I be rendering to an FBO in either case? any help appreciated,


If you need to read image data after the shader processing you can read data from screen or from a texture. It depends on what you want to do. If the shader output is the final rendering output you can use then glReadPixels to read the framebuffer. On the other hand, if the shader output is the rendering output, what I would do is use a fbo to render to texture and then read the texture with glGetTexImage.

But do you really need to get back all this data on RAM? It would be faster if you could do all this with maybe another shader on gpu.

Hi, thanks for the response. I experimenting more than anything at the moment to be honest, just trying to learn about using the GPU more (I am reading a lot of stuff on too!).

In this particular case, the fragment shader is simply rendering some animated colorful fractal noise type stuff - I don’t want it to be rendered to the screen, I want to use it as a color map for a particle system (which is entirely CPU based - but am considering porting that to the GPU as well… but one thing at a time :P). And whenever a new particle is spawn, I want it to take its initial color from the relevant position in the colormap. I have particles being spawned all over the screen at the same time so this approach seemed more logical than a function(x, y, t) - and I was hoping that the GPU could calculate all of the values much quicker as it could do multiple texels at once.

Maybe i’ve completely misunderstood these concepts but I’m just experimenting at the moment trying to understand it all…

So in this case, I should create a texture, bind it to an FBO, set the FBO active, activate my shader, draw a quad, deactivate shader, deactivate FBO, and then use glGetTexImage on that texture?

You just make a FBO, make a render buffer, render to it, then call glReadPixels.

Yes, this exactly what you have to do if then, you want to read the generated texture with CPU.
Then it would be easy to move all this on the gpu (If I well understand). You will just need to generate texture coordinates for your particles and use them to read the texture at the good location. Maybe with a shader it would be even simpler.

In first, place, for the CPU, you can just render the fractal in a render buffer like V-man said. Then you will need a texture for HW acceleration.