dxgi-capture.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. if (should_passthrough()) {
  160. dxgi_presenting = true;
  161. const HRESULT hr = RealPresent(swap, sync_interval, flags);
  162. dxgi_presenting = false;
  163. return hr;
  164. }
  165. const bool capture_overlay = global_hook_info->capture_overlay;
  166. const bool test_draw = (flags & DXGI_PRESENT_TEST) != 0;
  167. if (data.swap) {
  168. update_mismatch_count(swap == data.swap);
  169. }
  170. if (!data.swap && !capture_active()) {
  171. setup_dxgi(swap);
  172. }
  173. hlog_verbose(
  174. "Present callback: sync_interval=%u, flags=%u, current_swap=0x%" PRIX64
  175. ", expected_swap=0x%" PRIX64,
  176. sync_interval, flags, swap, data.swap);
  177. const bool capture = !test_draw && swap == data.swap && data.capture;
  178. if (capture && !capture_overlay) {
  179. IUnknown *backbuffer = get_dxgi_backbuffer(swap);
  180. if (backbuffer) {
  181. data.capture(swap, backbuffer);
  182. backbuffer->Release();
  183. }
  184. }
  185. dxgi_presenting = true;
  186. const HRESULT hr = RealPresent(swap, sync_interval, flags);
  187. dxgi_presenting = false;
  188. dxgi_present_attempted = true;
  189. if (capture && capture_overlay) {
  190. /*
  191. * It seems that the first call to Present after ResizeBuffers
  192. * will cause the backbuffer to be invalidated, so do not
  193. * perform the post-overlay capture if ResizeBuffers has
  194. * recently been called. (The backbuffer returned by
  195. * get_dxgi_backbuffer *will* be invalid otherwise)
  196. */
  197. if (resize_buffers_called) {
  198. resize_buffers_called = false;
  199. } else {
  200. IUnknown *backbuffer = get_dxgi_backbuffer(swap);
  201. if (backbuffer) {
  202. data.capture(swap, backbuffer);
  203. backbuffer->Release();
  204. }
  205. }
  206. }
  207. return hr;
  208. }
  209. static HRESULT STDMETHODCALLTYPE
  210. hook_present1(IDXGISwapChain1 *swap, UINT sync_interval, UINT flags,
  211. const DXGI_PRESENT_PARAMETERS *params)
  212. {
  213. if (should_passthrough()) {
  214. dxgi_presenting = true;
  215. const HRESULT hr =
  216. RealPresent1(swap, sync_interval, flags, params);
  217. dxgi_presenting = false;
  218. return hr;
  219. }
  220. const bool capture_overlay = global_hook_info->capture_overlay;
  221. const bool test_draw = (flags & DXGI_PRESENT_TEST) != 0;
  222. if (data.swap) {
  223. update_mismatch_count(swap == data.swap);
  224. }
  225. if (!data.swap && !capture_active()) {
  226. setup_dxgi(swap);
  227. }
  228. hlog_verbose(
  229. "Present1 callback: sync_interval=%u, flags=%u, current_swap=0x%" PRIX64
  230. ", expected_swap=0x%" PRIX64,
  231. sync_interval, flags, swap, data.swap);
  232. const bool capture = !test_draw && swap == data.swap && !!data.capture;
  233. if (capture && !capture_overlay) {
  234. IUnknown *backbuffer = get_dxgi_backbuffer(swap);
  235. if (backbuffer) {
  236. DXGI_SWAP_CHAIN_DESC1 desc;
  237. swap->GetDesc1(&desc);
  238. data.capture(swap, backbuffer);
  239. backbuffer->Release();
  240. }
  241. }
  242. dxgi_presenting = true;
  243. const HRESULT hr = RealPresent1(swap, sync_interval, flags, params);
  244. dxgi_presenting = false;
  245. dxgi_present_attempted = true;
  246. if (capture && capture_overlay) {
  247. if (resize_buffers_called) {
  248. resize_buffers_called = false;
  249. } else {
  250. IUnknown *backbuffer = get_dxgi_backbuffer(swap);
  251. if (backbuffer) {
  252. data.capture(swap, backbuffer);
  253. backbuffer->Release();
  254. }
  255. }
  256. }
  257. return hr;
  258. }
  259. bool hook_dxgi(void)
  260. {
  261. HMODULE dxgi_module = get_system_module("dxgi.dll");
  262. if (!dxgi_module) {
  263. hlog_verbose("Failed to find dxgi.dll. Skipping hook attempt.");
  264. return false;
  265. }
  266. /* ---------------------- */
  267. void *present_addr = get_offset_addr(
  268. dxgi_module, global_hook_info->offsets.dxgi.present);
  269. void *resize_addr = get_offset_addr(
  270. dxgi_module, global_hook_info->offsets.dxgi.resize);
  271. void *present1_addr = nullptr;
  272. if (global_hook_info->offsets.dxgi.present1)
  273. present1_addr = get_offset_addr(
  274. dxgi_module, global_hook_info->offsets.dxgi.present1);
  275. void *release_addr = nullptr;
  276. if (global_hook_info->offsets.dxgi2.release)
  277. release_addr = get_offset_addr(
  278. dxgi_module, global_hook_info->offsets.dxgi2.release);
  279. DetourTransactionBegin();
  280. RealPresent = (present_t)present_addr;
  281. DetourAttach(&(PVOID &)RealPresent, hook_present);
  282. RealResizeBuffers = (resize_buffers_t)resize_addr;
  283. DetourAttach(&(PVOID &)RealResizeBuffers, hook_resize_buffers);
  284. if (present1_addr) {
  285. RealPresent1 = (present1_t)present1_addr;
  286. DetourAttach(&(PVOID &)RealPresent1, hook_present1);
  287. }
  288. if (release_addr) {
  289. RealRelease = (release_t)release_addr;
  290. DetourAttach(&(PVOID &)RealRelease, hook_release);
  291. }
  292. const LONG error = DetourTransactionCommit();
  293. const bool success = error == NO_ERROR;
  294. if (success) {
  295. hlog("Hooked IDXGISwapChain::Present");
  296. hlog("Hooked IDXGISwapChain::ResizeBuffers");
  297. if (RealPresent1)
  298. hlog("Hooked IDXGISwapChain1::Present1");
  299. if (RealRelease)
  300. hlog("Hooked IDXGISwapChain::Release");
  301. hlog("Hooked DXGI");
  302. } else {
  303. RealPresent = nullptr;
  304. RealResizeBuffers = nullptr;
  305. RealPresent1 = nullptr;
  306. RealRelease = nullptr;
  307. hlog("Failed to attach Detours hook: %ld", error);
  308. }
  309. return success;
  310. }