Prevent frame-rate dependant games from running too fast

This commit is contained in:
Luke Usher 2018-03-06 09:15:04 +00:00
parent 779be6c823
commit 65dfda6e2e
11 changed files with 173 additions and 137 deletions

Binary file not shown.

Binary file not shown.

View File

@ -40,7 +40,7 @@
// ******************************************************************
// * func: XBVideo::XBVideo
// ******************************************************************
XBVideo::XBVideo() : m_bVSync(true), m_bFullscreen(false), m_bHardwareYUV(false)
XBVideo::XBVideo() : m_bVSync(false), m_bFullscreen(false), m_bHardwareYUV(false)
{
strcpy(m_szVideoResolution, "Automatic (Default)");
}

View File

@ -1,6 +1,6 @@
//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include file.
// Used by C:\Work\Cxbx-Reloaded\resource\Cxbx.rc
// Used by C:\Users\Luke\Desktop\Projects\Cxbx-Reloaded\resource\Cxbx.rc
//
#define IDI_CXBX 101
#define IDB_SPLASH 102
@ -109,6 +109,7 @@
#define ID_SETTINGS_INITIALIZE 40091
#define ID_EMULATION_STARTDEBUGGER 40092
#define ID_FPS 40096
#define ID_HACKS_UNCAPFRAMERATE 40097
#define IDC_STATIC -1
// Next default values for new objects

View File

