0
\$\begingroup\$

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 .

\$\endgroup\$
3

0

You must log in to answer this question.

Browse other questions tagged .