public override void PreRenderFrame(PipelineCamera cam, ref PipelineCommandData data) { 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 = data.cullResults.visibleLights; LightFilter.allVisibleLight = visLights.Ptr(); allLights.Clear(); foreach (var i in visLights) { allLights.Add(i.light); } 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; lightingHandle = (new LightFilter { camPos = cam.cam.transform.position, lightDist = localLightDistance }).Schedule(allLights.Count, 1); 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); } }
public override void PreRenderFrame(PipelineCamera cam, ref PipelineCommandData data) { var visLights = data.cullResults.visibleLights; LightFilter.allVisibleLight = visLights.Ptr(); allLights.Clear(); cbdr.UpdateFroxel(volumetricEvent && volumetricEvent.Enabled); foreach (var i in visLights) { allLights.Add(i.light); } 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; lightingHandle = (new LightFilter()).Schedule(allLights.Count, 1); 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); cascadeWorldToCamera = (float4x4 *)UnsafeUtility.Malloc(SunLight.CASCADELEVELCOUNT * sizeof(float4x4), 16, Allocator.Temp); cascadeProjection = (float4x4 *)UnsafeUtility.Malloc(SunLight.CASCADELEVELCOUNT * sizeof(float4x4), 16, Allocator.Temp); PipelineFunctions.GetfrustumCorners(clipDistances, SunLight.CASCADELEVELCOUNT + 1, cam.cam, staticFit.frustumCorners.Ptr()); csmStruct = new CascadeShadowmap { cascadeShadowmapVPs = (float4x4 *)cascadeShadowMapVP.Ptr(), cascadeProjection = cascadeProjection, cascadeWorldToCamera = cascadeWorldToCamera, 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); } }
public override void DrawDirectionalShadow(Camera currentCam, ref RenderClusterOptions opts, ref ShadowmapSettings settings, ref ShadowMapComponent shadMap, Matrix4x4[] cascadeShadowMapVP) { const int CASCADELEVELCOUNT = 4; const int CASCADECLIPSIZE = (CASCADELEVELCOUNT + 1) * sizeof(float); ComputeShader gpuFrustumShader = opts.cullingShader; StaticFit staticFit; staticFit.resolution = settings.resolution; staticFit.mainCamTrans = currentCam; staticFit.frustumCorners = shadMap.frustumCorners; float *clipDistances = stackalloc float[CASCADECLIPSIZE]; clipDistances[0] = shadMap.shadCam.nearClipPlane; clipDistances[1] = settings.firstLevelDistance; clipDistances[2] = settings.secondLevelDistance; clipDistances[3] = settings.thirdLevelDistance; clipDistances[4] = settings.farestDistance; opts.command.SetGlobalBuffer(ShaderIDs.verticesBuffer, baseBuffer.verticesBuffer); opts.command.SetGlobalBuffer(ShaderIDs.resultBuffer, baseBuffer.resultBuffer); for (int pass = 0; pass < CASCADELEVELCOUNT; ++pass) { Vector2 farClipDistance = new Vector2(clipDistances[pass], clipDistances[pass + 1]); PipelineFunctions.GetfrustumCorners(farClipDistance, ref shadMap, currentCam); // PipelineFunctions.SetShadowCameraPositionCloseFit(ref shadMap, ref settings); Matrix4x4 invpVPMatrix; PipelineFunctions.SetShadowCameraPositionStaticFit(ref staticFit, ref shadMap.shadCam, pass, cascadeShadowMapVP, out invpVPMatrix); PipelineFunctions.GetCullingPlanes(ref invpVPMatrix, opts.frustumPlanes); PipelineFunctions.SetBaseBuffer(baseBuffer, gpuFrustumShader, opts.frustumPlanes, opts.command); PipelineFunctions.RunCullDispatching(baseBuffer, gpuFrustumShader, true, opts.command); float *biasList = (float *)UnsafeUtility.AddressOf(ref settings.bias); PipelineFunctions.UpdateCascadeState(ref shadMap, opts.command, biasList[pass] / currentCam.farClipPlane, pass); opts.command.DrawProceduralIndirect(Matrix4x4.identity, shadMap.shadowDepthMaterial, 0, MeshTopology.Triangles, baseBuffer.instanceCountBuffer, 0); opts.command.DispatchCompute(gpuFrustumShader, 1, 1, 1, 1); } }
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); 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(CBDRSharedData.MAXIMUMSPOTLIGHTCOUNT, max(1, CBDRSharedData.MAXIMUMSPOTLIGHTCOUNT / 2), lightingHandle); pointLightCustomCullHandle = new PointLightCull { ptrs = allPointCustomCullResults, drawShadowList = CustomDrawRequest.drawShadowList }.Schedule(CBDRSharedData.MAXIMUMPOINTLIGHTCOUNT, max(1, CBDRSharedData.MAXIMUMPOINTLIGHTCOUNT / 2), 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); } decalEvt.PreRenderFrame(cam, ref data); }