Precision of glReadPixels when reading unsigned int

1.1k views Asked by At

I am having problems storing and retrieving 32 bit unsigned from my framebuffer. The max value for a framebuffer on my intel laptop (nvidia card and Ubuntu) is:

4.294.967.295

However, the max value that i can read, for some reason, is only:

1.040.992.698

I was wondering if someone can tell me if I am doing something wrong or if this a limitation of my graphics card.

I am setting up my framebuffer like this:

  // generate render and frame buffer objects
  glGenRenderbuffers( 1, &colorbufId );
  glGenRenderbuffers( 1, &depthbufId );
  glGenFramebuffers ( 1, &framebufId );

  // setup renderbuffer
  glBindRenderbuffer(GL_RENDERBUFFER, colorbufId);
  glRenderbufferStorage(GL_RENDERBUFFER, GL_R32UI, _viewWidth, _viewHeight);

  // setup depth buffer
  glBindRenderbuffer(GL_RENDERBUFFER, depthbufId);
  glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT32, _viewWidth, _viewHeight);

  // setup framebuffer
  glBindFramebuffer( GL_FRAMEBUFFER, framebufId );
  glFramebufferRenderbuffer( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorbufId );
  glFramebufferRenderbuffer( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,  GL_RENDERBUFFER, depthbufId );

  // check if everything went well
  GLenum stat = glCheckFramebufferStatus(GL_FRAMEBUFFER);  
  if(stat != GL_FRAMEBUFFER_COMPLETE) { printf("stat=%d != %d\n", stat, GL_FRAMEBUFFER_COMPLETE); Error(); exit(0); }

  // define where the framebuffer outputs will be written
  const GLenum bufs[] = { GL_NONE, GL_COLOR_ATTACHMENT0 };
  glDrawBuffers(2, bufs);

Now when i try to read a pixel

  const uint32_t MAX_UINT = std::numeric_limits< uint32_t >::max();

  glClearBufferuiv(GL_RENDERBUFFER, colorbufId, &MAX_UINT);
  glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );

  uint32_t pix_value=0;
  glReadPixels(0, 0, 1, 1, GL_RED_INTEGER, GL_UNSIGNED_INT, &pix_value);

the value of pix_value = 1.040.992.698.

I have also tried use 'GL_RED' instead of 'GL_RED_INTEGER' but that still does not work.

(for anyone interested, the reason i'm trying to do the above is for picking objects)

EDIT: So in the end this had nothing to do with precision. I was not clearing the buffer correctly. Se answer below

1

There are 1 answers

1
wesdec On BEST ANSWER

Ok i found the error:

This is the correct command to clear the buffer:

  glClearBufferuiv(GL_COLOR, colorbufId, &UNDEF_IDX);
  glClear( /*GL_COLOR_BUFFER_BIT |*/ GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );