Search Unity

Bug Frame Drop when game loses focus

Discussion in 'General Graphics' started by Zaddo, Apr 1, 2023.

  1. Zaddo

    Zaddo

    Joined:
    May 19, 2012
    Posts:
    78
    I am getting a huge frame drop when the game window loses focus. I am trying to run two instances of the game to test multiplayer features. This issue occurs both inside the editor and a build of the game.

    The only clue I have is from the profiler, which shows that the frame drop is due to Gfx.WaitForPresentOnGfxThread. I have no clue what this is. You can see the spikes on the right of the graph which correlates with the game window losing focus.

    I suspect this problem started with Unity 2021. I have recovered an old version of my project and it does not have this problem. I have tried to revert any other major changes to my project, eg: I had installed The Vegetation Engine. Reverting these changes did not fix the problem.

    Is there any way I can see what WaitForPresentOnGfxThread is waiting for? It is not an editor issue because the frame drop happens in a build.

    My project is URP 12.1.8, 2021.3.15f1

    Here is a video showing the problem:

    upload_2023-4-1_19-12-54.png
     
  2. Zaddo

    Zaddo

    Joined:
    May 19, 2012
    Posts:
    78
  3. c0d3_m0nk3y

    c0d3_m0nk3y

    Joined:
    Oct 21, 2021
    Posts:
    672
    Usually, this is what you want. You want the window in focus to have higher priority than the windows in the back.

    Having said that, I noticed that your frame rate is very high. You could try limiting it to 60 FPS either by enabling v-sync or setting a target frame rate. That should leave more CPU/GPU resources for the processes with lower priority.

    You could also try to disable priority boost for the process:
    https://learn.microsoft.com/en-us/d...ics.process.priorityboostenabled?view=net-7.0
    (might have to use p-invoke if this function is not available in Unities .Net version)
     
  4. AndyBarnard

    AndyBarnard

    Unity Technologies

    Joined:
    Nov 12, 2019
    Posts:
    14
    What is your Application.runInBackground setting configured to? By default it is false - apps are paused in the background. From watching the video, it looks like your app is paused when moving to the background.

    Gfx.WaitForPresentOnGfxThread means your CPU was waiting for the GPU to present the frame. This can be when there is a genuine performance problem, such as if your GPU was busy processing rendering commands. Or, it can also occur for example when the GPU was waiting for VSync, which is obviously not a performance problem. In your case, I'm presuming there is simply no frame to present as the application is paused and this is how it manifests in the Profiler on this platform. This is a useful documentation page that explains some of those Wait markers and some of the numerous permutations they can appear in: https://docs.unity3d.com/Manual/profiler-markers.html.
     
    Last edited: Apr 3, 2023
    MartinTilo and GrahamHuws like this.
  5. Zaddo

    Zaddo

    Joined:
    May 19, 2012
    Posts:
    78
    @c0d3_m0nk3y Thanks. Setting vsync worked. But then I tried turned VSync off and the problem went away. It is like toggling Vsync on and off cleared something else that was causing the problem? With VSync on, the frame rate was much more stable when switching between the two game instances. When it is off, the game in focus gets better frame rate, which as you said, is what you want.

    @AndyBarnard I put in a debug output to ensure runInBackground is on and to ensure that none of the asset store tools I have in the project was turning it off. It is definitely on. (I did pause the game to grab the screen capture). Thanks for the link to the profiler-markers, I had read through that careful when trying to figure out what was happening. I understand that WaitForPresentOnGfxThread means that the CPU was waiting for the GPU. Is there any way to see what is happening on the GPU with the profiler when WaitForPresentOnGfxThread is active?

    Here is a short video with Vsync off, without the frame drop, fixed after I toggled VSync.
     
  6. c0d3_m0nk3y

    c0d3_m0nk3y

    Joined:
    Oct 21, 2021
    Posts:
    672
    There is the GPU module in the profiler but I doubt that it will give you useful information in this case since it's unlikely that the draw calls themselves take that long.