dxgi-capture.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. #include <d3d10_1.h>
  2. #include <d3d11.h>
  3. #include <dxgi1_2.h>
  4. #include <d3dcompiler.h>
  5. #include "graphics-hook.h"
  6. #include "../funchook.h"
  7. #if COMPILE_D3D12_HOOK
  8. #include <d3d12.h>
  9. #endif
  10. typedef ULONG(STDMETHODCALLTYPE *release_t)(IUnknown *);
  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. static struct func_hook release;
  18. static struct func_hook resize_buffers;
  19. static struct func_hook present;
  20. static struct func_hook present1;
  21. thread_local bool dxgi_presenting = false;
  22. struct ID3D12CommandQueue *dxgi_possible_swap_queue = nullptr;
  23. bool dxgi_present_attempted = false;
  24. struct dxgi_swap_data {
  25. IDXGISwapChain *swap;
  26. void (*capture)(void *, void *, bool);
  27. void (*free)(void);
  28. };
  29. static struct dxgi_swap_data data = {};
  30. static bool setup_dxgi(IDXGISwapChain *swap)
  31. {
  32. IUnknown *device;
  33. HRESULT hr;
  34. hr = swap->GetDevice(__uuidof(ID3D11Device), (void **)&device);
  35. if (SUCCEEDED(hr)) {
  36. ID3D11Device *d3d11 = static_cast<ID3D11Device *>(device);
  37. D3D_FEATURE_LEVEL level = d3d11->GetFeatureLevel();
  38. device->Release();
  39. if (level >= D3D_FEATURE_LEVEL_11_0) {
  40. data.swap = swap;
  41. data.capture = d3d11_capture;
  42. data.free = d3d11_free;
  43. return true;
  44. }
  45. }
  46. hr = swap->GetDevice(__uuidof(ID3D10Device), (void **)&device);
  47. if (SUCCEEDED(hr)) {
  48. device->Release();
  49. data.swap = swap;
  50. data.capture = d3d10_capture;
  51. data.free = d3d10_free;
  52. return true;
  53. }
  54. hr = swap->GetDevice(__uuidof(ID3D11Device), (void **)&device);
  55. if (SUCCEEDED(hr)) {
  56. device->Release();
  57. data.swap = swap;
  58. data.capture = d3d11_capture;
  59. data.free = d3d11_free;
  60. return true;
  61. }
  62. #if COMPILE_D3D12_HOOK
  63. hr = swap->GetDevice(__uuidof(ID3D12Device), (void **)&device);
  64. if (SUCCEEDED(hr)) {
  65. device->Release();
  66. if (!global_hook_info->d3d12_use_swap_queue ||
  67. dxgi_possible_swap_queue) {
  68. data.swap = swap;
  69. data.capture = d3d12_capture;
  70. data.free = d3d12_free;
  71. return true;
  72. }
  73. }
  74. #endif
  75. return false;
  76. }
  77. static ULONG STDMETHODCALLTYPE hook_release(IUnknown *unknown)
  78. {
  79. unhook(&release);
  80. release_t call = (release_t)release.call_addr;
  81. ULONG refs = call(unknown);
  82. rehook(&release);
  83. if (unknown == data.swap && refs == 0) {
  84. data.swap = nullptr;
  85. data.capture = nullptr;
  86. dxgi_possible_swap_queue = nullptr;
  87. dxgi_present_attempted = false;
  88. data.free();
  89. data.free = nullptr;
  90. }
  91. return refs;
  92. }
  93. static bool resize_buffers_called = false;
  94. static HRESULT STDMETHODCALLTYPE hook_resize_buffers(IDXGISwapChain *swap,
  95. UINT buffer_count,
  96. UINT width, UINT height,
  97. DXGI_FORMAT format,
  98. UINT flags)
  99. {
  100. HRESULT hr;
  101. data.swap = nullptr;
  102. data.capture = nullptr;
  103. dxgi_possible_swap_queue = nullptr;
  104. dxgi_present_attempted = false;
  105. if (data.free)
  106. data.free();
  107. data.free = nullptr;
  108. unhook(&resize_buffers);
  109. resize_buffers_t call = (resize_buffers_t)resize_buffers.call_addr;
  110. hr = call(swap, buffer_count, width, height, format, flags);
  111. rehook(&resize_buffers);
  112. resize_buffers_called = true;
  113. return hr;
  114. }
  115. static inline IUnknown *get_dxgi_backbuffer(IDXGISwapChain *swap)
  116. {
  117. IDXGIResource *res = nullptr;
  118. HRESULT hr;
  119. hr = swap->GetBuffer(0, __uuidof(IUnknown), (void **)&res);
  120. if (FAILED(hr))
  121. hlog_hr("get_dxgi_backbuffer: GetBuffer failed", hr);
  122. return res;
  123. }
  124. static HRESULT STDMETHODCALLTYPE hook_present(IDXGISwapChain *swap,
  125. UINT sync_interval, UINT flags)
  126. {
  127. IUnknown *backbuffer = nullptr;
  128. bool capture_overlay = global_hook_info->capture_overlay;
  129. bool test_draw = (flags & DXGI_PRESENT_TEST) != 0;
  130. bool capture;
  131. HRESULT hr;
  132. if (!data.swap && !capture_active()) {
  133. setup_dxgi(swap);
  134. }
  135. capture = !test_draw && swap == data.swap && !!data.capture;
  136. if (capture && !capture_overlay) {
  137. backbuffer = get_dxgi_backbuffer(swap);
  138. if (!!backbuffer) {
  139. data.capture(swap, backbuffer, capture_overlay);
  140. backbuffer->Release();
  141. }
  142. }
  143. dxgi_presenting = true;
  144. unhook(&present);
  145. present_t call = (present_t)present.call_addr;
  146. hr = call(swap, sync_interval, flags);
  147. rehook(&present);
  148. dxgi_presenting = false;
  149. dxgi_present_attempted = true;
  150. if (capture && capture_overlay) {
  151. /*
  152. * It seems that the first call to Present after ResizeBuffers
  153. * will cause the backbuffer to be invalidated, so do not
  154. * perform the post-overlay capture if ResizeBuffers has
  155. * recently been called. (The backbuffer returned by
  156. * get_dxgi_backbuffer *will* be invalid otherwise)
  157. */
  158. if (resize_buffers_called) {
  159. resize_buffers_called = false;
  160. } else {
  161. backbuffer = get_dxgi_backbuffer(swap);
  162. if (!!backbuffer) {
  163. data.capture(swap, backbuffer, capture_overlay);
  164. backbuffer->Release();
  165. }
  166. }
  167. }
  168. return hr;
  169. }
  170. static HRESULT STDMETHODCALLTYPE
  171. hook_present1(IDXGISwapChain1 *swap, UINT sync_interval, UINT flags,
  172. const DXGI_PRESENT_PARAMETERS *params)
  173. {
  174. IUnknown *backbuffer = nullptr;
  175. bool capture_overlay = global_hook_info->capture_overlay;
  176. bool test_draw = (flags & DXGI_PRESENT_TEST) != 0;
  177. bool capture;
  178. HRESULT hr;
  179. if (!data.swap && !capture_active()) {
  180. setup_dxgi(swap);
  181. }
  182. capture = !test_draw && swap == data.swap && !!data.capture;
  183. if (capture && !capture_overlay) {
  184. backbuffer = get_dxgi_backbuffer(swap);
  185. if (!!backbuffer) {
  186. DXGI_SWAP_CHAIN_DESC1 desc;
  187. swap->GetDesc1(&desc);
  188. data.capture(swap, backbuffer, capture_overlay);
  189. backbuffer->Release();
  190. }
  191. }
  192. dxgi_presenting = true;
  193. unhook(&present1);
  194. present1_t call = (present1_t)present1.call_addr;
  195. hr = call(swap, sync_interval, flags, params);
  196. rehook(&present1);
  197. dxgi_presenting = false;
  198. dxgi_present_attempted = true;
  199. if (capture && capture_overlay) {
  200. if (resize_buffers_called) {
  201. resize_buffers_called = false;
  202. } else {
  203. backbuffer = get_dxgi_backbuffer(swap);
  204. if (!!backbuffer) {
  205. data.capture(swap, backbuffer, capture_overlay);
  206. backbuffer->Release();
  207. }
  208. }
  209. }
  210. return hr;
  211. }
  212. bool hook_dxgi(void)
  213. {
  214. HMODULE dxgi_module = get_system_module("dxgi.dll");
  215. void *present_addr;
  216. void *resize_addr;
  217. void *present1_addr = nullptr;
  218. void *release_addr = nullptr;
  219. if (!dxgi_module) {
  220. return false;
  221. }
  222. /* ---------------------- */
  223. present_addr = get_offset_addr(dxgi_module,
  224. global_hook_info->offsets.dxgi.present);
  225. resize_addr = get_offset_addr(dxgi_module,
  226. global_hook_info->offsets.dxgi.resize);
  227. if (global_hook_info->offsets.dxgi.present1)
  228. present1_addr = get_offset_addr(
  229. dxgi_module, global_hook_info->offsets.dxgi.present1);
  230. if (global_hook_info->offsets.dxgi2.release)
  231. release_addr = get_offset_addr(
  232. dxgi_module, global_hook_info->offsets.dxgi2.release);
  233. hook_init(&present, present_addr, (void *)hook_present,
  234. "IDXGISwapChain::Present");
  235. hook_init(&resize_buffers, resize_addr, (void *)hook_resize_buffers,
  236. "IDXGISwapChain::ResizeBuffers");
  237. if (present1_addr)
  238. hook_init(&present1, present1_addr, (void *)hook_present1,
  239. "IDXGISwapChain1::Present1");
  240. if (release_addr)
  241. hook_init(&release, release_addr, (void *)hook_release,
  242. "IDXGISwapChain::Release");
  243. rehook(&resize_buffers);
  244. rehook(&present);
  245. if (present1_addr)
  246. rehook(&present1);
  247. if (release_addr)
  248. rehook(&release);
  249. hlog("Hooked DXGI");
  250. return true;
  251. }