Skip to content

Commit 56ad420

Browse files
committed
Miscellaneous optimization... and removed isnormal (...) checks from frame pacing statistics
1 parent fd08d17 commit 56ad420

8 files changed

Lines changed: 89 additions & 44 deletions

File tree

include/SpecialK/framerate.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -842,9 +842,9 @@ struct game_pacer_s {
842842
DWORD last_paced_time = 0UL;
843843
HANDLE event = 0;
844844

845-
bool isSupported (void) noexcept;
846-
bool wantPacing (void) noexcept;
847-
void signalEvent (void) noexcept;
845+
bool isSupported (ULONG64 frames_drawn = SK_GetFramesDrawn ()) noexcept;
846+
bool wantPacing (ULONG64 frames_drawn = SK_GetFramesDrawn ()) noexcept;
847+
void signalEvent (void) noexcept;
848848
} game_pace;
849849

850850

src/core.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2904,8 +2904,18 @@ SK_Inject_PostHeartbeatToSKIF (void)
29042904
// but alt-tab can still flicker.
29052905
if (hWndSKIF != game_window.hWnd && IsWindow (hWndSKIF))
29062906
{
2907-
DWORD dwPid = 0x0;
2908-
SK_GetWindowThreadProcessId (hWndSKIF, &dwPid);
2907+
static HWND hWndLast = 0;
2908+
static DWORD dwPidLast = 0;
2909+
2910+
DWORD dwPid = 0x0;
2911+
2912+
if (hWndLast == hWndSKIF)
2913+
dwPid = dwPidLast;
2914+
else
2915+
{
2916+
SK_GetWindowThreadProcessId (hWndSKIF, &dwPid);
2917+
hWndLast = hWndSKIF; dwPidLast = dwPid;
2918+
}
29092919

29102920
if ( dwPid != 0x0 &&
29112921
dwPid != GetCurrentProcessId () )
@@ -4730,13 +4740,11 @@ SK_EndBufferSwap (HRESULT hr, IUnknown* device, SK_TLS* pTLS)
47304740
if ( config.render.framerate.present_interval == 0 &&
47314741
config.render.framerate.target_fps > 0.0f )
47324742
{
4733-
SK_AutoHandle hTimer (
4734-
INVALID_HANDLE_VALUE
4735-
);
4743+
static thread_local HANDLE hTimer = (HANDLE)-1;
47364744

47374745
SK_Framerate_WaitUntilQPC (
47384746
qpcTimeOfSwap.QuadPart + __SK_LatentSyncPostDelay,
4739-
hTimer.m_h );
4747+
hTimer );
47404748
}
47414749
}
47424750

src/framerate.cpp

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2162,6 +2162,13 @@ SK_Framerate_SanitizeTimerResolution (void) noexcept
21622162
if (! config.render.framerate.max_timer_resolution)
21632163
return;
21642164

2165+
// Only do this occasionally, it defeats the purpose to
2166+
// call it frequently.
2167+
static int
2168+
tick_tock = 0;
2169+
if (++tick_tock % 64 != 0)
2170+
return;
2171+
21652172
auto _SetTimerResolution =
21662173
( ZwSetTimerResolution_Original != nullptr ) ?
21672174
ZwSetTimerResolution_Original :
@@ -2211,7 +2218,7 @@ extern bool reset_frame_history;
22112218

22122219
void
22132220
SK::Framerate::Limiter::wait (void) noexcept
2214-
{
2221+
{
22152222
// This is actually counter-productive in testing... when the thread priority
22162223
// is lowered after this goes out of scope it may reschedule the thread.
22172224
//SK_Thread_ScopedPriority prio_scope (THREAD_PRIORITY_TIME_CRITICAL);
@@ -2506,6 +2513,7 @@ SK::Framerate::Limiter::wait (void) noexcept
25062513
}
25072514
}
25082515

2516+
// Trigger a timer phase shift.
25092517
SK_Framerate_SanitizeTimerResolution ();
25102518

