/// <summary> /// </summary> /// <param name="src">RenderTexture to capture.</param> /// <param name="functor">Completion functor for handling the captured data. The object passed is a byte[] of the captured data.</param> /// <returns>AsyncRequest<object></returns> public static AsyncRequest <object> Capture(RenderTexture src, Func <AsyncRequest <object>, AsyncRequest <object> .Result> functor = null) { var req = Manager.Instance.CreateRequest <AsyncRequest <object> >(); #if !UNITY_2019_2_OR_NEWER && (PLATFORM_STANDALONE_OSX || UNITY_EDITOR) req.data = GraphicsUtilities.GetPixelsSlow(src as RenderTexture); req.Enqueue(functor); req.Execute(); #else if (GraphicsUtilities.SupportsAsyncReadback()) { AsyncGPUReadback.Request(src, 0, (AsyncGPUReadbackRequest request) => { if (request.hasError) { req.error = true; } else { req.data = request.GetData <byte>().ToArray(); req.Enqueue(functor); req.Execute(); } }); } else { req.data = GraphicsUtilities.GetPixelsSlow(src as RenderTexture); req.Enqueue(functor); req.Execute(); } #endif return(req); }
/// <summary> /// Perform async read back from the provided source texture. /// </summary> /// <param name="src">Texture source to be used for the read back.</param> /// <param name="mipIndex">Index of the mipmap to be fetched.</param> /// <param name="functor">Functor that will be invoked after the async read back request is complete.</param> /// <typeparam name="T">Type for the destination data buffer.</typeparam> /// <returns>Returns an AsyncRequest</returns> public static AsyncRequest <object> Capture <T>(Texture src, int mipIndex = 0, Func <AsyncRequest <object>, AsyncRequest <object> .Result> functor = null) where T : struct { var req = Manager.Instance.CreateRequest <AsyncRequest <object> >(); if (GraphicsUtilities.SupportsAsyncReadback()) { AsyncGPUReadback.Request(src, mipIndex, (AsyncGPUReadbackRequest request) => { req.error = request.hasError; if (!request.hasError) { req.data = request.GetData <T>().ToArray(); req.Enqueue(functor); req.Execute(); } }); } else { req.data = GraphicsUtilities.GetPixelsSlow(src as RenderTexture); req.Enqueue(functor); req.Execute(); } return(req); }
void ProcessBatch() { while (_requestsBatch.Count > 0) { var request = _requestsBatch.Dequeue(); request.InvokeCallback(GraphicsUtilities.GetPixelsSlow(request.renderTexture)); _requestsPool.Enqueue(request); } }
static void SetupCaptureRequest ( AsyncRequest <CaptureState> req, Channel channel, Camera camera, CameraEvent cameraEvent, BuiltinRenderTextureType source, GraphicsFormat format, Func <AsyncRequest <CaptureState>, AsyncRequest <CaptureState> .Result> functor, bool flipY ) { if (functor != null) { // declared for possible capture, to avoid use from other threads. var cameraTargetTexture = camera.targetTexture; RenderTexture target1 = null; RenderTexture target2 = null; Action ReleaseTargets = () => { if (target1 != null && target1 != cameraTargetTexture) { RenderTexture.ReleaseTemporary(target1); target1 = null; } if (target2 != null) { Debug.Assert(target2 != cameraTargetTexture); RenderTexture.ReleaseTemporary(target2); target2 = null; } }; Material depthMaterial = null; if (source == BuiltinRenderTextureType.Depth) { depthMaterial = SelectDepthShaderVariant(format); } #if UNITY_2019_3_OR_NEWER if (scriptableRenderPipeline) { if (CaptureOptions.useBatchReadback) { QueueForAsyncBatchReadback(req, channel, functor, SetupRenderTargets(ref target1, ref target2, camera, null, format, cameraTargetTexture, depthMaterial, flipY)); } else { req.data.SetFunctor(channel, (AsyncRequest <CaptureState> r) => { var target = SetupRenderTargets(ref target1, ref target2, camera, null, format, cameraTargetTexture, depthMaterial, flipY); if (GraphicsUtilities.SupportsAsyncReadback()) { AsyncGPUReadback.Request(target, 0, (AsyncGPUReadbackRequest request) => { ReleaseTargets(); if (request.hasError) { req.error = true; } else { if (functor != null) { req.data.SetBuffer(channel, request.GetData <byte>().ToArray()); req.Enqueue(functor); req.Execute(); } } }); } else { r.data.SetBuffer(channel, GraphicsUtilities.GetPixelsSlow(target)); ReleaseTargets(); req.Enqueue(functor); req.Execute(); } return(AsyncRequest.Result.None); }); } } else #endif // UNITY_2019_3_OR_NEWER { req.data.SetFunctor(channel, functor); CommandBuffer commandBuffer = GetCommandBufferForCamera(cameraEvent, camera); commandBuffer.name = $"CaptureCamera.{channel.ToString()}"; var target = SetupRenderTargets(ref target1, ref target2, camera, commandBuffer, format, cameraTargetTexture, depthMaterial, flipY); if (GraphicsUtilities.SupportsAsyncReadback()) { #if UNITY_2019_3_OR_NEWER if (CaptureOptions.useBatchReadback) { QueueForAsyncBatchReadback(req, channel, functor, target); ReleaseTargets(); } else #endif { commandBuffer.RequestAsyncReadback(target, (AsyncGPUReadbackRequest request) => { commandBuffer.Clear(); if (request.hasError) { req.error = true; } else { functor = req.data.SetFunctor(channel, null); if (functor != null) { req.data.SetBuffer(channel, request.GetData <byte>().ToArray()); req.Enqueue(functor); req.Execute(); } } ReleaseTargets(); }); } } else { Func <AsyncRequest <CaptureState>, AsyncRequest <CaptureState> .Result> wrapper; #if UNITY_2019_3_OR_NEWER if (CaptureOptions.useBatchReadback) { wrapper = (AsyncRequest <CaptureState> r) => { BatchReadback.Instance().QueueReadback(target, data => { r.data.SetBuffer(channel, data); ReleaseTargets(); r.Enqueue(functor); r.Execute(); return(AsyncRequest.Result.Completed); }); return(AsyncRequest.Result.Completed); }; } else #endif // UNITY_2019_3_OR_NEWER { wrapper = (AsyncRequest <CaptureState> r) => { r.data.SetBuffer(channel, GraphicsUtilities.GetPixelsSlow(target)); ReleaseTargets(); r.Enqueue(functor); r.Execute(); return(AsyncRequest.Result.Completed); }; } req.Enqueue(wrapper); req.Execute(AsyncRequest.ExecutionContext.EndOfFrame); } } } }