Difference between revisions of "Framebuffer"

From OpenGL.org
Jump to: navigation, search
(Blitting)
(Multisampling Considerations)
Line 44: Line 44:
 
=== Multisampling Considerations ===
 
=== Multisampling Considerations ===
  
Multisampling is supported with the [Default Framebuffer]] (through WGL/GLX_multisample) and/or [[Framebuffer Object]]s (through multisampled [[Renderbuffer Objects|renderbuffers]] or [[Multisample Textures|textures]], where supported).
+
Multisampling is supported with the [[Default Framebuffer]] (through WGL/GLX_multisample) and/or [[Framebuffer Object]]s (through multisampled [[Renderbuffer Objects|renderbuffers]] or [[Multisample Textures|textures]], where supported).
  
 
As explained in the article on [[Multisampling]], a multisampled buffer must be resolved into a single sample before it can be displayed. When the default framebuffer uses multisampling, this resolving operation is automatic, occurring during framebuffer swapping (though reading from the framebuffer can cause it to happen anyway).
 
As explained in the article on [[Multisampling]], a multisampled buffer must be resolved into a single sample before it can be displayed. When the default framebuffer uses multisampling, this resolving operation is automatic, occurring during framebuffer swapping (though reading from the framebuffer can cause it to happen anyway).
Line 52: Line 52:
 
It is an error to blit between buffers with different numbers of samples, ''unless'' one of them has zero samples. You get this by not attaching multisampled images to an FBO, or not using a multisampled [[Default Framebuffer]].
 
It is an error to blit between buffers with different numbers of samples, ''unless'' one of them has zero samples. You get this by not attaching multisampled images to an FBO, or not using a multisampled [[Default Framebuffer]].
  
If one of the framebuffers has zero samples, what happens depends on which framebuffer has zero samples.If the read framebuffer is the one with zero samples, then the draw buffer has all of its samples per-pixel replaced with the values from the read framebuffer. However, if the draw framebuffer is the one with zero samples, then it causes the multisampled framebuffer to have its multisamples resolved into a single sample per pixel. This explicit resolution is very useful when dealing with multisampled buffers.
+
If one of the framebuffers has zero samples, what happens depends on which framebuffer has zero samples. If the read framebuffer is the one with zero samples, then the draw framebuffer has all of its samples per-pixel replaced with the values from the read framebuffer. However, if the draw framebuffer is the one with zero samples, then it causes the multisampled framebuffer to have its multisamples resolved into a single sample per pixel into the draw framebuffer. This explicit resolution is very useful when wanting to display multisampled buffers.
  
 
As with all multisample behavior, none of this works at all unless {{apifunc|glEnable|(GL_MULTISAMPLE)}} is in effect (which is the default).
 
As with all multisample behavior, none of this works at all unless {{apifunc|glEnable|(GL_MULTISAMPLE)}} is in effect (which is the default).

Revision as of 18:18, 17 February 2013

A Framebuffer is a series of images that can be used as the destination for rendering. OpenGL has two kinds of framebuffers: the Default Framebuffer, which is provided by the OpenGL Context; and user-created framebuffers called Framebuffer Objects (FBOs). The images for default framebuffers are part of the context and usually represent a window or display device. The images for FBOs come from either Textures or Renderbuffers, and are never visible.

Images

Colorspace

Fragment shader outputs

Blitting

Framebuffers can be bound to two separate targets with glBindFramebuffer: GL_DRAW_FRAMEBUFFER and GL_READ_FRAMEBUFFER. Binding to GL_FRAMEBUFFER is equivalent to binding the framebuffer to both.

The reason for the separation of these targets is to allow data in one framebuffer to be blitted to another framebuffer.

A blit operation is a special form of copy operation; it copies a rectangular area of pixels from one framebuffer to another. This function also has some very specific properties with regard to multisampling.

You bind the source framebuffer to GL_READ_FRAMEBUFFER, then bind the destination framebuffer to GL_DRAW_FRAMEBUFFER. The read framebuffer is the source of the blit, and the draw framebuffer is the destination. The read and draw framebuffers can be the same.

After binding the framebuffers, you call this function:

void glBlitFramebuffer(
     GLint srcX0​, GLint srcY0​, GLint srcX1​, GLint srcY1​,
     GLint dstX0​, GLint dstY0​, GLint dstX1​, GLint dstY1​,
     GLbitfield mask​, GLenum filter​);

The pixels in the rectangular area specified by the src​ values are copied to the rectangular area specified by the dst​ values. The mask​ parameter is a bitfield that specifies which kinds of buffers you want copied: GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT, GL_STENCIL_BUFFER_BIT, or some combination. The filter​ parameter specifies how you want filtering performed if the two rectangles are not the same size.

One thing to keep in mind is this: blit operations only read from the color buffer specified by glReadBuffer in the read FBO and will only write to the color buffers specified by glDrawBuffers in the write FBO. If multiple draw buffers are specified, then multiple color buffers are updated. This assumes that mask​ included the color buffer. The depth and stencil buffers of the source framebuffers are blitted to the destination if the mask​ specifies them.

The glReadBuffer state is stored with the FBO/default framebuffer, just like the glDrawBuffers state.

Note that it is perfectly valid to read from the default framebuffer and write to an FBO, or vice-versa.

Format Considerations

Blitting is not the same as performing a pixel transfer operation. The conversion between source and destination format is more limited. Blitting depth and stencil buffers works as expected: values are converted from one bitdepth to the other as needed. Conversion between color formats is different.

A blit operation can only convert between formats within 3 groups. Signed integral and unsigned integral formats make up two groups, with all normalized and floating-point formats making up the third. Thus, it is legal to blit from an GL_RGB8 buffer to a GL_RGB32F and vice versa. But it is not legal to blit a GL_RGB8 from or to a GL_RGBI8 format image.

The data during blitting is converted according to simple rules. Blitting from a floating-point format to a normalized integer format will cause clamping, either to [0, 1] for unsigned normalized or [-1, 1] for signed normalized.

Multisampling Considerations

Multisampling is supported with the Default Framebuffer (through WGL/GLX_multisample) and/or Framebuffer Objects (through multisampled renderbuffers or textures, where supported).

As explained in the article on Multisampling, a multisampled buffer must be resolved into a single sample before it can be displayed. When the default framebuffer uses multisampling, this resolving operation is automatic, occurring during framebuffer swapping (though reading from the framebuffer can cause it to happen anyway).

Each framebuffer has a specific number of multisample samples (for Framebuffer Objects, they cannot be framebuffer-complete if all of the attached images do not have the same number of samples). When you blit between two framebuffers with the same number of samples, the copy is done directly; the destination buffer gets the same information the source had.

It is an error to blit between buffers with different numbers of samples, unless one of them has zero samples. You get this by not attaching multisampled images to an FBO, or not using a multisampled Default Framebuffer.

If one of the framebuffers has zero samples, what happens depends on which framebuffer has zero samples. If the read framebuffer is the one with zero samples, then the draw framebuffer has all of its samples per-pixel replaced with the values from the read framebuffer. However, if the draw framebuffer is the one with zero samples, then it causes the multisampled framebuffer to have its multisamples resolved into a single sample per pixel into the draw framebuffer. This explicit resolution is very useful when wanting to display multisampled buffers.

As with all multisample behavior, none of this works at all unless glEnable(GL_MULTISAMPLE) is in effect (which is the default).