Exemple #1
0
        static void QueueForAsyncBatchReadback(AsyncRequest <CaptureState> req,
                                               Channel channel,
                                               Func <AsyncRequest <CaptureState>, AsyncRequest.Result> functor,
                                               RenderTexture target)
        {
            Func <AsyncRequest <CaptureState>, AsyncRequest <CaptureState> .Result> wrapper;

            wrapper = (AsyncRequest <CaptureState> r) =>
            {
                BatchReadback.Instance().QueueReadback(target, bytes =>
                {
                    if (functor != null)
                    {
                        r.data.SetBuffer(channel, bytes);
                        r.Enqueue(functor);
                        r.Execute();
                    }

                    return(AsyncRequest.Result.Completed);
                });
                return(AsyncRequest.Result.Completed);
            };

            req.Enqueue(wrapper);
            req.Execute(AsyncRequest.ExecutionContext.EndOfFrame);
        }
Exemple #2
0
 /// <summary>
 /// Invoke the request functor upon completion of the readback request.
 /// </summary>
 /// <param name="data"></param>
 public void InvokeCallback(byte[] data)
 {
     Debug.Assert(data != null && data.Length != 0);
     Debug.Assert(callback != null);
     request.data.SetBuffer(channel, data);
     request.Enqueue(callback);
     request.Execute();
 }
Exemple #3
0
        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);
                    }
                }
            }
        }