dxgi-capture.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 "../funchook.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. static struct func_hook release;
  19. static struct func_hook resize_buffers;
  20. static struct func_hook present;
  21. static struct func_hook present1;
  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 *, bool);
  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 ", use_queue=%d",
  75. (uint64_t)(uintptr_t)swap, (uint64_t)(uintptr_t)device,
  76. (int)global_hook_info->d3d12_use_swap_queue);
  77. for (size_t i = 0; i < dxgi_possible_swap_queue_count; ++i) {
  78. hlog("\tqueue=0x%" PRIX64,
  79. (uint64_t)(uintptr_t)dxgi_possible_swap_queues[i]);
  80. }
  81. if (!global_hook_info->d3d12_use_swap_queue ||
  82. (dxgi_possible_swap_queue_count > 0)) {
  83. data.swap = swap;
  84. data.capture = d3d12_capture;
  85. data.free = d3d12_free;
  86. return true;
  87. }
  88. }
  89. #endif
  90. hlog_verbose("Failed to setup DXGI");
  91. return false;
  92. }
  93. static ULONG STDMETHODCALLTYPE hook_release(IUnknown *unknown)
  94. {
  95. unhook(&release);
  96. release_t call = (release_t)release.call_addr;
  97. ULONG refs = call(unknown);
  98. rehook(&release);
  99. hlog_verbose("Release callback: Refs=%lu", refs);
  100. if (unknown == data.swap && refs == 0) {
  101. hlog_verbose("No more refs, so reset capture");
  102. data.swap = nullptr;
  103. data.capture = nullptr;
  104. memset(dxgi_possible_swap_queues, 0,
  105. sizeof(dxgi_possible_swap_queues));
  106. dxgi_possible_swap_queue_count = 0;
  107. dxgi_present_attempted = false;
  108. data.free();
  109. data.free = nullptr;
  110. }
  111. return refs;
  112. }
  113. static bool resize_buffers_called = false;
  114. static HRESULT STDMETHODCALLTYPE hook_resize_buffers(IDXGISwapChain *swap,
  115. UINT buffer_count,
  116. UINT width, UINT height,
  117. DXGI_FORMAT format,
  118. UINT flags)
  119. {
  120. hlog_verbose("ResizeBuffers callback");
  121. data.swap = nullptr;
  122. data.capture = nullptr;
  123. memset(dxgi_possible_swap_queues, 0, sizeof(dxgi_possible_swap_queues));
  124. dxgi_possible_swap_queue_count = 0;
  125. dxgi_present_attempted = false;
  126. if (data.free)
  127. data.free();
  128. data.free = nullptr;
  129. unhook(&resize_buffers);
  130. resize_buffers_t call = (resize_buffers_t)resize_buffers.call_addr;
  131. const HRESULT hr =
  132. call(swap, buffer_count, width, height, format, flags);
  133. rehook(&resize_buffers);
  134. resize_buffers_called = true;
  135. return hr;
  136. }
  137. static inline IUnknown *get_dxgi_backbuffer(IDXGISwapChain *swap)
  138. {
  139. IUnknown *res = nullptr;
  140. const HRESULT hr = swap->GetBuffer(0, IID_PPV_ARGS(&res));
  141. if (FAILED(hr))
  142. hlog_hr("get_dxgi_backbuffer: GetBuffer failed", hr);
  143. return res;
  144. }
  145. static void update_mismatch_count(bool match)
  146. {
  147. if (match) {
  148. swap_chain_mismatch_count = 0;
  149. } else {
  150. ++swap_chain_mismatch_count;
  151. if (swap_chain_mismatch_count == swap_chain_mismtach_limit) {
  152. data.swap = nullptr;
  153. data.capture = nullptr;
  154. memset(dxgi_possible_swap_queues, 0,
  155. sizeof(dxgi_possible_swap_queues));
  156. dxgi_possible_swap_queue_count = 0;
  157. dxgi_present_attempted = false;
  158. data.free();
  159. data.free = nullptr;
  160. swap_chain_mismatch_count = 0;
  161. }
  162. }
  163. }
  164. static HRESULT STDMETHODCALLTYPE hook_present(IDXGISwapChain *swap,
  165. UINT sync_interval, UINT flags)
  166. {
  167. const bool capture_overlay = global_hook_info->capture_overlay;
  168. const bool test_draw = (flags & DXGI_PRESENT_TEST) != 0;
  169. if (data.swap) {
  170. update_mismatch_count(swap == data.swap);
  171. }
  172. if (!data.swap && !capture_active()) {
  173. setup_dxgi(swap);
  174. }
  175. hlog_verbose(
  176. "Present callback: sync_interval=%u, flags=%u, current_swap=0x%" PRIX64
  177. ", expected_swap=0x%" PRIX64,
  178. sync_interval, flags, swap, data.swap);
  179. const bool capture = !test_draw && swap == data.swap && data.capture;
  180. if (capture && !capture_overlay) {
  181. IUnknown *backbuffer = get_dxgi_backbuffer(swap);
  182. if (backbuffer) {
  183. data.capture(swap, backbuffer, capture_overlay);
  184. backbuffer->Release();
  185. }
  186. }
  187. dxgi_presenting = true;
  188. unhook(&present);
  189. present_t call = (present_t)present.call_addr;
  190. const HRESULT hr = call(swap, sync_interval, flags);
  191. rehook(&present);
  192. dxgi_presenting = false;
  193. dxgi_present_attempted = true;
  194. if (capture && capture_overlay) {
  195. /*
  196. * It seems that the first call to Present after ResizeBuffers
  197. * will cause the backbuffer to be invalidated, so do not
  198. * perform the post-overlay capture if ResizeBuffers has
  199. * recently been called. (The backbuffer returned by
  200. * get_dxgi_backbuffer *will* be invalid otherwise)
  201. */
  202. if (resize_buffers_called) {
  203. resize_buffers_called = false;
  204. } else {
  205. IUnknown *backbuffer = get_dxgi_backbuffer(swap);
  206. if (backbuffer) {
  207. data.capture(swap, backbuffer, capture_overlay);
  208. backbuffer->Release();
  209. }
  210. }
  211. }
  212. return hr;
  213. }
  214. static HRESULT STDMETHODCALLTYPE
  215. hook_present1(IDXGISwapChain1 *swap, UINT sync_interval, UINT flags,
  216. const DXGI_PRESENT_PARAMETERS *params)
  217. {
  218. const bool capture_overlay = global_hook_info->capture_overlay;
  219. const bool test_draw = (flags & DXGI_PRESENT_TEST) != 0;
  220. if (data.swap) {
  221. update_mismatch_count(swap == data.swap);
  222. }
  223. if (!data.swap && !capture_active()) {
  224. setup_dxgi(swap);
  225. }
  226. hlog_verbose(
  227. "Present1 callback: sync_interval=%u, flags=%u, current_swap=0x%" PRIX64
  228. ", expected_swap=0x%" PRIX64,
  229. sync_interval, flags, swap, data.swap);
  230. const bool capture = !test_draw && swap == data.swap && !!data.capture;
  231. if (capture && !capture_overlay) {
  232. IUnknown *backbuffer = get_dxgi_backbuffer(swap);
  233. if (backbuffer) {
  234. DXGI_SWAP_CHAIN_DESC1 desc;
  235. swap->GetDesc1(&desc);
  236. data.capture(swap, backbuffer, capture_overlay);
  237. backbuffer->Release();
  238. }
  239. }
  240. dxgi_presenting = true;
  241. unhook(&present1);
  242. present1_t call = (present1_t)present1.call_addr;
  243. const HRESULT hr = call(swap, sync_interval, flags, params);
  244. rehook(&present1);
  245. dxgi_presenting = false;
  246. dxgi_present_attempted = true;
  247. if (capture && capture_overlay) {
  248. if (resize_buffers_called) {
  249. resize_buffers_called = false;
  250. } else {
  251. IUnknown *backbuffer = get_dxgi_backbuffer(swap);
  252. if (backbuffer) {
  253. data.capture(swap, backbuffer, capture_overlay);
  254. backbuffer->Release();
  255. }
  256. }
  257. }
  258. return hr;
  259. }
  260. bool hook_dxgi(void)
  261. {
  262. HMODULE dxgi_module = get_system_module("dxgi.dll");
  263. if (!dxgi_module) {
  264. hlog_verbose("Failed to find dxgi.dll. Skipping hook attempt.");
  265. return false;
  266. }
  267. /* ---------------------- */
  268. void *present_addr = get_offset_addr(
  269. dxgi_module, global_hook_info->offsets.dxgi.present);
  270. void *resize_addr = get_offset_addr(
  271. dxgi_module, global_hook_info->offsets.dxgi.resize);
  272. void *present1_addr = nullptr;
  273. if (global_hook_info->offsets.dxgi.present1)
  274. present1_addr = get_offset_addr(
  275. dxgi_module, global_hook_info->offsets.dxgi.present1);
  276. void *release_addr = nullptr;
  277. if (global_hook_info->offsets.dxgi2.release)
  278. release_addr = get_offset_addr(
  279. dxgi_module, global_hook_info->offsets.dxgi2.release);
  280. hook_init(&present, present_addr, (void *)hook_present,
  281. "IDXGISwapChain::Present");
  282. hlog("Hooked IDXGISwapChain::Present");
  283. hook_init(&resize_buffers, resize_addr, (void *)hook_resize_buffers,
  284. "IDXGISwapChain::ResizeBuffers");
  285. hlog("Hooked IDXGISwapChain::ResizeBuffers");
  286. if (present1_addr) {
  287. hook_init(&present1, present1_addr, (void *)hook_present1,
  288. "IDXGISwapChain1::Present1");
  289. hlog("Hooked IDXGISwapChain::Present1");
  290. }
  291. if (release_addr) {
  292. hook_init(&release, release_addr, (void *)hook_release,
  293. "IDXGISwapChain::Release");
  294. hlog("Hooked IDXGISwapChain::Release");
  295. }
  296. rehook(&resize_buffers);
  297. rehook(&present);
  298. if (present1_addr)
  299. rehook(&present1);
  300. if (release_addr)
  301. rehook(&release);
  302. hlog("Hooked DXGI");
  303. return true;
  304. }