示例#1
0
        public LightLoopLightsData lightLoopLightsData;   // = new LightLoopLightsData();

        public void Initilize(PipelineSettings pipelineSettings)
        {
            bool needRebuild = false;

            if (NumMaxLights != pipelineSettings.MaxItemsOnScreen)
            {
                needRebuild = true;
            }
            if (NativeLightsBoundList == null ||
                NativeLightsDataList == null)
            {
                needRebuild = true;
            }

            if (needRebuild)
            {
                Dispose();
                NumMaxLights          = pipelineSettings.MaxItemsOnScreen;
                NativeLightsBoundList = new NativeArray <SFiniteLightBound>(NumMaxLights, Allocator.Persistent);
                LightsSphereList      = new NativeArray <Sphere>(NumMaxLights, Allocator.Persistent);
                NativeLightsDataList  = new NativeArray <LightData>(NumMaxLights, Allocator.Persistent);
                lights.Clear();
                culsterDataGenerator = new CulsterDataGenerator();
                tileAndClusterData   = new TileAndClusterData();
                lightLoopLightsData  = new LightLoopLightsData();
                culsterDataGenerator.Initilize(pipelineSettings);
                tileAndClusterData.Initilize(pipelineSettings.NumClusters, pipelineSettings.MaxItemsPerCluster);
                lightLoopLightsData.Initialize(pipelineSettings.MaxItemsOnScreen);

                LightsDataForShadow.Allocate(NumMaxLights);
            }
        }
示例#2
0
        private static void InitializeCameraData(PipelineSettings settings, Camera camera, out CameraData cameraData)
        {
            cameraData        = new CameraData();
            cameraData.camera = camera;
            bool msaaEnabled = camera.allowMSAA && settings.msaaSampleCount > 1;

            if (msaaEnabled)
            {
                cameraData.msaaSamples = (camera.targetTexture != null) ? camera.targetTexture.antiAliasing : settings.msaaSampleCount;
            }
            else
            {
                cameraData.msaaSamples = 1;
            }

            cameraData.isSceneViewCamera = camera.cameraType == CameraType.SceneView;
            //cameraData.isOffscreenRender = camera.targetTexture != null && !cameraData.isSceneViewCamera;
            //cameraData.isHdrEnabled = camera.allowHDR;

            Rect cameraRect = camera.rect;

            var  commonOpaqueFlags        = SortFlags.CommonOpaque;
            var  noFrontToBackOpaqueFlags = SortFlags.SortingLayer | SortFlags.RenderQueue | SortFlags.OptimizeStateChanges | SortFlags.CanvasOrder;
            bool hasHSRGPU = SystemInfo.hasHiddenSurfaceRemovalOnGPU;
            bool canSkipFrontToBackSorting = (camera.opaqueSortMode == OpaqueSortMode.Default && hasHSRGPU) || camera.opaqueSortMode == OpaqueSortMode.NoDistanceSort;

            cameraData.defaultOpaqueSortFlags = canSkipFrontToBackSorting ? noFrontToBackOpaqueFlags : commonOpaqueFlags;
            cameraData.Refresh(camera, settings);
        }
示例#3
0
 public void Begin(PipelineSettings settings)
 {
     ClustersAABBs         = new NativeArray <AABB>(settings.NumClusters, Allocator.TempJob);
     ResultClusterNumItems = new NativeArray <clusternumlights_t>(settings.NumClusters, Allocator.TempJob);
     ResultItemsIDList     = new NativeArray <uint>(settings.MaxItemsPerCluster * settings.NumClusters, Allocator.TempJob);
     ResultItemsIDVec      = new NativeMultiHashMap <int, uint>(settings.NumClusters * settings.MaxItemsPerCluster, Allocator.TempJob);
 }
