dxexp.cpp 10 KB

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