EncodeToPNG super slow, ReadPixels also quite slow. Any faster ways to capturing camera shots?

I need to record large screenshots from multiple cameras at once - one full screen screenshot per camera. The only way I can think to do that is to tell the cameras to:

Render to texture
ReadPixels from that RenderTexture into another texture
EncodeToPNG from that other texture
Save PNG to disk

Saving to disk is actually quite fast, but the ReadPixels call and especially EncodeToPNG really slow everything down. Like, 0.3 frames per second slow. I can’t use JPG because I need the higher quality of PNG, and I can’t use Application.CaptureScreenshot because that only captures visually rendered data, not individual cameras. The only step that could be multithreaded is saving to disk, and as I said that’s actually quite fast so it wouldn’t save hardly any time.

Anybody have any ideas?

We are currently doing exactly what you’re doing for rendered-to-texture high-res screenshots and cubemap screenshots. We’ve tried various iterations of it, messed with command buffers, and also tried capturing raw and encoded frames piped via a thread to external ffmpeg processes etc.

Nothing we have done in c#/scripting has been close to realtime or very fast at all. You might have some native plugin luck on the Asset Store (sorry, nothing to point at).

Copying data from gpu → memory is the first bottleneck, ie. the gpu rendertexture → memory readable texture step. We asked Unity when they released Adam what technique they used to capture stuff in realtime and if it was going to be available publicly - it was some custom low-level directx plugin buffer magic and they were unsure if it would be released.

Is this something that needs to be done at realtime? if you have the texture object reference, why do you need to store it to disk ? or what is the need to convert it to a PNG ?

It doesn’t need to be in realtime, but better than 3 seconds per frame would be nice :slight_smile: At that speed, rendering 10 minutes of 30fps data would take like 15 hours. I can’t get into details why we are doing this, but it’s basically because we have different cameras applying special filters, and we are analyzing the results on a per-frame basis, comparing the different camera outputs to each other.

If supported (see description), Graphics.CopyTexture can provide a significant performance boost over ReadPixels. Unity - Scripting API: Graphics.CopyTexture

Try EncodeToJPEG. It’s faster.

Unfortunately, JPEG is lossy, and we need lossless images. I tried Graphics.CopyTexture, but I just get gray images - I think Graphics.CopyTexture is really for copying from GPU to GPU, but we are copying the RenderTexture to the CPU.

The docs state that you can also copy textures that are both readable on the cpu - but rendertextures are on the gpu and the readable destination texture would be on the cpu.

I know I’m late for the party, but this discussion is the first in google’s result.

I’m working on my hobby project, where I want to stream the camera view through the socket. I haven’t yet found a solution, but here is a good article about this problem:

… ReadPixels() will trigger a GPU flush… “Flushing the GPU,” means waiting for all remaining commands in the current command buffer to execute. The CPU and GPU can run in parallel, but during a flush, the CPU sits idle waiting for the GPU to become idle as well, which is why this is also known as a “synchronization point."

1 Like