示例#4
0
        public static void RenderSingleCamera(DoomSRPPipeline pipelineInstance, ScriptableRenderContext context, Camera camera, ref CullResults cullResults, IRendererSetup setup = null)
        {
            if (pipelineInstance == null)
            {
                Debug.LogError("Trying to render a camera with an invalid render pipeline instance.");
                return;
            }

            ScriptableCullingParameters cullingParameters;

            if (!CullResults.GetCullingParameters(camera, out cullingParameters))
            {
                return;
            }

            CommandBuffer cmd = CommandBufferPool.Get(k_RenderCameraTag);

            using (new ProfilingSample(cmd, k_RenderCameraTag))
            {
                CameraData         cameraData;
                PipelineSettings   settings  = pipelineInstance.settings;
                ScriptableRenderer renderer  = pipelineInstance.renderer;
                LightLoop          lightloop = pipelineInstance.GetLightLoop();
                InitializeCameraData(pipelineInstance.settings, camera, out cameraData);
                SetupPerCameraShaderConstants(cameraData);

                cullingParameters.shadowDistance = Mathf.Min(cameraData.maxShadowDistance, camera.farClipPlane);
                context.ExecuteCommandBuffer(cmd);
                cmd.Clear();

#if UNITY_EDITOR
                // Emit scene view UI
                if (cameraData.isSceneViewCamera)
                {
                    ScriptableRenderContext.EmitWorldGeometryForSceneView(camera);
                }
#endif
                CullResults.Cull(ref cullingParameters, context, ref cullResults);

                RenderingData renderingData;
                InitializeRenderingData(settings, ref cameraData, ref cullResults, ref lightloop, out renderingData);
                var setupToUse = setup;
                if (setupToUse == null)
                {
                    setupToUse = defaultRendererSetup;
                }

                renderer.Clear();
                setupToUse.Setup(renderer, ref renderingData);
                renderer.Execute(context, ref renderingData);
            }
            context.ExecuteCommandBuffer(cmd);
            CommandBufferPool.Release(cmd);
            context.Submit();
#if UNITY_EDITOR
            Handles.DrawGizmos(camera);
#endif
        }
示例#5
0
        public void ClearNumItems(PipelineSettings pipelineSettings)
        {
            int maxItemsPerCluster = pipelineSettings.MaxItemsPerCluster;

            for (int i = 0; i < pipelineSettings.NumClusters; ++i)
            {
                clusternumlights_t clusterInfo;
                clusterInfo.offset       = maxItemsPerCluster * i;
                clusterInfo.numItems     = 0;
                ResultClusterNumItems[i] = clusterInfo;
            }
        }
示例#6
0
        static void InitializeLightData(PipelineSettings settings, List <VisibleLight> visibleLights, LightLoop lightloop, out LightsData lightData)
        {
            //        public int mainLightIndex;
            //public int additionalLightsCount;
            //public int maxPerObjectAdditionalLightsCount;
            //public List<VisibleLight> visibleLights;
            //public bool shadeAdditionalLightsPerVertex;
            //public bool supportsMixedLighting;


            lightData.visibleLights = visibleLights;
            lightData.lightLoop     = lightloop;
            //lightData.mainLightIndex = 0;
        }
示例#7
0
        public void Initilize(PipelineSettings settings)
        {
            //pipelineSettings = settings;
            //pipelineSettings.Check();

            //CleanUp();
#if UNITY_EDITOR
            ClustersAABBsCache = new NativeArray <AABB>(settings.NumClusters, Allocator.Persistent);
#endif

            generateClusterJob = new GenerateClusterJob();
            //pointLightListGenJob = new PointLightListGenJob();
            pointLightListGenJobSingleLine = new PointLightListGenJobSingleLine();
            pointLightListGenJob           = new PointLightListGenJob();
        }
