1 // Copyright 2018 The Dawn Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #include "dawn_native/d3d12/PlatformFunctions.h" 16 17 #include "common/DynamicLib.h" 18 19 #include <comdef.h> 20 #include <array> 21 #include <sstream> 22 23 namespace dawn_native { namespace d3d12 { 24 namespace { 25 // Extract Version from "10.0.{Version}.0" if possible, otherwise return 0. GetWindowsSDKVersionFromDirectoryName(const char * directoryName)26 uint32_t GetWindowsSDKVersionFromDirectoryName(const char* directoryName) { 27 constexpr char kPrefix[] = "10.0."; 28 constexpr char kPostfix[] = ".0"; 29 30 constexpr uint32_t kPrefixLen = sizeof(kPrefix) - 1; 31 constexpr uint32_t kPostfixLen = sizeof(kPostfix) - 1; 32 const uint32_t directoryNameLen = strlen(directoryName); 33 34 if (directoryNameLen < kPrefixLen + kPostfixLen + 1) { 35 return 0; 36 } 37 38 // Check if directoryName starts with "10.0.". 39 if (strncmp(directoryName, kPrefix, kPrefixLen) != 0) { 40 return 0; 41 } 42 43 // Check if directoryName ends with ".0". 44 if (strncmp(directoryName + (directoryNameLen - kPostfixLen), kPostfix, kPostfixLen) != 45 0) { 46 return 0; 47 } 48 49 // Extract Version from "10.0.{Version}.0" and convert Version into an integer. 50 return atoi(directoryName + kPrefixLen); 51 } 52 53 class ScopedFileHandle final { 54 public: ScopedFileHandle(HANDLE handle)55 explicit ScopedFileHandle(HANDLE handle) : mHandle(handle) { 56 } ~ScopedFileHandle()57 ~ScopedFileHandle() { 58 if (mHandle != INVALID_HANDLE_VALUE) { 59 ASSERT(FindClose(mHandle)); 60 } 61 } GetHandle() const62 HANDLE GetHandle() const { 63 return mHandle; 64 } 65 66 private: 67 HANDLE mHandle; 68 }; 69 GetWindowsSDKBasePath()70 std::string GetWindowsSDKBasePath() { 71 const char* kDefaultWindowsSDKPath = 72 "C:\\Program Files (x86)\\Windows Kits\\10\\bin\\*"; 73 WIN32_FIND_DATAA fileData; 74 ScopedFileHandle handle(FindFirstFileA(kDefaultWindowsSDKPath, &fileData)); 75 if (handle.GetHandle() == INVALID_HANDLE_VALUE) { 76 return ""; 77 } 78 79 uint32_t highestWindowsSDKVersion = 0; 80 do { 81 if (!(fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { 82 continue; 83 } 84 85 highestWindowsSDKVersion = 86 std::max(highestWindowsSDKVersion, 87 GetWindowsSDKVersionFromDirectoryName(fileData.cFileName)); 88 } while (FindNextFileA(handle.GetHandle(), &fileData)); 89 90 if (highestWindowsSDKVersion == 0) { 91 return ""; 92 } 93 94 // Currently we only support using DXC on x64. 95 std::ostringstream ostream; 96 ostream << "C:\\Program Files (x86)\\Windows Kits\\10\\bin\\10.0." 97 << highestWindowsSDKVersion << ".0\\x64\\"; 98 99 return ostream.str(); 100 } 101 } // anonymous namespace 102 103 PlatformFunctions::PlatformFunctions() = default; 104 PlatformFunctions::~PlatformFunctions() = default; 105 LoadFunctions()106 MaybeError PlatformFunctions::LoadFunctions() { 107 DAWN_TRY(LoadD3D12()); 108 DAWN_TRY(LoadDXGI()); 109 LoadDXCLibraries(); 110 DAWN_TRY(LoadFXCompiler()); 111 DAWN_TRY(LoadD3D11()); 112 LoadPIXRuntime(); 113 return {}; 114 } 115 LoadD3D12()116 MaybeError PlatformFunctions::LoadD3D12() { 117 #if DAWN_PLATFORM_WINUWP 118 d3d12CreateDevice = &D3D12CreateDevice; 119 d3d12GetDebugInterface = &D3D12GetDebugInterface; 120 d3d12SerializeRootSignature = &D3D12SerializeRootSignature; 121 d3d12CreateRootSignatureDeserializer = &D3D12CreateRootSignatureDeserializer; 122 d3d12SerializeVersionedRootSignature = &D3D12SerializeVersionedRootSignature; 123 d3d12CreateVersionedRootSignatureDeserializer = 124 &D3D12CreateVersionedRootSignatureDeserializer; 125 #else 126 std::string error; 127 if (!mD3D12Lib.Open("d3d12.dll", &error) || 128 !mD3D12Lib.GetProc(&d3d12CreateDevice, "D3D12CreateDevice", &error) || 129 !mD3D12Lib.GetProc(&d3d12GetDebugInterface, "D3D12GetDebugInterface", &error) || 130 !mD3D12Lib.GetProc(&d3d12SerializeRootSignature, "D3D12SerializeRootSignature", 131 &error) || 132 !mD3D12Lib.GetProc(&d3d12CreateRootSignatureDeserializer, 133 "D3D12CreateRootSignatureDeserializer", &error) || 134 !mD3D12Lib.GetProc(&d3d12SerializeVersionedRootSignature, 135 "D3D12SerializeVersionedRootSignature", &error) || 136 !mD3D12Lib.GetProc(&d3d12CreateVersionedRootSignatureDeserializer, 137 "D3D12CreateVersionedRootSignatureDeserializer", &error)) { 138 return DAWN_INTERNAL_ERROR(error.c_str()); 139 } 140 #endif 141 142 return {}; 143 } 144 LoadD3D11()145 MaybeError PlatformFunctions::LoadD3D11() { 146 #if DAWN_PLATFORM_WINUWP 147 d3d11on12CreateDevice = &D3D11On12CreateDevice; 148 #else 149 std::string error; 150 if (!mD3D11Lib.Open("d3d11.dll", &error) || 151 !mD3D11Lib.GetProc(&d3d11on12CreateDevice, "D3D11On12CreateDevice", &error)) { 152 return DAWN_INTERNAL_ERROR(error.c_str()); 153 } 154 #endif 155 156 return {}; 157 } 158 LoadDXGI()159 MaybeError PlatformFunctions::LoadDXGI() { 160 #if DAWN_PLATFORM_WINUWP 161 # if defined(_DEBUG) 162 // DXGIGetDebugInterface1 is tagged as a development-only capability 163 // which implies that linking to this function will cause 164 // the application to fail Windows store certification 165 // But we need it when debuging using VS Graphics Diagnostics or PIX 166 // So we only link to it in debug build 167 dxgiGetDebugInterface1 = &DXGIGetDebugInterface1; 168 # endif 169 createDxgiFactory2 = &CreateDXGIFactory2; 170 #else 171 std::string error; 172 if (!mDXGILib.Open("dxgi.dll", &error) || 173 !mDXGILib.GetProc(&dxgiGetDebugInterface1, "DXGIGetDebugInterface1", &error) || 174 !mDXGILib.GetProc(&createDxgiFactory2, "CreateDXGIFactory2", &error)) { 175 return DAWN_INTERNAL_ERROR(error.c_str()); 176 } 177 #endif 178 179 return {}; 180 } 181 LoadDXCLibraries()182 void PlatformFunctions::LoadDXCLibraries() { 183 // TODO(dawn:766) 184 // Statically linked with dxcompiler.lib in UWP 185 // currently linked with dxcompiler.lib making CoreApp unable to activate 186 // LoadDXIL and LoadDXCompiler will fail in UWP, but LoadFunctions() can still be 187 // successfully executed. 188 189 const std::string& windowsSDKBasePath = GetWindowsSDKBasePath(); 190 191 LoadDXIL(windowsSDKBasePath); 192 LoadDXCompiler(windowsSDKBasePath); 193 } 194 LoadDXIL(const std::string & baseWindowsSDKPath)195 void PlatformFunctions::LoadDXIL(const std::string& baseWindowsSDKPath) { 196 const char* dxilDLLName = "dxil.dll"; 197 const std::array<std::string, 2> kDxilDLLPaths = { 198 {dxilDLLName, baseWindowsSDKPath + dxilDLLName}}; 199 200 for (const std::string& dxilDLLPath : kDxilDLLPaths) { 201 if (mDXILLib.Open(dxilDLLPath, nullptr)) { 202 return; 203 } 204 } 205 ASSERT(!mDXILLib.Valid()); 206 } 207 LoadDXCompiler(const std::string & baseWindowsSDKPath)208 void PlatformFunctions::LoadDXCompiler(const std::string& baseWindowsSDKPath) { 209 // DXIL must be loaded before DXC, otherwise shader signing is unavailable 210 if (!mDXILLib.Valid()) { 211 return; 212 } 213 214 const char* dxCompilerDLLName = "dxcompiler.dll"; 215 const std::array<std::string, 2> kDxCompilerDLLPaths = { 216 {dxCompilerDLLName, baseWindowsSDKPath + dxCompilerDLLName}}; 217 218 DynamicLib dxCompilerLib; 219 for (const std::string& dxCompilerDLLName : kDxCompilerDLLPaths) { 220 if (dxCompilerLib.Open(dxCompilerDLLName, nullptr)) { 221 break; 222 } 223 } 224 225 if (dxCompilerLib.Valid() && 226 dxCompilerLib.GetProc(&dxcCreateInstance, "DxcCreateInstance", nullptr)) { 227 mDXCompilerLib = std::move(dxCompilerLib); 228 } else { 229 mDXILLib.Close(); 230 } 231 } 232 LoadFXCompiler()233 MaybeError PlatformFunctions::LoadFXCompiler() { 234 #if DAWN_PLATFORM_WINUWP 235 d3dCompile = &D3DCompile; 236 d3dDisassemble = &D3DDisassemble; 237 #else 238 std::string error; 239 if (!mFXCompilerLib.Open("d3dcompiler_47.dll", &error) || 240 !mFXCompilerLib.GetProc(&d3dCompile, "D3DCompile", &error) || 241 !mFXCompilerLib.GetProc(&d3dDisassemble, "D3DDisassemble", &error)) { 242 return DAWN_INTERNAL_ERROR(error.c_str()); 243 } 244 #endif 245 return {}; 246 } 247 IsPIXEventRuntimeLoaded() const248 bool PlatformFunctions::IsPIXEventRuntimeLoaded() const { 249 return mPIXEventRuntimeLib.Valid(); 250 } 251 IsDXCAvailable() const252 bool PlatformFunctions::IsDXCAvailable() const { 253 return mDXILLib.Valid() && mDXCompilerLib.Valid(); 254 } 255 LoadPIXRuntime()256 void PlatformFunctions::LoadPIXRuntime() { 257 // TODO(dawn:766): 258 // In UWP PIX should be statically linked WinPixEventRuntime_UAP.lib 259 // So maybe we should put WinPixEventRuntime as a third party package 260 // Currently PIX is not going to be loaded in UWP since the following 261 // mPIXEventRuntimeLib.Open will fail. 262 if (!mPIXEventRuntimeLib.Open("WinPixEventRuntime.dll") || 263 !mPIXEventRuntimeLib.GetProc(&pixBeginEventOnCommandList, 264 "PIXBeginEventOnCommandList") || 265 !mPIXEventRuntimeLib.GetProc(&pixEndEventOnCommandList, "PIXEndEventOnCommandList") || 266 !mPIXEventRuntimeLib.GetProc(&pixSetMarkerOnCommandList, "PIXSetMarkerOnCommandList")) { 267 mPIXEventRuntimeLib.Close(); 268 } 269 } 270 271 }} // namespace dawn_native::d3d12 272