Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Standalone only error: Releasing render texture that is set to be RenderTexture.active!

Discussion in 'Scripting' started by maleone0487, Aug 9, 2014.

  1. maleone0487

    maleone0487

    Joined:
    Feb 23, 2011
    Posts:
    59
    Releasing render texture that is set to be RenderTexture.active!
    RenderTexture warning: Destroying active render texture. Switching to main context.

    Does anyone have a good explanation as to why this error is occurring, or how to prevent it? As far as I can tell it doesn't happen in the editor, and this error seems to occur when loading a level that doesn't have a camera which uses a render texture when the previous one did. I've tried releasing the active render texture before loading, after loading, during loading (LoadLevelASync), on PreRender, etc, but this error always happens on the first frame once the new level is loaded. I've also tried adding a dummy camera into the problematic scene with the render texture set up, but the error still occurs.
     
    elqtfy likes this.
  2. DrKucho

    DrKucho

    Joined:
    Oct 14, 2013
    Posts:
    140
    hey i am having similar issue, only on standalone

    Setting up 2 worker threads for Enlighten.
    Thread -> id: 116cad000 -> priority: 1
    Thread -> id: 1171c8000 -> priority: 1
    Releasing render texture that is set as Camera.targetTexture!

    (Filename: /Users/builduser/buildslave/unity/build/Runtime/Camera/Camera.cpp Line: 2939)

    Releasing render texture that is set as Camera.targetTexture!

    (Filename: /Users/builduser/buildslave/unity/build/Runtime/Camera/Camera.cpp Line: 2939)

    Using Mac and Unity 5.3.0
     
  3. DrKucho

    DrKucho

    Joined:
    Oct 14, 2013
    Posts:
    140
    fixed , not sure what was your case but in my case the problem happened every time i change resolution at runtime, so i fixed it by not assigning the targetTexture right immediately after setting the new resolution but a frame later, i had noticed that real resolution change ( not the emulation that happens at editor ) does not happen immediately even in other parts of my code that depends on the actual resolution i could not execute it immediately but had to wait till the resolution was actually set...
    Despite it seems to be related to the same "not immediate resolution change thing" , this releasing render texture issue is totally new though, it was not happening in unity 4.6 and started to happen when i moved to unity 5...

    hope this helps someone
     
  4. Johny_G

    Johny_G

    Joined:
    Mar 28, 2014
    Posts:
    20
    It confirms my experience. I had to do any changes to the RenderTexture after "WaitForEndOfFrame", otherwise it was often crashing on certain devices (it was not even consistent between same GPUs; yet it was game-breaking). Now it seems even weirder that it isn't documented everywhere, because I'm obviously not the only one with the same problem :). FYI, it even crashes on certain Android and iOS devices. But not all of them.
     
    DrKucho likes this.
  5. DrKucho

    DrKucho

    Joined:
    Oct 14, 2013
    Posts:
    140
    to be totally sure i understood correctly , all problems and crashes went away when you moved all changes behind a yield WaitForEndOfFrame ?
     
  6. Johny_G

    Johny_G

    Joined:
    Mar 28, 2014
    Posts:
    20
    Yes. My use-case was little bit different, but the symptoms seem to be very similar (mess with RenderTexture, get access crash in native code). If you are interested, the issue was reported and solution posted in my older thread:
    http://forum.unity3d.com/threads/re...-probably-many-other-mobile-platforms.368842/

    Not sure if these two have anything in common, but it seems so :). After I put the line there, I had no crash for last two months. I think it's because all EndOfFrame yields are called after the frame has been fully rendered, and therefore you cannot access it at the same time as your camera does. But what do I know :).
     
    DrKucho likes this.
  7. DrKucho

    DrKucho

    Joined:
    Oct 14, 2013
    Posts:
    140
    thanks, and lol, yeah , most of the time this coding in unity is all about fixing obscure undocumented problems and making workaround solutions that do work but you don't really know why they work ...
     
    Johny_G likes this.
  8. Johny_G

    Johny_G

    Joined:
    Mar 28, 2014
    Posts:
    20
    Word.
     
  9. Hickna

    Hickna

    Joined:
    Mar 14, 2013
    Posts:
    26
    Hi! Can you guys post some example? I tried to do all that you said but without success.
    I have to create 4 render textures using the Screen.widht and Screen.heigth. I tried to use a static value like 640x480. Also tried to do it after 5 seconds after the level loaded, so obviously much more than 1 frame after all was done.
    Why? I'm going to give up. So sad... This project was working since 2012 and now didn't work because the Unity5 :(
    Thanks in advance!
     
  10. DrKucho

    DrKucho

    Joined:
    Oct 14, 2013
    Posts:
    140
    this is how i do it, with a coroutine to start next frame

    Code (CSharp):
    1.     public void DoTheRenderTextureShit(){
    2.         if (debug) print ("DENTRO DE DO THE RENDER TEXTURE S***");
    3.         FindRenderToTextureSpecificShit();
    4.         int x = (int)fatRes.x;
    5.         int y = (int)fatRes.y;
    6.         if ( fatRes.y % 2 != 0 ){ // si la altura no es divisible por 2
    7.             canvasOffsetY = 0.5f;
    8.             scanLinesOffsetY = 2;
    9.             y = y + 1;
    10.         }
    11.         else{
    12.             canvasOffsetY = 0;
    13.             scanLinesOffsetY = 0;
    14.         }
    15.             if (debug) print(" Y = " + y + " fatRes.y = " + fatRes.y + " canvasOffsetY = " + canvasOffsetY);
    16.    
    17.         myRenderTexture = new RenderTexture((int)(x * pixelResolution.x), (int)(fatRes.y * pixelResolution.y), 24, RenderTextureFormat.ARGB32); // !
    18.         myRenderTexture.filterMode = renderTextureFilterMode;
    19.  
    20.         SnapTo.realPixel.x = (int)pixelResolution.x;
    21.         SnapTo.realPixel.y = (int)pixelResolution.y;
    22.    
    23.         float xFloat = (float)x; // para no tener que castear todo el rato
    24.         float yFloat = (float)y; // para no tener que castear todo el rato
    25.         Rect newRect = screenCanvasRectTransform.rect;
    26.         newRect.width = xFloat;
    27.         newRect.height = yFloat;
    28.         screenRect.rectTransform.sizeDelta = new Vector2( xFloat, yFloat);
    29.         float orthoSize;
    30.         if (Application.isEditor == false) orthoSize= fatRes.y/2f;
    31.         else orthoSize = (gResolutions[gri].height/zoomFactor) / 2f;
    32.         if (debug) print ("Y ="+ y +" ORTHO SIZE ="+ orthoSize);
    33.         camTex.orthographicSize = orthoSize;
    34.         camTexBounds.size = new Vector3(camTex.pixelWidth, camTex.pixelHeight, 0) / zoomFactor;
    35.         camTexBounds.center = camTex.transform.position;
    36.         for (int n=0; n < shiftOnOddVerticalRes.Length; n++){
    37.             TransformHelper.SetLocalPosY(shiftOnOddVerticalRes[n].transform, canvasOffsetY);
    38.         }
    39.    
    40.         camTK2D.forceResolutionInEditor = true;
    41.         camTK2D.forceResolution = new Vector2(xFloat , fatRes.y);
    42.         camTK2D._screenExtents = new Rect(-xFloat/2, -fatRes.y/2, xFloat, fatRes.y);
    43.         StartCoroutine( AssignRenderTextureNextFrame() ); // WORKAROUND
    44.    
    45.         camUnity.orthographicSize = orthoSize;
    46.         camUnity.enabled = false;
    47.         camUnity.enabled = true;
    48.        
    49.         if (camUI){
    50.             camUI.orthographicSize = camUnity.orthographicSize;
    51.             camUI.gameObject.SetActive(false);
    52.             camUI.gameObject.SetActive(true);
    53.         }
    54.     }
    55.     public IEnumerator AssignRenderTextureNextFrame(){ // WORKAROUND unity5.3 me da un error SOLO EN STANDALONE que dice Releasing RenderTexture that is set as Camera.targetTexture
    56.         yield return null;
    57.         camUnity.targetTexture = myRenderTexture;
    58.         screenRect.texture = myRenderTexture;
    59.         if (camSky) camSky.targetTexture = myRenderTexture;
    60.         if (camUI) camUI.targetTexture = myRenderTexture;
    61.         if (shadowMaker) shadowMaker.OnSetResolution();
    62.     }
     
  11. DrKucho

    DrKucho

    Joined:
    Oct 14, 2013
    Posts:
    140
  12. Hickna

    Hickna

    Joined:
    Mar 14, 2013
    Posts:
    26
    Thanks for share DrKucho!
    So, I already tried to solve using the Create method.
    In true nothing changes using this in my case.
    In some post someone said to do something like:
    Code (CSharp):
    1. rt = new RenderTexture(256, 256, 16, RenderTextureFormat.ARGB32);
    2. if (!rt.isCreated(){
    3.     rt.Create();
    4. }
    Well... I have good news (for me at least :) ).

    I solved this issue disabling the camera that use rendertexture every time that I need to do some of this actions:
    1. When changing any attribute of RenderTexture after isCreated().
    2. When changing Screen size or Resolution of the camera where was added a RenderTexture.
    3. When changing VSyncCount of QualitySettings.
    4. When changing Antialising of QualitySettings.
    We don't need to use the WaitForEndOfFrame or even Coroutine in most cases ;) The only case for is while changing the resolution to fullscreen, because it take some time, and even with WaitForEndOfFrame I didn't have success, so was better to use a delay using WaitForSeconds(2);

    In short, all that we need is to disable the camera before some action and then enable again after all changes was completed.

    I created this method to be called from any case:
    Code (CSharp):
    1.     public void SwitchOnOffAllCameras(bool enabled) {
    2.         for (int i = 0; i < XMLHandler.Instance.layersNow; i++) {
    3.             camLayer[i].GetComponent<Camera>().enabled = enabled;
    4.         }
    5.      
    6.     }

    NOTE: In my case I use 1,2,3 or 4 cameras using RenderTexture, but you can simplify using only something like:


    Code (CSharp):
    1.     public void SwitchOnOffCameraWithRenderTexture(bool enabled) {
    2.         camera.enabled = enabled;
    3.     }

    I like to use methods to be more easy to identify where I'm changing the attribute, as I need to call it from other script too.


    So... I called that method before to change something that was causing the error.
    After all is done I called it again: SwitchOnOffAllCameras(true);

    Example 1 (quality settings):
    Code (CSharp):
    1.  
    2. SwitchOnOffAllCameras(false);
    3. QualitySettings.antiAliasing = QualitySettings.antiAliasing ==0?2: QualitySettings.antiAliasing *2;
    4. SwitchOnOffAllCameras(true);
    5.  
    Example 2 (fullscreen issue):
    Code (CSharp):
    1.    
    2. private IEnumerator SwitchFullscreen() {
    3.         CameraHandler.Instance.SwitchOnOffAllCameras(false);
    4.         if (Screen.fullScreen) {
    5.             Screen.SetResolution(940, 720, false);
    6.         } else {
    7.             Screen.SetResolution(Screen.currentResolution.width, Screen.currentResolution.height, true);
    8.         }
    9.         yield return new WaitForSeconds(2);
    10.         CameraHandler.Instance.SwitchOnOffAllCameras(true);
    11. }
    Example 3 (creating camera and RenderTexture):
    In this case I didn't use that method as I'm creating cameras and RenderTextures for the first time and would generate a null exception.

    Code (CSharp):
    1.  
    2. private void CreateCamerasForRenderTexture() {
    3.         Debug.LogAssertion("Before Create RT");
    4.         camLayer = new GameObject[XMLHandler.Instance.layersNow];
    5.         renderTextureLayer = new RenderTexture[XMLHandler.Instance.layersNow];
    6.         for (int i = 0; i < XMLHandler.Instance.layersNow; i++) {
    7.             camLayer[i] = new GameObject("CamLayerAB" + i);
    8.             camLayer[i].AddComponent<Camera>();
    9.             camLayer[i].GetComponent<Camera>().enabled = false;
    10.             camLayer[i].GetComponent<Camera>().orthographic = true;
    11.             camLayer[i].GetComponent<Camera>().orthographicSize = 0.6f * (XMLHandler.Instance.scale);
    12.             camLayer[i].transform.parent = cam.transform;
    13.             camLayer[i].transform.localPosition = Vector3.zero;
    14.             camLayer[i].transform.localRotation = Quaternion.identity;
    15.             camLayer[i].GetComponent<Camera>().backgroundColor = new Color(0, 0, 0, 0);
    16.             camLayer[i].GetComponent<Camera>().cullingMask = 1 << LayerMask.NameToLayer(XMLHandler.Instance.layerNameToApply[i]);
    17.             camLayer[i].GetComponent<Camera>().useOcclusionCulling = false;
    18.             renderTextureLayer[i] = new RenderTexture(Screen.width, Screen.height, 24, RenderTextureFormat.ARGB32);
    19.             renderTextureLayer[i].name = "RenderTextureLayerAB" + i;
    20.             camLayer[i].GetComponent<Camera>().targetTexture = renderTextureLayer[i];
    21.             camLayer[i].GetComponent<Camera>().renderingPath = RenderingPath.Forward;
    22.             camLayer[i].GetComponent<Camera>().enabled = true;
    23.         }
    24.         Debug.LogAssertion("After Create RT");
    25.     }
    26.  
    To discover where (in my case) was occurring the error I create a LOT of Debug.LogAssertion() entries to see in the console of Standalone build.

    NOTE: I listed 4 cases, but probably have more situations where the issue could happens.

    I hope that it can help someone.
     
    elqtfy, forestrf and DrKucho like this.
  13. DrKucho

    DrKucho

    Joined:
    Oct 14, 2013
    Posts:
    140
    good to know, thanks for sharing, i'll keep in mind to disable camera in case i get any more errors