Improved some comments

このコミットが含まれているのは:
Sergio Pedri committed 2020-12-29 23:51:07 +01:00
1 親 9146efc596
コミット 7cf7bf6c85
2ファイル変更
+85 -37

検索条件に一致するファイルはありません

+25 -21
ファイルの表示
@@ -7,7 +7,6 @@ using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using ComputeSharp.Core.Extensions;
using ComputeSharp.Exceptions;
using Microsoft.Toolkit.Diagnostics;
using TerraFX.Interop;
using FX = TerraFX.Interop.Windows;
@@ -62,32 +61,35 @@ namespace ComputeSharp.Shaders.Translation
/// </summary>
private static void InitializeDxcLibrariesLoading()
{
// We are probing for a "dotnet run" scenario
string nugetNativeLibsPath = Path.Combine(AppContext.BaseDirectory, @$"runtimes\win-x64\native");
bool probeNuGetDirectory = Directory.Exists(nugetNativeLibsPath);
// Test whether the native libraries are present in the same folder of the executable
// (which is the case when the program was built with a runtime identifier), or whether
// they are in the "runtimes\win-x64\native" folder in the executable directory.
string nugetNativeLibsPath = Path.Combine(AppContext.BaseDirectory, @"runtimes\win-x64\native");
bool isNuGetRuntimeLibrariesDirectoryPresent = Directory.Exists(nugetNativeLibsPath);
// We fail to load "dxil.dll" when doing "dotnet bin\Debug\net5.0\MyApp.dll"
FX.ResolveLibrary += (n, a, s) => ResolveLibrary(n, a, s, probeNuGetDirectory);
// Register a custom library resolver for the two DXC libraries. We need to either manually load the two
// libraries from the NuGet directory, if an RID is not in use, or we need to ensure that dxil.dll is
// loaded correctly in case the program was executed with the host being in another directory.
// This happens when doing eg. "dotnet bin\Debug\net5.0\MyApp.dll", which would crash at runtime.
FX.ResolveLibrary += (n, a, s) => ResolveLibrary(n, a, s, isNuGetRuntimeLibrariesDirectoryPresent);
}
/// <summary>
/// A custom resolver to override the default library resolution behavior for the DXC and DXIL libraries.
/// Meant to enable "dotnet run"-like scenarios for projects without RIDs by loading native dependencies from where the SDK puts them.
/// This either loads the libraries from the NuGet directory, or just ensures that DXIL is always loaded.
/// </summary>
/// <inheritdoc cref="DllImportResolver"/>
private static IntPtr ResolveLibrary(string libraryName, Assembly assembly, DllImportSearchPath? searchPath, bool probeNuGetDirectory)
private static IntPtr ResolveLibrary(string libraryName, Assembly assembly, DllImportSearchPath? searchPath, bool isNuGetRuntimeLibrariesDirectoryPresent)
{
if (libraryName != "dxcompiler") return IntPtr.Zero;
if (libraryName is not "dxcompiler") return IntPtr.Zero;
if (probeNuGetDirectory)
if (isNuGetRuntimeLibrariesDirectoryPresent)
{
string dxcompilerPath = Path.Combine(AppContext.BaseDirectory, @$"runtimes\win-x64\native\dxcompiler.dll");
string dxilPath = Path.Combine(AppContext.BaseDirectory, @$"runtimes\win-x64\native\dxil.dll");
string
dxcompilerPath = Path.Combine(AppContext.BaseDirectory, @"runtimes\win-x64\native\dxcompiler.dll"),
dxilPath = Path.Combine(AppContext.BaseDirectory, @"runtimes\win-x64\native\dxil.dll");
// We are loading the dependency of dxcompiler.dll, dxil.dll, first, so that it is automatically picked up
// from memory by dxcompiler.dll on it being loaded
// This is necessary because .NET does not currently provide great facilities for loading
// native dependencies of native dependencies
// Load DXIL first so that DXC doesn't fail to load it, and then DXIL, both from the NuGet path
if (NativeLibrary.TryLoad(dxilPath, out _) && NativeLibrary.TryLoad(dxcompilerPath, out IntPtr handle))
{
return handle;
@@ -95,11 +97,13 @@ namespace ComputeSharp.Shaders.Translation
}
else
{
// It may come as a surprise we need this, but apparently we do
// The suspicion is that's because Windows doesn't know anything about .NET's
// "App directory" concept, only "Process directory", and when we invoke "dotnet bin\Managed.dll"
// Neither our current directory ("bin"), nor our "Process directory" ("C:\Program Files\dotnet") contain the native dependency
// Which means LoadLibraryEx fails to load it
// Even when the two libraries are correctly copied next to the executable in use, we load them
// manually to ensure the operation is successful. This is to avoid failures in cases such as when
// doing "dotnet bin\MyApp.dll", ie. when the host is in another path than the executable in use.
// This is probably because DXIL is a native dependency for DXC, but the way Windows loads these
// libraries doesn't take into account the .NET concepts of "app directory": neither the current "bin"
// directory nor the "process directory", which is "C:\Program Files\dotnet", actually contain the
// native library we need, hence the runtime crash. Manually loading the library this way solves this.
if (NativeLibrary.TryLoad("dxil", assembly, searchPath, out _) &&
NativeLibrary.TryLoad("dxcompiler", assembly, searchPath, out IntPtr handle))
{