Skip to content

[Backport 7.x.x] Fix various leaks in HDRP #120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,26 @@ public static TType instance
{
if (s_Instance == null)
{
GameObject go = new GameObject("Default " + typeof(TType)) { hideFlags = HideFlags.HideAndDontSave };
GameObject go = new GameObject("Default " + typeof(TType).Name) { hideFlags = HideFlags.HideAndDontSave };
go.SetActive(false);
s_Instance = go.AddComponent<TType>();
}

return s_Instance;
}
}

/// <summary>
/// Release the component singleton.
/// </summary>
public static void Release()
{
if (s_Instance != null)
{
var go = s_Instance.gameObject;
CoreUtils.Destroy(go);
s_Instance = null;
}
}
}
}
1 change: 1 addition & 0 deletions com.unity.render-pipelines.high-definition/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Fixed an issue where changing the default volume profile from another inspector would not update the default volume editor.
- Fix for range compression factor for probes going negative (now clamped to positive values).
- Fixed path validation when creating new volume profile (case 1229933)
- Fixed various object leaks in HDRP.

### Changed
- Rejecting history for ray traced reflections based on a threshold evaluated on the neighborhood of the sampled history.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,9 @@ public void Release()
CoreUtils.Destroy(m_CubeBlitMaterial);
}

m_Cache.Release();
CoreUtils.Destroy(m_BlitCubemapFaceMaterial);

CoreUtils.Destroy(m_Cache);
}

private bool TransferToPanoCache(CommandBuffer cmd, int sliceIndex, Texture[] textureArray)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ partial class HDShadowManager : IDisposable
int m_CascadeCount;
int m_ShadowResolutionRequestCounter;

Material m_ClearShadowMaterial;

private static HDShadowManager s_Instance = new HDShadowManager();

