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).
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.
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.
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.
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.
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.
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.