private static int AddToList(List <CustomDrawRequest> targetLst, CustomDrawRequest ths)
        {
            int index = targetLst.Count;

            targetLst.Add(ths);
            return(index);
        }
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
            if (current == this)
            {
                current = null;
            }
            iRunnableObjects.Dispose();
            CustomDrawRequest.Dispose();
            if (m_afterFrameBuffer != null)
            {
                m_afterFrameBuffer.Dispose();
                m_afterFrameBuffer = null;
            }
            if (m_beforeFrameBuffer != null)
            {
                m_beforeFrameBuffer.Dispose();
                m_beforeFrameBuffer = null;
            }

            try
            {
                eventsGuideBook.Dispose();
                waitReleaseRT.Dispose();
            }
            catch { }
            SceneController.Dispose(resources);
            resources.loadingThread.Dispose();
            data.buffer.Dispose();
            var allEvents = resources.allEvents;

            for (int i = 0; i < resources.availiableEvents.Length; ++i)
            {
                resources.availiableEvents[i].DisposeEvent();
            }
            for (int i = 0; i < resources.availiableEvents.Length; ++i)
            {
                resources.availiableEvents[i].DisposeDependEventsList();
            }

            foreach (var camPtr in PipelineCamera.CameraSearchDict)
            {
                PipelineCamera cam = MUnsafeUtility.GetObject <PipelineCamera>((void *)camPtr.value);
                if (cam.allDatas.isCreated)
                {
                    foreach (var i in cam.allDatas)
                    {
                        IPerCameraData data = ((IPerCameraData)MUnsafeUtility.GetHookedObject(i.value));
                        data.DisposeProperty();
                        MUnsafeUtility.RemoveHookedObject(i.value);
                    }
                    cam.allDatas.Dispose();
                }
            }
            if (motionVectorMatricesBuffer != null)
            {
                motionVectorMatricesBuffer.Dispose();
            }
            MotionVectorDrawer.Dispose();
        }
        public RenderPipeline(PipelineResources resources)
        {
            current        = this;
            this.resources = resources;
            if (resources.loadingThread == null)
            {
                resources.loadingThread = new LoadingThread();
            }
            resources.loadingThread.Init();
            SceneController.Awake(resources);
            eventsGuideBook = new NativeDictionary <UIntPtr, int, PtrEqual>(resources.availiableEvents.Length, Allocator.Persistent, new PtrEqual());
            resources.SetRenderingPath();
            var allEvents = resources.allEvents;

            GraphicsUtility.UpdatePlatform();
            MLight.ClearLightDict();
            CustomDrawRequest.Initialize();
            data.buffer = new CommandBuffer();
            for (int i = 0; i < resources.availiableEvents.Length; ++i)
            {
                resources.availiableEvents[i].InitDependEventsList();
            }
            for (int i = 0; i < resources.availiableEvents.Length; ++i)
            {
                eventsGuideBook.Add(new UIntPtr(MUnsafeUtility.GetManagedPtr(resources.availiableEvents[i].GetType())), i);
                resources.availiableEvents[i].Prepare();
            }
            for (int i = 0; i < resources.availiableEvents.Length; ++i)
            {
                resources.availiableEvents[i].InitEvent(resources);
            }
            waitReleaseRT = new NativeList <int>(20, Allocator.Persistent);
        }
        private static CustomDrawRequest RemoveFromList(List <CustomDrawRequest> targetLst, int targetIndex)
        {
            targetLst[targetIndex] = targetLst[targetLst.Count - 1];
            CustomDrawRequest cdr = targetLst[targetIndex];

            targetLst.RemoveAt(targetLst.Count - 1);
            return(cdr);
        }
            public void Execute(int index)
            {
                CustomDrawRequest cdr = CustomDrawRequest.AllEvents[index];

                if (cdr.Cull(frustumPlanes))
                {
                    cullResult.ConcurrentAdd(index);
                }
            }
Example #6
0
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
            if (current == this)
            {
                current = null;
            }
            iRunnableObjects.Dispose();
            CustomDrawRequest.Dispose();
            if (m_afterFrameBuffer != null)
            {
                m_afterFrameBuffer.Dispose();
                m_afterFrameBuffer = null;
            }
            if (m_beforeFrameBuffer != null)
            {
                m_beforeFrameBuffer.Dispose();
                m_beforeFrameBuffer = null;
            }

            try
            {
                eventsGuideBook.Dispose();
                waitReleaseRT.Dispose();
            }
            catch { }
            SceneController.Dispose(resources);
            resources.loadingThread.Dispose();
            data.buffer.Dispose();
            var allEvents = resources.allEvents;

            for (int i = 0; i < resources.availiableEvents.Length; ++i)
            {
                resources.availiableEvents[i].DisposeEvent();
            }
            for (int i = 0; i < resources.availiableEvents.Length; ++i)
            {
                resources.availiableEvents[i].DisposeDependEventsList();
            }

            foreach (var cam in PipelineCamera.allCameras)
            {
                var values = cam.allDatas.Values;
                foreach (var j in values)
                {
                    j.DisposeProperty();
                }
                cam.allDatas.Clear();
            }

            if (motionVectorMatricesBuffer != null)
            {
                motionVectorMatricesBuffer.Dispose();
            }
            MotionVectorDrawer.Dispose();
        }
