From 60e10e5bd9592adfd6e6a6e2ba050e8ee9948f57 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 4 Jan 2024 01:45:39 +0100 Subject: [PATCH 1/2] Fix D2D generator incrementality tests --- ...haderDescriptorGenerator_Incrementality.cs | 54 ++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator_Incrementality.cs b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator_Incrementality.cs index f2d97272..d1e44801 100644 --- a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator_Incrementality.cs +++ b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator_Incrementality.cs @@ -107,6 +107,7 @@ public class Test_D2DPixelShaderDescriptorGenerator_Incrementality using ComputeSharp; using ComputeSharp.D2D1; using float4 = global::ComputeSharp.Float4; + using double4 = global::ComputeSharp.Double4; [D2DInputCount(1)] [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] @@ -124,6 +125,7 @@ public class Test_D2DPixelShaderDescriptorGenerator_Incrementality using ComputeSharp; using ComputeSharp.D2D1; using float4 = global::ComputeSharp.Float4; + using double4 = global::ComputeSharp.Double4; [D2DInputCount(1)] [D2DRequiresDoublePrecisionSupport] @@ -144,7 +146,57 @@ public class Test_D2DPixelShaderDescriptorGenerator_Incrementality executeReason: IncrementalStepRunReason.Modified, diagnosticsReason: IncrementalStepRunReason.Modified, outputReason: IncrementalStepRunReason.Unchanged, - diagnosticsSourceReason: IncrementalStepRunReason.Modified, + diagnosticsSourceReason: IncrementalStepRunReason.Removed, + sourceReason: IncrementalStepRunReason.Cached); + } + + [TestMethod] + public void RemovedRequiresDoublePrecisionSupport_DoesNotModifyOutputAndProducesDiagnostics() + { + const string source = """ + using ComputeSharp; + using ComputeSharp.D2D1; + using float4 = global::ComputeSharp.Float4; + using double4 = global::ComputeSharp.Double4; + + [D2DInputCount(1)] + [D2DRequiresDoublePrecisionSupport] + [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] + [D2DGeneratedPixelShaderDescriptor] + internal readonly partial struct MyShader(double factor) : ID2D1PixelShader + { + public float4 Execute() + { + return (float4)((double4)D2D.GetInput(0) * factor); + } + } + """; + + const string updatedSource = """ + using ComputeSharp; + using ComputeSharp.D2D1; + using float4 = global::ComputeSharp.Float4; + using double4 = global::ComputeSharp.Double4; + + [D2DInputCount(1)] + [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] + [D2DGeneratedPixelShaderDescriptor] + internal readonly partial struct MyShader(double factor) : ID2D1PixelShader + { + public float4 Execute() + { + return (float4)((double4)D2D.GetInput(0) * factor); + } + } + """; + + CSharpGeneratorTest.VerifyIncrementalSteps( + source, + updatedSource, + executeReason: IncrementalStepRunReason.Modified, + diagnosticsReason: IncrementalStepRunReason.Modified, + outputReason: IncrementalStepRunReason.Unchanged, + diagnosticsSourceReason: IncrementalStepRunReason.New, sourceReason: IncrementalStepRunReason.Cached); } } \ No newline at end of file From 913c1ec5f6fc9117dd7a0dbaf4754719c5d0a2c9 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 4 Jan 2024 01:45:55 +0100 Subject: [PATCH 2/2] Add D2DPixelShaderSourceGenerator incrementality tests --- .../D2DPixelShaderSourceGenerator.cs | 18 +- ...xelShaderSourceGenerator_Incrementality.cs | 275 ++++++++++++++++++ 2 files changed, 290 insertions(+), 3 deletions(-) create mode 100644 tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator_Incrementality.cs diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs index ed19e18e..c26dfc4a 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs @@ -1,6 +1,7 @@ using System.Collections.Immutable; using System.Linq; using ComputeSharp.D2D1.SourceGenerators.Models; +using ComputeSharp.SourceGeneration.Constants; using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; using ComputeSharp.SourceGeneration.Models; @@ -77,10 +78,21 @@ public sealed partial class D2DPixelShaderSourceGenerator : IIncrementalGenerato HlslInfoKey: hlslInfoKey, HlslInfo: hlslInfo, Diagnostcs: diagnostics.ToImmutable()); - }); + }) + .WithTrackingName(WellKnownTrackingNames.Execute); - IncrementalValuesProvider> diagnosticInfo = methodInfo.Select(static (item, _) => item.Diagnostcs); - IncrementalValuesProvider outputInfo = methodInfo.Select(static (item, _) => item with { Diagnostcs = default }); + // Get the diagnostic incremental collection with filtering (same as in the D2D1 generator) + IncrementalValuesProvider> diagnosticInfo = + methodInfo + .Select(static (item, _) => item.Diagnostcs) + .WithTrackingName(WellKnownTrackingNames.Diagnostics) + .Where(static item => !item.IsEmpty); + + // Also get the comparison-friendly incremental collection for the output (same as above) + IncrementalValuesProvider outputInfo = + methodInfo + .Select(static (item, _) => item with { Diagnostcs = default }) + .WithTrackingName(WellKnownTrackingNames.Output); // Output the diagnostics, if any context.ReportDiagnostics(diagnosticInfo); diff --git a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator_Incrementality.cs b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator_Incrementality.cs new file mode 100644 index 00000000..94765751 --- /dev/null +++ b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator_Incrementality.cs @@ -0,0 +1,275 @@ +using ComputeSharp.D2D1.SourceGenerators; +using ComputeSharp.Tests.SourceGenerators.Helpers; +using Microsoft.CodeAnalysis; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace ComputeSharp.D2D1.Tests.SourceGenerators; + +[TestClass] +public class Test_D2DPixelShaderSourceGenerator_Incrementality +{ + [TestMethod] + public void ModifiedOptions_ModifiesOutput() + { + const string source = """" + using System; + using ComputeSharp; + using ComputeSharp.D2D1; + + partial class C + { + [D2DPixelShaderSource(""" + #define D2D_INPUT_COUNT 1 + #define D2D_INPUT0_SIMPLE + + #include "d2d1effecthelpers.hlsli" + + D2D_PS_ENTRY(Execute) + { + float4 color = D2DGetInput(0); + float3 rgb = saturate(1.0 - color.rgb); + return float4(rgb, 1); + } + """)] + [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] + [D2DCompileOptions(D2D1CompileOptions.OptimizationLevel0)] + static partial ReadOnlySpan MyShader(); + } + """"; + + const string updatedSource = """" + using System; + using ComputeSharp; + using ComputeSharp.D2D1; + + partial class C + { + [D2DPixelShaderSource(""" + #define D2D_INPUT_COUNT 1 + #define D2D_INPUT0_SIMPLE + + #include "d2d1effecthelpers.hlsli" + + D2D_PS_ENTRY(Execute) + { + float4 color = D2DGetInput(0); + float3 rgb = saturate(1.0 - color.rgb); + return float4(rgb, 1); + } + """)] + [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] + [D2DCompileOptions(D2D1CompileOptions.OptimizationLevel1)] + static partial ReadOnlySpan MyShader(); + } + """"; + + CSharpGeneratorTest.VerifyIncrementalSteps( + source, + updatedSource, + executeReason: IncrementalStepRunReason.Modified, + diagnosticsReason: null, + outputReason: IncrementalStepRunReason.Modified, + diagnosticsSourceReason: null, + sourceReason: IncrementalStepRunReason.Modified); + } + + [TestMethod] + public void AddedLeadingTrivia_DoesNotModifyOutput() + { + const string source = """" + using System; + using ComputeSharp; + using ComputeSharp.D2D1; + + partial class C + { + [D2DPixelShaderSource(""" + #define D2D_INPUT_COUNT 1 + #define D2D_INPUT0_SIMPLE + + #include "d2d1effecthelpers.hlsli" + + D2D_PS_ENTRY(Execute) + { + float4 color = D2DGetInput(0); + float3 rgb = saturate(1.0 - color.rgb); + return float4(rgb, 1); + } + """)] + [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] + [D2DCompileOptions(D2D1CompileOptions.OptimizationLevel0)] + static partial ReadOnlySpan MyShader(); + } + """"; + + const string updatedSource = """" + using System; + using ComputeSharp; + using ComputeSharp.D2D1; + + // Useless comment + partial class C + { + // Useless method + public static int AddOne(int x) => x + 1; + + // Hello + [D2DPixelShaderSource(""" + #define D2D_INPUT_COUNT 1 + #define D2D_INPUT0_SIMPLE + + #include "d2d1effecthelpers.hlsli" + + D2D_PS_ENTRY(Execute) + { + float4 color = D2DGetInput(0); + float3 rgb = saturate(1.0 - color.rgb); + return float4(rgb, 1); + } + """)] + [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] + [D2DCompileOptions(D2D1CompileOptions.OptimizationLevel0)] + static partial ReadOnlySpan MyShader(); + } + """"; + + CSharpGeneratorTest.VerifyIncrementalSteps( + source, + updatedSource, + executeReason: IncrementalStepRunReason.Unchanged, + diagnosticsReason: null, + outputReason: IncrementalStepRunReason.Cached, + diagnosticsSourceReason: null, + sourceReason: IncrementalStepRunReason.Cached); + } + + [TestMethod] + public void AddedCompileError_ModifiesOutputAndProducesDiagnostics() + { + const string source = """" + using System; + using ComputeSharp; + using ComputeSharp.D2D1; + + partial class C + { + [D2DPixelShaderSource(""" + #define D2D_INPUT_COUNT 1 + #define D2D_INPUT0_SIMPLE + + #include "d2d1effecthelpers.hlsli" + + D2D_PS_ENTRY(Execute) + { + float4 color = D2DGetInput(0); + float3 rgb = saturate(1.0 - color.rgb); + return float4(rgb, 1); + } + """)] + [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] + [D2DCompileOptions(D2D1CompileOptions.OptimizationLevel0)] + static partial ReadOnlySpan MyShader(); + } + """"; + + const string updatedSource = """" + using System; + using ComputeSharp; + using ComputeSharp.D2D1; + + partial class C + { + [D2DPixelShaderSource(""" + #define D2D_INPUT_COUNT 0 + #define D2D_INPUT0_SIMPLE + + #include "d2d1effecthelpers.hlsli" + + D2D_PS_ENTRY(Execute) + { + float4 color = D2DGetInput(0); + float3 rgb = saturate(1.0 - color.rgb); + return float4(rgb, 1); + } + """)] + [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] + [D2DCompileOptions(D2D1CompileOptions.OptimizationLevel0)] + static partial ReadOnlySpan MyShader(); + } + """"; + + CSharpGeneratorTest.VerifyIncrementalSteps( + source, + updatedSource, + executeReason: IncrementalStepRunReason.Modified, + diagnosticsReason: IncrementalStepRunReason.Modified, + outputReason: IncrementalStepRunReason.Modified, + diagnosticsSourceReason: IncrementalStepRunReason.New, + sourceReason: IncrementalStepRunReason.Modified); + } + + [TestMethod] + public void FixedCompileError_ModifiesOutputAndRemovesDiagnostics() + { + const string source = """" + using System; + using ComputeSharp; + using ComputeSharp.D2D1; + + partial class C + { + [D2DPixelShaderSource(""" + #define D2D_INPUT_COUNT 0 + #define D2D_INPUT0_SIMPLE + + #include "d2d1effecthelpers.hlsli" + + D2D_PS_ENTRY(Execute) + { + float4 color = D2DGetInput(0); + float3 rgb = saturate(1.0 - color.rgb); + return float4(rgb, 1); + } + """)] + [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] + [D2DCompileOptions(D2D1CompileOptions.OptimizationLevel0)] + static partial ReadOnlySpan MyShader(); + } + """"; + + const string updatedSource = """" + using System; + using ComputeSharp; + using ComputeSharp.D2D1; + + partial class C + { + [D2DPixelShaderSource(""" + #define D2D_INPUT_COUNT 1 + #define D2D_INPUT0_SIMPLE + + #include "d2d1effecthelpers.hlsli" + + D2D_PS_ENTRY(Execute) + { + float4 color = D2DGetInput(0); + float3 rgb = saturate(1.0 - color.rgb); + return float4(rgb, 1); + } + """)] + [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] + [D2DCompileOptions(D2D1CompileOptions.OptimizationLevel0)] + static partial ReadOnlySpan MyShader(); + } + """"; + + CSharpGeneratorTest.VerifyIncrementalSteps( + source, + updatedSource, + executeReason: IncrementalStepRunReason.Modified, + diagnosticsReason: IncrementalStepRunReason.Modified, + outputReason: IncrementalStepRunReason.Modified, + diagnosticsSourceReason: IncrementalStepRunReason.Removed, + sourceReason: IncrementalStepRunReason.Modified); + } +} \ No newline at end of file