Hi all,
I’m trying to setup a pipeline where an external tool sends commands to the Editor to redraw a camera and return the output of that camera back to the tool through native code.
My initial setup was to manually call Camera.Render()
to an RT + GetPixels
every time I needed a refresh but obviously this would be too slow for a high framerate (target is around 60-90 FPS)
While trying to switch over to async readbacks - they end up working while the editor is focused but once the other tool has focus the callbacks to process the data never execute.
The setup I’m trying to use looks more or less like the following:
void Setup()
{
coroutine = EditorCoroutineUtility.StartCoroutineOwnerless(
EditorCoroutineUpdate()
);
}
IEnumerator EditorCoroutineUpdate()
{
while (true)
{
CaptureRenderTexture(); // <-- Executes regardless of editor focus
yield return new EditorWaitForSeconds(rate);
}
}
public void CaptureRenderTexture()
{
cam.Render();
AsyncGPUReadback.Request(
cam.targetTexture, 0,
0, width, 0, height,
0, 1, TextureFormat.RGB24,
OnAsyncReadback
);
}
private void OnAsyncReadback(AsyncGPUReadbackRequest request)
{
if (request.hasError)
{
Debug.LogError("GPU readback error.");
return;
}
Debug.Log("Readback"); // <--- never executes when the editor is unfocused
unsafe
{
// ... do native work ...
}
}
I can get this to work when the editor is unfocused if I do an ASyncGPUReadback.WaitAllRequests()
at the end ofCaptureRenderTexture
but then I incur a ~30ms semaphore wait on the GPU each frame.
Any other suggestions that may help here?