Skip to content

Commit

Permalink
Run all of Starfield's Steam callbacks on its main thread
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaldaien committed Sep 28, 2023
1 parent 9fe0258 commit 97de623
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 8 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
23.9.29
23.9.29.1
=========
+ Run all of Starfield's Steam callbacks on its main thread

23.9.29
=======
+ Added DLSS-G detection for all D3D12 games; false positives may happen
when using menus in some games that switch DLSS-G off in menus.
Expand Down
4 changes: 2 additions & 2 deletions include/SpecialK/DLL_VERSION.H
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#define SK_YEAR 23
#define SK_MONTH 9
#define SK_DATE 29
#define SK_REV_N 0
#define SK_REV 0
#define SK_REV_N 1
#define SK_REV 1

#ifndef _A2
#define _A2(a) #a
Expand Down
4 changes: 4 additions & 0 deletions include/SpecialK/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -1387,6 +1387,10 @@ SK_GAME_ID
__stdcall
SK_GetCurrentGameID (void);

bool
__stdcall
SK_IsCurrentGame (SK_GAME_ID game_id);

const wchar_t*
__stdcall
SK_GetConfigPath (void);
Expand Down
8 changes: 8 additions & 0 deletions src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ UINT SK_RecursiveMove ( const wchar_t* wszOrigDir,
const wchar_t* wszDestDir,
bool replace );

bool
__stdcall
SK_IsCurrentGame (SK_GAME_ID game_id)
{
return
SK_GetCurrentGameID () == game_id;
}

SK_GAME_ID
__stdcall
SK_GetCurrentGameID (void)
Expand Down
1 change: 0 additions & 1 deletion src/diagnostics/load_library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,6 @@ SK_LoadLibrary_IsPinnable (const _T* pStr)
SK_TEXT ("XAudio2_9"),

SK_TEXT ("nvapi"), SK_TEXT ("NvCameraAllowlisting"),
SK_TEXT ("nvofapi"), // Leave optical flow loaded, some DLSS-G games re-init


SK_TEXT ("kbd"), // Keyboard Layouts take > ~20 ms to load, leave 'em loaded
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/bethesda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -908,8 +908,8 @@ SK_SF_SetThreadPriorityHook ( HANDLE hThread,
{
SK_SetThreadPriorityBoost (hThread, FALSE);

if (SK_Thread_GetCurrentPriority () > nPriority)
return TRUE;
//if (SK_Thread_GetCurrentPriority () > nPriority)
// return TRUE;

return
SetThreadPriority_Original (hThread, nPriority);
Expand Down
2 changes: 1 addition & 1 deletion src/render/reflex/reflex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ SK_RenderBackend_V2::driverSleepNV (int site)
if (__target_fps > 10.0f)
{
config.nvidia.reflex.frame_interval_us =
(UINT)(1000000.0 / __target_fps) + ( __SK_ForceDLSSGPacing ? 3
(UINT)(1000000.0 / __target_fps) + ( __SK_ForceDLSSGPacing ? 24
: 0 );
}
}
Expand Down
41 changes: 41 additions & 0 deletions src/scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1259,6 +1259,42 @@ Sleep_Detour (DWORD dwMilliseconds)
SleepEx_Detour (dwMilliseconds, FALSE);
}

using SleepConditionVariableSRW_pfn = BOOL (WINAPI *)(
_Inout_ PCONDITION_VARIABLE ConditionVariable,
_Inout_ PSRWLOCK SRWLock,
_In_ DWORD dwMilliseconds,
_In_ ULONG Flags
);

static SleepConditionVariableSRW_pfn
SleepConditionVariableSRW_Original = nullptr;

BOOL
WINAPI
SleepConditionVariableSRW_Detour (
_Inout_ PCONDITION_VARIABLE ConditionVariable,
_Inout_ PSRWLOCK SRWLock,
_In_ DWORD dwMilliseconds,
_In_ ULONG Flags )
{
//SK_LOG_FIRST_CALL

#if 0
if (SK_IsCurrentGame (SK_GAME_ID::Starfield))
{
SK_LOGs0 ( L"Scheduler ",
L"SleepConditionVariableSRW (..., ..., %d, %x) - %ws",
dwMilliseconds, Flags,
SK_Thread_GetName (SK_GetCurrentThreadId ()).c_str () );
}
#endif

return
SleepConditionVariableSRW_Original (
ConditionVariable, SRWLock, dwMilliseconds, Flags
);
}

BOOL
WINAPI
QueryPerformanceFrequency_Detour (_Out_ LARGE_INTEGER *lpPerfFreq) noexcept
Expand Down Expand Up @@ -1618,6 +1654,11 @@ void SK_Scheduler_Init (void)
SleepEx_Detour,
static_cast_p2p <void> (&SleepEx_Original) );

SK_CreateDLLHook2 ( L"Kernel32",
"SleepConditionVariableSRW",
SleepConditionVariableSRW_Detour,
static_cast_p2p <void> (&SleepConditionVariableSRW_Original) );

SK_CreateDLLHook2 ( L"Kernel32",
"SwitchToThread",
SwitchToThread_Detour,
Expand Down
9 changes: 9 additions & 0 deletions src/steam/steam_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2782,6 +2782,15 @@ SK_Steam_DrawOSD (void)
bool
SK_Steam_ShouldThrottleCallbacks (void)
{
static const bool
bOnlyRunOnMainThread = SK_IsCurrentGame (SK_GAME_ID::Starfield);
if (bOnlyRunOnMainThread)
{
// Eliminate this call; main thread will pick up the slack
if (SK_GetCurrentThreadId () != SK_GetMainThreadID ())
return true;
}

if (ReadAcquire (&__SK_Steam_init))
{
InterlockedIncrement64 (&SK_SteamAPI_CallbackRunCount);
Expand Down
6 changes: 5 additions & 1 deletion src/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,11 @@ SK_GetRenderThreadID (void)
DWORD
SK_GetMainThreadID (void)
{
static DWORD tid = 0;

if (tid != 0)
return tid;

SK_AutoHandle hThreadSnapshot (
CreateToolhelp32Snapshot (TH32CS_SNAPTHREAD, 0)
);
Expand All @@ -1141,7 +1146,6 @@ SK_GetMainThreadID (void)
tent = { };
tent.dwSize = sizeof (THREADENTRY32);

DWORD tid = 0;
DWORD pid = GetCurrentProcessId ();

if (Thread32First (hThreadSnapshot, &tent))
Expand Down

0 comments on commit 97de623

Please sign in to comment.