Skip to content

Commit 758c3e3

Browse files
committed
Add hooking helpers for catching function signature mismatches
Only used in d3d12 hooks for now
1 parent 68278bc commit 758c3e3

File tree

7 files changed

+158
-146
lines changed

7 files changed

+158
-146
lines changed

OptiScaler/OptiScaler.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ copy NUL "$(SolutionDir)x64\Release\a\!! EXTRACT ALL FILES TO GAME FOLDER !!" /Y
320320
<ClInclude Include="hooks\DxgiFactory_WrappedCalls.h" />
321321
<ClInclude Include="hooks\Dxgi_Hooks.h" />
322322
<ClInclude Include="hooks\FG_Hooks.h" />
323+
<ClInclude Include="hooks\Hook_Utils.h" />
323324
<ClInclude Include="hooks\LibraryLoad_Hooks.h" />
324325
<ClInclude Include="hooks\CommandBuffer_StateTracker.h" />
325326
<ClInclude Include="hooks\Xell_Hooks.h" />

OptiScaler/OptiScaler.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,9 @@
815815
<ClInclude Include="shaders\hud_copy\precompile\HudCopy_Shader.h">
816816
<Filter>Header Files</Filter>
817817
</ClInclude>
818+
<ClInclude Include="hooks\Hook_Utils.h">
819+
<Filter>Header Files</Filter>
820+
</ClInclude>
818821
</ItemGroup>
819822
<ItemGroup>
820823
<ClCompile Include="Config.cpp">

OptiScaler/exports/d3d12.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ HRESULT _D3D12CreateDeviceExport(IUnknown* adapter, D3D_FEATURE_LEVEL minLevel,
4242
return D3d12Proxy::D3D12CreateDevice_Hooked()(adapter, minLevel, riid, ppDevice);
4343
}
4444

