Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Large lag spike with ray tracing

Discussion in 'High Definition Render Pipeline' started by Grabbaduc, Jan 27, 2022.

  1. Grabbaduc

    Grabbaduc

    Joined:
    Aug 28, 2017
    Posts:
    3
    Hi there,

    I've been working with a team on this art installation unity project. It's a scene with a single camera pose and has a large amount of characters interacting with each other. Right now we're working in Unity version 2020.2.2f1.

    The project is focused on presentation so we've been using HDRP. We've also been using raytracing with pretty good results. Despite having up to 500 characters on screen we're getting a decent frame rate and right now we're aiming to have this scene run in real time (contrary to making a render like we did with the previous scene).

    Tribe War Screenshot.png

    The problem is that despite the good frame rate, we're getting a very noticeable lag spike about every 10 seconds. This is both in the editor and in a build and reproducible on both our work rigs (my laptop with RTX2070 and a desktop with RTX3090, so I don't think it's caused by a lack of hardware). When I enable deep profiling and look in the profiler I see the lag spike is a Semiphore.WaitForSignal which is caused by inexplicably long time for "D3D12RayTracingState.Create" under RaytracingDeferredLighting.

    Tribe War Ray Tracing Lag Spike.png

    Has anyone seen this before and/or might know what's going on? Is this a bug within Unity? I'm really stuck on this as it is ruining the real time version of the project.

    Thanks!
     
  2. eggsamurai

    eggsamurai

    Joined:
    Oct 10, 2015
    Posts:
    116
    RenderDoc, my friend.
     
  3. gwyrwasEICC

    gwyrwasEICC

    Joined:
    Mar 1, 2021
    Posts:
    19
    This seems like it may be an issue with how the deferred rending path interacts with raytraced effects in that version of HDRP. The first thing that I would try is switching to the forward rendering path and seeing if that resolves the issue. One caveat with this is that depending on the number of lights you have in your scenes the performance may drop below your target framerate due to the way lighting is handled in forward rendering compared to deferred rendering.

    If you'd like to read more about the differences between deferred and forward rendering you can read this page in the HDRP documentation.
    https://docs.unity3d.com/Packages/c....1/manual/Forward-And-Deferred-Rendering.html
     
    Last edited: Jan 31, 2022
  4. chap-unity

    chap-unity

    Unity Technologies

    Joined:
    Nov 4, 2019
    Posts:
    774
    What ray traced effects are you using exactly?
    That could help narrow down where the issue is coming from
     
  5. Grabbaduc

    Grabbaduc

    Joined:
    Aug 28, 2017
    Posts:
    3
    The lit shader mode is already set to forward sadly, so that won't fix the issue.

    I'm not really sure what you mean with ray tracing effects, we're using the build in preview ray tracing setting and we have it enabled on all our post processing component (ambient occlusion, screen space global illumination and screen space reflection). When I disable all the those ray tracing checks the lag spike is still present but takes up less frames.

    For now I'm going to resort to upgrading to unity 2021 (either porting or trying to rebuild the scene using packages) as we've been having more success with a different project in that version.
     
  6. Grabbaduc

    Grabbaduc

    Joined:
    Aug 28, 2017
    Posts:
    3
    Sooo upgrading still didn't give any decent results. I've done a lot of testing now and the problem still remains. The only valuable info I was able to get was that the lag spike happens whenever I add or remove characters from the scene (with SetActive(), all characters are pre-instantiated).

    I can't seem to fix the problem but I'll try to manage it by enabling and disabling all the characters in bulk and have them waiting outside of the view frustum until needed (the amount of characters enabled/disabled doesn't affect the lag intensity). This way I'll only have one or two spikes every 5 minutes or so. It's not perfect but better than having the spikes all the time.
     
  7. TheGamery

    TheGamery

    Joined:
    Oct 14, 2013
    Posts:
    94
    From my experience HDRP even before you add RT is a CPU (main thread) hog.

    Until Unity moves it to jobs/multi threads it will have limited uses imo.
     
  8. auzaiffe

    auzaiffe

    Unity Technologies

    Joined:
    Sep 4, 2018
    Posts:
    255
    Hi,

    I am not sure what your problem is, but i'll try to give you pointers to narrow it down without having access to your project for profiling.
    If the spikes happen during "RaytracingDeferredLighting" and only during that call it means that it is indeed related to ray traced effects (in this case Ray traced global illumination and/or ray traced reflections). There can be many reasons for that:
    • A new (or multiple) high density mesh(es) has(ve) been added to the RTAS for the first time since X frames, the building job is blocking and that could explain the stall.
    • One or multiple rays endup hitting a very complex path from time to time, but I wouldn't expect "multiple frames), more like a 5% slow down for that specific effect.
    • An issue in the resource table building (pre-trace) which leads to a new "massive" memory allocation.
    That said you've mentionned that you still get spikes even with those disabled. If that is true, you should be able to captures the spikes with the profiler and find where they happen. This could be caused by a couple things.
    • You still have an other ray tracing effect enabled (ray traced shadows, ray traced contact shadows), then we would go back to the previous set of problems.
    • The ray tracing culling process is taking too much time.
    • It's a DX12 issue, not related to ray tracing.
    Without a repro, it is going to be hard for me to understand if something is wrong on the HDRP/Unity side. So 'I'd suggest maybe trying to wrap up a simple project and file a bug (if you can). If not possible, we'll try to do one but I can't guarantee we'll be able to reproduce you problem.

    Cheers,
     
    chap-unity likes this.