d3d12-capture.cpp 11 KB

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