Search Unity

Cached Shadows Issues/ Feedback

Discussion in 'High Definition Render Pipeline' started by MylesLambert, Dec 30, 2021.

  1. MylesLambert

    MylesLambert

    Joined:
    Dec 31, 2012
    Posts:
    61
    Hello!
    I've been trying out cached shadows in 2021.2.7f1 HDRP 12.1.2 and have ran into multiple issues.

    Issue 1
    Setup

    5 1024x Shadow casting spotlights set to 'OnDemand', 'PreserveCachedShadows' set to false and 'OnDemandShadowRenderOnPlacement' set to false. This is set up so that only 4 of these can fit in the cached shadow atlas which has a size of 2048x. All light components are enabled.

    I'd expect no shadows to be cached when the scene is loaded, however they are, "WouldFitInAtlas" returns false for "Spot Light (1)" even if it is the first to be enabled/ request shadows. If I call "HDCachedShadowManager.instance.ForceEvictLight" in awake on all lights then "WouldFitInAtlas" returns true. If after this returning true I call "RequestShadowMapRendering" it does correctly render the shadows however it throws "Assertion Failed".

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Rendering.HighDefinition;
    3.  
    4. public class RequestShadows : MonoBehaviour
    5. {
    6.     private HDAdditionalLightData _hdAdditionalLightData;
    7.     private Light _light;
    8.  
    9.     private void Awake()
    10.     {
    11.         _hdAdditionalLightData = GetComponent<HDAdditionalLightData>();
    12.         _light = GetComponent<Light>();
    13.  
    14.         HDCachedShadowManager.instance.ForceEvictLight(_hdAdditionalLightData); // This is required to be able to request shadow map rendering.
    15.     }
    16.  
    17.     [Button]
    18.     void RequestShadowMapRendering()
    19.     {
    20.         _hdAdditionalLightData.RequestShadowMapRendering();
    21.     }
    22. }
    23.  
    I've isolated this in a test project and submitted via the bug reporter - 1391709

    Issue 2
    The terminology 'OnEnable' makes you believe that it's the light components enabled state, it's infact the "HDAdditionalLightData" that affects this.

    Feedback
    'OnEnable' is generally not very useful for larger scenes, I'm very surprised there isn't an option here to cache shadows when the light comes into view/ the first time it renders? And to queue it for caching when the atlas frees up space? This is essential and I can imagine a lot of developers writing something similar to achieve this. Is this something that is planned for?
     

    Attached Files:

    Last edited: Dec 30, 2021
    PutridEx likes this.
  2. PutridEx

    PutridEx

    Joined:
    Feb 3, 2021
    Posts:
    1,136
    Agree with this, this is actually also what I thought at the start reading OnEnable.
    What you mentioned in feedback would be really useful.

    I'm also curious about the cost of cached shadows, it seems they still have a cost, probably related to work with the texture/atlas.
     
  3. francescoc_unity

    francescoc_unity

    Unity Technologies

    Joined:
    Sep 19, 2018
    Posts:
    193
    Thank you for the bug report, we'll get to it.

    Indeed, unfortunately this is a limitation we have HDRP side where we don't have access to modifications of the OnEnable of the Light itself and we piggy back all the development on the HDAdditionalLightData.
    This is in line with what happens with reflection probes and HDProbe.
    In any case, we should at the very least update the documentation to make this clearer.

    The OnEnable should enqueue the rendering of the light immediately, but the actual caching happens only the first time the light will be processed/is in the view. So it should do what you mention. I am in vacation and don't have the code at hand, but that should be already the case.

    Cache shadows will remove the cost of rendering shadow maps. If they are set to Always draw dynamic then an additional blit happens and has a small cost. This is mentioned in the doc. Without that option the only cost left should be of course the one for reading the shadow map; that one will never go away as the caching only acts on the shadow map side, not their projection on screen.

    There is still a very minimal CPU overhead for cached shadows as a bit of checks are required to verify if shadows need rendering or not, but should very be small.
     
    PutridEx and MylesLambert like this.
  4. MylesLambert

    MylesLambert

    Joined:
    Dec 31, 2012
    Posts:
    61
    Thanks for the response @francescoc_unity. Do you know if my bug report was looked into at all?
    If anything doesn't make sense I'd be happy to expand upon it.
     
  5. francescoc_unity

    francescoc_unity

    Unity Technologies

    Joined:
    Sep 19, 2018
    Posts:
    193

    I recently looked at a bug and fixed a couple of issues, not sure it was your bug or not :) Do you have the bug number?

    https://github.com/Unity-Technologies/Graphics/pull/6825 This is what I fixed recently.
     
  6. francescoc_unity

    francescoc_unity

    Unity Technologies

    Joined:
    Sep 19, 2018
    Posts:
    193
    Judging by the sender email for that bug and your user name I am assuming it is you indeed. Let me copy paste also what I wrote in the bug itself: