Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Performance spikes due to DXGI.WaitOnSwapChain

Discussion in 'Editor & General Support' started by Caparrini, May 13, 2021.

  1. Caparrini

    Caparrini

    Joined:
    Jun 21, 2017
    Posts:
    56
    I'm running the profiler on a Standalone build, and while 99% of the time I run 60fps fixed no problem, every now and then I have a huge spike of 50+ms per frame. There is no Loading or Unloading of scenes, and all I can see in the Profiler as a cause for it is "DXGI.WaitOnSwapChain" but I couldn't find any information about what might be causing it. I'm leaving here a profiler capture of a normal frame, and one of the spikes.

    I appreciate any clue about what might be happening or how can I investigate this further?
    Thank you

    Profiler1.png Profiler2.png
     
    vanger1 likes this.
  2. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,645
    It's waiting for the GPU to finish rendering the frame according to QualitySettings.maxQueuedFrames (frame n - 2 in the default case). Did you try turning on the GPU profiler?

    Did you disable VSync and instead use Application.targetFrameRate to limit framerate? I would expect DXGI.WaitOnSwapChain to be the one limiting the framerate, not wait for target fps.
     
  3. Caparrini

    Caparrini

    Joined:
    Jun 21, 2017
    Posts:
    56
    Yes, for those screenshots I limited the Framerate the way you described. But I run another build with no fps cap and still spikes the same way.
     
  4. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,645
    Does it only repro with vsync disabled? Or does it also repro if you enable it?
     
  5. Caparrini

    Caparrini

    Joined:
    Jun 21, 2017
    Posts:
    56
    Only repro with VSync disabled. With VSync enables runs completely smooth at 144fps on my monitor, and at 1440p. Honestly, it has never run better than it does now just by changing the Vsync setting. What might be happening here?
     
    Last edited: May 13, 2021
  6. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,645
    The fact that it runs smoother with VSync enabled is not surprising, as it forces the frame time to align with your monitor refresh rate. Still, disabling it shouldn't cause spikes like that (they should be on the order of 1-3 ms).

    I'm not sure why you're seeing spikes like that. As I mentioned above, the GPU profiler might reveal more details about it. Did you try turning it on?
     
  7. Caparrini

    Caparrini

    Joined:
    Jun 21, 2017
    Posts:
    56
    I did, but I couldn't see anything out of the ordinary. Another thing that surprisingly has affected as well is that I have a multi-scene project where I load and unload async. I used to have big spikes when loading multiple scenes. Now, it doesn't even hiccup a frame.
     
  8. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,645
    So what is the gpu doing during that lag spike? Can you show a screenshot?
     
  9. Caparrini

    Caparrini

    Joined:
    Jun 21, 2017
    Posts:
    56
    I had to run it in the Editor to have the data of the GPU profiler, but I reproduced it going back to the previous settings. It looks like it uses all its time in "Fixed Exposure" Here is the screenshot

    upload_2021-5-13_18-48-21.png
     
  10. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,645
    Yeah, I don't know why it does that. If you want, you can submit a bug report and we'll investigate. However, since enabling VSync makes the issue go away, I suggest you do that as you should ship your game with VSync enabled (and allow to optionally disable it in options).
     
  11. Caparrini

    Caparrini

    Joined:
    Jun 21, 2017
    Posts:
    56
    I'll do that. Thanks a lot for the help :)
     
  12. VincentAbert

    VincentAbert

    Joined:
    May 2, 2020
    Posts:
    123
    Hello,

    I have exactly the same issue, using Unity 2020.3 LTS, but it happens when using VSync as well. I mostly get 60fps, but a few spikes of 50 to 100 ms due to DXGI.WaitOnSwapChain happen. Especially in the first 20 seconds, I always get a few spikes, and then occasionnally they come back.
     
    Alverik likes this.
  13. h0neyfr0g

    h0neyfr0g

    Joined:
    Jul 13, 2019
    Posts:
    35
    I am also still having this issue in 2020.3. Has anyone found a solution? Thanks!
     
    b_bizet likes this.
  14. poprev-3d

    poprev-3d

    Joined:
    Apr 12, 2019
    Posts:
    71
    Having this issue in Unity 2020.3 as well, very annoying !
     
    ExcaliburGames and b_bizet like this.
  15. ViicEsquivel

    ViicEsquivel

    Joined:
    Sep 23, 2014
    Posts:
    7
    Same here, in Unity 2020.3.19f1.1034 @Tautvydas-Zilys
    The issue is present with and without VSync. In my case is pretty brutal. While profiling the build it delays 3.5k ms the execution. The game runs at 1fps. vsync_issue.png
     
  16. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,645
    That sounds like a completely different issue. What does your GPU usage look like in the task manager when this happens?
     
  17. Mctuggernuts25

    Mctuggernuts25

    Joined:
    May 26, 2020
    Posts:
    8
    Has anyone found a fix for this? I just updated from 2019 to 2020.3 and my build is literally unplayable. its like 1fps. dxgi.waitforswapchain is using 97%
     
  18. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,645
    I'd check the log. Something is going very wrong with rendering.
     
  19. mehmet_soyturk

    mehmet_soyturk

    Joined:
    Oct 17, 2022
    Posts:
    15
    I have the same issue as the one of @ViicEsquivel .

    DXGI.WaitOnSwapChain() and Gfx.WaitForGfxCommandsFromMainThread() seem to be waiting at the same time for no apparent reason. CPU usage is 7.5% (8 cores in total). GPU usage is 90%.

    profiler.png

    Please note that the application initially starts at around 30 FPS. Then it gradually decreases to 10 FPS.

    There is nothing relevant in the application log.
     
  20. Lynx7

    Lynx7

    Joined:
    Apr 10, 2021
    Posts:
    2
    I encountered this problem in a slightly different case.
    When I put together the build of the game, I noticed a lock of frames per second up to 60, even if VSync and the target frame rate are disabled.
    Cause of this behavior is the same process GfxDeviceD3D11.WaitForLastPresent and subprocess DXGI.WaitOnSwapChain.

    But this began to appear only when my hardware began to support DX11.
    Before, on DRX10 hardware I have not locking FPS in build.
    And when testing the build, my friends with good discrete graphics cards did not notice fps blocking (I have intel HD graphics 4000).

    When I builded game on OpenGLCore the problem has solved, but I prefer to use DRX11 on windows platform.

    Below I provide the profiler data in different situations that I collected.
    1)VSync off. TargetFrameRate = 1000. maxQueuedFrame = standard. QualitySetting = low;
    upload_2023-3-1_12-39-44.png

    2)VSync off. TargetFrameRate = 60. maxQueuedFrame = standard. QualitySetting = low;
    upload_2023-3-1_12-40-4.png

    3)VSync off. TargetFrameRate = 30. maxQueuedFrame = standard. QualitySetting = low;
    upload_2023-3-1_12-40-48.png

    4)VSync ON (count = 1, monitor refresh rate is 60Hz). TargetFrameRate = 1000. maxQueuedFrame = standard. QualitySetting = low;
    upload_2023-3-1_12-41-18.png

    5)VSync ON (count = 2). TargetFrameRate = 1000. maxQueuedFrame = standard. QualitySetting = low;
    upload_2023-3-1_12-41-53.png
     
    Last edited: Mar 1, 2023
  21. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,645
    Check your power management/driver settings. It's probably one of those throttling the game.
     
  22. StefanWo

    StefanWo

    Joined:
    May 24, 2015
    Posts:
    120
    Same issue as Lynx7 i have here. I switched
    from Unity 2021 URP Deferred
    to Unity 2022 URP Forward+
    didn't change more things. It results in huge waiting times. Strange part is that it looks like it just starts on a resolution after Full.

    FullHD 80fps
    A bit larger: 4fps with the WaitOnSwapChain

    I checked my power management, everything is on max as far as i see.
    Target Framerate and vscny seems to have no effect.

    I tried to switch back to deferred rendering, but there its better, but similar. The fps with the same scene dropped between these to unity versions from 100fps to 50fps. No idea what i made wrong.

    I tried to debug the frame to see what's going on, but as soon as i enable the frame debugger, the problem disappeared. It than has the >100fps as i expect from the main menu scene, even when there are screenshots taken. So its clearly not the GPU causing the problem.

    Update:
    I think the problem were realtime reflection nodes, in the old version the render pipeline was not supporting this. I saw a big amount of draw calls for the reflection nodes (so 20 for mainscene and 200 for the reflection node), but i was not able to see these drawcalls in the frame debugger.
     
    Last edited: Apr 5, 2023
  23. Carlos9x

    Carlos9x

    Joined:
    Aug 5, 2017
    Posts:
    12
    Same issue, I've tested with an empty scene (delete all camera, lighting, images) on a standalone build. It still takes 40% GPU (Check on TaskManager).

    I see "DXGI.WaitOnSwapChain" on CPU Usage Profiler and "Device.Present" on GPU Usage Profiler.

    I've disabled vsync (vsync count = 0)

    Unity Version: 2022.2.13f1
     
    Last edited: Apr 16, 2023
  24. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,645
    Well, that will make your game run as fast as it can. How many frames per second are you pushing? :)
     
  25. Carlos9x

    Carlos9x

    Joined:
    Aug 5, 2017
    Posts:
    12
    It is about 450 fps :)
     
  26. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,645
    Looks like it's working as intended then!
     
  27. Carlos9x

    Carlos9x

    Joined:
    Aug 5, 2017
    Posts:
    12
    But why it takes 40% GPU though it is an empty scene? I still don't understand why is it like that?

    Is there anyway to decrease the GPU usage? I need to test performance of my code using Measure.Frames() but the measurement is incorrect in frames containing DXGI.WaitOnSwapChain
     
    Last edited: Apr 18, 2023
  28. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,645
    You configured your project to run as fast as it can, so it runs as fast as it can using as much resources as possible. It being an empty scene is irrelevant. GPU usage is a function of frame rate and time it takes to render the scene on the GPU. If you are running at 450 fps, and it takes 0.89 ms (0.00089 s) to render the empty scene, then your GPU usage will be 450 * 0.00089 = 40%. To reduce it, you can either reduce the time it takes to render the scene (by reducing resolution or reducing scene complexity, but the latter isn't applicable if you are using an empty scene) or by reducing frame rate.

    I don't understand what you're trying to measure. Waiting for GPU to finish rendering is a normal operation in any kind of game. It is part of overall game performance.
     
    Carlos9x likes this.
  29. Carlos9x

    Carlos9x

    Joined:
    Aug 5, 2017
    Posts:
    12
    That makes sense. Thanks!
    I just want to measure the average CPU usage performance per frame (not include GPU) :)
     
  30. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,645
    Then just change your game resolution to 400x300 or something similar that the GPU would have no problems chewing through.
     
  31. Daxten

    Daxten

    Joined:
    Sep 20, 2013
    Posts:
    30
    Hi, I think I still have this issue on 2022.3.4f1 with HDRP 14.0.8

    upload_2023-7-30_15-39-54.png

    Is this correct behaviour? In my case it "only" kills .1% frame performance
     
  32. MikitaRyzhkouski

    MikitaRyzhkouski

    Joined:
    Jun 16, 2022
    Posts:
    8
    Hi. Have the same problem. I'm using Unity 2021.3.33f1 HDRP 12.1.13, Direct X 11
    Something like this at some parts of the level (for 1 frame only):
    upload_2024-1-19_21-11-52.png
    upload_2024-1-19_21-11-29.png

    The same in the Editor and Build. FPS drops at the same places of my Scene, but only for first time per run. Tryed to disable / enable almost everything, result is the same. What can it be?
     
    Last edited: Jan 19, 2024
  33. MikitaRyzhkouski

    MikitaRyzhkouski

    Joined:
    Jun 16, 2022
    Posts:
    8
    So, I guess the problem is in the Batching. I made a trick with a second camera, and teleport it through the level with preloader overlay. Now all FPS drops are concentraited under loading screen. upload_2024-1-20_18-31-40.png
     
  34. unity_OwsoyU3laIykcQ

    unity_OwsoyU3laIykcQ

    Joined:
    Dec 9, 2019
    Posts:
    3
    We have a same issue.

    In editor we have 100+fps

    In build 5-10 constantly.

    For some reason this error only occurs on notebooks. Tested on 3 different laptops: 30s 20s and 10 and Nvidia series.

    Tested with these settings and consistently got this result:

    maxQueuedFrame =2
    Vsync =0 / 1
    Unity version:
    2023.2.* /HDRP
     
  35. zwave00

    zwave00

    Joined:
    Jan 28, 2023
    Posts:
    3
    Coming back to this thread since I see nobody fixed the issue. I'm having the same problem on 2022.3.8f1. WaitOnSwapChain takes too much and the game freezes every few seconds. I have the same result with Vsync ON or OFF on URP. Anyone got any idea?

    EDIT: I forgot to say this is only in the build of the game. In the editor, it does not appear anymore and it freezes rarely, but when it does I get dont get WaitOnSwapChain anymore and I get Semaphore.WaitForSignal. But it has the same freeze effect.
    Also my GPU is on 80% usage in windows when playing the game and I can hear the fans going, I belive it is not normal but my game has good graphics indeed and I have 3.5M tris loaded most of the time
     

    Attached Files:

    Last edited: Mar 19, 2024