Hi, just tried the example from this page - https://docs.unity3d.com/ScriptReference/Graphics.DrawMeshInstancedIndirect.html All objects are gone when Unity editor looses focus and then gets on focus again.
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
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 !
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.
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): void OnApplicationFocus (bool focused) { if (focused) instanceMaterial.SetBuffer("positionBuffer", positionBuffer); }
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.
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 )