1
votes

Can I use FBO blitting https://www.opengl.org/registry/specs/EXT/framebuffer_blit.txt , to pass rendered FBO, as texture, to my resolve shader input?

Can I use FBO as input for shader "sampler2D"?

I mean, I render something to fbo. Now I want to post-process image, can I pass FBO as texture to shader?

1
Sorry, your question as is doesn't make much sense, can you please elaborate?peppe
@peppe Is it better now?tower120

1 Answers

10
votes

You don't "pass FBO as texture".

What happens instead is that you create a FBO using textures as attachments (notably, to the color attachments); then you can draw normally (to the default FBO target, usually the screen) using the contents of such texture.

For typical full-screen post processing effects one usually draws a fullscreen quad, but of course you're pretty much limited to your own imagination (for instance you could draw the FBO contents on the mesh of a TV set, to simulate a live camera). At the same time, you could perform any kind of postprocessing in the fragment shader.

So, the simplest plan possible is:

  1. Create a texture of the right size and type (f.i. same size as the viewport, and 8 bit RGB)
  2. Create a FBO by
    1. creating the FBO object itself
    2. attaching the previously created texture to it to a color attachment (GL_COLOR_ATTACHMENT0) via glFramebufferTexture2D​

Then

  1. Bind your FBO
  2. Draw the contents you want to postprocess
  3. Bind the default FBO (i.e. the screen)
  4. Bind your post-processing shader program
  5. (Optionally) Bind your texture (now holding the FBO color contents) as active on a texture unit, and set the texture unit index as a sampler2D uniform in your shader program
  6. Draw a fullscreen quad. In your fragment shader you can sample the texture and apply any kind of filtering.

Note that the operations performed above are not what's meant for "framebuffer blitting". Blitting stands for the capability of copying a subrectangle of a FBO into a subrectangle of another FBO (of course, you can just blit the entire area). The FBO involved here may include the default FBO (i.e. the screen). There are no ways of performing post-processing via a blit, since there are no shaders involved.

Blitting is mostly useful in a specific scenario: copying the contents of a multisampled FBO into a non-multisampled FBO, then using the latter to perform post processing or as a texture source. The reason for this is usage is mostly historical -- older GL versions (< 3.2) provided a way to create a multisampled FBO by attaching a multisampled color Renderbuffer, but no way to read the values back in a shader (a Renderbuffer is a buffer you can attach to FBOs, just like textures, but unlike a texture, you can't sample its contents). Therefore, one created two FBOs -- a multisampled one to perform the drawing, which would then resolved into a second non-multisampled FBO via a blit operation. This second FBO had a texture attached as the color attachment, and you then could sample from it.

These days (>= 3.2, or with GL_ARB_texture_multisample) OpenGL offers a way to create multisampled textures, which are usable directly in shaders (via sampler2DMS​ samplers). No blit step is necessary.