mirror of
https://github.com/narzoul/DDrawCompat
synced 2024-12-30 08:55:36 +01:00
Attempt to mitigate thread scheduling issues caused by busy waiting
Fixes black intro videos in Star Wars Rebellion (issue #22) and performance issues in The Longest Journey.
This commit is contained in:
parent
32bcf16dc8
commit
ec7a40333e
@ -227,6 +227,8 @@
|
||||
<ClInclude Include="Win32\Log.h" />
|
||||
<ClInclude Include="Win32\MsgHooks.h" />
|
||||
<ClInclude Include="Win32\Registry.h" />
|
||||
<ClInclude Include="Win32\TimeFunctions.h" />
|
||||
<ClInclude Include="Win32\WaitFunctions.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="Common\Log.cpp" />
|
||||
@ -293,6 +295,8 @@
|
||||
<ClCompile Include="Win32\Log.cpp" />
|
||||
<ClCompile Include="Win32\MsgHooks.cpp" />
|
||||
<ClCompile Include="Win32\Registry.cpp" />
|
||||
<ClCompile Include="Win32\TimeFunctions.cpp" />
|
||||
<ClCompile Include="Win32\WaitFunctions.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="Dll\DDrawCompat.def" />
|
||||
|
@ -363,6 +363,12 @@
|
||||
<ClInclude Include="DDraw\Log.h">
|
||||
<Filter>Header Files\DDraw</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Win32\WaitFunctions.h">
|
||||
<Filter>Header Files\Win32</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Win32\TimeFunctions.h">
|
||||
<Filter>Header Files\Win32</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="Gdi\Gdi.cpp">
|
||||
@ -557,6 +563,12 @@
|
||||
<ClCompile Include="DDraw\Log.cpp">
|
||||
<Filter>Source Files\DDraw</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Win32\WaitFunctions.cpp">
|
||||
<Filter>Source Files\Win32</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Win32\TimeFunctions.cpp">
|
||||
<Filter>Source Files\Win32</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="Dll\DDrawCompat.def">
|
||||
|
@ -18,6 +18,8 @@
|
||||
#include <Win32/FontSmoothing.h>
|
||||
#include <Win32/MsgHooks.h>
|
||||
#include <Win32/Registry.h>
|
||||
#include <Win32/TimeFunctions.h>
|
||||
#include <Win32/WaitFunctions.h>
|
||||
|
||||
HRESULT WINAPI SetAppCompatData(DWORD, DWORD);
|
||||
|
||||
@ -30,7 +32,6 @@ namespace
|
||||
static bool isAlreadyInstalled = false;
|
||||
if (!isAlreadyInstalled)
|
||||
{
|
||||
SetProcessDPIAware();
|
||||
Win32::DisplayMode::disableDwm8And16BitMitigation();
|
||||
Compat::Log() << "Installing registry hooks";
|
||||
Win32::Registry::installHooks();
|
||||
@ -38,6 +39,8 @@ namespace
|
||||
D3dDdi::installHooks(g_origDDrawModule);
|
||||
Compat::Log() << "Installing display mode hooks";
|
||||
Win32::DisplayMode::installHooks();
|
||||
Win32::TimeFunctions::installHooks();
|
||||
Win32::WaitFunctions::installHooks();
|
||||
Gdi::VirtualScreen::init();
|
||||
|
||||
CompatPtr<IDirectDraw> dd;
|
||||
@ -138,6 +141,7 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
|
||||
SetProcessPriorityBoost(GetCurrentProcess(), disablePriorityBoost);
|
||||
SetProcessAffinityMask(GetCurrentProcess(), 1);
|
||||
timeBeginPeriod(1);
|
||||
SetProcessDPIAware();
|
||||
SetThemeAppProperties(0);
|
||||
|
||||
Compat::redirectIatHooks("ddraw.dll", "DirectDrawCreate",
|
||||
|
37
DDrawCompat/Win32/TimeFunctions.cpp
Normal file
37
DDrawCompat/Win32/TimeFunctions.cpp
Normal file
@ -0,0 +1,37 @@
|
||||
#include <Windows.h>
|
||||
#include <timeapi.h>
|
||||
|
||||
#include <Common/Hook.h>
|
||||
#include <Win32/TimeFunctions.h>
|
||||
|
||||
namespace
|
||||
{
|
||||
typedef DWORD(WINAPI* TimeFunc)();
|
||||
|
||||
template <TimeFunc timeFunc>
|
||||
DWORD WINAPI getTime()
|
||||
{
|
||||
thread_local DWORD prevTime = 0;
|
||||
auto origTimeFunc = Compat::getOrigFuncPtr<TimeFunc, timeFunc>();
|
||||
DWORD time = origTimeFunc();
|
||||
if (prevTime == time)
|
||||
{
|
||||
SwitchToThread();
|
||||
time = origTimeFunc();
|
||||
}
|
||||
prevTime = time;
|
||||
return time;
|
||||
}
|
||||
}
|
||||
|
||||
namespace Win32
|
||||
{
|
||||
namespace TimeFunctions
|
||||
{
|
||||
void installHooks()
|
||||
{
|
||||
HOOK_FUNCTION(kernel32, GetTickCount, getTime<&GetTickCount>);
|
||||
HOOK_FUNCTION(winmm, timeGetTime, getTime<&timeGetTime>);
|
||||
}
|
||||
}
|
||||
}
|
9
DDrawCompat/Win32/TimeFunctions.h
Normal file
9
DDrawCompat/Win32/TimeFunctions.h
Normal file
@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
namespace Win32
|
||||
{
|
||||
namespace TimeFunctions
|
||||
{
|
||||
void installHooks();
|
||||
}
|
||||
}
|
66
DDrawCompat/Win32/WaitFunctions.cpp
Normal file
66
DDrawCompat/Win32/WaitFunctions.cpp
Normal file
@ -0,0 +1,66 @@
|
||||
#include <Windows.h>
|
||||
|
||||
#include <Config/Config.h>
|
||||
#include <Common/Hook.h>
|
||||
#include <Win32/WaitFunctions.h>
|
||||
|
||||
namespace
|
||||
{
|
||||
DWORD mitigateBusyWaiting(DWORD dwMilliseconds, DWORD waitResult)
|
||||
{
|
||||
if (0 == dwMilliseconds && WAIT_TIMEOUT == waitResult)
|
||||
{
|
||||
SwitchToThread();
|
||||
}
|
||||
return waitResult;
|
||||
}
|
||||
|
||||
DWORD WINAPI msgWaitForMultipleObjects(
|
||||
DWORD nCount, const HANDLE* pHandles, BOOL fWaitAll, DWORD dwMilliseconds, DWORD dwWakeMask)
|
||||
{
|
||||
return mitigateBusyWaiting(dwMilliseconds, CALL_ORIG_FUNC(MsgWaitForMultipleObjects)(
|
||||
nCount, pHandles, fWaitAll, dwMilliseconds, dwWakeMask));
|
||||
}
|
||||
|
||||
DWORD WINAPI msgWaitForMultipleObjectsEx(
|
||||
DWORD nCount, const HANDLE* pHandles, DWORD dwMilliseconds, DWORD dwWakeMask, DWORD dwFlags)
|
||||
{
|
||||
return mitigateBusyWaiting(dwMilliseconds, CALL_ORIG_FUNC(MsgWaitForMultipleObjectsEx)(
|
||||
nCount, pHandles, dwMilliseconds, dwWakeMask, dwFlags));
|
||||
}
|
||||
|
||||
DWORD WINAPI signalObjectAndWait(
|
||||
HANDLE hObjectToSignal, HANDLE hObjectToWaitOn, DWORD dwMilliseconds, BOOL bAlertable)
|
||||
{
|
||||
return mitigateBusyWaiting(dwMilliseconds, CALL_ORIG_FUNC(SignalObjectAndWait)(
|
||||
hObjectToSignal, hObjectToWaitOn, dwMilliseconds, bAlertable));
|
||||
}
|
||||
|
||||
DWORD WINAPI waitForSingleObjectEx(HANDLE hHandle, DWORD dwMilliseconds, BOOL bAlertable)
|
||||
{
|
||||
return mitigateBusyWaiting(dwMilliseconds, CALL_ORIG_FUNC(WaitForSingleObjectEx)(
|
||||
hHandle, dwMilliseconds, bAlertable));
|
||||
}
|
||||
|
||||
DWORD WINAPI waitForMultipleObjectsEx(
|
||||
DWORD nCount, const HANDLE* lpHandles, BOOL bWaitAll, DWORD dwMilliseconds, BOOL bAlertable)
|
||||
{
|
||||
return mitigateBusyWaiting(dwMilliseconds, CALL_ORIG_FUNC(WaitForMultipleObjectsEx)(
|
||||
nCount, lpHandles, bWaitAll, dwMilliseconds, bAlertable));
|
||||
}
|
||||
}
|
||||
|
||||
namespace Win32
|
||||
{
|
||||
namespace WaitFunctions
|
||||
{
|
||||
void installHooks()
|
||||
{
|
||||
HOOK_FUNCTION(user32, MsgWaitForMultipleObjects, msgWaitForMultipleObjects);
|
||||
HOOK_FUNCTION(user32, MsgWaitForMultipleObjectsEx, msgWaitForMultipleObjectsEx);
|
||||
HOOK_FUNCTION(kernel32, SignalObjectAndWait, signalObjectAndWait);
|
||||
HOOK_FUNCTION(kernel32, WaitForSingleObjectEx, waitForSingleObjectEx);
|
||||
HOOK_FUNCTION(kernel32, WaitForMultipleObjectsEx, waitForMultipleObjectsEx);
|
||||
}
|
||||
}
|
||||
}
|
9
DDrawCompat/Win32/WaitFunctions.h
Normal file
9
DDrawCompat/Win32/WaitFunctions.h
Normal file
@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
namespace Win32
|
||||
{
|
||||
namespace WaitFunctions
|
||||
{
|
||||
void installHooks();
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user