d3d12-capture.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. const static D3D_FEATURE_LEVEL feature_levels[] = {
  116. D3D_FEATURE_LEVEL_11_0,
  117. D3D_FEATURE_LEVEL_10_1,
  118. D3D_FEATURE_LEVEL_10_0,
  119. D3D_FEATURE_LEVEL_9_3,
  120. };
  121. static bool d3d12_init_11on12(void)
  122. {
  123. static HMODULE d3d11 = nullptr;
  124. static create_11_on_12_t create_11_on_12 = nullptr;
  125. static bool initialized_11 = false;
  126. static bool initialized_func = false;
  127. HRESULT hr;
  128. if (!initialized_11 && !d3d11) {
  129. d3d11 = load_system_library("d3d11.dll");
  130. if (!d3d11) {
  131. hlog("d3d12_init_11on12: failed to load d3d11");
  132. }
  133. initialized_11 = true;
  134. }
  135. if (!d3d11) {
  136. return false;
  137. }
  138. if (!initialized_func && !create_11_on_12) {
  139. create_11_on_12 = (create_11_on_12_t)GetProcAddress(
  140. d3d11, "D3D11On12CreateDevice");
  141. if (!create_11_on_12) {
  142. hlog("d3d12_init_11on12: Failed to get "
  143. "D3D11On12CreateDevice address");
  144. }
  145. initialized_func = true;
  146. }
  147. if (!create_11_on_12) {
  148. return false;
  149. }
  150. hr = create_11_on_12(data.device, 0, nullptr, 0, nullptr, 0, 0,
  151. &data.device11, &data.context11, nullptr);
  152. if (FAILED(hr)) {
  153. hlog_hr("d3d12_init_11on12: failed to create 11 device", hr);
  154. return false;
  155. }
  156. data.device11->QueryInterface(__uuidof(ID3D11On12Device),
  157. (void **)&data.device11on12);
  158. if (FAILED(hr)) {
  159. hlog_hr("d3d12_init_11on12: failed to query 11on12 device", hr);
  160. return false;
  161. }
  162. return true;
  163. }
  164. static bool d3d12_shtex_init(HWND window, bb_info &bb)
  165. {
  166. if (!d3d12_init_11on12()) {
  167. return false;
  168. }
  169. if (!create_d3d12_tex(bb)) {
  170. return false;
  171. }
  172. if (!capture_init_shtex(&data.shtex_info, window, data.base_cx,
  173. data.base_cy, data.cx, data.cy, data.format,
  174. false, (uintptr_t)data.handle)) {
  175. return false;
  176. }
  177. hlog("d3d12 shared texture capture successful");
  178. return true;
  179. }
  180. static inline bool d3d12_init_format(IDXGISwapChain *swap, HWND &window,
  181. bb_info &bb)
  182. {
  183. DXGI_SWAP_CHAIN_DESC desc;
  184. IDXGISwapChain3 *swap3;
  185. HRESULT hr;
  186. hr = swap->GetDesc(&desc);
  187. if (FAILED(hr)) {
  188. hlog_hr("d3d12_init_format: swap->GetDesc failed", hr);
  189. return false;
  190. }
  191. data.format = fix_dxgi_format(desc.BufferDesc.Format);
  192. data.multisampled = desc.SampleDesc.Count > 1;
  193. window = desc.OutputWindow;
  194. data.base_cx = desc.BufferDesc.Width;
  195. data.base_cy = desc.BufferDesc.Height;
  196. hr = swap->QueryInterface(__uuidof(IDXGISwapChain3), (void **)&swap3);
  197. if (SUCCEEDED(hr)) {
  198. data.dxgi_1_4 = true;
  199. hlog("We're DXGI1.4 boys!");
  200. swap3->Release();
  201. }
  202. hlog("Buffer count: %d, swap effect: %d", (int)desc.BufferCount,
  203. (int)desc.SwapEffect);
  204. bb.count = desc.SwapEffect == DXGI_SWAP_EFFECT_DISCARD
  205. ? 1
  206. : desc.BufferCount;
  207. if (bb.count == 1)
  208. data.dxgi_1_4 = false;
  209. if (bb.count > MAX_BACKBUFFERS) {
  210. hlog("Somehow it's using more than the max backbuffers. "
  211. "Not sure why anyone would do that.");
  212. bb.count = 1;
  213. data.dxgi_1_4 = false;
  214. }
  215. for (UINT i = 0; i < bb.count; i++) {
  216. hr = swap->GetBuffer(i, __uuidof(ID3D12Resource),
  217. (void **)&bb.backbuffer[i]);
  218. if (SUCCEEDED(hr)) {
  219. bb.backbuffer[i]->Release();
  220. } else {
  221. return false;
  222. }
  223. }
  224. if (data.using_scale) {
  225. data.cx = global_hook_info->cx;
  226. data.cy = global_hook_info->cy;
  227. } else {
  228. data.cx = desc.BufferDesc.Width;
  229. data.cy = desc.BufferDesc.Height;
  230. }
  231. return true;
  232. }
  233. static void d3d12_init(IDXGISwapChain *swap)
  234. {
  235. bool success = true;
  236. bb_info bb = {};
  237. HWND window;
  238. HRESULT hr;
  239. data.using_scale = global_hook_info->use_scale;
  240. hr = swap->GetDevice(__uuidof(ID3D12Device), (void **)&data.device);
  241. if (FAILED(hr)) {
  242. hlog_hr("d3d12_init: failed to get device from swap", hr);
  243. return;
  244. }
  245. data.device->Release();
  246. if (!d3d12_init_format(swap, window, bb)) {
  247. return;
  248. }
  249. if (data.using_scale) {
  250. hlog("d3d12_init: scaling currently unsupported; ignoring");
  251. }
  252. if (success) {
  253. if (global_hook_info->force_shmem) {
  254. hlog("d3d12_init: shared memory capture currently "
  255. "unsupported; ignoring");
  256. }
  257. success = d3d12_shtex_init(window, bb);
  258. }
  259. if (!success)
  260. d3d12_free();
  261. }
  262. static inline void d3d12_copy_texture(ID3D11Resource *dst, ID3D11Resource *src)
  263. {
  264. if (data.multisampled) {
  265. data.context11->ResolveSubresource(dst, 0, src, 0, data.format);
  266. } else {
  267. data.context11->CopyResource(dst, src);
  268. }
  269. }
  270. static inline void d3d12_shtex_capture(IDXGISwapChain *swap,
  271. bool capture_overlay)
  272. {
  273. bool dxgi_1_4 = data.dxgi_1_4;
  274. UINT cur_idx;
  275. if (dxgi_1_4) {
  276. IDXGISwapChain3 *swap3 =
  277. reinterpret_cast<IDXGISwapChain3 *>(swap);
  278. cur_idx = swap3->GetCurrentBackBufferIndex();
  279. if (!capture_overlay) {
  280. if (++cur_idx >= data.backbuffer_count)
  281. cur_idx = 0;
  282. }
  283. } else {
  284. cur_idx = data.cur_backbuffer;
  285. }
  286. ID3D11Resource *backbuffer = data.backbuffer11[cur_idx];
  287. data.device11on12->AcquireWrappedResources(&backbuffer, 1);
  288. d3d12_copy_texture(data.copy_tex, backbuffer);
  289. data.device11on12->ReleaseWrappedResources(&backbuffer, 1);
  290. data.context11->Flush();
  291. if (!dxgi_1_4) {
  292. if (++data.cur_backbuffer >= data.backbuffer_count)
  293. data.cur_backbuffer = 0;
  294. }
  295. }
  296. void d3d12_capture(void *swap_ptr, void *, bool capture_overlay)
  297. {
  298. IDXGISwapChain *swap = (IDXGISwapChain *)swap_ptr;
  299. if (capture_should_stop()) {
  300. d3d12_free();
  301. }
  302. if (capture_should_init()) {
  303. d3d12_init(swap);
  304. }
  305. if (capture_ready()) {
  306. d3d12_shtex_capture(swap, capture_overlay);
  307. }
  308. }
  309. #endif