Move library loading logic to ReflectionServices

このコミットが含まれているのは:
Sergio Pedri committed 2023-10-17 01:55:16 +02:00
1 親 1734c09d1a
コミット 27596e26f5
4ファイル変更
+80 -79

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

+77
ファイルの表示
@@ -0,0 +1,77 @@
using System;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
namespace ComputeSharp.Interop;
/// <inheritdoc/>
partial class ReflectionServices
{
/// <summary>
/// Initializes the DLL resolvers for the dxcompiler.dll and dxil.dll libraries.
/// </summary>
static ReflectionServices()
{
// 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\net8.0\MyApp.dll", which would crash at runtime.
NativeLibrary.SetDllImportResolver(typeof(ReflectionServices).Assembly, OnDllImport);
}
/// <summary>
/// The custom <see cref="DllImportResolver"/> for this assembly.
/// </summary>
/// <inheritdoc cref="DllImportResolver"/>
private static IntPtr OnDllImport(string libraryName, Assembly assembly, DllImportSearchPath? searchPath)
{
if (libraryName is not "dxcompiler")
{
return IntPtr.Zero;
}
string rid = RuntimeInformation.ProcessArchitecture switch
{
Architecture.X64 => "win-x64",
Architecture.Arm64 => "win-arm64",
_ => default(NotSupportedException).Throw<string>()
};
// 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\{rid}\native");
bool isNuGetRuntimeLibrariesDirectoryPresent = Directory.Exists(nugetNativeLibsPath);
if (isNuGetRuntimeLibrariesDirectoryPresent)
{
string dxcompilerPath = Path.Combine(AppContext.BaseDirectory, $@"runtimes\{rid}\native\dxcompiler.dll");
string dxilPath = Path.Combine(AppContext.BaseDirectory, $@"runtimes\{rid}\native\dxil.dll");
// 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;
}
}
else
{
// 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))
{
return handle;
}
}
return IntPtr.Zero;
}
}
+1 -1
ファイルの表示
@@ -13,7 +13,7 @@ namespace ComputeSharp.Interop;
/// <summary>
/// Provides methods to extract reflection info on compute shaders generated using this library.
/// </summary>
public static class ReflectionServices
public static partial class ReflectionServices
{
/// <summary>
/// Gets the shader info associated with a given compute shader.
-75
ファイルの表示
@@ -1,75 +0,0 @@
#if NET6_0_OR_GREATER
using System;
using System.IO;
using System.Runtime.InteropServices;
using TerraFX.Interop.Windows;
namespace ComputeSharp.Shaders.Translation;
/// <inheritdoc cref="ShaderCompiler"/>
unsafe partial class ShaderCompiler
{
/// <summary>
/// Initializes the DLL resolvers for the dxcompiler.dll and dxil.dll libraries.
/// </summary>
static ShaderCompiler()
{
// 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.
Windows.ResolveLibrary += static (libraryName, assembly, searchPath) =>
{
if (libraryName is not "dxcompiler")
{
return IntPtr.Zero;
}
string rid = RuntimeInformation.ProcessArchitecture switch
{
Architecture.X64 => "win-x64",
Architecture.Arm64 => "win-arm64",
_ => default(NotSupportedException).Throw<string>()
};
// 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\{rid}\native");
bool isNuGetRuntimeLibrariesDirectoryPresent = Directory.Exists(nugetNativeLibsPath);
if (isNuGetRuntimeLibrariesDirectoryPresent)
{
string dxcompilerPath = Path.Combine(AppContext.BaseDirectory, $@"runtimes\{rid}\native\dxcompiler.dll");
string dxilPath = Path.Combine(AppContext.BaseDirectory, $@"runtimes\{rid}\native\dxil.dll");
// 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;
}
}
else
{
// 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))
{
return handle;
}
}
return IntPtr.Zero;
};
}
}
#endif
+2 -3
ファイルの表示
@@ -10,7 +10,7 @@ namespace ComputeSharp.Shaders.Translation;
/// <summary>
/// A <see langword="class"/> that uses the DXC APIs to compile compute shaders.
/// </summary>
internal sealed unsafe partial class ShaderCompiler
internal sealed unsafe class ShaderCompiler
{
/// <summary>
/// The thread local <see cref="ShaderCompiler"/> instance.
@@ -158,8 +158,7 @@ internal sealed unsafe partial class ShaderCompiler
message = Regex.Replace(message, @"^hlsl\.hlsl:\d+:\d+: (\w+:)", static m => m.Groups[1].Value, RegexOptions.Multiline);
// Add a trailing '.' if not present
if (message is { Length: > 0 } &&
message[message.Length - 1] != '.')
if (message is [.., not '.'])
{
message += '.';
}