dxgi-capture.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. #include <d3d10_1.h>
  2. #include <d3d11.h>
  3. #include <dxgi1_2.h>
  4. #include <d3dcompiler.h>
  5. #include <inttypes.h>
  6. #include "graphics-hook.h"
  7. #include <detours.h>
  8. #if COMPILE_D3D12_HOOK
  9. #include <d3d12.h>
  10. #endif
  11. typedef HRESULT(STDMETHODCALLTYPE *resize_buffers_t)(IDXGISwapChain *, UINT,
  12. UINT, UINT, DXGI_FORMAT,
  13. UINT);
  14. typedef HRESULT(STDMETHODCALLTYPE *present_t)(IDXGISwapChain *, UINT, UINT);
  15. typedef HRESULT(STDMETHODCALLTYPE *present1_t)(IDXGISwapChain1 *, UINT, UINT,
  16. const DXGI_PRESENT_PARAMETERS *);
  17. resize_buffers_t RealResizeBuffers = nullptr;
  18. present_t RealPresent = nullptr;
  19. present1_t RealPresent1 = nullptr;
  20. thread_local int dxgi_presenting = 0;
  21. struct ID3D12CommandQueue *dxgi_possible_swap_queues[8]{};
  22. size_t dxgi_possible_swap_queue_count;
  23. bool dxgi_present_attempted = false;
  24. struct dxgi_swap_data {
  25. IDXGISwapChain *swap;
  26. void (*capture)(void *, void *);
  27. void (*free)(void);
  28. };
  29. static struct dxgi_swap_data data = {};
  30. static int swap_chain_mismatch_count = 0;
  31. constexpr int swap_chain_mismtach_limit = 16;
  32. static void STDMETHODCALLTYPE SwapChainDestructed(void *pData)
  33. {
  34. if (pData == data.swap) {
  35. data.swap = nullptr;
  36. data.capture = nullptr;
  37. memset(dxgi_possible_swap_queues, 0,
  38. sizeof(dxgi_possible_swap_queues));
  39. dxgi_possible_swap_queue_count = 0;
  40. dxgi_present_attempted = false;
  41. data.free();
  42. data.free = nullptr;
  43. }
  44. }
  45. static void init_swap_data(IDXGISwapChain *swap,
  46. void (*capture)(void *, void *), void (*free)(void))
  47. {
  48. data.swap = swap;
  49. data.capture = capture;
  50. data.free = free;
  51. ID3DDestructionNotifier *notifier;
  52. if (SUCCEEDED(
  53. swap->QueryInterface<ID3DDestructionNotifier>(&notifier))) {
  54. UINT callbackID;
  55. notifier->RegisterDestructionCallback(&SwapChainDestructed,
  56. swap, &callbackID);
  57. notifier->Release();
  58. }
  59. }
  60. static bool setup_dxgi(IDXGISwapChain *swap)
  61. {
  62. IUnknown *device;
  63. HRESULT hr;
  64. hr = swap->GetDevice(__uuidof(ID3D11Device), (void **)&device);
  65. if (SUCCEEDED(hr)) {
  66. ID3D11Device *d3d11 = static_cast<ID3D11Device *>(device);
  67. D3D_FEATURE_LEVEL level = d3d11->GetFeatureLevel();
  68. device->Release();
  69. if (level >= D3D_FEATURE_LEVEL_11_0) {
  70. hlog("Found D3D11 11.0 device on swap chain");
  71. init_swap_data(swap, d3d11_capture, d3d11_free);
  72. return true;
  73. }
  74. }
  75. hr = swap->GetDevice(__uuidof(ID3D10Device), (void **)&device);
  76. if (SUCCEEDED(hr)) {
  77. device->Release();
  78. hlog("Found D3D10 device on swap chain");
  79. init_swap_data(swap, d3d10_capture, d3d10_free);
  80. return true;
  81. }
  82. hr = swap->GetDevice(__uuidof(ID3D11Device), (void **)&device);
  83. if (SUCCEEDED(hr)) {
  84. device->Release();
  85. hlog("Found D3D11 device on swap chain");
  86. init_swap_data(swap, d3d11_capture, d3d11_free);
  87. return true;
  88. }
  89. #if COMPILE_D3D12_HOOK
  90. hr = swap->GetDevice(__uuidof(ID3D12Device), (void **)&device);
  91. if (SUCCEEDED(hr)) {
  92. device->Release();
  93. hlog("Found D3D12 device on swap chain: swap=0x%" PRIX64
  94. ", device=0x%" PRIX64,
  95. (uint64_t)(uintptr_t)swap, (uint64_t)(uintptr_t)device);
  96. for (size_t i = 0; i < dxgi_possible_swap_queue_count; ++i) {
  97. hlog(" queue=0x%" PRIX64,
  98. (uint64_t)(uintptr_t)dxgi_possible_swap_queues[i]);
  99. }
  100. if (dxgi_possible_swap_queue_count > 0) {
  101. init_swap_data(swap, d3d12_capture, d3d12_free);
  102. return true;
  103. }
  104. }
  105. #endif
  106. hlog_verbose("Failed to setup DXGI");
  107. return false;
  108. }
  109. static bool resize_buffers_called = false;
  110. static HRESULT STDMETHODCALLTYPE hook_resize_buffers(IDXGISwapChain *swap,
  111. UINT buffer_count,
  112. UINT width, UINT height,
  113. DXGI_FORMAT format,
  114. UINT flags)
  115. {
  116. hlog_verbose("ResizeBuffers callback");
  117. data.swap = nullptr;
  118. data.capture = nullptr;
  119. memset(dxgi_possible_swap_queues, 0, sizeof(dxgi_possible_swap_queues));
  120. dxgi_possible_swap_queue_count = 0;
  121. dxgi_present_attempted = false;
  122. if (data.free)
  123. data.free();
  124. data.free = nullptr;
  125. const HRESULT hr = RealResizeBuffers(swap, buffer_count, width, height,
  126. format, flags);
  127. resize_buffers_called = true;
  128. return hr;
  129. }
  130. static inline IUnknown *get_dxgi_backbuffer(IDXGISwapChain *swap)
  131. {
  132. IUnknown *res = nullptr;
  133. const HRESULT hr = swap->GetBuffer(0, IID_PPV_ARGS(&res));
  134. if (FAILED(hr))
  135. hlog_hr("get_dxgi_backbuffer: GetBuffer failed", hr);
  136. return res;
  137. }
  138. static void update_mismatch_count(bool match)
  139. {
  140. if (match) {
  141. swap_chain_mismatch_count = 0;
  142. } else {
  143. ++swap_chain_mismatch_count;
  144. if (swap_chain_mismatch_count == swap_chain_mismtach_limit) {
  145. data.swap = nullptr;
  146. data.capture = nullptr;
  147. memset(dxgi_possible_swap_queues, 0,
  148. sizeof(dxgi_possible_swap_queues));
  149. dxgi_possible_swap_queue_count = 0;
  150. dxgi_present_attempted = false;
  151. data.free();
  152. data.free = nullptr;
  153. swap_chain_mismatch_count = 0;
  154. }
  155. }
  156. }
  157. static HRESULT STDMETHODCALLTYPE hook_present(IDXGISwapChain *swap,
  158. UINT sync_interval, UINT flags)
  159. {
  160. if (should_passthrough()) {
  161. dxgi_presenting = true;
  162. const HRESULT hr = RealPresent(swap, sync_interval, flags);
  163. dxgi_presenting = false;
  164. return hr;
  165. }
  166. const bool capture_overlay = global_hook_info->capture_overlay;
  167. const bool test_draw = (flags & DXGI_PRESENT_TEST) != 0;
  168. if (data.swap) {
  169. update_mismatch_count(swap == data.swap);
  170. }
  171. if (!data.swap && !capture_active()) {
  172. setup_dxgi(swap);
  173. }
  174. hlog_verbose(
  175. "Present callback: sync_interval=%u, flags=%u, current_swap=0x%" PRIX64
  176. ", expected_swap=0x%" PRIX64,
  177. sync_interval, flags, swap, data.swap);
  178. const bool capture = !test_draw && swap == data.swap && data.capture;
  179. if (capture && !capture_overlay) {
  180. IUnknown *backbuffer = get_dxgi_backbuffer(swap);
  181. if (backbuffer) {
  182. data.capture(swap, backbuffer);
  183. backbuffer->Release();
  184. }
  185. }
  186. ++dxgi_presenting;
  187. const HRESULT hr = RealPresent(swap, sync_interval, flags);
  188. --dxgi_presenting;
  189. dxgi_present_attempted = true;
  190. if (capture && capture_overlay) {
  191. /*
  192. * It seems that the first call to Present after ResizeBuffers
  193. * will cause the backbuffer to be invalidated, so do not
  194. * perform the post-overlay capture if ResizeBuffers has
  195. * recently been called. (The backbuffer returned by
  196. * get_dxgi_backbuffer *will* be invalid otherwise)
  197. */
  198. if (resize_buffers_called) {
  199. resize_buffers_called = false;
  200. } else {
  201. IUnknown *backbuffer = get_dxgi_backbuffer(swap);
  202. if (backbuffer) {
  203. data.capture(swap, backbuffer);
  204. backbuffer->Release();
  205. }
  206. }
  207. }
  208. return hr;
  209. }
  210. static HRESULT STDMETHODCALLTYPE
  211. hook_present1(IDXGISwapChain1 *swap, UINT sync_interval, UINT flags,
  212. const DXGI_PRESENT_PARAMETERS *params)
  213. {
  214. if (should_passthrough()) {
  215. dxgi_presenting = true;
  216. const HRESULT hr =
  217. RealPresent1(swap, sync_interval, flags, params);
  218. dxgi_presenting = false;
  219. return hr;
  220. }
  221. const bool capture_overlay = global_hook_info->capture_overlay;
  222. const bool test_draw = (flags & DXGI_PRESENT_TEST) != 0;
  223. if (data.swap) {
  224. update_mismatch_count(swap == data.swap);
  225. }
  226. if (!data.swap && !capture_active()) {
  227. setup_dxgi(swap);
  228. }
  229. hlog_verbose(
  230. "Present1 callback: sync_interval=%u, flags=%u, current_swap=0x%" PRIX64
  231. ", expected_swap=0x%" PRIX64,
  232. sync_interval, flags, swap, data.swap);
  233. const bool capture = !test_draw && swap == data.swap && !!data.capture;
  234. if (capture && !capture_overlay) {
  235. IUnknown *backbuffer = get_dxgi_backbuffer(swap);
  236. if (backbuffer) {
  237. data.capture(swap, backbuffer);
  238. backbuffer->Release();
  239. }
  240. }
  241. ++dxgi_presenting;
  242. const HRESULT hr = RealPresent1(swap, sync_interval, flags, params);
  243. --dxgi_presenting;
  244. dxgi_present_attempted = true;
  245. if (capture && capture_overlay) {
  246. if (resize_buffers_called) {
  247. resize_buffers_called = false;
  248. } else {
  249. IUnknown *backbuffer = get_dxgi_backbuffer(swap);
  250. if (backbuffer) {
  251. data.capture(swap, backbuffer);
  252. backbuffer->Release();
  253. }
  254. }
  255. }
  256. return hr;
  257. }
  258. bool hook_dxgi(void)
  259. {
  260. HMODULE dxgi_module = get_system_module("dxgi.dll");
  261. if (!dxgi_module) {
  262. hlog_verbose("Failed to find dxgi.dll. Skipping hook attempt.");
  263. return false;
  264. }
  265. /* ---------------------- */
  266. void *present_addr = get_offset_addr(
  267. dxgi_module, global_hook_info->offsets.dxgi.present);
  268. void *resize_addr = get_offset_addr(
  269. dxgi_module, global_hook_info->offsets.dxgi.resize);
  270. void *present1_addr = nullptr;
  271. if (global_hook_info->offsets.dxgi.present1)
  272. present1_addr = get_offset_addr(
  273. dxgi_module, global_hook_info->offsets.dxgi.present1);
  274. DetourTransactionBegin();
  275. RealPresent = (present_t)present_addr;
  276. DetourAttach(&(PVOID &)RealPresent, hook_present);
  277. RealResizeBuffers = (resize_buffers_t)resize_addr;
  278. DetourAttach(&(PVOID &)RealResizeBuffers, hook_resize_buffers);
  279. if (present1_addr) {
  280. RealPresent1 = (present1_t)present1_addr;
  281. DetourAttach(&(PVOID &)RealPresent1, hook_present1);
  282. }
  283. const LONG error = DetourTransactionCommit();
  284. const bool success = error == NO_ERROR;
  285. if (success) {
  286. hlog("Hooked IDXGISwapChain::Present");
  287. hlog("Hooked IDXGISwapChain::ResizeBuffers");
  288. if (RealPresent1)
  289. hlog("Hooked IDXGISwapChain1::Present1");
  290. hlog("Hooked DXGI");
  291. } else {
  292. RealPresent = nullptr;
  293. RealResizeBuffers = nullptr;
  294. RealPresent1 = nullptr;
  295. hlog("Failed to attach Detours hook: %ld", error);
  296. }
  297. return success;
  298. }