dxexp.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // dxexp.cpp //
  4. // Copyright (C) Microsoft Corporation. All rights reserved. //
  5. // This file is distributed under the University of Illinois Open Source //
  6. // License. See LICENSE.TXT for details. //
  7. // //
  8. // Provides a command-line tool to detect the status of D3D experimental //
  9. // feature support for experimental shaders. //
  10. // //
  11. ///////////////////////////////////////////////////////////////////////////////
  12. #define NOMINMAX
  13. #define WIN32_LEAN_AND_MEAN
  14. #include <windows.h>
  15. #include <dxgi1_4.h>
  16. #include <d3d12.h>
  17. #include <atlbase.h>
  18. #include <stdio.h>
  19. #pragma comment(lib, "d3d12.lib")
  20. #pragma comment(lib, "dxgi.lib")
  21. #pragma comment(lib, "dxguid.lib")
  22. // A more recent Windows SDK than currently required is needed for these.
  23. typedef HRESULT (WINAPI *D3D12EnableExperimentalFeaturesFn)(
  24. UINT NumFeatures,
  25. __in_ecount(NumFeatures) const IID* pIIDs,
  26. __in_ecount_opt(NumFeatures) void* pConfigurationStructs,
  27. __in_ecount_opt(NumFeatures) UINT* pConfigurationStructSizes);
  28. static const GUID D3D12ExperimentalShaderModelsID = { /* 76f5573e-f13a-40f5-b297-81ce9e18933f */
  29. 0x76f5573e,
  30. 0xf13a,
  31. 0x40f5,
  32. { 0xb2, 0x97, 0x81, 0xce, 0x9e, 0x18, 0x93, 0x3f }
  33. };
  34. static HRESULT AtlCheck(HRESULT hr) {
  35. if (FAILED(hr))
  36. AtlThrow(hr);
  37. return hr;
  38. }
  39. // Not defined in Creators Update version of d3d12.h:
  40. #if WDK_NTDDI_VERSION <= NTDDI_WIN10_RS2
  41. #define D3D_SHADER_MODEL_6_1 ((D3D_SHADER_MODEL)0x61)
  42. #define D3D12_FEATURE_D3D12_OPTIONS3 ((D3D12_FEATURE)21)
  43. typedef
  44. enum D3D12_COMMAND_LIST_SUPPORT_FLAGS
  45. {
  46. D3D12_COMMAND_LIST_SUPPORT_FLAG_NONE = 0,
  47. D3D12_COMMAND_LIST_SUPPORT_FLAG_DIRECT = (1 << D3D12_COMMAND_LIST_TYPE_DIRECT),
  48. D3D12_COMMAND_LIST_SUPPORT_FLAG_BUNDLE = (1 << D3D12_COMMAND_LIST_TYPE_BUNDLE),
  49. D3D12_COMMAND_LIST_SUPPORT_FLAG_COMPUTE = (1 << D3D12_COMMAND_LIST_TYPE_COMPUTE),
  50. D3D12_COMMAND_LIST_SUPPORT_FLAG_COPY = (1 << D3D12_COMMAND_LIST_TYPE_COPY),
  51. D3D12_COMMAND_LIST_SUPPORT_FLAG_VIDEO_DECODE = (1 << 4),
  52. D3D12_COMMAND_LIST_SUPPORT_FLAG_VIDEO_PROCESS = (1 << 5)
  53. } D3D12_COMMAND_LIST_SUPPORT_FLAGS;
  54. typedef
  55. enum D3D12_VIEW_INSTANCING_TIER
  56. {
  57. D3D12_VIEW_INSTANCING_TIER_NOT_SUPPORTED = 0,
  58. D3D12_VIEW_INSTANCING_TIER_1 = 1,
  59. D3D12_VIEW_INSTANCING_TIER_2 = 2,
  60. D3D12_VIEW_INSTANCING_TIER_3 = 3
  61. } D3D12_VIEW_INSTANCING_TIER;
  62. typedef struct D3D12_FEATURE_DATA_D3D12_OPTIONS3
  63. {
  64. _Out_ BOOL CopyQueueTimestampQueriesSupported;
  65. _Out_ BOOL CastingFullyTypedFormatSupported;
  66. _Out_ DWORD WriteBufferImmediateSupportFlags;
  67. _Out_ D3D12_VIEW_INSTANCING_TIER ViewInstancingTier;
  68. _Out_ BOOL BarycentricsSupported;
  69. } D3D12_FEATURE_DATA_D3D12_OPTIONS3;
  70. #endif
  71. #ifndef NTDDI_WIN10_RS3
  72. #define NTDDI_WIN10_RS3 0x0A000004
  73. #endif
  74. #if WDK_NTDDI_VERSION <= NTDDI_WIN10_RS3
  75. #define D3D_SHADER_MODEL_6_2 ((D3D_SHADER_MODEL)0x62)
  76. #define D3D12_FEATURE_D3D12_OPTIONS4 ((D3D12_FEATURE)23)
  77. typedef enum D3D12_SHARED_RESOURCE_COMPATIBILITY_TIER
  78. {
  79. D3D12_SHARED_RESOURCE_COMPATIBILITY_TIER_0,
  80. D3D12_SHARED_RESOURCE_COMPATIBILITY_TIER_1,
  81. } D3D12_SHARED_RESOURCE_COMPATIBILITY_TIER;
  82. typedef struct D3D12_FEATURE_DATA_D3D12_OPTIONS4
  83. {
  84. _Out_ BOOL ReservedBufferPlacementSupported;
  85. _Out_ D3D12_SHARED_RESOURCE_COMPATIBILITY_TIER SharedResourceCompatibilityTier;
  86. _Out_ BOOL Native16BitShaderOpsSupported;
  87. } D3D12_FEATURE_DATA_D3D12_OPTIONS4;
  88. #endif
  89. #ifndef NTDDI_WIN10_RS4
  90. #define NTDDI_WIN10_RS4 0x0A000005
  91. #endif
  92. #if WDK_NTDDI_VERSION <= NTDDI_WIN10_RS4
  93. #define D3D_SHADER_MODEL_6_3 ((D3D_SHADER_MODEL)0x63)
  94. #define D3D12_FEATURE_D3D12_OPTIONS5 ((D3D12_FEATURE)27)
  95. typedef enum D3D12_RENDER_PASS_TIER
  96. {
  97. D3D12_RENDER_PASS_TIER_0 = 0,
  98. D3D12_RENDER_PASS_TIER_1 = 1,
  99. D3D12_RENDER_PASS_TIER_2 = 2
  100. } D3D12_RENDER_PASS_TIER;
  101. typedef enum D3D12_RAYTRACING_TIER
  102. {
  103. D3D12_RAYTRACING_TIER_NOT_SUPPORTED = 0,
  104. D3D12_RAYTRACING_TIER_1_0 = 10
  105. } D3D12_RAYTRACING_TIER;
  106. typedef struct D3D12_FEATURE_DATA_D3D12_OPTIONS5
  107. {
  108. _Out_ BOOL SRVOnlyTiledResourceTier3;
  109. _Out_ D3D12_RENDER_PASS_TIER RenderPassesTier;
  110. _Out_ D3D12_RAYTRACING_TIER RaytracingTier;
  111. } D3D12_FEATURE_DATA_D3D12_OPTIONS5;
  112. #endif
  113. #ifndef NTDDI_WIN10_RS5
  114. #define NTDDI_WIN10_RS5 0x0A000006
  115. #endif
  116. #if WDK_NTDDI_VERSION <= NTDDI_WIN10_RS5
  117. #define D3D_SHADER_MODEL_6_4 ((D3D_SHADER_MODEL)0x64)
  118. #endif
  119. // TODO: Place under new version once available
  120. #if WDK_NTDDI_VERSION <= NTDDI_WIN10_RS5
  121. #define D3D_SHADER_MODEL_6_5 ((D3D_SHADER_MODEL)0x65)
  122. #endif
  123. #ifndef D3D_SHADER_MODEL_6_6
  124. #define D3D_SHADER_MODEL_6_6 ((D3D_SHADER_MODEL)0x66)
  125. #endif
  126. static char *BoolToStrJson(bool value) {
  127. return value ? "true" : "false";
  128. }
  129. static char *BoolToStrText(bool value) {
  130. return value ? "YES" : "NO";
  131. }
  132. static char *(*BoolToStr)(bool value);
  133. static bool IsOutputJson;
  134. #define json_printf(...) if (IsOutputJson) { printf(__VA_ARGS__); }
  135. #define text_printf(...) if (!IsOutputJson) { printf(__VA_ARGS__); }
  136. static char *ShaderModelToStr(D3D_SHADER_MODEL SM) {
  137. switch ((UINT32)SM) {
  138. case D3D_SHADER_MODEL_5_1: return "5.1";
  139. case D3D_SHADER_MODEL_6_0: return "6.0";
  140. case D3D_SHADER_MODEL_6_1: return "6.1";
  141. case D3D_SHADER_MODEL_6_2: return "6.2";
  142. case D3D_SHADER_MODEL_6_3: return "6.3";
  143. case D3D_SHADER_MODEL_6_4: return "6.4";
  144. case D3D_SHADER_MODEL_6_5: return "6.5";
  145. case D3D_SHADER_MODEL_6_6: return "6.6";
  146. default: return "ERROR";
  147. }
  148. }
  149. static char *ViewInstancingTierToStr(D3D12_VIEW_INSTANCING_TIER Tier) {
  150. switch (Tier) {
  151. case D3D12_VIEW_INSTANCING_TIER_NOT_SUPPORTED: return "NO";
  152. case D3D12_VIEW_INSTANCING_TIER_1: return "Tier1";
  153. case D3D12_VIEW_INSTANCING_TIER_2: return "Tier2";
  154. case D3D12_VIEW_INSTANCING_TIER_3: return "Tier3";
  155. default: return "ERROR";
  156. }
  157. }
  158. static char *RaytracingTierToStr(D3D12_RAYTRACING_TIER Tier) {
  159. switch (Tier) {
  160. case D3D12_RAYTRACING_TIER_NOT_SUPPORTED: return "NO";
  161. case D3D12_RAYTRACING_TIER_1_0: return "1.0";
  162. default: return "ERROR";
  163. }
  164. }
  165. static HRESULT GetHighestShaderModel(ID3D12Device *pDevice, D3D12_FEATURE_DATA_SHADER_MODEL &DeviceSM) {
  166. HRESULT hr = E_INVALIDARG;
  167. D3D_SHADER_MODEL SM = D3D_SHADER_MODEL_6_6;
  168. while (hr == E_INVALIDARG && SM >= D3D_SHADER_MODEL_6_0) {
  169. DeviceSM.HighestShaderModel = SM;
  170. hr = pDevice->CheckFeatureSupport(D3D12_FEATURE_SHADER_MODEL, &DeviceSM, sizeof(DeviceSM));
  171. SM = (D3D_SHADER_MODEL)((UINT32)SM - 1);
  172. }
  173. return hr;
  174. }
  175. static HRESULT PrintAdapters() {
  176. HRESULT hr = S_OK;
  177. char comma = ' ';
  178. json_printf("{ \"adapters\" : [\n");
  179. try {
  180. CComPtr<IDXGIFactory2> pFactory;
  181. AtlCheck(CreateDXGIFactory2(0, IID_PPV_ARGS(&pFactory)));
  182. UINT AdapterIndex = 0;
  183. for (;;) {
  184. CComPtr<IDXGIAdapter1> pAdapter;
  185. CComPtr<ID3D12Device> pDevice;
  186. HRESULT hrEnum = pFactory->EnumAdapters1(AdapterIndex, &pAdapter);
  187. if (hrEnum == DXGI_ERROR_NOT_FOUND)
  188. break;
  189. AtlCheck(hrEnum);
  190. DXGI_ADAPTER_DESC1 AdapterDesc;
  191. D3D12_FEATURE_DATA_D3D12_OPTIONS1 DeviceOptions;
  192. D3D12_FEATURE_DATA_D3D12_OPTIONS3 DeviceOptions3;
  193. D3D12_FEATURE_DATA_D3D12_OPTIONS4 DeviceOptions4;
  194. D3D12_FEATURE_DATA_D3D12_OPTIONS5 DeviceOptions5;
  195. memset(&DeviceOptions, 0, sizeof(DeviceOptions));
  196. memset(&DeviceOptions3, 0, sizeof(DeviceOptions3));
  197. memset(&DeviceOptions4, 0, sizeof(DeviceOptions4));
  198. memset(&DeviceOptions5, 0, sizeof(DeviceOptions5));
  199. D3D12_FEATURE_DATA_SHADER_MODEL DeviceSM;
  200. AtlCheck(pAdapter->GetDesc1(&AdapterDesc));
  201. AtlCheck(D3D12CreateDevice(pAdapter, D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&pDevice)));
  202. AtlCheck(pDevice->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS1, &DeviceOptions, sizeof(DeviceOptions)));
  203. pDevice->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS3, &DeviceOptions3, sizeof(DeviceOptions3));
  204. pDevice->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS4, &DeviceOptions4, sizeof(DeviceOptions4));
  205. pDevice->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS5, &DeviceOptions5, sizeof(DeviceOptions5));
  206. AtlCheck(GetHighestShaderModel(pDevice, DeviceSM));
  207. const char *Format = IsOutputJson ?
  208. "%c { \"name\": \"%S\", \"sm\": \"%s\", \"wave\": %s, \"i64\": %s, \"bary\": %s, \"view-inst\": \"%s\", \"16bit\": %s, \"raytracing\": \"%s\" }\n" :
  209. "%c %S - Highest SM [%s] Wave [%s] I64 [%s] Barycentrics [%s] View Instancing [%s] 16bit Support [%s] Raytracing [%s]\n";
  210. printf(Format,
  211. comma,
  212. AdapterDesc.Description,
  213. ShaderModelToStr(DeviceSM.HighestShaderModel),
  214. BoolToStr(DeviceOptions.WaveOps),
  215. BoolToStr(DeviceOptions.Int64ShaderOps),
  216. BoolToStr(DeviceOptions3.BarycentricsSupported),
  217. ViewInstancingTierToStr(DeviceOptions3.ViewInstancingTier),
  218. BoolToStr(DeviceOptions4.Native16BitShaderOpsSupported),
  219. RaytracingTierToStr(DeviceOptions5.RaytracingTier)
  220. );
  221. AdapterIndex++;
  222. comma = IsOutputJson ? ',' : ' ';
  223. }
  224. }
  225. catch (ATL::CAtlException &e) {
  226. hr = e.m_hr;
  227. json_printf("%c { \"err\": \"unable to print information for adapters - 0x%08x\" }\n", comma, hr);
  228. text_printf("%s", "Unable to print information for adapters.\n");
  229. }
  230. json_printf(" ] }\n");
  231. return hr;
  232. }
  233. // Return codes:
  234. // 0 - experimental mode worked
  235. // 1 - cannot load d3d12.dll
  236. // 2 - cannot find D3D12EnableExperimentalFeatures
  237. // 3 - experimental shader mode interface unsupported
  238. // 4 - other error
  239. int main(int argc, const char *argv[]) {
  240. BoolToStr = BoolToStrText;
  241. IsOutputJson = false;
  242. if (argc > 1) {
  243. const char *pArg = argv[1];
  244. if (0 == strcmp(pArg, "-?") || 0 == strcmp(pArg, "--?") || 0 == strcmp(pArg, "/?")) {
  245. printf("Checks the available of D3D support for experimental shader models.\n\n");
  246. printf("dxexp [-json]\n\n");
  247. printf("Sets errorlevel to 0 on success, non-zero for failure cases.\n");
  248. return 4;
  249. }
  250. else if (0 == strcmp(pArg, "-json") || 0 == strcmp(pArg, "/json")) {
  251. IsOutputJson = true;
  252. BoolToStr = BoolToStrJson;
  253. }
  254. else {
  255. printf("Unrecognized command line arguments.\n");
  256. return 4;
  257. }
  258. }
  259. DWORD err;
  260. HMODULE hRuntime;
  261. hRuntime = LoadLibraryW(L"d3d12.dll");
  262. if (hRuntime == NULL) {
  263. err = GetLastError();
  264. printf("Failed to load library d3d12.dll - Win32 error %u\n", err);
  265. return 1;
  266. }
  267. json_printf("{ \"noexp\":\n");
  268. text_printf("Adapter feature support without experimental shaders enabled:\n");
  269. if (FAILED(PrintAdapters())) {
  270. return 4;
  271. }
  272. FreeLibrary(hRuntime);
  273. json_printf(" , \"exp\":");
  274. text_printf("-------------------------------------------------------------\n");
  275. hRuntime = LoadLibraryW(L"d3d12.dll");
  276. if (hRuntime == NULL) {
  277. err = GetLastError();
  278. printf("Failed to load library d3d12.dll - Win32 error %u\n", err);
  279. return 1;
  280. }
  281. D3D12EnableExperimentalFeaturesFn pD3D12EnableExperimentalFeatures =
  282. (D3D12EnableExperimentalFeaturesFn)GetProcAddress(hRuntime, "D3D12EnableExperimentalFeatures");
  283. if (pD3D12EnableExperimentalFeatures == nullptr) {
  284. err = GetLastError();
  285. printf("Failed to find export 'D3D12EnableExperimentalFeatures' in "
  286. "d3d12.dll - Win32 error %u%s\n", err,
  287. err == ERROR_PROC_NOT_FOUND ? " (The specified procedure could not be found.)" : "");
  288. printf("Consider verifying the operating system version - Creators Update or newer "
  289. "is currently required.\n");
  290. PrintAdapters();
  291. return 2;
  292. }
  293. HRESULT hr = pD3D12EnableExperimentalFeatures(1, &D3D12ExperimentalShaderModelsID, nullptr, nullptr);
  294. if (SUCCEEDED(hr)) {
  295. text_printf("Experimental shader model feature succeeded.\n");
  296. hr = PrintAdapters();
  297. json_printf("\n}\n");
  298. return (SUCCEEDED(hr)) ? 0 : 4;
  299. }
  300. else if (hr == E_NOINTERFACE) {
  301. text_printf("Experimental shader model feature failed with error E_NOINTERFACE.\n");
  302. text_printf("The most likely cause is that Windows Developer mode is not on.\n");
  303. text_printf("See https://msdn.microsoft.com/en-us/windows/uwp/get-started/enable-your-device-for-development\n");
  304. json_printf("{ \"err\": \"E_NOINTERFACE\" }");
  305. json_printf("\n}\n");
  306. return 3;
  307. }
  308. else if (hr == E_INVALIDARG) {
  309. text_printf("Experimental shader model feature failed with error E_INVALIDARG.\n");
  310. text_printf("This means the configuration of a feature is incorrect, the set of features passed\n"
  311. "in are known to be incompatible with each other, or other errors occured,\n"
  312. "and is generally unexpected for the experimental shader model feature.\n");
  313. json_printf("{ \"err\": \"E_INVALIDARG\" }");
  314. json_printf("\n}\n");
  315. return 4;
  316. }
  317. else {
  318. text_printf("Experimental shader model feature failed with unexpected HRESULT 0x%08x.\n", hr);
  319. json_printf("{ \"err\": \"0x%08x\" }", hr);
  320. json_printf("\n}\n");
  321. return 4;
  322. }
  323. }