diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Suppressors/D2D1ResourceTextureUninitializedFieldDiagnosticSuppressor.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Suppressors/D2D1ResourceTextureUninitializedFieldDiagnosticSuppressor.cs index 43e4f83c..02822084 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Suppressors/D2D1ResourceTextureUninitializedFieldDiagnosticSuppressor.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Suppressors/D2D1ResourceTextureUninitializedFieldDiagnosticSuppressor.cs @@ -61,7 +61,7 @@ public sealed class D2D1ResourceTextureUninitializedFieldDiagnosticSuppressor : } // Also check if the containing type is in fact a D2D1 pixel shader type - if (ID2D1ShaderGenerator.IsD2D1PixelShaderType(structSymbol, pixelShaderInterfaceSymbol)) + if (ID2D1ShaderGenerator.IsD2D1PixelShaderType(structSymbol, semanticModel.Compilation)) { context.ReportSuppression(Suppression.Create(UninitializedD2D1ResourceTextureField, diagnostic)); } diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadInputDescriptionsMethod.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadInputDescriptionsMethod.cs index 04228cff..1bfbf0c5 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadInputDescriptionsMethod.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadInputDescriptionsMethod.cs @@ -20,17 +20,16 @@ partial class ID2D1ShaderGenerator /// /// 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. - /// The collection of produced instances. public static void GetInfo( + ImmutableArray.Builder diagnostics, INamedTypeSymbol structDeclarationSymbol, - out ImmutableArray inputDescriptions, - out ImmutableArray diagnostics) + out ImmutableArray inputDescriptions) { int inputCount = 0; ImmutableArray.Builder inputDescriptionsBuilder = ImmutableArray.CreateBuilder(); - ImmutableArray.Builder diagnosticsBuilder = ImmutableArray.CreateBuilder(); foreach (AttributeData attributeData in structDeclarationSymbol.GetAttributes()) { @@ -60,15 +59,15 @@ partial class ID2D1ShaderGenerator // Validate the input count (ignore if invalid, this will be validated by GetInputType() generator) if (inputCount is not (>= 0 and <= 8)) { - goto End; + return; } // All simple indices must be in range if (inputDescriptionsBuilder.Any(description => description.Index >= inputCount)) { - diagnosticsBuilder.Add(OutOfRangeInputDescriptionIndex, structDeclarationSymbol, structDeclarationSymbol); + diagnostics.Add(OutOfRangeInputDescriptionIndex, structDeclarationSymbol, structDeclarationSymbol); - goto End; + return; } HashSet inputDescriptionIndices = new(inputDescriptionsBuilder.Select(description => description.Index)); @@ -76,15 +75,12 @@ partial class ID2D1ShaderGenerator // All input description indices must be unique if (inputDescriptionIndices.Count != inputDescriptionsBuilder.Count) { - diagnosticsBuilder.Add(RepeatedD2DInputDescriptionIndices, structDeclarationSymbol, structDeclarationSymbol); + diagnostics.Add(RepeatedD2DInputDescriptionIndices, structDeclarationSymbol, structDeclarationSymbol); - goto End; + return; } inputDescriptions = inputDescriptionsBuilder.ToImmutable(); - - End: - diagnostics = diagnosticsBuilder.ToImmutable(); } } } diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.Helpers.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.Helpers.cs index b213fcc4..6d1c78fc 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.Helpers.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.Helpers.cs @@ -14,16 +14,20 @@ partial class ID2D1ShaderGenerator /// Gets the shader type for a given shader, if any. /// /// The input instance to check. - /// The instance for . - /// Whether or not implements . - public static bool IsD2D1PixelShaderType(INamedTypeSymbol typeSymbol, INamedTypeSymbol d2D1PixelShaderSymbol) + /// The instance currently in use. + /// Whether or not is a D2D1 interface type. + public static bool IsD2D1PixelShaderType(INamedTypeSymbol typeSymbol, Compilation compilation) { foreach (INamedTypeSymbol interfaceSymbol in typeSymbol.AllInterfaces) { - if (interfaceSymbol.Name == nameof(ID2D1PixelShader) && - SymbolEqualityComparer.Default.Equals(interfaceSymbol, d2D1PixelShaderSymbol)) + if (interfaceSymbol.Name == nameof(ID2D1PixelShader)) { - return true; + INamedTypeSymbol d2D1PixelShaderSymbol = compilation.GetTypeByMetadataName("ComputeSharp.D2D1.ID2D1PixelShader")!; + + if (SymbolEqualityComparer.Default.Equals(interfaceSymbol, d2D1PixelShaderSymbol)) + { + return true; + } } } diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs index 4a174c41..a3350ad5 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; using System.Text; @@ -22,121 +21,121 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator /// public void Initialize(IncrementalGeneratorInitializationContext context) { + IncrementalValuesProvider shaderInfoWithErrors = + context.SyntaxProvider + .CreateSyntaxProvider( + static (node, _) => node.IsTypeDeclarationWithOrPotentiallyWithBaseTypes(), + static (context, token) => + { + StructDeclarationSyntax typeDeclaration = (StructDeclarationSyntax)context.Node; + + // If the type symbol doesn't have at least one interface, it can't possibly be a shader type + if (context.SemanticModel.GetDeclaredSymbol(typeDeclaration, token) is not INamedTypeSymbol { AllInterfaces.Length: > 0 } typeSymbol) + { + return default; + } + + // Check that the shader implements the ID2D1PixelShader interface + if (!IsD2D1PixelShaderType(typeSymbol, context.SemanticModel.Compilation)) + { + return default; + } + + ImmutableArray.Builder diagnostics = ImmutableArray.CreateBuilder(); + + // LoadDispatchData() info + ImmutableArray fieldInfos = LoadDispatchData.GetInfo( + diagnostics, + typeSymbol, + out int constantBufferSizeInBytes); + + token.ThrowIfCancellationRequested(); + + // Get the input info for GetInputInfo() + GetInputType.GetInfo( + diagnostics, + typeSymbol, + out int inputCount, + out ImmutableArray inputSimpleIndices, + out ImmutableArray inputComplexIndices, + out ImmutableArray inputTypes); + + // Get the resource texture info for LoadResourceTextureDescriptions() + LoadResourceTextureDescriptions.GetInfo( + diagnostics, + typeSymbol, + inputCount, + out ImmutableArray resourceTextureDescriptions); + + // Get HLSL source for BuildHlslSource() + string hlslSource = BuildHlslSource.GetHlslSource( + diagnostics, + context.SemanticModel.Compilation, + typeDeclaration, + typeSymbol, + inputCount, + inputSimpleIndices, + inputComplexIndices); + + token.ThrowIfCancellationRequested(); + + // Get the shader profile and linking info for LoadBytecode() + D2D1ShaderProfile? shaderProfile = LoadBytecode.GetShaderProfile(typeSymbol); + D2D1CompileOptions? compileOptions = LoadBytecode.GetCompileOptions(diagnostics, typeSymbol); + bool isLinkingSupported = LoadBytecode.IsSimpleInputShader(typeSymbol, inputCount); + + token.ThrowIfCancellationRequested(); + + // Get the info for GetOutputBuffer() + GetOutputBuffer.GetInfo(typeSymbol, out D2D1BufferPrecision bufferPrecision, out D2D1ChannelDepth channelDepth); + + token.ThrowIfCancellationRequested(); + + // Get the info for LoadInputDescriptions() + LoadInputDescriptions.GetInfo( + diagnostics, + typeSymbol, + out ImmutableArray inputDescriptions); + + token.ThrowIfCancellationRequested(); + + // Get the info for GetPixelOptions() + GetPixelOptions.GetInfo(typeSymbol, out D2D1PixelOptions pixelOptions); + + token.ThrowIfCancellationRequested(); + + return new D2D1ShaderInfo( + Hierarchy: HierarchyInfo.From(typeSymbol), + DispatchData: new DispatchDataInfo(fieldInfos, constantBufferSizeInBytes), + InputTypes: new InputTypesInfo(inputTypes), + ResourceTextureDescriptions: new ResourceTextureDescriptionsInfo(resourceTextureDescriptions), + HlslShaderSource: new HlslShaderSourceInfo( + hlslSource, + shaderProfile, + compileOptions, + isLinkingSupported, + HasErrors: diagnostics.Count > 0), + OutputBuffer: new OutputBufferInfo(bufferPrecision, channelDepth), + InputDescriptions: new InputDescriptionsInfo(inputDescriptions), + PixelOptions: pixelOptions, + Diagnostcs: diagnostics.ToImmutable()); + }) + .Where(static item => item is not null)!; + // Check whether [SkipLocalsInit] can be used IncrementalValueProvider canUseSkipLocalsInit = context.CompilationProvider - .Select(static (compilation, token) => + .Select(static (compilation, _) => compilation.Options is CSharpCompilationOptions { AllowUnsafe: true } && compilation.HasAccessibleTypeWithMetadataName("System.Runtime.CompilerServices.SkipLocalsInitAttribute")); - // Get all declared struct types and their type symbols - IncrementalValuesProvider<(StructDeclarationSyntax Syntax, INamedTypeSymbol Symbol)> structDeclarations = - context.SyntaxProvider - .CreateSyntaxProvider( - static (node, token) => node is StructDeclarationSyntax structDeclaration, - static (context, token) => ( - (StructDeclarationSyntax)context.Node, - Symbol: (INamedTypeSymbol?)context.SemanticModel.GetDeclaredSymbol(context.Node, token))) - .Where(static pair => pair.Symbol is { Interfaces.IsEmpty: false })!; - - // Get the symbol for the ID2D1PixelShader interface - IncrementalValueProvider pixelShaderInterface = - context.CompilationProvider - .Select(static (compilation, token) => compilation.GetTypeByMetadataName(typeof(ID2D1PixelShader).FullName)!); - - // Filter struct declarations that implement the shader interface, and also gather the hierarchy info - IncrementalValuesProvider<(StructDeclarationSyntax Syntax, INamedTypeSymbol Symbol, HierarchyInfo Hierarchy)> shaderDeclarations = - structDeclarations - .Combine(pixelShaderInterface) - .Where(static item => IsD2D1PixelShaderType(item.Left.Symbol, item.Right)) - .Select(static (item, token) => (item.Left.Syntax, item.Left.Symbol, HierarchyInfo.From(item.Left.Symbol))); - - // Get the dispatch data, input types, resource texture descriptions, HLSL source and embedded bytecode info. This info - // is computed on the same step as parts are shared in following sub-branches in the incremental generator pipeline. - IncrementalValuesProvider<(HierarchyInfo Hierarchy, DispatchDataInfo Dispatch, InputTypesInfo InputTypes, ResourceTextureDescriptionsInfo ResourceTextureDescriptions, HlslShaderSourceInfo Source, ImmutableArray Diagnostics)> shaderInfoWithErrors = - shaderDeclarations - .Combine(context.CompilationProvider) - .Select(static (item, token) => - { - ImmutableArray.Builder diagnostics = ImmutableArray.CreateBuilder(); - - // LoadDispatchData() info - ImmutableArray fieldInfos = LoadDispatchData.GetInfo( - diagnostics, - item.Left.Symbol, - out int constantBufferSizeInBytes); - - token.ThrowIfCancellationRequested(); - - DispatchDataInfo dispatchDataInfo = new(fieldInfos, constantBufferSizeInBytes); - - // Get the input info for GetInputInfo() - GetInputType.GetInfo( - diagnostics, - item.Left.Symbol, - out int inputCount, - out ImmutableArray inputSimpleIndices, - out ImmutableArray inputComplexIndices, - out ImmutableArray inputTypes); - - InputTypesInfo inputTypesInfo = new(inputTypes); - - // Get the resource texture info for LoadResourceTextureDescriptions() - LoadResourceTextureDescriptions.GetInfo( - diagnostics, - item.Left.Symbol, - inputCount, - out ImmutableArray resourceTextureDescriptions); - - ResourceTextureDescriptionsInfo resourceTextureDescriptionsInfo = new(resourceTextureDescriptions); - - // Get HLSL source for BuildHlslSource() - string hlslSource = BuildHlslSource.GetHlslSource( - diagnostics, - item.Right, - item.Left.Syntax, - item.Left.Symbol, - inputCount, - inputSimpleIndices, - inputComplexIndices); - - token.ThrowIfCancellationRequested(); - - // Get the shader profile and linking info for LoadBytecode() - D2D1ShaderProfile? shaderProfile = LoadBytecode.GetShaderProfile(item.Left.Symbol); - D2D1CompileOptions? compileOptions = LoadBytecode.GetCompileOptions(diagnostics, item.Left.Symbol); - bool isLinkingSupported = LoadBytecode.IsSimpleInputShader(item.Left.Symbol, inputCount); - - HlslShaderSourceInfo sourceInfo = new( - hlslSource, - shaderProfile, - compileOptions, - isLinkingSupported, - HasErrors: diagnostics.Count > 0); - - token.ThrowIfCancellationRequested(); - - return ( - item.Left.Hierarchy, - dispatchDataInfo, - inputTypesInfo, - resourceTextureDescriptionsInfo, - sourceInfo, - diagnostics.ToImmutableArray()); - }); - // Output the diagnostics - context.ReportDiagnostics(shaderInfoWithErrors.Select(static (item, token) => item.Diagnostics)); + context.ReportDiagnostics(shaderInfoWithErrors.Select(static (item, _) => item.Diagnostcs)); - // Filter all items to enable caching at a coarse level, and remove diagnostics - IncrementalValuesProvider<(HierarchyInfo Hierarchy, DispatchDataInfo Dispatch, InputTypesInfo InputTypes, ResourceTextureDescriptionsInfo ResourceTextureDescriptions, HlslShaderSourceInfo Source)> shaderInfo = - shaderInfoWithErrors - .Select(static (item, token) => (item.Hierarchy, item.Dispatch, item.InputTypes, item.ResourceTextureDescriptions, item.Source)); - - // Filter items to enable caching for the input count methods + // Get the GetInputCount() info (hierarchy and input count) IncrementalValuesProvider<(HierarchyInfo Hierarchy, int InputCount)> inputCountInfo = - shaderInfo - .Select(static (item, token) => (item.Hierarchy, item.InputTypes.InputTypes.Length)); + shaderInfoWithErrors + .Select(static (item, _) => (item.Hierarchy, item.InputTypes.InputTypes.Length)); // Generate the GetInputCount() methods context.RegisterSourceOutput(inputCountInfo, static (context, item) => @@ -147,10 +146,10 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator context.AddSource($"{item.Hierarchy.FilenameHint}.{nameof(GetInputCount)}.g.cs", compilationUnit.GetText(Encoding.UTF8)); }); - // Get a filtered sequence to enable caching + // Get the GetInputType() info (hierarchy and input types) IncrementalValuesProvider<(HierarchyInfo Hierarchy, InputTypesInfo InputTypes)> inputTypesInfo = - shaderInfo - .Select(static (item, token) => (item.Hierarchy, item.InputTypes)); + shaderInfoWithErrors + .Select(static (item, _) => (item.Hierarchy, item.InputTypes)); // Generate the GetInputType() methods context.RegisterSourceOutput(inputTypesInfo, static (context, item) => @@ -161,10 +160,10 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator context.AddSource($"{item.Hierarchy.FilenameHint}.{nameof(GetInputType)}.g.cs", compilationUnit.GetText(Encoding.UTF8)); }); - // Get a filtered sequence to enable caching + // Get the LoadResourceTextureDescriptions() info (hierarchy and resource texture descriptions) IncrementalValuesProvider<(HierarchyInfo Hierarchy, ResourceTextureDescriptionsInfo ResourceTextureDescriptions)> resourceTextureDescriptionsInfo = - shaderInfo - .Select(static (item, token) => (item.Hierarchy, item.ResourceTextureDescriptions)); + shaderInfoWithErrors + .Select(static (item, _) => (item.Hierarchy, item.ResourceTextureDescriptions)); // Generate the LoadResourceTextureDescriptions() methods context.RegisterSourceOutput(resourceTextureDescriptionsInfo.Combine(canUseSkipLocalsInit), static (context, item) => @@ -175,10 +174,10 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator context.AddSource($"{item.Left.Hierarchy.FilenameHint}.{nameof(LoadResourceTextureDescriptions)}.g.cs", compilationUnit.GetText(Encoding.UTF8)); }); - // Get a filtered sequence to enable caching + // Get the info for both LoadDispatchData() and InitializeFromDispatchData() (hierarchy and dispatch data) IncrementalValuesProvider<(HierarchyInfo Hierarchy, DispatchDataInfo Dispatch)> dispatchDataInfo = - shaderInfo - .Select(static (item, token) => (item.Hierarchy, item.Dispatch)); + shaderInfoWithErrors + .Select(static (item, _) => (item.Hierarchy, item.DispatchData)); // Generate the LoadDispatchData() methods context.RegisterSourceOutput(dispatchDataInfo.Combine(canUseSkipLocalsInit), static (context, item) => @@ -198,10 +197,10 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator context.AddSource($"{item.Hierarchy.FilenameHint}.{nameof(InitializeFromDispatchData)}.g.cs", compilationUnit.GetText(Encoding.UTF8)); }); - // Get the filtered sequence to enable caching + // Get the BuildHlslSource() info (hierarchy and HLSL source) IncrementalValuesProvider<(HierarchyInfo Hierarchy, string HlslSource)> hlslSourceInfo = - shaderInfo - .Select(static (item, token) => (item.Hierarchy, item.Source.HlslSource)); + shaderInfoWithErrors + .Select(static (item, _) => (item.Hierarchy, item.HlslShaderSource.HlslSource)); // Generate the BuildHlslSource() methods context.RegisterSourceOutput(hlslSourceInfo, static (context, item) => @@ -212,10 +211,10 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator context.AddSource($"{item.Hierarchy.FilenameHint}.{nameof(BuildHlslSource)}.g.cs", compilationUnit.GetText(Encoding.UTF8)); }); - // Get a filtered sequence to enable caching + // Get a filtered sequence to enable caching for the HLSL source info, before the shader compilation step IncrementalValuesProvider<(HierarchyInfo Hierarchy, HlslShaderSourceInfo Source)> shaderBytecodeInfo = - shaderInfo - .Select(static (item, token) => (item.Hierarchy, item.Source)); + shaderInfoWithErrors + .Select(static (item, _) => (item.Hierarchy, item.HlslShaderSource)); // Compile the requested shader bytecodes IncrementalValuesProvider<(HierarchyInfo Hierarchy, EmbeddedBytecodeInfo BytecodeInfo, DiagnosticInfo? Diagnostic)> embeddedBytecodeWithErrors = @@ -242,10 +241,10 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator // Gather the diagnostics IncrementalValuesProvider embeddedBytecodeDiagnostics = embeddedBytecodeWithErrors - .Select(static (item, token) => (item.Hierarchy.FullyQualifiedMetadataName, item.Diagnostic)) + .Select(static (item, _) => (item.Hierarchy.FullyQualifiedMetadataName, item.Diagnostic)) .Where(static item => item.Diagnostic is not null) .Combine(context.CompilationProvider) - .Select(static (item, token) => + .Select(static (item, _) => { INamedTypeSymbol typeSymbol = item.Right.GetTypeByMetadataName(item.Left.FullyQualifiedMetadataName)!; @@ -261,7 +260,7 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator // Get the filtered sequence to enable caching IncrementalValuesProvider<(HierarchyInfo Hierarchy, EmbeddedBytecodeInfo BytecodeInfo)> embeddedBytecode = embeddedBytecodeWithErrors - .Select(static (item, token) => (item.Hierarchy, item.BytecodeInfo)); + .Select(static (item, _) => (item.Hierarchy, item.BytecodeInfo)); // Generate the LoadBytecode() methods context.RegisterSourceOutput(embeddedBytecode, static (context, item) => @@ -273,84 +272,43 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator context.AddSource($"{item.Hierarchy.FilenameHint}.{nameof(LoadBytecode)}.g.cs", text); }); - // Get the output buffer data, which can be computed separately from all other generation steps. - // Caching at a fine grained level can be enabled immediately, as both models are comparable. - IncrementalValuesProvider<(HierarchyInfo Hierarchy, OutputBufferInfo Info)> outputBufferInfo = - shaderDeclarations - .Select(static (item, token) => - { - // GetOutputBuffer() info - GetOutputBuffer.GetInfo(item.Symbol, out D2D1BufferPrecision bufferPrecision, out D2D1ChannelDepth channelDepth); - - token.ThrowIfCancellationRequested(); - - OutputBufferInfo outputBufferInfo = new(bufferPrecision, channelDepth); - - return (item.Hierarchy, outputBufferInfo); - }); + // Get the GetOutputBuffer() info (hierarchy and output buffers) + IncrementalValuesProvider<(HierarchyInfo Hierarchy, OutputBufferInfo OutputBuffer)> outputBufferInfo = + shaderInfoWithErrors + .Select(static (item, _) => (item.Hierarchy, item.OutputBuffer)); // Generate the GetOutputBuffer() methods context.RegisterSourceOutput(outputBufferInfo, static (context, item) => { - MethodDeclarationSyntax getOutputBufferMethod = GetOutputBuffer.GetSyntax(item.Info); + MethodDeclarationSyntax getOutputBufferMethod = GetOutputBuffer.GetSyntax(item.OutputBuffer); CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Hierarchy, getOutputBufferMethod, canUseSkipLocalsInit: false); context.AddSource($"{item.Hierarchy.FilenameHint}.{nameof(GetOutputBuffer)}.g.cs", compilationUnit.GetText(Encoding.UTF8)); }); - // Get the input description data, which can also be computed separately from all other generation steps. - IncrementalValuesProvider<(HierarchyInfo Hierarchy, Result Info)> inputDescriptionsInfoWithErrors = - shaderDeclarations - .Select(static (item, token) => - { - // LoadInputDescriptions() info - LoadInputDescriptions.GetInfo( - item.Symbol, - out ImmutableArray inputDescriptions, - out ImmutableArray diagnostics); - - token.ThrowIfCancellationRequested(); - - InputDescriptionsInfo inputDescriptionsInfo = new(inputDescriptions); - - return (item.Hierarchy, new Result(inputDescriptionsInfo, diagnostics)); - }); - - // Output the diagnostics - context.ReportDiagnostics(inputDescriptionsInfoWithErrors.Select(static (item, token) => item.Info.Errors)); - - // Get the filtered sequence to enable caching - IncrementalValuesProvider<(HierarchyInfo Hierarchy, InputDescriptionsInfo Info)> inputDescriptionsInfo = - inputDescriptionsInfoWithErrors - .Select(static (item, token) => (item.Hierarchy, item.Info.Value)); + // Get the LoadInputDescriptions() info (hierarchy and input descriptions) + IncrementalValuesProvider<(HierarchyInfo Hierarchy, InputDescriptionsInfo InputDescriptions)> inputDescriptionsInfo = + shaderInfoWithErrors + .Select(static (item, _) => (item.Hierarchy, item.InputDescriptions)); // Generate the LoadInputDescriptions() methods context.RegisterSourceOutput(inputDescriptionsInfo.Combine(canUseSkipLocalsInit), static (context, item) => { - MethodDeclarationSyntax loadInputDescriptionsMethod = LoadInputDescriptions.GetSyntax(item.Left.Info); + MethodDeclarationSyntax loadInputDescriptionsMethod = LoadInputDescriptions.GetSyntax(item.Left.InputDescriptions); CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Left.Hierarchy, loadInputDescriptionsMethod, canUseSkipLocalsInit: item.Right); context.AddSource($"{item.Left.Hierarchy.FilenameHint}.{nameof(LoadInputDescriptions)}.g.cs", compilationUnit.GetText(Encoding.UTF8)); }); - // Get the pixel options, again independently from other generation steps. - // This can also be immediately cached at a fine grained level. - IncrementalValuesProvider<(HierarchyInfo Hierarchy, D2D1PixelOptions Options)> pixelOptionsInfo = - shaderDeclarations - .Select(static (item, token) => - { - // GetPixelOptions() info - GetPixelOptions.GetInfo(item.Symbol, out D2D1PixelOptions pixelOptions); - - token.ThrowIfCancellationRequested(); - - return (item.Hierarchy, pixelOptions); - }); + // Get the GetPixelOptions() info (hierarchy and pixel options) + IncrementalValuesProvider<(HierarchyInfo Hierarchy, D2D1PixelOptions PixelOptions)> pixelOptionsInfo = + shaderInfoWithErrors + .Select(static (item, _) => (item.Hierarchy, item.PixelOptions)); // Generate the GetPixelOptions() methods context.RegisterSourceOutput(pixelOptionsInfo, static (context, item) => { - MethodDeclarationSyntax getPixelOptionsMethod = GetPixelOptions.GetSyntax(item.Options); + MethodDeclarationSyntax getPixelOptionsMethod = GetPixelOptions.GetSyntax(item.PixelOptions); CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Hierarchy, getPixelOptionsMethod, canUseSkipLocalsInit: false); context.AddSource($"{item.Hierarchy.FilenameHint}.{nameof(GetPixelOptions)}.g.cs", compilationUnit.GetText(Encoding.UTF8)); diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Models/D2D1ShaderInfo.cs b/src/ComputeSharp.D2D1.SourceGenerators/Models/D2D1ShaderInfo.cs new file mode 100644 index 00000000..ed1692f0 --- /dev/null +++ b/src/ComputeSharp.D2D1.SourceGenerators/Models/D2D1ShaderInfo.cs @@ -0,0 +1,74 @@ +using System; +using System.Collections.Generic; +using System.Collections.Immutable; +using ComputeSharp.SourceGeneration.Extensions; +using ComputeSharp.SourceGeneration.Helpers; +using ComputeSharp.SourceGeneration.Models; +using Microsoft.CodeAnalysis; + +namespace ComputeSharp.D2D1.SourceGenerators.Models; + +/// +/// A model representing all necessary info for a full generation pass for a D2D1 shader. +/// +/// The hierarchy info for the shader type. +/// The gathered shader dispatch data. +/// The gathered input types for the shader. +/// The gathered resource texture descriptions for the shader. +/// The processed HLSL source for the shader. +/// The output buffer info for the shader. +/// The gathered input descriptions for the shader. +/// The pixel options used by the shader. +/// The discovered diagnostics, if any. +internal sealed record D2D1ShaderInfo( + HierarchyInfo Hierarchy, + DispatchDataInfo DispatchData, + InputTypesInfo InputTypes, + ResourceTextureDescriptionsInfo ResourceTextureDescriptions, + HlslShaderSourceInfo HlslShaderSource, + OutputBufferInfo OutputBuffer, + InputDescriptionsInfo InputDescriptions, + D2D1PixelOptions PixelOptions, + ImmutableArray Diagnostcs) +{ + /// + public bool Equals(D2D1ShaderInfo? obj) => Comparer.Default.Equals(this, obj); + + /// + public override int GetHashCode() => Comparer.Default.GetHashCode(this); + + /// + /// An implementation for . + /// + private sealed class Comparer : Comparer + { + /// + protected override void AddToHashCode(ref HashCode hashCode, D2D1ShaderInfo obj) + { + hashCode.Add(obj.Hierarchy); + hashCode.Add(obj.DispatchData); + hashCode.Add(obj.InputTypes); + hashCode.Add(obj.ResourceTextureDescriptions); + hashCode.Add(obj.HlslShaderSource); + hashCode.Add(obj.OutputBuffer); + hashCode.Add(obj.InputDescriptions); + hashCode.Add(obj.PixelOptions); + hashCode.AddRange(obj.Diagnostcs); + } + + /// + protected override bool AreEqual(D2D1ShaderInfo x, D2D1ShaderInfo y) + { + return + x.Hierarchy == y.Hierarchy && + x.DispatchData == y.DispatchData && + x.InputTypes == y.InputTypes && + x.ResourceTextureDescriptions == y.ResourceTextureDescriptions && + x.HlslShaderSource == y.HlslShaderSource && + x.OutputBuffer == y.OutputBuffer && + x.InputDescriptions == y.InputDescriptions && + x.PixelOptions == y.PixelOptions && + x.Diagnostcs.SequenceEqual(y.Diagnostcs); + } + } +} diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/Extensions/SyntaxNodeExtensions.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Extensions/SyntaxNodeExtensions.cs index 86d574e5..d9a2fd78 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/Extensions/SyntaxNodeExtensions.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/Extensions/SyntaxNodeExtensions.cs @@ -13,6 +13,40 @@ namespace ComputeSharp.SourceGeneration.Extensions; /// internal static class SyntaxNodeExtensions { + /// + /// Checks whether a given is a given type declaration with or potentially with any base types, using only syntax. + /// + /// The type of declaration to check for. + /// The input to check. + /// Whether is a given type declaration with or potentially with any base types. + public static bool IsTypeDeclarationWithOrPotentiallyWithBaseTypes(this SyntaxNode node) + where T : TypeDeclarationSyntax + { + // Immediately bail if the node is not a type declaration of the specified type + if (node is not T typeDeclaration) + { + return false; + } + + // If the base types list is not empty, the type can definitely has implemented interfaces + if (typeDeclaration.BaseList is { Types.Count: > 0 }) + { + return true; + } + + // If the base types list is empty, check if the type is partial. If it is, it means + // that there could be another partial declaration with a non-empty base types list. + foreach (SyntaxToken modifier in typeDeclaration.Modifiers) + { + if (modifier.IsKind(SyntaxKind.PartialKeyword)) + { + return true; + } + } + + return false; + } + /// /// Checks a value and replaces the value type to be HLSL compatible, if needed. ///