d3d12-capture.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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 bool 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. data.format = strip_dxgi_format_srgb(desc.BufferDesc.Format);
  196. data.multisampled = desc.SampleDesc.Count > 1;
  197. window = desc.OutputWindow;
  198. data.cx = desc.BufferDesc.Width;
  199. data.cy = desc.BufferDesc.Height;
  200. hr = swap->QueryInterface(&swap3);
  201. if (SUCCEEDED(hr)) {
  202. data.dxgi_1_4 = true;
  203. hlog("We're DXGI1.4 boys!");
  204. swap3->Release();
  205. }
  206. hlog("Buffer count: %d, swap effect: %d", (int)desc.BufferCount,
  207. (int)desc.SwapEffect);
  208. bb.count = desc.SwapEffect == DXGI_SWAP_EFFECT_DISCARD
  209. ? 1
  210. : desc.BufferCount;
  211. if (bb.count == 1)
  212. data.dxgi_1_4 = false;
  213. if (bb.count > MAX_BACKBUFFERS) {
  214. hlog("Somehow it's using more than the max backbuffers. "
  215. "Not sure why anyone would do that.");
  216. bb.count = 1;
  217. data.dxgi_1_4 = false;
  218. }
  219. for (UINT i = 0; i < bb.count; i++) {
  220. hr = swap->GetBuffer(i, IID_PPV_ARGS(&bb.backbuffer[i]));
  221. if (SUCCEEDED(hr)) {
  222. bb.backbuffer[i]->Release();
  223. } else {
  224. return false;
  225. }
  226. }
  227. return true;
  228. }
  229. static void d3d12_init(IDXGISwapChain *swap)
  230. {
  231. ID3D12Device *device = nullptr;
  232. const HRESULT hr = swap->GetDevice(IID_PPV_ARGS(&device));
  233. if (SUCCEEDED(hr)) {
  234. hlog("d3d12_init: device=0x%" PRIX64,
  235. (uint64_t)(uintptr_t)device);
  236. HWND window;
  237. bb_info bb = {};
  238. if (d3d12_init_format(swap, window, bb)) {
  239. if (global_hook_info->force_shmem) {
  240. hlog("d3d12_init: shared memory capture currently "
  241. "unsupported; ignoring");
  242. }
  243. if (!d3d12_shtex_init(device, window, bb))
  244. d3d12_free();
  245. }
  246. device->Release();
  247. } else {
  248. hlog_hr("d3d12_init: failed to get device from swap", hr);
  249. }
  250. }
  251. static inline void d3d12_copy_texture(ID3D11Resource *dst, ID3D11Resource *src)
  252. {
  253. if (data.multisampled) {
  254. data.context11->ResolveSubresource(dst, 0, src, 0, data.format);
  255. } else {
  256. data.context11->CopyResource(dst, src);
  257. }
  258. }
  259. static inline void d3d12_shtex_capture(IDXGISwapChain *swap)
  260. {
  261. bool dxgi_1_4 = data.dxgi_1_4;
  262. UINT cur_idx;
  263. if (dxgi_1_4) {
  264. IDXGISwapChain3 *swap3 =
  265. reinterpret_cast<IDXGISwapChain3 *>(swap);
  266. cur_idx = swap3->GetCurrentBackBufferIndex();
  267. } else {
  268. cur_idx = data.cur_backbuffer;
  269. }
  270. ID3D11Resource *backbuffer = data.backbuffer11[cur_idx];
  271. data.device11on12->AcquireWrappedResources(&backbuffer, 1);
  272. d3d12_copy_texture(data.copy_tex, backbuffer);
  273. data.device11on12->ReleaseWrappedResources(&backbuffer, 1);
  274. data.context11->Flush();
  275. if (!dxgi_1_4) {
  276. if (++data.cur_backbuffer >= data.backbuffer_count)
  277. data.cur_backbuffer = 0;
  278. }
  279. }
  280. void d3d12_capture(void *swap_ptr, void *)
  281. {
  282. IDXGISwapChain *swap = (IDXGISwapChain *)swap_ptr;
  283. if (capture_should_stop()) {
  284. d3d12_free();
  285. }
  286. if (capture_should_init()) {
  287. d3d12_init(swap);
  288. }
  289. if (capture_ready()) {
  290. d3d12_shtex_capture(swap);
  291. }
  292. }
  293. static bool try_append_queue_if_unique(ID3D12CommandQueue *queue)
  294. {
  295. for (size_t i = 0; i < dxgi_possible_swap_queue_count; ++i) {
  296. if (dxgi_possible_swap_queues[i] == queue)
  297. return false;
  298. }
  299. dxgi_possible_swap_queues[dxgi_possible_swap_queue_count] = queue;
  300. ++dxgi_possible_swap_queue_count;
  301. return true;
  302. }
  303. static HRESULT STDMETHODCALLTYPE
  304. hook_execute_command_lists(ID3D12CommandQueue *queue, UINT NumCommandLists,
  305. ID3D12CommandList *const *ppCommandLists)
  306. {
  307. hlog_verbose("ExecuteCommandLists callback: queue=0x%" PRIX64,
  308. (uint64_t)(uintptr_t)queue);
  309. if (dxgi_possible_swap_queue_count <
  310. _countof(dxgi_possible_swap_queues)) {
  311. if (dxgi_presenting &&
  312. (queue->GetDesc().Type == D3D12_COMMAND_LIST_TYPE_DIRECT)) {
  313. if (try_append_queue_if_unique(queue)) {
  314. hlog("Remembering D3D12 queue from present: queue=0x%" PRIX64,
  315. (uint64_t)(uintptr_t)queue);
  316. }
  317. } else if (dxgi_present_attempted &&
  318. (queue->GetDesc().Type ==
  319. D3D12_COMMAND_LIST_TYPE_DIRECT)) {
  320. if (try_append_queue_if_unique(queue)) {
  321. hlog("Remembering D3D12 queue from first direct submit after present: queue=0x%" PRIX64,
  322. (uint64_t)(uintptr_t)queue);
  323. }
  324. } else {
  325. hlog_verbose("Ignoring D3D12 queue=0x%" PRIX64,
  326. (uint64_t)(uintptr_t)queue);
  327. }
  328. }
  329. return RealExecuteCommandLists(queue, NumCommandLists, ppCommandLists);
  330. }
  331. static bool
  332. manually_get_d3d12_addrs(HMODULE d3d12_module,
  333. PFN_ExecuteCommandLists *execute_command_lists_addr)
  334. {
  335. PFN_D3D12_CREATE_DEVICE create =
  336. (PFN_D3D12_CREATE_DEVICE)GetProcAddress(d3d12_module,
  337. "D3D12CreateDevice");
  338. if (!create) {
  339. hlog("Failed to load D3D12CreateDevice");
  340. return false;
  341. }
  342. bool success = false;
  343. ID3D12Device *device;
  344. if (SUCCEEDED(create(NULL, D3D_FEATURE_LEVEL_11_0,
  345. IID_PPV_ARGS(&device)))) {
  346. D3D12_COMMAND_QUEUE_DESC desc{};
  347. ID3D12CommandQueue *queue;
  348. HRESULT hr =
  349. device->CreateCommandQueue(&desc, IID_PPV_ARGS(&queue));
  350. success = SUCCEEDED(hr);
  351. if (success) {
  352. void **queue_vtable = *(void ***)queue;
  353. *execute_command_lists_addr =
  354. (PFN_ExecuteCommandLists)queue_vtable[10];
  355. queue->Release();
  356. } else {
  357. hlog("Failed to create D3D12 command queue");
  358. }
  359. device->Release();
  360. } else {
  361. hlog("Failed to create D3D12 device");
  362. }
  363. return success;
  364. }
  365. bool hook_d3d12(void)
  366. {
  367. HMODULE d3d12_module = get_system_module("d3d12.dll");
  368. if (!d3d12_module) {
  369. hlog_verbose(
  370. "Failed to find d3d12.dll. Skipping hook attempt.");
  371. return false;
  372. }
  373. PFN_ExecuteCommandLists execute_command_lists_addr = nullptr;
  374. if (!manually_get_d3d12_addrs(d3d12_module,
  375. &execute_command_lists_addr)) {
  376. hlog("Failed to get D3D12 values");
  377. return true;
  378. }
  379. if (!execute_command_lists_addr) {
  380. hlog("Invalid D3D12 values");
  381. return true;
  382. }
  383. DetourTransactionBegin();
  384. RealExecuteCommandLists = execute_command_lists_addr;
  385. DetourAttach(&(PVOID &)RealExecuteCommandLists,
  386. hook_execute_command_lists);
  387. const LONG error = DetourTransactionCommit();
  388. const bool success = error == NO_ERROR;
  389. if (success) {
  390. hlog("Hooked ID3D12CommandQueue::ExecuteCommandLists");
  391. hlog("Hooked D3D12");
  392. } else {
  393. RealExecuteCommandLists = nullptr;
  394. hlog("Failed to attach Detours hook: %ld", error);
  395. }
  396. return success;
  397. }
  398. #endif