d3d12-capture.cpp 8.4 KB

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