d3d12-capture.cpp 7.5 KB

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