From 604ca2fea7e3942c1e5e3d69a5622fb2867bcf3d Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 29 Dec 2020 13:57:00 +0100 Subject: [PATCH] Added custom DXC/DXIL libraries resolver --- .../ComputeSharp.Graphics.csproj | 2 +- .../Translation/ShaderCompiler.cs | 23 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/ComputeSharp.Graphics/ComputeSharp.Graphics.csproj b/src/ComputeSharp.Graphics/ComputeSharp.Graphics.csproj index 945a84ec..8c720d45 100644 --- a/src/ComputeSharp.Graphics/ComputeSharp.Graphics.csproj +++ b/src/ComputeSharp.Graphics/ComputeSharp.Graphics.csproj @@ -7,7 +7,7 @@ - + diff --git a/src/ComputeSharp.Shaders/Translation/ShaderCompiler.cs b/src/ComputeSharp.Shaders/Translation/ShaderCompiler.cs index 38c19d3a..3992bc49 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderCompiler.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderCompiler.cs @@ -1,7 +1,10 @@ using System; using System.Diagnostics.CodeAnalysis; using System.Diagnostics.Contracts; +using System.IO; +using System.Reflection; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; using ComputeSharp.Core.Extensions; using ComputeSharp.Exceptions; using TerraFX.Interop; @@ -34,6 +37,8 @@ namespace ComputeSharp.Shaders.Translation /// static ShaderCompiler() { + FX.ResolveLibrary += ResolveLibrary; + using ComPtr dxcCompiler = default; using ComPtr dxcLibrary = default; using ComPtr dxcIncludeHandler = default; @@ -51,6 +56,24 @@ namespace ComputeSharp.Shaders.Translation DxcIncludeHandler = dxcIncludeHandler.Move(); } + /// + /// A custom resolver to override the default library resolution behavior for the DXC and DXIL libraries. + /// + /// + private static IntPtr ResolveLibrary(string libraryName, Assembly assembly, DllImportSearchPath? searchPath) + { + if (libraryName != "dxcompiler" && libraryName != "dxil") return IntPtr.Zero; + + string libraryPath = Path.GetFullPath(@$"runtimes\win-x64\native\{libraryName}.dll"); + + if (NativeLibrary.TryLoad(libraryPath, out IntPtr handle)) + { + return handle; + } + + return IntPtr.Zero; + } + /// /// Compiles a new HLSL shader from the input source code. ///