Renderbuffer Object

From OpenGL Wiki
Jump to navigation Jump to search

Renderbuffer Objects are OpenGL Objects that contain images. They are created and used specifically with Framebuffer Objects. They are optimized for use as render targets, while Textures may not be, and are the logical choice when you do not need to sample (i.e. in a post-pass shader) from the produced image. If you need to resample (such as when reading depth back in a second shader pass), use Textures instead. Renderbuffer objects also natively accommodate Multisampling (MSAA).

Creation[edit]

Renderbuffer objects are standard OpenGL objects. So they have the usual glGenRenderbuffers/glDeleteRenderbuffers creation and destruction functions. They also have the usual glBindRenderbuffer function to bind them. It takes a target parameter, but the only viable target is GL_RENDERBUFFER.

Similar to Texture objects, renderbuffers are empty at initialization. Before you can bind them to a Framebuffer Object, you must allocate storage for the renderbuffer. To do this, simply call:

 void glRenderbufferStorage(GLenum target​, GLenum internalformat​, GLsizei width​, GLsizei height​);

The target​ must be GL_RENDERBUFFER, the same target you bound the renderbuffer object to. The internalformat​ should be an internal format used for images. The article on Image Formats elaborates in detail on the meaning of these formats. The width​ and height​ are the width and height of the renderbuffer.

If you wish to create a multisample renderbuffer, you use a slightly different function:

 void glRenderbufferStorageMultisample(GLenum target​, GLsizei samples​, GLenum internalformat​, GLsizei width​, GLsizei height​);

This works exactly like the original except for the samples​ parameter. Indeed, it works exactly like the original if you pass 0 for samples​. The samples​ parameter is the number of samples in the buffer. It must be less than GL_MAX_SAMPLES.

Similar to glTexImage2D, calling this function on a renderbuffer that has already had this function called on it will cause it to deallocate any resources associated with the previous call and allocate new storage.

Note: You are strongly advised not to do this. If you need a new renderbuffer, just delete the old object and create a new one. Recreating a renderbuffer with the same object name can cause completeness problems, particularly if it is attached to another object at the time.

Uses[edit]

You may have noticed that, unlike glTexImage2D, there are no parameters in these creation functions to actually initialize the data. The contents of the renderbuffer are undefined (it could be holding old data, or zeroed out, or anything) There is also no function to upload data to the renderbuffer like glTexImage2D.

No, the only way to use a renderbuffer object is to attach it to a Framebuffer Object. After you bind that FBO to the context, and set up the draw or read buffers appropriately, you can use pixel transfer operations to read and write to it. Of course, you can also render to it. The standard glClear function and its friends like glClearBuffer will also clear the appropriate buffer.

See also[edit]

Reference[edit]