public static HDShadowManager instance { get { return s_Instance; } }
Expand All @@ -268,7 +270,7 @@ private HDShadowManager()
public void InitShadowManager(RenderPipelineResources renderPipelineResources, DepthBits directionalShadowDepthBits,
HDShadowInitParameters.HDShadowAtlasInitParams punctualLightAtlasInfo, HDShadowInitParameters.HDShadowAtlasInitParams areaLightAtlasInfo, int maxShadowRequests, Shader clearShader)
{
Material clearMaterial = CoreUtils.CreateEngineMaterial(clearShader);
m_ClearShadowMaterial = CoreUtils.CreateEngineMaterial(clearShader);

// Prevent the list from resizing their internal container when we add shadow requests
m_ShadowDatas.Capacity = Math.Max(maxShadowRequests, m_ShadowDatas.Capacity);
Expand All @@ -282,13 +284,13 @@ public void InitShadowManager(RenderPipelineResources renderPipelineResources, D
}

// The cascade atlas will be allocated only if there is a directional light
m_Atlas = new HDShadowAtlas(renderPipelineResources, punctualLightAtlasInfo.shadowAtlasResolution, punctualLightAtlasInfo.shadowAtlasResolution, HDShaderIDs._ShadowmapAtlas, HDShaderIDs._ShadowAtlasSize, clearMaterial, maxShadowRequests, depthBufferBits: punctualLightAtlasInfo.shadowAtlasDepthBits, name: "Shadow Map Atlas");
m_Atlas = new HDShadowAtlas(renderPipelineResources, punctualLightAtlasInfo.shadowAtlasResolution, punctualLightAtlasInfo.shadowAtlasResolution, HDShaderIDs._ShadowmapAtlas, HDShaderIDs._ShadowAtlasSize, m_ClearShadowMaterial, maxShadowRequests, depthBufferBits: punctualLightAtlasInfo.shadowAtlasDepthBits, name: "Shadow Map Atlas");
// Cascade atlas render texture will only be allocated if there is a shadow casting directional light
HDShadowAtlas.BlurAlgorithm cascadeBlur = GetDirectionalShadowAlgorithm() == DirectionalShadowAlgorithm.IMS ? HDShadowAtlas.BlurAlgorithm.IM : HDShadowAtlas.BlurAlgorithm.None;
m_CascadeAtlas = new HDShadowAtlas(renderPipelineResources, 1, 1, HDShaderIDs._ShadowmapCascadeAtlas, HDShaderIDs._CascadeShadowAtlasSize, clearMaterial, maxShadowRequests, cascadeBlur, depthBufferBits: directionalShadowDepthBits, name: "Cascade Shadow Map Atlas");
m_CascadeAtlas = new HDShadowAtlas(renderPipelineResources, 1, 1, HDShaderIDs._ShadowmapCascadeAtlas, HDShaderIDs._CascadeShadowAtlasSize, m_ClearShadowMaterial, maxShadowRequests, cascadeBlur, depthBufferBits: directionalShadowDepthBits, name: "Cascade Shadow Map Atlas");

if (ShaderConfig.s_AreaLights == 1)
m_AreaLightShadowAtlas = new HDShadowAtlas(renderPipelineResources, areaLightAtlasInfo.shadowAtlasResolution, areaLightAtlasInfo.shadowAtlasResolution, HDShaderIDs._AreaLightShadowmapAtlas, HDShaderIDs._AreaShadowAtlasSize, clearMaterial, maxShadowRequests, HDShadowAtlas.BlurAlgorithm.EVSM, depthBufferBits: areaLightAtlasInfo.shadowAtlasDepthBits, name: "Area Light Shadow Map Atlas", momentAtlasShaderID: HDShaderIDs._AreaShadowmapMomentAtlas);
m_AreaLightShadowAtlas = new HDShadowAtlas(renderPipelineResources, areaLightAtlasInfo.shadowAtlasResolution, areaLightAtlasInfo.shadowAtlasResolution, HDShaderIDs._AreaLightShadowmapAtlas, HDShaderIDs._AreaShadowAtlasSize, m_ClearShadowMaterial, maxShadowRequests, HDShadowAtlas.BlurAlgorithm.EVSM, depthBufferBits: areaLightAtlasInfo.shadowAtlasDepthBits, name: "Area Light Shadow Map Atlas", momentAtlasShaderID: HDShaderIDs._AreaShadowmapMomentAtlas);

m_ShadowDataBuffer = new ComputeBuffer(maxShadowRequests, System.Runtime.InteropServices.Marshal.SizeOf(typeof(HDShadowData)));
m_DirectionalShadowDataBuffer = new ComputeBuffer(1, System.Runtime.InteropServices.Marshal.SizeOf(typeof(HDDirectionalShadowData)));
Expand Down Expand Up @@ -822,6 +824,8 @@ public void Dispose()
if (ShaderConfig.s_AreaLights == 1)
m_AreaLightShadowAtlas.Release();
m_CascadeAtlas.Release();

CoreUtils.Destroy(m_ClearShadowMaterial);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,8 @@ public void Cleanup()
RTHandles.Release(m_InternalLogLut);
CoreUtils.Destroy(m_FinalPassMaterial);
CoreUtils.Destroy(m_ClearBlackMaterial);
CoreUtils.Destroy(m_SMAAMaterial);
CoreUtils.Destroy(m_TemporalAAMaterial);
CoreUtils.SafeRelease(m_BokehNearKernel);
CoreUtils.SafeRelease(m_BokehFarKernel);
CoreUtils.SafeRelease(m_BokehIndirectCmd);
Expand All @@ -285,6 +287,8 @@ public void Cleanup()
m_InternalLogLut = null;
m_FinalPassMaterial = null;
m_ClearBlackMaterial = null;
m_SMAAMaterial = null;
m_TemporalAAMaterial = null;
m_BokehNearKernel = null;
m_BokehFarKernel = null;
m_BokehIndirectCmd = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,8 @@ void DisposeProbeCameraPool()
}

CameraCaptureBridge.enabled = false;

