From d90266fc61821333f552426089495f3f3df88b83 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 14 Oct 2023 10:20:31 +0200 Subject: [PATCH] Add and generate IShader.ConstantBufferSize API --- ...enerator.CreateLoadDispatchDataMethod.Syntax.cs | 2 +- ...ShaderGenerator.CreateLoadDispatchDataMethod.cs | 10 +++++----- ...ator.CreateLoadDispatchMetadataMethod.Syntax.cs | 14 +++++++++++++- .../IShaderGenerator.cs | 9 +++++---- .../Models/ShaderInfo.cs | 8 ++++---- .../Core/Interfaces/__Internals/IShader.cs | 10 ++++++++++ 6 files changed, 38 insertions(+), 15 deletions(-) diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchDataMethod.Syntax.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchDataMethod.Syntax.cs index 6c272144..a6a3cd5b 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchDataMethod.Syntax.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchDataMethod.Syntax.cs @@ -24,7 +24,7 @@ partial class IShaderGenerator using (writer.WriteBlock()) { - writer.WriteLine($"global::System.Span span0 = stackalloc uint[{info.Root32BitConstantCount}];"); + writer.WriteLine($"global::System.Span span0 = stackalloc uint[{info.ConstantBufferSizeInBytes}];"); writer.WriteLineIf($"global::System.Span span1 = stackalloc ulong[{info.ResourceCount}];", info.ResourceCount > 0); writer.WriteLine("ref uint r0 = ref span0[0];"); writer.WriteLineIf("ref ulong r1 = ref span1[0];", info.ResourceCount > 0); diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchDataMethod.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchDataMethod.cs index 0636ebc0..a026863c 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchDataMethod.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchDataMethod.cs @@ -27,16 +27,16 @@ partial class IShaderGenerator /// /// The collection of produced instances. /// The current shader type being explored. + /// The size of the shader constant buffer. /// Whether the compute shader is "pixel shader like", ie. outputting a pixel into a target texture. /// The total number of captured resources in the shader. - /// The total number of needed 32 bit constants in the shader root signature. /// The sequence of instances for all captured resources and values. public static ImmutableArray GetInfo( ImmutableArrayBuilder diagnostics, ITypeSymbol structDeclarationSymbol, bool isPixelShaderLike, - out int resourceCount, - out int root32BitConstantCount) + out int constantBufferSizeInBytes, + out int resourceCount) { // Helper method that uses boxes instead of ref-s (illegal in enumerators) static IEnumerable GetCapturedFieldInfos( @@ -108,7 +108,7 @@ partial class IShaderGenerator // the local variables is padded to a multiple of a 32 bit value. This is necessary to // enable loading all the dispatch data after reinterpreting it to a sequence of values // of size 32 bits (via SetComputeRoot32BitConstants), without reading out of bounds. - root32BitConstantCount = AlignmentHelper.Pad(rawDataOffsetAsBox.Value, sizeof(int)) / sizeof(int); + constantBufferSizeInBytes = AlignmentHelper.Pad(rawDataOffsetAsBox.Value, sizeof(int)) / sizeof(int); // A shader root signature has a maximum size of 64 DWORDs, so 256 bytes. // Loaded values in the root signature have the following costs: @@ -118,7 +118,7 @@ partial class IShaderGenerator // So here we check whether the current signature respects that constraint, // and emit a build error otherwise. For more info on this, see the docs here: // https://docs.microsoft.com/windows/win32/direct3d12/root-signature-limits. - int rootSignatureDwordSize = root32BitConstantCount + resourceCount; + int rootSignatureDwordSize = constantBufferSizeInBytes + resourceCount; if (rootSignatureDwordSize > 64) { diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchMetadataMethod.Syntax.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchMetadataMethod.Syntax.cs index 2faed681..22881355 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchMetadataMethod.Syntax.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchMetadataMethod.Syntax.cs @@ -11,6 +11,18 @@ partial class IShaderGenerator /// partial class LoadDispatchMetadata { + /// + /// Writes the ConstantBufferSize property. + /// + /// The input instance with gathered shader info. + /// The instance to write into. + public static void WriteConstantBufferSizeSyntax(ShaderInfo info, IndentedTextWriter writer) + { + writer.WriteLine("/// "); + writer.WriteGeneratedAttributes(GeneratorName); + writer.WriteLine($"readonly int global::ComputeSharp.__Internals.IShader.ConstantBufferSize => {info.ConstantBufferSizeInBytes};"); + } + /// /// Writes the LoadDispatchMetadata method. /// @@ -34,7 +46,7 @@ partial class IShaderGenerator writer.WriteLine("ref global::ComputeSharp.__Internals.ResourceDescriptor r1 = ref span1[0];"); // Write the serialized shader metadata - writer.WriteLine($"global::System.Runtime.CompilerServices.Unsafe.WriteUnaligned(ref global::System.Runtime.CompilerServices.Unsafe.Add(ref r0, 0), {info.Root32BitConstantCount});"); + writer.WriteLine($"global::System.Runtime.CompilerServices.Unsafe.WriteUnaligned(ref global::System.Runtime.CompilerServices.Unsafe.Add(ref r0, 0), {info.ConstantBufferSizeInBytes});"); writer.WriteLine($"global::System.Runtime.CompilerServices.Unsafe.WriteUnaligned(ref global::System.Runtime.CompilerServices.Unsafe.Add(ref r0, 4), {info.IsSamplerUsed.ToString(CultureInfo.InvariantCulture).ToLowerInvariant()});"); // Populate the sequence of resource descriptors diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs index d7c41ae6..49144cbd 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs @@ -59,8 +59,8 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator diagnostics, typeSymbol, isPixelShaderLike, - out int resourceCount, - out int root32BitConstantCount); + out int root32BitConstantCount, + out int resourceCount); token.ThrowIfCancellationRequested(); @@ -123,10 +123,10 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator ThreadsZ: threadsZ, IsPixelShaderLike: isPixelShaderLike, IsSamplerUsed: isSamplerUsed, + ConstantBufferSizeInBytes: root32BitConstantCount, + ResourceCount: resourceCount, Fields: fieldInfos, ResourceDescriptors: resourceDescriptors, - ResourceCount: resourceCount, - Root32BitConstantCount: root32BitConstantCount, HlslInfoKey: hlslInfoKey, HlslInfo: hlslInfo, Diagnostcs: diagnostics.ToImmutable()); @@ -148,6 +148,7 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator declaredMembers.Add(LoadBytecode.WriteThreadsXSyntax); declaredMembers.Add(LoadBytecode.WriteThreadsYSyntax); declaredMembers.Add(LoadBytecode.WriteThreadsZSyntax); + declaredMembers.Add(LoadDispatchMetadata.WriteConstantBufferSizeSyntax); declaredMembers.Add(LoadBytecode.WriteHlslBytecodeSyntax); declaredMembers.Add(LoadDispatchData.WriteSyntax); declaredMembers.Add(LoadDispatchMetadata.WriteSyntax); diff --git a/src/ComputeSharp.SourceGenerators/Models/ShaderInfo.cs b/src/ComputeSharp.SourceGenerators/Models/ShaderInfo.cs index cb51e35d..70370257 100644 --- a/src/ComputeSharp.SourceGenerators/Models/ShaderInfo.cs +++ b/src/ComputeSharp.SourceGenerators/Models/ShaderInfo.cs @@ -12,10 +12,10 @@ namespace ComputeSharp.SourceGenerators.Models; /// The thread ids value for the Z axis. /// Whether the compute shader is "pixel shader like", ie. outputting a pixel into a target texture. /// Whether or not the static sampler is used. +/// /// The size of the shader constant buffer. +/// The total number of captured resources in the shader. /// The description on shader instance fields. /// The sequence of resource descriptors for the shader. -/// The total number of captured resources in the shader. -/// The size of the shader root signature, in 32 bit constants. /// The key with processed info on the shader. /// The value with processed info on the shader. /// The discovered diagnostics, if any. @@ -26,10 +26,10 @@ internal sealed record ShaderInfo( int ThreadsZ, bool IsPixelShaderLike, bool IsSamplerUsed, + int ConstantBufferSizeInBytes, + int ResourceCount, EquatableArray Fields, EquatableArray ResourceDescriptors, - int ResourceCount, - int Root32BitConstantCount, HlslBytecodeInfoKey HlslInfoKey, HlslBytecodeInfo HlslInfo, EquatableArray Diagnostcs); \ No newline at end of file diff --git a/src/ComputeSharp/Core/Interfaces/__Internals/IShader.cs b/src/ComputeSharp/Core/Interfaces/__Internals/IShader.cs index dc110632..b1fa6a4e 100644 --- a/src/ComputeSharp/Core/Interfaces/__Internals/IShader.cs +++ b/src/ComputeSharp/Core/Interfaces/__Internals/IShader.cs @@ -25,6 +25,16 @@ public interface IShader /// int ThreadsZ { get; } + /// + /// Gets the size in bytes of the constant buffer for the current shader. + /// + /// + /// Constant buffer data is bound to shaders via 32 bit root constants, and loaded before dispatching via + /// ID3D12GraphicsCommandList::SetComputeRoot32BitConstants, + /// so the size must be a multiple of 4. + /// + int ConstantBufferSize { get; } + /// /// Gets the HLSL source code for the current shader instance. ///