How to deallocate memory for shadowmaps when shadows are disabled (HDRP)

Is there any way to deallocate memory for shadow maps, cached shadow maps, etc at runtime in HDRP?

I’m working on a HDRP project targeted at gaming PCs but would like to give users extensive settings control so it can run on much lower end hardware, for example on integrated graphics found in typical non-gaming laptops. A lot of those have 8GB shared memory so the “boilerplate” that comes with HDRP can be quite an issue.

Total memory usage is a big concern and in profiling I’ve found, despite disabling shadows (setting draw distance to 0 in volume profile via script based on user input), Unity continues to allocate memory to both shadow maps and cached shadows maps, since I’m using on demand real-time lights. Totally makes sense why it would continue to allocate, but I am wondering, is there anyway to avoid this allocation at runtime based on a user setting?

You should be able to fully disable shadowmaps by using the frame settings (here on a scene camera) :

It can be done through scripting with FrameSettings.SetEnabled().

Thanks, I did try this out and while disabling shadowsmaps in the frame settings inspector does prevent them being allocated in memory, if I disable shadowmaps via script (FrameSettings.SetEnabled() ), they still remain in memory. Is there a way to clear them from memory once the SetEnabled() is called or would that require a restart?

Did you also enable custom frame settings on the camera for it to have effect ?
How do you inspect that the shadowmap is allocated or not ?

Here is a simple script that I tested working to effectively disable shadowmaps :

using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.Rendering.HighDefinition;

[RequireComponent(typeof(Camera))]
public class DisableShadows : MonoBehaviour
{
    bool shadowState = true;
    HDAdditionalCameraData hdCamData;

    private void Start()
    {
        hdCamData = GetComponent<HDAdditionalCameraData>();

        // Enable the use of custom frame settings.
        hdCamData.customRenderingSettings = true;
        // Enable the ShadowMaps toggle override of the custom frame settings.
        hdCamData.renderingPathCustomFrameSettingsOverrideMask.mask[(int) FrameSettingsField.ShadowMaps] = true;
    }

    void Update()
    {
        if (Keyboard.current.spaceKey.wasPressedThisFrame)
        {
            Debug.Log("Toggle Shadows");
            shadowState = !shadowState;
            // Change the state of the ShadowMaps toggle in the custom frame settings.
            hdCamData.renderingPathCustomFrameSettings.SetEnabled(FrameSettingsField.ShadowMaps, shadowState);
        }
    }
}

I did inspect the frame debugger before and after disabling the shadowmaps, and indeed the maps are still there and allocated, but they are a single pixel black texture (here by looking at the deferred lighting pass) :

I did see shadowmaps and shadow cache/atlases are set to 1x1 textures in frame debugger as well, and visually shadows are indeed disabled (better method than what I was doing setting volume profile shadow distance to 0).

I was looking in memory profiler to see whether the shadowmaps/cache/atlases were still allocated. If the camera frame settings are set in the editor before running the game, nothing is allocated as expected, but if I toggle the frame setting override at runtime, and then collect a memory profile snapshot, the shadowmaps/cache/atlasas remain in memory. I just wanted to check if this is working as intended, the frame debugger result does look good.

You could try to call HDRenderPipeline.ReleasePersistentShadowAtlases() after disabling the shadows in the frame settings.

image2


thank you! for some reason this doesn’t get rid of all of them, but still makes a huge difference!
edit: top = after, bottom = before