This is a follow up on this question I asked few days ago. I’m copying Texture2D to another Texture2D with and intent to get the raw data bytes into a buffer after copying.
Graphics.CopyTexture(videoSource, dstTexture); // also tried with Graphics.ConvertTexture
NativeArray<byte> nativeByteArray = dstTexture.GetRawTextureData<byte>();
The resulting array has correct number of members/pixels, but all its members are set to the value 205 always. Is this bug or me?
Actually refining the above as I realized the copying/converting seems not to be relevant here. I have Texture2D attached to RawImage and the image is displayed as it should. Calling
NativeArray<byte> nativeByteArray = videoSource.GetRawTextureData<byte>();
produces the problem described in the above post where all the members are at value 205.
My guess: If the texture data only lives on the GPU, you first have to read it back to the CPU via ReadPixels or AsyncGPUReadback.
Thanks again @c0d3_m0nk3y ! Using AsyncGPUReadback.RequestIntoNativeArray() I was able to get the data back correctly.
Out of curiosity and trying to understand this little bit better I have follow up question. I’m using this
Rendering.AsyncGPUReadbackRequest RequestIntoNativeArray(ref NativeArray<T> output, Texture src, int mipIndex, Action<AsyncGPUReadbackRequest> callback);
where I pass the reference to the NativeArray I want the function to write to. However, if I try to read the data from that referenced array in the callback, I’m getting garbage.
// NOT working way. This is inside the callback and texArray is the referenced NativeArray in the above function. _shareData is the final byte buffer where data needs to get written.
texArray.CopyTo(_shareData);
What I need to do to get this working, is to get the data back from the request inside callback like below.
//this is inside the callback and texArray is the referenced NativeArray in the RequestIntoNativeArray function.
nativeByteArray.Dispose();
nativeByteArray = new NativeArray<byte>(request.GetData<byte>(), Allocator.Persistent);
texArray.Dispose();
nativeByteArray.CopyTo(_shareData);
Am I thinking this wrong to assume that I should be able to use the texArray straight away in the callback without this extra NativeArray creation?
Sorry, don’t know. Maybe somebody else can answer this.