HDUtils.ReleaseComponentSingletons();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public void Release()
RTHandles.Release(m_TempDownsamplePyramid[i]);
m_TempDownsamplePyramid[i] = null;
}

CoreUtils.Destroy(m_ColorPyramidPSMat);
}

private int tmpTargetCount
Expand Down Expand Up @@ -222,4 +224,4 @@ public int RenderColorGaussianPyramid(CommandBuffer cmd, Vector2Int size, Textur
return srcMipLevel + 1;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class HDUtils
static internal HDAdditionalLightData s_DefaultHDAdditionalLightData { get { return ComponentSingleton<HDAdditionalLightData>.instance; } }
/// <summary>Default HDAdditionalCameraData</summary>
static internal HDAdditionalCameraData s_DefaultHDAdditionalCameraData { get { return ComponentSingleton<HDAdditionalCameraData>.instance; } }

static List<CustomPassVolume> m_TempCustomPassVolumeList = new List<CustomPassVolume>();

static Texture3D m_ClearTexture3D;
Expand Down Expand Up @@ -473,7 +473,7 @@ internal static RenderPipelineAsset SwitchToBuiltinRenderPipeline(out bool asset
}

// Set the renderPipelineAsset, either on the quality settings if it was unset from there or in GraphicsSettings.
// IMPORTANT: RenderPipelineManager.currentPipeline won't be HDRP until a camera.Render() call is made.
// IMPORTANT: RenderPipelineManager.currentPipeline won't be HDRP until a camera.Render() call is made.
internal static void RestoreRenderPipelineAsset(bool wasUnsetFromQuality, RenderPipelineAsset renderPipelineAsset)
{
if(wasUnsetFromQuality)
Expand Down Expand Up @@ -1002,6 +1002,13 @@ internal static void DisplayUnsupportedAPIMessage(string graphicAPI = null)
DisplayUnsupportedMessage(msg);
}

internal static void ReleaseComponentSingletons()
{
ComponentSingleton<HDAdditionalReflectionData>.Release();
ComponentSingleton<HDAdditionalLightData>.Release();
ComponentSingleton<HDAdditionalCameraData>.Release();
}

internal static void DisplayUnsupportedXRMessage()
{
string msg = "AR/VR devices are not supported, no rendering will occur";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public enum PbrSkyConfig

static ComputeShader s_GroundIrradiancePrecomputationCS;
static ComputeShader s_InScatteredRadiancePrecomputationCS;
static Material s_PbrSkyMaterial;
Material s_PbrSkyMaterial;
static MaterialPropertyBlock s_PbrSkyMaterialProperties;

static GraphicsFormat s_ColorFormat = GraphicsFormat.R16G16B16A16_SFloat;
Expand Down Expand Up @@ -80,8 +80,7 @@ public override void Build()
s_InScatteredRadiancePrecomputationCS = hdrpResources.shaders.inScatteredRadiancePrecomputationCS;
s_PbrSkyMaterialProperties = new MaterialPropertyBlock();

if (s_PbrSkyMaterial == null) // Material instance is static.
s_PbrSkyMaterial = CoreUtils.CreateEngineMaterial(hdrpResources.shaders.physicallyBasedSkyPS);
s_PbrSkyMaterial = CoreUtils.CreateEngineMaterial(hdrpResources.shaders.physicallyBasedSkyPS);

Debug.Assert(s_GroundIrradiancePrecomputationCS != null);
Debug.Assert(s_InScatteredRadiancePrecomputationCS != null);
Expand Down Expand Up @@ -126,6 +125,8 @@ public override void Cleanup()
RTHandles.Release(m_InScatteredRadianceTables[3]); m_InScatteredRadianceTables[3] = null;
RTHandles.Release(m_InScatteredRadianceTables[4]); m_InScatteredRadianceTables[4] = null;

CoreUtils.Destroy(s_PbrSkyMaterial);

m_LastPrecomputedBounce = 0;
}

Expand Down