d3d12-capture.cpp 7.9 KB

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