Repository navigation
Expand file tree
/
Copy pathpresent_source_hooks.hpp
More file actions
141 lines (132 loc) · 6.81 KB
/
Copy pathpresent_source_hooks.hpp
File metadata and controls
141 lines (132 loc) · 6.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* Copyright (C) 2026
* SPDX-License-Identifier: MIT
*/
#pragma once
// Included inside the Present implementation namespace. Only commands that
// can write the still-UAV SR image, or bypass the legacy layout contract,
// need observation. CopyResource and CopyTextureRegion already have hooks.
namespace present_source_hooks {
struct Hook {
void** original;
void* callback;
void* target = nullptr;
};
template <typename List, size_t Slot, bool UnknownLayout, typename... Args>
struct Command {
inline static constexpr size_t slot = Slot;
inline static void* original = nullptr;
static void STDMETHODCALLTYPE Call(List* list, Args... args) {
const CallbackScope callback;
const PresentCommandScope scope;
if (callback && scope.outermost) {
GuardHook([&] {
if constexpr (Slot == 26) {
ObservePresentSourceBarriers(list, args...);
} else {
ObservePresentSourceWork(list, UnknownLayout);
}
});
}
reinterpret_cast<void(STDMETHODCALLTYPE*)(List*, Args...)>(original)(list, args...);
}
inline static Hook hook{&original, reinterpret_cast<void*>(&Call)};
};
using Draw = Command<ID3D12GraphicsCommandList, 12, false, UINT, UINT, UINT, UINT>;
using DrawIndexed = Command<ID3D12GraphicsCommandList, 13, false, UINT, UINT, UINT, INT, UINT>;
using Dispatch = Command<ID3D12GraphicsCommandList, 14, false, UINT, UINT, UINT>;
using CopyTiles = Command<ID3D12GraphicsCommandList, 18, true, ID3D12Resource*, const D3D12_TILED_RESOURCE_COORDINATE*,
const D3D12_TILE_REGION_SIZE*, ID3D12Resource*, UINT64, D3D12_TILE_COPY_FLAGS>;
using Barriers = Command<ID3D12GraphicsCommandList, 26, false, UINT, const D3D12_RESOURCE_BARRIER*>;
using Bundle = Command<ID3D12GraphicsCommandList, 27, true, ID3D12GraphicsCommandList*>;
using ClearUInt = Command<ID3D12GraphicsCommandList, 49, false, D3D12_GPU_DESCRIPTOR_HANDLE,
D3D12_CPU_DESCRIPTOR_HANDLE, ID3D12Resource*, const UINT*, UINT, const D3D12_RECT*>;
using ClearFloat = Command<ID3D12GraphicsCommandList, 50, false, D3D12_GPU_DESCRIPTOR_HANDLE,
D3D12_CPU_DESCRIPTOR_HANDLE, ID3D12Resource*, const FLOAT*, UINT, const D3D12_RECT*>;
using Discard = Command<ID3D12GraphicsCommandList, 51, true, ID3D12Resource*, const D3D12_DISCARD_REGION*>;
using Indirect = Command<ID3D12GraphicsCommandList, 59, true, ID3D12CommandSignature*, UINT,
ID3D12Resource*, UINT64, ID3D12Resource*, UINT64>;
using InitMeta = Command<ID3D12GraphicsCommandList4, 70, true, ID3D12MetaCommand*, const void*, SIZE_T>;
using ExecuteMeta = Command<ID3D12GraphicsCommandList4, 71, true, ID3D12MetaCommand*, const void*, SIZE_T>;
using Rays = Command<ID3D12GraphicsCommandList4, 76, false, const D3D12_DISPATCH_RAYS_DESC*>;
using Mesh = Command<ID3D12GraphicsCommandList6, 79, false, UINT, UINT, UINT>;
using EnhancedBarriers = Command<ID3D12GraphicsCommandList7, 80, true, UINT32, const D3D12_BARRIER_GROUP*>;
using Graph = Command<ID3D12GraphicsCommandList10, 85, true, const D3D12_DISPATCH_GRAPH_DESC*>;
inline std::array hooks{&Draw::hook, &DrawIndexed::hook, &Dispatch::hook, &CopyTiles::hook,
&Barriers::hook, &Bundle::hook, &ClearUInt::hook, &ClearFloat::hook, &Discard::hook, &Indirect::hook,
&InitMeta::hook, &ExecuteMeta::hook, &Rays::hook, &Mesh::hook, &EnhancedBarriers::hook, &Graph::hook};
inline std::shared_mutex mutex;
} // namespace present_source_hooks
inline bool InstallPresentSourceHooks(ID3D12GraphicsCommandList* list) {
using namespace present_source_hooks;
if (list == nullptr) return false;
renodx::utils::directx::NativeFromReShadeProxy(&list);
const auto entries = [](IUnknown* object) { return *reinterpret_cast<void***>(object); };
const auto base = entries(list);
Microsoft::WRL::ComPtr<ID3D12GraphicsCommandList4> list4;
Microsoft::WRL::ComPtr<ID3D12GraphicsCommandList6> list6;
Microsoft::WRL::ComPtr<ID3D12GraphicsCommandList7> list7;
Microsoft::WRL::ComPtr<ID3D12GraphicsCommandList10> list10;
list->QueryInterface(IID_PPV_ARGS(&list4));
list->QueryInterface(IID_PPV_ARGS(&list6));
list->QueryInterface(IID_PPV_ARGS(&list7));
list->QueryInterface(IID_PPV_ARGS(&list10));
// The source watch compares the native list pointer before entering any
// shared state. Distinct extended-interface faces cannot use that proof.
if ((list4 != nullptr && static_cast<ID3D12GraphicsCommandList*>(list4.Get()) != list)
|| (list6 != nullptr && static_cast<ID3D12GraphicsCommandList*>(list6.Get()) != list)
|| (list7 != nullptr && static_cast<ID3D12GraphicsCommandList*>(list7.Get()) != list)
|| (list10 != nullptr && static_cast<ID3D12GraphicsCommandList*>(list10.Get()) != list)) return false;
const auto ray_tracing = (list4 != nullptr ? entries(list4.Get()) : nullptr);
const std::array targets{base[Draw::slot], base[DrawIndexed::slot], base[Dispatch::slot], base[CopyTiles::slot],
base[Barriers::slot], base[Bundle::slot], base[ClearUInt::slot], base[ClearFloat::slot], base[Discard::slot],
base[Indirect::slot], (ray_tracing != nullptr ? ray_tracing[InitMeta::slot] : nullptr),
(ray_tracing != nullptr ? ray_tracing[ExecuteMeta::slot] : nullptr), (ray_tracing != nullptr ? ray_tracing[Rays::slot] : nullptr),
(list6 != nullptr ? entries(list6.Get())[Mesh::slot] : nullptr),
(list7 != nullptr ? entries(list7.Get())[EnhancedBarriers::slot] : nullptr),
(list10 != nullptr ? entries(list10.Get())[Graph::slot] : nullptr)};
std::lock_guard lock(mutex);
if (Barriers::hook.target != nullptr) {
for (size_t i = 0; i < hooks.size(); ++i) {
if (targets[i] != hooks[i]->target) return false;
}
return true;
}
if (!fg::PinRuntimeCallback()) return false;
std::lock_guard transaction(renodx::utils::vtable::TransactionMutex());
if (!renodx::utils::vtable::BeginTransaction()) return false;
DetourUpdateThread(GetCurrentThread());
for (size_t i = 0; i < hooks.size(); ++i) {
if (targets[i] == nullptr) continue;
auto* hook = hooks[i];
*hook->original = targets[i];
if (DetourAttach(hook->original, hook->callback) != NO_ERROR) {
DetourTransactionAbort();
return false;
}
}
if (DetourTransactionCommit() != NO_ERROR) return false;
for (size_t i = 0; i < hooks.size(); ++i) {
hooks[i]->target = targets[i];
}
return true;
}
inline void UnhookPresentSourceHooks() {
using namespace present_source_hooks;
std::lock_guard lock(mutex);
if (Barriers::hook.target == nullptr) return;
std::lock_guard transaction(renodx::utils::vtable::TransactionMutex());
if (!renodx::utils::vtable::BeginTransaction()) return;
DetourUpdateThread(GetCurrentThread());
for (auto* hook : hooks) {
if (hook->target != nullptr && DetourDetach(hook->original, hook->callback) != NO_ERROR) {
DetourTransactionAbort();
return;
}
}
if (DetourTransactionCommit() == NO_ERROR) {
for (auto* hook : hooks) {
hook->target = nullptr;
}
}
}