Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question HDRP Shadows On Enable/Demand not working properly?

Discussion in 'High Definition Render Pipeline' started by Lifee00, Nov 30, 2023.

  1. Lifee00

    Lifee00

    Joined:
    Apr 13, 2021
    Posts:
    80
    Hello, so since shadows are tough on the CPU, I would like to consider the option of not updating them every frame, so basically on enable or on demand. However, when I Select any of those 2 options, there is very strange shadow deformation from my cameraview, I've tweaked every little shadow option and it's always there, can't figure out what's causing it. I'll post pics.
    Shadows Update Mode Every Frame:

    Shadows Update mode on Enable/On Demand
     
  2. Misaki_eKU

    Misaki_eKU

    Joined:
    May 3, 2018
    Posts:
    85
    If it's directional light, the shadow needs to be calculating every frame if the camera is moving. Because the camera that captures the depth information to calculate the shadow moves with the main camera.
     
  3. Jack_Martison

    Jack_Martison

    Joined:
    Jun 24, 2018
    Posts:
    134
    Yeah this is weird thing been there forever. But I'd suggest writing script to update shadows every 0.1 second or more. Drastically improves performance
     
    Lifee00 likes this.
  4. Lifee00

    Lifee00

    Joined:
    Apr 13, 2021
    Posts:
    80
    I see, so it's not possible to use the Enable/Demand mode without those weird deformations. The best case then indeed would be updating shadows every X seconds via script for performance. Actually the On Demand mode is waiting for input from a script right.
     
  5. Misaki_eKU

    Misaki_eKU

    Joined:
    May 3, 2018
    Posts:
    85
    For most (or some) AAA titled game, they change the shadow update frequency depend on cascade. They update the shadow in cascade 0 every frame and decrease the update frequency if cascade level is increase. For example, update every 2 frames on cascade 1, every 4 frames in cascade 2, etc.
     
    Lifee00 likes this.
  6. Lifee00

    Lifee00

    Joined:
    Apr 13, 2021
    Posts:
    80
    That indeed sounds like smart optimization and high quality shadows. I'm probably fine either way as long as shadows dont update every frame, really drops FPS a lot, like 90 to 70. I'm a just a bit afraid when I put zombies in my scene, tris count will jump by a lot and fps will drop below 60.
     
  7. mgeorgedeveloper

    mgeorgedeveloper

    Joined:
    Jul 10, 2012
    Posts:
    306
    The overall issue with the "on demand" style update for directional light shadow maps, is that they are completely view dependent. We had another long thread here a while back when we thought there was some bug with the new mixed cached shadow support for directional lights, but it turned out it was not a bug but rather just the fact that these shadow maps are view dependent and need to be updated quite frequently...

    Now, if you update the entire directional light shadow map on demand once every few frames, the issue is that you will still get jittery updates for dynamic objects walking around in camera view.

    So the way I deal with this now is as follows:
    - Set the shadows to on demand.
    - Enable the "always draw dynamic" box or whatever its called. This will update the shadow map every frame, but only for dynamic objects like the characters walking around etc.
    - Now that leaves the static stuff - what I do is update one cascade per frame. In my opinion this is better than just doing no update at all, and then suddenly doing a full shadow map update (all cascades) every X frames. This approach causes a mild spike which I dislike.

    To update one cascade per frame, you can use HDAdditionalLightData.RequestSubShadowMapRendering().
     
    Genebris, pierre92nicot and Lifee00 like this.
  8. Lifee00

    Lifee00

    Joined:
    Apr 13, 2021
    Posts:
    80
    Sounds good. I shall try few of these methods to see what works best for me in the near future.
    So each frame you update one shadowmap and you pass indexes HDAdditionalLightData.RequestSubShadowMapRendering(1)
    HDAdditionalLightData.RequestSubShadowMapRendering(2) etc. ? depending on how many shadow maps the light supports.
    So in that case, directional lights should have 4 shadows maps(cascades) and it would gives us x4 performance compared to updating everything every frame.
     
  9. Jack_Martison

    Jack_Martison

    Joined:
    Jun 24, 2018
    Posts:
    134
    Wait if I remember correctly your game uses day/night cycle, right? Is that how you actually manage to do all of that, or it's just for static lighting involving dynamic objects like characters?
     
  10. mgeorgedeveloper

    mgeorgedeveloper

    Joined:
    Jul 10, 2012
    Posts:
    306
    All the lighting is dynamic. The sunlight moves dynamically but slowly. Most of the environment is static, but some of the environment is dynamic (characters, foliage, etc).

    The dynamic shadow maps for the directional light is updated every frame.

    The on-demand shadow maps (static objects) for the direction light is updated one cascade per frame. This is frequent enough to avoid any issues due to movement of the camera (it is not so much the movement of the sunlight that is an issue, but rather the camera).
     
    Jack_Martison likes this.
  11. Genebris

    Genebris

    Joined:
    Mar 18, 2013
    Posts:
    141
    Thanks! I had no idea you can do per cascade, this is a real way to solve that.