45-
HRESULT _D3D12SerializeRootSignatureExport(D3d12Proxy::D3D12_ROOT_SIGNATURE_DESC_L* pRootSignature,
45+
HRESULT _D3D12SerializeRootSignatureExport(D3D12_ROOT_SIGNATURE_DESC* pRootSignature,
4646
D3D_ROOT_SIGNATURE_VERSION Version, ID3DBlob** ppBlob,
4747
ID3DBlob** ppErrorBlob)
4848
{
@@ -57,7 +57,7 @@ HRESULT _D3D12CreateRootSignatureDeserializerExport(LPCVOID pSrcData, SIZE_T Src
5757
pSrcData, SrcDataSizeInBytes, pRootSignatureDeserializerInterface, ppRootSignatureDeserializer);
5858
}
5959

60-
HRESULT _D3D12SerializeVersionedRootSignatureExport(D3d12Proxy::D3D12_VERSIONED_ROOT_SIGNATURE_DESC_L* pRootSignature,
60+
HRESULT _D3D12SerializeVersionedRootSignatureExport(D3D12_VERSIONED_ROOT_SIGNATURE_DESC* pRootSignature,
6161
ID3DBlob** ppBlob, ID3DBlob** ppErrorBlob)
6262
{
6363
return D3d12Proxy::D3D12SerializeVersionedRootSignature_Hooked()(pRootSignature, ppBlob, ppErrorBlob);

OptiScaler/hooks/D3D12_Hooks.cpp

Lines changed: 122 additions & 87 deletions
Large diffs are not rendered by default.

OptiScaler/hooks/Hook_Utils.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#pragma once
2+
3+
#include <type_traits>
4+
5+
// For getting function signature of methods, example of usage:
6+
// using PFN_CheckFeatureSupport = rewrite_signature<decltype(&ID3D12Device::CheckFeatureSupport)>::type;
7+
template <typename T> struct rewrite_signature;
8+
template <typename Ret, typename Class, typename... Args> struct rewrite_signature<Ret (Class::*)(Args...)>
9+
{
10+
using type = Ret(WINAPI*)(Class*, Args...);
11+
};
12+
13+
// For checking that the hooked function's signature matches the original
14+
// Place just above function definition, example of usage:
15+
// VALIDATE_HOOK(hkCheckFeatureSupport, PFN_CheckFeatureSupport)
16+
#define VALIDATE_HOOK(HookName, PfnType) \
17+
extern std::remove_pointer_t<PfnType> HookName; \
18+
static_assert(std::is_same_v<decltype(&HookName), PfnType>, \
19+
"Signature mismatch: " #HookName " does not match " #PfnType);

OptiScaler/proxies/D3D12_Proxy.h

Lines changed: 8 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -14,61 +14,14 @@
1414
class D3d12Proxy
1515
{
1616
public:
17-
typedef struct D3D12_ROOT_SIGNATURE_DESC_L
18-
{
19-
UINT NumParameters;
20-
D3D12_ROOT_PARAMETER* pParameters;
21-
UINT NumStaticSamplers;
22-
D3D12_STATIC_SAMPLER_DESC* pStaticSamplers;
23-
D3D12_ROOT_SIGNATURE_FLAGS Flags;
24-
} D3D12_ROOT_SIGNATURE_DESC_L;
25-
26-
typedef struct D3D12_ROOT_SIGNATURE_DESC1_L
27-
{
28-
UINT NumParameters;
29-
D3D12_ROOT_PARAMETER1* pParameters;
30-
UINT NumStaticSamplers;
31-
D3D12_STATIC_SAMPLER_DESC* pStaticSamplers;
32-
D3D12_ROOT_SIGNATURE_FLAGS Flags;
33-
} D3D12_ROOT_SIGNATURE_DESC1_L;
34-
35-
typedef struct D3D12_ROOT_SIGNATURE_DESC2_L
36-
{
37-
UINT NumParameters;
38-
D3D12_ROOT_PARAMETER1* pParameters;
39-
UINT NumStaticSamplers;
40-
D3D12_STATIC_SAMPLER_DESC1* pStaticSamplers;
41-
D3D12_ROOT_SIGNATURE_FLAGS Flags;
42-
} D3D12_ROOT_SIGNATURE_DESC2_L;
43-
44-
typedef struct D3D12_VERSIONED_ROOT_SIGNATURE_DESC_L
45-
{
46-
D3D_ROOT_SIGNATURE_VERSION Version;
47-
union
48-
{
49-
D3D12_ROOT_SIGNATURE_DESC_L Desc_1_0;
50-
D3D12_ROOT_SIGNATURE_DESC1_L Desc_1_1;
51-
D3D12_ROOT_SIGNATURE_DESC2_L Desc_1_2;
52-
};
53-
} D3D12_VERSIONED_ROOT_SIGNATURE_DESC_L;
54-
55-
typedef HRESULT (*PFN_D3D12CreateDevice)(IUnknown*, D3D_FEATURE_LEVEL, REFIID, void**);
56-
typedef HRESULT (*PFN_D3D12SerializeRootSignature)(D3D12_ROOT_SIGNATURE_DESC_L* pRootSignature,
57-
D3D_ROOT_SIGNATURE_VERSION Version, ID3DBlob** ppBlob,
58-
ID3DBlob** ppErrorBlob);
59-
typedef HRESULT (*PFN_D3D12CreateRootSignatureDeserializer)(LPCVOID pSrcData, SIZE_T SrcDataSizeInBytes,
60-
REFIID pRootSignatureDeserializerInterface,
61-
void** ppRootSignatureDeserializer);
62-
typedef HRESULT (*PFN_D3D12SerializeVersionedRootSignature)(D3D12_VERSIONED_ROOT_SIGNATURE_DESC_L* pRootSignature,
63-
ID3DBlob** ppBlob, ID3DBlob** ppErrorBlob);
64-
typedef HRESULT (*PFN_D3D12CreateVersionedRootSignatureDeserializer)(LPCVOID pSrcData, SIZE_T SrcDataSizeInBytes,
65-
REFIID pRootSignatureDeserializerInterface,
66-
void** ppRootSignatureDeserializer);
67-
typedef HRESULT (*PFN_D3D12GetDebugInterface)(REFIID, void**);
68-
typedef HRESULT (*PFN_D3D12EnableExperimentalFeatures)(UINT NumFeatures, const IID* pIIDs,
69-
void* pConfigurationStructs,
70-
UINT* pConfigurationStructSizes);
71-
typedef HRESULT (*PFN_D3D12GetInterface)(REFCLSID, REFIID, void**);
17+
typedef decltype(&D3D12CreateDevice) PFN_D3D12CreateDevice;
18+
typedef decltype(&D3D12SerializeRootSignature) PFN_D3D12SerializeRootSignature;
19+
typedef decltype(&D3D12CreateRootSignatureDeserializer) PFN_D3D12CreateRootSignatureDeserializer;
20+
typedef decltype(&D3D12SerializeVersionedRootSignature) PFN_D3D12SerializeVersionedRootSignature;
21+
typedef decltype(&D3D12CreateVersionedRootSignatureDeserializer) PFN_D3D12CreateVersionedRootSignatureDeserializer;
22+
typedef decltype(&D3D12GetDebugInterface) PFN_D3D12GetDebugInterface;
23+
typedef decltype(&D3D12EnableExperimentalFeatures) PFN_D3D12EnableExperimentalFeatures;
24+
typedef decltype(&D3D12GetInterface) PFN_D3D12GetInterface;
7225

7326
static void Init(HMODULE module = nullptr)
7427
{

OptiScaler/proxies/IGDExt_Proxy.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ class IGDExtProxy
416416
}
417417

418418
static D3D12_RESOURCE_ALLOCATION_INFO GetResourceAllocationInfo(UINT visibleMask, UINT numResourceDescs,
419-
D3D12_RESOURCE_DESC* pResourceDescs)
419+
const D3D12_RESOURCE_DESC* pResourceDescs)
420420
{
421421
if (_context == nullptr)
422422
return {};
@@ -431,7 +431,8 @@ class IGDExtProxy
431431
return {};
432432

433433
INTC_D3D12_RESOURCE_DESC_0001 localDesc {};
434-
localDesc.pD3D12Desc = pResourceDescs;
434+
D3D12_RESOURCE_DESC localD3D12Desc = *pResourceDescs;
435+
localDesc.pD3D12Desc = &localD3D12Desc;
435436
localDesc.EmulatedTyped64bitAtomics = true;
436437

437438
auto result = _INTC_D3D12_GetResourceAllocationInfo(_context, visibleMask, numResourceDescs, &localDesc);

0 commit comments

Comments
 (0)