Search Unity

Different instances of a postFX script are sharing RenderTextures?

Discussion in 'Shaders' started by AcidArrow, Aug 28, 2017.

  1. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,755
    So I have this script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class testPost : MonoBehaviour
    6. {
    7.     public RenderTexture rt;
    8.  
    9.     void OnPreRender()
    10.     {
    11.         rt = RenderTexture.GetTemporary(Screen.width, Screen.height, 24);
    12.         GetComponent<Camera>().targetTexture = rt;
    13.     }
    14.     void OnPostRender()
    15.     {
    16.         GetComponent<Camera>().targetTexture = null;
    17.         Graphics.Blit(rt, null as RenderTexture);
    18.         RenderTexture.ReleaseTemporary(rt);
    19.     }
    20. }
    21.  
    It's the basis of some post FX, but it does nothing useful as it is.

    Now, I add the script to 2 different cameras and press play. Here is what the inspector shows:
    camera1.jpg camera2.jpg

    So the two different instances of the script seem to use the exact same temporary rendertexture (?!) (and memory profiler does confirm).

    Which does not make too much sense to me. Or maybe it does make sense and I'm missing something?

    But the most important question: Can I rely on it working like this?

    I mean, it is what it is, but if it's not intended behaviour, it most probably won't stay the same in future versions and on different platforms.

    EDIT: Does it happen because I'm releasing the rendertexture, so when the other script starts rendering, it grabs the same rt? (since the resolution is the same). If that's the case, it doesn't make much sense that I'm also getting the following error:
    since it should be properly being released from both scripts.
     
    Last edited: Aug 28, 2017