using ComputeSharp.SourceGeneration.Extensions; using Microsoft.CodeAnalysis; namespace ComputeSharp.D2D1.SourceGenerators; /// partial class ID2D1ShaderGenerator { /// /// A helper with all logic to generate the GetOutputBuffer method. /// private static partial class GetOutputBuffer { /// /// Extracts the output buffer info for the current shader. /// /// The input instance to process. /// The output buffer precision for the shader. /// The output buffer channel depth for the shader. public static void GetInfo( INamedTypeSymbol structDeclarationSymbol, out D2D1BufferPrecision bufferPrecision, out D2D1ChannelDepth channelDepth) { // Try to get the [D2DOutputBuffer] attribute if (!structDeclarationSymbol.TryGetAttributeWithFullMetadataName("ComputeSharp.D2D1.D2DOutputBufferAttribute", out AttributeData? attributeData)) { bufferPrecision = D2D1BufferPrecision.Unknown; channelDepth = D2D1ChannelDepth.Default; return; } // Extract the buffer precision and channel depth, if available if (attributeData!.ConstructorArguments.Length == 1) { if (attributeData.AttributeConstructor is IMethodSymbol { Parameters.Length: 1 } constructorSymbol && constructorSymbol.Parameters[0].Type.HasFullyQualifiedName("ComputeSharp.D2D1.D2D1BufferPrecision")) { bufferPrecision = (D2D1BufferPrecision)attributeData.ConstructorArguments[0].Value!; channelDepth = D2D1ChannelDepth.Default; } else { bufferPrecision = D2D1BufferPrecision.Unknown; channelDepth = (D2D1ChannelDepth)attributeData.ConstructorArguments[0].Value!; } } else if (attributeData.ConstructorArguments.Length == 2) { bufferPrecision = (D2D1BufferPrecision)attributeData.ConstructorArguments[0].Value!; channelDepth = (D2D1ChannelDepth)attributeData.ConstructorArguments[1].Value!; } else { // The syntax tree is likely invalid, so just ignore it for now bufferPrecision = D2D1BufferPrecision.Unknown; channelDepth = D2D1ChannelDepth.Default; } } } }