dxgi-capture.cpp 9.1 KB

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