Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to use results of a previous rendering inside a fragment shader?

I'm using the fragment shader to render to a texture. I would like the rendering of the previous iteration of the shader to be used by the next one. How is that possible?

like image 208
MaiaVictor Avatar asked Nov 27 '25 12:11

MaiaVictor


1 Answers

To re-use the texture in the fragment shader:

either

  • unbind the current framebuffer object (bind the null framebuffer object or bind another framebuffer object) to which the texture is attached,

or

  • detach the texture from the framebuffer object (and attach null, attach another texture with framebufferTexture2D or attach a renderbuffer instead).

and then bind the texture as a normal texture to be used in the fragment shader (via sampler2D and texture2D in the shader code).

To re-use the texture in the vertex shader, it is roughly the same, except that this feature (vertex textures) is non-standard (and has some limitations). Check MAX_VERTEX_TEXTURE_IMAGE_UNITS for support.

If you have an iterative algorithm that requires multiple rendering passes, one typical approach is to switch between two textures 1 and 2: during odd rendering passes, 1 is attached to the current framebuffer object and 2 is bound as a normal texture and during even rendering passes, it's the opposite. See also this answer on whether texture attachments should be changed in a single framebuffer object or whole framebuffer objects should instead be swapped.

like image 50
user3146587 Avatar answered Nov 29 '25 03:11

user3146587



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!