dxgi-capture.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. data.swap = nullptr;
  101. data.capture = nullptr;
  102. dxgi_possible_swap_queue = nullptr;
  103. dxgi_present_attempted = false;
  104. if (data.free)
  105. data.free();
  106. data.free = nullptr;
  107. unhook(&resize_buffers);
  108. resize_buffers_t call = (resize_buffers_t)resize_buffers.call_addr;
  109. const HRESULT hr =
  110. 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. IUnknown *res = nullptr;
  118. const HRESULT hr = swap->GetBuffer(0, IID_PPV_ARGS(&res));
  119. if (FAILED(hr))
  120. hlog_hr("get_dxgi_backbuffer: GetBuffer failed", hr);
  121. return res;
  122. }
  123. static HRESULT STDMETHODCALLTYPE hook_present(IDXGISwapChain *swap,
  124. UINT sync_interval, UINT flags)
  125. {
  126. const bool capture_overlay = global_hook_info->capture_overlay;
  127. const bool test_draw = (flags & DXGI_PRESENT_TEST) != 0;
  128. if (!data.swap && !capture_active()) {
  129. setup_dxgi(swap);
  130. }
  131. const bool capture = !test_draw && swap == data.swap && data.capture;
  132. if (capture && !capture_overlay) {
  133. IUnknown *backbuffer = get_dxgi_backbuffer(swap);
  134. if (backbuffer) {
  135. data.capture(swap, backbuffer, capture_overlay);
  136. backbuffer->Release();
  137. }
  138. }
  139. dxgi_presenting = true;
  140. unhook(&present);
  141. present_t call = (present_t)present.call_addr;
  142. const HRESULT hr = call(swap, sync_interval, flags);
  143. rehook(&present);
  144. dxgi_presenting = false;
  145. dxgi_present_attempted = true;
  146. if (capture && capture_overlay) {
  147. /*
  148. * It seems that the first call to Present after ResizeBuffers
  149. * will cause the backbuffer to be invalidated, so do not
  150. * perform the post-overlay capture if ResizeBuffers has
  151. * recently been called. (The backbuffer returned by
  152. * get_dxgi_backbuffer *will* be invalid otherwise)
  153. */
  154. if (resize_buffers_called) {
  155. resize_buffers_called = false;
  156. } else {
  157. IUnknown *backbuffer = get_dxgi_backbuffer(swap);
  158. if (backbuffer) {
  159. data.capture(swap, backbuffer, capture_overlay);
  160. backbuffer->Release();
  161. }
  162. }
  163. }
  164. return hr;
  165. }
  166. static HRESULT STDMETHODCALLTYPE
  167. hook_present1(IDXGISwapChain1 *swap, UINT sync_interval, UINT flags,
  168. const DXGI_PRESENT_PARAMETERS *params)
  169. {
  170. const bool capture_overlay = global_hook_info->capture_overlay;
  171. const bool test_draw = (flags & DXGI_PRESENT_TEST) != 0;
  172. if (!data.swap && !capture_active()) {
  173. setup_dxgi(swap);
  174. }
  175. const bool capture = !test_draw && swap == data.swap && data.capture;
  176. if (capture && !capture_overlay) {
  177. IUnknown *backbuffer = get_dxgi_backbuffer(swap);
  178. if (backbuffer) {
  179. DXGI_SWAP_CHAIN_DESC1 desc;
  180. swap->GetDesc1(&desc);
  181. data.capture(swap, backbuffer, capture_overlay);
  182. backbuffer->Release();
  183. }
  184. }
  185. dxgi_presenting = true;
  186. unhook(&present1);
  187. present1_t call = (present1_t)present1.call_addr;
  188. const HRESULT hr = call(swap, sync_interval, flags, params);
  189. rehook(&present1);
  190. dxgi_presenting = false;
  191. dxgi_present_attempted = true;
  192. if (capture && capture_overlay) {
  193. if (resize_buffers_called) {
  194. resize_buffers_called = false;
  195. } else {
  196. IUnknown *backbuffer = get_dxgi_backbuffer(swap);
  197. if (backbuffer) {
  198. data.capture(swap, backbuffer, capture_overlay);
  199. backbuffer->Release();
  200. }
  201. }
  202. }
  203. return hr;
  204. }
  205. bool hook_dxgi(void)
  206. {
  207. HMODULE dxgi_module = get_system_module("dxgi.dll");
  208. if (!dxgi_module) {
  209. return false;
  210. }
  211. /* ---------------------- */
  212. void *present_addr = get_offset_addr(
  213. dxgi_module, global_hook_info->offsets.dxgi.present);
  214. void *resize_addr = get_offset_addr(
  215. dxgi_module, global_hook_info->offsets.dxgi.resize);
  216. void *present1_addr = nullptr;
  217. if (global_hook_info->offsets.dxgi.present1)
  218. present1_addr = get_offset_addr(
  219. dxgi_module, global_hook_info->offsets.dxgi.present1);
  220. void *release_addr = nullptr;
  221. if (global_hook_info->offsets.dxgi2.release)
  222. release_addr = get_offset_addr(
  223. dxgi_module, global_hook_info->offsets.dxgi2.release);
  224. hook_init(&present, present_addr, (void *)hook_present,
  225. "IDXGISwapChain::Present");
  226. hook_init(&resize_buffers, resize_addr, (void *)hook_resize_buffers,
  227. "IDXGISwapChain::ResizeBuffers");
  228. if (present1_addr)
  229. hook_init(&present1, present1_addr, (void *)hook_present1,
  230. "IDXGISwapChain1::Present1");
  231. if (release_addr)
  232. hook_init(&release, release_addr, (void *)hook_release,
  233. "IDXGISwapChain::Release");
  234. rehook(&resize_buffers);
  235. rehook(&present);
  236. if (present1_addr)
  237. rehook(&present1);
  238. if (release_addr)
  239. rehook(&release);
  240. hlog("Hooked DXGI");
  241. return true;
  242. }