diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadBytecodeMethod.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadBytecodeMethod.cs
index 6a72ca51..e21a34e2 100644
--- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadBytecodeMethod.cs
+++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadBytecodeMethod.cs
@@ -29,6 +29,11 @@ partial class IShaderGenerator
///
private static partial class LoadBytecode
{
+ ///
+ /// The shared cache of values.
+ ///
+ private static readonly DynamicCache HlslBytecodeCache = new();
+
///
/// Indicates whether the required dxcompiler.dll and dxil.dll libraries have been loaded.
///
@@ -125,43 +130,48 @@ partial class IShaderGenerator
/// The instance for the shader to compile.
/// The used to cancel the operation, if needed.
/// The instance for the current shader.
- public static unsafe HlslBytecodeInfo GetBytecode(HlslBytecodeInfoKey key, CancellationToken token)
+ public static unsafe HlslBytecodeInfo GetBytecode(ref HlslBytecodeInfoKey key, CancellationToken token)
{
- // Skip even attempting to compile if compilation is disabled (see comments in D2D1 generator)
- if (!key.IsCompilationEnabled)
+ static unsafe HlslBytecodeInfo GetInfo(HlslBytecodeInfoKey key, CancellationToken token)
{
- return HlslBytecodeInfo.Missing.Instance;
+ // Skip even attempting to compile if compilation is disabled (see comments in D2D1 generator)
+ if (!key.IsCompilationEnabled)
+ {
+ return HlslBytecodeInfo.Missing.Instance;
+ }
+
+ try
+ {
+ // Try to load dxcompiler.dll and dxil.dll
+ LoadNativeDxcLibraries();
+
+ token.ThrowIfCancellationRequested();
+
+ // Compile the shader bytecode
+ using ComPtr dxcBlobBytecode = ShaderCompiler.Instance.Compile(key.HlslSource.AsSpan());
+
+ token.ThrowIfCancellationRequested();
+
+ byte* buffer = (byte*)dxcBlobBytecode.Get()->GetBufferPointer();
+ int length = checked((int)dxcBlobBytecode.Get()->GetBufferSize());
+
+ byte[] array = new ReadOnlySpan(buffer, length).ToArray();
+
+ ImmutableArray bytecode = Unsafe.As>(ref array);
+
+ return new HlslBytecodeInfo.Success(bytecode);
+ }
+ catch (Win32Exception e)
+ {
+ return new HlslBytecodeInfo.Win32Error(e.NativeErrorCode, FixupExceptionMessage(e.Message));
+ }
+ catch (DxcCompilationException e)
+ {
+ return new HlslBytecodeInfo.CompilerError(FixupExceptionMessage(e.Message));
+ }
}
- try
- {
- // Try to load dxcompiler.dll and dxil.dll
- LoadNativeDxcLibraries();
-
- token.ThrowIfCancellationRequested();
-
- // Compile the shader bytecode
- using ComPtr dxcBlobBytecode = ShaderCompiler.Instance.Compile(key.HlslSource.AsSpan());
-
- token.ThrowIfCancellationRequested();
-
- byte* buffer = (byte*)dxcBlobBytecode.Get()->GetBufferPointer();
- int length = checked((int)dxcBlobBytecode.Get()->GetBufferSize());
-
- byte[] array = new ReadOnlySpan(buffer, length).ToArray();
-
- ImmutableArray bytecode = Unsafe.As>(ref array);
-
- return new HlslBytecodeInfo.Success(bytecode);
- }
- catch (Win32Exception e)
- {
- return new HlslBytecodeInfo.Win32Error(e.NativeErrorCode, FixupExceptionMessage(e.Message));
- }
- catch (DxcCompilationException e)
- {
- return new HlslBytecodeInfo.CompilerError(FixupExceptionMessage(e.Message));
- }
+ return HlslBytecodeCache.GetOrCreate(ref key, GetInfo, token);
}
///
diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs
index cf3a562a..ecabb530 100644
--- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs
+++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs
@@ -103,7 +103,7 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator
HlslBytecodeInfoKey hlslInfoKey = new(hlslSource, isCompilationEnabled);
// Try to get the HLSL bytecode
- HlslBytecodeInfo hlslInfo = LoadBytecode.GetBytecode(hlslInfoKey, token);
+ HlslBytecodeInfo hlslInfo = LoadBytecode.GetBytecode(ref hlslInfoKey, token);
token.ThrowIfCancellationRequested();