Search code examples
opengllwjgl

Understanding glClearBuffer* (difference between fixed- and floating point- color buffers)


Imagine having a framebuffer color attachment, a texture of format:

glTexImage2D(target, 0, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

RGBA8, Unsigned byte.

Normally I would use "glClear()" to clear the framebuffer as a whole. Even with multiple attachments of various formats, using "glClear()" clears all of them

(together with a clear color "glClearColor()")

And it confusingly clears the the RGBA8 (uint) format texture with THAT FLOATING POINT clear color.

So if I want to clear a single fbo attachment one possible option would be to use:

glClearBuffer*

The *fv, *iv and *uiv forms of these commands should be used to clear fixed- and floating-point, signed integer, and unsigned integer color buffers respectively.

But which one should I use to clear the RGBA8 uint attachment? I have tried both:

glClearBufferfv(GL_COLOR,0,new float[4]);

and

glClearBufferuiv(GL_COLOR,0,new int[4]);

And both work.

What would be the correct way of clearing this particular attachment?


Solution

  • GL_RGBA8 is a normalized, fixed-point image format. Therefore, you should use the fv function.

    That being said, the standard says that if the clear buffer function you use is not appropriate for the format of the buffer, you get undefined behavior. But you don't get an error.

    Undefined behavior can also include "works as I expected".