Add and generate IShader.ConstantBufferSize API

このコミットが含まれているのは:
Sergio Pedri committed 2023-10-14 10:20:31 +02:00
1 親 5b8113c2ce
コミット d90266fc61
6ファイル変更
+38 -15

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

@@ -24,7 +24,7 @@ partial class IShaderGenerator
using (writer.WriteBlock())
{
writer.WriteLine($"global::System.Span<uint> span0 = stackalloc uint[{info.Root32BitConstantCount}];");
writer.WriteLine($"global::System.Span<uint> span0 = stackalloc uint[{info.ConstantBufferSizeInBytes}];");
writer.WriteLineIf($"global::System.Span<ulong> 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);
+5 -5
ファイルの表示
@@ -27,16 +27,16 @@ partial class IShaderGenerator
/// </summary>
/// <param name="diagnostics">The collection of produced <see cref="DiagnosticInfo"/> instances.</param>
/// <param name="structDeclarationSymbol">The current shader type being explored.</param>
/// <param name="constantBufferSizeInBytes">The size of the shader constant buffer.</param>
/// <param name="isPixelShaderLike">Whether the compute shader is "pixel shader like", ie. outputting a pixel into a target texture.</param>
/// <param name="resourceCount">The total number of captured resources in the shader.</param>
/// <param name="root32BitConstantCount">The total number of needed 32 bit constants in the shader root signature.</param>
/// <returns>The sequence of <see cref="FieldInfo"/> instances for all captured resources and values.</returns>
public static ImmutableArray<FieldInfo> GetInfo(
ImmutableArrayBuilder<DiagnosticInfo> 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<FieldInfo> 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)
{
@@ -11,6 +11,18 @@ partial class IShaderGenerator
/// <inheritdoc/>
partial class LoadDispatchMetadata
{
/// <summary>
/// Writes the <c>ConstantBufferSize</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 WriteConstantBufferSizeSyntax(ShaderInfo info, IndentedTextWriter writer)
{
writer.WriteLine("/// <inheritdoc/>");
writer.WriteGeneratedAttributes(GeneratorName);
writer.WriteLine($"readonly int global::ComputeSharp.__Internals.IShader.ConstantBufferSize => {info.ConstantBufferSizeInBytes};");
}
/// <summary>
/// Writes the <c>LoadDispatchMetadata</c> method.
/// </summary>
@@ -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<int>(ref global::System.Runtime.CompilerServices.Unsafe.Add(ref r0, 0), {info.Root32BitConstantCount});");
writer.WriteLine($"global::System.Runtime.CompilerServices.Unsafe.WriteUnaligned<int>(ref global::System.Runtime.CompilerServices.Unsafe.Add(ref r0, 0), {info.ConstantBufferSizeInBytes});");
writer.WriteLine($"global::System.Runtime.CompilerServices.Unsafe.WriteUnaligned<bool>(ref global::System.Runtime.CompilerServices.Unsafe.Add(ref r0, 4), {info.IsSamplerUsed.ToString(CultureInfo.InvariantCulture).ToLowerInvariant()});");
// Populate the sequence of resource descriptors
+5 -4
ファイルの表示
@@ -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);
+4 -4
ファイルの表示
@@ -12,10 +12,10 @@ namespace ComputeSharp.SourceGenerators.Models;
/// <param name="ThreadsZ">The thread ids value for the Z axis.</param>
/// <param name="IsPixelShaderLike">Whether the compute shader is "pixel shader like", ie. outputting a pixel into a target texture.</param>
/// <param name="IsSamplerUsed">Whether or not the static sampler is used.</param>
/// /// <param name="ConstantBufferSizeInBytes">The size of the shader constant buffer.</param>
/// <param name="ResourceCount">The total number of captured resources in the shader.</param>
/// <param name="Fields">The description on shader instance fields.</param>
/// <param name="ResourceDescriptors">The sequence of resource descriptors for the shader.</param>
/// <param name="ResourceCount">The total number of captured resources in the shader.</param>
/// <param name="Root32BitConstantCount">The size of the shader root signature, in 32 bit constants.</param>
/// <param name="HlslInfoKey">The key with processed info on the shader.</param>
/// <param name="HlslInfo">The value with processed info on the shader.</param>
/// <param name="Diagnostcs">The discovered diagnostics, if any.</param>
@@ -26,10 +26,10 @@ internal sealed record ShaderInfo(
int ThreadsZ,
bool IsPixelShaderLike,
bool IsSamplerUsed,
int ConstantBufferSizeInBytes,
int ResourceCount,
EquatableArray<FieldInfo> Fields,
EquatableArray<ResourceDescriptor> ResourceDescriptors,
int ResourceCount,
int Root32BitConstantCount,
HlslBytecodeInfoKey HlslInfoKey,
HlslBytecodeInfo HlslInfo,
EquatableArray<DiagnosticInfo> Diagnostcs);
+10
ファイルの表示
@@ -25,6 +25,16 @@ public interface IShader
/// </summary>
int ThreadsZ { get; }
/// <summary>
/// Gets the size in bytes of the constant buffer for the current shader.
/// </summary>
/// <remarks>
/// Constant buffer data is bound to shaders via 32 bit root constants, and loaded before dispatching via
/// <see href="https://learn.microsoft.com/windows/win32/api/d3d12/nf-d3d12-id3d12graphicscommandlist-setcomputeroot32bitconstants"><c>ID3D12GraphicsCommandList::SetComputeRoot32BitConstants</c></see>,
/// so the size must be a multiple of 4.
/// </remarks>
int ConstantBufferSize { get; }
/// <summary>
/// Gets the HLSL source code for the current shader instance.
/// </summary>