using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Text.RegularExpressions;
using ComputeSharp.Graphics.Helpers;
using ComputeSharp.SourceGeneration.Extensions;
using ComputeSharp.SourceGeneration.Helpers;
using ComputeSharp.SourceGeneration.Mappings;
using ComputeSharp.SourceGeneration.Models;
using Microsoft.CodeAnalysis;
namespace ComputeSharp.SourceGeneration.SyntaxProcessors;
///
/// A processor responsible for extracting info and writing syntax for the constant buffer syntax for shader types.
///
internal static partial class ConstantBufferSyntaxProcessor
{
///
/// A to extract the parameter name of a primary constructor capture field.
///
///
/// Temporary workaround until is available.
///
private static readonly Regex PrimaryConstructorNameRegex = new("^<([^>]+)>P$", RegexOptions.Compiled);
///
/// Explores a given type hierarchy and generates statements to load fields.
///
/// The input object currently in use.
/// The current shader type being explored.
/// The size of the shader constant buffer.
/// The sequence of instances for all captured values.
public static void GetInfo(
Compilation compilation,
ITypeSymbol structDeclarationSymbol,
ref int constantBufferSizeInBytes,
out ImmutableArray fields)
{
// Helper method to traverse the type hierarchy and append all valid fields
static void GetInfo(
Compilation compilation,
ITypeSymbol currentTypeSymbol,
ImmutableArray fieldPath,
ref int constantBufferSizeInBytes,
ImmutableArrayBuilder fields)
{
bool isFirstField = true;
foreach (ISymbol memberSymbol in currentTypeSymbol.GetMembers())
{
// Only process fields of a valid shape/type
if (memberSymbol is not IFieldSymbol { Type: INamedTypeSymbol { IsStatic: false }, IsConst: false, IsStatic: false, IsFixedSizeBuffer: false } fieldSymbol)
{
continue;
}
// Skip fields of not accessible types (the analyzer will handle this)
if (!fieldSymbol.Type.IsAccessibleFromCompilationAssembly(compilation))
{
continue;
}
// Try to get the name to use for the field and the accessor
if (!TryGetFieldAccessorName(fieldSymbol, out string? fieldName, out string? unspeakableName))
{
continue;
}
string typeName = fieldSymbol.Type.GetFullyQualifiedMetadataName();
// The first item in each nested struct needs to be aligned to 16 bytes
if (isFirstField && fieldPath.Length > 0)
{
constantBufferSizeInBytes = AlignmentHelper.Pad(constantBufferSizeInBytes, 16);
isFirstField = false;
}
if (HlslKnownTypes.IsKnownHlslType(typeName))
{
ImmutableArray nestedFieldPath = fieldPath.Add(new FieldPathPart.Leaf(fieldName, unspeakableName));
fields.Add(GetHlslKnownTypeFieldInfo(nestedFieldPath, typeName, ref constantBufferSizeInBytes));
}
else if (fieldSymbol.Type.IsUnmanagedType)
{
string nestedTypeName = fieldSymbol.Type.GetFullyQualifiedName(includeGlobal: true);
FieldPathPart fieldPathPart = new FieldPathPart.Nested(fieldName, unspeakableName, nestedTypeName);
// Custom struct type defined by the user
GetInfo(compilation, fieldSymbol.Type, fieldPath.Add(fieldPathPart), ref constantBufferSizeInBytes, fields);
}
}
}
using ImmutableArrayBuilder fieldBuilder = new();
// Traverse all shader fields and gather info, and update the tracking offsets
GetInfo(
compilation,
structDeclarationSymbol,
ImmutableArray.Empty,
ref constantBufferSizeInBytes,
fieldBuilder);
fields = fieldBuilder.ToImmutable();
}
///
public static void GetInfo(
Compilation compilation,
ITypeSymbol structDeclarationSymbol,
ref int constantBufferSizeInBytes)
{
// Like with the GetHlslKnownTypeFieldInfo overload below, this method is a minimal, optimized
// version of the GetInfo overload above, only meant to be used by the analyzer to compute the
// constant buffer size in bytes, and nothing else. See comments above for the code below.
static void GetInfo(
Compilation compilation,
ITypeSymbol currentTypeSymbol,
int fieldDepth,
ref int constantBufferSizeInBytes)
{
bool isFirstField = true;
foreach (ISymbol memberSymbol in currentTypeSymbol.GetMembers())
{
// Only process fields of a valid shape/type (same as the previous method).
// We don't need the additional checks here for accessibility or the field
// being valid, since those would already get separate warnings.
if (memberSymbol is not IFieldSymbol { Type: INamedTypeSymbol { IsStatic: false }, IsConst: false, IsStatic: false, IsFixedSizeBuffer: false } fieldSymbol)
{
continue;
}
string typeName = fieldSymbol.Type.GetFullyQualifiedMetadataName();
// Pad items (same as method above)
if (isFirstField && fieldDepth > 0)
{
constantBufferSizeInBytes = AlignmentHelper.Pad(constantBufferSizeInBytes, 16);
isFirstField = false;
}
// Quickly inspect the shader fields and update the rolling buffer size
if (HlslKnownTypes.IsKnownHlslType(typeName))
{
GetHlslKnownTypeFieldInfo(typeName, ref constantBufferSizeInBytes);
}
else if (fieldSymbol.Type.IsUnmanagedType)
{
GetInfo(compilation, fieldSymbol.Type, fieldDepth + 1, ref constantBufferSizeInBytes);
}
}
}
// Traverse all shader fields and simply track the buffer size
GetInfo(
compilation,
structDeclarationSymbol,
fieldDepth: 0,
ref constantBufferSizeInBytes);
}
///
/// Tries to get the field name and the accessor name for a given field.
///
/// The field symbol to analyze.
/// The name of the field that's referenced in code by users.
/// The real name of the field that should be accessed, when different.
/// Whether the field is supported and the necessary info could be retrieved.
public static bool TryGetFieldAccessorName(
IFieldSymbol fieldSymbol,
[NotNullWhen(true)] out string? referencedName,
out string? unspeakableName)
{
// If the field can be referenced by name, we can just access it directly
if (fieldSymbol.CanBeReferencedByName)
{
referencedName = fieldSymbol.Name;
unspeakableName = null;
return true;
}
// If the field is a primary constructor capture field, get the original name
if (PrimaryConstructorNameRegex.Match(fieldSymbol.Name) is { Success: true, Groups: [_, { Value: string parameterName }] })
{
referencedName = parameterName;
unspeakableName = fieldSymbol.Name;
return true;
}
// The field is not recognized, so just invalid
referencedName = null;
unspeakableName = null;
return false;
}
///
/// Gets info on a given captured HLSL primitive type (either a scalar, a vector or a matrix).
///
/// The current path of the field with respect to the shader instance.
/// The type name currently being read.
/// The current offset within the loaded data buffer.
/// The for the inspected shader field.
private static FieldInfo GetHlslKnownTypeFieldInfo(
ImmutableArray fieldPath,
string typeName,
ref int rawDataOffset)
{
// For scalar, vector and linear matrix types, serialize the value normally.
// Only the initial alignment needs to be considered, while data is packed.
(int fieldSize, int fieldPack) = HlslKnownSizes.GetTypeInfo(typeName);
// Check if the current type is a matrix type with more than one row. In this
// case, each row is aligned as if it was a separate array, so the start of
// each row needs to be at a multiple of 16 bytes (a float4 register).
if (HlslKnownTypes.IsNonLinearMatrixType(typeName, out string? elementName, out int rows, out int columns))
{
using ImmutableArrayBuilder builder = new();
for (int j = 0; j < rows; j++)
{
int startingRawDataOffset = AlignmentHelper.Pad(rawDataOffset, 16);
builder.Add(startingRawDataOffset);
rawDataOffset = startingRawDataOffset + (fieldPack * columns);
}
return new FieldInfo.NonLinearMatrix(
fieldPath,
typeName,
elementName!,
rows,
columns,
builder.ToImmutable());
}
else
{
// Calculate the right offset with 16-bytes padding (HLSL constant buffer).
// Since we're in a constant buffer, we need to both pad the starting offset
// to be aligned to the packing size of the type, and also to align the initial
// offset to ensure that values do not cross 16 bytes boundaries either.
int startingRawDataOffset = AlignmentHelper.AlignToBoundary(
AlignmentHelper.Pad(rawDataOffset, fieldPack),
fieldSize,
16);
rawDataOffset = startingRawDataOffset + fieldSize;
return new FieldInfo.Primitive(fieldPath, typeName, startingRawDataOffset);
}
}
///
private static void GetHlslKnownTypeFieldInfo(string typeName, ref int rawDataOffset)
{
// Note: this method is a minimal, optimized version of the overload above, simply computing
// the field offset and nothing else. See comments there for the code below. There are no
// changes done here other than removing all redundant code to compute additional field info.
(int fieldSize, int fieldPack) = HlslKnownSizes.GetTypeInfo(typeName);
if (HlslKnownTypes.IsNonLinearMatrixType(typeName, out _, out int rows, out int columns))
{
for (int j = 0; j < rows; j++)
{
rawDataOffset = AlignmentHelper.Pad(rawDataOffset, 16) + (fieldPack * columns);
}
}
else
{
int startingRawDataOffset = AlignmentHelper.AlignToBoundary(
AlignmentHelper.Pad(rawDataOffset, fieldPack),
fieldSize,
16);
rawDataOffset = startingRawDataOffset + fieldSize;
}
}
}