Bläddra i källkod

Spinwait using a dummy calculation to get accurate frame limiting.

Lasse Öörni 14 år sedan
förälder
incheckning
f4650362c0
2 ändrade filer med 10 tillägg och 2 borttagningar
  1. 8 2
      Engine/Engine/Engine.cpp
  2. 2 0
      Engine/Engine/Engine.h

+ 8 - 2
Engine/Engine/Engine.cpp

@@ -426,12 +426,18 @@ void Engine::ApplyFrameLimit()
         for (;;)
         {
             timeAcc += frameTimer_.GetUSec(true);
-            // Sleep if more than 1 ms off the frame limiting goal
-            if (targetMax - timeAcc > 1000LL)
+            // Sleep if 1 ms or more off the frame limiting goal
+            if (targetMax - timeAcc >= 1000LL)
             {
                 unsigned sleepTime = (unsigned)((targetMax - timeAcc) / 1000LL);
                 Time::Sleep(sleepTime);
             }
+            else if (targetMax - timeAcc >= 20LL)
+            {
+                // Hack: calling QueryPerformanceCounter() too often leads to lost time. Perform a dummy calculation instead
+                for (int i = 0; i < 4096; ++i)
+                    waitResult_ = SDBMHash(waitResult_, i & 255);
+            }
             else
                 break;
         }

+ 2 - 0
Engine/Engine/Engine.h

@@ -100,6 +100,8 @@ private:
     unsigned maxFps_;
     /// Maximum frames per second when the application is inactive.
     unsigned maxInactiveFps_;
+    /// Dummy calculation result for frame limiter spinwait loop.
+    unsigned waitResult_;
     /// Initialized flag.
     bool initialized_;
     /// Exiting flag.