public override void PreRenderFrame(PipelineCamera cam, ref PipelineCommandData data) { if (!cam.cam.TryGetCullingParameters(out cullParams)) { return; } data.buffer.SetInvertCulling(cam.inverseRender); cullParams.reflectionProbeSortingCriteria = ReflectionProbeSortingCriteria.ImportanceThenSize; cullParams.cullingOptions = CullingOptions.NeedsLighting | CullingOptions.NeedsReflectionProbes; if (cam.cam.useOcclusionCulling) { cullParams.cullingOptions |= CullingOptions.OcclusionCull; } cullResults = data.context.Cull(ref cullParams); for (int i = 0; i < frustumPlanes.Length; ++i) { Plane p = cullParams.GetCullingPlane(i); //GPU Driven RP's frustum plane is inverse from SRP's frustum plane frustumPlanes[i] = new Vector4(-p.normal.x, -p.normal.y, -p.normal.z, -p.distance); } PipelineFunctions.InitRenderTarget(ref cam.targets, cam.cam, data.buffer); lastData = IPerCameraData.GetProperty(cam, getLastVP); calculateJob.isD3D = GraphicsUtility.platformIsD3D; calculateJob.nonJitterP = cam.cam.nonJitteredProjectionMatrix; calculateJob.worldToView = cam.cam.worldToCameraMatrix; calculateJob.lastVP = lastData.lastVP; calculateJob.rand = (Random *)UnsafeUtility.AddressOf(ref rand); calculateJob.p = cam.cam.projectionMatrix; calculateJob.VP = (float4x4 *)UnsafeUtility.AddressOf(ref VP); calculateJob.inverseVP = (float4x4 *)UnsafeUtility.AddressOf(ref inverseVP); Transform camTrans = cam.cam.transform; perspCam.forward = camTrans.forward; perspCam.up = camTrans.up; perspCam.right = camTrans.right; perspCam.position = camTrans.position; perspCam.nearClipPlane = cam.cam.nearClipPlane; perspCam.farClipPlane = cam.cam.farClipPlane; perspCam.aspect = cam.cam.aspect; perspCam.fov = cam.cam.fieldOfView; float3 *corners = stackalloc float3[8]; PipelineFunctions.GetFrustumCorner(ref perspCam, corners); frustumMinPoint = corners[0]; frustumMaxPoint = corners[0]; for (int i = 1; i < 8; ++i) { frustumMinPoint = min(frustumMinPoint, corners[i]); frustumMaxPoint = max(frustumMaxPoint, corners[i]); } handle = calculateJob.ScheduleRefBurst(); }
public void BeforeFrameRendering() { Transform camTrans = cam.transform; perspCam.forward = camTrans.forward; perspCam.up = camTrans.up; perspCam.right = camTrans.right; perspCam.position = camTrans.position; perspCam.nearClipPlane = cam.nearClipPlane; perspCam.farClipPlane = cam.farClipPlane; perspCam.aspect = cam.aspect; perspCam.fov = cam.fieldOfView; float3 *corners = stackalloc float3[8]; PipelineFunctions.GetFrustumCorner(ref perspCam, corners); frustumMinPoint = corners[0]; frustumMaxPoint = corners[0]; for (int i = 1; i < 8; ++i) { frustumMinPoint = min(frustumMinPoint, corners[i]); frustumMaxPoint = max(frustumMaxPoint, corners[i]); } PipelineFunctions.GetPerspFrustumPlanesWithCorner(ref perspCam, frustumArray.unsafePtr, corners + 4); }
public void FrameUpdate(PipelineCamera cam, ref PipelineCommandData data) { CommandBuffer buffer = data.buffer; if (!Decal.decalDatas.isCreated) { buffer.SetGlobalInt(_EnableDecal, 0); return; } buffer.SetGlobalInt(_EnableDecal, 1); handle.Complete(); if (cullJob.count > decalBuffer.count) { int oldCount = decalBuffer.count; decalBuffer.Dispose(); decalBuffer = new ComputeBuffer((int)max(oldCount * 1.5f, cullJob.count), sizeof(DecalStrct)); } if (!minMaxBoundMat) { minMaxBoundMat = new Material(data.resources.shaders.minMaxDepthBounding); } int pixelWidth = cam.cam.pixelWidth; int pixelHeight = cam.cam.pixelHeight; decalBuffer.SetDataPtr(sortJob.sortedDecalDatas.unsafePtr, 0, cullJob.count); buffer.GetTemporaryRT(_DownSampledDepth0, pixelWidth / 2, pixelHeight / 2, 0, FilterMode.Point, RenderTextureFormat.RGHalf, RenderTextureReadWrite.Linear, 1, false, RenderTextureMemoryless.None, false); buffer.GetTemporaryRT(_DownSampledDepth1, pixelWidth / 4, pixelHeight / 4, 0, FilterMode.Point, RenderTextureFormat.RGHalf, RenderTextureReadWrite.Linear, 1, false, RenderTextureMemoryless.None, false); buffer.GetTemporaryRT(_DownSampledDepth2, pixelWidth / 8, pixelHeight / 8, 0, FilterMode.Point, RenderTextureFormat.RGHalf, RenderTextureReadWrite.Linear, 1, false, RenderTextureMemoryless.None, false); buffer.GetTemporaryRT(_DownSampledDepth3, pixelWidth / 16, pixelHeight / 16, 0, FilterMode.Point, RenderTextureFormat.RGHalf, RenderTextureReadWrite.Linear, 1, false, RenderTextureMemoryless.None, false); buffer.BlitSRT(_DownSampledDepth0, minMaxBoundMat, 0); buffer.SetGlobalTexture(ShaderIDs._TargetDepthTexture, _DownSampledDepth0); buffer.BlitSRT(_DownSampledDepth1, minMaxBoundMat, 1); buffer.SetGlobalTexture(ShaderIDs._TargetDepthTexture, _DownSampledDepth1); buffer.BlitSRT(_DownSampledDepth2, minMaxBoundMat, 1); buffer.SetGlobalTexture(ShaderIDs._TargetDepthTexture, _DownSampledDepth2); buffer.BlitSRT(_DownSampledDepth3, minMaxBoundMat, 1); int2 tileSize = int2(pixelWidth / 16, pixelHeight / 16); RenderTextureDescriptor lightTileDisc = new RenderTextureDescriptor { autoGenerateMips = false, bindMS = false, colorFormat = RenderTextureFormat.RInt, depthBufferBits = 0, dimension = TextureDimension.Tex3D, enableRandomWrite = true, width = tileSize.x, height = tileSize.y, msaaSamples = 1, volumeDepth = CBDRSharedData.MAXLIGHTPERTILE }; buffer.GetTemporaryRT(ShaderIDs._DecalTile, lightTileDisc); tileSizeArray[0] = tileSize.x; tileSizeArray[1] = tileSize.y; float3 * corners = stackalloc float3[4]; PerspCam perspCam = new PerspCam { fov = cam.cam.fieldOfView, up = cam.cam.transform.up, right = cam.cam.transform.right, forward = cam.cam.transform.forward, position = cam.cam.transform.position, aspect = cam.cam.aspect, }; PipelineFunctions.GetFrustumCorner(ref perspCam, 1, corners); for (int i = 0; i < 4; ++i) { frustumCorners[i] = float4(corners[i], 1); } buffer.SetComputeVectorArrayParam(cbdrShader, ShaderIDs._FrustumCorners, frustumCorners); buffer.SetComputeVectorParam(cbdrShader, ShaderIDs._CameraPos, cam.cam.transform.position); buffer.SetComputeIntParams(cbdrShader, ShaderIDs._TileSize, tileSizeArray); buffer.SetGlobalVector(ShaderIDs._TileSize, new Vector4(tileSize.x, tileSize.y)); buffer.SetComputeVectorParam(cbdrShader, ShaderIDs._CameraForward, cam.cam.transform.forward); buffer.SetComputeTextureParam(cbdrShader, CBDRSharedData.DecalCull, ShaderIDs._DepthBoundTexture, new RenderTargetIdentifier(_DownSampledDepth3)); buffer.SetComputeTextureParam(cbdrShader, CBDRSharedData.DecalCull, ShaderIDs._DecalTile, new RenderTargetIdentifier(ShaderIDs._DecalTile)); buffer.SetComputeBufferParam(cbdrShader, CBDRSharedData.DecalCull, ShaderIDs._AllDecals, decalBuffer); buffer.SetComputeIntParam(cbdrShader, ShaderIDs._DecalCount, cullJob.count); buffer.SetGlobalBuffer(ShaderIDs._AllDecals, decalBuffer); buffer.DispatchCompute(cbdrShader, CBDRSharedData.DecalCull, Mathf.CeilToInt(tileSize.x / 8f), Mathf.CeilToInt(tileSize.y / 8f), 1); buffer.ReleaseTemporaryRT(_DownSampledDepth0); buffer.ReleaseTemporaryRT(_DownSampledDepth1); buffer.ReleaseTemporaryRT(_DownSampledDepth2); buffer.ReleaseTemporaryRT(_DownSampledDepth3); RenderPipeline.ReleaseRTAfterFrame(ShaderIDs._DecalTile); decalCullResults.Dispose(); decalCompareResults.Dispose(); }