d3d12-capture.cpp 12 KB

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