Skip to content

Commit

Permalink
Various D3D11 state tracking optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaldaien committed Dec 27, 2024
1 parent f55e15f commit 2be3c42
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 133 deletions.
13 changes: 12 additions & 1 deletion CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
24.12.26.3
24.12.27
========
+ Optimized D3D11 state tracker to ignore set shader calls, and draw calls unless
specific combinations of render mods that actually need them are active.
+ Optimized control panel's end-of-frame input polling and cursor status checks.
+ Optimized shader tracker to avoid clearing active RTV cache at the end-of-frame
unless render mod tools window is open.
+ Optimized D3D11 Stateblock Restore code to bypass invoking SK's state tracking
hooks.
+

24.12.26.3
==========
+ Added missing draw call information to Reflex latency markers in D3D11 games

Expand Down
6 changes: 3 additions & 3 deletions include/SpecialK/DLL_VERSION.H
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

#define SK_YEAR 24
#define SK_MONTH 12
#define SK_DATE 26
#define SK_REV_N 3
#define SK_REV 3
#define SK_DATE 27
#define SK_REV_N 0
#define SK_REV 0

#ifndef _A2
#define _A2(a) #a
Expand Down
2 changes: 2 additions & 0 deletions include/SpecialK/render/d3d11/d3d11_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,8 @@ SK_ImGui_FlagDrawing_OnD3D11Ctx (UINT dev_idx);
extern bool
SK_ImGui_IsDrawing_OnD3D11Ctx (UINT& dev_idx, ID3D11DeviceContext* pDevCtx);

extern thread_local bool SK_D3D11_ApplyingStateBlock;


struct shader_stage_s
{
Expand Down
114 changes: 71 additions & 43 deletions src/render/d3d11/d3d11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6951,11 +6951,14 @@ SK_D3D11_HookDevCtx (sk_hook_d3d11_t *pHooks)
D3D11_VSSetConstantBuffers_Original,
D3D11_VSSetConstantBuffers_pfn );

DXGI_VIRTUAL_HOOK ( pHooks->ppImmediateContext, 8,
"ID3D11DeviceContext::PSSetShaderResources",
D3D11_PSSetShaderResources_Override,
D3D11_PSSetShaderResources_Original,
D3D11_PSSetShaderResources_pfn );
if (config.render.d3d11.track_set_shader_res)
{
DXGI_VIRTUAL_HOOK ( pHooks->ppImmediateContext, 8,
"ID3D11DeviceContext::PSSetShaderResources",
D3D11_PSSetShaderResources_Override,
D3D11_PSSetShaderResources_Original,
D3D11_PSSetShaderResources_pfn );
}

DXGI_VIRTUAL_HOOK ( pHooks->ppImmediateContext, 9,
"ID3D11DeviceContext::PSSetShader",
Expand Down Expand Up @@ -7029,11 +7032,14 @@ SK_D3D11_HookDevCtx (sk_hook_d3d11_t *pHooks)
D3D11_GSSetShader_Original,
D3D11_GSSetShader_pfn );

DXGI_VIRTUAL_HOOK ( pHooks->ppImmediateContext, 25,
"ID3D11DeviceContext::VSSetShaderResources",
D3D11_VSSetShaderResources_Override,
D3D11_VSSetShaderResources_Original,
D3D11_VSSetShaderResources_pfn );
if (config.render.d3d11.track_set_shader_res)
{
DXGI_VIRTUAL_HOOK ( pHooks->ppImmediateContext, 25,
"ID3D11DeviceContext::VSSetShaderResources",
D3D11_VSSetShaderResources_Override,
D3D11_VSSetShaderResources_Original,
D3D11_VSSetShaderResources_pfn );
}

#ifdef INSTALL_UNNECESSARY_HOOKS
DXGI_VIRTUAL_HOOK ( pHooks->ppImmediateContext, 29,
Expand All @@ -7043,11 +7049,14 @@ SK_D3D11_HookDevCtx (sk_hook_d3d11_t *pHooks)
D3D11_GetData_pfn );
#endif

