Search Unity

Is this a bug in Unity !?

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

  1. Vagabond_

    Vagabond_

    Joined:
    Aug 26, 2014
    Posts:
    1,148
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,342
    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:
    1,148
    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:
    12,342
    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:
    357
    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.
  6. a436t4ataf

    a436t4ataf

    Joined:
    May 19, 2013
    Posts:
    1,932
    Following Unity's examples ... I'm filling vertex shader input (for instanced rendering) using: Material.SetBuffer(...).

    Unity deletes the buffers any time it does a refresh (even e.g. recompiling in editor / rebuilding assemblies).

    This feels like it cannot be correct: the whole point of a Material object is to collate the data needed to configure a Shader, and persist it.

    So ... either I'm misusing the API (there's another way to fill vertex-shader's instanced / per-vertex data?) and I'd have some suggestions to Unity to update their docs on isntnced rendering :), or this is a bug.
     
  7. a436t4ataf

    a436t4ataf

    Joined:
    May 19, 2013
    Posts:
    1,932
    But ... computebuffer is (wrongly?) one of the material properties, so ...

    Link: https://docs.unity3d.com/ScriptReference/Material.SetBuffer.html

    (obviously, this is Unity, so there's no actual documentation on the official API documentation link :D )