pix3_win.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. // Copyright (c) Microsoft Corporation.
  2. // Licensed under the MIT License.
  3. // Don't include this file directly - use pix3.h
  4. #pragma once
  5. #ifndef _PIX3_H_
  6. #error Don't include this file directly - use pix3.h
  7. #endif
  8. #ifndef _PIX3_WIN_H_
  9. #define _PIX3_WIN_H_
  10. // PIXEventsThreadInfo is defined in PIXEventsCommon.h
  11. struct PIXEventsThreadInfo;
  12. extern "C" PIXEventsThreadInfo* WINAPI PIXGetThreadInfo() noexcept;
  13. #if defined(USE_PIX) && defined(USE_PIX_SUPPORTED_ARCHITECTURE)
  14. // Notifies PIX that an event handle was set as a result of a D3D12 fence being signaled.
  15. // The event specified must have the same handle value as the handle
  16. // used in ID3D12Fence::SetEventOnCompletion.
  17. extern "C" void WINAPI PIXNotifyWakeFromFenceSignal(_In_ HANDLE event);
  18. // Notifies PIX that a block of memory was allocated
  19. extern "C" void WINAPI PIXRecordMemoryAllocationEvent(USHORT allocatorId, void* baseAddress, size_t size, UINT64 metadata);
  20. // Notifies PIX that a block of memory was freed
  21. extern "C" void WINAPI PIXRecordMemoryFreeEvent(USHORT allocatorId, void* baseAddress, size_t size, UINT64 metadata);
  22. #else
  23. // Eliminate these APIs when not using PIX
  24. inline void PIXRecordMemoryAllocationEvent(USHORT, void*, size_t, UINT64) {}
  25. inline void PIXRecordMemoryFreeEvent(USHORT, void*, size_t, UINT64) {}
  26. #endif
  27. // The following WINPIX_EVENT_* defines denote the different metadata values that have
  28. // been used by tools to denote how to parse pix marker event data. The first two values
  29. // are legacy values used by pix.h in the Windows SDK.
  30. #define WINPIX_EVENT_UNICODE_VERSION 0
  31. #define WINPIX_EVENT_ANSI_VERSION 1
  32. // These values denote PIX marker event data that was created by the WinPixEventRuntime.
  33. // In early 2023 we revised the PIX marker format and defined a new version number.
  34. #define WINPIX_EVENT_PIX3BLOB_VERSION 2
  35. #define WINPIX_EVENT_PIX3BLOB_V2 6345127 // A number that other applications are unlikely to have used before
  36. // For backcompat reasons, the WinPixEventRuntime uses the older PIX3BLOB format when it passes data
  37. // into the D3D12 runtime. It will be updated to use the V2 format in the future.
  38. #define D3D12_EVENT_METADATA WINPIX_EVENT_PIX3BLOB_VERSION
  39. __forceinline UINT64 PIXGetTimestampCounter()
  40. {
  41. LARGE_INTEGER time = {};
  42. QueryPerformanceCounter(&time);
  43. return static_cast<UINT64>(time.QuadPart);
  44. }
  45. enum PIXHUDOptions
  46. {
  47. PIX_HUD_SHOW_ON_ALL_WINDOWS = 0x1,
  48. PIX_HUD_SHOW_ON_TARGET_WINDOW_ONLY = 0x2,
  49. PIX_HUD_SHOW_ON_NO_WINDOWS = 0x4
  50. };
  51. DEFINE_ENUM_FLAG_OPERATORS(PIXHUDOptions);
  52. #if defined(USE_PIX_SUPPORTED_ARCHITECTURE) && defined(USE_PIX)
  53. #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP | WINAPI_PARTITION_SYSTEM | WINAPI_PARTITION_GAMES)
  54. #include <shlobj.h>
  55. #include <strsafe.h>
  56. #include <knownfolders.h>
  57. #include <shellapi.h>
  58. #define PIXERRORCHECK(value) do { \
  59. if (FAILED(value)) \
  60. return nullptr; \
  61. } while(0)
  62. namespace PixImpl
  63. {
  64. #ifndef PIX3_WIN_UNIT_TEST
  65. __forceinline BOOL GetModuleHandleExW(
  66. DWORD dwFlags,
  67. LPCWSTR lpModuleName,
  68. HMODULE* phModule)
  69. {
  70. return ::GetModuleHandleExW(dwFlags, lpModuleName, phModule);
  71. }
  72. __forceinline HRESULT SHGetKnownFolderPath(
  73. REFKNOWNFOLDERID rfid,
  74. DWORD dwFlags,
  75. HANDLE hToken,
  76. PWSTR* ppszPath)
  77. {
  78. return ::SHGetKnownFolderPath(rfid, dwFlags, hToken, ppszPath);
  79. }
  80. __forceinline void CoTaskMemFree(LPVOID pv)
  81. {
  82. return ::CoTaskMemFree(pv);
  83. }
  84. __forceinline HANDLE FindFirstFileW(
  85. LPCWSTR lpFileName,
  86. LPWIN32_FIND_DATAW lpFindFileData)
  87. {
  88. return ::FindFirstFileW(lpFileName, lpFindFileData);
  89. }
  90. __forceinline DWORD GetFileAttributesW(LPCWSTR lpFileName)
  91. {
  92. return ::GetFileAttributesW(lpFileName);
  93. }
  94. __forceinline BOOL FindNextFileW(
  95. HANDLE hFindFile,
  96. LPWIN32_FIND_DATAW lpFindFileData)
  97. {
  98. return ::FindNextFileW(hFindFile, lpFindFileData);
  99. }
  100. __forceinline BOOL FindClose(HANDLE hFindFile)
  101. {
  102. return ::FindClose(hFindFile);
  103. }
  104. __forceinline HMODULE LoadLibraryExW(LPCWSTR lpLibFileName, DWORD flags)
  105. {
  106. return ::LoadLibraryExW(lpLibFileName, NULL, flags);
  107. }
  108. #endif // !PIX3_WIN_UNIT_TESTS
  109. __forceinline void * GetGpuCaptureFunctionPtr(LPCSTR fnName) noexcept
  110. {
  111. HMODULE module = GetModuleHandleW(L"WinPixGpuCapturer.dll");
  112. if (module == NULL)
  113. {
  114. return nullptr;
  115. }
  116. auto fn = (void*)GetProcAddress(module, fnName);
  117. if (fn == nullptr)
  118. {
  119. return nullptr;
  120. }
  121. return fn;
  122. }
  123. __forceinline void* GetTimingCaptureFunctionPtr(LPCSTR fnName) noexcept
  124. {
  125. HMODULE module = GetModuleHandleW(L"WinPixTimingCapturer.dll");
  126. if (module == NULL)
  127. {
  128. return nullptr;
  129. }
  130. auto fn = (void*)GetProcAddress(module, fnName);
  131. if (fn == nullptr)
  132. {
  133. return nullptr;
  134. }
  135. return fn;
  136. }
  137. __forceinline HMODULE PIXLoadLatestCapturerLibrary(wchar_t const* capturerDllName, DWORD flags)
  138. {
  139. HMODULE libHandle{};
  140. if (PixImpl::GetModuleHandleExW(0, capturerDllName, &libHandle))
  141. {
  142. return libHandle;
  143. }
  144. LPWSTR programFilesPath = nullptr;
  145. if (FAILED(PixImpl::SHGetKnownFolderPath(FOLDERID_ProgramFiles, KF_FLAG_DEFAULT, NULL, &programFilesPath)))
  146. {
  147. PixImpl::CoTaskMemFree(programFilesPath);
  148. return nullptr;
  149. }
  150. wchar_t pixSearchPath[MAX_PATH];
  151. if (FAILED(StringCchCopyW(pixSearchPath, MAX_PATH, programFilesPath)))
  152. {
  153. PixImpl::CoTaskMemFree(programFilesPath);
  154. return nullptr;
  155. }
  156. PixImpl::CoTaskMemFree(programFilesPath);
  157. PIXERRORCHECK(StringCchCatW(pixSearchPath, MAX_PATH, L"\\Microsoft PIX\\*"));
  158. WIN32_FIND_DATAW findData;
  159. bool foundPixInstallation = false;
  160. wchar_t newestVersionFound[MAX_PATH];
  161. wchar_t output[MAX_PATH];
  162. wchar_t possibleOutput[MAX_PATH];
  163. HANDLE hFind = PixImpl::FindFirstFileW(pixSearchPath, &findData);
  164. if (hFind != INVALID_HANDLE_VALUE)
  165. {
  166. do
  167. {
  168. if (((findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY) &&
  169. (findData.cFileName[0] != '.'))
  170. {
  171. if (!foundPixInstallation || wcscmp(newestVersionFound, findData.cFileName) <= 0)
  172. {
  173. // length - 1 to get rid of the wildcard character in the search path
  174. PIXERRORCHECK(StringCchCopyNW(possibleOutput, MAX_PATH, pixSearchPath, wcslen(pixSearchPath) - 1));
  175. PIXERRORCHECK(StringCchCatW(possibleOutput, MAX_PATH, findData.cFileName));
  176. PIXERRORCHECK(StringCchCatW(possibleOutput, MAX_PATH, L"\\"));
  177. PIXERRORCHECK(StringCchCatW(possibleOutput, MAX_PATH, capturerDllName));
  178. DWORD result = PixImpl::GetFileAttributesW(possibleOutput);
  179. if (result != INVALID_FILE_ATTRIBUTES && !(result & FILE_ATTRIBUTE_DIRECTORY))
  180. {
  181. foundPixInstallation = true;
  182. PIXERRORCHECK(StringCchCopyW(newestVersionFound, _countof(newestVersionFound), findData.cFileName));
  183. PIXERRORCHECK(StringCchCopyW(output, _countof(possibleOutput), possibleOutput));
  184. }
  185. }
  186. }
  187. } while (PixImpl::FindNextFileW(hFind, &findData) != 0);
  188. }
  189. PixImpl::FindClose(hFind);
  190. if (!foundPixInstallation)
  191. {
  192. SetLastError(ERROR_FILE_NOT_FOUND);
  193. return nullptr;
  194. }
  195. return PixImpl::LoadLibraryExW(output, flags);
  196. }
  197. }
  198. #undef PIXERRORCHECK
  199. __forceinline HMODULE PIXLoadLatestWinPixGpuCapturerLibrary()
  200. {
  201. return PixImpl::PIXLoadLatestCapturerLibrary(
  202. L"WinPixGpuCapturer.dll",
  203. LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);
  204. }
  205. __forceinline HMODULE PIXLoadLatestWinPixTimingCapturerLibrary()
  206. {
  207. return PixImpl::PIXLoadLatestCapturerLibrary(
  208. L"WinPixTimingCapturer.dll",
  209. LOAD_LIBRARY_SEARCH_DEFAULT_DIRS | LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR);
  210. }
  211. __forceinline HRESULT WINAPI PIXSetTargetWindow(HWND hwnd)
  212. {
  213. typedef void(WINAPI* SetGlobalTargetWindowFn)(HWND);
  214. auto fn = (SetGlobalTargetWindowFn)PixImpl::GetGpuCaptureFunctionPtr("SetGlobalTargetWindow");
  215. if (fn == nullptr)
  216. {
  217. return HRESULT_FROM_WIN32(GetLastError());
  218. }
  219. fn(hwnd);
  220. return S_OK;
  221. }
  222. __forceinline HRESULT WINAPI PIXGpuCaptureNextFrames(PCWSTR fileName, UINT32 numFrames)
  223. {
  224. typedef HRESULT(WINAPI* CaptureNextFrameFn)(PCWSTR, UINT32);
  225. auto fn = (CaptureNextFrameFn)PixImpl::GetGpuCaptureFunctionPtr("CaptureNextFrame");
  226. if (fn == nullptr)
  227. {
  228. return HRESULT_FROM_WIN32(GetLastError());
  229. }
  230. return fn(fileName, numFrames);
  231. }
  232. extern "C" __forceinline HRESULT WINAPI PIXBeginCapture2(DWORD captureFlags, _In_opt_ const PPIXCaptureParameters captureParameters)
  233. {
  234. if (captureFlags == PIX_CAPTURE_GPU)
  235. {
  236. typedef HRESULT(WINAPI* BeginProgrammaticGpuCaptureFn)(const PPIXCaptureParameters);
  237. auto fn = (BeginProgrammaticGpuCaptureFn)PixImpl::GetGpuCaptureFunctionPtr("BeginProgrammaticGpuCapture");
  238. if (fn == nullptr)
  239. {
  240. return HRESULT_FROM_WIN32(GetLastError());
  241. }
  242. return fn(captureParameters);
  243. }
  244. else if (captureFlags == PIX_CAPTURE_TIMING)
  245. {
  246. typedef HRESULT(WINAPI* BeginProgrammaticTimingCaptureFn)(void const*, UINT64);
  247. auto fn = (BeginProgrammaticTimingCaptureFn)PixImpl::GetTimingCaptureFunctionPtr("BeginProgrammaticTimingCapture");
  248. if (fn == nullptr)
  249. {
  250. return HRESULT_FROM_WIN32(GetLastError());
  251. }
  252. return fn(&captureParameters->TimingCaptureParameters, sizeof(captureParameters->TimingCaptureParameters));
  253. }
  254. else
  255. {
  256. return E_NOTIMPL;
  257. }
  258. }
  259. extern "C" __forceinline HRESULT WINAPI PIXEndCapture(BOOL discard)
  260. {
  261. // We can't tell if the user wants to end a GPU Capture or a Timing Capture.
  262. // The user shouldn't have both WinPixGpuCapturer and WinPixTimingCapturer loaded in the process though,
  263. // so we can just look for one of them and call it.
  264. typedef HRESULT(WINAPI* EndProgrammaticGpuCaptureFn)(void);
  265. auto gpuFn = (EndProgrammaticGpuCaptureFn)PixImpl::GetGpuCaptureFunctionPtr("EndProgrammaticGpuCapture");
  266. if (gpuFn != NULL)
  267. {
  268. return gpuFn();
  269. }
  270. typedef HRESULT(WINAPI* EndProgrammaticTimingCaptureFn)(BOOL);
  271. auto timingFn = (EndProgrammaticTimingCaptureFn)PixImpl::GetTimingCaptureFunctionPtr("EndProgrammaticTimingCapture");
  272. if (timingFn != NULL)
  273. {
  274. return timingFn(discard);
  275. }
  276. return HRESULT_FROM_WIN32(GetLastError());
  277. }
  278. __forceinline HRESULT WINAPI PIXForceD3D11On12()
  279. {
  280. typedef HRESULT (WINAPI* ForceD3D11On12Fn)(void);
  281. auto fn = (ForceD3D11On12Fn)PixImpl::GetGpuCaptureFunctionPtr("ForceD3D11On12");
  282. if (fn == NULL)
  283. {
  284. return HRESULT_FROM_WIN32(GetLastError());
  285. }
  286. return fn();
  287. }
  288. __forceinline HRESULT WINAPI PIXSetHUDOptions(PIXHUDOptions hudOptions)
  289. {
  290. typedef HRESULT(WINAPI* SetHUDOptionsFn)(PIXHUDOptions);
  291. auto fn = (SetHUDOptionsFn)PixImpl::GetGpuCaptureFunctionPtr("SetHUDOptions");
  292. if (fn == NULL)
  293. {
  294. return HRESULT_FROM_WIN32(GetLastError());
  295. }
  296. return fn(hudOptions);
  297. }
  298. __forceinline bool WINAPI PIXIsAttachedForGpuCapture()
  299. {
  300. typedef bool(WINAPI* GetIsAttachedToPixFn)(void);
  301. auto fn = (GetIsAttachedToPixFn)PixImpl::GetGpuCaptureFunctionPtr("GetIsAttachedToPix");
  302. if (fn == NULL)
  303. {
  304. OutputDebugStringW(L"WinPixEventRuntime error: Mismatched header/dll. Please ensure that pix3.h and WinPixGpuCapturer.dll match");
  305. return false;
  306. }
  307. return fn();
  308. }
  309. __forceinline HINSTANCE WINAPI PIXOpenCaptureInUI(PCWSTR fileName)
  310. {
  311. return ShellExecuteW(0, 0, fileName, 0, 0, SW_SHOW);
  312. }
  313. #else
  314. __forceinline HMODULE PIXLoadLatestWinPixGpuCapturerLibrary()
  315. {
  316. return nullptr;
  317. }
  318. __forceinline HMODULE PIXLoadLatestWinPixTimingCapturerLibrary()
  319. {
  320. return nullptr;
  321. }
  322. __forceinline HRESULT WINAPI PIXSetTargetWindow(HWND)
  323. {
  324. return E_NOTIMPL;
  325. }
  326. __forceinline HRESULT WINAPI PIXGpuCaptureNextFrames(PCWSTR, UINT32)
  327. {
  328. return E_NOTIMPL;
  329. }
  330. extern "C" __forceinline HRESULT WINAPI PIXBeginCapture2(DWORD, _In_opt_ const PPIXCaptureParameters)
  331. {
  332. return E_NOTIMPL;
  333. }
  334. extern "C" __forceinline HRESULT WINAPI PIXEndCapture(BOOL)
  335. {
  336. return E_NOTIMPL;
  337. }
  338. __forceinline HRESULT WINAPI PIXForceD3D11On12()
  339. {
  340. return E_NOTIMPL;
  341. }
  342. __forceinline HRESULT WINAPI PIXSetHUDOptions(PIXHUDOptions)
  343. {
  344. return E_NOTIMPL;
  345. }
  346. __forceinline bool WINAPI PIXIsAttachedForGpuCapture()
  347. {
  348. return false;
  349. }
  350. __forceinline HINSTANCE WINAPI PIXOpenCaptureInUI(PCWSTR)
  351. {
  352. return 0;
  353. }
  354. #endif // WINAPI_PARTITION
  355. #endif // USE_PIX_SUPPORTED_ARCHITECTURE || USE_PIX
  356. #if defined(__d3d12_h__)
  357. inline void PIXInsertTimingMarkerOnContextForBeginEvent(_In_ ID3D12GraphicsCommandList* commandList, UINT8 eventType, _In_reads_bytes_(size) void* data, UINT size)
  358. {
  359. UNREFERENCED_PARAMETER(eventType);
  360. commandList->BeginEvent(WINPIX_EVENT_PIX3BLOB_V2, data, size);
  361. }
  362. inline void PIXInsertTimingMarkerOnContextForBeginEvent(_In_ ID3D12CommandQueue* commandQueue, UINT8 eventType, _In_reads_bytes_(size) void* data, UINT size)
  363. {
  364. UNREFERENCED_PARAMETER(eventType);
  365. commandQueue->BeginEvent(WINPIX_EVENT_PIX3BLOB_V2, data, size);
  366. }
  367. inline void PIXInsertTimingMarkerOnContextForSetMarker(_In_ ID3D12GraphicsCommandList* commandList, UINT8 eventType, _In_reads_bytes_(size) void* data, UINT size)
  368. {
  369. UNREFERENCED_PARAMETER(eventType);
  370. commandList->SetMarker(WINPIX_EVENT_PIX3BLOB_V2, data, size);
  371. }
  372. inline void PIXInsertTimingMarkerOnContextForSetMarker(_In_ ID3D12CommandQueue* commandQueue, UINT8 eventType, _In_reads_bytes_(size) void* data, UINT size)
  373. {
  374. UNREFERENCED_PARAMETER(eventType);
  375. commandQueue->SetMarker(WINPIX_EVENT_PIX3BLOB_V2, data, size);
  376. }
  377. inline void PIXInsertTimingMarkerOnContextForEndEvent(_In_ ID3D12GraphicsCommandList* commandList, UINT8 eventType)
  378. {
  379. UNREFERENCED_PARAMETER(eventType);
  380. commandList->EndEvent();
  381. }
  382. inline void PIXInsertTimingMarkerOnContextForEndEvent(_In_ ID3D12CommandQueue* commandQueue, UINT8 eventType)
  383. {
  384. UNREFERENCED_PARAMETER(eventType);
  385. commandQueue->EndEvent();
  386. }
  387. inline void PIXInsertGPUMarkerOnContextForBeginEvent(_In_ ID3D12GraphicsCommandList* commandList, UINT8 eventType, _In_reads_bytes_(size) void* data, UINT size)
  388. {
  389. UNREFERENCED_PARAMETER(eventType);
  390. commandList->BeginEvent(WINPIX_EVENT_PIX3BLOB_V2, data, size);
  391. }
  392. inline void PIXInsertGPUMarkerOnContextForBeginEvent(_In_ ID3D12CommandQueue* commandQueue, UINT8 eventType, _In_reads_bytes_(size) void* data, UINT size)
  393. {
  394. UNREFERENCED_PARAMETER(eventType);
  395. commandQueue->BeginEvent(WINPIX_EVENT_PIX3BLOB_V2, data, size);
  396. }
  397. inline void PIXInsertGPUMarkerOnContextForSetMarker(_In_ ID3D12GraphicsCommandList* commandList, UINT8 eventType, _In_reads_bytes_(size) void* data, UINT size)
  398. {
  399. UNREFERENCED_PARAMETER(eventType);
  400. commandList->SetMarker(WINPIX_EVENT_PIX3BLOB_V2, data, size);
  401. }
  402. inline void PIXInsertGPUMarkerOnContextForSetMarker(_In_ ID3D12CommandQueue* commandQueue, UINT8 eventType, _In_reads_bytes_(size) void* data, UINT size)
  403. {
  404. UNREFERENCED_PARAMETER(eventType);
  405. commandQueue->SetMarker(WINPIX_EVENT_PIX3BLOB_V2, data, size);
  406. }
  407. inline void PIXInsertGPUMarkerOnContextForEndEvent(_In_ ID3D12GraphicsCommandList* commandList, UINT8, void*, UINT)
  408. {
  409. commandList->EndEvent();
  410. }
  411. inline void PIXInsertGPUMarkerOnContextForEndEvent(_In_ ID3D12CommandQueue* commandQueue, UINT8, void*, UINT)
  412. {
  413. commandQueue->EndEvent();
  414. }
  415. #endif
  416. #endif //_PIX3_WIN_H_