Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Is this a bug in Unity !?

Discussion in 'Shaders' started by Vagabond_, Jul 15, 2019.

  1. Vagabond_

    Vagabond_

    Joined:
    Aug 26, 2014
    Posts:
    868
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    6,973
    I'm loathe to call this a bug, but it is annoying. Most likely it's "correct" behavior by some metric, likely related to the GPU doing context switching in the background, but the short version is any data that is only on the GPU is not guaranteed to survive when switching application focus (or loading levels!). Most other data (textures, meshes, material properties etc) all don't seem to have a problem, just specifically ComputeBuffer and RenderTexture as they have no CPU side storage.

    The only solution is to refill any data set in compute buffers or render textures when the application regains focus. In the worst case you may need to use GetData() to copy the data back to the CPU when losing focus.
    https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnApplicationFocus.html
     
    Invertex and grizzly like this.
  3. Vagabond_

    Vagabond_

    Joined:
    Aug 26, 2014
    Posts:
    868
    Hi, thanks for the input. I did test that in Unity 2017.1.5 and it works ok. Objects do not disappear. Also isn't what you say also valid for build. What if i alt+tab when i am in a game and then get back to it. This is actually working in both 2017.1.5 and in latest 2019.2b9 in builds ( i did test both ) ! That's why i am thinking that this might be a bug, but wanted to be sure before fire a bug report !
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    6,973
    I think it can still happen with standalone builds, it's not not as consistent. If you want you could report it as a bug and see if Unity does anything with it.
     
    Vagabond_ likes this.
  5. grizzly

    grizzly

    Joined:
    Dec 5, 2012
    Posts:
    259
    This may be the case on some platforms, but from my observations the data survives while references are lost. So there's no need to refill the buffer, just re-assign it;
    Code (CSharp):
    1. void OnApplicationFocus (bool focused)
    2. {
    3.     if (focused) instanceMaterial.SetBuffer("positionBuffer", positionBuffer);
    4. }
     
    SugoiDev and bgolus like this.