2
0

dxexp.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. static char *BoolToStrJson(bool value) {
  90. return value ? "true" : "false";
  91. }
  92. static char *BoolToStrText(bool value) {
  93. return value ? "YES" : "NO";
  94. }
  95. static char *(*BoolToStr)(bool value);
  96. static bool IsOutputJson;
  97. #define json_printf(...) if (IsOutputJson) { printf(__VA_ARGS__); }
  98. #define text_printf(...) if (!IsOutputJson) { printf(__VA_ARGS__); }
  99. static char *ShaderModelToStr(D3D_SHADER_MODEL SM) {
  100. switch ((UINT32)SM) {
  101. case D3D_SHADER_MODEL_5_1: return "5.1";
  102. case D3D_SHADER_MODEL_6_0: return "6.0";
  103. case D3D_SHADER_MODEL_6_1: return "6.1";
  104. case D3D_SHADER_MODEL_6_2: return "6.2";
  105. default: return "ERROR";
  106. }
  107. }
  108. static char *ViewInstancingTierToStr(D3D12_VIEW_INSTANCING_TIER Tier) {
  109. switch (Tier) {
  110. case D3D12_VIEW_INSTANCING_TIER_NOT_SUPPORTED: return "NO";
  111. case D3D12_VIEW_INSTANCING_TIER_1: return "Tier1";
  112. case D3D12_VIEW_INSTANCING_TIER_2: return "Tier2";
  113. case D3D12_VIEW_INSTANCING_TIER_3: return "Tier3";
  114. default: return "ERROR";
  115. }
  116. }
  117. static HRESULT PrintAdapters() {
  118. HRESULT hr = S_OK;
  119. char comma = ' ';
  120. json_printf("{ \"adapters\" : [\n");
  121. try {
  122. CComPtr<IDXGIFactory2> pFactory;
  123. AtlCheck(CreateDXGIFactory2(0, IID_PPV_ARGS(&pFactory)));
  124. UINT AdapterIndex = 0;
  125. for (;;) {
  126. CComPtr<IDXGIAdapter1> pAdapter;
  127. CComPtr<ID3D12Device> pDevice;
  128. HRESULT hrEnum = pFactory->EnumAdapters1(AdapterIndex, &pAdapter);
  129. if (hrEnum == DXGI_ERROR_NOT_FOUND)
  130. break;
  131. AtlCheck(hrEnum);
  132. DXGI_ADAPTER_DESC1 AdapterDesc;
  133. D3D12_FEATURE_DATA_D3D12_OPTIONS1 DeviceOptions;
  134. D3D12_FEATURE_DATA_D3D12_OPTIONS3 DeviceOptions3;
  135. D3D12_FEATURE_DATA_D3D12_OPTIONS4 DeviceOptions4;
  136. memset(&DeviceOptions, 0, sizeof(DeviceOptions));
  137. memset(&DeviceOptions3, 0, sizeof(DeviceOptions3));
  138. memset(&DeviceOptions4, 0, sizeof(DeviceOptions4));
  139. D3D12_FEATURE_DATA_SHADER_MODEL DeviceSM;
  140. AtlCheck(pAdapter->GetDesc1(&AdapterDesc));
  141. AtlCheck(D3D12CreateDevice(pAdapter, D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&pDevice)));
  142. AtlCheck(pDevice->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS1, &DeviceOptions, sizeof(DeviceOptions)));
  143. DeviceSM.HighestShaderModel = D3D_SHADER_MODEL_6_0;
  144. // CheckFeatureSupport with D3D12_FEATURE_D3D12_OPTIONS3 will fail on Creators Update,
  145. // but succeed on newer versions of Windows. Use this to control the initial value
  146. // for highest shader model.
  147. if (SUCCEEDED(pDevice->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS3, &DeviceOptions3, sizeof(DeviceOptions3))))
  148. DeviceSM.HighestShaderModel = D3D_SHADER_MODEL_6_1;
  149. // CheckFeatureSupport with D3D12_FEATURE_D3D12_OPTIONS3 will fail on Fall Creators Update,
  150. // but succeed on newer versions of Windows. Use this to control the initial value
  151. // for highest shader model.
  152. if (SUCCEEDED(pDevice->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS4, &DeviceOptions4, sizeof(DeviceOptions4))))
  153. DeviceSM.HighestShaderModel = D3D_SHADER_MODEL_6_2;
  154. AtlCheck(pDevice->CheckFeatureSupport(D3D12_FEATURE_SHADER_MODEL, &DeviceSM, sizeof(DeviceSM)));
  155. const char *Format = IsOutputJson ?
  156. "%c { \"name\": \"%S\", \"sm\": \"%s\", \"wave\": %s, \"i64\": %s, \"bary\": %s, \"view-inst\": \"%s\" }\n" :
  157. "%c %S - Highest SM [%s] Wave [%s] I64 [%s] Barycentrics [%s] View Instancing [%s] 16bit Support [%s]\n";
  158. printf(Format,
  159. comma,
  160. AdapterDesc.Description,
  161. ShaderModelToStr(DeviceSM.HighestShaderModel),
  162. BoolToStr(DeviceOptions.WaveOps),
  163. BoolToStr(DeviceOptions.Int64ShaderOps),
  164. BoolToStr(DeviceOptions3.BarycentricsSupported),
  165. ViewInstancingTierToStr(DeviceOptions3.ViewInstancingTier),
  166. BoolToStr(DeviceOptions4.Native16BitShaderOpsSupported)
  167. );
  168. AdapterIndex++;
  169. comma = IsOutputJson ? ',' : ' ';
  170. }
  171. }
  172. catch (ATL::CAtlException &e) {
  173. hr = e.m_hr;
  174. json_printf("%c { \"err\": \"unable to print information for adapters - 0x%08x\" }\n", comma, hr);
  175. text_printf("%s", "Unable to print information for adapters.\n");
  176. }
  177. json_printf(" ] }\n");
  178. return hr;
  179. }
  180. // Return codes:
  181. // 0 - experimental mode worked
  182. // 1 - cannot load d3d12.dll
  183. // 2 - cannot find D3D12EnableExperimentalFeatures
  184. // 3 - experimental shader mode interface unsupported
  185. // 4 - other error
  186. int main(int argc, const char *argv[]) {
  187. BoolToStr = BoolToStrText;
  188. IsOutputJson = false;
  189. if (argc > 1) {
  190. const char *pArg = argv[1];
  191. if (0 == strcmp(pArg, "-?") || 0 == strcmp(pArg, "--?") || 0 == strcmp(pArg, "/?")) {
  192. printf("Checks the available of D3D support for experimental shader models.\n\n");
  193. printf("dxexp [-json]\n\n");
  194. printf("Sets errorlevel to 0 on success, non-zero for failure cases.\n");
  195. return 4;
  196. }
  197. else if (0 == strcmp(pArg, "-json") || 0 == strcmp(pArg, "/json")) {
  198. IsOutputJson = true;
  199. BoolToStr = BoolToStrJson;
  200. }
  201. else {
  202. printf("Unrecognized command line arguments.\n");
  203. return 4;
  204. }
  205. }
  206. DWORD err;
  207. HMODULE hRuntime;
  208. hRuntime = LoadLibraryW(L"d3d12.dll");
  209. if (hRuntime == NULL) {
  210. err = GetLastError();
  211. printf("Failed to load library d3d12.dll - Win32 error %u\n", err);
  212. return 1;
  213. }
  214. json_printf("{ \"noexp\":\n");
  215. text_printf("Adapter feature support without experimental shaders enabled:\n");
  216. if (FAILED(PrintAdapters())) {
  217. return 4;
  218. }
  219. FreeLibrary(hRuntime);
  220. json_printf(" , \"exp\":");
  221. text_printf("-------------------------------------------------------------\n");
  222. hRuntime = LoadLibraryW(L"d3d12.dll");
  223. if (hRuntime == NULL) {
  224. err = GetLastError();
  225. printf("Failed to load library d3d12.dll - Win32 error %u\n", err);
  226. return 1;
  227. }
  228. D3D12EnableExperimentalFeaturesFn pD3D12EnableExperimentalFeatures =
  229. (D3D12EnableExperimentalFeaturesFn)GetProcAddress(hRuntime, "D3D12EnableExperimentalFeatures");
  230. if (pD3D12EnableExperimentalFeatures == nullptr) {
  231. err = GetLastError();
  232. printf("Failed to find export 'D3D12EnableExperimentalFeatures' in "
  233. "d3d12.dll - Win32 error %u%s\n", err,
  234. err == ERROR_PROC_NOT_FOUND ? " (The specified procedure could not be found.)" : "");
  235. printf("Consider verifying the operating system version - Creators Update or newer "
  236. "is currently required.\n");
  237. PrintAdapters();
  238. return 2;
  239. }
  240. HRESULT hr = pD3D12EnableExperimentalFeatures(1, &D3D12ExperimentalShaderModelsID, nullptr, nullptr);
  241. if (SUCCEEDED(hr)) {
  242. text_printf("Experimental shader model feature succeeded.\n");
  243. hr = PrintAdapters();
  244. json_printf("\n}\n");
  245. return (SUCCEEDED(hr)) ? 0 : 4;
  246. }
  247. else if (hr == E_NOINTERFACE) {
  248. text_printf("Experimental shader model feature failed with error E_NOINTERFACE.\n");
  249. text_printf("The most likely cause is that Windows Developer mode is not on.\n");
  250. text_printf("See https://msdn.microsoft.com/en-us/windows/uwp/get-started/enable-your-device-for-development\n");
  251. json_printf("{ \"err\": \"E_NOINTERFACE\" }");
  252. json_printf("\n}\n");
  253. return 3;
  254. }
  255. else if (hr == E_INVALIDARG) {
  256. text_printf("Experimental shader model feature failed with error E_INVALIDARG.\n");
  257. text_printf("This means the configuration of a feature is incorrect, the set of features passed\n"
  258. "in are known to be incompatible with each other, or other errors occured,\n"
  259. "and is generally unexpected for the experimental shader model feature.\n");
  260. json_printf("{ \"err\": \"E_INVALIDARG\" }");
  261. json_printf("\n}\n");
  262. return 4;
  263. }
  264. else {
  265. text_printf("Experimental shader model feature failed with unexpected HRESULT 0x%08x.\n", hr);
  266. json_printf("{ \"err\": \"0x%08x\" }", hr);
  267. json_printf("\n}\n");
  268. return 4;
  269. }
  270. }