Add D2DPixelShaderSourceGenerator incrementality tests

このコミットが含まれているのは:
Sergio Pedri committed 2024-01-04 01:47:31 +01:00
1 親 60e10e5bd9
コミット 913c1ec5f6
2ファイル変更
+290 -3

検索条件に一致するファイルはありません

+15 -3
ファイルの表示
@@ -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<EquatableArray<DiagnosticInfo>> diagnosticInfo = methodInfo.Select(static (item, _) => item.Diagnostcs);
IncrementalValuesProvider<D2D1PixelShaderSourceInfo> outputInfo = methodInfo.Select(static (item, _) => item with { Diagnostcs = default });
// Get the diagnostic incremental collection with filtering (same as in the D2D1 generator)
IncrementalValuesProvider<EquatableArray<DiagnosticInfo>> 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<D2D1PixelShaderSourceInfo> outputInfo =
methodInfo
.Select(static (item, _) => item with { Diagnostcs = default })
.WithTrackingName(WellKnownTrackingNames.Output);
// Output the diagnostics, if any
context.ReportDiagnostics(diagnosticInfo);
@@ -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<byte> 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<byte> MyShader();
}
"""";
CSharpGeneratorTest<D2DPixelShaderSourceGenerator>.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<byte> 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<byte> MyShader();
}
"""";
CSharpGeneratorTest<D2DPixelShaderSourceGenerator>.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<byte> 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<byte> MyShader();
}
"""";
CSharpGeneratorTest<D2DPixelShaderSourceGenerator>.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<byte> 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<byte> MyShader();
}
"""";
CSharpGeneratorTest<D2DPixelShaderSourceGenerator>.VerifyIncrementalSteps(
source,
updatedSource,
executeReason: IncrementalStepRunReason.Modified,
diagnosticsReason: IncrementalStepRunReason.Modified,
outputReason: IncrementalStepRunReason.Modified,
diagnosticsSourceReason: IncrementalStepRunReason.Removed,
sourceReason: IncrementalStepRunReason.Modified);
}
}