DXGI_VIRTUAL_HOOK ( pHooks->ppImmediateContext, 31,
"ID3D11DeviceContext::GSSetShaderResources",
D3D11_GSSetShaderResources_Override,
D3D11_GSSetShaderResources_Original,
D3D11_GSSetShaderResources_pfn );
if (config.render.d3d11.track_set_shader_res)
{
DXGI_VIRTUAL_HOOK ( pHooks->ppImmediateContext, 31,
"ID3D11DeviceContext::GSSetShaderResources",
D3D11_GSSetShaderResources_Override,
D3D11_GSSetShaderResources_Original,
D3D11_GSSetShaderResources_pfn );
}

DXGI_VIRTUAL_HOOK ( pHooks->ppImmediateContext, 33,
"ID3D11DeviceContext::OMSetRenderTargets",
Expand Down Expand Up @@ -7139,35 +7148,44 @@ SK_D3D11_HookDevCtx (sk_hook_d3d11_t *pHooks)
D3D11_ExecuteCommandList_Original,
D3D11_ExecuteCommandList_pfn );

DXGI_VIRTUAL_HOOK ( pHooks->ppImmediateContext, 59,
"ID3D11DeviceContext::HSSetShaderResources",
D3D11_HSSetShaderResources_Override,
D3D11_HSSetShaderResources_Original,
D3D11_HSSetShaderResources_pfn );
if (config.render.d3d11.track_set_shader_res)
{
DXGI_VIRTUAL_HOOK ( pHooks->ppImmediateContext, 59,
"ID3D11DeviceContext::HSSetShaderResources",
D3D11_HSSetShaderResources_Override,
D3D11_HSSetShaderResources_Original,
D3D11_HSSetShaderResources_pfn );
}

DXGI_VIRTUAL_HOOK ( pHooks->ppImmediateContext, 60,
"ID3D11DeviceContext::HSSetShader",
D3D11_HSSetShader_Override,
D3D11_HSSetShader_Original,
D3D11_HSSetShader_pfn);

DXGI_VIRTUAL_HOOK ( pHooks->ppImmediateContext, 63,
"ID3D11DeviceContext::DSSetShaderResources",
D3D11_DSSetShaderResources_Override,
D3D11_DSSetShaderResources_Original,
D3D11_DSSetShaderResources_pfn );
if (config.render.d3d11.track_set_shader_res)
{
DXGI_VIRTUAL_HOOK ( pHooks->ppImmediateContext, 63,
"ID3D11DeviceContext::DSSetShaderResources",
D3D11_DSSetShaderResources_Override,
D3D11_DSSetShaderResources_Original,
D3D11_DSSetShaderResources_pfn );
}

DXGI_VIRTUAL_HOOK ( pHooks->ppImmediateContext, 64,
"ID3D11DeviceContext::DSSetShader",
D3D11_DSSetShader_Override,
D3D11_DSSetShader_Original,
D3D11_DSSetShader_pfn );

DXGI_VIRTUAL_HOOK ( pHooks->ppImmediateContext, 67,
"ID3D11DeviceContext::CSSetShaderResources",
D3D11_CSSetShaderResources_Override,
D3D11_CSSetShaderResources_Original,
D3D11_CSSetShaderResources_pfn );
if (config.render.d3d11.track_set_shader_res)
{
DXGI_VIRTUAL_HOOK ( pHooks->ppImmediateContext, 67,
"ID3D11DeviceContext::CSSetShaderResources",
D3D11_CSSetShaderResources_Override,
D3D11_CSSetShaderResources_Original,
D3D11_CSSetShaderResources_pfn );
}

