From 34ff214b44bd04bff33862df515941a6ac628da3 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 12 Feb 2021 23:06:34 +0100 Subject: [PATCH] Switched shader compiler to thread-local instances --- .../Interop/ReflectionServices.cs | 2 +- src/ComputeSharp.Shaders/ShaderRunner{T}.cs | 2 +- .../ShaderCompiler.LibraryLoading.cs | 2 +- .../Translation/ShaderCompiler.cs | 41 +++++++++++++++---- .../ShaderCompilerTests.cs | 4 +- 5 files changed, 38 insertions(+), 13 deletions(-) diff --git a/src/ComputeSharp.Shaders/Interop/ReflectionServices.cs b/src/ComputeSharp.Shaders/Interop/ReflectionServices.cs index 2b2d79cf..c0a04789 100644 --- a/src/ComputeSharp.Shaders/Interop/ReflectionServices.cs +++ b/src/ComputeSharp.Shaders/Interop/ReflectionServices.cs @@ -51,7 +51,7 @@ namespace ComputeSharp.Interop using var shaderSource = HlslShaderRenderer.Render(1, 1, 1, shaderLoader); - using ComPtr dxcBlobBytecode = ShaderCompiler.CompileShader(shaderSource.WrittenSpan); + using ComPtr dxcBlobBytecode = ShaderCompiler.Instance.CompileShader(shaderSource.WrittenSpan); using ComPtr dxcUtils = default; Guid dxcLibraryClsid = FX.CLSID_DxcLibrary; diff --git a/src/ComputeSharp.Shaders/ShaderRunner{T}.cs b/src/ComputeSharp.Shaders/ShaderRunner{T}.cs index 6efd7e14..0bbac8f9 100644 --- a/src/ComputeSharp.Shaders/ShaderRunner{T}.cs +++ b/src/ComputeSharp.Shaders/ShaderRunner{T}.cs @@ -140,7 +140,7 @@ namespace ComputeSharp.Shaders using var shaderSource = HlslShaderRenderer.Render(threadsX, threadsY, threadsZ, shaderLoader); // Compile the loaded shader to HLSL bytecode - IDxcBlobObject shaderBytecode = new(ShaderCompiler.CompileShader(shaderSource.WrittenSpan)); + IDxcBlobObject shaderBytecode = new(ShaderCompiler.Instance.CompileShader(shaderSource.WrittenSpan)); // Get the cached shader data shaderData = new CachedShader(shaderLoader, shaderBytecode); diff --git a/src/ComputeSharp.Shaders/Translation/ShaderCompiler.LibraryLoading.cs b/src/ComputeSharp.Shaders/Translation/ShaderCompiler.LibraryLoading.cs index 61d2b051..69f140cc 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderCompiler.LibraryLoading.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderCompiler.LibraryLoading.cs @@ -6,7 +6,7 @@ using FX = TerraFX.Interop.Windows; namespace ComputeSharp.Shaders.Translation { /// - internal static unsafe partial class ShaderCompiler + internal sealed unsafe partial class ShaderCompiler { /// /// Initializes the DLL resolvers for the dxcompiler.dll and dxil.dll libraries. diff --git a/src/ComputeSharp.Shaders/Translation/ShaderCompiler.cs b/src/ComputeSharp.Shaders/Translation/ShaderCompiler.cs index 321e4548..cbc6e6b6 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderCompiler.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderCompiler.cs @@ -11,29 +11,39 @@ namespace ComputeSharp.Shaders.Translation /// /// A that uses the DXC APIs to compile compute shaders. /// - internal static unsafe partial class ShaderCompiler + internal sealed unsafe partial class ShaderCompiler { + /// + /// The thread local instance. + /// This is necessary because the DXC library is strictly single-threaded. + /// + [ThreadStatic] + private static ShaderCompiler? instance; + /// /// The instance to use to create the bytecode for HLSL sources. /// - private static readonly ComPtr DxcCompiler; + private readonly ComPtr DxcCompiler; /// /// The instance to use to work with . /// - private static readonly ComPtr DxcLibrary; + private readonly ComPtr DxcLibrary; /// /// The instance used to compile shaders with . /// - private static readonly ComPtr DxcIncludeHandler; + private readonly ComPtr DxcIncludeHandler; /// - /// Initializes , and . + /// Creates a new instance. /// - static ShaderCompiler() + private ShaderCompiler() { - InitializeDxcLibrariesLoading(); + lock(this) + { + InitializeDxcLibrariesLoading(); + } using ComPtr dxcCompiler = default; using ComPtr dxcLibrary = default; @@ -52,13 +62,28 @@ namespace ComputeSharp.Shaders.Translation DxcIncludeHandler = dxcIncludeHandler.Move(); } + /// + /// Destroys the current instance. + /// + ~ShaderCompiler() + { + DxcCompiler.Dispose(); + DxcLibrary.Dispose(); + DxcIncludeHandler.Dispose(); + } + + /// + /// Gets a instance to use. + /// + public static ShaderCompiler Instance => instance ??= new(); + /// /// Compiles a new HLSL shader from the input source code. /// /// The HLSL source code to compile. /// The bytecode for the compiled shader. [Pure] - public static ComPtr CompileShader(ReadOnlySpan source) + public ComPtr CompileShader(ReadOnlySpan source) { using ComPtr dxcBlobEncoding = default; using ComPtr dxcOperationResult = default; diff --git a/tests/ComputeSharp.Tests.Internals/ShaderCompilerTests.cs b/tests/ComputeSharp.Tests.Internals/ShaderCompilerTests.cs index 611628d2..cdf75920 100644 --- a/tests/ComputeSharp.Tests.Internals/ShaderCompilerTests.cs +++ b/tests/ComputeSharp.Tests.Internals/ShaderCompilerTests.cs @@ -31,7 +31,7 @@ namespace ComputeSharp.Tests.Internals [TestMethod] public void CompileTest_Ok() { - using ComPtr dxcBlob = ShaderCompiler.CompileShader(ShaderSource); + using ComPtr dxcBlob = ShaderCompiler.Instance.CompileShader(ShaderSource); } [TestMethod] @@ -40,7 +40,7 @@ namespace ComputeSharp.Tests.Internals { var faultyShader = ShaderSource.Replace("ids.x", "ids.X"); - using ComPtr dxcBlob = ShaderCompiler.CompileShader(faultyShader); + using ComPtr dxcBlob = ShaderCompiler.Instance.CompileShader(faultyShader); } } }