Merge pull request #621 from Sergio0694/dev/d3d12-generator-constant-buffer

Shader FieldInfo model across D2D1/D3D12 generators
このコミットが含まれているのは:
Sergio Pedri authored and GitHub committed 2023-10-17 01:54:53 +02:00
コミット 5cbc583a19
12ファイル変更
+77 -98

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

@@ -2,6 +2,7 @@ using ComputeSharp.D2D1.SourceGenerators.Models;
using ComputeSharp.SourceGeneration.Extensions;
using ComputeSharp.SourceGeneration.Helpers;
using ComputeSharp.SourceGeneration.Mappings;
using ComputeSharp.SourceGeneration.Models;
namespace ComputeSharp.D2D1.SourceGenerators;
@@ -2,6 +2,7 @@ using ComputeSharp.D2D1.SourceGenerators.Models;
using ComputeSharp.SourceGeneration.Extensions;
using ComputeSharp.SourceGeneration.Helpers;
using ComputeSharp.SourceGeneration.Mappings;
using ComputeSharp.SourceGeneration.Models;
namespace ComputeSharp.D2D1.SourceGenerators;
@@ -4,7 +4,6 @@ using System.Collections.Immutable;
using System.Linq;
using System.Runtime.CompilerServices;
using ComputeSharp.Core.Helpers;
using ComputeSharp.D2D1.SourceGenerators.Models;
using ComputeSharp.SourceGeneration.Extensions;
using ComputeSharp.SourceGeneration.Helpers;
using ComputeSharp.SourceGeneration.Mappings;
@@ -21,6 +21,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Mappings\HlslKnownProperties.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Mappings\HlslKnownSizes.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Mappings\HlslKnownTypes.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Models\FieldInfo.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Models\HlslBytecodeInfo.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SyntaxRewriters\HlslSourceRewriter.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SyntaxRewriters\HlslSourceRewriter.Diagnostics.cs" />
@@ -1,25 +1,27 @@
using ComputeSharp.SourceGeneration.Helpers;
namespace ComputeSharp.D2D1.SourceGenerators.Models;
namespace ComputeSharp.SourceGeneration.Models;
/// <summary>
/// A base type for a model describing a given captured field in a shader.
/// </summary>
internal abstract record FieldInfo
/// <param name="FieldPath">The path of the field with respect to the shader instance.</param>
/// <param name="TypeName">The full metadata name for the primitive type.</param>
internal abstract record FieldInfo(EquatableArray<string> FieldPath, string TypeName)
{
/// <summary>
/// A captured primitive value (either a scalar, a vector, or a linear matrix).
/// </summary>
/// <param name="FieldPath">The path of the field with respect to the shader instance.</param>
/// <param name="TypeName">The full metadata name for the primitive type.</param>
/// <param name="FieldPath"><inheritdoc cref="FieldInfo(EquatableArray{string}, string)" path="/param[@name='FieldPath']/node()"/></param>
/// <param name="TypeName"><inheritdoc cref="FieldInfo(EquatableArray{string}, string)" path="/param[@name='TypeName']/node()"/></param>
/// <param name="Offset">The byte offset of the value within the root signature.</param>
public sealed record Primitive(EquatableArray<string> FieldPath, string TypeName, int Offset) : FieldInfo;
public sealed record Primitive(EquatableArray<string> FieldPath, string TypeName, int Offset) : FieldInfo(FieldPath, TypeName);
/// <summary>
/// A captured non linear matrix value.
/// </summary>
/// <param name="FieldPath">The path of the field with respect to the shader instance.</param>
/// <param name="TypeName">The full metadata name for the matrix type.</param>
/// <param name="FieldPath"><inheritdoc cref="FieldInfo(EquatableArray{string}, string)" path="/param[@name='FieldPath']/node()"/></param>
/// <param name="TypeName"><inheritdoc cref="FieldInfo(EquatableArray{string}, string)" path="/param[@name='TypeName']/node()"/></param>
/// <param name="ElementName">The name of each element in the matrix type.</param>
/// <param name="Rows">The number of rows in the matrix.</param>
/// <param name="Columns">The number of columns in the matrix.</param>
@@ -30,5 +32,5 @@ internal abstract record FieldInfo
string ElementName,
int Rows,
int Columns,
EquatableArray<int> Offsets) : FieldInfo;
EquatableArray<int> Offsets) : FieldInfo(FieldPath, TypeName);
}
@@ -1,5 +1,6 @@
using ComputeSharp.SourceGeneration.Extensions;
using ComputeSharp.SourceGeneration.Helpers;
using ComputeSharp.SourceGeneration.Models;
using ComputeSharp.SourceGenerators.Models;
namespace ComputeSharp.SourceGenerators;
@@ -103,12 +104,9 @@ partial class ComputeShaderDescriptorGenerator
using (writer.WriteBlock())
{
// Generate loading statements for each captured resource
foreach (FieldInfo fieldInfo in info.Fields)
foreach (ResourceInfo resource in info.Resources)
{
if (fieldInfo is FieldInfo.Resource resource)
{
writer.WriteLine($"loader.LoadGraphicsResource(shader.{resource.FieldName}, {resource.Offset});");
}
writer.WriteLine($"loader.LoadGraphicsResource(shader.{resource.FieldName}, {resource.Offset});");
}
}
}
+40 -35
ファイルの表示
@@ -1,8 +1,5 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Runtime.CompilerServices;
using ComputeSharp.Core.Helpers;
using ComputeSharp.SourceGeneration.Extensions;
using ComputeSharp.SourceGeneration.Helpers;
@@ -30,79 +27,89 @@ partial class ComputeShaderDescriptorGenerator
/// <param name="isPixelShaderLike">Whether the compute shader is "pixel shader like", ie. outputting a pixel into a target texture.</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>
/// <returns>The sequence of <see cref="FieldInfo"/> instances for all captured resources and values.</returns>
public static ImmutableArray<FieldInfo> GetInfo(
/// <param name="fields">The sequence of <see cref="FieldInfo"/> instances for all captured values.</param>
/// <param name="resources">The sequence of <see cref="ResourceInfo"/> instances for all captured resources.</param>
public static void GetInfo(
ImmutableArrayBuilder<DiagnosticInfo> diagnostics,
ITypeSymbol structDeclarationSymbol,
bool isPixelShaderLike,
out int constantBufferSizeInBytes,
out int resourceCount)
out int resourceCount,
out ImmutableArray<FieldInfo> fields,
out ImmutableArray<ResourceInfo> resources)
{
// Helper method that uses boxes instead of ref-s (illegal in enumerators)
static IEnumerable<FieldInfo> GetCapturedFieldInfos(
static void GetInfo(
ITypeSymbol currentTypeSymbol,
ImmutableArray<string> fieldPath,
StrongBox<int> resourceOffset,
StrongBox<int> rawDataOffset)
ref int resourceOffset,
ref int rawDataOffset,
ImmutableArrayBuilder<FieldInfo> fields,
ImmutableArrayBuilder<ResourceInfo> resources)
{
bool isFirstField = true;
foreach (
IFieldSymbol fieldSymbol in
from fieldSymbol in currentTypeSymbol.GetMembers().OfType<IFieldSymbol>()
where fieldSymbol is { Type: INamedTypeSymbol { IsStatic: false }, IsConst: false, IsStatic: false, IsFixedSizeBuffer: false, IsImplicitlyDeclared: false }
select fieldSymbol)
foreach (ISymbol memberSymbol in currentTypeSymbol.GetMembers())
{
// Only process fields
if (memberSymbol is not IFieldSymbol { Type: INamedTypeSymbol { IsStatic: false }, IsConst: false, IsStatic: false, IsFixedSizeBuffer: false, IsImplicitlyDeclared: false } fieldSymbol)
{
continue;
}
string fieldName = fieldSymbol.Name;
string typeName = fieldSymbol.Type.GetFullyQualifiedMetadataName();
// The first item in each nested struct needs to be aligned to 16 bytes
if (isFirstField && fieldPath.Length > 0)
{
rawDataOffset.Value = AlignmentHelper.Pad(rawDataOffset.Value, 16);
rawDataOffset = AlignmentHelper.Pad(rawDataOffset, 16);
isFirstField = false;
}
if (HlslKnownTypes.IsTypedResourceType(typeName))
{
yield return new FieldInfo.Resource(fieldName, typeName, resourceOffset.Value++);
resources.Add(new ResourceInfo(fieldName, typeName, resourceOffset++));
}
else if (HlslKnownTypes.IsKnownHlslType(typeName))
{
yield return GetHlslKnownTypeFieldInfo(fieldPath.Add(fieldName), typeName, ref rawDataOffset.Value);
fields.Add(GetHlslKnownTypeFieldInfo(fieldPath.Add(fieldName), typeName, ref rawDataOffset));
}
else if (fieldSymbol.Type.IsUnmanagedType)
{
// Custom struct type defined by the user
foreach (FieldInfo fieldInfo in GetCapturedFieldInfos(fieldSymbol.Type, fieldPath.Add(fieldName), resourceOffset, rawDataOffset))
{
yield return fieldInfo;
}
GetInfo(fieldSymbol.Type, fieldPath.Add(fieldName), ref resourceOffset, ref rawDataOffset, fields, resources);
}
}
}
// Setup the resource and byte offsets for tracking. Pixel shaders have only two
// implicitly captured int values, as they're always dispatched over a 2D texture.
int initialRawDataOffset = sizeof(int) * (isPixelShaderLike ? 2 : 3);
StrongBox<int> resourceOffsetAsBox = new();
StrongBox<int> rawDataOffsetAsBox = new(initialRawDataOffset);
int rawDataOffset = sizeof(int) * (isPixelShaderLike ? 2 : 3);
int resourceOffset = 0;
// Traverse all shader fields and gather info, and update the tracking offsets
ImmutableArray<FieldInfo> fieldInfos = GetCapturedFieldInfos(
structDeclarationSymbol,
ImmutableArray<string>.Empty,
resourceOffsetAsBox,
rawDataOffsetAsBox).ToImmutableArray();
using (ImmutableArrayBuilder<FieldInfo> fieldBuilder = new())
using (ImmutableArrayBuilder<ResourceInfo> resourceBuilder = new())
{
// Traverse all shader fields and gather info, and update the tracking offsets
GetInfo(
structDeclarationSymbol,
ImmutableArray<string>.Empty,
ref resourceOffset,
ref rawDataOffset,
fieldBuilder,
resourceBuilder);
resourceCount = resourceOffsetAsBox.Value;
resourceCount = resourceOffset;
fields = fieldBuilder.ToImmutable();
resources = resourceBuilder.ToImmutable();
}
// After all the captured fields have been processed, ensure 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.
constantBufferSizeInBytes = AlignmentHelper.Pad(rawDataOffsetAsBox.Value, sizeof(int));
constantBufferSizeInBytes = AlignmentHelper.Pad(rawDataOffset, 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,8 +125,6 @@ partial class ComputeShaderDescriptorGenerator
{
diagnostics.Add(ShaderDispatchDataSizeExceeded, structDeclarationSymbol, structDeclarationSymbol);
}
return fieldInfos;
}
/// <summary>
@@ -18,12 +18,12 @@ partial class ComputeShaderDescriptorGenerator
/// </summary>
/// <param name="isImplicitTextureUsed">Indicates whether the current shader uses an implicit texture.</param>
/// <param name="isSamplerUsed">Whether the static sampler is used by the shader.</param>
/// <param name="capturedFields">The sequence of captured fields for the shader.</param>
/// <param name="resources">The sequence of captured resources for the shader.</param>
/// <returns>The metadata info for the shader.</returns>
public static ImmutableArray<ResourceDescriptor> GetInfo(
bool isImplicitTextureUsed,
bool isSamplerUsed,
ImmutableArray<FieldInfo> capturedFields)
ImmutableArray<ResourceInfo> resources)
{
using ImmutableArrayBuilder<ResourceDescriptor> descriptors = new();
@@ -38,7 +38,7 @@ partial class ComputeShaderDescriptorGenerator
}
// Populate the sequence of resource descriptors
foreach (FieldInfo.Resource resource in capturedFields.OfType<FieldInfo.Resource>())
foreach (ResourceInfo resource in resources)
{
if (HlslKnownTypes.IsConstantBufferType(resource.TypeName))
{
+7 -4
ファイルの表示
@@ -51,13 +51,15 @@ public sealed partial class ComputeShaderDescriptorGenerator : IIncrementalGener
using ImmutableArrayBuilder<DiagnosticInfo> diagnostics = new();
// Dispatch data loading info
ImmutableArray<FieldInfo> fieldInfos = DispatchDataLoading.GetInfo(
// Get the fields info
DispatchDataLoading.GetInfo(
diagnostics,
typeSymbol,
isPixelShaderLike,
out int constantBufferSizeInBytes,
out int resourceCount);
out int resourceCount,
out ImmutableArray<FieldInfo> fieldInfos,
out ImmutableArray<ResourceInfo> resourceInfo);
token.ThrowIfCancellationRequested();
@@ -91,7 +93,7 @@ public sealed partial class ComputeShaderDescriptorGenerator : IIncrementalGener
ImmutableArray<ResourceDescriptor> resourceDescriptors = ResourceDescriptorRanges.GetInfo(
isImplicitTextureUsed,
isSamplerUsed,
fieldInfos);
resourceInfo);
token.ThrowIfCancellationRequested();
@@ -122,6 +124,7 @@ public sealed partial class ComputeShaderDescriptorGenerator : IIncrementalGener
ConstantBufferSizeInBytes: constantBufferSizeInBytes,
ResourceCount: resourceCount,
Fields: fieldInfos,
Resources: resourceInfo,
ResourceDescriptors: resourceDescriptors,
HlslInfoKey: hlslInfoKey,
HlslInfo: hlslInfo,
-42
ファイルの表示
@@ -1,42 +0,0 @@
using ComputeSharp.SourceGeneration.Helpers;
namespace ComputeSharp.SourceGenerators.Models;
/// <summary>
/// A base type for a model describing a given captured field in a shader.
/// </summary>
internal abstract record FieldInfo
{
/// <summary>
/// A captured resource (either a buffer or a texture).
/// </summary>
/// <param name="FieldName">The name of the resource field.</param>
/// <param name="TypeName">The full metadata name for the resource type.</param>
/// <param name="Offset">The offset for the resource within the root signature.</param>
public sealed record Resource(string FieldName, string TypeName, int Offset) : FieldInfo;
/// <summary>
/// A captured primitive value (either a scalar, a vector, or a linear matrix).
/// </summary>
/// <param name="FieldPath">The path of the field with respect to the shader instance.</param>
/// <param name="TypeName">The full metadata name for the primitive type.</param>
/// <param name="Offset">The byte offset of the value within the root signature.</param>
public sealed record Primitive(EquatableArray<string> FieldPath, string TypeName, int Offset) : FieldInfo;
/// <summary>
/// A captured non linear matrix value.
/// </summary>
/// <param name="FieldPath">The path of the field with respect to the shader instance.</param>
/// <param name="TypeName">The full metadata name for the matrix type.</param>
/// <param name="ElementName">The name of each element in the matrix type.</param>
/// <param name="Rows">The number of rows in the matrix.</param>
/// <param name="Columns">The number of columns in the matrix.</param>
/// <param name="Offsets">The sequence of byte offsets for each row within the root signature.</param>
public sealed record NonLinearMatrix(
EquatableArray<string> FieldPath,
string TypeName,
string ElementName,
int Rows,
int Columns,
EquatableArray<int> Offsets) : FieldInfo;
}
+9
ファイルの表示
@@ -0,0 +1,9 @@
namespace ComputeSharp.SourceGenerators.Models;
/// <summary>
/// A model describing a captured resource (either a buffer or a texture).
/// </summary>
/// <param name="FieldName">The name of the resource field.</param>
/// <param name="TypeName">The full metadata name for the resource type.</param>
/// <param name="Offset">The offset for the resource within the root signature.</param>
public sealed record ResourceInfo(string FieldName, string TypeName, int Offset);
+2
ファイルの表示
@@ -15,6 +15,7 @@ namespace ComputeSharp.SourceGenerators.Models;
/// <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="Resources">The description on shader captured resources.</param>
/// <param name="ResourceDescriptors">The sequence of resource descriptors for the shader.</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>
@@ -29,6 +30,7 @@ internal sealed record ShaderInfo(
int ConstantBufferSizeInBytes,
int ResourceCount,
EquatableArray<FieldInfo> Fields,
EquatableArray<ResourceInfo> Resources,
EquatableArray<ResourceDescriptor> ResourceDescriptors,
HlslBytecodeInfoKey HlslInfoKey,
HlslBytecodeInfo HlslInfo,