using System; using System.Collections.Immutable; using ComputeSharp.D2D1.SourceGenerators.Models; using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; using ComputeSharp.SourceGeneration.Models; using Microsoft.CodeAnalysis; using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors; namespace ComputeSharp.D2D1.SourceGenerators; /// partial class D2DPixelShaderDescriptorGenerator { /// /// A helper with all logic to generate the InputDescriptions property. /// private static partial class InputDescriptions { /// /// Extracts the input descriptions for the current shader. /// /// The collection of produced instances. /// The input instance to process. /// The produced input descriptions for the shader. public static void GetInfo( ImmutableArrayBuilder diagnostics, INamedTypeSymbol structDeclarationSymbol, out ImmutableArray inputDescriptions) { int inputCount = 0; using ImmutableArrayBuilder inputDescriptionsBuilder = new(); foreach (AttributeData attributeData in structDeclarationSymbol.GetAttributes()) { using ImmutableArrayBuilder builder = new(); attributeData.AttributeClass?.AppendFullyQualifiedMetadataName(in builder); switch (builder.WrittenSpan) { case "ComputeSharp.D2D1.D2DInputCountAttribute": inputCount = (int)attributeData.ConstructorArguments[0].Value!; break; case "ComputeSharp.D2D1.D2DInputDescriptionAttribute": if (attributeData.ConstructorArguments.Length == 2) { int index = (int)attributeData.ConstructorArguments[0].Value!; D2D1Filter filter = (D2D1Filter)attributeData.ConstructorArguments[1].Value!; _ = attributeData.TryGetNamedArgument("LevelOfDetailCount", out int levelOfDetailCount); inputDescriptionsBuilder.Add(new InputDescription(index, filter, levelOfDetailCount)); } break; default: break; } } inputDescriptions = ImmutableArray.Empty; // Validate the input count (ignore if invalid, this will be validated by GetInputType() generator) if (inputCount is not (>= 0 and <= 8)) { return; } // All simple indices must be in range foreach (InputDescription inputDescription in inputDescriptionsBuilder.WrittenSpan) { if (inputDescription.Index >= inputCount) { diagnostics.Add(OutOfRangeInputDescriptionIndex, structDeclarationSymbol, structDeclarationSymbol); return; } } Span selectedInputDescriptionIndices = stackalloc bool[8]; selectedInputDescriptionIndices.Clear(); // All input description indices must be unique foreach (InputDescription inputDescription in inputDescriptionsBuilder.WrittenSpan) { ref bool isInputDescriptionIndexUsed = ref selectedInputDescriptionIndices[inputDescription.Index]; if (isInputDescriptionIndexUsed) { diagnostics.Add(RepeatedD2DInputDescriptionIndices, structDeclarationSymbol, structDeclarationSymbol); return; } isInputDescriptionIndexUsed = true; } inputDescriptions = inputDescriptionsBuilder.ToImmutable(); } } }