glReadPixels

Hi all

I have to read the color value of the framebuffer. I’m using glReadPixels to read 1 pixel of the framebuffer. I need to call this function several times.

For instance:


for(uint i=0;i<numEntities;i++)
{
  Vector2 coord = scene->getEntity(i).getWindowViewPosition(); //the position in screen
  
  glReadPixels((int)coord.x, (int)coord.y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, &pixel);
}

Each coord has different values ((121,200) , (345,800) … ). That way makes my application very slow. Exists a way of read several pixels in differents positions? For instance, glreadpixels passing a pointer of x and y coordinates?

I think the idea is clear.

Thank’s

Your current method is slow because of overhead and ‘pipeline stall’. I’ve read that there’s little performance difference between reading back one pixel or the entire framebuffer (except perhaps for very large resolutions).

If the rendered image does not change, you may as well read back the entire framebuffer once, keep it in application memory and retrieve the pixel manually.

Also, there is no function to read back pixels at multiple coordinates, so the only option is to use glReadPixels.

That works faster if numEntities is quite big. I mean, if numEntities is more or less the screen resolution, that works faster, if not is slower.

Anyway in may case numEntities is enough large so it’s usefull for me but not enough. It’s still too slow. Thanks for your reply.

Any more ideas?

Maybe you can compute the bounding box in window coordinates of your coords.

If they are not too way apart, you can hope to have a rectangle small enough so that you do one glReadPixel on a small rectangle and save the values in a small array.

Then you read the values from the small array only for the coords you are interested in.

Good idea, but my entites are very uniformly distributed so the bounding box could be nearly the hole screen.

But thanks.

I don’t know what you are using the read pixels for so this idea may not make sense but when I need to uniformly sample pixels from various places on the image (testing for scene changes in my app) I scale the image smaller on the GPU by texturing it to a quad on pbuffer or small FBO, then read the entire much-smaller image at one time using glReadPixels.

If you are just sampling coarse areas of the image this works quickly, but if you need exact pixel by pixel precision (from the original image) then you can ignore this post.

Robo

I need it :slight_smile: But thanks