示例#8
0
        private void Set(CameraData cameraData, PipelineSettings pipelineSettings, LightLoop lightLoop)
        {
            generateClusterJob.ClusterCB_GridDimX         = pipelineSettings.NumClusterX;
            generateClusterJob.ClusterCB_GridDimY         = pipelineSettings.NumClusterY;
            generateClusterJob.ClusterCB_GridDimZ         = pipelineSettings.NumClusterZ;
            generateClusterJob.ClusterCB_ViewNear         = cameraData.zNear;
            generateClusterJob.ClusterCB_SizeX            = ((float)cameraData.pixelWidth / (float)pipelineSettings.NumClusterX);
            generateClusterJob.ClusterCB_SizeY            = ((float)cameraData.pixelHeight / (float)pipelineSettings.NumClusterY);
            generateClusterJob.ClusterCB_NearK            = 1.0f + cameraData.sD;
            generateClusterJob.ClusterCB_ScreenDimensions = new Vector4(
                cameraData.pixelWidth,
                cameraData.pixelHeight,
                1.0f / (float)cameraData.pixelWidth,
                1.0f / (float)cameraData.pixelHeight
                );
            generateClusterJob.ClusterCB_InverseProjectionMatrix = cameraData.GPUProjectMatrix.inverse;
            generateClusterJob.cameraToWorldMatrix = cameraData.cameraToWorldMatrix;
            generateClusterJob._CameraWorldMatrix  = cameraData._CameraWorldMatrix;
            generateClusterJob.ScreenSizeX         = cameraData.pixelWidth;
            generateClusterJob.ScreenSizeY         = cameraData.pixelHeight;
            generateClusterJob.NearZ              = cameraData.zNear;
            generateClusterJob.FarZ               = cameraData.zFar;
            generateClusterJob.ClusterCG_ZDIV     = cameraData.ClusterdLighting.z;
            generateClusterJob.ClusterdLighting   = cameraData.ClusterdLighting;
            generateClusterJob.ResultClusterAABBS = ClustersAABBs;

            pointLightListGenJobSingleLine.NumClusters           = pipelineSettings.NumClusters;
            pointLightListGenJobSingleLine.VisibleLightsCount    = lightLoop.VisibleLight;
            pointLightListGenJobSingleLine.LightBounds           = lightLoop.LigtsBoundList;
            pointLightListGenJobSingleLine.IsClusterEditorHelper = pipelineSettings.IsClusterEditorHelper;
            pointLightListGenJobSingleLine._CameraWorldMatrix    = cameraData._CameraWorldMatrix;
            pointLightListGenJobSingleLine.NumItemsPerCluster    = LightDefins.MaxItemsPerCluster;
            pointLightListGenJobSingleLine.InputClusterAABBS     = ClustersAABBs;
            pointLightListGenJobSingleLine.ResultClusterNumItems = ResultClusterNumItems;
            pointLightListGenJobSingleLine.ResultItemsIDList     = ResultItemsIDList;


            pointLightListGenJob.NumClusters           = pipelineSettings.NumClusters;
            pointLightListGenJob.VisibleLightsCount    = lightLoop.VisibleLight;
            pointLightListGenJob.LightBounds           = lightLoop.LigtsBoundList;
            pointLightListGenJob.IsClusterEditorHelper = pipelineSettings.IsClusterEditorHelper;
            pointLightListGenJob._CameraWorldMatrix    = cameraData._CameraWorldMatrix;
            pointLightListGenJob.NumItemsPerCluster    = LightDefins.MaxItemsPerCluster;
            pointLightListGenJob.InputClusterAABBS     = ClustersAABBs;
            pointLightListGenJob.ResultClusterNumItems = ResultClusterNumItems;
            pointLightListGenJob.ResultItemsIDVec      = ResultItemsIDVec.ToConcurrent();
        }
示例#9
0
        public void Refresh(Camera camera, PipelineSettings pipelineSettings)
        {
            fieldOfViewY = camera.fieldOfView * Mathf.Deg2Rad * 0.5f; //Degree 2 Radiance:  Param.CameraInfo.Property.Perspective.fFovAngleY * 0.5f;
            zNear        = camera.nearClipPlane;                      // Param.CameraInfo.Property.Perspective.fMinVisibleDistance;
            zFar         = camera.farClipPlane;                       // Param.CameraInfo.Property.Perspective.fMaxVisibleDistance;

            // Number of clusters in the screen X direction.
            clusterDimX = pipelineSettings.NumClusterX; /* Mathf.CeilToInt(Screen.width / (float)m_ClusterGridBlockSize);*/
            // Number of clusters in the screen Y direction.
            clusterDimY = pipelineSettings.NumClusterY; //Mathf.CeilToInt(Screen.height / (float)m_ClusterGridBlockSize);

            // The depth of the cluster grid during clustered rendering is dependent on the
            // number of clusters subdivisions in the screen Y direction.
            // Source: Clustered Deferred and Forward Shading (2012) (Ola Olsson, Markus Billeter, Ulf Assarsson).
            sD      = 2.0f * Mathf.Tan(fieldOfViewY) / (float)clusterDimY;
            logDimY = 1.0f / Mathf.Log(1.0f + sD);

            float logDepth = Mathf.Log(zFar / zNear);

            logDepth         = Mathf.Log(zFar / zNear);
            pixelWidth       = camera.pixelWidth;
            pixelHeight      = camera.pixelHeight;
            projectMatrix    = camera.projectionMatrix;
            GPUProjectMatrix = GL.GetGPUProjectionMatrix(projectMatrix, false);

            ClusterdLighting.z = (zFar - zNear) / (float)pipelineSettings.NumClusterZ;
            ClusterdLighting.w = 1.0f / zNear;

            _ClusterCB_Size.x = pixelWidth / (float)clusterDimX;
            _ClusterCB_Size.y = pixelHeight / (float)clusterDimY;
            _ClusterCB_Size.z = logDepth / logDimY;


            ClusterdLighting.x = zNear;
            ClusterdLighting.y = Mathf.Log(zFar / zNear, 2) / (float)pipelineSettings.NumClusterZ;
            //ClusterdLighting.y = zFar;
            WorldToCameraSpace = camera.worldToCameraMatrix;

            ClusterCameraParam.x = camera.farClipPlane;
            ClusterCameraParam.y = camera.nearClipPlane;

            cameraToWorldMatrix = camera.cameraToWorldMatrix;
            _CameraWorldMatrix  = camera.cameraToWorldMatrix * Matrix4x4.Scale(new Vector3(1, 1, -1));// camera.transform.localToWorldMatrix;

            screenSize = new Vector4(pixelWidth, pixelHeight, 1.0f / pixelWidth, 1.0f / pixelWidth);
        }