25112519
// Create an unnamed waitable timer.
@@ -4502,7 +4510,7 @@ SK::Framerate::TickEx ( bool /*wait*/,
45024510

45034511

45044512
// Prevent inserting infinity into the dataset
4505-
if ( std::isnormal (dt) )
4513+
//if ( std::isnormal (dt) )
45064514
{
45074515
if (snapshots->frame_history.addSample (1000.0 * dt, now))
45084516
{
@@ -4597,7 +4605,7 @@ SK::Framerate::TickEx ( bool /*wait*/,
45974605
} break;
45984606
}
45994607

4600-
if (std::isnormal (sample) && now.QuadPart > 0)
4608+
if (/*std::isnormal(sample) &&*/ now.QuadPart > 0)
46014609
{
46024610
container->addSample (
46034611
sample, now
@@ -4610,25 +4618,31 @@ SK::Framerate::TickEx ( bool /*wait*/,
46104618

46114619
int sk_config_t::fps_osd_s::getTimingMethod (void)
46124620
{
4613-
if (__SK_IsDLSSGActive)
4614-
{
4615-
if (config.render.framerate.streamline.enable_native_limit)
4621+
switch (timing_method)
4622+
{
4623+
case SK_FrametimeMeasures_LimiterPacing:
46164624
{
4617-
if (__target_fps_now <= 0.0f && timing_method == SK_FrametimeMeasures_LimiterPacing)
4625+
if (__SK_IsDLSSGActive)
4626+
{
4627+
if (config.render.framerate.streamline.enable_native_limit)
4628+
{
4629+
if (__target_fps_now <= 0.0f)
4630+
{
4631+
return SK_FrametimeMeasures_NewFrameBegin;
4632+
}
4633+
}
4634+
4635+
else if (__target_fps_now > 0.0f)
4636+
{
4637+
return SK_FrametimeMeasures_PresentSubmit;
4638+
}
4639+
}
4640+
4641+
else if (__target_fps_now <= 0.0f)
46184642
{
46194643
return SK_FrametimeMeasures_NewFrameBegin;
46204644
}
4621-
}
4622-
4623-
else if (__target_fps_now > 0.0f && timing_method == SK_FrametimeMeasures_LimiterPacing)
4624-
{
4625-
return SK_FrametimeMeasures_PresentSubmit;
4626-
}
4627-
}
4628-
4629-
else if (__target_fps_now <= 0.0f && timing_method == SK_FrametimeMeasures_LimiterPacing)
4630-
{
4631-
return SK_FrametimeMeasures_NewFrameBegin;
4645+
} break;
46324646
}
46334647

46344648
return timing_method;
@@ -4860,7 +4874,7 @@ SK::Framerate::Stats::sortAndCacheFrametimeHistory (void) //noexcept
48604874
{
48614875
if (datum.when.QuadPart >= 0)
48624876
{
4863-
if (isnormal (datum.val))
4877+
//if (isnormal (datum.val))
48644878
{
48654879
kWriteBuffer.second.emplace_back (datum.val);
48664880
}
@@ -4891,6 +4905,7 @@ SK_Framerate_WaitUntilQPC (LONGLONG llQPC, HANDLE& hTimer) noexcept
48914905
if (llQPC < SK_QueryPerf ().QuadPart)
48924906
return;
48934907

4908+
// Trigger a timer phase shift.
48944909
SK_Framerate_SanitizeTimerResolution ();
48954910

48964911
if ((LONG_PTR)hTimer < 0)
@@ -5066,10 +5081,10 @@ SK_Framerate_EnergyControlPanel (void)
50665081
}
50675082

50685083
bool
5069-
game_pacer_s::wantPacing (void) noexcept
5084+
game_pacer_s::wantPacing (ULONG64 frames_drawn) noexcept
50705085
{
50715086
return
5072-
config.render.framerate.pace_game_thread && isSupported () && event != 0 && last_frame_id > SK_GetFramesDrawn () - 2;
5087+
config.render.framerate.pace_game_thread && isSupported (frames_drawn) && event != 0 && last_frame_id > frames_drawn - 2;
50735088
}
50745089

50755090
void
@@ -5080,10 +5095,10 @@ game_pacer_s::signalEvent (void) noexcept
50805095
}
50815096

50825097
bool
5083-
game_pacer_s::isSupported (void) noexcept
5098+
game_pacer_s::isSupported (ULONG64 frames_drawn) noexcept
50845099
{
50855100
static constexpr auto SK_Reflex_MinimumFramesBeforeNative = 150;
50865101

50875102
return
5088-
last_frame_id != 0 && !config.nvidia.reflex.native && SK_GetFramesDrawn () > SK_Reflex_MinimumFramesBeforeNative;
5103+
last_frame_id != 0 && !config.nvidia.reflex.native && frames_drawn > SK_Reflex_MinimumFramesBeforeNative;
50895104
}

src/plugins/unity.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2981,9 +2981,9 @@ SK_Unity_PaceGameThreadDxgi (IDXGISwapChain *pSwapChain, DXGI_FRAME_STATISTICS *
29812981
game_pace.last_frame_id =
29822982
SK_GetFramesDrawn ();
29832983

2984-
if (game_pace.wantPacing ())
2984+
if (game_pace.wantPacing (game_pace.last_frame_id))
29852985
{
2986-
static HANDLE hTimer = (HANDLE)-1;
2986+
static thread_local HANDLE hTimer = (HANDLE)-1;
29872987

29882988
auto& rb =
29892989
SK_GetCurrentRenderBackend ();

src/render/d3d11/d3d11_state_tracker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ SK_D3D11_ShouldTrackRenderOp ( ID3D11DeviceContext* pDevCtx,
201201
const auto frame_id =
202202
SK_GetFramesDrawn ();
203203

204-
if (!game_pace.wantPacing () || game_pace.last_paced_time < SK_timeGetTime ())
204+
if (!game_pace.wantPacing (frame_id) || game_pace.last_paced_time < SK::ControlPanel::current_time)
205205
{
206206
static ULONG64
207207
last_frame = 0;

src/render/dxgi/dxgi.cpp

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2144,12 +2144,14 @@ SK_ImGui_DrawD3D12 (IDXGISwapChain* This)
21442144
}
21452145
}
21462146

2147+
#ifdef USE_D3D11_GPU_WAIT
21472148
static HANDLE SK_ImGui_D3D11GPUEvent = 0;
2149+
#endif
21482150

21492151
void
21502152
SK_ImGui_WaitD3D11 (void)
21512153
{
2152-
#if 0
2154+
#ifdef USE_D3D11_GPU_WAIT
21532155
if (SK_ImGui_D3D11GPUEvent != 0)
21542156
WaitForSingleObject (SK_ImGui_D3D11GPUEvent, 100UL);
21552157
#endif
@@ -2178,15 +2180,21 @@ SK_ImGui_DrawD3D11 (IDXGISwapChain* This)
21782180
if (! pTLS)
21792181
return;
21802182

2183+
#ifdef USE_D3D11_GPU_WAIT
21812184
SK_RunOnce (
21822185
SK_ImGui_D3D11GPUEvent =
21832186
SK_CreateEvent (nullptr, FALSE, TRUE, nullptr);
21842187
);
21852188

2186-
SK_ComQIPtr <IDXGIDevice2>
2187-
pDXGIDev2 (pDev);
2188-
if (pDXGIDev2 != nullptr)
2189-
pDXGIDev2->EnqueueSetEvent (SK_ImGui_D3D11GPUEvent);
2189+
// This is currently unused... it's for a more advanced framerate limiter.
2190+
if (SK_ImGui_D3D11GPUEvent != nullptr)
2191+
{
2192+
SK_ComQIPtr <IDXGIDevice2>
2193+
pDXGIDev2 (pDev);
2194+
if (pDXGIDev2 != nullptr)
2195+
pDXGIDev2->EnqueueSetEvent (SK_ImGui_D3D11GPUEvent);
2196+
}
2197+
#endif
21902198

21912199
#define _SetupThreadContext() \
21922200
pTLS->imgui->drawing = FALSE; \
@@ -2336,8 +2344,16 @@ SK_ImGui_DrawD3D11 (IDXGISwapChain* This)
23362344
}
23372345
}
23382346

2339-
if (pDXGIDev2 != nullptr)
2340-
pDXGIDev2->EnqueueSetEvent (SK_ImGui_D3D11GPUEvent);
2347+
#ifdef USE_D3D11_GPU_WAIT
2348+
// This is currently unused... it's for a more advanced framerate limiter.
2349+
if (SK_ImGui_D3D11GPUEvent != nullptr)
2350+
{
2351+
SK_ComQIPtr <IDXGIDevice2>
2352+
pDXGIDev2 (pDev);
2353+
if (pDXGIDev2 != nullptr)
2354+
pDXGIDev2->EnqueueSetEvent (SK_ImGui_D3D11GPUEvent);
2355+
}
2356+
#endif
23412357
}
23422358

23432359
HRESULT

src/widgets/latency.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -880,8 +880,11 @@ SK_ImGui_DrawGraph_Latency (bool predraw)
880880

881881
ImGui::EndGroup ();
882882

883+
const auto frames_drawn =
884+
SK_GetFramesDrawn ();
885+
883886
if ( ReadULong64Acquire (&SK_Reflex_LastFrameMarked) <
884-
ReadULong64Acquire (&SK_RenderBackend::frames_drawn) - 2 && !game_pace.wantPacing () )
887+
frames_drawn - 2 && !game_pace.wantPacing (frames_drawn) )
885888
{
886889
static bool bRTSS64 =
887890
SK_GetModuleHandle (

src/window.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4688,9 +4688,12 @@ GetWindowInfo_Detour (HWND hwnd, PWINDOWINFO pwi)
46884688
pwi->cyWindowBorders = 0;
46894689
}
46904690

4691+
static HMODULE hModSystemDXGI =
4692+
LoadLibraryExW (L"dxgi.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32);
4693+
46914694
// DXGI checks on this during SwapChain creation...
46924695
// lie to DXGI if we have to, so that SwapChain creation succeeds.
4693-
if (StrStrIW (SK_GetCallerName ().c_str (), L"dxgi.dll"))
4696+
if (SK_IsCallingDLL (hModSystemDXGI))
46944697
{
46954698
pwi->dwExStyle &= ~WS_EX_TOPMOST;
46964699

0 commit comments

Comments
 (0)