Switched shader compiler to thread-local instances

このコミットが含まれているのは:
Sergio Pedri committed 2021-02-12 23:06:34 +01:00
1 親 7cad8dae60
コミット 34ff214b44
5ファイル変更
+38 -13

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

+1 -1
ファイルの表示
@@ -51,7 +51,7 @@ namespace ComputeSharp.Interop
using var shaderSource = HlslShaderRenderer.Render(1, 1, 1, shaderLoader);
using ComPtr<IDxcBlob> dxcBlobBytecode = ShaderCompiler.CompileShader(shaderSource.WrittenSpan);
using ComPtr<IDxcBlob> dxcBlobBytecode = ShaderCompiler.Instance.CompileShader(shaderSource.WrittenSpan);
using ComPtr<IDxcUtils> dxcUtils = default;
Guid dxcLibraryClsid = FX.CLSID_DxcLibrary;
+1 -1
ファイルの表示
@@ -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<T>(shaderLoader, shaderBytecode);
+1 -1
ファイルの表示
@@ -6,7 +6,7 @@ using FX = TerraFX.Interop.Windows;
namespace ComputeSharp.Shaders.Translation
{
/// <inheritdoc cref="ShaderCompiler"/>
internal static unsafe partial class ShaderCompiler
internal sealed unsafe partial class ShaderCompiler
{
/// <summary>
/// Initializes the DLL resolvers for the dxcompiler.dll and dxil.dll libraries.
+33 -8
ファイルの表示
@@ -11,29 +11,39 @@ namespace ComputeSharp.Shaders.Translation
/// <summary>
/// A <see langword="class"/> that uses the DXC APIs to compile compute shaders.
/// </summary>
internal static unsafe partial class ShaderCompiler
internal sealed unsafe partial class ShaderCompiler
{
/// <summary>
/// The thread local <see cref="ShaderCompiler"/> instance.
/// This is necessary because the DXC library is strictly single-threaded.
/// </summary>
[ThreadStatic]
private static ShaderCompiler? instance;
/// <summary>
/// The <see cref="IDxcCompiler"/> instance to use to create the bytecode for HLSL sources.
/// </summary>
private static readonly ComPtr<IDxcCompiler> DxcCompiler;
private readonly ComPtr<IDxcCompiler> DxcCompiler;
/// <summary>
/// The <see cref="IDxcLibrary"/> instance to use to work with <see cref="DxcCompiler"/>.
/// </summary>
private static readonly ComPtr<IDxcLibrary> DxcLibrary;
private readonly ComPtr<IDxcLibrary> DxcLibrary;
/// <summary>
/// The <see cref="IDxcIncludeHandler"/> instance used to compile shaders with <see cref="DxcCompiler"/>.
/// </summary>
private static readonly ComPtr<IDxcIncludeHandler> DxcIncludeHandler;
private readonly ComPtr<IDxcIncludeHandler> DxcIncludeHandler;
/// <summary>
/// Initializes <see cref="DxcCompiler"/>, <see cref="DxcLibrary"/> and <see cref="DxcIncludeHandler"/>.
/// Creates a new <see cref="ShaderCompiler"/> instance.
/// </summary>
static ShaderCompiler()
private ShaderCompiler()
{
InitializeDxcLibrariesLoading();
lock(this)
{
InitializeDxcLibrariesLoading();
}
using ComPtr<IDxcCompiler> dxcCompiler = default;
using ComPtr<IDxcLibrary> dxcLibrary = default;
@@ -52,13 +62,28 @@ namespace ComputeSharp.Shaders.Translation
DxcIncludeHandler = dxcIncludeHandler.Move();
}
/// <summary>
/// Destroys the current <see cref="ShaderCompiler"/> instance.
/// </summary>
~ShaderCompiler()
{
DxcCompiler.Dispose();
DxcLibrary.Dispose();
DxcIncludeHandler.Dispose();
}
/// <summary>
/// Gets a <see cref="ShaderCompiler"/> instance to use.
/// </summary>
public static ShaderCompiler Instance => instance ??= new();
/// <summary>
/// Compiles a new HLSL shader from the input source code.
/// </summary>
/// <param name="source">The HLSL source code to compile.</param>
/// <returns>The bytecode for the compiled shader.</returns>
[Pure]
public static ComPtr<IDxcBlob> CompileShader(ReadOnlySpan<char> source)
public ComPtr<IDxcBlob> CompileShader(ReadOnlySpan<char> source)
{
using ComPtr<IDxcBlobEncoding> dxcBlobEncoding = default;
using ComPtr<IDxcOperationResult> dxcOperationResult = default;
+2 -2
ファイルの表示
@@ -31,7 +31,7 @@ namespace ComputeSharp.Tests.Internals
[TestMethod]
public void CompileTest_Ok()
{
using ComPtr<IDxcBlob> dxcBlob = ShaderCompiler.CompileShader(ShaderSource);
using ComPtr<IDxcBlob> dxcBlob = ShaderCompiler.Instance.CompileShader(ShaderSource);
}
[TestMethod]
@@ -40,7 +40,7 @@ namespace ComputeSharp.Tests.Internals
{
var faultyShader = ShaderSource.Replace("ids.x", "ids.X");
using ComPtr<IDxcBlob> dxcBlob = ShaderCompiler.CompileShader(faultyShader);
using ComPtr<IDxcBlob> dxcBlob = ShaderCompiler.Instance.CompileShader(faultyShader);
}
}
}