d3d12-capture.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 "dxgi-helpers.hpp"
  8. #include "../funchook.h"
  9. #define MAX_BACKBUFFERS 8
  10. struct d3d12_data {
  11. ID3D12Device *device; /* do not release */
  12. uint32_t cx;
  13. uint32_t cy;
  14. DXGI_FORMAT format;
  15. bool using_shtex;
  16. bool multisampled;
  17. bool dxgi_1_4;
  18. ID3D11Device *device11;
  19. ID3D11DeviceContext *context11;
  20. ID3D11On12Device *device11on12;
  21. union {
  22. struct {
  23. struct shtex_data *shtex_info;
  24. ID3D11Resource *backbuffer11[MAX_BACKBUFFERS];
  25. UINT backbuffer_count;
  26. UINT cur_backbuffer;
  27. ID3D11Texture2D *copy_tex;
  28. HANDLE handle;
  29. };
  30. };
  31. };
  32. static struct d3d12_data data = {};
  33. void d3d12_free(void)
  34. {
  35. if (data.copy_tex)
  36. data.copy_tex->Release();
  37. for (size_t i = 0; i < data.backbuffer_count; i++) {
  38. if (data.backbuffer11[i])
  39. data.backbuffer11[i]->Release();
  40. }
  41. if (data.device11)
  42. data.device11->Release();
  43. if (data.context11)
  44. data.context11->Release();
  45. if (data.device11on12)
  46. data.device11on12->Release();
  47. capture_free();
  48. memset(&data, 0, sizeof(data));
  49. hlog("----------------- d3d12 capture freed ----------------");
  50. }
  51. struct bb_info {
  52. ID3D12Resource *backbuffer[MAX_BACKBUFFERS];
  53. UINT count;
  54. };
  55. static bool create_d3d12_tex(bb_info &bb)
  56. {
  57. D3D11_RESOURCE_FLAGS rf11 = {};
  58. HRESULT hr;
  59. if (!bb.count)
  60. return false;
  61. data.backbuffer_count = bb.count;
  62. for (UINT i = 0; i < bb.count; i++) {
  63. hr = data.device11on12->CreateWrappedResource(
  64. bb.backbuffer[i], &rf11,
  65. D3D12_RESOURCE_STATE_COPY_SOURCE,
  66. D3D12_RESOURCE_STATE_PRESENT, __uuidof(ID3D11Resource),
  67. (void **)&data.backbuffer11[i]);
  68. if (FAILED(hr)) {
  69. hlog_hr("create_d3d12_tex: failed to create "
  70. "backbuffer11",
  71. hr);
  72. return false;
  73. }
  74. }
  75. D3D11_TEXTURE2D_DESC desc11 = {};
  76. desc11.Width = data.cx;
  77. desc11.Height = data.cy;
  78. desc11.MipLevels = 1;
  79. desc11.ArraySize = 1;
  80. desc11.Format = data.format;
  81. desc11.SampleDesc.Count = 1;
  82. desc11.BindFlags = D3D11_BIND_SHADER_RESOURCE;
  83. desc11.MiscFlags = D3D11_RESOURCE_MISC_SHARED;
  84. hr = data.device11->CreateTexture2D(&desc11, nullptr, &data.copy_tex);
  85. if (FAILED(hr)) {
  86. hlog_hr("create_d3d12_tex: creation of d3d11 copy tex failed",
  87. hr);
  88. return false;
  89. }
  90. for (UINT i = 0; i < bb.count; i++) {
  91. data.device11on12->ReleaseWrappedResources(
  92. &data.backbuffer11[i], 1);
  93. }
  94. IDXGIResource *dxgi_res;
  95. hr = data.copy_tex->QueryInterface(__uuidof(IDXGIResource),
  96. (void **)&dxgi_res);
  97. if (FAILED(hr)) {
  98. hlog_hr("create_d3d12_tex: failed to query "
  99. "IDXGIResource interface from texture",
  100. hr);
  101. return false;
  102. }
  103. hr = dxgi_res->GetSharedHandle(&data.handle);
  104. dxgi_res->Release();
  105. if (FAILED(hr)) {
  106. hlog_hr("create_d3d12_tex: failed to get shared handle", hr);
  107. return false;
  108. }
  109. return true;
  110. }
  111. typedef PFN_D3D11ON12_CREATE_DEVICE create_11_on_12_t;
  112. static bool d3d12_init_11on12(void)
  113. {
  114. static HMODULE d3d11 = nullptr;
  115. static create_11_on_12_t create_11_on_12 = nullptr;
  116. static bool initialized_11 = false;
  117. static bool initialized_func = false;
  118. HRESULT hr;
  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 = (create_11_on_12_t)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. hr = create_11_on_12(data.device, 0, nullptr, 0, nullptr, 0, 0,
  142. &data.device11, &data.context11, nullptr);
  143. if (FAILED(hr)) {
  144. hlog_hr("d3d12_init_11on12: failed to create 11 device", hr);
  145. return false;
  146. }
  147. data.device11->QueryInterface(__uuidof(ID3D11On12Device),
  148. (void **)&data.device11on12);
  149. if (FAILED(hr)) {
  150. hlog_hr("d3d12_init_11on12: failed to query 11on12 device", hr);
  151. return false;
  152. }
  153. return true;
  154. }
  155. static bool d3d12_shtex_init(HWND window, bb_info &bb)
  156. {
  157. if (!d3d12_init_11on12()) {
  158. return false;
  159. }
  160. if (!create_d3d12_tex(bb)) {
  161. return false;
  162. }
  163. if (!capture_init_shtex(&data.shtex_info, window, data.cx, data.cy,
  164. data.format, false, (uintptr_t)data.handle)) {
  165. return false;
  166. }
  167. hlog("d3d12 shared texture capture successful");
  168. return true;
  169. }
  170. static inline bool d3d12_init_format(IDXGISwapChain *swap, HWND &window,
  171. bb_info &bb)
  172. {
  173. DXGI_SWAP_CHAIN_DESC desc;
  174. IDXGISwapChain3 *swap3;
  175. HRESULT hr;
  176. hr = swap->GetDesc(&desc);
  177. if (FAILED(hr)) {
  178. hlog_hr("d3d12_init_format: swap->GetDesc failed", hr);
  179. return false;
  180. }
  181. data.format = fix_dxgi_format(desc.BufferDesc.Format);
  182. data.multisampled = desc.SampleDesc.Count > 1;
  183. window = desc.OutputWindow;
  184. data.cx = desc.BufferDesc.Width;
  185. data.cy = desc.BufferDesc.Height;
  186. hr = swap->QueryInterface(__uuidof(IDXGISwapChain3), (void **)&swap3);
  187. if (SUCCEEDED(hr)) {
  188. data.dxgi_1_4 = true;
  189. hlog("We're DXGI1.4 boys!");
  190. swap3->Release();
  191. }
  192. hlog("Buffer count: %d, swap effect: %d", (int)desc.BufferCount,
  193. (int)desc.SwapEffect);
  194. bb.count = desc.SwapEffect == DXGI_SWAP_EFFECT_DISCARD
  195. ? 1
  196. : desc.BufferCount;
  197. if (bb.count == 1)
  198. data.dxgi_1_4 = false;
  199. if (bb.count > MAX_BACKBUFFERS) {
  200. hlog("Somehow it's using more than the max backbuffers. "
  201. "Not sure why anyone would do that.");
  202. bb.count = 1;
  203. data.dxgi_1_4 = false;
  204. }
  205. for (UINT i = 0; i < bb.count; i++) {
  206. hr = swap->GetBuffer(i, __uuidof(ID3D12Resource),
  207. (void **)&bb.backbuffer[i]);
  208. if (SUCCEEDED(hr)) {
  209. bb.backbuffer[i]->Release();
  210. } else {
  211. return false;
  212. }
  213. }
  214. return true;
  215. }
  216. static void d3d12_init(IDXGISwapChain *swap)
  217. {
  218. bb_info bb = {};
  219. HWND window;
  220. HRESULT hr;
  221. hr = swap->GetDevice(__uuidof(ID3D12Device), (void **)&data.device);
  222. if (FAILED(hr)) {
  223. hlog_hr("d3d12_init: failed to get device from swap", hr);
  224. return;
  225. }
  226. data.device->Release();
  227. if (!d3d12_init_format(swap, window, bb)) {
  228. return;
  229. }
  230. if (global_hook_info->force_shmem) {
  231. hlog("d3d12_init: shared memory capture currently "
  232. "unsupported; ignoring");
  233. }
  234. if (!d3d12_shtex_init(window, bb))
  235. d3d12_free();
  236. }
  237. static inline void d3d12_copy_texture(ID3D11Resource *dst, ID3D11Resource *src)
  238. {
  239. if (data.multisampled) {
  240. data.context11->ResolveSubresource(dst, 0, src, 0, data.format);
  241. } else {
  242. data.context11->CopyResource(dst, src);
  243. }
  244. }
  245. static inline void d3d12_shtex_capture(IDXGISwapChain *swap,
  246. bool capture_overlay)
  247. {
  248. bool dxgi_1_4 = data.dxgi_1_4;
  249. UINT cur_idx;
  250. if (dxgi_1_4) {
  251. IDXGISwapChain3 *swap3 =
  252. reinterpret_cast<IDXGISwapChain3 *>(swap);
  253. cur_idx = swap3->GetCurrentBackBufferIndex();
  254. if (!capture_overlay) {
  255. if (++cur_idx >= data.backbuffer_count)
  256. cur_idx = 0;
  257. }
  258. } else {
  259. cur_idx = data.cur_backbuffer;
  260. }
  261. ID3D11Resource *backbuffer = data.backbuffer11[cur_idx];
  262. data.device11on12->AcquireWrappedResources(&backbuffer, 1);
  263. d3d12_copy_texture(data.copy_tex, backbuffer);
  264. data.device11on12->ReleaseWrappedResources(&backbuffer, 1);
  265. data.context11->Flush();
  266. if (!dxgi_1_4) {
  267. if (++data.cur_backbuffer >= data.backbuffer_count)
  268. data.cur_backbuffer = 0;
  269. }
  270. }
  271. void d3d12_capture(void *swap_ptr, void *, bool capture_overlay)
  272. {
  273. IDXGISwapChain *swap = (IDXGISwapChain *)swap_ptr;
  274. if (capture_should_stop()) {
  275. d3d12_free();
  276. }
  277. if (capture_should_init()) {
  278. d3d12_init(swap);
  279. }
  280. if (capture_ready()) {
  281. d3d12_shtex_capture(swap, capture_overlay);
  282. }
  283. }
  284. #endif