dxgi-capture.cpp 6.0 KB

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