diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslBytecode.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslBytecode.cs index 3a594262..24299b93 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslBytecode.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslBytecode.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Immutable; using System.ComponentModel; -using System.Linq; using System.Runtime.CompilerServices; using System.Threading; using ComputeSharp.D2D1.Shaders.Translation; @@ -113,34 +112,71 @@ partial class D2DPixelShaderDescriptorGenerator /// /// Extracts the metadata definition for the current shader. /// + /// The input object currently in use. /// The input instance to process. /// The number of inputs for the shader. /// Whether the shader only has simple inputs. - public static bool IsSimpleInputShader(INamedTypeSymbol structDeclarationSymbol, int inputCount) + public static bool IsSimpleInputShader( + Compilation compilation, + INamedTypeSymbol structDeclarationSymbol, + int inputCount) { + return IsSimpleInputShader( + structDeclarationSymbol, + compilation.GetTypeByMetadataName("ComputeSharp.D2D1.D2DInputSimpleAttribute")!, + inputCount); + } + + /// + /// Extracts the metadata definition for the current shader. + /// + /// The input instance to process. + /// The symbol for the [D2DInputSimple] attribute. + /// The number of inputs for the shader. + /// Whether the shader only has simple inputs. + public static bool IsSimpleInputShader( + INamedTypeSymbol structDeclarationSymbol, + INamedTypeSymbol d2DInputSimpleSymbolAttributeSymbol, + int inputCount) + { + // We cannot trust the input count to be valid at this point (it may be invalid and + // with diagnostic already emitted for it). So first, just clamp it in the right range. + inputCount = Math.Max(0, Math.Min(8, inputCount)); + // If there are no inputs, the shader is as if only had simple inputs if (inputCount == 0) { return true; } - // Build a map of all simple inputs (unmarked inputs default to being complex) - bool[] simpleInputsMap = new bool[inputCount]; + // Build a map of all simple inputs (unmarked inputs default to being complex). + // We can never have more than 8 inputs, and if there are it means the shader is + // not valid. Just ignore them, and the generator will emit a separate diagnostic. + Span simpleInputsMap = stackalloc bool[8]; + + // We first start with all inputs marked as complex (ie. not simple) + simpleInputsMap.Clear(); foreach (AttributeData attributeData in structDeclarationSymbol.GetAttributes()) { - switch (attributeData.AttributeClass?.GetFullyQualifiedMetadataName()) + // Only retrieve indices of simple inputs that are in range. If an input is out of + // range, the diagnostic for it will already be emitted by a previous generator step. + if (SymbolEqualityComparer.Default.Equals(attributeData.AttributeClass, d2DInputSimpleSymbolAttributeSymbol) && + attributeData.ConstructorArguments is [{ Value: >= 0 and < 8 and int index }]) { - // Only retrieve indices of simple inputs that are in range. If an input is out of - // range, the diagnostic for it will already be emitted by a previous generator step. - case "ComputeSharp.D2D1.D2DInputSimpleAttribute" - when attributeData.ConstructorArguments[0].Value is int index && index < inputCount: - simpleInputsMap[index] = true; - break; + simpleInputsMap[index] = true; } } - return simpleInputsMap.All(static x => x); + bool isSimpleInputShader = true; + + // Validate all inputs in our range (filtered by the allowed one) + foreach (bool isSimpleInput in simpleInputsMap[..inputCount]) + { + isSimpleInputShader &= isSimpleInput; + } + + return isSimpleInputShader; } /// diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.cs index 8eddfaaa..dd88f388 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.cs @@ -145,7 +145,7 @@ public sealed partial class D2DPixelShaderDescriptorGenerator : IIncrementalGene token.ThrowIfCancellationRequested(); // Get the shader profile and linking info for LoadBytecode() - bool isLinkingSupported = HlslBytecode.IsSimpleInputShader(typeSymbol, inputCount); + bool isLinkingSupported = HlslBytecode.IsSimpleInputShader(context.SemanticModel.Compilation, typeSymbol, inputCount); D2D1ShaderProfile? requestedShaderProfile = HlslBytecode.GetRequestedShaderProfile(typeSymbol); D2D1CompileOptions? requestedCompileOptions = HlslBytecode.GetRequestedCompileOptions(diagnostics, typeSymbol); D2D1ShaderProfile effectiveShaderProfile = HlslBytecode.GetEffectiveShaderProfile(requestedShaderProfile, out bool isCompilationEnabled);