Skip to content

Commit

Permalink
Optimized D3D11 state tracking for SetShaderResources and removed unn…
Browse files Browse the repository at this point in the history
…ecessary hooks on SetSamplers
  • Loading branch information
Kaldaien committed Dec 26, 2024
1 parent 7c016d7 commit 6a30acb
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 393 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
24.12.26.1
24.12.26.2
==========
+ Small optimization to text OSD
+ Optimized D3D11 state tracking for SetShaderResources and removed unnecessary
hooks on SetSamplers.

24.12.26.1
==========
+ Fixed potential deadlock in RenoDX mods, the ones that do not check for
nullptr before dereferencing ptrs will simply have to crash until fixed.
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 24
#define SK_MONTH 12
#define SK_DATE 26
#define SK_REV_N 1
#define SK_REV 1
#define SK_REV_N 2
#define SK_REV 2

#ifndef _A2
#define _A2(a) #a
Expand Down
40 changes: 15 additions & 25 deletions src/render/d3d11/d3d11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5213,10 +5213,14 @@ _In_opt_ ID3D11DepthStencilView *pDepthStencilView,
#endif
(! SK_D3D11_ShouldTrackRenderOp (pDevCtx, dev_idx)))
{
if (tracked_rtv->active_count [dev_idx] > 0)
// Clear the RTVs only if the render mod tools are open
if (SK::ControlPanel::D3D11::show_shader_mod_dlg)
{
for (auto& rtv : tracked_rtv->active [dev_idx])
rtv.store (false);
if (tracked_rtv->active_count [dev_idx] > 0)
{
for (auto& rtv : tracked_rtv->active [dev_idx])
rtv.store (false);
}

tracked_rtv->active_count [dev_idx] = 0;
}
Expand Down Expand Up @@ -6979,54 +6983,38 @@ SK_D3D11_HookDevCtx (sk_hook_d3d11_t *pHooks)
/// return;
///}

#if 0
DXGI_VIRTUAL_OVERRIDE ( pHooks->ppImmediateContext, 7, "ID3D11DeviceContext::VSSetConstantBuffers",
D3D11_VSSetConstantBuffers_Override, D3D11_VSSetConstantBuffers_Original,
D3D11_VSSetConstantBuffers_pfn);
#else
DXGI_VIRTUAL_HOOK ( pHooks->ppImmediateContext, 7,
"ID3D11DeviceContext::VSSetConstantBuffers",
D3D11_VSSetConstantBuffers_Override,
D3D11_VSSetConstantBuffers_Original,
D3D11_VSSetConstantBuffers_pfn );
#endif

DXGI_VIRTUAL_HOOK ( pHooks->ppImmediateContext, 8,
"ID3D11DeviceContext::PSSetShaderResources",
D3D11_PSSetShaderResources_Override,
D3D11_PSSetShaderResources_Original,
D3D11_PSSetShaderResources_pfn );

#if 0
DXGI_VIRTUAL_OVERRIDE (pHooks->ppImmediateContext, 9,
"ID3D11DeviceContext::PSSetShader",
D3D11_PSSetShader_Override, D3D11_PSSetShader_Original,
D3D11_PSSetShader_pfn);
#else
DXGI_VIRTUAL_HOOK ( pHooks->ppImmediateContext, 9,
"ID3D11DeviceContext::PSSetShader",
D3D11_PSSetShader_Override,
D3D11_PSSetShader_Original,
D3D11_PSSetShader_pfn );
#endif

// Hook is unneeded currently
#if 0
DXGI_VIRTUAL_HOOK ( pHooks->ppImmediateContext, 10,
"ID3D11DeviceContext::PSSetSamplers",
D3D11_PSSetSamplers_Override,
D3D11_PSSetSamplers_Original,
D3D11_PSSetSamplers_pfn );
#endif

#if 0
DXGI_VIRTUAL_OVERRIDE ( pHooks->ppImmediateContext, 11, "ID3D11DeviceContext::VSSetShader",
D3D11_VSSetShader_Override, D3D11_VSSetShader_Original,
D3D11_VSSetShader_pfn);
#else
DXGI_VIRTUAL_HOOK ( pHooks->ppImmediateContext, 11,
"ID3D11DeviceContext::VSSetShader",
D3D11_VSSetShader_Override,
D3D11_VSSetShader_Original,
D3D11_VSSetShader_pfn );
#endif

DXGI_VIRTUAL_HOOK ( pHooks->ppImmediateContext, 12,
"ID3D11DeviceContext::DrawIndexed",
Expand Down Expand Up @@ -7085,11 +7073,13 @@ SK_D3D11_HookDevCtx (sk_hook_d3d11_t *pHooks)
D3D11_VSSetShaderResources_Original,
D3D11_VSSetShaderResources_pfn );

#ifdef INSTALL_UNNECESSARY_HOOKS
DXGI_VIRTUAL_HOOK ( pHooks->ppImmediateContext, 29,
"ID3D11DeviceContext::GetData",
D3D11_GetData_Override,
D3D11_GetData_Original,
D3D11_GetData_pfn );
#endif

DXGI_VIRTUAL_HOOK ( pHooks->ppImmediateContext, 31,
"ID3D11DeviceContext::GSSetShaderResources",
Expand Down Expand Up @@ -8838,7 +8828,7 @@ D3D11Dev_CreateDeferredContext1_Override (
L"ContextFlags=0x%x, **ppDeferredContext=%p",
ContextFlags, ppDeferredContext1 );

if (ppDeferredContext1 != nullptr)
if (config.render.d3d11.wrap_d3d11_dev_ctx && config.render.dxgi.deferred_isolation && ppDeferredContext1 != nullptr)
{
ID3D11DeviceContext1* pTemp = nullptr;
const HRESULT hr =
Expand Down Expand Up @@ -8884,7 +8874,7 @@ D3D11Dev_CreateDeferredContext2_Override (
L"ContextFlags=0x%x, **ppDeferredContext=%p",
ContextFlags, ppDeferredContext2 );

if (ppDeferredContext2 != nullptr)
if (config.render.d3d11.wrap_d3d11_dev_ctx && config.render.dxgi.deferred_isolation && ppDeferredContext2 != nullptr)
{
ID3D11DeviceContext2* pTemp = nullptr;
const HRESULT hr =
Expand Down Expand Up @@ -8929,7 +8919,7 @@ D3D11Dev_CreateDeferredContext3_Override (
L"ContextFlags=0x%x, **ppDeferredContext=%p",
ContextFlags, ppDeferredContext3 );

if (ppDeferredContext3 != nullptr)
if (config.render.d3d11.wrap_d3d11_dev_ctx && config.render.dxgi.deferred_isolation && ppDeferredContext3 != nullptr)
{
ID3D11DeviceContext3* pTemp = nullptr;
const HRESULT hr =
Expand Down
72 changes: 47 additions & 25 deletions src/render/d3d11/d3d11_shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,53 @@ SK_D3D11_SetShaderResources_Impl (

int stage_id = 0;

switch (ShaderType)
{
case SK_D3D11_ShaderType::Vertex:
pTargetFn = ( hooked ? D3D11_VSSetShaderResources_Original :
_vftable [25] );
break;
case SK_D3D11_ShaderType::Pixel:
pTargetFn = ( hooked ? D3D11_PSSetShaderResources_Original :
_vftable [8] );
break;
case SK_D3D11_ShaderType::Geometry:
pTargetFn = ( hooked ? D3D11_GSSetShaderResources_Original :
_vftable [31] );
break;
case SK_D3D11_ShaderType::Hull:
pTargetFn = ( hooked ? D3D11_HSSetShaderResources_Original :
_vftable [59] );
break;
case SK_D3D11_ShaderType::Domain:
pTargetFn = ( hooked ? D3D11_DSSetShaderResources_Original :
_vftable [63] );
break;
case SK_D3D11_ShaderType::Compute:
pTargetFn = ( hooked ? D3D11_CSSetShaderResources_Original :
_vftable [67] );
break;

default:
break;
}

assert (shader_base != nullptr);
assert (cs_lock != nullptr);

auto _Finish =
[&](void) -> void
{
static_cast <D3D11_VSSetShaderResources_pfn> (pTargetFn) (
pDevContext, StartSlot, NumViews, ppShaderResourceViews);
};

if (! SK_D3D11_ShouldTrackSetShaderResources (pDevContext, dev_idx))
{
return
_Finish ();
}

switch (ShaderType)
{
case SK_D3D11_ShaderType::Vertex:
Expand All @@ -778,8 +825,6 @@ SK_D3D11_SetShaderResources_Impl (
shader_base = vs_base;
stage_id = VERTEX_SHADER_STAGE;
cs_lock = cs_shader_vs.get ();
pTargetFn = ( hooked ? D3D11_VSSetShaderResources_Original :
_vftable [25] );
} break;

case SK_D3D11_ShaderType::Pixel:
Expand All @@ -790,8 +835,6 @@ SK_D3D11_SetShaderResources_Impl (
shader_base = ps_base;
stage_id = PIXEL_SHADER_STAGE;
cs_lock = cs_shader_ps.get ();
pTargetFn = ( hooked ? D3D11_PSSetShaderResources_Original :
_vftable [8] );
} break;

case SK_D3D11_ShaderType::Geometry:
Expand All @@ -802,8 +845,6 @@ SK_D3D11_SetShaderResources_Impl (
shader_base = gs_base;
stage_id = GEOMETRY_SHADER_STAGE;
cs_lock = cs_shader_gs.get ();
pTargetFn = ( hooked ? D3D11_GSSetShaderResources_Original :
_vftable [31] );
} break;

case SK_D3D11_ShaderType::Hull:
Expand All @@ -814,8 +855,6 @@ SK_D3D11_SetShaderResources_Impl (
shader_base = hs_base;
stage_id = HULL_SHADER_STAGE;
cs_lock = cs_shader_hs.get ();
pTargetFn = ( hooked ? D3D11_HSSetShaderResources_Original :
_vftable [59] );
} break;

case SK_D3D11_ShaderType::Domain:
Expand All @@ -826,8 +865,6 @@ SK_D3D11_SetShaderResources_Impl (
shader_base = ds_base;
stage_id = DOMAIN_SHADER_STAGE;
cs_lock = cs_shader_ds.get ();
pTargetFn = ( hooked ? D3D11_DSSetShaderResources_Original :
_vftable [63] );
} break;

case SK_D3D11_ShaderType::Compute:
Expand All @@ -838,8 +875,6 @@ SK_D3D11_SetShaderResources_Impl (
shader_base = cs_base;
stage_id = COMPUTE_SHADER_STAGE;
cs_lock = cs_shader_cs.get ();
pTargetFn = ( hooked ? D3D11_CSSetShaderResources_Original :
_vftable [67] );
} break;

default:
Expand All @@ -849,19 +884,6 @@ SK_D3D11_SetShaderResources_Impl (
assert (shader_base != nullptr);
assert (cs_lock != nullptr);

auto _Finish =
[&](void) -> void
{
static_cast <D3D11_VSSetShaderResources_pfn> (pTargetFn) (
pDevContext, StartSlot, NumViews, ppShaderResourceViews);
};

if (! SK_D3D11_ShouldTrackSetShaderResources (pDevContext, dev_idx))
{
return
_Finish ();
}

#define pDevCtx pDevContext
SK_WRAP_AND_HOOK
#undef pDevCtx
Expand Down
Loading

0 comments on commit 6a30acb

Please sign in to comment.