Hack VSync: use sleep

This commit is contained in:
wwylele 2016-09-04 19:41:44 +08:00
parent 55375a91b1
commit fd7d698ee0
2 changed files with 16 additions and 1 deletions

View File

@ -276,7 +276,7 @@ void GRenderWindow::InitRenderTarget() {
QGLFormat fmt; QGLFormat fmt;
fmt.setVersion(3, 3); fmt.setVersion(3, 3);
fmt.setProfile(QGLFormat::CoreProfile); fmt.setProfile(QGLFormat::CoreProfile);
fmt.setSwapInterval(Settings::values.use_vsync); fmt.setSwapInterval(false);
// Requests a forward-compatible context, which is required to get a 3.2+ context on OS X // Requests a forward-compatible context, which is required to get a 3.2+ context on OS X
fmt.setOption(QGL::NoDeprecatedFunctions); fmt.setOption(QGL::NoDeprecatedFunctions);

View File

@ -2,8 +2,10 @@
// Licensed under GPLv2 or any later version // Licensed under GPLv2 or any later version
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include <chrono>
#include <cstring> #include <cstring>
#include <numeric> #include <numeric>
#include <thread>
#include <type_traits> #include <type_traits>
#include "common/color.h" #include "common/color.h"
@ -411,6 +413,19 @@ static void VBlankCallback(u64 userdata, int cycles_late) {
last_skip_frame = g_skip_frame; last_skip_frame = g_skip_frame;
g_skip_frame = (frame_count & Settings::values.frame_skip) != 0; g_skip_frame = (frame_count & Settings::values.frame_skip) != 0;
if (Settings::values.use_vsync) {
using namespace std::chrono;
static auto frame_time = high_resolution_clock::now();
static constexpr auto update_duration = duration_cast<high_resolution_clock::duration>(16667us);
auto now = high_resolution_clock::now();
if (now < frame_time) {
std::this_thread::sleep_until(frame_time);
frame_time += update_duration;
} else {
frame_time = now + update_duration;
}
}
// Swap buffers based on the frameskip mode, which is a little bit tricky. When // Swap buffers based on the frameskip mode, which is a little bit tricky. When
// a frame is being skipped, nothing is being rendered to the internal framebuffer(s). // a frame is being skipped, nothing is being rendered to the internal framebuffer(s).
// So, we should only swap frames if the last frame was rendered. The rules are: // So, we should only swap frames if the last frame was rendered. The rules are: