From f52392a1a93410f42b9708c364b1cc85a761ac2f Mon Sep 17 00:00:00 2001 From: JulienIgnace-Unity Date: Fri, 7 May 2021 14:57:54 +0200 Subject: [PATCH] Improve performance of OnDemand reflection probes [NEED C++] (#2775) * Removed some GCAlloc when creating probes * Stop updating volume system for Lighting Override when not necessary. Disabled ApplyMaterialPropertyDrawers when rendering HDRP to avoid unwanted perf spikes * Removed C++ change dependency * Used new API to disable terrain data deletion for reflection cameras. * Fixed Render Graph immediate mode. (#3033) Co-authored-by: Sebastien Lagarde * Fix issue with shadow mask and area lights (#3019) * Not checking NdotL since it's not really valid for area lights (We have multiple valid light directions, not one) * Changelog Co-authored-by: sebastienlagarde * Fix issue with capture callback (now includes post processing results) (#3035) Co-authored-by: sebastienlagarde * [HDRP] Fix decal draw order for ShaderGraph decal materials (#3018) * Fixed ShaderGraph decal draw order * Updated changelog Co-authored-by: sebastienlagarde * Fixed various Look Dev issues after exiting Playmode (#2956) * Fixed access to invalid Contexts references after exiting playmode. * Fixed comparison gizmo after playmode. * Fixes from PR feedback * StackLit: Fix SG surface option property block to only display energy conserving specular color option for the specular input parametrization (similar to case 1257050) (#3060) * Fixed missing BeginCameraRendering call for custom render mode of a Camera (#3063) * Fix wrong merge * Fixed C++ API change * Use new no alloc API to retrieve active terrain list. * Fixed API change Co-authored-by: Sebastien Lagarde Co-authored-by: FrancescoC-unity <43168857+FrancescoC-unity@users.noreply.github.com> Co-authored-by: Pavlos Mavridis Co-authored-by: Antoine Lelievre Co-authored-by: slunity <37302815+slunity@users.noreply.github.com> --- .../Material/GGXConvolution/IBLFilterGGX.cs | 13 +++--- .../Runtime/RenderPipeline/Camera/HDCamera.cs | 40 ++++++++++--------- .../RenderPipeline/HDRenderPipeline.cs | 11 +++-- 3 files changed, 36 insertions(+), 28 deletions(-) diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/GGXConvolution/IBLFilterGGX.cs b/com.unity.render-pipelines.high-definition/Runtime/Material/GGXConvolution/IBLFilterGGX.cs index 42aed7c6567..a6534936eda 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Material/GGXConvolution/IBLFilterGGX.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/GGXConvolution/IBLFilterGGX.cs @@ -27,6 +27,8 @@ class IBLFilterGGX : IBLFilterBSDF const int k_DefaultPlanarResolution = 512; // Intermediate variables Vector4 currentScreenSize = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); + MaterialPropertyBlock m_MaterialPropertyBlock = new MaterialPropertyBlock(); + public IBLFilterGGX(HDRenderPipelineRuntimeResources renderPipelineResources, MipGenerator mipGenerator) { @@ -138,23 +140,22 @@ void FilterCubemapCommon(CommandBuffer cmd, m_convolveMaterial.SetTexture("_GgxIblSamples", m_GgxIblSampleData); - var props = new MaterialPropertyBlock(); - props.SetTexture("_MainTex", source); - props.SetFloat("_InvOmegaP", invOmegaP); + m_MaterialPropertyBlock.SetTexture("_MainTex", source); + m_MaterialPropertyBlock.SetFloat("_InvOmegaP", invOmegaP); for (int mip = 1; mip < (int)EnvConstants.ConvolutionMipCount; ++mip) { - props.SetFloat("_Level", mip); + m_MaterialPropertyBlock.SetFloat("_Level", mip); for (int face = 0; face < 6; ++face) { var faceSize = new Vector4(source.width >> mip, source.height >> mip, 1.0f / (source.width >> mip), 1.0f / (source.height >> mip)); var transform = HDUtils.ComputePixelCoordToWorldSpaceViewDirectionMatrix(0.5f * Mathf.PI, Vector2.zero, faceSize, worldToViewMatrices[face], true); - props.SetMatrix(HDShaderIDs._PixelCoordToViewDirWS, transform); + m_MaterialPropertyBlock.SetMatrix(HDShaderIDs._PixelCoordToViewDirWS, transform); CoreUtils.SetRenderTarget(cmd, target, ClearFlag.None, mip, (CubemapFace)face); - CoreUtils.DrawFullScreen(cmd, m_convolveMaterial, props); + CoreUtils.DrawFullScreen(cmd, m_convolveMaterial, m_MaterialPropertyBlock); } } } diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Camera/HDCamera.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Camera/HDCamera.cs index adcd98d1d7b..7784b295bfd 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Camera/HDCamera.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Camera/HDCamera.cs @@ -1216,29 +1216,31 @@ internal void UpdateCurrentSky(SkyManager skyManager) visualSky.skySettings = SkyManager.GetSkySetting(volumeStack); visualSky.cloudSettings = SkyManager.GetCloudSetting(volumeStack); - // Now, see if we have a lighting override - // Update needs to happen before testing if the component is active other internal data structure are not properly updated yet. - VolumeManager.instance.Update(skyManager.lightingOverrideVolumeStack, volumeAnchor, skyManager.lightingOverrideLayerMask); - if (VolumeManager.instance.IsComponentActiveInMask(skyManager.lightingOverrideLayerMask)) + lightingSky = visualSky; + + if (skyManager.lightingOverrideLayerMask != 0) { - SkySettings newSkyOverride = SkyManager.GetSkySetting(skyManager.lightingOverrideVolumeStack); - CloudSettings newCloudOverride = SkyManager.GetCloudSetting(skyManager.lightingOverrideVolumeStack); + // Now, see if we have a lighting override + // Update needs to happen before testing if the component is active other internal data structure are not properly updated yet. + VolumeManager.instance.Update(skyManager.lightingOverrideVolumeStack, volumeAnchor, skyManager.lightingOverrideLayerMask); - if ((m_LightingOverrideSky.skySettings != null && newSkyOverride == null) || - (m_LightingOverrideSky.cloudSettings != null && newCloudOverride == null)) + if (VolumeManager.instance.IsComponentActiveInMask(skyManager.lightingOverrideLayerMask)) { - // When we switch from override to no override, we need to make sure that the visual sky will actually be properly re-rendered. - // Resetting the visual sky hash will ensure that. - visualSky.skyParametersHash = -1; - } + SkySettings newSkyOverride = SkyManager.GetSkySetting(skyManager.lightingOverrideVolumeStack); + CloudSettings newCloudOverride = SkyManager.GetCloudSetting(skyManager.lightingOverrideVolumeStack); - m_LightingOverrideSky.skySettings = newSkyOverride; - m_LightingOverrideSky.cloudSettings = newCloudOverride; - lightingSky = m_LightingOverrideSky; - } - else - { - lightingSky = visualSky; + if ((m_LightingOverrideSky.skySettings != null && newSkyOverride == null) || + (m_LightingOverrideSky.cloudSettings != null && newCloudOverride == null)) + { + // When we switch from override to no override, we need to make sure that the visual sky will actually be properly re-rendered. + // Resetting the visual sky hash will ensure that. + visualSky.skyParametersHash = -1; + + m_LightingOverrideSky.skySettings = newSkyOverride; + m_LightingOverrideSky.cloudSettings = newCloudOverride; + lightingSky = m_LightingOverrideSky; + } + } } } } 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 1fc6fd403cf..21422e0250d 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs @@ -144,6 +144,7 @@ internal static HDRenderPipeline currentPipeline RenderStateBlock m_AlphaToMaskBlock; readonly List m_ActivePassVolumes = new List(6); + readonly List m_ActiveTerrains = new List(); // Detect when windows size is changing int m_MaxCameraWidth; @@ -1081,13 +1082,13 @@ protected override void Render(ScriptableRenderContext renderContext, Camera[] c if (newCount != m_FrameCount) { m_FrameCount = newCount; - m_ProbeCameraCache.ClearCamerasUnusedFor(2, Time.frameCount); HDCamera.CleanUnused(); } #if ENABLE_NVIDIA && ENABLE_NVIDIA_MODULE m_DebugDisplaySettings.nvidiaDebugView.Update(); #endif + Terrain.GetActiveTerrains(m_ActiveTerrains); // This syntax is awful and hostile to debugging, please don't use it... using (ListPool.Get(out List renderRequests)) @@ -1453,15 +1454,19 @@ ref List renderDatas for (int j = 0; j < cameraSettings.Count; ++j) { var camera = m_ProbeCameraCache.GetOrCreate((viewerTransform, visibleProbe, j), Time.frameCount, CameraType.Reflection); - var additionalCameraData = camera.GetComponent(); var settingsCopy = m_Asset.currentPlatformRenderPipelineSettings.dynamicResolutionSettings; settingsCopy.forcedPercentage = 100.0f; settingsCopy.forceResolution = true; DynamicResolutionHandler.UpdateAndUseCamera(camera, settingsCopy); - if (additionalCameraData == null) + foreach (var terrain in m_ActiveTerrains) + terrain.SetKeepUnusedCameraRenderingResources(camera.GetInstanceID(), true); + + if (!camera.TryGetComponent(out var additionalCameraData)) + { additionalCameraData = camera.gameObject.AddComponent(); + } additionalCameraData.hasPersistentHistory = true; // We need to set a targetTexture with the right otherwise when setting pixelRect, it will be rescaled internally to the size of the screen