Results 1 to 3 of 3

Thread: Can I read what was written to gl_FragData[] ?

  1. #1
    Junior Member Newbie
    Join Date
    Mar 2017
    Posts
    8

    Can I read what was written to gl_FragData[] ?

    What I wan't to do is some kind of ping-pong rendering,
    If I write to gl_FragData[0] one frame, is it then stored somewhere where I can I read from it the next frame?
    And if that is the case, then can I read from an arbitrary coordinate or just a fixed value like with
    #extension GL_EXT_shader_framebuffer_fetch :
    gl_LastFragData[] ?
    Or is gl_FragData[] only good for passing on multiple values to the next step in the pipeline?

  2. #2
    Senior Member Regular Contributor
    Join Date
    May 2016
    Posts
    477
    Quote Originally Posted by prince_polka View Post
    If I write to gl_FragData[0] one frame, is it then stored somewhere where I can I read from it the next frame?
    if you use double-buffering, i think next frame its stored in the backbuffer
    https://www.khronos.org/opengl/wiki/...uble_buffering

    Quote Originally Posted by prince_polka View Post
    And if that is the case, then can I read from an arbitrary coordinate ... ?
    if you want to have access to framebuffer data of the previous frame, its better you create 2 FBOs with 2 texture attachments:
    https://www.khronos.org/opengl/wiki/Framebuffer_Object
    https://www.khronos.org/opengl/wiki/...taching_Images

    here an example:
    https://sites.google.com/site/john87...ramebuffer-mrt

    FBO 1:
    reads from texture 1
    writes into texture 2

    FBO 2:
    reads from texture 2
    writes into texture 1

    each odd frame you render into FBO 1, otherwise into FBO2, so you effectively create your own "doublebuffer" which is fully under your control

    bind texture 1 to texture unit 1 (glActiveTexture(GL_TEXTURE1))
    bind texture 2 to texture unit 2 (glActiveTexture(GL_TEXTURE2))

    your fragmentshader should have a "uniform sampler2D mypreviousframe;" so that you can fetch from a texture
    each odd frame you want have read access to texture 1, so you call:
    Code :
    GLint location = glGetUniformocation(myprogram, "mypreviousframe");
    glUseProgram(myprogram);
    if (... is odd frame ...)
       glUniform1i(location, 1);
    else
       glUniform1i(location, 2);

  3. #3
    Senior Member OpenGL Guru
    Join Date
    Oct 2004
    Posts
    4,658
    Quote Originally Posted by prince_polka View Post
    What I wan't to do is some kind of ping-pong rendering,

    If I write to gl_FragData[0] one frame, is it then stored somewhere where I can I read from it the next frame?
    Yes, if you set up your destination framebuffer (and possibly source textures) properly.

    And if that is the case, then can I read from an arbitrary coordinate or just a fixed value like with

    #extension GL_EXT_shader_framebuffer_fetch :
    gl_LastFragData[] ?
    Ok, first thing's first. Are you using OpenGL or OpenGL ES? Your mention of EXT_shader_framebuffer_fetch suggests the latter.

    If you are using OpenGL ES and have this extension available, definitely consider using it. Also consider using EXT_shader_pixel_local_storage if it'd be useful to you and your GPU has it. These can save a ton of bandwidth and latency on most mobile GPUs (which are tilers).

    With these extensions, your writes/reads of old pixel data in the shader are stored to/read in from super-fast on-chip cache and (if you play your cards right) often is never written out to or read back from slow DRAM, which is very efficient (be sure you're calling glClear() and glInvalidateFramebuffer()/glDiscardFramebuffer() in all the places you should be on all the buffers you can). This is the preferable way to do multipass on mobile (tiler) GPUs. Doing it "the OpenGL way" instead on a tiler GPU results in higher latencies and other details you have to be aware of such as extra render target GPU memory consumption, and how to pipeline daisy-chained writes/reads to multiple framebuffers efficiently on your GPU+driver.

    Or is gl_FragData[] only good for passing on multiple values to the next step in the pipeline?
    That's one way to do it. If you've got access to one or both of the above-mentioend extensions though, prefer them instead.

    On some desktop drivers, there are more efficient ways to do what you want than ping-ponging between two textures. But I'll spare you that because it doesn't sound like you're writing for a desktop GPU.

Similar Threads

  1. Can some gl_FragData info be skipped?
    By Leadwerks in forum OpenGL: GLSL
    Replies: 3
    Last Post: 08-02-2008, 01:13 AM
  2. Can I use something like gl_FragData
    By 4fingers in forum OpenGL: GLSL
    Replies: 1
    Last Post: 06-20-2008, 08:48 AM
  3. NVIDIA 81.85 and gl_FragData
    By Jay Cornwall in forum OpenGL: Advanced Coding
    Replies: 1
    Last Post: 11-10-2005, 02:21 PM
  4. NV_render_texture_rectangle + gl_FragData[n]
    By imported_dimensionX in forum OpenGL: GLSL
    Replies: 1
    Last Post: 02-17-2005, 05:29 PM
  5. Replies: 0
    Last Post: 03-14-2002, 05:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean