From 1c0281ae45b8b20eb8b1c190bcd3021995c411a4 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 30 Nov 2023 00:17:45 +0100 Subject: [PATCH 1/3] Check cancellation token when rewriting DX12 HLSL --- ...uteShaderDescriptorGenerator.HlslSource.cs | 76 ++++++++++++++++--- .../ComputeShaderDescriptorGenerator.cs | 2 +- 2 files changed, 66 insertions(+), 12 deletions(-) diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.cs index 25ee4f1c..e61493bf 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.cs +++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; +using System.Threading; using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; using ComputeSharp.SourceGeneration.Mappings; @@ -29,22 +30,22 @@ partial class ComputeShaderDescriptorGenerator /// /// The collection of produced instances. /// The input object currently in use. - /// The node to process. - /// The for . + /// The for the shader type. /// The thread ids value for the X axis. /// The thread ids value for the Y axis. /// The thread ids value for the Z axis. + /// The used to cancel the operation, if needed. /// Indicates whether the current shader uses an implicit texture. /// Whether or not the static sampler is used. /// The resulting HLSL source for the current shader. public static void GetInfo( ImmutableArrayBuilder diagnostics, Compilation compilation, - StructDeclarationSyntax structDeclaration, INamedTypeSymbol structDeclarationSymbol, int threadsX, int threadsY, int threadsZ, + CancellationToken token, out bool isImplicitTextureUsed, out bool isSamplerUsed, out string hlslSource) @@ -58,21 +59,66 @@ partial class ComputeShaderDescriptorGenerator Dictionary instanceMethods = new(SymbolEqualityComparer.Default); Dictionary constantDefinitions = new(SymbolEqualityComparer.Default); - // Explore the syntax tree and extract the processed info - SemanticModelProvider semanticModelProvider = new(compilation); + // Setup the semantic model and basic properties INamedTypeSymbol? pixelShaderSymbol = structDeclarationSymbol.AllInterfaces.FirstOrDefault(static interfaceSymbol => interfaceSymbol is { IsGenericType: true, Name: nameof(IComputeShader) }); bool isComputeShader = pixelShaderSymbol is null; string? implicitTextureType = isComputeShader ? null : HlslKnownTypes.GetMappedNameForPixelShaderType(pixelShaderSymbol!); - (ImmutableArray<(string MetadataName, string Name, string HlslType)> resourceFields, ImmutableArray<(string Name, string HlslType)> valueFields) = GetInstanceFields(diagnostics, structDeclarationSymbol, discoveredTypes, isComputeShader); - ImmutableArray<(string Name, string Type, int? Count)> sharedBuffers = GetSharedBuffers(diagnostics, structDeclarationSymbol, discoveredTypes); - (string entryPoint, ImmutableArray<(string Signature, string Definition)> processedMethods, isSamplerUsed) = GetProcessedMethods(diagnostics, structDeclarationSymbol, semanticModelProvider, discoveredTypes, staticMethods, instanceMethods, constantDefinitions, isComputeShader); + + token.ThrowIfCancellationRequested(); + + (ImmutableArray<(string MetadataName, string Name, string HlslType)> resourceFields, ImmutableArray<(string Name, string HlslType)> valueFields) = GetInstanceFields( + diagnostics, + structDeclarationSymbol, + discoveredTypes, + isComputeShader); + + token.ThrowIfCancellationRequested(); + + ImmutableArray<(string Name, string Type, int? Count)> sharedBuffers = GetSharedBuffers( + diagnostics, + structDeclarationSymbol, + discoveredTypes); + + token.ThrowIfCancellationRequested(); + + SemanticModelProvider semanticModelProvider = new(compilation); + + (string entryPoint, ImmutableArray<(string Signature, string Definition)> processedMethods, isSamplerUsed) = GetProcessedMethods( + diagnostics, + structDeclarationSymbol, + semanticModelProvider, + discoveredTypes, + staticMethods, + instanceMethods, + constantDefinitions, + isComputeShader, + token); + + token.ThrowIfCancellationRequested(); + (string, string)? implicitSamplerField = isSamplerUsed ? ("SamplerState", "__sampler") : default((string, string)?); - ImmutableArray<(string Name, string TypeDeclaration, string? Assignment)> staticFields = GetStaticFields(diagnostics, semanticModelProvider, structDeclarationSymbol, discoveredTypes, constantDefinitions); + ImmutableArray<(string Name, string TypeDeclaration, string? Assignment)> staticFields = GetStaticFields( + diagnostics, + semanticModelProvider, + structDeclarationSymbol, + discoveredTypes, + constantDefinitions); + + token.ThrowIfCancellationRequested(); // Process the discovered types and constants - ImmutableArray<(string Name, string Definition)> declaredTypes = HlslDefinitionsSyntaxProcessor.GetDeclaredTypes(diagnostics, structDeclarationSymbol, discoveredTypes, instanceMethods); + ImmutableArray<(string Name, string Definition)> declaredTypes = HlslDefinitionsSyntaxProcessor.GetDeclaredTypes( + diagnostics, + structDeclarationSymbol, + discoveredTypes, + instanceMethods); + + token.ThrowIfCancellationRequested(); + ImmutableArray<(string Name, string Value)> definedConstants = HlslDefinitionsSyntaxProcessor.GetDefinedConstants(constantDefinitions); + token.ThrowIfCancellationRequested(); + // Check whether an implicit texture is used in the shader isImplicitTextureUsed = implicitTextureType is not null; @@ -312,6 +358,7 @@ partial class ComputeShaderDescriptorGenerator /// The collection of discovered instance methods for custom struct types. /// The collection of discovered constant definitions. /// Indicates whether or not represents a compute shader. + /// The used to cancel the operation, if needed. /// A sequence of processed methods in , and the entry point. private static (string EntryPoint, ImmutableArray<(string Signature, string Definition)> Methods, bool IsSamplerUser) GetProcessedMethods( ImmutableArrayBuilder diagnostics, @@ -321,7 +368,8 @@ partial class ComputeShaderDescriptorGenerator IDictionary staticMethods, IDictionary instanceMethods, IDictionary constantDefinitions, - bool isComputeShader) + bool isComputeShader, + CancellationToken token) { using ImmutableArrayBuilder<(string, string)> methods = new(); @@ -359,6 +407,8 @@ partial class ComputeShaderDescriptorGenerator continue; } + token.ThrowIfCancellationRequested(); + // Create the source rewriter for the current method ShaderSourceRewriter shaderSourceRewriter = new( structDeclarationSymbol, @@ -373,6 +423,8 @@ partial class ComputeShaderDescriptorGenerator // Rewrite the method syntax tree MethodDeclarationSyntax? processedMethod = shaderSourceRewriter.Visit(methodDeclaration)!.WithoutTrivia(); + token.ThrowIfCancellationRequested(); + // Track the implicit sampler, if used isSamplerUsed = isSamplerUsed || shaderSourceRewriter.IsSamplerUsed; @@ -403,6 +455,8 @@ partial class ComputeShaderDescriptorGenerator } } + token.ThrowIfCancellationRequested(); + // Process static methods as well foreach (MethodDeclarationSyntax staticMethod in staticMethods.Values) { diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.cs index bf952bab..0c070ebc 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.cs @@ -85,11 +85,11 @@ public sealed partial class ComputeShaderDescriptorGenerator : IIncrementalGener HlslSource.GetInfo( diagnostics, context.SemanticModel.Compilation, - (StructDeclarationSyntax)context.TargetNode, typeSymbol, threadsX, threadsY, threadsZ, + token, out bool isImplicitTextureUsed, out bool isSamplerUsed, out string hlslSource); From c5a79fd1e11d6bee505257449f55cd3bc438349f Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 30 Nov 2023 00:17:59 +0100 Subject: [PATCH 2/3] Port cancellation token checks to D2D1 generator --- ...xelShaderDescriptorGenerator.HlslSource.cs | 67 ++++++++++++++++--- .../D2DPixelShaderDescriptorGenerator.cs | 4 +- 2 files changed, 60 insertions(+), 11 deletions(-) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.cs index eb9e9769..dc3838d8 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; +using System.Threading; using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; using ComputeSharp.SourceGeneration.Mappings; @@ -28,20 +29,20 @@ partial class D2DPixelShaderDescriptorGenerator /// /// The collection of produced instances. /// The input object currently in use. - /// The node to process. - /// The for . + /// The for the shader type. /// The number of inputs for the shader. /// The indicess of the simple shader inputs. /// The indicess of the complex shader inputs. + /// The used to cancel the operation, if needed. /// The HLSL source for the shader. public static string GetHlslSource( ImmutableArrayBuilder diagnostics, Compilation compilation, - StructDeclarationSyntax structDeclaration, INamedTypeSymbol structDeclarationSymbol, int inputCount, ImmutableArray inputSimpleIndices, - ImmutableArray inputComplexIndices) + ImmutableArray inputComplexIndices, + CancellationToken token) { // Detect any invalid properties HlslDefinitionsSyntaxProcessor.DetectAndReportInvalidPropertyDeclarations(diagnostics, structDeclarationSymbol); @@ -52,6 +53,8 @@ partial class D2DPixelShaderDescriptorGenerator Dictionary instanceMethods = new(SymbolEqualityComparer.Default); Dictionary constantDefinitions = new(SymbolEqualityComparer.Default); + token.ThrowIfCancellationRequested(); + // Extract information on all captured fields GetInstanceFields( diagnostics, @@ -60,20 +63,58 @@ partial class D2DPixelShaderDescriptorGenerator out ImmutableArray<(string Name, string HlslType)> valueFields, out ImmutableArray<(string Name, string HlslType, int Index)> resourceTextureFields); - // Explore the syntax tree and extract the processed info + token.ThrowIfCancellationRequested(); + SemanticModelProvider semanticModelProvider = new(compilation); - (string entryPoint, ImmutableArray<(string Signature, string Definition)> processedMethods) = GetProcessedMethods(diagnostics, structDeclarationSymbol, semanticModelProvider, discoveredTypes, staticMethods, instanceMethods, constantDefinitions, out bool methodsNeedD2D1RequiresScenePosition); - ImmutableArray<(string Name, string TypeDeclaration, string? Assignment)> staticFields = GetStaticFields(diagnostics, semanticModelProvider, structDeclarationSymbol, discoveredTypes, constantDefinitions, out bool fieldsNeedD2D1RequiresScenePosition); + + // Explore the syntax tree and extract the processed info + (string entryPoint, ImmutableArray<(string Signature, string Definition)> processedMethods) = GetProcessedMethods( + diagnostics, + structDeclarationSymbol, + semanticModelProvider, + discoveredTypes, + staticMethods, + instanceMethods, + constantDefinitions, + token, + out bool methodsNeedD2D1RequiresScenePosition); + + token.ThrowIfCancellationRequested(); + + ImmutableArray<(string Name, string TypeDeclaration, string? Assignment)> staticFields = GetStaticFields( + diagnostics, + semanticModelProvider, + structDeclarationSymbol, + discoveredTypes, + constantDefinitions, + out bool fieldsNeedD2D1RequiresScenePosition); + + token.ThrowIfCancellationRequested(); // Process the discovered types and constants - ImmutableArray<(string Name, string Definition)> declaredTypes = HlslDefinitionsSyntaxProcessor.GetDeclaredTypes(diagnostics, structDeclarationSymbol, discoveredTypes, instanceMethods); + ImmutableArray<(string Name, string Definition)> declaredTypes = HlslDefinitionsSyntaxProcessor.GetDeclaredTypes( + diagnostics, + structDeclarationSymbol, + discoveredTypes, + instanceMethods); + + token.ThrowIfCancellationRequested(); + ImmutableArray<(string Name, string Value)> definedConstants = HlslDefinitionsSyntaxProcessor.GetDefinedConstants(constantDefinitions); + token.ThrowIfCancellationRequested(); + // Check whether the scene position is required bool requiresScenePosition = GetD2DRequiresScenePositionInfo(structDeclarationSymbol); // Emit diagnostics for incorrect scene position uses - ReportInvalidD2DRequiresScenePositionUse(diagnostics, structDeclarationSymbol, requiresScenePosition, methodsNeedD2D1RequiresScenePosition || fieldsNeedD2D1RequiresScenePosition); + ReportInvalidD2DRequiresScenePositionUse( + diagnostics, + structDeclarationSymbol, + requiresScenePosition, + methodsNeedD2D1RequiresScenePosition || fieldsNeedD2D1RequiresScenePosition); + + token.ThrowIfCancellationRequested(); // Get the HLSL source return GetHlslSource( @@ -264,6 +305,7 @@ partial class D2DPixelShaderDescriptorGenerator /// The collection of discovered instance methods for custom struct types. /// The collection of discovered constant definitions. /// Whether or not the shader needs the [D2DRequiresScenePosition] annotation. + /// The used to cancel the operation, if needed. /// A sequence of processed methods in , and the entry point. private static (string EntryPoint, ImmutableArray<(string Signature, string Definition)> Methods) GetProcessedMethods( ImmutableArrayBuilder diagnostics, @@ -273,6 +315,7 @@ partial class D2DPixelShaderDescriptorGenerator IDictionary staticMethods, IDictionary instanceMethods, IDictionary constantDefinitions, + CancellationToken token, out bool needsD2D1RequiresScenePosition) { using ImmutableArrayBuilder<(string, string)> methods = new(); @@ -305,6 +348,8 @@ partial class D2DPixelShaderDescriptorGenerator continue; } + token.ThrowIfCancellationRequested(); + // Create the source rewriter for the current method ShaderSourceRewriter shaderSourceRewriter = new( structDeclarationSymbol, @@ -319,6 +364,8 @@ partial class D2DPixelShaderDescriptorGenerator // Rewrite the method syntax tree MethodDeclarationSyntax? processedMethod = shaderSourceRewriter.Visit(methodDeclaration)!.WithoutTrivia(); + token.ThrowIfCancellationRequested(); + // Update the position requirement needsD2D1RequiresScenePosition |= shaderSourceRewriter.NeedsD2DRequiresScenePositionAttribute; @@ -346,6 +393,8 @@ partial class D2DPixelShaderDescriptorGenerator } } + token.ThrowIfCancellationRequested(); + // Process static methods as well foreach (MethodDeclarationSyntax staticMethod in staticMethods.Values) { diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.cs index 48acf353..2358e15b 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.cs @@ -119,11 +119,11 @@ public sealed partial class D2DPixelShaderDescriptorGenerator : IIncrementalGene string hlslSource = HlslSource.GetHlslSource( diagnostics, context.SemanticModel.Compilation, - (StructDeclarationSyntax)context.TargetNode, typeSymbol, inputCount, inputSimpleIndices, - inputComplexIndices); + inputComplexIndices, + token); token.ThrowIfCancellationRequested(); From 8536bcb16ada846960458c63829b732f6b88bc71 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 30 Nov 2023 00:19:20 +0100 Subject: [PATCH 3/3] Remove IComputeShader file links from DX12 generator --- .../ComputeShaderDescriptorGenerator.HlslSource.cs | 6 +++--- .../ComputeSharp.SourceGenerators.csproj | 2 -- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.cs index e61493bf..1c346c43 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.cs +++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.cs @@ -60,7 +60,7 @@ partial class ComputeShaderDescriptorGenerator Dictionary constantDefinitions = new(SymbolEqualityComparer.Default); // Setup the semantic model and basic properties - INamedTypeSymbol? pixelShaderSymbol = structDeclarationSymbol.AllInterfaces.FirstOrDefault(static interfaceSymbol => interfaceSymbol is { IsGenericType: true, Name: nameof(IComputeShader) }); + INamedTypeSymbol? pixelShaderSymbol = structDeclarationSymbol.AllInterfaces.FirstOrDefault(static interfaceSymbol => interfaceSymbol is { IsGenericType: true, Name: "IComputeShader" }); bool isComputeShader = pixelShaderSymbol is null; string? implicitTextureType = isComputeShader ? null : HlslKnownTypes.GetMappedNameForPixelShaderType(pixelShaderSymbol!); @@ -391,12 +391,12 @@ partial class ComputeShaderDescriptorGenerator bool isShaderEntryPoint = (isComputeShader && - methodSymbol.Name == nameof(IComputeShader.Execute) && + methodSymbol.Name == "Execute" && methodSymbol.ReturnsVoid && methodSymbol.TypeParameters.Length == 0 && methodSymbol.Parameters.Length == 0) || (!isComputeShader && - methodSymbol.Name == nameof(IComputeShader.Execute) && + methodSymbol.Name == "Execute" && methodSymbol.ReturnType is not null && // TODO: match for pixel type methodSymbol.TypeParameters.Length == 0 && methodSymbol.Parameters.Length == 0); diff --git a/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj b/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj index bdd7240f..330353f5 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj +++ b/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj @@ -67,8 +67,6 @@ - -