diff --git a/ComputeSharp.sln b/ComputeSharp.sln index 715e9770..066d9426 100644 --- a/ComputeSharp.sln +++ b/ComputeSharp.sln @@ -43,7 +43,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.Sample.NuGet", EndProject Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "ComputeSharp.Sample.Shared", "samples\ComputeSharp.Sample.Shared\ComputeSharp.Sample.Shared.shproj", "{8CEFC324-CA6C-4845-9AD9-70B92BD87057}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ComputeSharp.Tests.Internals.SourceGenerators", "tests\ComputeSharp.Tests.Internals.SourceGenerators\ComputeSharp.Tests.Internals.SourceGenerators.csproj", "{BBBD0ADD-F2F9-49A3-BD5E-D223F0735D8B}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.Tests.Internals.SourceGenerators", "tests\ComputeSharp.Tests.Internals.SourceGenerators\ComputeSharp.Tests.Internals.SourceGenerators.csproj", "{BBBD0ADD-F2F9-49A3-BD5E-D223F0735D8B}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ComputeSharp.Tests.NativeLibrariesResolver", "tests\ComputeSharp.Tests.NativeLibrariesResolver\ComputeSharp.Tests.NativeLibrariesResolver.csproj", "{BF52326F-E415-49E6-8595-62762216762B}" EndProject Global GlobalSection(SharedMSBuildProjectFiles) = preSolution @@ -154,14 +156,6 @@ Global {C423C061-64F7-4C12-8E5F-75C5351D6308}.Release|Any CPU.Build.0 = Release|Any CPU {C423C061-64F7-4C12-8E5F-75C5351D6308}.Release|x64.ActiveCfg = Release|Any CPU {C423C061-64F7-4C12-8E5F-75C5351D6308}.Release|x64.Build.0 = Release|Any CPU - {149173F5-D0CE-4FEC-A040-DE472707D6B2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {149173F5-D0CE-4FEC-A040-DE472707D6B2}.Debug|Any CPU.Build.0 = Debug|Any CPU - {149173F5-D0CE-4FEC-A040-DE472707D6B2}.Debug|x64.ActiveCfg = Debug|Any CPU - {149173F5-D0CE-4FEC-A040-DE472707D6B2}.Debug|x64.Build.0 = Debug|Any CPU - {149173F5-D0CE-4FEC-A040-DE472707D6B2}.Release|Any CPU.ActiveCfg = Release|Any CPU - {149173F5-D0CE-4FEC-A040-DE472707D6B2}.Release|Any CPU.Build.0 = Release|Any CPU - {149173F5-D0CE-4FEC-A040-DE472707D6B2}.Release|x64.ActiveCfg = Release|Any CPU - {149173F5-D0CE-4FEC-A040-DE472707D6B2}.Release|x64.Build.0 = Release|Any CPU {BBBD0ADD-F2F9-49A3-BD5E-D223F0735D8B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {BBBD0ADD-F2F9-49A3-BD5E-D223F0735D8B}.Debug|Any CPU.Build.0 = Debug|Any CPU {BBBD0ADD-F2F9-49A3-BD5E-D223F0735D8B}.Debug|x64.ActiveCfg = Debug|Any CPU @@ -170,6 +164,14 @@ Global {BBBD0ADD-F2F9-49A3-BD5E-D223F0735D8B}.Release|Any CPU.Build.0 = Release|Any CPU {BBBD0ADD-F2F9-49A3-BD5E-D223F0735D8B}.Release|x64.ActiveCfg = Release|Any CPU {BBBD0ADD-F2F9-49A3-BD5E-D223F0735D8B}.Release|x64.Build.0 = Release|Any CPU + {BF52326F-E415-49E6-8595-62762216762B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BF52326F-E415-49E6-8595-62762216762B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BF52326F-E415-49E6-8595-62762216762B}.Debug|x64.ActiveCfg = Debug|Any CPU + {BF52326F-E415-49E6-8595-62762216762B}.Debug|x64.Build.0 = Debug|Any CPU + {BF52326F-E415-49E6-8595-62762216762B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BF52326F-E415-49E6-8595-62762216762B}.Release|Any CPU.Build.0 = Release|Any CPU + {BF52326F-E415-49E6-8595-62762216762B}.Release|x64.ActiveCfg = Release|Any CPU + {BF52326F-E415-49E6-8595-62762216762B}.Release|x64.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -184,6 +186,7 @@ Global {C423C061-64F7-4C12-8E5F-75C5351D6308} = {0ED8F632-5E17-46BE-8CC3-B14A82D4AEB1} {8CEFC324-CA6C-4845-9AD9-70B92BD87057} = {0ED8F632-5E17-46BE-8CC3-B14A82D4AEB1} {BBBD0ADD-F2F9-49A3-BD5E-D223F0735D8B} = {F8EFBB27-4EE2-4463-A75B-7EFDFB55D0F7} + {BF52326F-E415-49E6-8595-62762216762B} = {F8EFBB27-4EE2-4463-A75B-7EFDFB55D0F7} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {4664C5E3-0340-4E22-BCFD-98AAEDF5F2DC} diff --git a/src/ComputeSharp.Shaders/Translation/ShaderCompiler.cs b/src/ComputeSharp.Shaders/Translation/ShaderCompiler.cs index 06b89783..75e77b7d 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderCompiler.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderCompiler.cs @@ -62,39 +62,49 @@ namespace ComputeSharp.Shaders.Translation /// private static void InitializeDxcLibrariesLoading() { - string - dxcompilerPath = Path.GetFullPath(@$"runtimes\win-x64\native\dxcompiler.dll"), - dxilPath = Path.GetFullPath(@$"runtimes\win-x64\native\dxil.dll"); + // We are probing for a "dotnet run" scenario + string nugetNativeLibsPath = Path.Combine(AppContext.BaseDirectory, @$"runtimes\win-x64\native"); + bool probeNuGetDirectory = Directory.Exists(nugetNativeLibsPath); - if (File.Exists(dxcompilerPath)) - { - FX.ResolveLibrary += ResolveLibrary; - } - - if (File.Exists(dxilPath)) - { - fixed (char* p = @"runtimes\win-x64\native") - { - int result = FX.SetDllDirectoryW((ushort*)p); - - if (result == 0) ThrowHelper.ThrowWin32Exception(); - } - } + // 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); } /// /// 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. /// /// - private static IntPtr ResolveLibrary(string libraryName, Assembly assembly, DllImportSearchPath? searchPath) + private static IntPtr ResolveLibrary(string libraryName, Assembly assembly, DllImportSearchPath? searchPath, bool probeNuGetDirectory) { if (libraryName != "dxcompiler") return IntPtr.Zero; - string libraryPath = Path.GetFullPath(@$"runtimes\win-x64\native\{libraryName}.dll"); - - if (NativeLibrary.TryLoad(libraryPath, out IntPtr handle)) + if (probeNuGetDirectory) { - return handle; + string dxcompilerPath = Path.Combine(AppContext.BaseDirectory, @$"runtimes\win-x64\native\dxcompiler.dll"); + string 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 + if (NativeLibrary.TryLoad(dxilPath, out _) && NativeLibrary.TryLoad(dxcompilerPath, out IntPtr handle)) + { + return handle; + } + } + 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 + if (NativeLibrary.TryLoad("dxil", assembly, searchPath, out _) && + NativeLibrary.TryLoad("dxcompiler", assembly, searchPath, out IntPtr handle)) + { + return handle; + } } return IntPtr.Zero; diff --git a/tests/ComputeSharp.Tests.NativeLibrariesResolver/ComputeSharp.Tests.NativeLibrariesResolver.csproj b/tests/ComputeSharp.Tests.NativeLibrariesResolver/ComputeSharp.Tests.NativeLibrariesResolver.csproj new file mode 100644 index 00000000..36d9d5ca --- /dev/null +++ b/tests/ComputeSharp.Tests.NativeLibrariesResolver/ComputeSharp.Tests.NativeLibrariesResolver.csproj @@ -0,0 +1,16 @@ + + + + net5.0 + 9.0 + false + + + + + + + + + + diff --git a/tests/ComputeSharp.Tests.NativeLibrariesResolver/NativeLibrariesResolverTests.cs b/tests/ComputeSharp.Tests.NativeLibrariesResolver/NativeLibrariesResolverTests.cs new file mode 100644 index 00000000..27dedca6 --- /dev/null +++ b/tests/ComputeSharp.Tests.NativeLibrariesResolver/NativeLibrariesResolverTests.cs @@ -0,0 +1,152 @@ +using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.ComponentModel; +using System.Diagnostics; +using System.Globalization; +using System.IO; + +namespace ComputeSharp.Tests.NativeLibrariesResolver +{ + [TestClass] + public class NativeLibrariesResolverTests + { + static NativeLibrariesResolverTests() + { + var path = Path.GetDirectoryName(typeof(NativeLibrariesResolverTests).Assembly.Location); + while (Path.GetFileName(path) is not "ComputeSharp") + { + path = Path.GetDirectoryName(path); + } + + SampleProjectDirectory = Path.Combine(path, "samples", "ComputeSharp.Sample.NuGet"); + var sampleProjectPath = Path.Combine(SampleProjectDirectory, "ComputeSharp.Sample.NuGet.csproj"); + + var packagingProjectPath = Path.Combine(path, "src", "ComputeSharp.Package", "ComputeSharp.Package.msbuildproj"); + Process.Start("dotnet", $"pack {packagingProjectPath} -c Release").WaitForExit(); + // We use "--no-restore" where possible to speed up the tests + Process.Start("dotnet", $"restore {sampleProjectPath}").WaitForExit(); + } + + private static string SampleProjectDirectory { get; } + + [TestMethod] + [DataRow(Configuration.Debug, RID.None)] + [DataRow(Configuration.Debug, RID.Win_x64)] + [DataRow(Configuration.Release, RID.None)] + [DataRow(Configuration.Release, RID.Win_x64)] + public void DotnetRunWorks(Configuration configuration, RID rid) + { + // "dotnet run" fails with "--no-restore" + Assert.AreEqual(0, Exec(SampleProjectDirectory, "dotnet", $"run -c {configuration} {ToOption(rid)}")); + } + + [TestMethod] + [DataRow(Configuration.Debug, RID.None)] + [DataRow(Configuration.Debug, RID.Win_x64)] + [DataRow(Configuration.Release, RID.None)] + [DataRow(Configuration.Release, RID.Win_x64)] + public void DotnetBuildWithRunningDotnetHostFromProjectDirectoryWorks(Configuration configuration, RID rid) + { + BuildSampleProject(configuration, rid); + var realtivePathToDll = Path.Combine("bin", $"{configuration}", "net5.0", $"{ToDirectory(rid)}", "ComputeSharp.Sample.NuGet.dll"); + Assert.AreEqual(0, Exec(SampleProjectDirectory, "dotnet", realtivePathToDll)); + } + + [TestMethod] + [DataRow(Configuration.Debug, RID.None)] + [DataRow(Configuration.Debug, RID.Win_x64)] + [DataRow(Configuration.Release, RID.None)] + [DataRow(Configuration.Release, RID.Win_x64)] + public void DotnetBuildWithRunningDotnetHostDirectlyWorks(Configuration configuration, RID rid) + { + BuildSampleProject(configuration, rid); + var pathToDllDirectory = Path.Combine(SampleProjectDirectory, "bin", $"{configuration}", "net5.0", $"{ToDirectory(rid)}"); + Assert.AreEqual(0, Exec(pathToDllDirectory, "dotnet", "ComputeSharp.Sample.NuGet.dll")); + } + + [TestMethod] + [DataRow(Configuration.Debug, RID.None)] + [DataRow(Configuration.Debug, RID.Win_x64)] + [DataRow(Configuration.Release, RID.None)] + [DataRow(Configuration.Release, RID.Win_x64)] + public void DotnetBuildWithRunningAppHostFromProjectDirectoryWorks(Configuration configuration, RID rid) + { + BuildSampleProject(configuration, rid); + var relativePathToAppHost = Path.Combine("bin", $"{configuration}", "net5.0", $"{ToDirectory(rid)}", "ComputeSharp.Sample.NuGet.exe"); + Assert.AreEqual(0, Exec(SampleProjectDirectory, relativePathToAppHost, "")); + } + + [TestMethod] + [DataRow(Configuration.Debug, RID.None)] + [DataRow(Configuration.Debug, RID.Win_x64)] + [DataRow(Configuration.Release, RID.None)] + [DataRow(Configuration.Release, RID.Win_x64)] + public void DotnetBuildWithRunningAppHostDirectlyWorks(Configuration configuration, RID rid) + { + BuildSampleProject(configuration, rid); + var pathToAppHostDirectory = Path.Combine(SampleProjectDirectory, "bin", $"{configuration}", "net5.0", $"{ToDirectory(rid)}"); + Assert.AreEqual(0, Exec(pathToAppHostDirectory, "ComputeSharp.Sample.NuGet.exe", "")); + } + + [TestMethod] + [DataRow(Configuration.Debug)] + [DataRow(Configuration.Release)] + public void DotnetPublishWorks(Configuration configuration) + { + // We do not support publishing without the RID + Exec(SampleProjectDirectory, "dotnet", $"publish -c {configuration} -r win-x64 --no-restore"); + var pathToAppHost = Path.Combine("bin", $"{configuration}", "net5.0", "win-x64", "publish", "ComputeSharp.Sample.NuGet.exe"); + Assert.AreEqual(0, Exec(SampleProjectDirectory, pathToAppHost, "")); + } + + private static void BuildSampleProject(Configuration configuration, RID rid) + { + Exec(SampleProjectDirectory, "dotnet", $"build -c {configuration} {ToOption(rid)} --no-restore"); + } + + private static int Exec(string workingDirectory, string filePath, string arguments) + { + var startInfo = new ProcessStartInfo + { + UseShellExecute = true, + CreateNoWindow = true, + WindowStyle = ProcessWindowStyle.Hidden, + FileName = filePath, + Arguments = arguments, + WorkingDirectory = workingDirectory + }; + + var process = Process.Start(startInfo); + process.WaitForExit(); + + return process.ExitCode; + } + + private static string ToOption(RID rid) => rid switch + { + RID.None => "", + RID.Win_x64 => "-r win-x64", + _ => throw new InvalidEnumArgumentException(nameof(rid), (int)rid, typeof(RID)) + }; + + private static string ToDirectory(RID rid) => rid switch + { + RID.None => "", + RID.Win_x64 => "win-x64", + _ => throw new InvalidEnumArgumentException(nameof(rid), (int)rid, typeof(RID)) + }; + + public enum Configuration + { + Debug, + Release + } + + public enum RID + { + None, + Win_x64, + } + } +}