@ -180,6 +180,12 @@ WndMain::WndMain(HINSTANCE x_hInstance) :
m_DisablePixelShaders = 0;
}
dwType = REG_DWORD; dwSize = sizeof(DWORD);
result = RegQueryValueEx(hKey, "HackUncapFrameRate", NULL, &dwType, (PBYTE)&m_UncapFramerate, &dwSize);
if (result != ERROR_SUCCESS) {
m_UncapFramerate = 0;
}
dwType = REG_DWORD; dwSize = sizeof(DWORD);
result = RegQueryValueEx(hKey, "CxbxDebug", NULL, &dwType, (PBYTE)&m_CxbxDebug, &dwSize);
if (result != ERROR_SUCCESS) {
@ -1296,6 +1302,12 @@ LRESULT CALLBACK WndMain::WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lP
RefreshMenus();
break;
case ID_HACKS_UNCAPFRAMERATE:
m_UncapFramerate = !m_UncapFramerate;
RefreshMenus();
break;
case ID_HELP_ABOUT:
{
ShowAboutDialog(hwnd);
@ -1715,6 +1727,9 @@ void WndMain::RefreshMenus()
chk_flag = (m_DisablePixelShaders) ? MF_CHECKED : MF_UNCHECKED;
CheckMenuItem(settings_menu, ID_HACKS_DISABLEPIXELSHADERS, chk_flag);
chk_flag = (m_UncapFramerate) ? MF_CHECKED : MF_UNCHECKED;
CheckMenuItem(settings_menu, ID_HACKS_UNCAPFRAMERATE, chk_flag);
}
// emulation menu
@ -2070,6 +2085,7 @@ void WndMain::StartEmulation(HWND hwndParent, DebuggerState LocalDebuggerState /
// register Hacks with emulator process
g_EmuShared->SetDisablePixelShaders(&m_DisablePixelShaders);
g_EmuShared->SetUncapFramerate(&m_UncapFramerate);
// shell exe
{

View File

@ -212,6 +212,7 @@ class WndMain : public Wnd
// * Hack Flags
// ******************************************************************
int m_DisablePixelShaders;
int m_UncapFramerate;
// ******************************************************************
// * debug output filenames

View File

@ -490,6 +490,7 @@ void PrintCurrentConfigurationLog()
{
printf("--------------------------- HACKS CONFIG ---------------------------\n");
printf("Disable Pixel Shaders: %s\n", g_DisablePixelShaders == 1 ? "On" : "Off");
printf("Uncap Framerate: %s\n", g_UncapFramerate == 1 ? "On" : "Off");
}
printf("------------------------- END OF CONFIG LOG ------------------------\n");
@ -939,6 +940,8 @@ __declspec(noreturn) void CxbxKrnlInit
int HackEnabled = 0;
g_EmuShared->GetDisablePixelShaders(&HackEnabled);
g_DisablePixelShaders = !!HackEnabled;
g_EmuShared->GetUncapFramerate(&HackEnabled);
g_UncapFramerate = !!HackEnabled;
}
#ifdef _DEBUG_PRINT_CURRENT_CONF

View File

@ -65,6 +65,7 @@ volatile bool g_bEmuSuspended = false;
volatile bool g_bPrintfOn = true;
bool g_XInputEnabled = false;
bool g_DisablePixelShaders = false;
bool g_UncapFramerate = false;
// Delta added to host SystemTime, used in xboxkrnl::KeQuerySystemTime and xboxkrnl::NtSetSystemTime
LARGE_INTEGER HostSystemTimeDelta = {};

View File

@ -108,5 +108,5 @@ typedef struct DUMMY_KERNEL
extern bool g_DisablePixelShaders;
extern bool g_UncapFramerate;
#endif

View File

@ -3982,13 +3982,15 @@ VOID WINAPI XTL::EMUPATCH(D3DDevice_Present)
EMUPATCH(D3DDevice_Swap)(CXBX_SWAP_PRESENT_FORWARD); // Xbox present ignores
}
std::chrono::time_point<std::chrono::high_resolution_clock> frameStartTime;
// ******************************************************************
// * patch: D3DDevice_Swap
// ******************************************************************
DWORD WINAPI XTL::EMUPATCH(D3DDevice_Swap)
(
DWORD Flags
)
)
{
FUNC_EXPORTS
@ -3997,22 +3999,31 @@ DWORD WINAPI XTL::EMUPATCH(D3DDevice_Swap)
static clock_t lastDrawFunctionCallTime = 0;
// TODO: Ensure this flag is always the same across library versions
if(Flags != 0)
if (Flags != 0)
if (Flags != CXBX_SWAP_PRESENT_FORWARD) // Avoid a warning when forwarded
EmuWarning("XTL::EmuD3DDevice_Swap: Flags != 0");
CxbxReleaseBackBufferLock();
// TODO: Make a video option to wait for VBlank before calling Present.
// Makes syncing to 30fps easier (which is the native frame rate for Azurik
// and Halo).
// g_pDD7->WaitForVerticalBlank( DDWAITVB_BLOCKEND, NULL );
// g_pDD7->WaitForVerticalBlank( DDWAITVB_BLOCKEND, NULL );
clock_t currentDrawFunctionCallTime = clock();
HRESULT hRet = g_pD3DDevice8->Present(0, 0, 0, 0);
DEBUG_D3DRESULT(hRet, "g_pD3DDevice8->Present");
if (!g_UncapFramerate) {
// If the last frame completed faster than the Xbox VBlank period, wait for it
// TODO: Read the frame rate target from the Xbox display mode
// See comments in GetNextVblankTime();
auto targetDuration = 16.6666666667ms;
while (std::chrono::high_resolution_clock::now() - frameStartTime < targetDuration) {
// We use an empty while loop because actually sleeping is too unstable
// Sleeping causes the frame duration to jitter...
;
}
frameStartTime = std::chrono::high_resolution_clock::now();
}
g_DeltaTime += currentDrawFunctionCallTime - lastDrawFunctionCallTime;
lastDrawFunctionCallTime = currentDrawFunctionCallTime;
g_Frames++;

View File

@ -124,6 +124,8 @@ class EmuShared : public Mutex
// ******************************************************************
void GetDisablePixelShaders(int* value) { Lock(); *value = m_DisablePixelShaders; Unlock(); }
void SetDisablePixelShaders(int* value) { Lock(); m_DisablePixelShaders = *value; Unlock(); }
void GetUncapFramerate(int* value) { Lock(); *value = m_UncapFramerate; Unlock(); }
void SetUncapFramerate(int* value) { Lock(); m_UncapFramerate = *value; Unlock(); }
// ******************************************************************
// * MSpF/Benchmark values Accessors
@ -196,6 +198,7 @@ class EmuShared : public Mutex
int m_FlagsLLE;
int m_XInputEnabled;
int m_DisablePixelShaders;
int m_UncapFramerate;
float m_MSpF;
float m_FPS;
bool m_bKeQuickReboot;