示例#10
0
        static void InitializeShadowData(PipelineSettings settings, LightLoop lightLoop, ref ShadowData shadowData)
        {
            m_ShadowBiasData.Clear();
            var lights = lightLoop.lights;

            for (int i = 0; i < lights.size; ++i)
            {
                var light = lights[i];
                m_ShadowBiasData.Add(new Vector4(light.shadowBias, light.shadowNormalBias, 0.0f, 0.0f));
            }

            shadowData.bias = m_ShadowBiasData;

            // Until we can have keyword stripping forcing single cascade hard shadows on gles2
            bool supportsScreenSpaceShadows = SystemInfo.graphicsDeviceType != GraphicsDeviceType.OpenGLES2;

            // we resolve shadows in screenspace when cascades are enabled to save ALU as computing cascade index + shadowCoord on fragment is expensive
            shadowData.requiresScreenSpaceShadowResolve = supportsScreenSpaceShadows;
            shadowData.supportsSoftShadows      = settings.supportsSoftShadows && shadowData.supportsSoftShadows;
            shadowData.shadowmapDepthBufferBits = 16;
        }
示例#11
0
        public void Run(/*Camera camera*/ CameraData cameraData, PipelineSettings pipelineSettings, LightLoop lightLoop)
        {
            UnityEngine.Profiling.Profiler.BeginSample("RebuildLightsList run");
            Set(cameraData, pipelineSettings, lightLoop);
            UnityEngine.Profiling.Profiler.BeginSample("RebuildLightsList run generateClusterJob");
            // Schedule the job with one Execute per index in the results array and only 1 item per processing batch
            generateClusterJob.Run(pipelineSettings.NumClusters);
            JobHandle generateClusterJobHandle = generateClusterJob.Schedule(pipelineSettings.NumClusters, 1);

            UnityEngine.Profiling.Profiler.EndSample();
            UnityEngine.Profiling.Profiler.BeginSample("RebuildLightsList run pointLightListGenJobSingleLine");
            //pointLightListGenJob.Run(pipelineSettings.NumClusters);
            JobHandle pointLightListGenJobHandle = pointLightListGenJob.Schedule(pipelineSettings.NumClusters, 8, generateClusterJobHandle);

            JobHandle.ScheduleBatchedJobs();
            pointLightListGenJobHandle.Complete();
            UnityEngine.Profiling.Profiler.EndSample();
            //JobHandle pointLightListGenJobSingleLineHandle = pointLightListGenJobSingleLine.Schedule(pipelineSettings.NumClusters, 1, generateClusterJobHandle);
            //pointLightListGenJobSingleLineHandle.Complete();
#if UNITY_EDITOR
            if (ClusterDebug.selectCamera == cameraData.camera)
            {
                ClustersAABBsCache.CopyFrom(ClustersAABBs);
            }
#endif
            int index = 0;
            for (int i = 0; i < pipelineSettings.NumClusters; ++i)
            {
                index = 0;
                bool found = ResultItemsIDVec.TryGetFirstValue(i, out uint item, out NativeMultiHashMapIterator <int> it);
                while (found && index < pipelineSettings.MaxItemsPerCluster)
                {
                    ResultItemsIDList[i * pipelineSettings.MaxItemsPerCluster + index] = item;
                    index++;
                    found = ResultItemsIDVec.TryGetNextValue(out item, ref it);
                }
            }
            ResultItemsIDVec.Dispose();
            UnityEngine.Profiling.Profiler.EndSample();
        }
