public void RemoveQuadTrees(NativeList <ulong> removeList) { void ErasePoint(TerrainQuadTree.QuadTreeNode *node) { node->listPosition = -1; notUsedHeightmapIndices.Add(node->panel.heightMapIndex); } int length = removeList.Length; TerrainQuadTree.QuadTreeNode **tree = (TerrainQuadTree.QuadTreeNode * *)removeList.unsafePtr; int targetLength = referenceBuffer.Length - length; int len = 0; if (targetLength <= 0) { for (int i = 0; i < length; ++i) { ErasePoint(tree[i]); } referenceBuffer.Clear(); return; } for (int i = 0; i < length; ++i) { TerrainQuadTree.QuadTreeNode *currentNode = tree[i]; if (currentNode->listPosition >= targetLength) { referenceBuffer[currentNode->listPosition] = 0; ErasePoint(currentNode); } } NativeArray <int2> transformList = new NativeArray <int2>(length, Allocator.Temp, NativeArrayOptions.UninitializedMemory); int2 *transformPtr = transformList.Ptr(); len = 0; int currentIndex = referenceBuffer.Length - 1; for (int i = 0; i < length; ++i) { TerrainQuadTree.QuadTreeNode *treeNode = tree[i]; if (treeNode->listPosition < 0) { continue; } while (referenceBuffer[currentIndex] == 0) { currentIndex--; if (currentIndex < 0) { goto FINALIZE; } } TerrainQuadTree.QuadTreeNode *lastNode = (TerrainQuadTree.QuadTreeNode *)referenceBuffer[currentIndex]; currentIndex--; transformPtr[len] = new int2(treeNode->listPosition, lastNode->listPosition); len++; lastNode->listPosition = treeNode->listPosition; referenceBuffer[lastNode->listPosition] = (ulong)lastNode; ErasePoint(treeNode); } FINALIZE: referenceBuffer.RemoveLast(length); if (len <= 0) { return; } if (len > removebuffer.count) { removebuffer.Dispose(); removebuffer = new ComputeBuffer(len, sizeof(int2)); } removebuffer.SetData(transformList, 0, 0, len); transformShader.SetBuffer(0, ShaderIDs._IndexBuffer, removebuffer); transformShader.SetBuffer(0, ShaderIDs.clusterBuffer, clusterBuffer); ComputeShaderUtility.Dispatch(transformShader, 0, len); transformList.Dispose(); }
public override void PreRenderFrame(PipelineCamera cam, ref PipelineCommandData data) { float shadowDist = clamp(shadowDistance, 0, cam.cam.farClipPlane); if (SunLight.current && SunLight.current.enabled && SunLight.current.gameObject.activeSelf) { lightEnabledSettings.x = SunLight.current.sunlightOnlyForVolumetricLight ? 0 : 1; lightEnabledSettings.y = SunLight.current.enableShadow ? 1 : 0; } else { lightEnabledSettings.x = 0; } var visLights = proper.cullResults.visibleLights; LightFilter.allVisibleLight = visLights.Ptr(); allLights.Clear(); foreach (var i in visLights) { allLights.Add(i.light); } shadowCheckJob.Init(cam.cam, shadowDistance); shadowCullHandle = shadowCheckJob.ScheduleRef(); addMLightCommandList.Clear(); LightFilter.allMLightCommandList = addMLightCommandList; pointLightArray = new NativeArray <PointLightStruct>(visLights.Length, Allocator.Temp, NativeArrayOptions.UninitializedMemory); spotLightArray = new NativeArray <SpotLight>(visLights.Length, Allocator.Temp, NativeArrayOptions.UninitializedMemory); cubemapVPMatrices = new NativeList <CubemapViewProjMatrix>(CBDRSharedData.MAXIMUMPOINTLIGHTCOUNT, Allocator.Temp); spotLightMatrices = new NativeList <SpotLightMatrix>(CBDRSharedData.MAXIMUMSPOTLIGHTCOUNT, Allocator.Temp); NativeList_ulong allSpotCustomCullResults = new NativeList_ulong(CBDRSharedData.MAXIMUMSPOTLIGHTCOUNT, Allocator.Temp); NativeList_ulong allPointCustomCullResults = new NativeList_ulong(CBDRSharedData.MAXIMUMPOINTLIGHTCOUNT, Allocator.Temp); LightFilter.allLights = allLights; LightFilter.pointLightArray = pointLightArray; LightFilter.spotLightArray = spotLightArray; LightFilter.cubemapVPMatrices = cubemapVPMatrices; LightFilter.spotLightMatrices = spotLightMatrices; Transform camTrans = cam.cam.transform; needCheckedShadows.Clear(); needCheckedShadows.AddCapacityTo(allLights.Count); lightingHandle = (new LightFilter { camPos = cam.cam.transform.position, shadowDist = shadowDist, lightDist = cbdrDistance, needCheckLight = needCheckedShadows, allSpotCustomDrawRequests = allSpotCustomCullResults, allPointCustomDrawRequests = allPointCustomCullResults }).Schedule(allLights.Count, max(1, allLights.Count / 4), shadowCullHandle); spotLightCustomCullHandle = new SpotLightCull { ptrs = allSpotCustomCullResults, drawShadowList = CustomDrawRequest.drawShadowList }.Schedule(2, 1, lightingHandle); pointLightCustomCullHandle = new PointLightCull { ptrs = allPointCustomCullResults, drawShadowList = CustomDrawRequest.drawShadowList }.Schedule(2, 1, lightingHandle); if (SunLight.current != null && SunLight.current.enabled && SunLight.current.enableShadow) { clipDistances = (float *)UnsafeUtility.Malloc(SunLight.CASCADECLIPSIZE * sizeof(float), 16, Allocator.Temp); staticFit = DirectionalShadowStaticFit(cam.cam, SunLight.current, clipDistances); sunShadowCams = MUnsafeUtility.Malloc <OrthoCam>(SunLight.CASCADELEVELCOUNT * sizeof(OrthoCam), Allocator.Temp); Matrix4x4 proj = cam.cam.projectionMatrix; // cam.cam.projectionMatrix = cam.cam.nonJitteredProjectionMatrix; PipelineFunctions.GetfrustumCorners(clipDistances, SunLight.CASCADELEVELCOUNT + 1, cam.cam, staticFit.frustumCorners.Ptr()); // cam.cam.projectionMatrix = proj; csmStruct = new CascadeShadowmap { cascadeShadowmapVPs = (float4x4 *)cascadeShadowMapVP.Ptr(), results = sunShadowCams, orthoCam = (OrthoCam *)UnsafeUtility.AddressOf(ref SunLight.current.shadCam), farClipPlane = SunLight.current.farestZ, frustumCorners = staticFit.frustumCorners.Ptr(), resolution = staticFit.resolution, isD3D = GraphicsUtility.platformIsD3D }; csmHandle = csmStruct.ScheduleRefBurst(SunLight.CASCADELEVELCOUNT, max(1, SunLight.CASCADELEVELCOUNT / 4)); for (int i = 0; i < SunLight.CASCADELEVELCOUNT; ++i) { SunLight.customCullResults[i] = new NativeList_Int(CustomDrawRequest.drawShadowList.Length, Allocator.Temp); } sunCullResultHandle = new SunCustomRenderCull { cams = sunShadowCams, cullResults = SunLight.customCullResults.Ptr() }.Schedule(SunLight.CASCADELEVELCOUNT, 1, csmHandle); } }
public override void PreRenderFrame(PipelineCamera cam, ref PipelineCommandData data) { float shadowDist = clamp(shadowDistance, 0, cam.cam.farClipPlane); if (SunLight.current && SunLight.current.enabled && SunLight.current.gameObject.activeSelf) { data.buffer.EnableShaderKeyword("ENABLE_SUN"); data.buffer.SetKeyword("ENABLE_SUNSHADOW", SunLight.current.enableShadow); } else { data.buffer.DisableShaderKeyword("ENABLE_SUN"); } var visLights = proper.cullResults.visibleLights; LightFilter.allVisibleLight = visLights.Ptr(); allLights.Clear(); foreach (var i in visLights) { allLights.Add(i.light); } shadowCheckJob.Init(cam.cam, shadowDistance); shadowCullHandle = shadowCheckJob.ScheduleRef(); addMLightCommandList.Clear(); LightFilter.allMLightCommandList = addMLightCommandList; pointLightArray = new NativeArray <PointLightStruct>(visLights.Length, Allocator.Temp, NativeArrayOptions.UninitializedMemory); spotLightArray = new NativeArray <SpotLight>(visLights.Length, Allocator.Temp, NativeArrayOptions.UninitializedMemory); cubemapVPMatrices = new NativeList <CubemapViewProjMatrix>(CBDRSharedData.MAXIMUMPOINTLIGHTCOUNT, Allocator.Temp); spotLightMatrices = new NativeList <SpotLightMatrix>(CBDRSharedData.MAXIMUMSPOTLIGHTCOUNT, Allocator.Temp); LightFilter.allLights = allLights; LightFilter.pointLightArray = pointLightArray; LightFilter.spotLightArray = spotLightArray; LightFilter.cubemapVPMatrices = cubemapVPMatrices; LightFilter.spotLightMatrices = spotLightMatrices; Transform camTrans = cam.cam.transform; needCheckedShadows.Clear(); needCheckedShadows.AddCapacityTo(allLights.Count); lightingHandle = (new LightFilter { camPos = cam.cam.transform.position, shadowDist = shadowDist, lightDist = cbdrDistance, needCheckLight = needCheckedShadows }).Schedule(allLights.Count, 1, shadowCullHandle); if (SunLight.current != null && SunLight.current.enabled && SunLight.current.enableShadow) { clipDistances = (float *)UnsafeUtility.Malloc(SunLight.CASCADECLIPSIZE * sizeof(float), 16, Allocator.Temp); staticFit = DirectionalShadowStaticFit(cam.cam, SunLight.current, clipDistances); sunShadowCams = MUnsafeUtility.Malloc <OrthoCam>(SunLight.CASCADELEVELCOUNT * sizeof(OrthoCam), Allocator.Temp); Matrix4x4 proj = cam.cam.projectionMatrix; // cam.cam.projectionMatrix = cam.cam.nonJitteredProjectionMatrix; PipelineFunctions.GetfrustumCorners(clipDistances, SunLight.CASCADELEVELCOUNT + 1, cam.cam, staticFit.frustumCorners.Ptr()); // cam.cam.projectionMatrix = proj; csmStruct = new CascadeShadowmap { cascadeShadowmapVPs = (float4x4 *)cascadeShadowMapVP.Ptr(), results = sunShadowCams, orthoCam = (OrthoCam *)UnsafeUtility.AddressOf(ref SunLight.current.shadCam), farClipPlane = SunLight.current.farestZ, frustumCorners = staticFit.frustumCorners.Ptr(), resolution = staticFit.resolution, isD3D = GraphicsUtility.platformIsD3D }; csmHandle = csmStruct.ScheduleRefBurst(SunLight.CASCADELEVELCOUNT, 1); } }