I have a tool that renders thousands of 4k images, then saves them to the disk. This is super fast, but with a huge bottleneck at getting the image from the gpu to the cpu. I am using this method:
Texture2D tex = new Texture2D(renderTexture.width, renderTexture.height, TextureFormat.ARGB32, false);
var oldRT = RenderTexture.active;
RenderTexture.active = renderTexture;
tex.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0, false); //SLOW!!
tex.Apply();
RenderTexture.active = oldRT;
That one method, getpixels, is making the process take hours, while the other things going on just takes minutes.
So, basically getting data from the GPU to the CPU is super slow in Unity. And to make it worse, I cannot do this in a thread as Unity won’t allow GPU-related instruction on anything else than the main render-thread. I found some threads about the issue, but they are mostly outdated.
Is there any workaround for this issue? Either a thread based approach or a native approach that gets the pixels in a faster way?