Fixed root signature initialization with root constant count
このコミットが含まれているのは:
1 親
1ed6a49210
コミット
47a5101d77
8ファイル変更
+78
-11
検索条件に一致するファイルはありません
@@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace ComputeSharp.__Internals
|
||||
{
|
||||
/// <summary>
|
||||
/// An attribute that contains info on the number of 32 bit constants for a given shader.
|
||||
/// </summary>
|
||||
/// <remarks>This attribute is not meant to be directly used by applications using ComputeSharp.</remarks>
|
||||
[AttributeUsage(AttributeTargets.ReturnValue, AllowMultiple = false)]
|
||||
[EditorBrowsable(EditorBrowsableState.Never)]
|
||||
[Obsolete("This attribute is not intended to be used directly by user code")]
|
||||
public sealed class ComputeRoot32BitConstantsAttribute : Attribute
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="ComputeRoot32BitConstantsAttribute"/> instance with the specified parameters.
|
||||
/// </summary>
|
||||
/// <param name="count">The number of 32 bit constants for the annotated method.</param>
|
||||
public ComputeRoot32BitConstantsAttribute(int count)
|
||||
{
|
||||
Count = count;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the number of 32 bit constants for the annotated method.
|
||||
/// </summary>
|
||||
internal int Count { get; }
|
||||
}
|
||||
}
|
||||
@@ -25,7 +25,7 @@ namespace ComputeSharp.__Internals
|
||||
/// <param name="executeMethod">The source code for the <see cref="IComputeShader.Execute"/> method.</param>
|
||||
/// <param name="methods">The collection of processed methods.</param>
|
||||
/// <param name="constants">The collection of discovered constants.</param>
|
||||
/// <param name="sharedBuffers">The collection of group shared buffers</param>
|
||||
/// <param name="sharedBuffers">The collection of group shared buffers.</param>
|
||||
public IComputeShaderSourceAttribute(
|
||||
string shaderTypeName,
|
||||
string[] types,
|
||||
|
||||
@@ -15,11 +15,13 @@ namespace ComputeSharp.Shaders.Extensions
|
||||
/// Creates a new <see cref="ID3D12RootSignature"/> for a given device.
|
||||
/// </summary>
|
||||
/// <param name="d3D12Device">The target <see cref="ID3D12Device"/> to use to create the root signature.</param>
|
||||
/// <param name="d3D12Root32BitConstantsCount">The number of 32 bit root constants to load.</param>
|
||||
/// <param name="d3D12DescriptorRanges1">The input descriptor ranges for the signature to create.</param>
|
||||
/// <returns>A pointer to the newly allocated <see cref="ID3D12RootSignature"/> instance.</returns>
|
||||
/// <exception cref="Exception">Thrown when the creation of the root signature fails.</exception>
|
||||
public static ComPtr<ID3D12RootSignature> CreateRootSignature(
|
||||
this ref ID3D12Device d3D12Device,
|
||||
int d3D12Root32BitConstantsCount,
|
||||
ReadOnlySpan<D3D12_DESCRIPTOR_RANGE1> d3D12DescriptorRanges1)
|
||||
{
|
||||
using ComPtr<ID3DBlob> d3D3Blob = default;
|
||||
@@ -32,7 +34,7 @@ namespace ComputeSharp.Shaders.Extensions
|
||||
|
||||
// The initial constant buffer is initialized with 32 bit root constants.
|
||||
// This avoids having to manage an extra buffer for each shader dispatch.
|
||||
D3D12_ROOT_PARAMETER1.InitAsConstants(out *d3D12RootParameters1, 3, 0); // TODO: fix length
|
||||
D3D12_ROOT_PARAMETER1.InitAsConstants(out *d3D12RootParameters1, (uint)d3D12Root32BitConstantsCount, 0);
|
||||
|
||||
// Pack each descriptor range into a root parameter with that single range as content
|
||||
for (int i = 0; i < d3D12DescriptorRanges1.Length; i++)
|
||||
|
||||
@@ -155,7 +155,7 @@ namespace ComputeSharp.Shaders
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
private static unsafe void CreatePipelineData(GraphicsDevice device, in CachedShader<T> shaderData, out PipelineData pipelineData)
|
||||
{
|
||||
using ComPtr<ID3D12RootSignature> d3D12RootSignature = device.D3D12Device->CreateRootSignature(shaderData.Loader.D3D12DescriptorRanges1);
|
||||
using ComPtr<ID3D12RootSignature> d3D12RootSignature = device.D3D12Device->CreateRootSignature(shaderData.Loader.D3D12Root32BitConstantsCount, shaderData.Loader.D3D12DescriptorRanges1);
|
||||
using ComPtr<ID3D12PipelineState> d3D12PipelineState = device.D3D12Device->CreateComputePipelineState(d3D12RootSignature.Get(), shaderData.Bytecode.D3D12ShaderBytecode);
|
||||
|
||||
pipelineData = new PipelineData(d3D12RootSignature.Move(), d3D12PipelineState.Move());
|
||||
|
||||
@@ -4,6 +4,7 @@ using System.Collections.Generic;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using ComputeSharp.__Internals;
|
||||
using ComputeSharp.Shaders.Translation.Models;
|
||||
using TerraFX.Interop;
|
||||
#if NET5_0
|
||||
@@ -12,6 +13,8 @@ using MemoryMarshal = System.Runtime.InteropServices.MemoryMarshal;
|
||||
using MemoryMarshal = Polyfills.MemoryMarshal;
|
||||
#endif
|
||||
|
||||
#pragma warning disable CS0618
|
||||
|
||||
namespace ComputeSharp.Shaders.Translation
|
||||
{
|
||||
/// <inheritdoc cref="ShaderLoader{T}"/>
|
||||
@@ -84,6 +87,10 @@ namespace ComputeSharp.Shaders.Translation
|
||||
Type type = typeof(T).Assembly.GetType("ComputeSharp.__Internals.DispatchDataLoader")!;
|
||||
MethodInfo method = type.GetMethod("LoadDispatchData", argumentTypes)!;
|
||||
|
||||
// Extract the computed count of 32 bit root constants to load
|
||||
D3D12Root32BitConstantsCount = ((ComputeRoot32BitConstantsAttribute)method.ReturnTypeCustomAttributes.GetCustomAttributes(false)[0]).Count;
|
||||
|
||||
// Create a delegate from the generated shader data loader
|
||||
this.dispatchDataLoader = method.CreateDelegate<DispatchDataLoader>();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,6 +78,11 @@ namespace ComputeSharp.Shaders.Translation
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the number of 32 bit constants in the root signature for the shader.
|
||||
/// </summary>
|
||||
public int D3D12Root32BitConstantsCount { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="Span{T}"/> with the loaded <see cref="D3D12_DESCRIPTOR_RANGE1"/> items for the shader.
|
||||
/// </summary>
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
<Compile Include="..\ComputeSharp.Core\Attributes\AutoConstructorAttribute.cs" Link="Core\Attributes\AutoConstructorAttribute.cs" />
|
||||
<Compile Include="..\ComputeSharp.Core\Attributes\GroupSharedAttribute.cs" Link="Core\Attributes\GroupSharedAttribute.cs" />
|
||||
<Compile Include="..\ComputeSharp.Core\Attributes\ShaderMethodAttribute.cs" Link="Core\Attributes\ShaderMethodAttribute.cs" />
|
||||
<Compile Include="..\ComputeSharp.Core\Attributes\__Internals\ComputeRoot32BitConstantsAttribute.cs" Link="Core\Attributes\__Internals\ComputeRoot32BitConstantsAttribute.cs" />
|
||||
<Compile Include="..\ComputeSharp.Core\Attributes\__Internals\IComputeShaderSourceAttribute.cs" Link="Core\Attributes\__Internals\IComputeShaderSourceAttribute.cs" />
|
||||
<Compile Include="..\ComputeSharp.Core\Attributes\__Internals\ShaderMethodSourceAttribute.cs" Link="Core\Attributes\__Internals\ShaderMethodSourceAttribute.cs" />
|
||||
<Compile Include="..\ComputeSharp.Core\Dispatch\GroupIds.cs" Link="Core\Dispatch\GroupIds.cs" />
|
||||
|
||||
@@ -4,6 +4,7 @@ using System.Collections.Immutable;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using ComputeSharp.__Internals;
|
||||
using ComputeSharp.Core.Helpers;
|
||||
using ComputeSharp.SourceGenerators.Extensions;
|
||||
using ComputeSharp.SourceGenerators.Mappings;
|
||||
@@ -13,6 +14,8 @@ using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
using Microsoft.CodeAnalysis.Text;
|
||||
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
|
||||
|
||||
#pragma warning disable CS0618
|
||||
|
||||
namespace ComputeSharp.SourceGenerators
|
||||
{
|
||||
[Generator]
|
||||
@@ -54,7 +57,7 @@ namespace ComputeSharp.SourceGenerators
|
||||
if (!structDeclarationSymbol.Interfaces.Any(static interfaceSymbol => interfaceSymbol.Name == nameof(IComputeShader))) continue;
|
||||
|
||||
TypeSyntax shaderType = ParseTypeName(structDeclarationSymbol.ToDisplayString());
|
||||
BlockSyntax block = Block(GetDispatchDataLoadingStatements(structDeclarationSymbol));
|
||||
BlockSyntax block = Block(GetDispatchDataLoadingStatements(structDeclarationSymbol, out int root32BitConstantsCount));
|
||||
|
||||
// Create a static method to create the combined hashcode for a given shader type.
|
||||
// This code takes a block syntax and produces a compilation unit as follows:
|
||||
@@ -68,6 +71,7 @@ namespace ComputeSharp.SourceGenerators
|
||||
// {
|
||||
// [System.ComponentModel.EditorBrowsable(EditorBrowsableState.Never)]
|
||||
// [System.Obsolete("This method is not intended to be called directly by user code")]
|
||||
// [return: ComputeRoot32BitConstants(42)]
|
||||
// public static int LoadDispatchData(GraphicsDevice device, in ShaderType shader, ref ulong r0, ref byte r1)
|
||||
// {
|
||||
// ...
|
||||
@@ -94,7 +98,13 @@ namespace ComputeSharp.SourceGenerators
|
||||
Attribute(IdentifierName("Obsolete")).AddArgumentListArguments(
|
||||
AttributeArgument(LiteralExpression(
|
||||
SyntaxKind.StringLiteralExpression,
|
||||
Literal("This method is not intended to be called directly by user code"))))))).AddModifiers(
|
||||
Literal("This method is not intended to be called directly by user code")))))),
|
||||
AttributeList(SingletonSeparatedList(
|
||||
Attribute(IdentifierName(nameof(ComputeRoot32BitConstantsAttribute).Replace("Attribute", "")))
|
||||
.AddArgumentListArguments(AttributeArgument(LiteralExpression(
|
||||
SyntaxKind.NumericLiteralExpression,
|
||||
Literal(root32BitConstantsCount))))))
|
||||
.WithTarget(AttributeTargetSpecifier(Token(SyntaxKind.ReturnKeyword)))).AddModifiers(
|
||||
Token(SyntaxKind.PublicKeyword),
|
||||
Token(SyntaxKind.StaticKeyword)).AddParameterListParameters(
|
||||
Parameter(Identifier("device")).WithType(IdentifierName("ComputeSharp.GraphicsDevice")),
|
||||
@@ -118,10 +128,13 @@ namespace ComputeSharp.SourceGenerators
|
||||
/// Gets a sequence of statements to load the dispatch data for a given shader
|
||||
/// </summary>
|
||||
/// <param name="structDeclarationSymbol">The input <see cref="INamedTypeSymbol"/> instance to process.</param>
|
||||
/// <param name="count">The total number of 32 bit root constants to load.</param>
|
||||
/// <returns>The sequence of <see cref="StatementSyntax"/> instances to load shader dispatch data.</returns>
|
||||
[Pure]
|
||||
private static IEnumerable<StatementSyntax> GetDispatchDataLoadingStatements(INamedTypeSymbol structDeclarationSymbol)
|
||||
private static IEnumerable<StatementSyntax> GetDispatchDataLoadingStatements(INamedTypeSymbol structDeclarationSymbol, out int count)
|
||||
{
|
||||
List<StatementSyntax> statements = new();
|
||||
|
||||
int
|
||||
resourceOffset = 0,
|
||||
rawDataOffset = sizeof(int) * 3;
|
||||
@@ -135,11 +148,11 @@ namespace ComputeSharp.SourceGenerators
|
||||
|
||||
if (HlslKnownTypes.IsTypedResourceType(typeName))
|
||||
{
|
||||
yield return ExpressionStatement(
|
||||
statements.Add(ExpressionStatement(
|
||||
AssignmentExpression(
|
||||
SyntaxKind.SimpleAssignmentExpression,
|
||||
ParseExpression($"Unsafe.Add(ref r0, {resourceOffset++})"),
|
||||
ParseExpression($"GraphicsResourceHelper.ValidateAndGetGpuDescriptorHandle(shader.{fieldSymbol.Name}, device)")));
|
||||
ParseExpression($"GraphicsResourceHelper.ValidateAndGetGpuDescriptorHandle(shader.{fieldSymbol.Name}, device)"))));
|
||||
}
|
||||
else if (HlslKnownTypes.IsScalarOrVectorType(typeName))
|
||||
{
|
||||
@@ -156,14 +169,24 @@ namespace ComputeSharp.SourceGenerators
|
||||
fieldSize,
|
||||
16);
|
||||
|
||||
yield return ExpressionStatement(
|
||||
ParseExpression($"Unsafe.WriteUnaligned(ref Unsafe.Add(ref r1, {rawDataOffset}), shader.{fieldSymbol.Name})"));
|
||||
statements.Add(ExpressionStatement(
|
||||
ParseExpression($"Unsafe.WriteUnaligned(ref Unsafe.Add(ref r1, {rawDataOffset}), shader.{fieldSymbol.Name})")));
|
||||
|
||||
rawDataOffset += fieldSize;
|
||||
}
|
||||
}
|
||||
|
||||
yield return ReturnStatement(LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(rawDataOffset)));
|
||||
// After all the captured fields have been processed, ansure the reported byte size for
|
||||
// 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.
|
||||
rawDataOffset = AlignmentHelper.Pad(rawDataOffset, sizeof(int));
|
||||
|
||||
statements.Add(ReturnStatement(LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(rawDataOffset))));
|
||||
|
||||
count = rawDataOffset / sizeof(int);
|
||||
|
||||
return statements;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする