Add and generate IShader.HlslBytecode API
このコミットが含まれているのは:
1 親
e909262a97
コミット
c73ff898b8
3ファイル変更
+112
-29
検索条件に一致するファイルはありません
+88
-14
@@ -12,38 +12,112 @@ partial class IShaderGenerator
|
||||
partial class LoadBytecode
|
||||
{
|
||||
/// <summary>
|
||||
/// Writes the <c>LoadBytecode</c> method.
|
||||
/// Writes the <c>HlslBytecode</c> property.
|
||||
/// </summary>
|
||||
/// <param name="info">The input <see cref="ShaderInfo"/> instance with gathered shader info.</param>
|
||||
/// <param name="writer">The <see cref="IndentedTextWriter"/> instance to write into.</param>
|
||||
public static void WriteSyntax(ShaderInfo info, IndentedTextWriter writer)
|
||||
public static void WriteHlslBytecodeSyntax(ShaderInfo info, IndentedTextWriter writer)
|
||||
{
|
||||
writer.WriteLine("/// <inheritdoc/>");
|
||||
writer.WriteGeneratedAttributes(GeneratorName);
|
||||
writer.WriteLine("readonly void global::ComputeSharp.__Internals.IShader.LoadBytecode<TLoader>(ref TLoader loader, int threadsX, int threadsY, int threadsZ)");
|
||||
writer.Write($"readonly global::System.ReadOnlyMemory<byte> global::ComputeSharp.__Internals.IShader.HlslBytecode => ");
|
||||
|
||||
using (writer.WriteBlock())
|
||||
// Just like with the D2D1 generator, only return the MemoryManager<T> 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})");
|
||||
/// <summary>
|
||||
/// Registers a callback to generate an additional type, if needed.
|
||||
/// </summary>
|
||||
/// <param name="info">The input <see cref="ShaderInfo"/> instance with gathered shader info.</param>
|
||||
/// <param name="callbacks">The registered callbacks to generate additional types.</param>
|
||||
/// <param name="usingDirectives">The using directives needed by the generated code.</param>
|
||||
public static void RegisterAdditionalTypeSyntax(
|
||||
ShaderInfo info,
|
||||
ImmutableArrayBuilder<IndentedTextWriter.Callback<ShaderInfo>> callbacks,
|
||||
ImmutableHashSetBuilder<string> 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($$"""/// <summary>""");
|
||||
writer.WriteLine($$"""/// <see cref="MemoryManager{T}"/> implementation to get the HLSL bytecode.""");
|
||||
writer.WriteLine($$"""/// </summary>""");
|
||||
writer.WriteGeneratedAttributes(GeneratorName, useFullyQualifiedTypeNames: false);
|
||||
writer.WriteLine($$"""file sealed class HlslBytecodeMemoryManager : MemoryManager<byte>""");
|
||||
|
||||
using (writer.WriteBlock())
|
||||
{
|
||||
writer.Write("global::System.ReadOnlySpan<byte> bytecode = new byte[] { ");
|
||||
// Static singleton instance (to avoid the static constructor, just like with the input types)
|
||||
writer.WriteLine($$"""/// <summary>The singleton <see cref="HlslBytecodeMemoryManager"/> instance to use.</summary>""");
|
||||
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("/// <summary>The RVA data with the HLSL bytecode.</summary>");
|
||||
writer.Write("private static ReadOnlySpan<byte> 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("""
|
||||
/// <inheritdoc/>
|
||||
public override unsafe Span<byte> GetSpan()
|
||||
{
|
||||
return new(Unsafe.AsPointer(ref MemoryMarshal.GetReference(Data)), Data.Length);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override Memory<byte> Memory
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => CreateMemory(Data.Length);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override unsafe MemoryHandle Pin(int elementIndex)
|
||||
{
|
||||
return new(Unsafe.AsPointer(ref Unsafe.AsRef(in Data[elementIndex])), pinnable: this);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void Unpin()
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
}
|
||||
""", isMultiline: true);
|
||||
}
|
||||
}
|
||||
|
||||
callbacks.Add(Callback);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -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<IndentedTextWriter.Callback<ShaderInfo>> additionalTypes = new();
|
||||
using ImmutableHashSetBuilder<string> usingDirectives = new();
|
||||
|
||||
LoadBytecode.RegisterAdditionalTypeSyntax(item, additionalTypes, usingDirectives);
|
||||
|
||||
using IndentedTextWriter writer = new();
|
||||
|
||||
@@ -161,6 +166,19 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator
|
||||
baseTypes: ReadOnlySpan<string>.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());
|
||||
});
|
||||
}
|
||||
|
||||
@@ -25,6 +25,11 @@ public interface IShader
|
||||
/// </summary>
|
||||
int ThreadsZ { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the HLSL bytecode for the current shader.
|
||||
/// </summary>
|
||||
ReadOnlyMemory<byte> HlslBytecode { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Loads the dispatch data for the shader.
|
||||
/// </summary>
|
||||
@@ -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);
|
||||
|
||||
/// <summary>
|
||||
/// Loads the bytecode for the current shader.
|
||||
/// </summary>
|
||||
/// <typeparam name="TLoader">The type of bytecode loader being used.</typeparam>
|
||||
/// <param name="loader">The <typeparamref name="TLoader"/> instance to use to load the bytecode.</param>
|
||||
/// <param name="threadsX">The number of threads in each thread group for the X axis.</param>
|
||||
/// <param name="threadsY">The number of threads in each thread group for the Y axis.</param>
|
||||
/// <param name="threadsZ">The number of threads in each thread group for the Z axis.</param>
|
||||
/// <exception cref="NotSupportedException">Thrown if the bytecode required runtime compilation and it wasn't supported on the current configuration.</exception>
|
||||
[EditorBrowsable(EditorBrowsableState.Never)]
|
||||
[Obsolete("This method is not intended to be used directly by user code")]
|
||||
void LoadBytecode<TLoader>(ref TLoader loader, int threadsX, int threadsY, int threadsZ)
|
||||
where TLoader : struct, IBytecodeLoader;
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする