diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadBytecodeMethod.Syntax.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadBytecodeMethod.Syntax.cs
index c35cc2ca..bd414527 100644
--- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadBytecodeMethod.Syntax.cs
+++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadBytecodeMethod.Syntax.cs
@@ -12,38 +12,112 @@ partial class IShaderGenerator
partial class LoadBytecode
{
///
- /// Writes the LoadBytecode method.
+ /// Writes the HlslBytecode property.
///
/// The input instance with gathered shader info.
/// The instance to write into.
- public static void WriteSyntax(ShaderInfo info, IndentedTextWriter writer)
+ public static void WriteHlslBytecodeSyntax(ShaderInfo info, IndentedTextWriter writer)
{
writer.WriteLine("/// ");
writer.WriteGeneratedAttributes(GeneratorName);
- writer.WriteLine("readonly void global::ComputeSharp.__Internals.IShader.LoadBytecode(ref TLoader loader, int threadsX, int threadsY, int threadsZ)");
+ writer.Write($"readonly global::System.ReadOnlyMemory global::ComputeSharp.__Internals.IShader.HlslBytecode => ");
- using (writer.WriteBlock())
+ // Just like with the D2D1 generator, only return the MemoryManager if we do have HLSL bytecode
+ if (info.HlslInfo is not HlslBytecodeInfo.Success)
{
- // If there is no HLSL bytecode, there's nothing left to do
- if (info.HlslInfo is not HlslBytecodeInfo.Success success)
- {
- return;
- }
+ writer.WriteLine("default;");
+ }
+ else
+ {
+ writer.WriteLine("global::ComputeSharp.Generated.HlslBytecodeMemoryManager.Instance.Memory;");
+ }
+ }
- // Check that the [numthreads] values match
- writer.WriteLine($"if (threadsX == {info.ThreadsX} && threadsY == {info.ThreadsY} && threadsZ == {info.ThreadsZ})");
+ ///
+ /// Registers a callback to generate an additional type, if needed.
+ ///
+ /// The input instance with gathered shader info.
+ /// The registered callbacks to generate additional types.
+ /// The using directives needed by the generated code.
+ public static void RegisterAdditionalTypeSyntax(
+ ShaderInfo info,
+ ImmutableArrayBuilder> callbacks,
+ ImmutableHashSetBuilder usingDirectives)
+ {
+ // If there is no bytecode, no memory manager is needed
+ if (info.HlslInfo is not HlslBytecodeInfo.Success)
+ {
+ return;
+ }
+
+ usingDirectives.Add("global::System");
+ usingDirectives.Add("global::System.Buffers");
+ usingDirectives.Add("global::System.CodeDom.Compiler");
+ usingDirectives.Add("global::System.Diagnostics");
+ usingDirectives.Add("global::System.Diagnostics.CodeAnalysis");
+ usingDirectives.Add("global::System.Runtime.CompilerServices");
+ usingDirectives.Add("global::System.Runtime.InteropServices");
+
+ // Declare the HlslBytecodeMemoryManager custom memory manager type
+ static void Callback(ShaderInfo info, IndentedTextWriter writer)
+ {
+ writer.WriteLine($$"""/// """);
+ writer.WriteLine($$"""/// implementation to get the HLSL bytecode.""");
+ writer.WriteLine($$"""/// """);
+ writer.WriteGeneratedAttributes(GeneratorName, useFullyQualifiedTypeNames: false);
+ writer.WriteLine($$"""file sealed class HlslBytecodeMemoryManager : MemoryManager""");
using (writer.WriteBlock())
{
- writer.Write("global::System.ReadOnlySpan bytecode = new byte[] { ");
+ // Static singleton instance (to avoid the static constructor, just like with the input types)
+ writer.WriteLine($$"""/// The singleton instance to use.""");
+ writer.WriteLine($$"""public static readonly HlslBytecodeMemoryManager Instance = new();""");
- SyntaxFormattingHelper.WriteByteArrayInitializationExpressions(success.Bytecode.AsSpan(), writer);
+ // RVA field (with the compiled HLSL bytecode, on a single line)
+ writer.WriteLine();
+ writer.WriteLine("/// The RVA data with the HLSL bytecode.");
+ writer.Write("private static ReadOnlySpan Data => new byte[] { ");
+
+ SyntaxFormattingHelper.WriteByteArrayInitializationExpressions(((HlslBytecodeInfo.Success)info.HlslInfo).Bytecode.AsSpan(), writer);
writer.WriteLine(" };");
writer.WriteLine();
- writer.WriteLine("loader.LoadEmbeddedBytecode(bytecode);");
+
+ // Add the remaining members for the memory manager
+ writer.WriteLine("""
+ ///
+ public override unsafe Span GetSpan()
+ {
+ return new(Unsafe.AsPointer(ref MemoryMarshal.GetReference(Data)), Data.Length);
+ }
+
+ ///
+ public override Memory Memory
+ {
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ get => CreateMemory(Data.Length);
+ }
+
+ ///
+ public override unsafe MemoryHandle Pin(int elementIndex)
+ {
+ return new(Unsafe.AsPointer(ref Unsafe.AsRef(in Data[elementIndex])), pinnable: this);
+ }
+
+ ///
+ public override void Unpin()
+ {
+ }
+
+ ///
+ protected override void Dispose(bool disposing)
+ {
+ }
+ """, isMultiline: true);
}
}
+
+ callbacks.Add(Callback);
}
///
diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs
index d62dee1b..d7c41ae6 100644
--- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs
+++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs
@@ -148,10 +148,15 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator
declaredMembers.Add(LoadBytecode.WriteThreadsXSyntax);
declaredMembers.Add(LoadBytecode.WriteThreadsYSyntax);
declaredMembers.Add(LoadBytecode.WriteThreadsZSyntax);
+ declaredMembers.Add(LoadBytecode.WriteHlslBytecodeSyntax);
declaredMembers.Add(LoadDispatchData.WriteSyntax);
declaredMembers.Add(LoadDispatchMetadata.WriteSyntax);
declaredMembers.Add(BuildHlslSource.WriteSyntax);
- declaredMembers.Add(LoadBytecode.WriteSyntax);
+
+ using ImmutableArrayBuilder> additionalTypes = new();
+ using ImmutableHashSetBuilder usingDirectives = new();
+
+ LoadBytecode.RegisterAdditionalTypeSyntax(item, additionalTypes, usingDirectives);
using IndentedTextWriter writer = new();
@@ -161,6 +166,19 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator
baseTypes: ReadOnlySpan.Empty,
memberCallbacks: declaredMembers.WrittenSpan);
+ // Append any additional types as well
+ if (additionalTypes.Count > 0)
+ {
+ writer.WriteLine();
+ writer.WriteLine("namespace ComputeSharp.Generated");
+
+ using (writer.WriteBlock())
+ {
+ writer.WriteSortedUsingDirectives(usingDirectives.AsEnumerable());
+ writer.WriteLineSeparatedMembers(additionalTypes.WrittenSpan, (callback, writer) => callback(item, writer));
+ }
+ }
+
context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.g.cs", writer.ToString());
});
}
diff --git a/src/ComputeSharp/Core/Interfaces/__Internals/IShader.cs b/src/ComputeSharp/Core/Interfaces/__Internals/IShader.cs
index 77e80df5..2a0e128c 100644
--- a/src/ComputeSharp/Core/Interfaces/__Internals/IShader.cs
+++ b/src/ComputeSharp/Core/Interfaces/__Internals/IShader.cs
@@ -25,6 +25,11 @@ public interface IShader
///
int ThreadsZ { get; }
+ ///
+ /// Gets the HLSL bytecode for the current shader.
+ ///
+ ReadOnlyMemory HlslBytecode { get; }
+
///
/// Loads the dispatch data for the shader.
///
@@ -57,18 +62,4 @@ public interface IShader
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("This method is not intended to be called directly by user code")]
void BuildHlslSource(out string hlslSource);
-
- ///
- /// Loads the bytecode for the current shader.
- ///
- /// The type of bytecode loader being used.
- /// The instance to use to load the bytecode.
- /// The number of threads in each thread group for the X axis.
- /// The number of threads in each thread group for the Y axis.
- /// The number of threads in each thread group for the Z axis.
- /// Thrown if the bytecode required runtime compilation and it wasn't supported on the current configuration.
- [EditorBrowsable(EditorBrowsableState.Never)]
- [Obsolete("This method is not intended to be used directly by user code")]
- void LoadBytecode(ref TLoader loader, int threadsX, int threadsY, int threadsZ)
- where TLoader : struct, IBytecodeLoader;
}
\ No newline at end of file