d3d12-capture.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. #include <windows.h>
  2. #include "graphics-hook.h"
  3. #if COMPILE_D3D12_HOOK
  4. #include <d3d11on12.h>
  5. #include <d3d12.h>
  6. #include <dxgi1_4.h>
  7. #include <inttypes.h>
  8. #include <detours.h>
  9. #include "dxgi-helpers.hpp"
  10. #define MAX_BACKBUFFERS 8
  11. typedef HRESULT(STDMETHODCALLTYPE *PFN_ExecuteCommandLists)(
  12. ID3D12CommandQueue *, UINT, ID3D12CommandList *const *);
  13. static PFN_ExecuteCommandLists RealExecuteCommandLists = nullptr;
  14. struct d3d12_data {
  15. uint32_t cx;
  16. uint32_t cy;
  17. DXGI_FORMAT format;
  18. bool using_shtex;
  19. bool multisampled;
  20. bool dxgi_1_4;
  21. ID3D11Device *device11;
  22. ID3D11DeviceContext *context11;
  23. ID3D11On12Device *device11on12;
  24. union {
  25. struct {
  26. struct shtex_data *shtex_info;
  27. UINT backbuffer_count;
  28. UINT cur_backbuffer;
  29. ID3D11Texture2D *copy_tex;
  30. HANDLE handle;
  31. };
  32. };
  33. };
  34. static struct d3d12_data data = {};
  35. extern thread_local int dxgi_presenting;
  36. extern ID3D12CommandQueue *dxgi_possible_swap_queues[8];
  37. extern size_t dxgi_possible_swap_queue_count;
  38. extern bool dxgi_present_attempted;
  39. void d3d12_free(void)
  40. {
  41. if (data.copy_tex)
  42. data.copy_tex->Release();
  43. if (data.device11)
  44. data.device11->Release();
  45. if (data.context11)
  46. data.context11->Release();
  47. if (data.device11on12)
  48. data.device11on12->Release();
  49. capture_free();
  50. memset(&data, 0, sizeof(data));
  51. hlog("----------------- d3d12 capture freed ----------------");
  52. }
  53. static bool create_d3d12_tex(UINT count)
  54. {
  55. HRESULT hr;
  56. if (count == 0)
  57. return false;
  58. data.backbuffer_count = count;
  59. D3D11_TEXTURE2D_DESC desc11 = {};
  60. desc11.Width = data.cx;
  61. desc11.Height = data.cy;
  62. desc11.MipLevels = 1;
  63. desc11.ArraySize = 1;
  64. desc11.Format = apply_dxgi_format_typeless(
  65. data.format, global_hook_info->allow_srgb_alias);
  66. desc11.SampleDesc.Count = 1;
  67. desc11.BindFlags = D3D11_BIND_SHADER_RESOURCE;
  68. desc11.MiscFlags = D3D11_RESOURCE_MISC_SHARED;
  69. hr = data.device11->CreateTexture2D(&desc11, nullptr, &data.copy_tex);
  70. if (FAILED(hr)) {
  71. hlog_hr("create_d3d12_tex: creation of d3d11 copy tex failed",
  72. hr);
  73. return false;
  74. }
  75. IDXGIResource *dxgi_res;
  76. hr = data.copy_tex->QueryInterface(&dxgi_res);
  77. if (FAILED(hr)) {
  78. hlog_hr("create_d3d12_tex: failed to query "
  79. "IDXGIResource interface from texture",
  80. hr);
  81. return false;
  82. }
  83. hr = dxgi_res->GetSharedHandle(&data.handle);
  84. dxgi_res->Release();
  85. if (FAILED(hr)) {
  86. hlog_hr("create_d3d12_tex: failed to get shared handle", hr);
  87. return false;
  88. }
  89. return true;
  90. }
  91. static bool d3d12_init_11on12(ID3D12Device *device)
  92. {
  93. static HMODULE d3d11 = nullptr;
  94. static PFN_D3D11ON12_CREATE_DEVICE create_11_on_12 = nullptr;
  95. static bool initialized_11 = false;
  96. static bool initialized_func = false;
  97. if (!initialized_11 && !d3d11) {
  98. d3d11 = load_system_library("d3d11.dll");
  99. if (!d3d11) {
  100. hlog("d3d12_init_11on12: failed to load d3d11");
  101. }
  102. initialized_11 = true;
  103. }
  104. if (!d3d11) {
  105. return false;
  106. }
  107. if (!initialized_func && !create_11_on_12) {
  108. create_11_on_12 = (PFN_D3D11ON12_CREATE_DEVICE)GetProcAddress(
  109. d3d11, "D3D11On12CreateDevice");
  110. if (!create_11_on_12) {
  111. hlog("d3d12_init_11on12: Failed to get "
  112. "D3D11On12CreateDevice address");
  113. }
  114. initialized_func = true;
  115. }
  116. if (!create_11_on_12) {
  117. return false;
  118. }
  119. bool created = false;
  120. for (size_t i = 0; i < dxgi_possible_swap_queue_count; ++i) {
  121. hlog("d3d12_init_11on12: creating 11 device: queue=0x%" PRIX64,
  122. (uint64_t)(uintptr_t)dxgi_possible_swap_queues[i]);
  123. IUnknown *const queue = dxgi_possible_swap_queues[i];
  124. const HRESULT hr = create_11_on_12(device, 0, nullptr, 0,
  125. &queue, 1, 0, &data.device11,
  126. &data.context11, nullptr);
  127. created = SUCCEEDED(hr);
  128. if (created) {
  129. break;
  130. }
  131. hlog_hr("d3d12_init_11on12: failed to create 11 device", hr);
  132. }
  133. if (!created) {
  134. return false;
  135. }
  136. memset(dxgi_possible_swap_queues, 0, sizeof(dxgi_possible_swap_queues));
  137. dxgi_possible_swap_queue_count = 0;
  138. dxgi_present_attempted = false;
  139. const HRESULT hr =
  140. data.device11->QueryInterface(IID_PPV_ARGS(&data.device11on12));
  141. if (FAILED(hr)) {
  142. hlog_hr("d3d12_init_11on12: failed to query 11on12 device", hr);
  143. return false;
  144. }
  145. return true;
  146. }
  147. static bool d3d12_shtex_init(ID3D12Device *device, HWND window, UINT count)
  148. {
  149. if (!d3d12_init_11on12(device)) {
  150. return false;
  151. }
  152. if (!create_d3d12_tex(count)) {
  153. return false;
  154. }
  155. if (!capture_init_shtex(&data.shtex_info, window, data.cx, data.cy,
  156. data.format, false, (uintptr_t)data.handle)) {
  157. return false;
  158. }
  159. hlog("d3d12 shared texture capture successful");
  160. return true;
  161. }
  162. static inline UINT d3d12_init_format(IDXGISwapChain *swap, HWND &window)
  163. {
  164. DXGI_SWAP_CHAIN_DESC desc;
  165. IDXGISwapChain3 *swap3;
  166. HRESULT hr;
  167. hr = swap->GetDesc(&desc);
  168. if (FAILED(hr)) {
  169. hlog_hr("d3d12_init_format: swap->GetDesc failed", hr);
  170. return 0;
  171. }
  172. print_swap_desc(&desc);
  173. data.format = strip_dxgi_format_srgb(desc.BufferDesc.Format);
  174. data.multisampled = desc.SampleDesc.Count > 1;
  175. window = desc.OutputWindow;
  176. data.cx = desc.BufferDesc.Width;
  177. data.cy = desc.BufferDesc.Height;
  178. hr = swap->QueryInterface(&swap3);
  179. if (SUCCEEDED(hr)) {
  180. data.dxgi_1_4 = true;
  181. hlog("We're DXGI1.4 boys!");
  182. swap3->Release();
  183. }
  184. UINT count = desc.SwapEffect == DXGI_SWAP_EFFECT_DISCARD
  185. ? 1
  186. : desc.BufferCount;
  187. if (count == 1)
  188. data.dxgi_1_4 = false;
  189. if (count > MAX_BACKBUFFERS) {
  190. hlog("Somehow it's using more than the max backbuffers. "
  191. "Not sure why anyone would do that.");
  192. count = 1;
  193. data.dxgi_1_4 = false;
  194. }
  195. return count;
  196. }
  197. static void d3d12_init(IDXGISwapChain *swap)
  198. {
  199. ID3D12Device *device = nullptr;
  200. const HRESULT hr = swap->GetDevice(IID_PPV_ARGS(&device));
  201. if (SUCCEEDED(hr)) {
  202. hlog("d3d12_init: device=0x%" PRIX64,
  203. (uint64_t)(uintptr_t)device);
  204. HWND window;
  205. UINT count = d3d12_init_format(swap, window);
  206. if (count > 0) {
  207. if (global_hook_info->force_shmem) {
  208. hlog("d3d12_init: shared memory capture currently "
  209. "unsupported; ignoring");
  210. }
  211. if (!d3d12_shtex_init(device, window, count))
  212. d3d12_free();
  213. }
  214. device->Release();
  215. } else {
  216. hlog_hr("d3d12_init: failed to get device from swap", hr);
  217. }
  218. }
  219. static inline void d3d12_copy_texture(ID3D11Resource *dst, ID3D11Resource *src)
  220. {
  221. if (data.multisampled) {
  222. data.context11->ResolveSubresource(dst, 0, src, 0, data.format);
  223. } else {
  224. data.context11->CopyResource(dst, src);
  225. }
  226. }
  227. static inline void d3d12_shtex_capture(IDXGISwapChain *swap)
  228. {
  229. bool dxgi_1_4 = data.dxgi_1_4;
  230. UINT cur_idx;
  231. if (dxgi_1_4) {
  232. IDXGISwapChain3 *swap3 =
  233. reinterpret_cast<IDXGISwapChain3 *>(swap);
  234. cur_idx = swap3->GetCurrentBackBufferIndex();
  235. } else {
  236. cur_idx = data.cur_backbuffer;
  237. }
  238. ID3D12Resource *backbuffer12;
  239. if (SUCCEEDED(swap->GetBuffer(cur_idx, IID_PPV_ARGS(&backbuffer12)))) {
  240. D3D11_RESOURCE_FLAGS rf11 = {};
  241. ID3D11Resource *backbuffer;
  242. if (SUCCEEDED(data.device11on12->CreateWrappedResource(
  243. backbuffer12, &rf11, D3D12_RESOURCE_STATE_PRESENT,
  244. D3D12_RESOURCE_STATE_PRESENT,
  245. IID_PPV_ARGS(&backbuffer)))) {
  246. data.device11on12->AcquireWrappedResources(&backbuffer,
  247. 1);
  248. d3d12_copy_texture(data.copy_tex, backbuffer);
  249. data.device11on12->ReleaseWrappedResources(&backbuffer,
  250. 1);
  251. data.context11->Flush();
  252. if (!dxgi_1_4) {
  253. if (++data.cur_backbuffer >=
  254. data.backbuffer_count)
  255. data.cur_backbuffer = 0;
  256. }
  257. backbuffer->Release();
  258. }
  259. backbuffer12->Release();
  260. }
  261. }
  262. void d3d12_capture(void *swap_ptr, void *)
  263. {
  264. IDXGISwapChain *swap = (IDXGISwapChain *)swap_ptr;
  265. if (capture_should_stop()) {
  266. d3d12_free();
  267. }
  268. if (capture_should_init()) {
  269. d3d12_init(swap);
  270. }
  271. if (capture_ready()) {
  272. d3d12_shtex_capture(swap);
  273. }
  274. }
  275. static bool try_append_queue_if_unique(ID3D12CommandQueue *queue)
  276. {
  277. for (size_t i = 0; i < dxgi_possible_swap_queue_count; ++i) {
  278. if (dxgi_possible_swap_queues[i] == queue)
  279. return false;
  280. }
  281. dxgi_possible_swap_queues[dxgi_possible_swap_queue_count] = queue;
  282. ++dxgi_possible_swap_queue_count;
  283. return true;
  284. }
  285. static HRESULT STDMETHODCALLTYPE
  286. hook_execute_command_lists(ID3D12CommandQueue *queue, UINT NumCommandLists,
  287. ID3D12CommandList *const *ppCommandLists)
  288. {
  289. hlog_verbose("ExecuteCommandLists callback: queue=0x%" PRIX64,
  290. (uint64_t)(uintptr_t)queue);
  291. if (dxgi_possible_swap_queue_count <
  292. _countof(dxgi_possible_swap_queues)) {
  293. if ((dxgi_presenting > 0) &&
  294. (queue->GetDesc().Type == D3D12_COMMAND_LIST_TYPE_DIRECT)) {
  295. if (try_append_queue_if_unique(queue)) {
  296. hlog("Remembering D3D12 queue from present: queue=0x%" PRIX64,
  297. (uint64_t)(uintptr_t)queue);
  298. }
  299. } else if (dxgi_present_attempted &&
  300. (queue->GetDesc().Type ==
  301. D3D12_COMMAND_LIST_TYPE_DIRECT)) {
  302. if (try_append_queue_if_unique(queue)) {
  303. hlog("Remembering D3D12 queue from first direct submit after present: queue=0x%" PRIX64,
  304. (uint64_t)(uintptr_t)queue);
  305. }
  306. } else {
  307. hlog_verbose("Ignoring D3D12 queue=0x%" PRIX64,
  308. (uint64_t)(uintptr_t)queue);
  309. }
  310. }
  311. return RealExecuteCommandLists(queue, NumCommandLists, ppCommandLists);
  312. }
  313. static bool
  314. manually_get_d3d12_addrs(HMODULE d3d12_module,
  315. PFN_ExecuteCommandLists *execute_command_lists_addr)
  316. {
  317. PFN_D3D12_CREATE_DEVICE create =
  318. (PFN_D3D12_CREATE_DEVICE)GetProcAddress(d3d12_module,
  319. "D3D12CreateDevice");
  320. if (!create) {
  321. hlog("Failed to load D3D12CreateDevice");
  322. return false;
  323. }
  324. bool success = false;
  325. ID3D12Device *device;
  326. if (SUCCEEDED(create(NULL, D3D_FEATURE_LEVEL_11_0,
  327. IID_PPV_ARGS(&device)))) {
  328. D3D12_COMMAND_QUEUE_DESC desc{};
  329. ID3D12CommandQueue *queue;
  330. HRESULT hr =
  331. device->CreateCommandQueue(&desc, IID_PPV_ARGS(&queue));
  332. success = SUCCEEDED(hr);
  333. if (success) {
  334. void **queue_vtable = *(void ***)queue;
  335. *execute_command_lists_addr =
  336. (PFN_ExecuteCommandLists)queue_vtable[10];
  337. queue->Release();
  338. } else {
  339. hlog("Failed to create D3D12 command queue");
  340. }
  341. device->Release();
  342. } else {
  343. hlog("Failed to create D3D12 device");
  344. }
  345. return success;
  346. }
  347. bool hook_d3d12(void)
  348. {
  349. HMODULE d3d12_module = get_system_module("d3d12.dll");
  350. if (!d3d12_module) {
  351. hlog_verbose(
  352. "Failed to find d3d12.dll. Skipping hook attempt.");
  353. return false;
  354. }
  355. PFN_ExecuteCommandLists execute_command_lists_addr = nullptr;
  356. if (!manually_get_d3d12_addrs(d3d12_module,
  357. &execute_command_lists_addr)) {
  358. hlog("Failed to get D3D12 values");
  359. return true;
  360. }
  361. if (!execute_command_lists_addr) {
  362. hlog("Invalid D3D12 values");
  363. return true;
  364. }
  365. DetourTransactionBegin();
  366. RealExecuteCommandLists = execute_command_lists_addr;
  367. DetourAttach(&(PVOID &)RealExecuteCommandLists,
  368. hook_execute_command_lists);
  369. const LONG error = DetourTransactionCommit();
  370. const bool success = error == NO_ERROR;
  371. if (success) {
  372. hlog("Hooked ID3D12CommandQueue::ExecuteCommandLists");
  373. hlog("Hooked D3D12");
  374. } else {
  375. RealExecuteCommandLists = nullptr;
  376. hlog("Failed to attach Detours hook: %ld", error);
  377. }
  378. return success;
  379. }
  380. #endif