DXGI_VIRTUAL_HOOK ( pHooks->ppImmediateContext, 68,
"ID3D11DeviceContext::CSSetUnorderedAccessViews",
Expand Down Expand Up @@ -9177,22 +9195,27 @@ SK_D3D11_EndFrame (SK_TLS* pTLS)
domain.tracked.deactivate (nullptr, dev_idx);
compute.tracked.deactivate (nullptr, dev_idx);

if (dev_idx < SK_D3D11_MAX_DEV_CONTEXTS)
// Optimization: Skip clearing tracked state when the render mod tools are not open
//
if (SK::ControlPanel::D3D11::show_shader_mod_dlg)
{
RtlZeroMemory (vertex.current.views [dev_idx], sizeof (ID3D11ShaderResourceView*) * 128);
RtlZeroMemory (pixel.current.views [dev_idx], sizeof (ID3D11ShaderResourceView*) * 128);
RtlZeroMemory (geometry.current.views [dev_idx], sizeof (ID3D11ShaderResourceView*) * 128);
RtlZeroMemory (domain.current.views [dev_idx], sizeof (ID3D11ShaderResourceView*) * 128);
RtlZeroMemory (hull.current.views [dev_idx], sizeof (ID3D11ShaderResourceView*) * 128);
RtlZeroMemory (compute.current.views [dev_idx], sizeof (ID3D11ShaderResourceView*) * 128);
}
if (dev_idx < SK_D3D11_MAX_DEV_CONTEXTS)
{
RtlZeroMemory (vertex.current.views [dev_idx], sizeof (ID3D11ShaderResourceView*) * 128);
RtlZeroMemory (pixel.current.views [dev_idx], sizeof (ID3D11ShaderResourceView*) * 128);
RtlZeroMemory (geometry.current.views [dev_idx], sizeof (ID3D11ShaderResourceView*) * 128);
RtlZeroMemory (domain.current.views [dev_idx], sizeof (ID3D11ShaderResourceView*) * 128);
RtlZeroMemory (hull.current.views [dev_idx], sizeof (ID3D11ShaderResourceView*) * 128);
RtlZeroMemory (compute.current.views [dev_idx], sizeof (ID3D11ShaderResourceView*) * 128);
}

tracked_rtv->clear ();
tracked_rtv->clear ();

////for ( auto& it : *used_textures ) it->Release ();
////for ( auto& it : *used_textures ) it->Release ();

used_textures->clear ();
mem_map_stats->clear ();
used_textures->clear ();
mem_map_stats->clear ();
}

// True if the disjoint query is complete and we can get the results of
// each tracked shader's timing
Expand Down Expand Up @@ -9473,11 +9496,16 @@ SK_D3D11_EndFrame (SK_TLS* pTLS)
it_ctx.used_textures.clear ();
}

SK_D3D11_RenderTargets [it_ctx.ctx_id_].clear ();
// These do not hold any references, so we can safely
// defer clearing this until the render mod dialog is
// active...
if (SK::ControlPanel::D3D11::show_shader_mod_dlg)
SK_D3D11_RenderTargets [it_ctx.ctx_id_].clear ();

InterlockedExchange (&it_ctx.writing_, 0);
}

// These must be released every frame!
SK_D3D11_TempResources->clear ();

SK_D3D11_Resampler_ProcessFinished (pDev, pDevCtx, pTLS);
Expand Down
6 changes: 6 additions & 0 deletions src/render/d3d11/d3d11_stateblock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#define __SK_SUBSYSTEM__ L"D3D11State"


thread_local bool SK_D3D11_ApplyingStateBlock = false;

// This causes The Witness and possibly other games to crash when the
// Steam Overlay is enabled
//#define SK_USE_D3D11_DEVICE_CTX_STATE
Expand Down Expand Up @@ -210,6 +212,8 @@ void ApplyStateblock (ID3D11DeviceContext* dc, D3DX11_STATE_BLOCK* sb)
if (pDev.p == nullptr)
return;

SK_D3D11_ApplyingStateBlock = true;

SK_ComPtr <ID3D11DeviceContext> pUnwrapped;
if (SUCCEEDED (dc->QueryInterface (IID_IUnwrappedD3D11DeviceContext,
(void **)&pUnwrapped.p)))
Expand Down Expand Up @@ -616,4 +620,6 @@ void ApplyStateblock (ID3D11DeviceContext* dc, D3DX11_STATE_BLOCK* sb)

if (sb->Predication != nullptr)
sb->Predication->Release ();

SK_D3D11_ApplyingStateBlock = false;
}
Loading

0 comments on commit 2be3c42

Please sign in to comment.