Search Unity

gfx.waitforpresent on empty scene?

Discussion in 'Editor & General Support' started by irwit, May 19, 2017.

  1. irwit

    irwit

    Joined:
    Sep 30, 2014
    Posts:
    55
    Hi all

    upload_2017-5-19_15-30-59.png

    As you can see, gfx.waitforpresent is causing all sorts of lag in my project. I've been doing a ton of research and it seems this stuttering and lag is a long time issue. Some cause I'm sure this is NOT being caused by.
    - v sync
    - shaders
    - shadow map settings
    - shadows in general
    - the profiler running
    - garbage collection ( I'm, not 100% but the intervals for garbage collection do not align with lag spikes)


    A couple of questions, in the bottom set of graphs, the orange moves up. My mesh count jumps from 19 to 124. I have nothing in my scene to start with. Any ideas what these additional 105 objects are?

    Secondly, is there any progress with identifying the cause of these fps issues? It seems its been around for at least the last two years and I can't seem to find anyone with anything solid.

    Closest I can get to a correlation is CPU multithreading. Lots of people claim, for android, turning off multithreading helps. On my machine, I can change the amount of gfx.waitforpresent by changing how many CPUs I assign to unity, however, nothing seems to remove completely.

    My actual project is lots of slow camera pans and the stuttering looks awful! I'd really love some help on this if possible?

    Many thanks for reading

    Will.
     
  2. irwit

    irwit

    Joined:
    Sep 30, 2014
    Posts:
    55
    Hi all

    Here is another graph showing my game in the profiler.

    upload_2017-5-19_21-24-17.png

    So the spikes in the GPU usage are just the GUI update for the profilers window. The huge CPU lag spikes, im still no closer to solving. It really does kill all the image quality we work on when the camera stutters so often. So the CPU lag spike, or delay, happens either in a busy scene or an empty one.

    Below are a list of variables I have tried to find a fix.

    - limiting unity to one CPU core
    - limiting Unity to various other combinations of CPU core
    - Open GL enabled instead of DX 11
    - Maximum pre-rendered frames options within Nvidia GPU settings.
    - windows compatibility modes.,

    As, outside of my prifler update, the GPU is really consistent, I can't help but feel this is some sort of fundamental bug within the Unity editor. I've read a few places that it's a DX11 issue. I haven't managed to pinpoint that as a definite factor yet. I'll keep looking but would love to hear if anyone else has this issue or any ideas of somewhere else to look.

    Thanks again

    Will
     
  3. irwit

    irwit

    Joined:
    Sep 30, 2014
    Posts:
    55
    So something quite interesting. I changed my game to run in dx9 and got the following.

    upload_2017-5-19_22-13-7.png
    upload_2017-5-19_22-13-49.png

    Finally a corrolation between graphics card and CPU gfx.waitforpresent. This graphs makes sense. The CPU is actually waiting for a frame because its taken so long. The cause is the garbage collection. Maybe dx11, the profiler isnt spotting or detecting the garbage collection with the GPU? Who knows, maybe its completely unrelated as its a different version of DX.
     
  4. sinokgr

    sinokgr

    Joined:
    May 22, 2014
    Posts:
    14
    Hey irwit, I'm experiencing exactly the same issues here. Not sure what those spikes are and what gfx.WaitForPresent actually means. Everybody seems to have their own explanation of what this thing is. In this Unity article, it mentions that it's VSync, but even with VSync off it still appears in the profiler. Reading through some reported issues, it looks like it might be a bug?! It would be great if anyone from Unity could answer about what's going on.
     
  5. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,650
    Possibly. Because in some cases enabling "Deep Profile" causes GFX.waitForPresent to no longer be displayed in the profiler. So what really is GFX.waitForPresent? Not sure if anyone really knows (besides people from Unity)
     
  6. irwit

    irwit

    Joined:
    Sep 30, 2014
    Posts:
    55
    GFX.waitforPresent is the CPU waiting for the graphics card to deliver something. So for example, some people have had a similar issue with android builds and disabling multithread helped solve the issue giving the idea that theres some bug with unity build for android and multicore processors.

    My issue is for windows builds and I can litterly have a default scene, build, and every few seconds, my fps just nose dives no matter what I do. Ive tried various builds and get the same issue.

    More things it isnt.

    dx version
    specific gfx card, tried on duplicate 970s and a 1080gtx, last ditch would be to try an AMD?
     
  7. Gregorik

    Gregorik

    Joined:
    Dec 11, 2014
    Posts:
    366
    I'm getting the waitforPresent bug on an AMD card. Been spending quite some time trying to debug it. I'm now convinced that it's a bug, and a nasty one.
     
    mdavi16 likes this.
  8. Osirius

    Osirius

    Joined:
    Oct 10, 2014
    Posts:
    47
    I think the fact that this issue is so incredibly varied means that there's little doubt we're dealing with a bug, and specifically a DX11 bug. There doesn't seem to be a sure-fire way of getting WaitForPresent to appear (aside from enabling VSync) and the "fixes" are incredibly bizarre (like closing the inspector tab, as mentioned above, or switching to Deep Profiling, which lessens the problem for me), so I think it's safe to say it's not working as it should.

    A few random quirks from my testing of this bug:
    The problem can occur with absolutely NOTHING in the scene. Removed everything (no camera, no lights, no realtime lighting, no shadows, lowest possible quality, etc, etc.) and experienced a nice steady framerate of ~350fps (ancient laptop, same problem happens on my desktop too, but not as pronounced) with regular drops down to <20fps every few seconds. Vsync was disabled. So this problem can happen even when Unity isn't supposed to be rendering anything at all and can decimate an otherwise high, stable FPS for literally no reason (well, I'm sure there IS a reason, it's just no one knows what it is).

    So far, the only way of fixing it is to run in an older version of DirectX or run in OpenGL but that's hardly an acceptable solution, so it seems like the issue is a bug with the DX11 integration in Unity.

    TL;DR
    It's safe to say Gfx.WaitForPresent is bugged and it's specifically a DX11 bug. It happens randomly and there's no known fix aside from running in OpenGL or DX9.

    Any response Unity Staff?
     
    mdavi16 likes this.
  9. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    6,459
    On android it has also been caused by Thermal Throttling, where the gpu gets too hot and the device throttles it back, causing everything to go slower.

    I've eliminated this on a really simple project with almost no content by giving the cpu something to do, like rotate a bunch of boxes off-screen. I think the problem was that the cpu was finishing insanely fast, then waiting for the gpu to at least return "ok, theres nothing to do". Once I got the cpu to do something for a couple of ms then it seemed to disappear. In other words, it wasn't actually a problem.

    Gfx.WaitForPresent is a really high level response. I think it's easy to say its buggy, but it's probably more accurate to say that it simply needs to have more subresponses that are more specific or perhaps some contextual analysis as to why we are seeing it in the first place. But with so many causes, how could it be built in?

    Not really sure if that's something that makes sense to do or how there could be built-in ways to support doing that, though... We're pretty spoiled by Unity in the sense that this is something devs have to fix which is lower level than we're accustomed to debugging. It's really a matter of balancing being cpu/gpu bound. When there is balance, this doesn't really present itself as an issue.
     
  10. VRStudy

    VRStudy

    Joined:
    Oct 24, 2017
    Posts:
    1
    Hi,

    I've been using the profiler heavily to optimize VR work and this issue just appeared. When I went to open my build this morning I was getting huge Gfx.waitForPresent spikes which were not there the day before. I'm running on a i7-7700k, 32MB ram, GTX 1080ti, I shouldn't have frames drop from 400fps one day down to mid 40's to 50's the next. Nothing has changed in my project since yesterday, the only thing that did change was a NVidia driver update. It had an overlay popup when Unity started which was the root cause of the Gfx.waitForPresent in this instance.

    To turn it off open GeForce Experience->Settings(Gear Icon)->General->In-Game Overlay (Turn this off)

    Hope this helps someone.
     
    xiaolinl and deab like this.
  11. iivo_k

    iivo_k

    Joined:
    Jan 28, 2013
    Posts:
    312
    Getting ~30 ms Gfx.waitForPresent spikes constantly (multiple times per second) in an empty scene both in editor and builds (full screen), when G-Sync is enabled for fullscreen and windowed. Disabling G-Sync for windowed drops the spikes to 3-4 ms in the editor.

    Tried with 5.6.4p1, 2017.1.2p2 and 2017.2.0p1.
    VSync is disabled in Unity, ran with DX12, DX11, DX9, Vulkan and OpenGL Core.

    Running fully updated Windows 10 on AMD Ryzen 1600 and Geforce GTX 1070 with latest drivers (388.13) and a 165 Hz G-Sync display.
     
  12. Martin_H

    Martin_H

    Joined:
    Jul 11, 2015
    Posts:
    3,954
    You could use System.Diagnostics.Stopwatch to verify in a non-dev build without attached profiler that it still occurs under conditions like a released product. If it only happens while profiling, I'd just ignore it.
    Here are some examples for the stopwatch class.
    https://www.dotnetperls.com/stopwatch
     
  13. iivo_k

    iivo_k

    Joined:
    Jan 28, 2013
    Posts:
    312
    Actually the first thing I did was run a non-dev build and got visible stuttering. Now that I tried again it was much better, not sure what variables changed. Something's F***y with G-Sync though.
     
    Martin_H likes this.
  14. deab

    deab

    Joined:
    Aug 11, 2013
    Posts:
    83
    This was the cause of my issue, thank you!
     
  15. Gua

    Gua

    Joined:
    Oct 29, 2012
    Posts:
    356
    Thanks! So far it works. Hope this issue won't comeback.
     
  16. roointan

    roointan

    Joined:
    Jan 8, 2018
    Posts:
    22
    I'm having this similar random problem on my android device, and this was probably not because of Thermal Throttling, because I put the phone in the freezer for a few minutes and it's not gone...

    UPDATE: I enabled Vulkan, and brought it higher in the priority! and the problem is gone!
     
    Last edited: Dec 8, 2018
unityunity