I have the following C# code in Unity version 2022.2.0a12:
[SerializeField]
public bool isLoging = true;
[SerializeField]
private RenderTexture _rt;
private NativeArray<byte> _data;
private void Start()
{
CreateBuffer(ref _data, ref _rt);
StartCoroutine(StartEncoding(_rt, isLoging));
}
System.Collections.IEnumerator StartEncoding(RenderTexture oRTs, bool active)
{
while (active)
{
yield return new WaitForEndOfFrame();
AsyncGPUReadback.RequestIntoNativeArray(ref _data, oRTs, 0, request =>
{
if (request.hasError) {
Debug.Log("GPU readback error detected.");
return;
}
Debug.Log("in Function ");
// encode to png and save
Debug.Log("saved PNG file ");
});
}
}
void CreateBuffer(ref NativeArray<byte> buffer, ref RenderTexture oRTS)
{
buffer = new NativeArray<byte>(oRTS.height * oRTS.width * 4, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
}
private void OnApplicationQuit()
{
_data.Dispose();
}
Before even the first frame ends I get this error:
1[System.Byte]
can no longer be accessed, since its owner has been invalidated.
What is happening? I can get date to be used once and then I cannot use it anymore?
My question is how can I use AsyncGPUReadback.RequestIntoNativeArray
correctly so I do not lose access to my NativeArray
?
I would like to hold on my data and pass it to Job so inside job i can do encoding to PNG .
NativeArray
, but the issue tracker reports this as fixed in 2020.3.X, 2021.2.X, and 2022.1.X. If you're on an older version, you may need to upgrade, or use the workaround in the first thread I linked. \$\endgroup\$