Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Where to call RenderTexture.DiscardContents

Discussion in 'Editor & General Support' started by MikeMarcin, Dec 6, 2013.

  1. MikeMarcin

    MikeMarcin

    Joined:
    May 15, 2012
    Posts:
    17
    I have a camera that renders a scene with Deferred Lighting and HDR into a RenderTextureFormat.ARGBHalf target.

    I have a another camera that renders to the screen and one of the objects it renders is textured with the first render texture.

    Where should I be calling DiscardContents?
    In OnPreRender on the first camera?
     
  2. Alexey

    Alexey

    Unity Technologies

    Joined:
    May 10, 2010
    Posts:
    1,623
    argh, our docs are certainly lacking in there
    here is the short rundown (i refer to mobile/gles rendering, as desktop simply ignores it for now)
    if we speak about tiled renderer the idea is that you might have resolve (save from tile to "VRAM") and restore (load from "VRAM" to tile) and both are kinda slow (well - it is extra copy through the bus).
    Resolve happens if you dont do discard (GL api, bear with me for unity one) at the end of the rendering (more precisely, before setting different fbo)
    Restore happens when you set new FBO as current and fail to do clear (clear tells driver that we dont need old contents, so no need to restore).

    So what do DiscardContents do:
    1. it marks appropriate render buffers (there are bools in there) to be forcibly cleared next time you activate the RenderTexture
    2. IF you call it on active RT it will discard when you will set another RT as active.

    Hope this helps.
    P.S. mandatory advertising: we fixed some bugs around it (i dont remember the exact version but should be there soon if not now) and for 4.5 iirc we really cleaned up our stuff regarding setting FBO, so on mobiles it will be done in most efficient way (no unneeded setting fbo = no unneeded restore/resolve)
     
  3. MikeMarcin

    MikeMarcin

    Joined:
    May 15, 2012
    Posts:
    17
    Thanks for the very useful information.
    In terms of the rendering callbacks and Graphics.Blit when does the active RT get changed?
     
  4. tatoforever

    tatoforever

    Joined:
    Apr 16, 2009
    Posts:
    4,364
    Can you please Alex update the documentation?
    At least add this part:

    So what do DiscardContents do?
    1. It marks appropriate render buffers (there are bools in there) to be forcibly cleared next time you activate the RenderTexture
    2. If you call it on active RT it will discard when you will set another RT as active.