Example #7
0
 private void OnDisable()
 {
     if (allDrawRequests.Count <= 1)
     {
         allDrawRequests.Clear();
     }
     else
     {
         allDrawRequests[currentIndex] = allDrawRequests[allDrawRequests.Count - 1];
         CustomDrawRequest cdr = allDrawRequests[currentIndex];
         cdr.currentIndex = currentIndex;
         allDrawRequests.RemoveAt(allDrawRequests.Count - 1);
     }
     OnDisableFunc();
 }
        protected override void Render(ScriptableRenderContext renderContext, Camera[] cameras)
        {
            currentFrameCamera.Clear();
            PipelineCamera.InitializeDict();
            foreach (var cam in cameras)
            {
                CameraSetting csg;
                ulong         pipelineCamPtr;
                if (!PipelineCamera.CameraSearchDict.Get(cam.gameObject.GetInstanceID(), out pipelineCamPtr))
                {
                    if (!cam.TryGetComponent(out csg.pipeCam))
                    {
#if UNITY_EDITOR
                        if (cam.cameraType == CameraType.SceneView)
                        {
                            csg.isRenderingEditor = true;
                            var pos = cam.transform.eulerAngles;
                            pos.z = 0;
                            cam.transform.eulerAngles = pos;
                            if (!Camera.main || !(csg.pipeCam = Camera.main.GetComponent <PipelineCamera>()))
                            {
                                continue;
                            }
                        }
                        else if (cam.cameraType == CameraType.Game)
                        {
                            csg.isRenderingEditor = false;
                            csg.pipeCam           = cam.gameObject.AddComponent <PipelineCamera>();
                        }
                        else
                        {
                            continue;
                        }
#else
                        csg.isRenderingEditor = false;
                        csg.pipeCam           = cam.gameObject.AddComponent <PipelineCamera>();
#endif
                    }
                    else
                    {
                        csg.isRenderingEditor = false;
                        csg.pipeCam.AddToDict();
                    }
                }
                else
                {
                    csg.isRenderingEditor = false;
                    csg.pipeCam           = MUnsafeUtility.GetObject <PipelineCamera>((void *)pipelineCamPtr);
                }
                csg.cam         = cam;
                csg.pipeCam.cam = cam;
                csg.pipeCam.BeforeFrameRendering();
                currentFrameCamera.Add(csg);
            }
            bool *propertyCheckedFlags = stackalloc bool[resources.allEvents.Length];
            bool  needSubmit           = false;
            CustomDrawRequest.Initialize();
            UnsafeUtility.MemClear(propertyCheckedFlags, resources.allEvents.Length);
            SceneController.SetState();
            data.context   = renderContext;
            data.resources = resources;
            if (motionVectorMatricesBuffer == null || !motionVectorMatricesBuffer.IsValid())
            {
                motionVectorMatricesBuffer = new ComputeBuffer(MotionVectorDrawer.Capacity, sizeof(float3x4));
            }
            else if (motionVectorMatricesBuffer.count < MotionVectorDrawer.Capacity)
            {
                motionVectorMatricesBuffer.Dispose();
                motionVectorMatricesBuffer = new ComputeBuffer(MotionVectorDrawer.Capacity, sizeof(float3x4));
            }
            MotionVectorDrawer.ExecuteBeforeFrame(motionVectorMatricesBuffer);
            data.buffer.SetGlobalBuffer(ShaderIDs._LastFrameModel, motionVectorMatricesBuffer);

#if UNITY_EDITOR
            int tempID = Shader.PropertyToID("_TempRT");

            foreach (var pair in bakeList)
            {
                PipelineCamera pipelineCam = pair.pipelineCamera;
                for (int i = 0; i < pair.worldToCamera.Length; ++i)
                {
                    pipelineCam.cam.worldToCameraMatrix = pair.worldToCamera[i];
                    pipelineCam.cam.projectionMatrix    = pair.projection[i];
                    pipelineCam.cameraTarget            = tempID;
                    data.buffer.GetTemporaryRT(tempID, pair.texArray.width, pair.texArray.height, pair.texArray.depth, FilterMode.Point, pair.texArray.format, RenderTextureReadWrite.Linear);
                    Render(pipelineCam, ref renderContext, pipelineCam.cam, propertyCheckedFlags);
                    data.buffer.CopyTexture(tempID, 0, 0, pair.texArray, i, 0);
                    data.buffer.ReleaseTemporaryRT(tempID);
                    data.ExecuteCommandBuffer();
                    renderContext.Submit();
                    needSubmit = false;
                }
                pair.worldToCamera.Dispose();
                pair.projection.Dispose();
                renderContext.ExecuteCommandBuffer(pair.buffer);
                pair.buffer.Clear();
                renderContext.Submit();
                needSubmit = false;
            }
            bakeList.Clear();
#endif
            foreach (var cam in preFrameRenderCamera)
            {
                Render(cam, ref renderContext, cam.cam, propertyCheckedFlags);
                data.ExecuteCommandBuffer();
                renderContext.Submit();
                needSubmit = false;
            }
            preFrameRenderCamera.Clear();
            if (CustomDrawRequest.allEvents.Count > 0 || JobProcessEvent.allEvents.Count > 0)
            {
                foreach (var i in CustomDrawRequest.allEvents)
                {
                    i.PrepareJob(resources);
                }
                foreach (var i in JobProcessEvent.allEvents)
                {
                    i.PrepareJob();
                }
                JobHandle.ScheduleBatchedJobs();
                foreach (var i in CustomDrawRequest.allEvents)
                {
                    i.FinishJob();
                }
                foreach (var i in JobProcessEvent.allEvents)
                {
                    i.FinishJob();
                }
            }
            if (Application.isPlaying && resources.clusterResources)
            {
                resources.clusterResources.UpdateData(data.buffer, resources);
            }
            resources.loadingThread.Update();
            if (useBeforeFrameBuffer)
            {
                renderContext.ExecuteCommandBuffer(m_beforeFrameBuffer);
                m_beforeFrameBuffer.Clear();
                needSubmit           = true;
                useBeforeFrameBuffer = false;
            }
            if (iRunnableObjects.isCreated)
            {
                foreach (var i in iRunnableObjects)
                {
                    IPipelineRunnable func = MUnsafeUtility.GetObject <IPipelineRunnable>((void *)i.value);
                    func.PipelineUpdate(ref data);
                }
            }
            if (cameras.Length > 0)
            {
                data.buffer.SetGlobalVector(ShaderIDs._SceneOffset, new float4(sceneOffset, 1));
            }
            foreach (var cam in currentFrameCamera)
            {
                renderingEditor = cam.isRenderingEditor;

                Render(cam.pipeCam, ref renderContext, cam.cam, propertyCheckedFlags);
                data.ExecuteCommandBuffer();
#if UNITY_EDITOR
                if (renderingEditor)
                {
                    renderContext.DrawGizmos(cam.cam, GizmoSubset.PostImageEffects);
                    ScriptableRenderContext.EmitWorldGeometryForSceneView(cam.cam);
                }
#endif
                renderContext.Submit();
                needSubmit = false;
            }

            if (useAfterFrameBuffer)
            {
                renderContext.ExecuteCommandBuffer(m_afterFrameBuffer);
                m_afterFrameBuffer.Clear();
                needSubmit          = true;
                useAfterFrameBuffer = false;
            }
            if (needSubmit)
            {
                renderContext.Submit();
            }
            MotionVectorDrawer.ExecuteAfterFrame();
            sceneOffset = 0;
            foreach (var cam in currentFrameCamera)
            {
                cam.pipeCam.cam = cam.cam;
                cam.pipeCam.AfterFrameRendering();
            }
        }
        protected override void Render(ScriptableRenderContext renderContext, Camera[] cameras)
        {
            bool *propertyCheckedFlags = stackalloc bool[resources.allEvents.Length];
            bool  needSubmit           = false;

            CustomDrawRequest.Initialize();
            UnsafeUtility.MemClear(propertyCheckedFlags, resources.allEvents.Length);
            GraphicsSettings.useScriptableRenderPipelineBatching = resources.useSRPBatcher;
            SceneController.SetState();
            data.context   = renderContext;
            data.resources = resources;
            if (motionVectorMatricesBuffer == null || !motionVectorMatricesBuffer.IsValid())
            {
                motionVectorMatricesBuffer = new ComputeBuffer(MotionVectorDrawer.Capacity, sizeof(float3x4));
            }
            else if (motionVectorMatricesBuffer.count < MotionVectorDrawer.Capacity)
            {
                motionVectorMatricesBuffer.Dispose();
                motionVectorMatricesBuffer = new ComputeBuffer(MotionVectorDrawer.Capacity, sizeof(float3x4));
            }
            MotionVectorDrawer.ExecuteBeforeFrame(motionVectorMatricesBuffer);
            data.buffer.SetGlobalBuffer(ShaderIDs._LastFrameModel, motionVectorMatricesBuffer);

#if UNITY_EDITOR
            int tempID = Shader.PropertyToID("_TempRT");

            foreach (var pair in bakeList)
            {
                PipelineCamera pipelineCam = pair.pipelineCamera;
                for (int i = 0; i < pair.worldToCamera.Length; ++i)
                {
                    pipelineCam.cam.worldToCameraMatrix = pair.worldToCamera[i];
                    pipelineCam.cam.projectionMatrix    = pair.projection[i];
                    pipelineCam.cameraTarget            = tempID;
                    data.buffer.GetTemporaryRT(tempID, pair.texArray.width, pair.texArray.height, pair.texArray.depth, FilterMode.Point, pair.texArray.format, RenderTextureReadWrite.Linear);
                    Render(pipelineCam, ref renderContext, pipelineCam.cam, propertyCheckedFlags);
                    data.buffer.CopyTexture(tempID, 0, 0, pair.texArray, i, 0);
                    data.buffer.ReleaseTemporaryRT(tempID);
                    data.ExecuteCommandBuffer();
                    renderContext.Submit();
                    needSubmit = false;
                }
                pair.worldToCamera.Dispose();
                pair.projection.Dispose();
                renderContext.ExecuteCommandBuffer(pair.buffer);
                pair.buffer.Clear();
                renderContext.Submit();
                needSubmit = false;
            }
            bakeList.Clear();
#endif
            foreach (var cam in preFrameRenderCamera)
            {
                Render(cam, ref renderContext, cam.cam, propertyCheckedFlags);
                data.ExecuteCommandBuffer();
                renderContext.Submit();
                needSubmit = false;
            }
            preFrameRenderCamera.Clear();

            if (CustomDrawRequest.allEvents.Count > 0 || JobProcessEvent.allEvents.Count > 0)
            {
                foreach (var i in CustomDrawRequest.allEvents)
                {
                    i.PrepareJob(resources);
                }
                foreach (var i in JobProcessEvent.allEvents)
                {
                    i.PrepareJob();
                }
                JobHandle.ScheduleBatchedJobs();
                foreach (var i in CustomDrawRequest.allEvents)
                {
                    i.FinishJob();
                }
            }
            if (useBeforeFrameBuffer)
            {
                renderContext.ExecuteCommandBuffer(m_beforeFrameBuffer);
                m_beforeFrameBuffer.Clear();
                needSubmit           = true;
                useBeforeFrameBuffer = false;
            }
            foreach (var cam in cameras)
            {
                PipelineCamera pipelineCam = cam.GetComponent <PipelineCamera>();
                if (!pipelineCam)
                {
#if UNITY_EDITOR
                    if (cam.cameraType == CameraType.SceneView)
                    {
                        renderingEditor = true;
                        var pos = cam.transform.eulerAngles;
                        pos.z = 0;
                        cam.transform.eulerAngles = pos;
                        if (!Camera.main || !(pipelineCam = Camera.main.GetComponent <PipelineCamera>()))
                        {
                            continue;
                        }
                    }
                    else if (cam.cameraType == CameraType.Game)
                    {
                        renderingEditor = false;
                        pipelineCam     = cam.gameObject.AddComponent <PipelineCamera>();
                    }
                    else
                    {
                        continue;
                    }
#else
                    renderingEditor = false;
                    pipelineCam     = cam.gameObject.AddComponent <PipelineCamera>();
#endif
                }
                else
                {
                    renderingEditor = false;
                }
                Render(pipelineCam, ref renderContext, cam, propertyCheckedFlags);
                data.ExecuteCommandBuffer();
#if UNITY_EDITOR
                if (renderingEditor)
                {
                    renderContext.DrawGizmos(cam, GizmoSubset.PostImageEffects);
                }
#endif
                renderContext.Submit();
                needSubmit = false;
            }

            if (useAfterFrameBuffer)
            {
                renderContext.ExecuteCommandBuffer(m_afterFrameBuffer);
                m_afterFrameBuffer.Clear();
                needSubmit          = true;
                useAfterFrameBuffer = false;
            }
            if (needSubmit)
            {
                renderContext.Submit();
            }
            MotionVectorDrawer.ExecuteAfterFrame();
            foreach (var i in JobProcessEvent.allEvents)
            {
                i.FinishJob();
            }
        }