示例#12
0
        static void InitializeRenderingData(PipelineSettings settings, ref CameraData cameraData, ref CullResults cullResults,
                                            ref LightLoop lightloop, out RenderingData renderingData)
        {
            List <VisibleLight> visibleLights = cullResults.visibleLights;

            renderingData.cullResults = cullResults;
            renderingData.cameraData  = cameraData;
            renderingData.settings    = settings;
            //todo 自定义光源裁剪,因为这个光源是projector光源
            InitializeLightData(settings, visibleLights, lightloop, out renderingData.lightData);
            renderingData.supportsDynamicBatching = false;// settings.supportsDynamicBatching;

            //todo: settings
            renderingData.shadowData = new ShadowData();
            renderingData.shadowData.lightsShadowmapHeight    = 1024;
            renderingData.shadowData.lightsShadowmapWidth     = 1024;
            renderingData.shadowData.supportsLightShadows     = true;
            renderingData.shadowData.supportsSoftShadows      = false;
            renderingData.shadowData.shadowmapDepthBufferBits = 24;

            lightloop.RebuildLightsList(cullResults.visibleLights, cameraData, settings, ref renderingData);
            InitializeShadowData(settings, lightloop, ref renderingData.shadowData);
        }
示例#13
0
        public void RebuildLightsList(List <VisibleLight> visibleLights, CameraData cameraData, PipelineSettings pipelineSettings, ref RenderingData renderingData)
        {
            UnityEngine.Profiling.Profiler.BeginSample("RebuildLightsList");
            lights.Clear();
            var unityLights = visibleLights;

            VisibleLight = 0;
            LightsDataForShadow.Clear();
            bool softShadow         = false;
            int  j                  = 0;
            int  currentShadowIndex = 0;
            int  maxShadowNum       = pipelineSettings.MaxShadowLightsNum;

            for (int i = 0; i < unityLights.Count; ++i)
            {
                var ul = unityLights[i];
                var pl = ul.light.GetComponent <ProjectorLight>();
                if (pl == null)
                {
                    continue;
                }
                lights.Add(pl);
                var ifLight = pl;
                if (ifLight.spritesAtlas != spritesAtlas)
                {
                    spritesAtlas = ifLight.spritesAtlas;
                }
                //矩阵是从右向左乘的,view需要z取反
                Matrix4x4 c2w         = /*Camera.main*/ cameraData.camera.cameraToWorldMatrix * Matrix4x4.Scale(new Vector3(1, 1, -1));// Camera.main.transform.localToWorldMatrix;
                int       shadowIndex = currentShadowIndex;
                if (currentShadowIndex >= maxShadowNum || !ifLight.lightParms_Shadow)
                {
                    shadowIndex = -1;
                }
                LightDataInAll lightDataInAll = ifLight.GetLightData(c2w, shadowIndex);
                NativeLightsBoundList[i] = lightDataInAll.sFiniteLightBound;
                NativeLightsDataList[i]  = lightDataInAll.lightData;
                ++VisibleLight;

                //for shadow
                // TODO  frustum cull
                if (ifLight.lightParms_Shadow && currentShadowIndex < maxShadowNum)
                {
                    LightDataForShadow lightDataForShadow = new LightDataForShadow();
                    lightDataForShadow.lightIndex      = j++;
                    lightDataForShadow.shadowData      = lightDataInAll.shadowData;
                    lightDataForShadow.visibleLight    = ul;
                    lightDataForShadow.projectorLight  = ifLight;
                    lightDataForShadow.unityLightIndex = i;

                    //Matrix4x4 scaleMatrix = Matrix4x4.identity;
                    ////scaleMatrix.m22 = -1.0f;
                    //Matrix4x4 view = scaleMatrix * ul.localToWorld.inverse;
                    //lightDataForShadow.shadowData.viewMatrix = view;

                    LightsDataForShadow.Add(lightDataForShadow);

                    softShadow |= ifLight.softShadow;
                    ++currentShadowIndex;
                }
            }
            renderingData.shadowData.supportsSoftShadows = softShadow;
            culsterDataGenerator.Begin(pipelineSettings);
            culsterDataGenerator.Run(cameraData, pipelineSettings, this);
            lightLoopLightsData.LightsDatasBuf.SetData(LightsDataList);
            tileAndClusterData.itemsIDListBuf.SetData(culsterDataGenerator.ResultItemsIDList);
            tileAndClusterData.clusterNumItemsBuf.SetData(culsterDataGenerator.ResultClusterNumItems);
            culsterDataGenerator.End();
            UnityEngine.Profiling.Profiler.EndSample();
        }
示例#14
0
 /// <summary>
 /// Configure the pass
 /// </summary>
 /// <param name="maxVisibleAdditionalLights">Maximum number of visible additional lights</param>
 /// <param name="perObjectLightIndices">Buffer holding per object light indicies</param>
 public void Setup(PipelineSettings set, ComputeBuffer perObjectLightIndices)
 {
     settings = set;
 }