From 551dd1492964775e0dfda90e2407928ef8a1e30a Mon Sep 17 00:00:00 2001 From: FrancescoC-Unity Date: Thu, 18 Jun 2020 14:51:44 +0200 Subject: [PATCH 01/11] StopNaNs and guardbands to rendergraph --- .../PostProcessSystem.RenderGraph.cs | 86 ++++++- .../PostProcessing/PostProcessSystem.cs | 215 ++++++++++-------- .../Runtime/RenderPipeline/HDProfileId.cs | 1 + 3 files changed, 209 insertions(+), 93 deletions(-) diff --git a/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.RenderGraph.cs b/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.RenderGraph.cs index 8e691623961..2c4538d2cb5 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.RenderGraph.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.RenderGraph.cs @@ -27,6 +27,19 @@ class AlphaCopyPassData public TextureHandle outputAlpha; } + class GuardBandPassData + { + public ClearWithGuardBandsParameters parameters; + public TextureHandle source; + } + + class StopNaNPassData + { + public StopNaNParameters parameters; + public TextureHandle source; + public TextureHandle destination; + } + public void Render( RenderGraph renderGraph, HDCamera hdCamera, BlueNoise blueNoise, @@ -65,6 +78,71 @@ public void Render( RenderGraph renderGraph, } } + if (m_PostProcessEnabled) + { + // Guard bands (also known as "horrible hack") to avoid bleeding previous RTHandle + // content into smaller viewports with some effects like Bloom that rely on bilinear + // filtering and can't use clamp sampler and the likes + // Note: some platforms can't clear a partial render target so we directly draw black triangles + using (var builder = renderGraph.AddRenderPass("Guard Band Clear", out var passData, ProfilingSampler.Get(HDProfileId.GuardBandClear))) + { + passData.source = builder.ReadTexture(source); + passData.parameters = PrepareClearWithGuardBandsParameters(hdCamera); + + builder.SetRenderFunc( + (GuardBandPassData data, RenderGraphContext ctx) => + { + ClearWithGuardBands(data.parameters, ctx.cmd, ctx.resources.GetTexture(data.source)); + }); + + source = passData.source; + } + + // Optional NaN killer before post-processing kicks in + bool stopNaNs = hdCamera.stopNaNs && m_StopNaNFS; + +#if UNITY_EDITOR + if (isSceneView) + stopNaNs = HDAdditionalSceneViewSettings.sceneViewStopNaNs; +#endif + if (stopNaNs) + { + using (var builder = renderGraph.AddRenderPass("Stop NaNs", out var passData, ProfilingSampler.Get(HDProfileId.StopNaNs))) + { + passData.source = builder.ReadTexture(source); + passData.parameters = PrepareStopNaNParameters(hdCamera); + TextureHandle dest = renderGraph.CreateTexture(new TextureDesc(Vector2.one, true, true) + { + name = "Stop NaNs Destination", + colorFormat = m_ColorFormat, + useMipMap = false, + enableRandomWrite = true + }); + passData.destination = builder.WriteTexture(dest); ; + + builder.SetRenderFunc( + (StopNaNPassData data, RenderGraphContext ctx) => + { + DoStopNaNs(data.parameters, ctx.cmd, ctx.resources.GetTexture(data.source), ctx.resources.GetTexture(data.destination)); + }); + + source = passData.destination; + } + } + + // if (stopNaNs) + // { + // using (new ProfilingScope(cmd, ProfilingSampler.Get(HDProfileId.StopNaNs))) + // { + // var destination = m_Pool.Get(Vector2.one, m_ColorFormat); + // DoStopNaNs(cmd, camera, source, destination); + // PoolSource(ref source, destination); + // } + // } + // } + + } + // TODO RENDERGRAPH: Implement // if (m_PostProcessEnabled) // { @@ -290,10 +368,10 @@ public void Render( RenderGraph renderGraph, builder.SetRenderFunc( (UberPostPassData data, RenderGraphContext ctx) => { - // Temp until bloom is implemented. - ctx.cmd.SetComputeTextureParam(data.parameters.uberPostCS, data.parameters.uberPostKernel, HDShaderIDs._BloomTexture, TextureXR.GetBlackTexture()); - ctx.cmd.SetComputeTextureParam(data.parameters.uberPostCS, data.parameters.uberPostKernel, HDShaderIDs._BloomDirtTexture, Texture2D.blackTexture); - ctx.cmd.SetComputeVectorParam(data.parameters.uberPostCS, HDShaderIDs._BloomParams, Vector4.zero); + //// Temp until bloom is implemented. + //ctx.cmd.SetComputeTextureParam(data.parameters.uberPostCS, data.parameters.uberPostKernel, HDShaderIDs._BloomTexture, TextureXR.GetBlackTexture()); + //ctx.cmd.SetComputeTextureParam(data.parameters.uberPostCS, data.parameters.uberPostKernel, HDShaderIDs._BloomDirtTexture, Texture2D.blackTexture); + //ctx.cmd.SetComputeVectorParam(data.parameters.uberPostCS, HDShaderIDs._BloomParams, Vector4.zero); DoUberPostProcess( data.parameters, diff --git a/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.cs b/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.cs index 30494b65cd7..0d25828a647 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.cs @@ -428,6 +428,44 @@ void PoolSourceGuard(ref RTHandle src, RTHandle dst, RTHandle colorBuffer) src = dst; } + struct ClearWithGuardBandsParameters + { + public Material clearMaterial; + public int cameraWidth; + public int cameraHeight; + } + + ClearWithGuardBandsParameters PrepareClearWithGuardBandsParameters(HDCamera camera) + { + ClearWithGuardBandsParameters parameters = new ClearWithGuardBandsParameters(); + parameters.clearMaterial = m_ClearBlackMaterial; + parameters.cameraWidth = camera.actualWidth; + parameters.cameraHeight = camera.actualHeight; + + return parameters; + } + + static void ClearWithGuardBands(in ClearWithGuardBandsParameters parameters, CommandBuffer cmd, RTHandle source) + { + // Guard bands (also known as "horrible hack") to avoid bleeding previous RTHandle + // content into smaller viewports with some effects like Bloom that rely on bilinear + // filtering and can't use clamp sampler and the likes + // Note: some platforms can't clear a partial render target so we directly draw black triangles + { + int w = parameters.cameraWidth; + int h = parameters.cameraHeight; + cmd.SetRenderTarget(source, 0, CubemapFace.Unknown, -1); + + if (w < source.rt.width || h < source.rt.height) + { + cmd.SetViewport(new Rect(w, 0, k_RTGuardBandSize, h)); + cmd.DrawProcedural(Matrix4x4.identity, parameters.clearMaterial, 0, MeshTopology.Triangles, 3, 1); + cmd.SetViewport(new Rect(0, h, w + k_RTGuardBandSize, k_RTGuardBandSize)); + cmd.DrawProcedural(Matrix4x4.identity, parameters.clearMaterial, 0, MeshTopology.Triangles, 3, 1); + } + } + } + public void Render(CommandBuffer cmd, HDCamera camera, BlueNoise blueNoise, RTHandle colorBuffer, RTHandle afterPostProcessTexture, RenderTargetIdentifier finalRT, RTHandle depthBuffer, RTHandle depthMipChain, bool flipY) { var dynResHandler = DynamicResolutionHandler.instance; @@ -455,23 +493,7 @@ void PoolSource(ref RTHandle src, RTHandle dst) if (m_PostProcessEnabled) { - // Guard bands (also known as "horrible hack") to avoid bleeding previous RTHandle - // content into smaller viewports with some effects like Bloom that rely on bilinear - // filtering and can't use clamp sampler and the likes - // Note: some platforms can't clear a partial render target so we directly draw black triangles - { - int w = camera.actualWidth; - int h = camera.actualHeight; - cmd.SetRenderTarget(source, 0, CubemapFace.Unknown, -1); - - if (w < source.rt.width || h < source.rt.height) - { - cmd.SetViewport(new Rect(w, 0, k_RTGuardBandSize, h)); - cmd.DrawProcedural(Matrix4x4.identity, m_ClearBlackMaterial, 0, MeshTopology.Triangles, 3, 1); - cmd.SetViewport(new Rect(0, h, w + k_RTGuardBandSize, k_RTGuardBandSize)); - cmd.DrawProcedural(Matrix4x4.identity, m_ClearBlackMaterial, 0, MeshTopology.Triangles, 3, 1); - } - } + ClearWithGuardBands(PrepareClearWithGuardBandsParameters(camera), cmd, source); // Optional NaN killer before post-processing kicks in bool stopNaNs = camera.stopNaNs && m_StopNaNFS; @@ -486,8 +508,8 @@ void PoolSource(ref RTHandle src, RTHandle dst) using (new ProfilingScope(cmd, ProfilingSampler.Get(HDProfileId.StopNaNs))) { var destination = m_Pool.Get(Vector2.one, m_ColorFormat); - var stopNanParams = PrepareStopNaNParameters(); - DoStopNaNs(stopNanParams, cmd, camera, source, destination); + var stopNanParams = PrepareStopNaNParameters(camera); + DoStopNaNs(stopNanParams, cmd, source, destination); PoolSource(ref source, destination); } } @@ -505,11 +527,11 @@ void PoolSource(ref RTHandle src, RTHandle dst) { if (m_Exposure.mode.value == ExposureMode.AutomaticHistogram) { - DoHistogramBasedExposure(exposureParameters, cmd, camera, source, prevExposure, nextExposure, m_DebugExposureData); + DoHistogramBasedExposure(exposureParameters, cmd, source, prevExposure, nextExposure, m_DebugExposureData); } else { - DoDynamicExposure(exposureParameters, cmd, camera, source, prevExposure, nextExposure, tmpRenderTarget1024, tmpRenderTarget32); + DoDynamicExposure(exposureParameters, cmd, source, prevExposure, nextExposure, tmpRenderTarget1024, tmpRenderTarget32); } // On reset history we need to apply dynamic exposure immediately to avoid @@ -562,7 +584,7 @@ void PoolSource(ref RTHandle src, RTHandle dst) var taaParams = PrepareTAAParameters(camera); GrabTemporalAntialiasingHistoryTextures(camera, out var prevHistory, out var nextHistory); GrabVelocityMagnitudeHistoryTextures(camera, out var prevMVLen, out var nextMVLen); - DoTemporalAntialiasing(taaParams, cmd, camera, source, destination, depthBuffer, depthMipChain, prevHistory, nextHistory, prevMVLen, nextMVLen); + DoTemporalAntialiasing(taaParams, cmd, source, destination, depthBuffer, depthMipChain, prevHistory, nextHistory, prevMVLen, nextMVLen); PoolSource(ref source, destination); } } @@ -573,7 +595,7 @@ void PoolSource(ref RTHandle src, RTHandle dst) var destination = m_Pool.Get(Vector2.one, m_ColorFormat); RTHandle smaaEdgeTex, smaaBlendTex; AllocateSMAARenderTargets(camera, out smaaEdgeTex, out smaaBlendTex); - DoSMAA(PrepareSMAAParameters(camera), cmd, camera, source, smaaEdgeTex, smaaBlendTex, destination, depthBuffer); + DoSMAA(PrepareSMAAParameters(camera), cmd, source, smaaEdgeTex, smaaBlendTex, destination, depthBuffer); RecycleSMAARenderTargets(smaaEdgeTex, smaaBlendTex); PoolSource(ref source, destination); } @@ -620,7 +642,7 @@ void PoolSource(ref RTHandle src, RTHandle dst) out preppedMotionVec, out minMaxTileVel, out maxTileNeigbourhood, out tileToScatterMax, out tileToScatterMin); - DoMotionBlur(PrepareMotionBlurParameters(camera), cmd, camera, source, destination, preppedMotionVec, minMaxTileVel, maxTileNeigbourhood, tileToScatterMax, tileToScatterMin); + DoMotionBlur(PrepareMotionBlurParameters(camera), cmd, source, destination, preppedMotionVec, minMaxTileVel, maxTileNeigbourhood, tileToScatterMax, tileToScatterMin); RecycleMotionBlurRenderTargets(preppedMotionVec, minMaxTileVel, maxTileNeigbourhood, tileToScatterMax, tileToScatterMin); PoolSource(ref source, destination); @@ -636,7 +658,7 @@ void PoolSource(ref RTHandle src, RTHandle dst) using (new ProfilingScope(cmd, ProfilingSampler.Get(HDProfileId.PaniniProjection))) { var destination = m_Pool.Get(Vector2.one, m_ColorFormat); - DoPaniniProjection(PreparePaniniProjectionParameters(camera), cmd, camera, source, destination); + DoPaniniProjection(PreparePaniniProjectionParameters(camera), cmd, source, destination); PoolSource(ref source, destination); } } @@ -655,7 +677,7 @@ void PoolSource(ref RTHandle src, RTHandle dst) { ComputeBloomMipSizesAndScales(camera); AllocateBloomMipTextures(); - DoBloom(PrepareBloomParameters(camera), cmd, camera, source, m_BloomMipsDown, m_BloomMipsUp, uberPostParams.uberPostCS, uberPostParams.uberPostKernel); + DoBloom(PrepareBloomParameters(camera), cmd, source, m_BloomMipsDown, m_BloomMipsUp, uberPostParams.uberPostCS, uberPostParams.uberPostKernel); RecycleUnusedBloomMips(); } } @@ -702,7 +724,7 @@ void PoolSource(ref RTHandle src, RTHandle dst) using (new ProfilingScope(cmd, ProfilingSampler.Get(HDProfileId.FXAA))) { var destination = m_Pool.Get(Vector2.one, m_ColorFormat); - DoFXAA(PrepareFXAAParameters(), cmd, camera, source, destination); + DoFXAA(PrepareFXAAParameters(camera), cmd, source, destination); PoolSource(ref source, destination); } } @@ -900,23 +922,30 @@ struct StopNaNParameters { public ComputeShader nanKillerCS; public int nanKillerKernel; + + public int width; + public int height; + public int viewCount; } - StopNaNParameters PrepareStopNaNParameters() + StopNaNParameters PrepareStopNaNParameters(HDCamera camera) { StopNaNParameters stopNanParams = new StopNaNParameters(); stopNanParams.nanKillerCS = m_Resources.shaders.nanKillerCS; stopNanParams.nanKillerKernel = stopNanParams.nanKillerCS.FindKernel("KMain"); + stopNanParams.width = camera.actualWidth; + stopNanParams.height = camera.actualHeight; + stopNanParams.viewCount = camera.viewCount; return stopNanParams; } - static void DoStopNaNs(in StopNaNParameters stopNanParameters, CommandBuffer cmd, HDCamera camera, RTHandle source, RTHandle destination) + static void DoStopNaNs(in StopNaNParameters stopNanParameters, CommandBuffer cmd, RTHandle source, RTHandle destination) { var cs = stopNanParameters.nanKillerCS; int kernel = stopNanParameters.nanKillerKernel; cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._InputTexture, source); cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._OutputTexture, destination); - cmd.DispatchCompute(cs, kernel, (camera.actualWidth + 7) / 8, (camera.actualHeight + 7) / 8, camera.viewCount); + cmd.DispatchCompute(cs, kernel, (stopNanParameters.width + 7) / 8, (stopNanParameters.height + 7) / 8, stopNanParameters.viewCount); } #endregion @@ -961,6 +990,8 @@ struct ExposureParameter public Texture textureMeteringMask; public Texture exposureCurve; + public HDCamera camera; + public ComputeBuffer histogramBuffer; public ExposureMode exposureMode; @@ -983,6 +1014,8 @@ ExposureParameter PrepareExposureParameters(HDCamera hdCamera) parameters.histogramExposureCS = m_Resources.shaders.histogramExposureCS; parameters.histogramExposureCS.shaderKeywords = null; + parameters.camera = hdCamera; + bool isFixed = IsExposureFixed(hdCamera); if (isFixed) { @@ -1161,6 +1194,7 @@ internal ComputeBuffer GetDebugImageHistogramBuffer() return m_DebugImageHistogramBuffer; } + // TODO_FCC: MISSING!! void DoFixedExposure(CommandBuffer cmd, HDCamera camera) { var cs = m_Resources.shaders.exposureCS; @@ -1281,30 +1315,7 @@ void GrabExposureRequiredTextures(HDCamera camera, out RTHandle prevExposure, ou tmpRenderTarget32 = m_TempTexture32; } - void DynamicExposureSetup(CommandBuffer cmd, HDCamera camera, out RTHandle prevExposure, out RTHandle nextExposure) - { - GrabExposureHistoryTextures(camera, out prevExposure, out nextExposure); - - // Setup variants - var adaptationMode = m_Exposure.adaptationMode.value; - - if (!Application.isPlaying || camera.resetPostProcessingHistory) - adaptationMode = AdaptationMode.Fixed; - - if (camera.resetPostProcessingHistory) - { - // For Dynamic Exposure, we need to undo the pre-exposure from the color buffer to calculate the correct one - // When we reset history we must setup neutral value - prevExposure = m_EmptyExposureTexture; // Use neutral texture - } - - m_ExposureVariants[0] = 1; // (int)exposureSettings.luminanceSource.value; - m_ExposureVariants[1] = (int)m_Exposure.meteringMode.value; - m_ExposureVariants[2] = (int)adaptationMode; - m_ExposureVariants[3] = 0; - } - - static void DoDynamicExposure(in ExposureParameter exposureParameters, CommandBuffer cmd, HDCamera camera, RTHandle colorBuffer, RTHandle prevExposure, RTHandle nextExposure, RTHandle tmpRenderTarget1024, RTHandle tmpRenderTarget32) + static void DoDynamicExposure(in ExposureParameter exposureParameters, CommandBuffer cmd, RTHandle colorBuffer, RTHandle prevExposure, RTHandle nextExposure, RTHandle tmpRenderTarget1024, RTHandle tmpRenderTarget32) { var cs = exposureParameters.exposureCS; int kernel; @@ -1353,7 +1364,7 @@ static void DoDynamicExposure(in ExposureParameter exposureParameters, CommandBu cmd.DispatchCompute(cs, kernel, 1, 1, 1); } - static void DoHistogramBasedExposure(in ExposureParameter exposureParameters, CommandBuffer cmd, HDCamera camera, RTHandle sourceTexture, RTHandle prevExposure, RTHandle nextExposure, RTHandle debugData) + static void DoHistogramBasedExposure(in ExposureParameter exposureParameters, CommandBuffer cmd, RTHandle sourceTexture, RTHandle prevExposure, RTHandle nextExposure, RTHandle debugData) { var cs = exposureParameters.histogramExposureCS; int kernel; @@ -1375,9 +1386,8 @@ static void DoHistogramBasedExposure(in ExposureParameter exposureParameters, Co int threadGroupSizeX = 16; int threadGroupSizeY = 8; - int dispatchSizeX = HDUtils.DivRoundUp(camera.actualWidth / 2, threadGroupSizeX); - int dispatchSizeY = HDUtils.DivRoundUp(camera.actualHeight / 2, threadGroupSizeY); - int totalPixels = camera.actualWidth * camera.actualHeight; + int dispatchSizeX = HDUtils.DivRoundUp(exposureParameters.camera.actualWidth / 2, threadGroupSizeX); + int dispatchSizeY = HDUtils.DivRoundUp(exposureParameters.camera.actualHeight / 2, threadGroupSizeY); cmd.DispatchCompute(cs, kernel, dispatchSizeX, dispatchSizeY, 1); // Now read the histogram @@ -1405,6 +1415,7 @@ static void DoHistogramBasedExposure(in ExposureParameter exposureParameters, Co cmd.DispatchCompute(cs, kernel, 1, 1, 1); } + // TODO_FCC: TODO ADD! MISSING! internal void GenerateDebugImageHistogram(CommandBuffer cmd, HDCamera camera, RTHandle sourceTexture) { var cs = m_Resources.shaders.debugImageHistogramCS; @@ -1430,10 +1441,11 @@ internal void GenerateDebugImageHistogram(CommandBuffer cmd, HDCamera camera, RT struct TemporalAntiAliasingParameters { public Material temporalAAMaterial; - public MaterialPropertyBlock taaHistoryPropertyBlock; public MaterialPropertyBlock taaPropertyBlock; + public HDCamera camera; + public Vector4 taaParameters; public Vector4 taaFilterWeights; } @@ -1441,6 +1453,9 @@ struct TemporalAntiAliasingParameters TemporalAntiAliasingParameters PrepareTAAParameters(HDCamera camera) { TemporalAntiAliasingParameters parameters = new TemporalAntiAliasingParameters(); + + parameters.camera = camera; + float minAntiflicker = 0.0f; float maxAntiflicker = 3.5f; float motionRejectionMultiplier = Mathf.Lerp(0.0f, 250.0f, camera.taaMotionVectorRejection * camera.taaMotionVectorRejection * camera.taaMotionVectorRejection); @@ -1511,7 +1526,6 @@ TemporalAntiAliasingParameters PrepareTAAParameters(HDCamera camera) static void DoTemporalAntialiasing(in TemporalAntiAliasingParameters taaParams, CommandBuffer cmd, - HDCamera camera, RTHandle source, RTHandle destination, RTHandle depthBuffer, @@ -1521,7 +1535,7 @@ static void DoTemporalAntialiasing(in TemporalAntiAliasingParameters taaParams, RTHandle prevMVLen, RTHandle nextMVLen) { - if (camera.resetPostProcessingHistory) + if (taaParams.camera.resetPostProcessingHistory) { taaParams.taaHistoryPropertyBlock.SetTexture(HDShaderIDs._BlitTexture, source); var rtScaleSource = source.rtHandleProperties.rtHandleScale; @@ -2231,6 +2245,8 @@ struct MotionBlurParameters public int tileMergeKernel; public int motionBlurKernel; + public HDCamera camera; + public Vector4 tileTargetSize; public Vector4 motionBlurParams0; public Vector4 motionBlurParams1; @@ -2242,6 +2258,9 @@ struct MotionBlurParameters MotionBlurParameters PrepareMotionBlurParameters(HDCamera camera) { MotionBlurParameters parameters = new MotionBlurParameters(); + + parameters.camera = camera; + int tileSize = 32; if (m_MotionBlurSupportsScattering) @@ -2342,7 +2361,7 @@ void RecycleMotionBlurRenderTargets(RTHandle preppedMotionVec, RTHandle minMaxTi } } - static void DoMotionBlur(in MotionBlurParameters motionBlurParams, CommandBuffer cmd, HDCamera camera, RTHandle source, RTHandle destination, + static void DoMotionBlur(in MotionBlurParameters motionBlurParams, CommandBuffer cmd, RTHandle source, RTHandle destination, RTHandle preppedMotionVec, RTHandle minMaxTileVel, RTHandle maxTileNeigbourhood, RTHandle tileToScatterMax, RTHandle tileToScatterMin) @@ -2374,11 +2393,11 @@ static void DoMotionBlur(in MotionBlurParameters motionBlurParams, CommandBuffer cmd.SetComputeVectorParam(cs, HDShaderIDs._MotionBlurParams1, motionBlurParams.motionBlurParams1); cmd.SetComputeVectorParam(cs, HDShaderIDs._MotionBlurParams2, motionBlurParams.motionBlurParams2); - cmd.SetComputeMatrixParam(cs, HDShaderIDs._PrevVPMatrixNoTranslation, camera.mainViewConstants.prevViewProjMatrixNoCameraTrans); + cmd.SetComputeMatrixParam(cs, HDShaderIDs._PrevVPMatrixNoTranslation, motionBlurParams.camera.mainViewConstants.prevViewProjMatrixNoCameraTrans); - threadGroupX = (camera.actualWidth + (groupSizeX - 1)) / groupSizeX; - threadGroupY = (camera.actualHeight + (groupSizeY - 1)) / groupSizeY; - cmd.DispatchCompute(cs, kernel, threadGroupX, threadGroupY, camera.viewCount); + threadGroupX = (motionBlurParams.camera.actualWidth + (groupSizeX - 1)) / groupSizeX; + threadGroupY = (motionBlurParams.camera.actualHeight + (groupSizeY - 1)) / groupSizeY; + cmd.DispatchCompute(cs, kernel, threadGroupX, threadGroupY, motionBlurParams.camera.viewCount); } @@ -2404,9 +2423,9 @@ static void DoMotionBlur(in MotionBlurParameters motionBlurParams, CommandBuffer cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._TileToScatterMin, tileToScatterMin); } - threadGroupX = (camera.actualWidth + (tileSize - 1)) / tileSize; - threadGroupY = (camera.actualHeight + (tileSize - 1)) / tileSize; - cmd.DispatchCompute(cs, kernel, threadGroupX, threadGroupY, camera.viewCount); + threadGroupX = (motionBlurParams.camera.actualWidth + (tileSize - 1)) / tileSize; + threadGroupY = (motionBlurParams.camera.actualHeight + (tileSize - 1)) / tileSize; + cmd.DispatchCompute(cs, kernel, threadGroupX, threadGroupY, motionBlurParams.camera.viewCount); } // ----------------------------------------------------------------------------- @@ -2434,7 +2453,7 @@ static void DoMotionBlur(in MotionBlurParameters motionBlurParams, CommandBuffer groupSizeY = 8; threadGroupX = ((int)motionBlurParams.tileTargetSize.x + (groupSizeX - 1)) / groupSizeX; threadGroupY = ((int)motionBlurParams.tileTargetSize.y + (groupSizeY - 1)) / groupSizeY; - cmd.DispatchCompute(cs, kernel, threadGroupX, threadGroupY, camera.viewCount); + cmd.DispatchCompute(cs, kernel, threadGroupX, threadGroupY, motionBlurParams.camera.viewCount); } // ----------------------------------------------------------------------------- @@ -2452,7 +2471,7 @@ static void DoMotionBlur(in MotionBlurParameters motionBlurParams, CommandBuffer cmd.SetComputeVectorParam(cs, HDShaderIDs._MotionBlurParams1, motionBlurParams.motionBlurParams1); cmd.SetComputeVectorParam(cs, HDShaderIDs._MotionBlurParams2, motionBlurParams.motionBlurParams2); - cmd.DispatchCompute(cs, kernel, threadGroupX, threadGroupY, camera.viewCount); + cmd.DispatchCompute(cs, kernel, threadGroupX, threadGroupY, motionBlurParams.camera.viewCount); } // ----------------------------------------------------------------------------- @@ -2473,9 +2492,9 @@ static void DoMotionBlur(in MotionBlurParameters motionBlurParams, CommandBuffer groupSizeX = 16; groupSizeY = 16; - threadGroupX = (camera.actualWidth + (groupSizeX - 1)) / groupSizeX; - threadGroupY = (camera.actualHeight + (groupSizeY - 1)) / groupSizeY; - cmd.DispatchCompute(cs, kernel, threadGroupX, threadGroupY, camera.viewCount); + threadGroupX = (motionBlurParams.camera.actualWidth + (groupSizeX - 1)) / groupSizeX; + threadGroupY = (motionBlurParams.camera.actualHeight + (groupSizeY - 1)) / groupSizeY; + cmd.DispatchCompute(cs, kernel, threadGroupX, threadGroupY, motionBlurParams.camera.viewCount); } } @@ -2489,11 +2508,20 @@ struct PaniniProjectionParameters public int paniniProjectionKernel; public Vector4 paniniParams; + + public int width; + public int height; + public int viewCount; } PaniniProjectionParameters PreparePaniniProjectionParameters(HDCamera camera) { PaniniProjectionParameters parameters = new PaniniProjectionParameters(); + + parameters.width = camera.actualWidth; + parameters.height = camera.actualHeight; + parameters.viewCount = camera.viewCount; + parameters.paniniProjectionCS = m_Resources.shaders.paniniProjectionCS; parameters.paniniProjectionCS.shaderKeywords = null; @@ -2524,7 +2552,7 @@ PaniniProjectionParameters PreparePaniniProjectionParameters(HDCamera camera) } // Back-ported & adapted from the work of the Stockholm demo team - thanks Lasse! - static void DoPaniniProjection(in PaniniProjectionParameters parameters, CommandBuffer cmd, HDCamera camera, RTHandle source, RTHandle destination) + static void DoPaniniProjection(in PaniniProjectionParameters parameters, CommandBuffer cmd, RTHandle source, RTHandle destination) { var cs = parameters.paniniProjectionCS; int kernel = parameters.paniniProjectionKernel; @@ -2532,7 +2560,7 @@ static void DoPaniniProjection(in PaniniProjectionParameters parameters, Command cmd.SetComputeVectorParam(cs, HDShaderIDs._Params, parameters.paniniParams); cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._InputTexture, source); cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._OutputTexture, destination); - cmd.DispatchCompute(cs, kernel, (camera.actualWidth + 7) / 8, (camera.actualHeight + 7) / 8, camera.viewCount); + cmd.DispatchCompute(cs, kernel, (parameters.width + 7) / 8, (parameters.height + 7) / 8, parameters.viewCount); } Vector2 CalcViewExtents(HDCamera camera) @@ -2596,18 +2624,19 @@ struct BloomParameters public int bloomBlurKernel; public int bloomDownsampleKernel; public int bloomUpsampleKernel; + + public int viewCount; public int bloomMipCount; public float bloomScatterParam; - public Vector4 thresholdParams; - public Vector4[] bloomMipInfo; } BloomParameters PrepareBloomParameters(HDCamera camera) { BloomParameters parameters = new BloomParameters(); + parameters.viewCount = camera.viewCount; parameters.bloomMipCount = m_BloomMipCount; parameters.bloomMipInfo = m_BloomMipsInfo; @@ -2757,12 +2786,12 @@ void RecycleUnusedBloomMips() } } - static void DoBloom(in BloomParameters bloomParameters, CommandBuffer cmd, HDCamera camera, RTHandle source, RTHandle[] bloomMipsDown, RTHandle[] bloomMipsUp, ComputeShader uberCS, int uberKernel) + static void DoBloom(in BloomParameters bloomParameters, CommandBuffer cmd, RTHandle source, RTHandle[] bloomMipsDown, RTHandle[] bloomMipsUp, ComputeShader uberCS, int uberKernel) { // All the computes for this effect use the same group size so let's use a local // function to simplify dispatches // Make sure the thread group count is sufficient to draw the guard bands - void DispatchWithGuardBands(ComputeShader shader, int kernelId, in Vector2Int size) + void DispatchWithGuardBands(ComputeShader shader, int kernelId, in Vector2Int size, in int viewCount) { int w = size.x; int h = size.y; @@ -2772,7 +2801,7 @@ void DispatchWithGuardBands(ComputeShader shader, int kernelId, in Vector2Int si if (h < source.rt.height && h % 8 < k_RTGuardBandSize) h += k_RTGuardBandSize; - cmd.DispatchCompute(shader, kernelId, (w + 7) / 8, (h + 7) / 8, camera.viewCount); + cmd.DispatchCompute(shader, kernelId, (w + 7) / 8, (h + 7) / 8, viewCount); } // Pre-filtering @@ -2788,7 +2817,7 @@ void DispatchWithGuardBands(ComputeShader shader, int kernelId, in Vector2Int si cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._OutputTexture, bloomMipsUp[0]); // Use m_BloomMipsUp as temp target cmd.SetComputeVectorParam(cs, HDShaderIDs._TexelSize, new Vector4(size.x, size.y, 1f / size.x, 1f / size.y)); cmd.SetComputeVectorParam(cs, HDShaderIDs._BloomThreshold, bloomParameters.thresholdParams); - DispatchWithGuardBands(cs, kernel, size); + DispatchWithGuardBands(cs, kernel, size, bloomParameters.viewCount); cs = bloomParameters.bloomBlurCS; kernel = bloomParameters.bloomBlurKernel; @@ -2796,7 +2825,7 @@ void DispatchWithGuardBands(ComputeShader shader, int kernelId, in Vector2Int si cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._InputTexture, bloomMipsUp[0]); cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._OutputTexture, bloomMipsDown[0]); cmd.SetComputeVectorParam(cs, HDShaderIDs._TexelSize, new Vector4(size.x, size.y, 1f / size.x, 1f / size.y)); - DispatchWithGuardBands(cs, kernel, size); + DispatchWithGuardBands(cs, kernel, size, bloomParameters.viewCount); } // Blur pyramid @@ -2811,7 +2840,7 @@ void DispatchWithGuardBands(ComputeShader shader, int kernelId, in Vector2Int si cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._InputTexture, src); cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._OutputTexture, dst); cmd.SetComputeVectorParam(cs, HDShaderIDs._TexelSize, new Vector4(size.x, size.y, 1f / size.x, 1f / size.y)); - DispatchWithGuardBands(cs, kernel, size); + DispatchWithGuardBands(cs, kernel, size, bloomParameters.viewCount); } // Upsample & combine @@ -2833,7 +2862,7 @@ void DispatchWithGuardBands(ComputeShader shader, int kernelId, in Vector2Int si cmd.SetComputeVectorParam(cs, HDShaderIDs._Params, new Vector4(bloomParameters.bloomScatterParam, 0f, 0f, 0f)); cmd.SetComputeVectorParam(cs, HDShaderIDs._BloomBicubicParams, new Vector4(lowSize.x, lowSize.y, 1f / lowSize.x, 1f / lowSize.y)); cmd.SetComputeVectorParam(cs, HDShaderIDs._TexelSize, new Vector4(highSize.x, highSize.y, 1f / highSize.x, 1f / highSize.y)); - DispatchWithGuardBands(cs, kernel, highSize); + DispatchWithGuardBands(cs, kernel, highSize, bloomParameters.viewCount); } } @@ -3237,24 +3266,32 @@ struct FXAAParameters { public ComputeShader fxaaCS; public int fxaaKernel; + + public int width; + public int height; + public int viewCount; } - FXAAParameters PrepareFXAAParameters() + FXAAParameters PrepareFXAAParameters(HDCamera camera) { FXAAParameters parameters = new FXAAParameters(); parameters.fxaaCS = m_Resources.shaders.FXAACS; parameters.fxaaKernel = parameters.fxaaCS.FindKernel("FXAA"); + parameters.width = camera.actualWidth; + parameters.height = camera.actualHeight; + parameters.viewCount = camera.viewCount; + return parameters; } - void DoFXAA(in FXAAParameters parameters, CommandBuffer cmd, HDCamera camera, RTHandle source, RTHandle destination) + void DoFXAA(in FXAAParameters parameters, CommandBuffer cmd, RTHandle source, RTHandle destination) { var cs = parameters.fxaaCS; int kernel = parameters.fxaaKernel; cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._InputTexture, source); cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._OutputTexture, destination); - cmd.DispatchCompute(cs, kernel, (camera.actualWidth + 7) / 8, (camera.actualHeight + 7) / 8, camera.viewCount); + cmd.DispatchCompute(cs, kernel, (parameters.width + 7) / 8, (parameters.height + 7) / 8, parameters.viewCount); } #endregion @@ -3311,7 +3348,7 @@ void RecycleSMAARenderTargets(RTHandle smaaEdgeTex, RTHandle smaaBlendTex) m_Pool.Recycle(smaaBlendTex); } - static void DoSMAA(in SMAAParameters parameters, CommandBuffer cmd, HDCamera camera, RTHandle source, RTHandle smaaEdgeTex, RTHandle smaaBlendTex, RTHandle destination, RTHandle depthBuffer) + static void DoSMAA(in SMAAParameters parameters, CommandBuffer cmd, RTHandle source, RTHandle smaaEdgeTex, RTHandle smaaBlendTex, RTHandle destination, RTHandle depthBuffer) { parameters.smaaMaterial.SetVector(HDShaderIDs._SMAARTMetrics, parameters.smaaRTMetrics); parameters.smaaMaterial.SetTexture(HDShaderIDs._SMAAAreaTex, parameters.smaaAreaTex); diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDProfileId.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDProfileId.cs index 0187f373785..0371848c1f4 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDProfileId.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDProfileId.cs @@ -156,6 +156,7 @@ internal enum HDProfileId UpsampleLowResTransparent, // Post-processing + GuardBandClear, AlphaCopy, StopNaNs, FixedExposure, From 0d7fff8bd9bb72f5b229cffa4ab058e5418692bb Mon Sep 17 00:00:00 2001 From: FrancescoC-Unity Date: Thu, 18 Jun 2020 15:11:03 +0200 Subject: [PATCH 02/11] Make fixed exposure rendergraph ready --- .../PostProcessSystem.RenderGraph.cs | 10 ----- .../PostProcessing/PostProcessSystem.cs | 45 ++++++------------- 2 files changed, 14 insertions(+), 41 deletions(-) diff --git a/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.RenderGraph.cs b/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.RenderGraph.cs index 2c4538d2cb5..3facd2e7083 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.RenderGraph.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.RenderGraph.cs @@ -130,16 +130,6 @@ public void Render( RenderGraph renderGraph, } } - // if (stopNaNs) - // { - // using (new ProfilingScope(cmd, ProfilingSampler.Get(HDProfileId.StopNaNs))) - // { - // var destination = m_Pool.Get(Vector2.one, m_ColorFormat); - // DoStopNaNs(cmd, camera, source, destination); - // PoolSource(ref source, destination); - // } - // } - // } } diff --git a/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.cs b/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.cs index 0d25828a647..4976f938fab 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.cs @@ -404,7 +404,9 @@ public void BeginFrame(CommandBuffer cmd, HDCamera camera, HDRenderPipeline hdIn { using (new ProfilingScope(cmd, ProfilingSampler.Get(HDProfileId.FixedExposure))) { - DoFixedExposure(cmd, camera); + RTHandle prevExposure; + GrabExposureHistoryTextures(camera, out prevExposure, out _); + DoFixedExposure(PrepareExposureParameters(camera), cmd, prevExposure); } } @@ -1024,6 +1026,12 @@ ExposureParameter PrepareExposureParameters(HDCamera hdCamera) { parameters.exposureReductionKernel = parameters.exposureCS.FindKernel("KFixedExposure"); parameters.exposureParams = new Vector4(m_Exposure.compensation.value + m_DebugExposureCompensation, m_Exposure.fixedExposure.value, 0f, 0f); +#if UNITY_EDITOR + if (HDAdditionalSceneViewSettings.sceneExposureOverriden) + { + parameters.exposureParams = new Vector4(0.0f, HDAdditionalSceneViewSettings.sceneExposure, 0f, 0f); + } +#endif } else if (m_Exposure.mode == ExposureMode.UsePhysicalCamera) { @@ -1194,38 +1202,13 @@ internal ComputeBuffer GetDebugImageHistogramBuffer() return m_DebugImageHistogramBuffer; } - // TODO_FCC: MISSING!! - void DoFixedExposure(CommandBuffer cmd, HDCamera camera) + void DoFixedExposure(in ExposureParameter parameters, CommandBuffer cmd, RTHandle prevExposure) { - var cs = m_Resources.shaders.exposureCS; - - cmd.SetComputeVectorParam(cs, HDShaderIDs._ExposureParams2, new Vector4(0.0f, 0.0f, ColorUtils.lensImperfectionExposureScale, ColorUtils.s_LightMeterCalibrationConstant)); - - GrabExposureHistoryTextures(camera, out var prevExposure, out _); - - int kernel = 0; + var cs = parameters.exposureCS; + int kernel = parameters.exposureReductionKernel; - if (m_Exposure.mode.value == ExposureMode.Fixed - #if UNITY_EDITOR - || (HDAdditionalSceneViewSettings.sceneExposureOverriden && camera.camera.cameraType == CameraType.SceneView) - #endif - ) - { - kernel = cs.FindKernel("KFixedExposure"); - var exposureParam = new Vector4(m_Exposure.compensation.value + m_DebugExposureCompensation, m_Exposure.fixedExposure.value, 0f, 0f); - #if UNITY_EDITOR - if (HDAdditionalSceneViewSettings.sceneExposureOverriden) - { - exposureParam = new Vector4(0.0f, HDAdditionalSceneViewSettings.sceneExposure, 0f, 0f); - } - #endif - cmd.SetComputeVectorParam(cs, HDShaderIDs._ExposureParams, exposureParam); - } - else if (m_Exposure.mode == ExposureMode.UsePhysicalCamera) - { - kernel = cs.FindKernel("KManualCameraExposure"); - cmd.SetComputeVectorParam(cs, HDShaderIDs._ExposureParams, new Vector4(m_Exposure.compensation.value + m_DebugExposureCompensation, m_PhysicalCamera.aperture, m_PhysicalCamera.shutterSpeed, m_PhysicalCamera.iso)); - } + cmd.SetComputeVectorParam(cs, HDShaderIDs._ExposureParams, parameters.exposureParams); + cmd.SetComputeVectorParam(cs, HDShaderIDs._ExposureParams2, parameters.exposureParams2); cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._OutputTexture, prevExposure); cmd.DispatchCompute(cs, kernel, 1, 1, 1); From 6e80082586f8851159f62b40d75a3020171cf468 Mon Sep 17 00:00:00 2001 From: FrancescoC-Unity Date: Mon, 29 Jun 2020 09:58:17 +0200 Subject: [PATCH 03/11] More steps toward final conversion --- .../PostProcessSystem.RenderGraph.cs | 774 +++++++++++------- .../PostProcessing/PostProcessSystem.cs | 75 +- .../Runtime/RenderPipeline/HDProfileId.cs | 1 + .../HDRenderPipeline.PostProcess.cs | 2 + .../HDRenderPipeline.RenderGraph.cs | 2 +- 5 files changed, 539 insertions(+), 315 deletions(-) diff --git a/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.RenderGraph.cs b/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.RenderGraph.cs index 3facd2e7083..7f560c4bc82 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.RenderGraph.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.RenderGraph.cs @@ -40,14 +40,57 @@ class StopNaNPassData public TextureHandle destination; } - public void Render( RenderGraph renderGraph, - HDCamera hdCamera, - BlueNoise blueNoise, - TextureHandle colorBuffer, - TextureHandle afterPostProcessTexture, - TextureHandle depthBuffer, - TextureHandle finalRT, - bool flipY) + class DynamicExposureData + { + public ExposureParameters parameters; + public TextureHandle source; + public TextureHandle prevExposure; + public TextureHandle nextExposure; + public TextureHandle exposureDebugData; + public TextureHandle tmpTarget1024; + public TextureHandle tmpTarget32; + } + + class ApplyExposureData + { + public ApplyExposureParameters parameters; + public TextureHandle source; + public TextureHandle destination; + public TextureHandle prevExposure; + } + + class TemporalAntiAliasingData + { + public TemporalAntiAliasingParameters parameters; + public TextureHandle source; + public TextureHandle destination; + public TextureHandle depthBuffer; + public TextureHandle depthMipChain; + public TextureHandle prevHistory; + public TextureHandle nextHistory; + public TextureHandle prevMVLen; + public TextureHandle nextMVLen; + } + + class SMAAData + { + public SMAAParameters parameters; + public TextureHandle source; + public TextureHandle destination; + public TextureHandle depthBuffer; + public TextureHandle smaaEdgeTex; + public TextureHandle smaaBlendTex; + } + + public void Render(RenderGraph renderGraph, + HDCamera hdCamera, + BlueNoise blueNoise, + TextureHandle colorBuffer, + TextureHandle afterPostProcessTexture, + TextureHandle depthBuffer, + TextureHandle depthBufferMipChain, + TextureHandle finalRT, + bool flipY) { var dynResHandler = DynamicResolutionHandler.instance; @@ -63,12 +106,12 @@ public void Render( RenderGraph renderGraph, passData.parameters = PrepareCopyAlphaParameters(hdCamera); passData.source = builder.ReadTexture(source); passData.outputAlpha = builder.WriteTexture(renderGraph.CreateTexture(new TextureDesc(Vector2.one, true, true) - { name = "Alpha Channel Copy", colorFormat = GraphicsFormat.R16_SFloat, enableRandomWrite = true })); + { name = "Alpha Channel Copy", colorFormat = GraphicsFormat.R16_SFloat, enableRandomWrite = true })); builder.SetRenderFunc( (AlphaCopyPassData data, RenderGraphContext ctx) => { - DoCopyAlpha( data.parameters, + DoCopyAlpha(data.parameters, ctx.resources.GetTexture(data.source), ctx.resources.GetTexture(data.outputAlpha), ctx.cmd); @@ -130,329 +173,482 @@ public void Render( RenderGraph renderGraph, } } + // Dynamic exposure - will be applied in the next frame + // Not considered as a post-process so it's not affected by its enabled state + // Dynamic exposure - will be applied in the next frame + // Not considered as a post-process so it's not affected by its enabled state + if (!IsExposureFixed(hdCamera) && m_ExposureControlFS) + { + var exposureParameters = PrepareExposureParameters(hdCamera); - } + GrabExposureRequiredTextures(hdCamera, out var prevExposure, out var nextExposure); - // TODO RENDERGRAPH: Implement - // if (m_PostProcessEnabled) - // { - // // Guard bands (also known as "horrible hack") to avoid bleeding previous RTHandle - // // content into smaller viewports with some effects like Bloom that rely on bilinear - // // filtering and can't use clamp sampler and the likes - // // Note: some platforms can't clear a partial render target so we directly draw black triangles - // { - // int w = camera.actualWidth; - // int h = camera.actualHeight; - // cmd.SetRenderTarget(source, 0, CubemapFace.Unknown, -1); - - // if (w < source.rt.width || h < source.rt.height) - // { - // cmd.SetViewport(new Rect(w, 0, k_RTGuardBandSize, h)); - // cmd.DrawProcedural(Matrix4x4.identity, m_ClearBlackMaterial, 0, MeshTopology.Triangles, 3, 1); - // cmd.SetViewport(new Rect(0, h, w + k_RTGuardBandSize, k_RTGuardBandSize)); - // cmd.DrawProcedural(Matrix4x4.identity, m_ClearBlackMaterial, 0, MeshTopology.Triangles, 3, 1); - // } - // } - - // // Optional NaN killer before post-processing kicks in - // bool stopNaNs = camera.stopNaNs && m_StopNaNFS; - - //#if UNITY_EDITOR - // if (isSceneView) - // stopNaNs = HDAdditionalSceneViewSettings.sceneViewStopNaNs; - //#endif - - // if (stopNaNs) - // { - // using (new ProfilingScope(cmd, ProfilingSampler.Get(HDProfileId.StopNaNs))) - // { - // var destination = m_Pool.Get(Vector2.one, m_ColorFormat); - // DoStopNaNs(cmd, camera, source, destination); - // PoolSource(ref source, destination); - // } - // } - // } - - // // Dynamic exposure - will be applied in the next frame - // // Not considered as a post-process so it's not affected by its enabled state - // if (!IsExposureFixed() && m_ExposureControlFS) - // { - // using (new ProfilingScope(cmd, ProfilingSampler.Get(HDProfileId.DynamicExposure))) - // { - // if (m_Exposure.mode.value == ExposureMode.AutomaticHistogram) - // { - // DoHistogramBasedExposure(cmd, camera, source); - // } - // else - // { - // DoDynamicExposure(cmd, camera, source); - // } - - // // On reset history we need to apply dynamic exposure immediately to avoid - // // white or black screen flashes when the current exposure isn't anywhere - // // near 0 - // if (camera.resetPostProcessingHistory) - // { - // var destination = m_Pool.Get(Vector2.one, m_ColorFormat); - - // var cs = m_Resources.shaders.applyExposureCS; - // int kernel = cs.FindKernel("KMain"); - - // // Note: we call GetPrevious instead of GetCurrent because the textures - // // are swapped internally as the system expects the texture will be used - // // on the next frame. So the actual "current" for this frame is in - // // "previous". - // cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._ExposureTexture, GetPreviousExposureTexture(camera)); - // cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._InputTexture, source); - // cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._OutputTexture, destination); - // cmd.DispatchCompute(cs, kernel, (camera.actualWidth + 7) / 8, (camera.actualHeight + 7) / 8, camera.viewCount); - - // PoolSource(ref source, destination); - // } - // } - // } + var prevExposureHandle = renderGraph.ImportTexture(prevExposure); + var nextExposureHandle = renderGraph.ImportTexture(nextExposure); - if (m_PostProcessEnabled) - { - // // Temporal anti-aliasing goes first - // bool taaEnabled = false; + using (var builder = renderGraph.AddRenderPass("Dynamic Exposure", out var passData, ProfilingSampler.Get(HDProfileId.DynamicExposure))) + { + passData.source = builder.ReadTexture(source); + passData.parameters = PrepareExposureParameters(hdCamera); + passData.prevExposure = prevExposureHandle; + passData.nextExposure = nextExposureHandle; - // if (m_AntialiasingFS) - // { - // taaEnabled = camera.antialiasing == AntialiasingMode.TemporalAntialiasing; + if (m_Exposure.mode.value == ExposureMode.AutomaticHistogram) + { + passData.exposureDebugData = renderGraph.ImportTexture(m_DebugExposureData); + builder.SetRenderFunc( + (DynamicExposureData data, RenderGraphContext ctx) => + { + DoHistogramBasedExposure(data.parameters, ctx.cmd, ctx.resources.GetTexture(data.source), + ctx.resources.GetTexture(data.prevExposure), + ctx.resources.GetTexture(data.nextExposure), + ctx.resources.GetTexture(data.exposureDebugData)); + }); + } + else + { + passData.tmpTarget1024 = builder.CreateTransientTexture(new TextureDesc(1024, 1024, true, false) + { colorFormat = GraphicsFormat.R16G16_SFloat, enableRandomWrite = true, name = "Average Luminance Temp 1024" }); + passData.tmpTarget32 = builder.CreateTransientTexture(new TextureDesc(32, 32, true, false) + { colorFormat = GraphicsFormat.R16G16_SFloat, enableRandomWrite = true, name = "Average Luminance Temp 32" }); + + builder.SetRenderFunc( + (DynamicExposureData data, RenderGraphContext ctx) => + { + DoDynamicExposure(data.parameters, ctx.cmd, ctx.resources.GetTexture(data.source), + ctx.resources.GetTexture(data.prevExposure), + ctx.resources.GetTexture(data.nextExposure), + ctx.resources.GetTexture(data.tmpTarget1024), + ctx.resources.GetTexture(data.tmpTarget32)); + }); + } + } - // if (taaEnabled) - // { - // using (new ProfilingScope(cmd, ProfilingSampler.Get(HDProfileId.TemporalAntialiasing))) - // { - // var destination = m_Pool.Get(Vector2.one, m_ColorFormat); - // DoTemporalAntialiasing(cmd, camera, source, destination, depthBuffer, depthMipChain); - // PoolSource(ref source, destination); - // } - // } - // else if (camera.antialiasing == AntialiasingMode.SubpixelMorphologicalAntiAliasing) - // { - // using (new ProfilingScope(cmd, ProfilingSampler.Get(HDProfileId.SMAA))) - // { - // var destination = m_Pool.Get(Vector2.one, m_ColorFormat); - // DoSMAA(cmd, camera, source, destination, depthBuffer); - // PoolSource(ref source, destination); - // } - // } - // } + if (hdCamera.resetPostProcessingHistory) + { + using (var builder = renderGraph.AddRenderPass("Apply Exposure", out var passData, ProfilingSampler.Get(HDProfileId.ApplyExposure))) + { + passData.source = builder.ReadTexture(source); + passData.parameters = PrepareApplyExposureParameters(hdCamera); + RTHandle prevExp; + GrabExposureHistoryTextures(hdCamera, out prevExp, out _); + passData.prevExposure = renderGraph.ImportTexture(prevExp); + + TextureHandle dest = renderGraph.CreateTexture(new TextureDesc(Vector2.one, true, true) + { + name = "Apply Exposure Destination", + colorFormat = m_ColorFormat, + useMipMap = false, + enableRandomWrite = true + }); + passData.destination = builder.WriteTexture(dest); ; + + builder.SetRenderFunc( + (ApplyExposureData data, RenderGraphContext ctx) => + { + ApplyExposure(data.parameters, ctx.cmd, ctx.resources.GetTexture(data.source), ctx.resources.GetTexture(data.destination), ctx.resources.GetTexture(data.prevExposure)); + }); + + source = passData.destination; + } + } + } - // if (camera.frameSettings.IsEnabled(FrameSettingsField.CustomPostProcess)) - // { - // using (new ProfilingScope(cmd, ProfilingSampler.Get(HDProfileId.CustomPostProcessBeforePP))) - // { - // foreach (var typeString in HDRenderPipeline.defaultAsset.beforePostProcessCustomPostProcesses) - // RenderCustomPostProcess(cmd, camera, ref source, colorBuffer, Type.GetType(typeString)); - // } - // } + // Temporal anti-aliasing goes first + bool taaEnabled = false; - // // If Path tracing is enabled, then DoF is computed in the path tracer by sampling the lens aperure (when using the physical camera mode) - // bool isDoFPathTraced = (camera.frameSettings.IsEnabled(FrameSettingsField.RayTracing) && - // camera.volumeStack.GetComponent().enable.value && - // camera.camera.cameraType != CameraType.Preview && - // m_DepthOfField.focusMode == DepthOfFieldMode.UsePhysicalCamera); - // // Depth of Field is done right after TAA as it's easier to just re-project the CoC - // // map rather than having to deal with all the implications of doing it before TAA - // if (m_DepthOfField.IsActive() && !isSceneView && m_DepthOfFieldFS && !isDoFPathTraced) + //if (camera.frameSettings.IsEnabled(FrameSettingsField.CustomPostProcess)) + //{ + // using (new ProfilingScope(cmd, ProfilingSampler.Get(HDProfileId.CustomPostProcessBeforeTAA))) + // { + // foreach (var typeString in HDRenderPipeline.defaultAsset.beforeTAACustomPostProcesses) + // RenderCustomPostProcess(cmd, camera, ref source, colorBuffer, Type.GetType(typeString)); + // } + //} + + if (m_AntialiasingFS) + { + taaEnabled = hdCamera.antialiasing == HDAdditionalCameraData.AntialiasingMode.TemporalAntialiasing; + + if (taaEnabled) + { + using (var builder = renderGraph.AddRenderPass("Temporal Anti-Aliasing", out var passData, ProfilingSampler.Get(HDProfileId.TemporalAntialiasing))) + { + GrabTemporalAntialiasingHistoryTextures(hdCamera, out var prevHistory, out var nextHistory); + GrabVelocityMagnitudeHistoryTextures(hdCamera, out var prevMVLen, out var nextMVLen); + + passData.source = builder.ReadTexture(source); + passData.parameters = PrepareTAAParameters(hdCamera); + passData.depthBuffer = builder.ReadTexture(depthBuffer); + passData.depthMipChain = builder.ReadTexture(depthBufferMipChain); + passData.prevHistory = renderGraph.ImportTexture(prevHistory); + passData.nextHistory = renderGraph.ImportTexture(nextHistory); + passData.prevMVLen = renderGraph.ImportTexture(prevMVLen); + passData.nextMVLen = renderGraph.ImportTexture(nextMVLen); + + TextureHandle dest = renderGraph.CreateTexture(new TextureDesc(Vector2.one, true, true) + { + name = "TAA Destination", + colorFormat = m_ColorFormat, + useMipMap = false, + enableRandomWrite = true + }); + passData.destination = builder.WriteTexture(dest); ; + + builder.SetRenderFunc( + (TemporalAntiAliasingData data, RenderGraphContext ctx) => + { + DoTemporalAntialiasing(data.parameters, ctx.cmd, ctx.resources.GetTexture(data.source), + ctx.resources.GetTexture(data.destination), + ctx.resources.GetTexture(data.depthBuffer), + ctx.resources.GetTexture(data.depthMipChain), + ctx.resources.GetTexture(data.prevHistory), + ctx.resources.GetTexture(data.nextHistory), + ctx.resources.GetTexture(data.prevMVLen), + ctx.resources.GetTexture(data.nextMVLen)); + }); + + source = passData.destination; + } + } + else if (hdCamera.antialiasing == HDAdditionalCameraData.AntialiasingMode.SubpixelMorphologicalAntiAliasing) + { + using (var builder = renderGraph.AddRenderPass("Temporal Anti-Aliasing", out var passData, ProfilingSampler.Get(HDProfileId.TemporalAntialiasing))) + { + + passData.source = builder.ReadTexture(source); + passData.parameters = PrepareSMAAParameters(hdCamera); + builder.ReadTexture(depthBuffer); + passData.depthBuffer = builder.WriteTexture(depthBuffer); + passData.smaaEdgeTex = builder.CreateTransientTexture(new TextureDesc(Vector2.one, true, true) + { colorFormat = GraphicsFormat.R8G8B8A8_UNorm, enableRandomWrite = true, name = "SMAA Edge Texture" }); + passData.smaaBlendTex = builder.CreateTransientTexture(new TextureDesc(Vector2.one, true, true) + { colorFormat = GraphicsFormat.R8G8B8A8_UNorm, enableRandomWrite = true, name = "SMAA Blend Texture" }); + TextureHandle dest = renderGraph.CreateTexture(new TextureDesc(Vector2.one, true, true) + { + name = "SMAA Destination", + colorFormat = m_ColorFormat, + useMipMap = false, + enableRandomWrite = true + }); + passData.destination = builder.WriteTexture(dest); ; + + builder.SetRenderFunc( + (SMAAData data, RenderGraphContext ctx) => + { + DoSMAA(data.parameters, ctx.cmd, ctx.resources.GetTexture(data.source), + ctx.resources.GetTexture(data.smaaEdgeTex), + ctx.resources.GetTexture(data.smaaBlendTex), + ctx.resources.GetTexture(data.destination), + ctx.resources.GetTexture(data.depthBuffer)); + }); + + source = passData.destination; + + + // DoSMAA(PrepareSMAAParameters(camera), cmd, source, smaaEdgeTex, smaaBlendTex, destination, depthBuffer); + + } + //{ + // using (new ProfilingScope(cmd, ProfilingSampler.Get(HDProfileId.SMAA))) + // { + // var destination = m_Pool.Get(Vector2.one, m_ColorFormat); + // RTHandle smaaEdgeTex, smaaBlendTex; + // AllocateSMAARenderTargets(camera, out smaaEdgeTex, out smaaBlendTex); + // DoSMAA(PrepareSMAAParameters(camera), cmd, source, smaaEdgeTex, smaaBlendTex, destination, depthBuffer); + // RecycleSMAARenderTargets(smaaEdgeTex, smaaBlendTex); + // PoolSource(ref source, destination); + // } + //} + } + } + + // TODO RENDERGRAPH: Implement + + // // Dynamic exposure - will be applied in the next frame + // // Not considered as a post-process so it's not affected by its enabled state + // if (!IsExposureFixed() && m_ExposureControlFS) + // { + // using (new ProfilingScope(cmd, ProfilingSampler.Get(HDProfileId.DynamicExposure))) // { - // using (new ProfilingScope(cmd, ProfilingSampler.Get(HDProfileId.DepthOfField))) + // if (m_Exposure.mode.value == ExposureMode.AutomaticHistogram) // { - // var destination = m_Pool.Get(Vector2.one, m_ColorFormat); - // DoDepthOfField(cmd, camera, source, destination, taaEnabled); - // PoolSource(ref source, destination); + // DoHistogramBasedExposure(cmd, camera, source); // } - // } - - // // Motion blur after depth of field for aesthetic reasons (better to see motion - // // blurred bokeh rather than out of focus motion blur) - // if (m_MotionBlur.IsActive() && m_AnimatedMaterialsEnabled && !camera.resetPostProcessingHistory && m_MotionBlurFS) - // { - // using (new ProfilingScope(cmd, ProfilingSampler.Get(HDProfileId.MotionBlur))) + // else // { - // var destination = m_Pool.Get(Vector2.one, m_ColorFormat); - // DoMotionBlur(cmd, camera, source, destination); - // PoolSource(ref source, destination); + // DoDynamicExposure(cmd, camera, source); // } - // } - // // Panini projection is done as a fullscreen pass after all depth-based effects are - // // done and before bloom kicks in - // // This is one effect that would benefit from an overscan mode or supersampling in - // // HDRP to reduce the amount of resolution lost at the center of the screen - // if (m_PaniniProjection.IsActive() && !isSceneView && m_PaniniProjectionFS) - // { - // using (new ProfilingScope(cmd, ProfilingSampler.Get(HDProfileId.PaniniProjection))) + // // On reset history we need to apply dynamic exposure immediately to avoid + // // white or black screen flashes when the current exposure isn't anywhere + // // near 0 + // if (camera.resetPostProcessingHistory) // { // var destination = m_Pool.Get(Vector2.one, m_ColorFormat); - // DoPaniniProjection(cmd, camera, source, destination); + + // var cs = m_Resources.shaders.applyExposureCS; + // int kernel = cs.FindKernel("KMain"); + + // // Note: we call GetPrevious instead of GetCurrent because the textures + // // are swapped internally as the system expects the texture will be used + // // on the next frame. So the actual "current" for this frame is in + // // "previous". + // cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._ExposureTexture, GetPreviousExposureTexture(camera)); + // cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._InputTexture, source); + // cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._OutputTexture, destination); + // cmd.DispatchCompute(cs, kernel, (camera.actualWidth + 7) / 8, (camera.actualHeight + 7) / 8, camera.viewCount); + // PoolSource(ref source, destination); // } // } + // } - // Uber post-process - //// Generate the bloom texture - //bool bloomActive = m_Bloom.IsActive() && m_BloomFS; - - //if (bloomActive) - //{ - // using (new ProfilingScope(cmd, ProfilingSampler.Get(HDProfileId.Bloom))) - // { - // DoBloom(cmd, camera, source, uberPostParams.uberPostCS, uberPostParams.uberPostKernel); - // } - //} - //else - //{ - // cmd.SetComputeTextureParam(uberPostParams.uberPostCS, uberPostParams.uberPostKernel, HDShaderIDs._BloomTexture, TextureXR.GetBlackTexture()); - // cmd.SetComputeTextureParam(uberPostParams.uberPostCS, uberPostParams.uberPostKernel, HDShaderIDs._BloomDirtTexture, Texture2D.blackTexture); - // cmd.SetComputeVectorParam(uberPostParams.uberPostCS, HDShaderIDs._BloomParams, Vector4.zero); - //} - - TextureHandle logLutOutput; - using (var builder = renderGraph.AddRenderPass("Color Grading", out var passData, ProfilingSampler.Get(HDProfileId.ColorGradingLUTBuilder))) + if (m_PostProcessEnabled) { - TextureHandle logLut = renderGraph.CreateTexture(new TextureDesc(m_LutSize, m_LutSize) + // // Temporal anti-aliasing goes first + // bool taaEnabled = false; + + // if (m_AntialiasingFS) + // { + // taaEnabled = camera.antialiasing == AntialiasingMode.TemporalAntialiasing; + + // if (taaEnabled) + // { + // using (new ProfilingScope(cmd, ProfilingSampler.Get(HDProfileId.TemporalAntialiasing))) + // { + // var destination = m_Pool.Get(Vector2.one, m_ColorFormat); + // DoTemporalAntialiasing(cmd, camera, source, destination, depthBuffer, depthMipChain); + // PoolSource(ref source, destination); + // } + // } + // else if (camera.antialiasing == AntialiasingMode.SubpixelMorphologicalAntiAliasing) + // { + // using (new ProfilingScope(cmd, ProfilingSampler.Get(HDProfileId.SMAA))) + // { + // var destination = m_Pool.Get(Vector2.one, m_ColorFormat); + // DoSMAA(cmd, camera, source, destination, depthBuffer); + // PoolSource(ref source, destination); + // } + // } + // } + + // if (camera.frameSettings.IsEnabled(FrameSettingsField.CustomPostProcess)) + // { + // using (new ProfilingScope(cmd, ProfilingSampler.Get(HDProfileId.CustomPostProcessBeforePP))) + // { + // foreach (var typeString in HDRenderPipeline.defaultAsset.beforePostProcessCustomPostProcesses) + // RenderCustomPostProcess(cmd, camera, ref source, colorBuffer, Type.GetType(typeString)); + // } + // } + + // // If Path tracing is enabled, then DoF is computed in the path tracer by sampling the lens aperure (when using the physical camera mode) + // bool isDoFPathTraced = (camera.frameSettings.IsEnabled(FrameSettingsField.RayTracing) && + // camera.volumeStack.GetComponent().enable.value && + // camera.camera.cameraType != CameraType.Preview && + // m_DepthOfField.focusMode == DepthOfFieldMode.UsePhysicalCamera); + + // // Depth of Field is done right after TAA as it's easier to just re-project the CoC + // // map rather than having to deal with all the implications of doing it before TAA + // if (m_DepthOfField.IsActive() && !isSceneView && m_DepthOfFieldFS && !isDoFPathTraced) + // { + // using (new ProfilingScope(cmd, ProfilingSampler.Get(HDProfileId.DepthOfField))) + // { + // var destination = m_Pool.Get(Vector2.one, m_ColorFormat); + // DoDepthOfField(cmd, camera, source, destination, taaEnabled); + // PoolSource(ref source, destination); + // } + // } + + // // Motion blur after depth of field for aesthetic reasons (better to see motion + // // blurred bokeh rather than out of focus motion blur) + // if (m_MotionBlur.IsActive() && m_AnimatedMaterialsEnabled && !camera.resetPostProcessingHistory && m_MotionBlurFS) + // { + // using (new ProfilingScope(cmd, ProfilingSampler.Get(HDProfileId.MotionBlur))) + // { + // var destination = m_Pool.Get(Vector2.one, m_ColorFormat); + // DoMotionBlur(cmd, camera, source, destination); + // PoolSource(ref source, destination); + // } + // } + + // // Panini projection is done as a fullscreen pass after all depth-based effects are + // // done and before bloom kicks in + // // This is one effect that would benefit from an overscan mode or supersampling in + // // HDRP to reduce the amount of resolution lost at the center of the screen + // if (m_PaniniProjection.IsActive() && !isSceneView && m_PaniniProjectionFS) + // { + // using (new ProfilingScope(cmd, ProfilingSampler.Get(HDProfileId.PaniniProjection))) + // { + // var destination = m_Pool.Get(Vector2.one, m_ColorFormat); + // DoPaniniProjection(cmd, camera, source, destination); + // PoolSource(ref source, destination); + // } + // } + + // Uber post-process + //// Generate the bloom texture + //bool bloomActive = m_Bloom.IsActive() && m_BloomFS; + + //if (bloomActive) + //{ + // using (new ProfilingScope(cmd, ProfilingSampler.Get(HDProfileId.Bloom))) + // { + // DoBloom(cmd, camera, source, uberPostParams.uberPostCS, uberPostParams.uberPostKernel); + // } + //} + //else + //{ + // cmd.SetComputeTextureParam(uberPostParams.uberPostCS, uberPostParams.uberPostKernel, HDShaderIDs._BloomTexture, TextureXR.GetBlackTexture()); + // cmd.SetComputeTextureParam(uberPostParams.uberPostCS, uberPostParams.uberPostKernel, HDShaderIDs._BloomDirtTexture, Texture2D.blackTexture); + // cmd.SetComputeVectorParam(uberPostParams.uberPostCS, HDShaderIDs._BloomParams, Vector4.zero); + //} + + TextureHandle logLutOutput; + using (var builder = renderGraph.AddRenderPass("Color Grading", out var passData, ProfilingSampler.Get(HDProfileId.ColorGradingLUTBuilder))) { - name = "Color Grading Log Lut", - dimension = TextureDimension.Tex3D, - slices = m_LutSize, - depthBufferBits = DepthBits.None, - colorFormat = m_LutFormat, - filterMode = FilterMode.Bilinear, - wrapMode = TextureWrapMode.Clamp, - anisoLevel = 0, - useMipMap = false, - enableRandomWrite = true - }); + TextureHandle logLut = renderGraph.CreateTexture(new TextureDesc(m_LutSize, m_LutSize) + { + name = "Color Grading Log Lut", + dimension = TextureDimension.Tex3D, + slices = m_LutSize, + depthBufferBits = DepthBits.None, + colorFormat = m_LutFormat, + filterMode = FilterMode.Bilinear, + wrapMode = TextureWrapMode.Clamp, + anisoLevel = 0, + useMipMap = false, + enableRandomWrite = true + }); - passData.parameters = PrepareColorGradingParameters(); - passData.logLut = builder.WriteTexture(logLut); - logLutOutput = passData.logLut; + passData.parameters = PrepareColorGradingParameters(); + passData.logLut = builder.WriteTexture(logLut); + logLutOutput = passData.logLut; - builder.SetRenderFunc( - (ColorGradingPassData data, RenderGraphContext ctx) => - { - DoColorGrading(data.parameters, ctx.resources.GetTexture(data.logLut), ctx.cmd); - }); - } + builder.SetRenderFunc( + (ColorGradingPassData data, RenderGraphContext ctx) => + { + DoColorGrading(data.parameters, ctx.resources.GetTexture(data.logLut), ctx.cmd); + }); + } - using (var builder = renderGraph.AddRenderPass("Uber Post", out var passData, ProfilingSampler.Get(HDProfileId.UberPost))) - { - TextureHandle dest = renderGraph.CreateTexture(new TextureDesc(Vector2.one, true, true) + using (var builder = renderGraph.AddRenderPass("Uber Post", out var passData, ProfilingSampler.Get(HDProfileId.UberPost))) { - name = "Uber Post Destination", - colorFormat = m_ColorFormat, - useMipMap = false, - enableRandomWrite = true - }); + TextureHandle dest = renderGraph.CreateTexture(new TextureDesc(Vector2.one, true, true) + { + name = "Uber Post Destination", + colorFormat = m_ColorFormat, + useMipMap = false, + enableRandomWrite = true + }); - passData.parameters = PrepareUberPostParameters(hdCamera, isSceneView); - passData.source = builder.ReadTexture(source); - passData.logLut = builder.ReadTexture(logLutOutput); - passData.destination = builder.WriteTexture(dest); + passData.parameters = PrepareUberPostParameters(hdCamera, isSceneView); + passData.source = builder.ReadTexture(source); + passData.logLut = builder.ReadTexture(logLutOutput); + passData.destination = builder.WriteTexture(dest); - builder.SetRenderFunc( - (UberPostPassData data, RenderGraphContext ctx) => - { + builder.SetRenderFunc( + (UberPostPassData data, RenderGraphContext ctx) => + { //// Temp until bloom is implemented. //ctx.cmd.SetComputeTextureParam(data.parameters.uberPostCS, data.parameters.uberPostKernel, HDShaderIDs._BloomTexture, TextureXR.GetBlackTexture()); //ctx.cmd.SetComputeTextureParam(data.parameters.uberPostCS, data.parameters.uberPostKernel, HDShaderIDs._BloomDirtTexture, Texture2D.blackTexture); //ctx.cmd.SetComputeVectorParam(data.parameters.uberPostCS, HDShaderIDs._BloomParams, Vector4.zero); - DoUberPostProcess( data.parameters, - ctx.resources.GetTexture(data.source), - ctx.resources.GetTexture(data.destination), - ctx.resources.GetTexture(data.logLut), - ctx.resources.GetTexture(data.source), // TODO: TMP VALUE, should be bloom texture and will be as soon as PP is ported to rendergraph. - ctx.cmd); - }); + DoUberPostProcess(data.parameters, + ctx.resources.GetTexture(data.source), + ctx.resources.GetTexture(data.destination), + ctx.resources.GetTexture(data.logLut), + ctx.resources.GetTexture(data.source), // TODO: TMP VALUE, should be bloom texture and will be as soon as PP is ported to rendergraph. + ctx.cmd); + }); - source = passData.destination; + source = passData.destination; + } + + m_HDInstance.PushFullScreenDebugTexture(renderGraph, source, FullScreenDebugMode.ColorLog); + + // if (camera.frameSettings.IsEnabled(FrameSettingsField.CustomPostProcess)) + // { + // using (new ProfilingScope(cmd, ProfilingSampler.Get(HDProfileId.CustomPostProcessAfterPP))) + // { + // foreach (var typeString in HDRenderPipeline.defaultAsset.afterPostProcessCustomPostProcesses) + // RenderCustomPostProcess(cmd, camera, ref source, colorBuffer, Type.GetType(typeString)); + // } + // } } - m_HDInstance.PushFullScreenDebugTexture(renderGraph, source, FullScreenDebugMode.ColorLog); + // if (dynResHandler.DynamicResolutionEnabled() && // Dynamic resolution is on. + // camera.antialiasing == AntialiasingMode.FastApproximateAntialiasing && + // m_AntialiasingFS) + // { + // using (new ProfilingScope(cmd, ProfilingSampler.Get(HDProfileId.FXAA))) + // { + // var destination = m_Pool.Get(Vector2.one, m_ColorFormat); + // DoFXAA(cmd, camera, source, destination); + // PoolSource(ref source, destination); + // } + // } - // if (camera.frameSettings.IsEnabled(FrameSettingsField.CustomPostProcess)) + // // Contrast Adaptive Sharpen Upscaling + // if (dynResHandler.DynamicResolutionEnabled() && + // dynResHandler.filter == DynamicResUpscaleFilter.ContrastAdaptiveSharpen) + // { + // using (new ProfilingScope(cmd, ProfilingSampler.Get(HDProfileId.ContrastAdaptiveSharpen))) // { - // using (new ProfilingScope(cmd, ProfilingSampler.Get(HDProfileId.CustomPostProcessAfterPP))) + // var destination = m_Pool.Get(Vector2.one, m_ColorFormat); + + // var cs = m_Resources.shaders.contrastAdaptiveSharpenCS; + // int kInit = cs.FindKernel("KInitialize"); + // int kMain = cs.FindKernel("KMain"); + // if (kInit >= 0 && kMain >= 0) // { - // foreach (var typeString in HDRenderPipeline.defaultAsset.afterPostProcessCustomPostProcesses) - // RenderCustomPostProcess(cmd, camera, ref source, colorBuffer, Type.GetType(typeString)); + // cmd.SetComputeFloatParam(cs, HDShaderIDs._Sharpness, 1); + // cmd.SetComputeTextureParam(cs, kMain, HDShaderIDs._InputTexture, source); + // cmd.SetComputeVectorParam(cs, HDShaderIDs._InputTextureDimensions, new Vector4(source.rt.width, source.rt.height)); + // cmd.SetComputeTextureParam(cs, kMain, HDShaderIDs._OutputTexture, destination); + // cmd.SetComputeVectorParam(cs, HDShaderIDs._OutputTextureDimensions, new Vector4(destination.rt.width, destination.rt.height)); + + // ValidateComputeBuffer(ref m_ContrastAdaptiveSharpen, 2, sizeof(uint) * 4); + + // cmd.SetComputeBufferParam(cs, kInit, "CasParameters", m_ContrastAdaptiveSharpen); + // cmd.SetComputeBufferParam(cs, kMain, "CasParameters", m_ContrastAdaptiveSharpen); + + // cmd.DispatchCompute(cs, kInit, 1, 1, 1); + + // int dispatchX = (int)System.Math.Ceiling(destination.rt.width / 16.0f); + // int dispatchY = (int)System.Math.Ceiling(destination.rt.height / 16.0f); + + // cmd.DispatchCompute(cs, kMain, dispatchX, dispatchY, camera.viewCount); // } + + // PoolSource(ref source, destination); // } - } + // } - // if (dynResHandler.DynamicResolutionEnabled() && // Dynamic resolution is on. - // camera.antialiasing == AntialiasingMode.FastApproximateAntialiasing && - // m_AntialiasingFS) - // { - // using (new ProfilingScope(cmd, ProfilingSampler.Get(HDProfileId.FXAA))) - // { - // var destination = m_Pool.Get(Vector2.one, m_ColorFormat); - // DoFXAA(cmd, camera, source, destination); - // PoolSource(ref source, destination); - // } - // } - - // // Contrast Adaptive Sharpen Upscaling - // if (dynResHandler.DynamicResolutionEnabled() && - // dynResHandler.filter == DynamicResUpscaleFilter.ContrastAdaptiveSharpen) - // { - // using (new ProfilingScope(cmd, ProfilingSampler.Get(HDProfileId.ContrastAdaptiveSharpen))) - // { - // var destination = m_Pool.Get(Vector2.one, m_ColorFormat); - - // var cs = m_Resources.shaders.contrastAdaptiveSharpenCS; - // int kInit = cs.FindKernel("KInitialize"); - // int kMain = cs.FindKernel("KMain"); - // if (kInit >= 0 && kMain >= 0) - // { - // cmd.SetComputeFloatParam(cs, HDShaderIDs._Sharpness, 1); - // cmd.SetComputeTextureParam(cs, kMain, HDShaderIDs._InputTexture, source); - // cmd.SetComputeVectorParam(cs, HDShaderIDs._InputTextureDimensions, new Vector4(source.rt.width, source.rt.height)); - // cmd.SetComputeTextureParam(cs, kMain, HDShaderIDs._OutputTexture, destination); - // cmd.SetComputeVectorParam(cs, HDShaderIDs._OutputTextureDimensions, new Vector4(destination.rt.width, destination.rt.height)); - - // ValidateComputeBuffer(ref m_ContrastAdaptiveSharpen, 2, sizeof(uint) * 4); - - // cmd.SetComputeBufferParam(cs, kInit, "CasParameters", m_ContrastAdaptiveSharpen); - // cmd.SetComputeBufferParam(cs, kMain, "CasParameters", m_ContrastAdaptiveSharpen); - - // cmd.DispatchCompute(cs, kInit, 1, 1, 1); - - // int dispatchX = (int)System.Math.Ceiling(destination.rt.width / 16.0f); - // int dispatchY = (int)System.Math.Ceiling(destination.rt.height / 16.0f); - - // cmd.DispatchCompute(cs, kMain, dispatchX, dispatchY, camera.viewCount); - // } - - // PoolSource(ref source, destination); - // } - // } - - using (var builder = renderGraph.AddRenderPass("Final Pass", out var passData, ProfilingSampler.Get(HDProfileId.FinalPost))) - { - passData.parameters = PrepareFinalPass(hdCamera, blueNoise, flipY); - passData.source = builder.ReadTexture(source); - passData.afterPostProcessTexture = builder.ReadTexture(afterPostProcessTexture); - passData.alphaTexture = builder.ReadTexture(alphaTexture); - passData.destination = builder.WriteTexture(finalRT); - - builder.SetRenderFunc( - (FinalPassData data, RenderGraphContext ctx) => + using (var builder = renderGraph.AddRenderPass("Final Pass", out var passData, ProfilingSampler.Get(HDProfileId.FinalPost))) { - DoFinalPass( data.parameters, - ctx.resources.GetTexture(data.source), - ctx.resources.GetTexture(data.afterPostProcessTexture), - ctx.resources.GetTexture(data.destination), - ctx.resources.GetTexture(data.alphaTexture), - ctx.cmd); - }); + passData.parameters = PrepareFinalPass(hdCamera, blueNoise, flipY); + passData.source = builder.ReadTexture(source); + passData.afterPostProcessTexture = builder.ReadTexture(afterPostProcessTexture); + passData.alphaTexture = builder.ReadTexture(alphaTexture); + passData.destination = builder.WriteTexture(finalRT); + + builder.SetRenderFunc( + (FinalPassData data, RenderGraphContext ctx) => + { + DoFinalPass(data.parameters, + ctx.resources.GetTexture(data.source), + ctx.resources.GetTexture(data.afterPostProcessTexture), + ctx.resources.GetTexture(data.destination), + ctx.resources.GetTexture(data.alphaTexture), + ctx.cmd); + }); + } + + hdCamera.resetPostProcessingHistory = false; } } diff --git a/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.cs b/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.cs index 4976f938fab..e1624f01fc8 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.cs @@ -523,7 +523,7 @@ void PoolSource(ref RTHandle src, RTHandle dst) { var exposureParameters = PrepareExposureParameters(camera); - GrabExposureRequiredTextures(camera, out var prevExposure, out var nextExposure, out var tmpRenderTarget1024, out var tmpRenderTarget32); + GrabExposureRequiredTextures(camera, out var prevExposure, out var nextExposure); using (new ProfilingScope(cmd, ProfilingSampler.Get(HDProfileId.DynamicExposure))) { @@ -533,7 +533,7 @@ void PoolSource(ref RTHandle src, RTHandle dst) } else { - DoDynamicExposure(exposureParameters, cmd, source, prevExposure, nextExposure, tmpRenderTarget1024, tmpRenderTarget32); + DoDynamicExposure(exposureParameters, cmd, source, prevExposure, nextExposure, m_TempTexture1024, m_TempTexture32); } // On reset history we need to apply dynamic exposure immediately to avoid @@ -542,19 +542,7 @@ void PoolSource(ref RTHandle src, RTHandle dst) if (camera.resetPostProcessingHistory) { var destination = m_Pool.Get(Vector2.one, m_ColorFormat); - - var cs = m_Resources.shaders.applyExposureCS; - int kernel = cs.FindKernel("KMain"); - - // Note: we call GetPrevious instead of GetCurrent because the textures - // are swapped internally as the system expects the texture will be used - // on the next frame. So the actual "current" for this frame is in - // "previous". - cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._ExposureTexture, GetPreviousExposureTexture(camera)); - cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._InputTexture, source); - cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._OutputTexture, destination); - cmd.DispatchCompute(cs, kernel, (camera.actualWidth + 7) / 8, (camera.actualHeight + 7) / 8, camera.viewCount); - + ApplyExposure(PrepareApplyExposureParameters(camera), cmd, source, destination, GetPreviousExposureTexture(camera)); PoolSource(ref source, destination); } } @@ -982,7 +970,46 @@ static void DoCopyAlpha(in DoCopyAlphaParameters parameters, RTHandle source, RT #region Exposure - struct ExposureParameter + struct ApplyExposureParameters + { + public ComputeShader applyExposureCS; + public int applyExposureKernel; + + public int width; + public int height; + public int viewCount; + } + + ApplyExposureParameters PrepareApplyExposureParameters(HDCamera camera) + { + ApplyExposureParameters parameters = new ApplyExposureParameters(); + parameters.applyExposureCS = m_Resources.shaders.applyExposureCS; + parameters.applyExposureKernel = parameters.applyExposureCS.FindKernel("KMain"); + + parameters.width = camera.actualWidth; + parameters.height = camera.actualHeight; + parameters.viewCount = camera.viewCount; + + return parameters; + } + + static void ApplyExposure(in ApplyExposureParameters parameters, CommandBuffer cmd, RTHandle source, RTHandle destination, RTHandle prevExposure) + { + var cs = parameters.applyExposureCS; + int kernel = parameters.applyExposureKernel; + + // Note: we use previous instead of current because the textures + // are swapped internally as the system expects the texture will be used + // on the next frame. So the actual "current" for this frame is in + // "previous". + cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._ExposureTexture, prevExposure); + cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._InputTexture, source); + cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._OutputTexture, destination); + cmd.DispatchCompute(cs, kernel, (parameters.width + 7) / 8, (parameters.height + 7) / 8, parameters.viewCount); + + } + + struct ExposureParameters { public ComputeShader exposureCS; public ComputeShader histogramExposureCS; @@ -1009,9 +1036,9 @@ struct ExposureParameter public Vector4 adaptationParams; } - ExposureParameter PrepareExposureParameters(HDCamera hdCamera) + ExposureParameters PrepareExposureParameters(HDCamera hdCamera) { - var parameters = new ExposureParameter(); + var parameters = new ExposureParameters(); parameters.exposureCS = m_Resources.shaders.exposureCS; parameters.histogramExposureCS = m_Resources.shaders.histogramExposureCS; parameters.histogramExposureCS.shaderKeywords = null; @@ -1202,7 +1229,7 @@ internal ComputeBuffer GetDebugImageHistogramBuffer() return m_DebugImageHistogramBuffer; } - void DoFixedExposure(in ExposureParameter parameters, CommandBuffer cmd, RTHandle prevExposure) + void DoFixedExposure(in ExposureParameters parameters, CommandBuffer cmd, RTHandle prevExposure) { var cs = parameters.exposureCS; int kernel = parameters.exposureReductionKernel; @@ -1284,7 +1311,7 @@ void PrepareExposureCurveData(out float min, out float max) m_ExposureCurveTexture.Apply(); } - void GrabExposureRequiredTextures(HDCamera camera, out RTHandle prevExposure, out RTHandle nextExposure, out RTHandle tmpRenderTarget1024, out RTHandle tmpRenderTarget32) + void GrabExposureRequiredTextures(HDCamera camera, out RTHandle prevExposure, out RTHandle nextExposure) { GrabExposureHistoryTextures(camera, out prevExposure, out nextExposure); if (camera.resetPostProcessingHistory) @@ -1293,18 +1320,16 @@ void GrabExposureRequiredTextures(HDCamera camera, out RTHandle prevExposure, ou // When we reset history we must setup neutral value prevExposure = m_EmptyExposureTexture; // Use neutral texture } - - tmpRenderTarget1024 = m_TempTexture1024; - tmpRenderTarget32 = m_TempTexture32; } - static void DoDynamicExposure(in ExposureParameter exposureParameters, CommandBuffer cmd, RTHandle colorBuffer, RTHandle prevExposure, RTHandle nextExposure, RTHandle tmpRenderTarget1024, RTHandle tmpRenderTarget32) + static void DoDynamicExposure(in ExposureParameters exposureParameters, CommandBuffer cmd, RTHandle colorBuffer, RTHandle prevExposure, RTHandle nextExposure, RTHandle tmpRenderTarget1024, RTHandle tmpRenderTarget32) { var cs = exposureParameters.exposureCS; int kernel; var sourceTex = colorBuffer; + // NOTE: Breakpoint here. kernel = exposureParameters.exposurePreparationKernel; cmd.SetComputeIntParams(cs, HDShaderIDs._Variants, exposureParameters.exposureVariants); cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._PreviousExposureTexture, prevExposure); @@ -1347,7 +1372,7 @@ static void DoDynamicExposure(in ExposureParameter exposureParameters, CommandBu cmd.DispatchCompute(cs, kernel, 1, 1, 1); } - static void DoHistogramBasedExposure(in ExposureParameter exposureParameters, CommandBuffer cmd, RTHandle sourceTexture, RTHandle prevExposure, RTHandle nextExposure, RTHandle debugData) + static void DoHistogramBasedExposure(in ExposureParameters exposureParameters, CommandBuffer cmd, RTHandle sourceTexture, RTHandle prevExposure, RTHandle nextExposure, RTHandle debugData) { var cs = exposureParameters.histogramExposureCS; int kernel; diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDProfileId.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDProfileId.cs index 0371848c1f4..ae885cb4cef 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDProfileId.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDProfileId.cs @@ -161,6 +161,7 @@ internal enum HDProfileId StopNaNs, FixedExposure, DynamicExposure, + ApplyExposure, TemporalAntialiasing, DepthOfField, DepthOfFieldKernel, diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.PostProcess.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.PostProcess.cs index 2c6aac34fb9..e49524646d4 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.PostProcess.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.PostProcess.cs @@ -17,6 +17,7 @@ class AfterPostProcessPassData TextureHandle RenderPostProcess( RenderGraph renderGraph, TextureHandle inputColor, TextureHandle depthBuffer, + TextureHandle depthBufferMipChain, TextureHandle backBuffer, CullingResults cullResults, HDCamera hdCamera) @@ -61,6 +62,7 @@ TextureHandle RenderPostProcess( RenderGraph renderGraph, inputColor, afterPostProcessBuffer, depthBuffer, + depthBufferMipChain, dest, parameters.flipYInPostProcess ); diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.RenderGraph.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.RenderGraph.cs index a3c5b74b9f4..9e9fa59f3d8 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.RenderGraph.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.RenderGraph.cs @@ -196,7 +196,7 @@ void ExecuteWithRenderGraph( RenderRequest renderRequest, aovRequest.PushCameraTexture(m_RenderGraph, AOVBuffers.Color, hdCamera, colorBuffer, aovBuffers); - TextureHandle postProcessDest = RenderPostProcess(m_RenderGraph, colorBuffer, prepassOutput.depthBuffer, backBuffer, cullingResults, hdCamera); + TextureHandle postProcessDest = RenderPostProcess(m_RenderGraph, colorBuffer, prepassOutput.depthBuffer, prepassOutput.depthPyramidTexture, backBuffer, cullingResults, hdCamera); // TODO RENDERGRAPH // RenderCustomPass(renderContext, cmd, hdCamera, customPassCullingResults, CustomPassInjectionPoint.AfterPostProcess); From 4da67e4417255c9338cfb1d0ca6796e4996c5ef3 Mon Sep 17 00:00:00 2001 From: FrancescoC-Unity Date: Mon, 29 Jun 2020 16:48:21 +0200 Subject: [PATCH 04/11] Port most of remaining passes to RG --- .../PostProcessSystem.RenderGraph.cs | 461 ++++++++++-------- .../PostProcessing/PostProcessSystem.cs | 4 +- 2 files changed, 265 insertions(+), 200 deletions(-) diff --git a/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.RenderGraph.cs b/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.RenderGraph.cs index 7f560c4bc82..f648acd12a2 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.RenderGraph.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.RenderGraph.cs @@ -18,6 +18,7 @@ class UberPostPassData public TextureHandle source; public TextureHandle destination; public TextureHandle logLut; + public TextureHandle bloomTexture; } class AlphaCopyPassData @@ -82,6 +83,69 @@ class SMAAData public TextureHandle smaaBlendTex; } + class FXAAData + { + public FXAAParameters parameters; + public TextureHandle source; + public TextureHandle destination; + } + + class MotionBlurData + { + public MotionBlurParameters parameters; + public TextureHandle source; + public TextureHandle destination; + public TextureHandle preppedMotionVec; + public TextureHandle minMaxTileVel; + public TextureHandle maxTileNeigbourhood; + public TextureHandle tileToScatterMax; + public TextureHandle tileToScatterMin; + } + + class PaniniProjectionData + { + public PaniniProjectionParameters parameters; + public TextureHandle source; + public TextureHandle destination; + } + + class BloomData + { + public BloomParameters parameters; + public TextureHandle source; + public TextureHandle[] mipsDown = new TextureHandle[k_MaxBloomMipCount + 1]; + public TextureHandle[] mipsUp = new TextureHandle[k_MaxBloomMipCount + 1]; + } + + void FillBloomMipsTextureHandles(ref BloomData bloomData, RenderGraph renderGraph, RenderGraphBuilder builder) + { + for (int i = 0; i < m_BloomMipCount; i++) + { + var scale = new Vector2(m_BloomMipsInfo[i].z, m_BloomMipsInfo[i].w); + var pixelSize = new Vector2Int((int)m_BloomMipsInfo[i].x, (int)m_BloomMipsInfo[i].y); + + bloomData.mipsDown[i] = builder.CreateTransientTexture(new TextureDesc(scale, true, true) + { colorFormat = m_ColorFormat, enableRandomWrite = true }); + + if (i != 0) + { + bloomData.mipsUp[i] = builder.CreateTransientTexture(new TextureDesc(scale, true, true) + { colorFormat = m_ColorFormat, enableRandomWrite = true }); + + } + } + + // the mip up 0 will be used by uber, so not allocated as transient. + var mip0Scale = new Vector2(m_BloomMipsInfo[0].z, m_BloomMipsInfo[0].w); + bloomData.mipsUp[0] = renderGraph.CreateTexture(new TextureDesc(mip0Scale, true, true) + { + name = "Bloom final mip up", + colorFormat = m_ColorFormat, + useMipMap = false, + enableRandomWrite = true + }); + } + public void Render(RenderGraph renderGraph, HDCamera hdCamera, BlueNoise blueNoise, @@ -314,9 +378,8 @@ public void Render(RenderGraph renderGraph, } else if (hdCamera.antialiasing == HDAdditionalCameraData.AntialiasingMode.SubpixelMorphologicalAntiAliasing) { - using (var builder = renderGraph.AddRenderPass("Temporal Anti-Aliasing", out var passData, ProfilingSampler.Get(HDProfileId.TemporalAntialiasing))) + using (var builder = renderGraph.AddRenderPass("Temporal Anti-Aliasing", out var passData, ProfilingSampler.Get(HDProfileId.SMAA))) { - passData.source = builder.ReadTexture(source); passData.parameters = PrepareSMAAParameters(hdCamera); builder.ReadTexture(depthBuffer); @@ -346,251 +409,253 @@ public void Render(RenderGraph renderGraph, source = passData.destination; - - // DoSMAA(PrepareSMAAParameters(camera), cmd, source, smaaEdgeTex, smaaBlendTex, destination, depthBuffer); - } - //{ - // using (new ProfilingScope(cmd, ProfilingSampler.Get(HDProfileId.SMAA))) - // { - // var destination = m_Pool.Get(Vector2.one, m_ColorFormat); - // RTHandle smaaEdgeTex, smaaBlendTex; - // AllocateSMAARenderTargets(camera, out smaaEdgeTex, out smaaBlendTex); - // DoSMAA(PrepareSMAAParameters(camera), cmd, source, smaaEdgeTex, smaaBlendTex, destination, depthBuffer); - // RecycleSMAARenderTargets(smaaEdgeTex, smaaBlendTex); - // PoolSource(ref source, destination); - // } - //} } } - // TODO RENDERGRAPH: Implement - - // // Dynamic exposure - will be applied in the next frame - // // Not considered as a post-process so it's not affected by its enabled state - // if (!IsExposureFixed() && m_ExposureControlFS) - // { - // using (new ProfilingScope(cmd, ProfilingSampler.Get(HDProfileId.DynamicExposure))) + // if (camera.frameSettings.IsEnabled(FrameSettingsField.CustomPostProcess)) // { - // if (m_Exposure.mode.value == ExposureMode.AutomaticHistogram) - // { - // DoHistogramBasedExposure(cmd, camera, source); - // } - // else + // using (new ProfilingScope(cmd, ProfilingSampler.Get(HDProfileId.CustomPostProcessBeforePP))) // { - // DoDynamicExposure(cmd, camera, source); + // foreach (var typeString in HDRenderPipeline.defaultAsset.beforePostProcessCustomPostProcesses) + // RenderCustomPostProcess(cmd, camera, ref source, colorBuffer, Type.GetType(typeString)); // } + // } + + // // If Path tracing is enabled, then DoF is computed in the path tracer by sampling the lens aperure (when using the physical camera mode) + // bool isDoFPathTraced = (camera.frameSettings.IsEnabled(FrameSettingsField.RayTracing) && + // camera.volumeStack.GetComponent().enable.value && + // camera.camera.cameraType != CameraType.Preview && + // m_DepthOfField.focusMode == DepthOfFieldMode.UsePhysicalCamera); - // // On reset history we need to apply dynamic exposure immediately to avoid - // // white or black screen flashes when the current exposure isn't anywhere - // // near 0 - // if (camera.resetPostProcessingHistory) + // // Depth of Field is done right after TAA as it's easier to just re-project the CoC + // // map rather than having to deal with all the implications of doing it before TAA + // if (m_DepthOfField.IsActive() && !isSceneView && m_DepthOfFieldFS && !isDoFPathTraced) + // { + // using (new ProfilingScope(cmd, ProfilingSampler.Get(HDProfileId.DepthOfField))) // { // var destination = m_Pool.Get(Vector2.one, m_ColorFormat); - - // var cs = m_Resources.shaders.applyExposureCS; - // int kernel = cs.FindKernel("KMain"); - - // // Note: we call GetPrevious instead of GetCurrent because the textures - // // are swapped internally as the system expects the texture will be used - // // on the next frame. So the actual "current" for this frame is in - // // "previous". - // cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._ExposureTexture, GetPreviousExposureTexture(camera)); - // cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._InputTexture, source); - // cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._OutputTexture, destination); - // cmd.DispatchCompute(cs, kernel, (camera.actualWidth + 7) / 8, (camera.actualHeight + 7) / 8, camera.viewCount); - + // DoDepthOfField(cmd, camera, source, destination, taaEnabled); // PoolSource(ref source, destination); // } // } - // } - if (m_PostProcessEnabled) + // Motion blur after depth of field for aesthetic reasons (better to see motion + // blurred bokeh rather than out of focus motion blur) + if (m_MotionBlur.IsActive() && m_AnimatedMaterialsEnabled && !hdCamera.resetPostProcessingHistory && m_MotionBlurFS) { - // // Temporal anti-aliasing goes first - // bool taaEnabled = false; - - // if (m_AntialiasingFS) - // { - // taaEnabled = camera.antialiasing == AntialiasingMode.TemporalAntialiasing; - - // if (taaEnabled) - // { - // using (new ProfilingScope(cmd, ProfilingSampler.Get(HDProfileId.TemporalAntialiasing))) - // { - // var destination = m_Pool.Get(Vector2.one, m_ColorFormat); - // DoTemporalAntialiasing(cmd, camera, source, destination, depthBuffer, depthMipChain); - // PoolSource(ref source, destination); - // } - // } - // else if (camera.antialiasing == AntialiasingMode.SubpixelMorphologicalAntiAliasing) - // { - // using (new ProfilingScope(cmd, ProfilingSampler.Get(HDProfileId.SMAA))) - // { - // var destination = m_Pool.Get(Vector2.one, m_ColorFormat); - // DoSMAA(cmd, camera, source, destination, depthBuffer); - // PoolSource(ref source, destination); - // } - // } - // } - - // if (camera.frameSettings.IsEnabled(FrameSettingsField.CustomPostProcess)) - // { - // using (new ProfilingScope(cmd, ProfilingSampler.Get(HDProfileId.CustomPostProcessBeforePP))) - // { - // foreach (var typeString in HDRenderPipeline.defaultAsset.beforePostProcessCustomPostProcesses) - // RenderCustomPostProcess(cmd, camera, ref source, colorBuffer, Type.GetType(typeString)); - // } - // } - - // // If Path tracing is enabled, then DoF is computed in the path tracer by sampling the lens aperure (when using the physical camera mode) - // bool isDoFPathTraced = (camera.frameSettings.IsEnabled(FrameSettingsField.RayTracing) && - // camera.volumeStack.GetComponent().enable.value && - // camera.camera.cameraType != CameraType.Preview && - // m_DepthOfField.focusMode == DepthOfFieldMode.UsePhysicalCamera); - - // // Depth of Field is done right after TAA as it's easier to just re-project the CoC - // // map rather than having to deal with all the implications of doing it before TAA - // if (m_DepthOfField.IsActive() && !isSceneView && m_DepthOfFieldFS && !isDoFPathTraced) - // { - // using (new ProfilingScope(cmd, ProfilingSampler.Get(HDProfileId.DepthOfField))) - // { - // var destination = m_Pool.Get(Vector2.one, m_ColorFormat); - // DoDepthOfField(cmd, camera, source, destination, taaEnabled); - // PoolSource(ref source, destination); - // } - // } - - // // Motion blur after depth of field for aesthetic reasons (better to see motion - // // blurred bokeh rather than out of focus motion blur) - // if (m_MotionBlur.IsActive() && m_AnimatedMaterialsEnabled && !camera.resetPostProcessingHistory && m_MotionBlurFS) - // { - // using (new ProfilingScope(cmd, ProfilingSampler.Get(HDProfileId.MotionBlur))) - // { - // var destination = m_Pool.Get(Vector2.one, m_ColorFormat); - // DoMotionBlur(cmd, camera, source, destination); - // PoolSource(ref source, destination); - // } - // } - - // // Panini projection is done as a fullscreen pass after all depth-based effects are - // // done and before bloom kicks in - // // This is one effect that would benefit from an overscan mode or supersampling in - // // HDRP to reduce the amount of resolution lost at the center of the screen - // if (m_PaniniProjection.IsActive() && !isSceneView && m_PaniniProjectionFS) - // { - // using (new ProfilingScope(cmd, ProfilingSampler.Get(HDProfileId.PaniniProjection))) - // { - // var destination = m_Pool.Get(Vector2.one, m_ColorFormat); - // DoPaniniProjection(cmd, camera, source, destination); - // PoolSource(ref source, destination); - // } - // } - - // Uber post-process - //// Generate the bloom texture - //bool bloomActive = m_Bloom.IsActive() && m_BloomFS; - - //if (bloomActive) - //{ - // using (new ProfilingScope(cmd, ProfilingSampler.Get(HDProfileId.Bloom))) - // { - // DoBloom(cmd, camera, source, uberPostParams.uberPostCS, uberPostParams.uberPostKernel); - // } - //} - //else - //{ - // cmd.SetComputeTextureParam(uberPostParams.uberPostCS, uberPostParams.uberPostKernel, HDShaderIDs._BloomTexture, TextureXR.GetBlackTexture()); - // cmd.SetComputeTextureParam(uberPostParams.uberPostCS, uberPostParams.uberPostKernel, HDShaderIDs._BloomDirtTexture, Texture2D.blackTexture); - // cmd.SetComputeVectorParam(uberPostParams.uberPostCS, HDShaderIDs._BloomParams, Vector4.zero); - //} - - TextureHandle logLutOutput; - using (var builder = renderGraph.AddRenderPass("Color Grading", out var passData, ProfilingSampler.Get(HDProfileId.ColorGradingLUTBuilder))) + using (var builder = renderGraph.AddRenderPass("Motion Blur", out var passData, ProfilingSampler.Get(HDProfileId.MotionBlur))) { - TextureHandle logLut = renderGraph.CreateTexture(new TextureDesc(m_LutSize, m_LutSize) + passData.source = builder.ReadTexture(source); + passData.parameters = PrepareMotionBlurParameters(hdCamera); + + Vector2 tileTexScale = new Vector2((float)passData.parameters.tileTargetSize.x / hdCamera.actualWidth, (float)passData.parameters.tileTargetSize.y / hdCamera.actualHeight); + + passData.preppedMotionVec = builder.CreateTransientTexture(new TextureDesc(Vector2.one, true, true) + { colorFormat = GraphicsFormat.B10G11R11_UFloatPack32, enableRandomWrite = true, name = "Prepped Motion Vectors" }); + + passData.minMaxTileVel = builder.CreateTransientTexture(new TextureDesc(tileTexScale, true, true) + { colorFormat = GraphicsFormat.B10G11R11_UFloatPack32, enableRandomWrite = true, name = "MinMax Tile Motion Vectors" }); + + passData.maxTileNeigbourhood = builder.CreateTransientTexture(new TextureDesc(tileTexScale, true, true) + { colorFormat = GraphicsFormat.B10G11R11_UFloatPack32, enableRandomWrite = true, name = "Max Neighbourhood Tile" }); + + passData.tileToScatterMax = TextureHandle.nullHandle; + passData.tileToScatterMin = TextureHandle.nullHandle; + + if (passData.parameters.motionblurSupportScattering) + { + passData.tileToScatterMax = builder.CreateTransientTexture(new TextureDesc(tileTexScale, true, true) + { colorFormat = GraphicsFormat.R32_UInt, enableRandomWrite = true, name = "Tile to Scatter Max" }); + + passData.tileToScatterMin = builder.CreateTransientTexture(new TextureDesc(tileTexScale, true, true) + { colorFormat = GraphicsFormat.R16_SFloat, enableRandomWrite = true, name = "Tile to Scatter Min" }); + } + + TextureHandle dest = renderGraph.CreateTexture(new TextureDesc(Vector2.one, true, true) { - name = "Color Grading Log Lut", - dimension = TextureDimension.Tex3D, - slices = m_LutSize, - depthBufferBits = DepthBits.None, - colorFormat = m_LutFormat, - filterMode = FilterMode.Bilinear, - wrapMode = TextureWrapMode.Clamp, - anisoLevel = 0, + name = "Motion Blur Destination", + colorFormat = m_ColorFormat, useMipMap = false, enableRandomWrite = true }); - - passData.parameters = PrepareColorGradingParameters(); - passData.logLut = builder.WriteTexture(logLut); - logLutOutput = passData.logLut; + passData.destination = builder.WriteTexture(dest); ; builder.SetRenderFunc( - (ColorGradingPassData data, RenderGraphContext ctx) => + (MotionBlurData data, RenderGraphContext ctx) => { - DoColorGrading(data.parameters, ctx.resources.GetTexture(data.logLut), ctx.cmd); + DoMotionBlur(data.parameters, ctx.cmd, ctx.resources.GetTexture(data.source), + ctx.resources.GetTexture(data.destination), + ctx.resources.GetTexture(data.preppedMotionVec), + ctx.resources.GetTexture(data.minMaxTileVel), + ctx.resources.GetTexture(data.maxTileNeigbourhood), + ctx.resources.GetTexture(data.tileToScatterMax), + ctx.resources.GetTexture(data.tileToScatterMin)); }); + + source = passData.destination; + } + } - using (var builder = renderGraph.AddRenderPass("Uber Post", out var passData, ProfilingSampler.Get(HDProfileId.UberPost))) + // Panini projection is done as a fullscreen pass after all depth-based effects are + // done and before bloom kicks in + // This is one effect that would benefit from an overscan mode or supersampling in + // HDRP to reduce the amount of resolution lost at the center of the screen + if (m_PaniniProjection.IsActive() && !isSceneView && m_PaniniProjectionFS) + { + using (var builder = renderGraph.AddRenderPass("Panini Projection", out var passData, ProfilingSampler.Get(HDProfileId.PaniniProjection))) { + passData.source = builder.ReadTexture(source); + passData.parameters = PreparePaniniProjectionParameters(hdCamera); TextureHandle dest = renderGraph.CreateTexture(new TextureDesc(Vector2.one, true, true) { - name = "Uber Post Destination", + name = "Panini Projection Destination", colorFormat = m_ColorFormat, useMipMap = false, enableRandomWrite = true }); + passData.destination = builder.WriteTexture(dest); + + builder.SetRenderFunc( + (PaniniProjectionData data, RenderGraphContext ctx) => + { + DoPaniniProjection(data.parameters, ctx.cmd, ctx.resources.GetTexture(data.source), ctx.resources.GetTexture(data.destination)); + }); + + source = passData.destination; + } + } - passData.parameters = PrepareUberPostParameters(hdCamera, isSceneView); + bool bloomActive = m_Bloom.IsActive() && m_BloomFS; + TextureHandle bloomTexture = renderGraph.defaultResources.blackTextureXR; + if (bloomActive) + { + ComputeBloomMipSizesAndScales(hdCamera); + using (var builder = renderGraph.AddRenderPass("Bloom", out var passData, ProfilingSampler.Get(HDProfileId.Bloom))) + { passData.source = builder.ReadTexture(source); - passData.logLut = builder.ReadTexture(logLutOutput); - passData.destination = builder.WriteTexture(dest); + passData.parameters = PrepareBloomParameters(hdCamera); + FillBloomMipsTextureHandles(ref passData, renderGraph, builder); + passData.mipsUp[0] = builder.WriteTexture(passData.mipsUp[0]); + builder.SetRenderFunc( - (UberPostPassData data, RenderGraphContext ctx) => + (BloomData data, RenderGraphContext ctx) => { - //// Temp until bloom is implemented. - //ctx.cmd.SetComputeTextureParam(data.parameters.uberPostCS, data.parameters.uberPostKernel, HDShaderIDs._BloomTexture, TextureXR.GetBlackTexture()); - //ctx.cmd.SetComputeTextureParam(data.parameters.uberPostCS, data.parameters.uberPostKernel, HDShaderIDs._BloomDirtTexture, Texture2D.blackTexture); - //ctx.cmd.SetComputeVectorParam(data.parameters.uberPostCS, HDShaderIDs._BloomParams, Vector4.zero); + var bloomMipDown = ctx.renderGraphPool.GetTempArray(data.parameters.bloomMipCount); + var bloomMipUp = ctx.renderGraphPool.GetTempArray(data.parameters.bloomMipCount); + for(int i=0; i("Color Grading", out var passData, ProfilingSampler.Get(HDProfileId.ColorGradingLUTBuilder))) + { + TextureHandle logLut = renderGraph.CreateTexture(new TextureDesc(m_LutSize, m_LutSize) + { + name = "Color Grading Log Lut", + dimension = TextureDimension.Tex3D, + slices = m_LutSize, + depthBufferBits = DepthBits.None, + colorFormat = m_LutFormat, + filterMode = FilterMode.Bilinear, + wrapMode = TextureWrapMode.Clamp, + anisoLevel = 0, + useMipMap = false, + enableRandomWrite = true + }); + + passData.parameters = PrepareColorGradingParameters(); + passData.logLut = builder.WriteTexture(logLut); + logLutOutput = passData.logLut; - // if (camera.frameSettings.IsEnabled(FrameSettingsField.CustomPostProcess)) - // { - // using (new ProfilingScope(cmd, ProfilingSampler.Get(HDProfileId.CustomPostProcessAfterPP))) - // { - // foreach (var typeString in HDRenderPipeline.defaultAsset.afterPostProcessCustomPostProcesses) - // RenderCustomPostProcess(cmd, camera, ref source, colorBuffer, Type.GetType(typeString)); - // } - // } + builder.SetRenderFunc( + (ColorGradingPassData data, RenderGraphContext ctx) => + { + DoColorGrading(data.parameters, ctx.resources.GetTexture(data.logLut), ctx.cmd); + }); } - // if (dynResHandler.DynamicResolutionEnabled() && // Dynamic resolution is on. - // camera.antialiasing == AntialiasingMode.FastApproximateAntialiasing && - // m_AntialiasingFS) - // { - // using (new ProfilingScope(cmd, ProfilingSampler.Get(HDProfileId.FXAA))) + using (var builder = renderGraph.AddRenderPass("Uber Post", out var passData, ProfilingSampler.Get(HDProfileId.UberPost))) + { + TextureHandle dest = renderGraph.CreateTexture(new TextureDesc(Vector2.one, true, true) + { + name = "Uber Post Destination", + colorFormat = m_ColorFormat, + useMipMap = false, + enableRandomWrite = true + }); + + passData.parameters = PrepareUberPostParameters(hdCamera, isSceneView); + passData.source = builder.ReadTexture(source); + passData.bloomTexture = builder.ReadTexture(bloomTexture); + passData.logLut = builder.ReadTexture(logLutOutput); + passData.destination = builder.WriteTexture(dest); + + + builder.SetRenderFunc( + (UberPostPassData data, RenderGraphContext ctx) => + { + DoUberPostProcess(data.parameters, + ctx.resources.GetTexture(data.source), + ctx.resources.GetTexture(data.destination), + ctx.resources.GetTexture(data.logLut), + ctx.resources.GetTexture(data.bloomTexture), // TODO: TMP VALUE, should be bloom texture and will be as soon as PP is ported to rendergraph. + ctx.cmd); + }); + + source = passData.destination; + } + + m_HDInstance.PushFullScreenDebugTexture(renderGraph, source, FullScreenDebugMode.ColorLog); + + // if (camera.frameSettings.IsEnabled(FrameSettingsField.CustomPostProcess)) // { - // var destination = m_Pool.Get(Vector2.one, m_ColorFormat); - // DoFXAA(cmd, camera, source, destination); - // PoolSource(ref source, destination); + // using (new ProfilingScope(cmd, ProfilingSampler.Get(HDProfileId.CustomPostProcessAfterPP))) + // { + // foreach (var typeString in HDRenderPipeline.defaultAsset.afterPostProcessCustomPostProcesses) + // RenderCustomPostProcess(cmd, camera, ref source, colorBuffer, Type.GetType(typeString)); + // } // } - // } + + + if (dynResHandler.DynamicResolutionEnabled() && // Dynamic resolution is on. + hdCamera.antialiasing == HDAdditionalCameraData.AntialiasingMode.FastApproximateAntialiasing && + m_AntialiasingFS) + { + using (var builder = renderGraph.AddRenderPass("FXAA", out var passData, ProfilingSampler.Get(HDProfileId.FXAA))) + { + passData.source = builder.ReadTexture(source); + passData.parameters = PrepareFXAAParameters(hdCamera); + TextureHandle dest = renderGraph.CreateTexture(new TextureDesc(Vector2.one, true, true) + { + name = "FXAA Destination", + colorFormat = m_ColorFormat, + useMipMap = false, + enableRandomWrite = true + }); + passData.destination = builder.WriteTexture(dest); ; + + builder.SetRenderFunc( + (FXAAData data, RenderGraphContext ctx) => + { + DoFXAA(data.parameters, ctx.cmd, ctx.resources.GetTexture(data.source), ctx.resources.GetTexture(data.destination)); + }); + + source = passData.destination; + } + } // // Contrast Adaptive Sharpen Upscaling // if (dynResHandler.DynamicResolutionEnabled() && diff --git a/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.cs b/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.cs index 59af7d47fab..e7f6d7ea603 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.cs @@ -667,7 +667,7 @@ void PoolSource(ref RTHandle src, RTHandle dst) { ComputeBloomMipSizesAndScales(camera); AllocateBloomMipTextures(); - DoBloom(PrepareBloomParameters(camera), cmd, source, m_BloomMipsDown, m_BloomMipsUp, uberPostParams.uberPostCS, uberPostParams.uberPostKernel); + DoBloom(PrepareBloomParameters(camera), cmd, source, m_BloomMipsDown, m_BloomMipsUp); RecycleUnusedBloomMips(); } } @@ -2893,7 +2893,7 @@ void RecycleUnusedBloomMips() } } - static void DoBloom(in BloomParameters bloomParameters, CommandBuffer cmd, RTHandle source, RTHandle[] bloomMipsDown, RTHandle[] bloomMipsUp, ComputeShader uberCS, int uberKernel) + static void DoBloom(in BloomParameters bloomParameters, CommandBuffer cmd, RTHandle source, RTHandle[] bloomMipsDown, RTHandle[] bloomMipsUp) { // All the computes for this effect use the same group size so let's use a local // function to simplify dispatches From 2ca49b5fad4af5ad01971a15080f3e19af5518fa Mon Sep 17 00:00:00 2001 From: FrancescoC-Unity Date: Mon, 29 Jun 2020 17:09:03 +0200 Subject: [PATCH 05/11] Port CAS to Rendergraph ready stage. --- .../PostProcessing/PostProcessSystem.cs | 81 +++++++++++++------ 1 file changed, 56 insertions(+), 25 deletions(-) diff --git a/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.cs b/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.cs index e7f6d7ea603..91eed628d5a 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.cs @@ -726,31 +726,7 @@ void PoolSource(ref RTHandle src, RTHandle dst) using (new ProfilingScope(cmd, ProfilingSampler.Get(HDProfileId.ContrastAdaptiveSharpen))) { var destination = m_Pool.Get(Vector2.one, m_ColorFormat); - - var cs = m_Resources.shaders.contrastAdaptiveSharpenCS; - int kInit = cs.FindKernel("KInitialize"); - int kMain = cs.FindKernel("KMain"); - if (kInit >= 0 && kMain >= 0) - { - cmd.SetComputeFloatParam(cs, HDShaderIDs._Sharpness, 1); - cmd.SetComputeTextureParam(cs, kMain, HDShaderIDs._InputTexture, source); - cmd.SetComputeVectorParam(cs, HDShaderIDs._InputTextureDimensions, new Vector4(source.rt.width, source.rt.height)); - cmd.SetComputeTextureParam(cs, kMain, HDShaderIDs._OutputTexture, destination); - cmd.SetComputeVectorParam(cs, HDShaderIDs._OutputTextureDimensions, new Vector4(destination.rt.width, destination.rt.height)); - - ValidateComputeBuffer(ref m_ContrastAdaptiveSharpen, 2, sizeof(uint) * 4); - - cmd.SetComputeBufferParam(cs, kInit, "CasParameters", m_ContrastAdaptiveSharpen); - cmd.SetComputeBufferParam(cs, kMain, "CasParameters", m_ContrastAdaptiveSharpen); - - cmd.DispatchCompute(cs, kInit, 1, 1, 1); - - int dispatchX = (int)System.Math.Ceiling(destination.rt.width / 16.0f); - int dispatchY = (int)System.Math.Ceiling(destination.rt.height / 16.0f); - - cmd.DispatchCompute(cs, kMain, dispatchX, dispatchY, camera.viewCount); - } - + DoContrastAdaptiveSharpening(PrepareContrastAdaptiveSharpeningParameters(camera), cmd, source, destination); PoolSource(ref source, destination); } } @@ -3487,6 +3463,61 @@ static void DoSMAA(in SMAAParameters parameters, CommandBuffer cmd, RTHandle sou #endregion + #region CAS + struct CASParameters + { + public ComputeShader casCS; + public int initKernel; + public int mainKernel; + + public ComputeBuffer casParametersBuffer; + + public int viewCount; + } + + CASParameters PrepareContrastAdaptiveSharpeningParameters(HDCamera camera) + { + CASParameters parameters = new CASParameters(); + + parameters.casCS = m_Resources.shaders.contrastAdaptiveSharpenCS; + parameters.initKernel = parameters.casCS.FindKernel("KInitialize"); + parameters.mainKernel = parameters.casCS.FindKernel("KMain"); + + ValidateComputeBuffer(ref m_ContrastAdaptiveSharpen, 2, sizeof(uint) * 4); + parameters.casParametersBuffer = m_ContrastAdaptiveSharpen; + + parameters.viewCount = camera.viewCount; + + return parameters; + } + + static void DoContrastAdaptiveSharpening(in CASParameters parameters, CommandBuffer cmd, RTHandle source, RTHandle destination) + { + var cs = parameters.casCS; + int kInit = parameters.initKernel; + int kMain = parameters.mainKernel; + if (kInit >= 0 && kMain >= 0) + { + cmd.SetComputeFloatParam(cs, HDShaderIDs._Sharpness, 1); + cmd.SetComputeTextureParam(cs, kMain, HDShaderIDs._InputTexture, source); + cmd.SetComputeVectorParam(cs, HDShaderIDs._InputTextureDimensions, new Vector4(source.rt.width, source.rt.height)); + cmd.SetComputeTextureParam(cs, kMain, HDShaderIDs._OutputTexture, destination); + cmd.SetComputeVectorParam(cs, HDShaderIDs._OutputTextureDimensions, new Vector4(destination.rt.width, destination.rt.height)); + + cmd.SetComputeBufferParam(cs, kInit, "CasParameters", parameters.casParametersBuffer); + cmd.SetComputeBufferParam(cs, kMain, "CasParameters", parameters.casParametersBuffer); + + cmd.DispatchCompute(cs, kInit, 1, 1, 1); + + int dispatchX = (int)System.Math.Ceiling(destination.rt.width / 16.0f); + int dispatchY = (int)System.Math.Ceiling(destination.rt.height / 16.0f); + + cmd.DispatchCompute(cs, kMain, dispatchX, dispatchY, parameters.viewCount); + } + + } + #endregion + #region Final Pass struct FinalPassParameters From 60a54c97e7d2e893dbb3c791becdee180ac9ca04 Mon Sep 17 00:00:00 2001 From: FrancescoC-Unity Date: Mon, 29 Jun 2020 18:24:00 +0200 Subject: [PATCH 06/11] Enable more tests --- .../Scenes/4x_PostProcessing/4011_MotionBlur_PerObject.unity | 5 ++++- .../Scenes/4x_PostProcessing/4022_PaniniProjection.unity | 2 +- .../GraphicTests/Scenes/4x_PostProcessing/4051_SMAA.unity | 5 ++++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/TestProjects/HDRP_Tests/Assets/GraphicTests/Scenes/4x_PostProcessing/4011_MotionBlur_PerObject.unity b/TestProjects/HDRP_Tests/Assets/GraphicTests/Scenes/4x_PostProcessing/4011_MotionBlur_PerObject.unity index 7871e0c33d1..495d079d160 100644 --- a/TestProjects/HDRP_Tests/Assets/GraphicTests/Scenes/4x_PostProcessing/4011_MotionBlur_PerObject.unity +++ b/TestProjects/HDRP_Tests/Assets/GraphicTests/Scenes/4x_PostProcessing/4011_MotionBlur_PerObject.unity @@ -527,7 +527,7 @@ PrefabInstance: - target: {fileID: 114995348509370400, guid: c07ace9ab142ca9469fa377877c2f1e7, type: 3} propertyPath: renderGraphCompatible - value: 0 + value: 1 objectReference: {fileID: 0} m_RemovedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: c07ace9ab142ca9469fa377877c2f1e7, type: 3} @@ -930,6 +930,7 @@ MonoBehaviour: m_EnableSpotReflector: 0 m_LuxAtDistance: 1 m_InnerSpotPercent: 0 + m_SpotIESCutoffPercent: 100 m_LightDimmer: 1 m_VolumetricDimmer: 1 m_LightUnit: 2 @@ -948,6 +949,8 @@ MonoBehaviour: m_ApplyRangeAttenuation: 1 m_DisplayAreaLightEmissiveMesh: 0 m_AreaLightCookie: {fileID: 0} + m_IESPoint: {fileID: 0} + m_IESSpot: {fileID: 0} m_AreaLightShadowCone: 120 m_UseScreenSpaceShadows: 0 m_InteractsWithSky: 1 diff --git a/TestProjects/HDRP_Tests/Assets/GraphicTests/Scenes/4x_PostProcessing/4022_PaniniProjection.unity b/TestProjects/HDRP_Tests/Assets/GraphicTests/Scenes/4x_PostProcessing/4022_PaniniProjection.unity index bc8bd1f9681..a1b86b75c52 100644 --- a/TestProjects/HDRP_Tests/Assets/GraphicTests/Scenes/4x_PostProcessing/4022_PaniniProjection.unity +++ b/TestProjects/HDRP_Tests/Assets/GraphicTests/Scenes/4x_PostProcessing/4022_PaniniProjection.unity @@ -536,7 +536,7 @@ PrefabInstance: - target: {fileID: 114995348509370400, guid: c07ace9ab142ca9469fa377877c2f1e7, type: 3} propertyPath: renderGraphCompatible - value: 0 + value: 1 objectReference: {fileID: 0} m_RemovedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: c07ace9ab142ca9469fa377877c2f1e7, type: 3} diff --git a/TestProjects/HDRP_Tests/Assets/GraphicTests/Scenes/4x_PostProcessing/4051_SMAA.unity b/TestProjects/HDRP_Tests/Assets/GraphicTests/Scenes/4x_PostProcessing/4051_SMAA.unity index 8944b649e7b..aaf19dca66e 100644 --- a/TestProjects/HDRP_Tests/Assets/GraphicTests/Scenes/4x_PostProcessing/4051_SMAA.unity +++ b/TestProjects/HDRP_Tests/Assets/GraphicTests/Scenes/4x_PostProcessing/4051_SMAA.unity @@ -3643,6 +3643,7 @@ MonoBehaviour: m_EnableSpotReflector: 0 m_LuxAtDistance: 1 m_InnerSpotPercent: 0 + m_SpotIESCutoffPercent: 100 m_LightDimmer: 1 m_VolumetricDimmer: 1 m_LightUnit: 2 @@ -3661,6 +3662,8 @@ MonoBehaviour: m_ApplyRangeAttenuation: 1 m_DisplayAreaLightEmissiveMesh: 0 m_AreaLightCookie: {fileID: 0} + m_IESPoint: {fileID: 0} + m_IESSpot: {fileID: 0} m_AreaLightShadowCone: 120 m_UseScreenSpaceShadows: 0 m_InteractsWithSky: 1 @@ -4111,7 +4114,7 @@ PrefabInstance: - target: {fileID: 114995348509370400, guid: c07ace9ab142ca9469fa377877c2f1e7, type: 3} propertyPath: renderGraphCompatible - value: 0 + value: 1 objectReference: {fileID: 0} m_RemovedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: c07ace9ab142ca9469fa377877c2f1e7, type: 3} From 7a5d7fe45f5baf7325bd8d27fadc4714089aa3b8 Mon Sep 17 00:00:00 2001 From: FrancescoC-Unity Date: Mon, 29 Jun 2020 18:24:23 +0200 Subject: [PATCH 07/11] Explicit motion vector texture --- .../PostProcessing/PostProcessSystem.RenderGraph.cs | 12 ++++++++++-- .../Runtime/PostProcessing/PostProcessSystem.cs | 12 ++++++++---- .../RenderPipeline/HDRenderPipeline.PostProcess.cs | 10 +++++----- .../RenderPipeline/HDRenderPipeline.RenderGraph.cs | 2 +- .../Runtime/RenderPipeline/HDRenderPipeline.cs | 1 + 5 files changed, 25 insertions(+), 12 deletions(-) diff --git a/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.RenderGraph.cs b/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.RenderGraph.cs index f648acd12a2..b72976424ef 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.RenderGraph.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.RenderGraph.cs @@ -65,6 +65,7 @@ class TemporalAntiAliasingData public TemporalAntiAliasingParameters parameters; public TextureHandle source; public TextureHandle destination; + public TextureHandle motionVecTexture; public TextureHandle depthBuffer; public TextureHandle depthMipChain; public TextureHandle prevHistory; @@ -95,6 +96,7 @@ class MotionBlurData public MotionBlurParameters parameters; public TextureHandle source; public TextureHandle destination; + public TextureHandle motionVecTexture; public TextureHandle preppedMotionVec; public TextureHandle minMaxTileVel; public TextureHandle maxTileNeigbourhood; @@ -117,7 +119,7 @@ class BloomData public TextureHandle[] mipsUp = new TextureHandle[k_MaxBloomMipCount + 1]; } - void FillBloomMipsTextureHandles(ref BloomData bloomData, RenderGraph renderGraph, RenderGraphBuilder builder) + void FillBloomMipsTextureHandles(BloomData bloomData, RenderGraph renderGraph, RenderGraphBuilder builder) { for (int i = 0; i < m_BloomMipCount; i++) { @@ -153,6 +155,7 @@ public void Render(RenderGraph renderGraph, TextureHandle afterPostProcessTexture, TextureHandle depthBuffer, TextureHandle depthBufferMipChain, + TextureHandle motionVectors, TextureHandle finalRT, bool flipY) { @@ -345,6 +348,7 @@ public void Render(RenderGraph renderGraph, passData.source = builder.ReadTexture(source); passData.parameters = PrepareTAAParameters(hdCamera); passData.depthBuffer = builder.ReadTexture(depthBuffer); + passData.motionVecTexture = builder.ReadTexture(motionVectors); passData.depthMipChain = builder.ReadTexture(depthBufferMipChain); passData.prevHistory = renderGraph.ImportTexture(prevHistory); passData.nextHistory = renderGraph.ImportTexture(nextHistory); @@ -365,6 +369,7 @@ public void Render(RenderGraph renderGraph, { DoTemporalAntialiasing(data.parameters, ctx.cmd, ctx.resources.GetTexture(data.source), ctx.resources.GetTexture(data.destination), + ctx.resources.GetTexture(data.motionVecTexture), ctx.resources.GetTexture(data.depthBuffer), ctx.resources.GetTexture(data.depthMipChain), ctx.resources.GetTexture(data.prevHistory), @@ -449,6 +454,8 @@ public void Render(RenderGraph renderGraph, passData.source = builder.ReadTexture(source); passData.parameters = PrepareMotionBlurParameters(hdCamera); + passData.motionVecTexture = builder.ReadTexture(motionVectors); + Vector2 tileTexScale = new Vector2((float)passData.parameters.tileTargetSize.x / hdCamera.actualWidth, (float)passData.parameters.tileTargetSize.y / hdCamera.actualHeight); passData.preppedMotionVec = builder.CreateTransientTexture(new TextureDesc(Vector2.one, true, true) @@ -486,6 +493,7 @@ public void Render(RenderGraph renderGraph, { DoMotionBlur(data.parameters, ctx.cmd, ctx.resources.GetTexture(data.source), ctx.resources.GetTexture(data.destination), + ctx.resources.GetTexture(data.motionVecTexture), ctx.resources.GetTexture(data.preppedMotionVec), ctx.resources.GetTexture(data.minMaxTileVel), ctx.resources.GetTexture(data.maxTileNeigbourhood), @@ -536,7 +544,7 @@ public void Render(RenderGraph renderGraph, { passData.source = builder.ReadTexture(source); passData.parameters = PrepareBloomParameters(hdCamera); - FillBloomMipsTextureHandles(ref passData, renderGraph, builder); + FillBloomMipsTextureHandles(passData, renderGraph, builder); passData.mipsUp[0] = builder.WriteTexture(passData.mipsUp[0]); diff --git a/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.cs b/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.cs index 91eed628d5a..18f0ee03441 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.cs @@ -468,7 +468,7 @@ static void ClearWithGuardBands(in ClearWithGuardBandsParameters parameters, Com } } - public void Render(CommandBuffer cmd, HDCamera camera, BlueNoise blueNoise, RTHandle colorBuffer, RTHandle afterPostProcessTexture, RenderTargetIdentifier finalRT, RTHandle depthBuffer, RTHandle depthMipChain, bool flipY) + public void Render(CommandBuffer cmd, HDCamera camera, BlueNoise blueNoise, RTHandle colorBuffer, RTHandle afterPostProcessTexture, RenderTargetIdentifier finalRT, RTHandle depthBuffer, RTHandle depthMipChain, RTHandle motionVecTexture, bool flipY) { var dynResHandler = DynamicResolutionHandler.instance; @@ -574,7 +574,7 @@ void PoolSource(ref RTHandle src, RTHandle dst) var taaParams = PrepareTAAParameters(camera); GrabTemporalAntialiasingHistoryTextures(camera, out var prevHistory, out var nextHistory); GrabVelocityMagnitudeHistoryTextures(camera, out var prevMVLen, out var nextMVLen); - DoTemporalAntialiasing(taaParams, cmd, source, destination, depthBuffer, depthMipChain, prevHistory, nextHistory, prevMVLen, nextMVLen); + DoTemporalAntialiasing(taaParams, cmd, source, destination, motionVecTexture, depthBuffer, depthMipChain, prevHistory, nextHistory, prevMVLen, nextMVLen); PoolSource(ref source, destination); } } @@ -632,7 +632,7 @@ void PoolSource(ref RTHandle src, RTHandle dst) out preppedMotionVec, out minMaxTileVel, out maxTileNeigbourhood, out tileToScatterMax, out tileToScatterMin); - DoMotionBlur(PrepareMotionBlurParameters(camera), cmd, source, destination, preppedMotionVec, minMaxTileVel, maxTileNeigbourhood, tileToScatterMax, tileToScatterMin); + DoMotionBlur(PrepareMotionBlurParameters(camera), cmd, source, destination, motionVecTexture, preppedMotionVec, minMaxTileVel, maxTileNeigbourhood, tileToScatterMax, tileToScatterMin); RecycleMotionBlurRenderTargets(preppedMotionVec, minMaxTileVel, maxTileNeigbourhood, tileToScatterMax, tileToScatterMin); PoolSource(ref source, destination); @@ -1512,6 +1512,7 @@ static void DoTemporalAntialiasing(in TemporalAntiAliasingParameters taaParams, CommandBuffer cmd, RTHandle source, RTHandle destination, + RTHandle motionVecTexture, RTHandle depthBuffer, RTHandle depthMipChain, RTHandle prevHistory, @@ -1531,6 +1532,7 @@ static void DoTemporalAntialiasing(in TemporalAntiAliasingParameters taaParams, taaParams.taaPropertyBlock.SetInt(HDShaderIDs._StencilMask, (int)StencilUsage.ExcludeFromTAA); taaParams.taaPropertyBlock.SetInt(HDShaderIDs._StencilRef, (int)StencilUsage.ExcludeFromTAA); + taaParams.taaPropertyBlock.SetTexture(HDShaderIDs._CameraMotionVectorsTexture, motionVecTexture); taaParams.taaPropertyBlock.SetTexture(HDShaderIDs._InputTexture, source); taaParams.taaPropertyBlock.SetTexture(HDShaderIDs._InputHistoryTexture, prevHistory); taaParams.taaPropertyBlock.SetTexture(HDShaderIDs._InputVelocityMagnitudeHistory, prevMVLen); @@ -2444,7 +2446,7 @@ void RecycleMotionBlurRenderTargets(RTHandle preppedMotionVec, RTHandle minMaxTi } } - static void DoMotionBlur(in MotionBlurParameters motionBlurParams, CommandBuffer cmd, RTHandle source, RTHandle destination, + static void DoMotionBlur(in MotionBlurParameters motionBlurParams, CommandBuffer cmd, RTHandle source, RTHandle destination, RTHandle motionVectorTexture, RTHandle preppedMotionVec, RTHandle minMaxTileVel, RTHandle maxTileNeigbourhood, RTHandle tileToScatterMax, RTHandle tileToScatterMin) @@ -2476,6 +2478,8 @@ static void DoMotionBlur(in MotionBlurParameters motionBlurParams, CommandBuffer cmd.SetComputeVectorParam(cs, HDShaderIDs._MotionBlurParams1, motionBlurParams.motionBlurParams1); cmd.SetComputeVectorParam(cs, HDShaderIDs._MotionBlurParams2, motionBlurParams.motionBlurParams2); + cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._CameraMotionVectorsTexture, motionVectorTexture); + cmd.SetComputeMatrixParam(cs, HDShaderIDs._PrevVPMatrixNoTranslation, motionBlurParams.camera.mainViewConstants.prevViewProjMatrixNoCameraTrans); threadGroupX = (motionBlurParams.camera.actualWidth + (groupSizeX - 1)) / groupSizeX; diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.PostProcess.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.PostProcess.cs index e49524646d4..977b6657507 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.PostProcess.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.PostProcess.cs @@ -15,9 +15,8 @@ class AfterPostProcessPassData } TextureHandle RenderPostProcess( RenderGraph renderGraph, + PrepassOutput prepassOutput, TextureHandle inputColor, - TextureHandle depthBuffer, - TextureHandle depthBufferMipChain, TextureHandle backBuffer, CullingResults cullResults, HDCamera hdCamera) @@ -37,7 +36,7 @@ TextureHandle RenderPostProcess( RenderGraph renderGraph, passData.afterPostProcessBuffer = builder.UseColorBuffer(renderGraph.CreateTexture( new TextureDesc(Vector2.one, true, true) { colorFormat = GraphicsFormat.R8G8B8A8_SRGB, clearBuffer = true, clearColor = Color.black, name = "OffScreen AfterPostProcess" }), 0); if (passData.parameters.useDepthBuffer) - passData.depthStencilBuffer = builder.UseDepthBuffer(depthBuffer, DepthAccess.ReadWrite); + passData.depthStencilBuffer = builder.UseDepthBuffer(prepassOutput.depthBuffer, DepthAccess.ReadWrite); passData.opaqueAfterPostprocessRL = builder.UseRendererList(renderGraph.CreateRendererList(passData.parameters.opaqueAfterPPDesc)); passData.transparentAfterPostprocessRL = builder.UseRendererList(renderGraph.CreateRendererList(passData.parameters.transparentAfterPPDesc)); @@ -61,8 +60,9 @@ TextureHandle RenderPostProcess( RenderGraph renderGraph, parameters.blueNoise, inputColor, afterPostProcessBuffer, - depthBuffer, - depthBufferMipChain, + prepassOutput.depthBuffer, + prepassOutput.depthPyramidTexture, + prepassOutput.resolvedMotionVectorsBuffer, dest, parameters.flipYInPostProcess ); diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.RenderGraph.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.RenderGraph.cs index 6a1c65bb8f4..fd90395768f 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.RenderGraph.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.RenderGraph.cs @@ -217,7 +217,7 @@ void ExecuteWithRenderGraph( RenderRequest renderRequest, aovRequest.PushCameraTexture(m_RenderGraph, AOVBuffers.Color, hdCamera, colorBuffer, aovBuffers); - TextureHandle postProcessDest = RenderPostProcess(m_RenderGraph, colorBuffer, prepassOutput.resolvedDepthBuffer, prepassOutput.depthPyramidTexture, backBuffer, cullingResults, hdCamera); + TextureHandle postProcessDest = RenderPostProcess(m_RenderGraph, prepassOutput, colorBuffer, backBuffer, cullingResults, hdCamera); // TODO RENDERGRAPH //// If requested, compute histogram of the very final image diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs index f8dabcad66b..7e1fd26b874 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs @@ -5322,6 +5322,7 @@ void RenderPostProcess(CullingResults cullResults, HDCamera hdCamera, RenderTarg finalRT: destination, depthBuffer: m_SharedRTManager.GetDepthStencilBuffer(), depthMipChain: m_SharedRTManager.GetDepthTexture(), + motionVecTexture: m_SharedRTManager.GetMotionVectorsBuffer(), flipY: parameters.flipYInPostProcess ); } From 9732568739437d6f0cf030af26f9a50655645c35 Mon Sep 17 00:00:00 2001 From: FrancescoC-Unity Date: Fri, 3 Jul 2020 09:24:40 +0200 Subject: [PATCH 08/11] PR addressing (part 1) --- .../PostProcessSystem.RenderGraph.cs | 91 ++++++------------- 1 file changed, 26 insertions(+), 65 deletions(-) diff --git a/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.RenderGraph.cs b/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.RenderGraph.cs index b72976424ef..1cde41f462b 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.RenderGraph.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.RenderGraph.cs @@ -119,6 +119,17 @@ class BloomData public TextureHandle[] mipsUp = new TextureHandle[k_MaxBloomMipCount + 1]; } + TextureHandle GetPostprocessOutputHandle(RenderGraph renderGraph, string name) + { + return renderGraph.CreateTexture(new TextureDesc(Vector2.one, true, true) + { + name = name, + colorFormat = m_ColorFormat, + useMipMap = false, + enableRandomWrite = true + }); + } + void FillBloomMipsTextureHandles(BloomData bloomData, RenderGraph renderGraph, RenderGraphBuilder builder) { for (int i = 0; i < m_BloomMipCount; i++) @@ -190,13 +201,9 @@ public void Render(RenderGraph renderGraph, if (m_PostProcessEnabled) { - // Guard bands (also known as "horrible hack") to avoid bleeding previous RTHandle - // content into smaller viewports with some effects like Bloom that rely on bilinear - // filtering and can't use clamp sampler and the likes - // Note: some platforms can't clear a partial render target so we directly draw black triangles using (var builder = renderGraph.AddRenderPass("Guard Band Clear", out var passData, ProfilingSampler.Get(HDProfileId.GuardBandClear))) { - passData.source = builder.ReadTexture(source); + passData.source = builder.WriteTexture(source); passData.parameters = PrepareClearWithGuardBandsParameters(hdCamera); builder.SetRenderFunc( @@ -221,13 +228,7 @@ public void Render(RenderGraph renderGraph, { passData.source = builder.ReadTexture(source); passData.parameters = PrepareStopNaNParameters(hdCamera); - TextureHandle dest = renderGraph.CreateTexture(new TextureDesc(Vector2.one, true, true) - { - name = "Stop NaNs Destination", - colorFormat = m_ColorFormat, - useMipMap = false, - enableRandomWrite = true - }); + TextureHandle dest = GetPostprocessOutputHandle(renderGraph, "Stop NaNs Destination"); passData.destination = builder.WriteTexture(dest); ; builder.SetRenderFunc( @@ -257,12 +258,12 @@ public void Render(RenderGraph renderGraph, { passData.source = builder.ReadTexture(source); passData.parameters = PrepareExposureParameters(hdCamera); - passData.prevExposure = prevExposureHandle; - passData.nextExposure = nextExposureHandle; + passData.prevExposure = builder.ReadTexture(prevExposureHandle); + passData.nextExposure = builder.WriteTexture(nextExposureHandle); if (m_Exposure.mode.value == ExposureMode.AutomaticHistogram) { - passData.exposureDebugData = renderGraph.ImportTexture(m_DebugExposureData); + passData.exposureDebugData = builder.WriteTexture(renderGraph.ImportTexture(m_DebugExposureData)); builder.SetRenderFunc( (DynamicExposureData data, RenderGraphContext ctx) => { @@ -299,15 +300,9 @@ public void Render(RenderGraph renderGraph, passData.parameters = PrepareApplyExposureParameters(hdCamera); RTHandle prevExp; GrabExposureHistoryTextures(hdCamera, out prevExp, out _); - passData.prevExposure = renderGraph.ImportTexture(prevExp); + passData.prevExposure = builder.ReadTexture(renderGraph.ImportTexture(prevExp)); - TextureHandle dest = renderGraph.CreateTexture(new TextureDesc(Vector2.one, true, true) - { - name = "Apply Exposure Destination", - colorFormat = m_ColorFormat, - useMipMap = false, - enableRandomWrite = true - }); + TextureHandle dest = GetPostprocessOutputHandle(renderGraph, "Apply Exposure Destination"); passData.destination = builder.WriteTexture(dest); ; builder.SetRenderFunc( @@ -355,13 +350,7 @@ public void Render(RenderGraph renderGraph, passData.prevMVLen = renderGraph.ImportTexture(prevMVLen); passData.nextMVLen = renderGraph.ImportTexture(nextMVLen); - TextureHandle dest = renderGraph.CreateTexture(new TextureDesc(Vector2.one, true, true) - { - name = "TAA Destination", - colorFormat = m_ColorFormat, - useMipMap = false, - enableRandomWrite = true - }); + TextureHandle dest = GetPostprocessOutputHandle(renderGraph, "TAA Destination"); passData.destination = builder.WriteTexture(dest); ; builder.SetRenderFunc( @@ -393,13 +382,8 @@ public void Render(RenderGraph renderGraph, { colorFormat = GraphicsFormat.R8G8B8A8_UNorm, enableRandomWrite = true, name = "SMAA Edge Texture" }); passData.smaaBlendTex = builder.CreateTransientTexture(new TextureDesc(Vector2.one, true, true) { colorFormat = GraphicsFormat.R8G8B8A8_UNorm, enableRandomWrite = true, name = "SMAA Blend Texture" }); - TextureHandle dest = renderGraph.CreateTexture(new TextureDesc(Vector2.one, true, true) - { - name = "SMAA Destination", - colorFormat = m_ColorFormat, - useMipMap = false, - enableRandomWrite = true - }); + + TextureHandle dest = GetPostprocessOutputHandle(renderGraph, "SMAA Destination"); passData.destination = builder.WriteTexture(dest); ; builder.SetRenderFunc( @@ -479,13 +463,7 @@ public void Render(RenderGraph renderGraph, { colorFormat = GraphicsFormat.R16_SFloat, enableRandomWrite = true, name = "Tile to Scatter Min" }); } - TextureHandle dest = renderGraph.CreateTexture(new TextureDesc(Vector2.one, true, true) - { - name = "Motion Blur Destination", - colorFormat = m_ColorFormat, - useMipMap = false, - enableRandomWrite = true - }); + TextureHandle dest = GetPostprocessOutputHandle(renderGraph, "Motion Blur Destination"); passData.destination = builder.WriteTexture(dest); ; builder.SetRenderFunc( @@ -516,13 +494,7 @@ public void Render(RenderGraph renderGraph, { passData.source = builder.ReadTexture(source); passData.parameters = PreparePaniniProjectionParameters(hdCamera); - TextureHandle dest = renderGraph.CreateTexture(new TextureDesc(Vector2.one, true, true) - { - name = "Panini Projection Destination", - colorFormat = m_ColorFormat, - useMipMap = false, - enableRandomWrite = true - }); + TextureHandle dest = GetPostprocessOutputHandle(renderGraph, "Panini Projection Destination"); passData.destination = builder.WriteTexture(dest); builder.SetRenderFunc( @@ -597,13 +569,8 @@ public void Render(RenderGraph renderGraph, using (var builder = renderGraph.AddRenderPass("Uber Post", out var passData, ProfilingSampler.Get(HDProfileId.UberPost))) { - TextureHandle dest = renderGraph.CreateTexture(new TextureDesc(Vector2.one, true, true) - { - name = "Uber Post Destination", - colorFormat = m_ColorFormat, - useMipMap = false, - enableRandomWrite = true - }); + TextureHandle dest = GetPostprocessOutputHandle(renderGraph, "Uber Post Destination"); + passData.parameters = PrepareUberPostParameters(hdCamera, isSceneView); passData.source = builder.ReadTexture(source); @@ -646,13 +613,7 @@ public void Render(RenderGraph renderGraph, { passData.source = builder.ReadTexture(source); passData.parameters = PrepareFXAAParameters(hdCamera); - TextureHandle dest = renderGraph.CreateTexture(new TextureDesc(Vector2.one, true, true) - { - name = "FXAA Destination", - colorFormat = m_ColorFormat, - useMipMap = false, - enableRandomWrite = true - }); + TextureHandle dest = GetPostprocessOutputHandle(renderGraph, "FXAA Destination"); passData.destination = builder.WriteTexture(dest); ; builder.SetRenderFunc( From c8417e8a7c94a277953d8fd8c1c0d76e5a300c27 Mon Sep 17 00:00:00 2001 From: FrancescoC-Unity Date: Fri, 3 Jul 2020 09:33:28 +0200 Subject: [PATCH 09/11] Rest of stuff from PR --- .../Runtime/RenderGraph/RenderGraphResources.cs | 8 ++++++++ .../PostProcessing/PostProcessSystem.RenderGraph.cs | 12 ++++++++---- .../Runtime/PostProcessing/PostProcessSystem.cs | 1 - .../RenderPipeline/HDRenderPipeline.PostProcess.cs | 2 +- 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/com.unity.render-pipelines.core/Runtime/RenderGraph/RenderGraphResources.cs b/com.unity.render-pipelines.core/Runtime/RenderGraph/RenderGraphResources.cs index 73ee1dc8535..a7cb0d31c03 100644 --- a/com.unity.render-pipelines.core/Runtime/RenderGraph/RenderGraphResources.cs +++ b/com.unity.render-pipelines.core/Runtime/RenderGraph/RenderGraphResources.cs @@ -41,6 +41,14 @@ internal ResourceHandle(int value, RenderGraphResourceType type) [DebuggerDisplay("Texture ({handle})")] public struct TextureHandle { + private static TextureHandle s_NullHandle = new TextureHandle(); + + /// + /// Returns a null texture handle + /// + /// A null texture handle. + public static TextureHandle nullHandle { get { return s_NullHandle; } } + internal ResourceHandle handle; internal TextureHandle(int handle) { this.handle = new ResourceHandle(handle, RenderGraphResourceType.Texture); } diff --git a/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.RenderGraph.cs b/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.RenderGraph.cs index 1cde41f462b..110d20432b5 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.RenderGraph.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.RenderGraph.cs @@ -345,10 +345,14 @@ public void Render(RenderGraph renderGraph, passData.depthBuffer = builder.ReadTexture(depthBuffer); passData.motionVecTexture = builder.ReadTexture(motionVectors); passData.depthMipChain = builder.ReadTexture(depthBufferMipChain); - passData.prevHistory = renderGraph.ImportTexture(prevHistory); - passData.nextHistory = renderGraph.ImportTexture(nextHistory); - passData.prevMVLen = renderGraph.ImportTexture(prevMVLen); - passData.nextMVLen = renderGraph.ImportTexture(nextMVLen); + passData.prevHistory = builder.ReadTexture(renderGraph.ImportTexture(prevHistory)); + if (passData.parameters.camera.resetPostProcessingHistory) + { + passData.prevHistory = builder.WriteTexture(passData.prevHistory); + } + passData.nextHistory = builder.WriteTexture(renderGraph.ImportTexture(nextHistory)); + passData.prevMVLen = builder.ReadTexture(renderGraph.ImportTexture(prevMVLen)); + passData.nextMVLen = builder.WriteTexture(renderGraph.ImportTexture(nextMVLen)); TextureHandle dest = GetPostprocessOutputHandle(renderGraph, "TAA Destination"); passData.destination = builder.WriteTexture(dest); ; diff --git a/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.cs b/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.cs index 6699d046758..cf5f7c28988 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.cs @@ -1305,7 +1305,6 @@ static void DoDynamicExposure(in ExposureParameters exposureParameters, CommandB var sourceTex = colorBuffer; - // NOTE: Breakpoint here. kernel = exposureParameters.exposurePreparationKernel; cmd.SetComputeIntParams(cs, HDShaderIDs._Variants, exposureParameters.exposureVariants); cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._PreviousExposureTexture, prevExposure); diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.PostProcess.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.PostProcess.cs index 977b6657507..040fec11e4d 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.PostProcess.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.PostProcess.cs @@ -60,7 +60,7 @@ TextureHandle RenderPostProcess( RenderGraph renderGraph, parameters.blueNoise, inputColor, afterPostProcessBuffer, - prepassOutput.depthBuffer, + prepassOutput.resolvedDepthBuffer, prepassOutput.depthPyramidTexture, prepassOutput.resolvedMotionVectorsBuffer, dest, From 836929db9b4a400ef80bddce09488128ac7630b6 Mon Sep 17 00:00:00 2001 From: FrancescoC-Unity Date: Fri, 3 Jul 2020 17:31:42 +0200 Subject: [PATCH 10/11] CAS + scene exposure fix --- .../PostProcessSystem.RenderGraph.cs | 58 ++++++++----------- .../PostProcessing/PostProcessSystem.cs | 8 ++- 2 files changed, 31 insertions(+), 35 deletions(-) diff --git a/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.RenderGraph.cs b/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.RenderGraph.cs index 110d20432b5..b8a260e2538 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.RenderGraph.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.RenderGraph.cs @@ -119,6 +119,13 @@ class BloomData public TextureHandle[] mipsUp = new TextureHandle[k_MaxBloomMipCount + 1]; } + class CASData + { + public CASParameters parameters; + public TextureHandle source; + public TextureHandle destination; + } + TextureHandle GetPostprocessOutputHandle(RenderGraph renderGraph, string name) { return renderGraph.CreateTexture(new TextureDesc(Vector2.one, true, true) @@ -630,41 +637,26 @@ public void Render(RenderGraph renderGraph, } } - // // Contrast Adaptive Sharpen Upscaling - // if (dynResHandler.DynamicResolutionEnabled() && - // dynResHandler.filter == DynamicResUpscaleFilter.ContrastAdaptiveSharpen) - // { - // using (new ProfilingScope(cmd, ProfilingSampler.Get(HDProfileId.ContrastAdaptiveSharpen))) - // { - // var destination = m_Pool.Get(Vector2.one, m_ColorFormat); - - // var cs = m_Resources.shaders.contrastAdaptiveSharpenCS; - // int kInit = cs.FindKernel("KInitialize"); - // int kMain = cs.FindKernel("KMain"); - // if (kInit >= 0 && kMain >= 0) - // { - // cmd.SetComputeFloatParam(cs, HDShaderIDs._Sharpness, 1); - // cmd.SetComputeTextureParam(cs, kMain, HDShaderIDs._InputTexture, source); - // cmd.SetComputeVectorParam(cs, HDShaderIDs._InputTextureDimensions, new Vector4(source.rt.width, source.rt.height)); - // cmd.SetComputeTextureParam(cs, kMain, HDShaderIDs._OutputTexture, destination); - // cmd.SetComputeVectorParam(cs, HDShaderIDs._OutputTextureDimensions, new Vector4(destination.rt.width, destination.rt.height)); - - // ValidateComputeBuffer(ref m_ContrastAdaptiveSharpen, 2, sizeof(uint) * 4); - - // cmd.SetComputeBufferParam(cs, kInit, "CasParameters", m_ContrastAdaptiveSharpen); - // cmd.SetComputeBufferParam(cs, kMain, "CasParameters", m_ContrastAdaptiveSharpen); - - // cmd.DispatchCompute(cs, kInit, 1, 1, 1); - - // int dispatchX = (int)System.Math.Ceiling(destination.rt.width / 16.0f); - // int dispatchY = (int)System.Math.Ceiling(destination.rt.height / 16.0f); + // Contrast Adaptive Sharpen Upscaling + if (dynResHandler.DynamicResolutionEnabled() && + dynResHandler.filter == DynamicResUpscaleFilter.ContrastAdaptiveSharpen) + { + using (var builder = renderGraph.AddRenderPass("Contrast Adaptive Sharpen", out var passData, ProfilingSampler.Get(HDProfileId.ContrastAdaptiveSharpen))) + { + passData.source = builder.ReadTexture(source); + passData.parameters = PrepareContrastAdaptiveSharpeningParameters(hdCamera); + TextureHandle dest = GetPostprocessOutputHandle(renderGraph, "Contrast Adaptive Sharpen Destination"); + passData.destination = builder.WriteTexture(dest); ; - // cmd.DispatchCompute(cs, kMain, dispatchX, dispatchY, camera.viewCount); - // } + builder.SetRenderFunc( + (CASData data, RenderGraphContext ctx) => + { + DoContrastAdaptiveSharpening(data.parameters, ctx.cmd, ctx.resources.GetTexture(data.source), ctx.resources.GetTexture(data.destination)); + }); - // PoolSource(ref source, destination); - // } - // } + source = passData.destination; + } + } using (var builder = renderGraph.AddRenderPass("Final Pass", out var passData, ProfilingSampler.Get(HDProfileId.FinalPost))) { diff --git a/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.cs b/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.cs index cf5f7c28988..f95108fcf97 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.cs @@ -1025,12 +1025,16 @@ ExposureParameters PrepareExposureParameters(HDCamera hdCamera) if (isFixed) { parameters.exposureParams2 = new Vector4(0.0f, 0.0f, ColorUtils.lensImperfectionExposureScale, ColorUtils.s_LightMeterCalibrationConstant); - if (m_Exposure.mode.value == ExposureMode.Fixed) + if (m_Exposure.mode.value == ExposureMode.Fixed +#if UNITY_EDITOR + || HDAdditionalSceneViewSettings.sceneExposureOverriden && hdCamera.camera.cameraType == CameraType.SceneView +#endif + ) { parameters.exposureReductionKernel = parameters.exposureCS.FindKernel("KFixedExposure"); parameters.exposureParams = new Vector4(m_Exposure.compensation.value + m_DebugExposureCompensation, m_Exposure.fixedExposure.value, 0f, 0f); #if UNITY_EDITOR - if (HDAdditionalSceneViewSettings.sceneExposureOverriden) + if (HDAdditionalSceneViewSettings.sceneExposureOverriden && hdCamera.camera.cameraType == CameraType.SceneView) { parameters.exposureParams = new Vector4(0.0f, HDAdditionalSceneViewSettings.sceneExposure, 0f, 0f); } From 9d9315f6dc91240eeaf8a464024a0e17674b5940 Mon Sep 17 00:00:00 2001 From: FrancescoC-Unity Date: Fri, 3 Jul 2020 19:18:05 +0200 Subject: [PATCH 11/11] Fixed an issue when PP is off --- .../PostProcessSystem.RenderGraph.cs | 66 +++++++++---------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.RenderGraph.cs b/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.RenderGraph.cs index b8a260e2538..e1326b0d860 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.RenderGraph.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.RenderGraph.cs @@ -637,48 +637,48 @@ public void Render(RenderGraph renderGraph, } } - // Contrast Adaptive Sharpen Upscaling - if (dynResHandler.DynamicResolutionEnabled() && - dynResHandler.filter == DynamicResUpscaleFilter.ContrastAdaptiveSharpen) - { - using (var builder = renderGraph.AddRenderPass("Contrast Adaptive Sharpen", out var passData, ProfilingSampler.Get(HDProfileId.ContrastAdaptiveSharpen))) - { - passData.source = builder.ReadTexture(source); - passData.parameters = PrepareContrastAdaptiveSharpeningParameters(hdCamera); - TextureHandle dest = GetPostprocessOutputHandle(renderGraph, "Contrast Adaptive Sharpen Destination"); - passData.destination = builder.WriteTexture(dest); ; - - builder.SetRenderFunc( - (CASData data, RenderGraphContext ctx) => - { - DoContrastAdaptiveSharpening(data.parameters, ctx.cmd, ctx.resources.GetTexture(data.source), ctx.resources.GetTexture(data.destination)); - }); - - source = passData.destination; - } - } + hdCamera.resetPostProcessingHistory = false; + } - using (var builder = renderGraph.AddRenderPass("Final Pass", out var passData, ProfilingSampler.Get(HDProfileId.FinalPost))) + // Contrast Adaptive Sharpen Upscaling + if (dynResHandler.DynamicResolutionEnabled() && + dynResHandler.filter == DynamicResUpscaleFilter.ContrastAdaptiveSharpen) + { + using (var builder = renderGraph.AddRenderPass("Contrast Adaptive Sharpen", out var passData, ProfilingSampler.Get(HDProfileId.ContrastAdaptiveSharpen))) { - passData.parameters = PrepareFinalPass(hdCamera, blueNoise, flipY); passData.source = builder.ReadTexture(source); - passData.afterPostProcessTexture = builder.ReadTexture(afterPostProcessTexture); - passData.alphaTexture = builder.ReadTexture(alphaTexture); - passData.destination = builder.WriteTexture(finalRT); + passData.parameters = PrepareContrastAdaptiveSharpeningParameters(hdCamera); + TextureHandle dest = GetPostprocessOutputHandle(renderGraph, "Contrast Adaptive Sharpen Destination"); + passData.destination = builder.WriteTexture(dest); ; builder.SetRenderFunc( - (FinalPassData data, RenderGraphContext ctx) => + (CASData data, RenderGraphContext ctx) => { - DoFinalPass(data.parameters, - ctx.resources.GetTexture(data.source), - ctx.resources.GetTexture(data.afterPostProcessTexture), - ctx.resources.GetTexture(data.destination), - ctx.resources.GetTexture(data.alphaTexture), - ctx.cmd); + DoContrastAdaptiveSharpening(data.parameters, ctx.cmd, ctx.resources.GetTexture(data.source), ctx.resources.GetTexture(data.destination)); }); + + source = passData.destination; } + } - hdCamera.resetPostProcessingHistory = false; + using (var builder = renderGraph.AddRenderPass("Final Pass", out var passData, ProfilingSampler.Get(HDProfileId.FinalPost))) + { + passData.parameters = PrepareFinalPass(hdCamera, blueNoise, flipY); + passData.source = builder.ReadTexture(source); + passData.afterPostProcessTexture = builder.ReadTexture(afterPostProcessTexture); + passData.alphaTexture = builder.ReadTexture(alphaTexture); + passData.destination = builder.WriteTexture(finalRT); + + builder.SetRenderFunc( + (FinalPassData data, RenderGraphContext ctx) => + { + DoFinalPass(data.parameters, + ctx.resources.GetTexture(data.source), + ctx.resources.GetTexture(data.afterPostProcessTexture), + ctx.resources.GetTexture(data.destination), + ctx.resources.GetTexture(data.alphaTexture), + ctx.cmd); + }); } }