From 8a6da6b30afe05c4d046f98ccc3b1c351e23cf1b Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 29 Apr 2022 02:05:59 +0200 Subject: [PATCH 1/3] Fix codegen for D2D1 shaders with no fields --- ...eInitializeFromDispatchDataMethod.Syntax.cs | 6 ++++++ ...ator.CreateLoadDispatchDataMethod.Syntax.cs | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateInitializeFromDispatchDataMethod.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateInitializeFromDispatchDataMethod.Syntax.cs index fcdb1248..9e6816a5 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateInitializeFromDispatchDataMethod.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateInitializeFromDispatchDataMethod.Syntax.cs @@ -44,6 +44,12 @@ partial class ID2D1ShaderGenerator /// The sequence of instances to initialize a shader from the serialized dispatch data buffer. private static ImmutableArray GetDispatchDataUnloadingStatements(ImmutableArray fieldInfos) { + // If there are no fields, just return no statements + if (fieldInfos.IsEmpty) + { + return ImmutableArray.Empty; + } + ImmutableArray.Builder statements = ImmutableArray.CreateBuilder(); // Insert the fallback for empty shaders. This will generate the following code: diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadDispatchDataMethod.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadDispatchDataMethod.Syntax.cs index 848298d9..c167d7b6 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadDispatchDataMethod.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadDispatchDataMethod.Syntax.cs @@ -45,6 +45,24 @@ partial class ID2D1ShaderGenerator /// The sequence of instances to load shader dispatch data. private static ImmutableArray GetDispatchDataLoadingStatements(ImmutableArray fieldInfos, int root32BitConstantsCount) { + // If there are no fields, just load an empty buffer + if (fieldInfos.IsEmpty) + { + // loader.LoadConstantBuffer(default); + return + ImmutableArray.Create( + ExpressionStatement( + InvocationExpression( + MemberAccessExpression( + SyntaxKind.SimpleMemberAccessExpression, + IdentifierName("loader"), + IdentifierName("LoadConstantBuffer"))) + .AddArgumentListArguments(Argument( + LiteralExpression( + SyntaxKind.DefaultLiteralExpression, + Token(SyntaxKind.DefaultKeyword)))))); + } + ImmutableArray.Builder statements = ImmutableArray.CreateBuilder(); // Generate loading statements for each captured field From 8af20e13b92ea70b8f1b6833a6fcfd11baa2e276 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 29 Apr 2022 02:09:38 +0200 Subject: [PATCH 2/3] Add unit tests for empty D2D1 shader --- .../Effects/InvertEffect.cs | 4 +--- .../Effects/InvertWithThresholdEffect.cs | 19 +++++++++++++++++++ .../ComputeSharp.D2D1.Tests/EndToEndTests.cs | 10 ++++++++-- 3 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 tests/ComputeSharp.D2D1.Tests/Effects/InvertWithThresholdEffect.cs diff --git a/tests/ComputeSharp.D2D1.Tests/Effects/InvertEffect.cs b/tests/ComputeSharp.D2D1.Tests/Effects/InvertEffect.cs index 14faec33..cf3c79d0 100644 --- a/tests/ComputeSharp.D2D1.Tests/Effects/InvertEffect.cs +++ b/tests/ComputeSharp.D2D1.Tests/Effects/InvertEffect.cs @@ -6,13 +6,11 @@ [AutoConstructor] public partial struct InvertEffect : ID2D1PixelShader { - public float number; - /// public float4 Execute() { float4 color = D2D.GetInput(0); - float3 rgb = Hlsl.Saturate(this.number - color.RGB); + float3 rgb = Hlsl.Saturate(1.0f - color.RGB); return new(rgb, 1); } diff --git a/tests/ComputeSharp.D2D1.Tests/Effects/InvertWithThresholdEffect.cs b/tests/ComputeSharp.D2D1.Tests/Effects/InvertWithThresholdEffect.cs new file mode 100644 index 00000000..6d98d350 --- /dev/null +++ b/tests/ComputeSharp.D2D1.Tests/Effects/InvertWithThresholdEffect.cs @@ -0,0 +1,19 @@ +namespace ComputeSharp.D2D1.Tests.Effects; + +[D2DInputCount(1)] +[D2DInputSimple(0)] +[D2DEmbeddedBytecode(D2D1ShaderProfile.PixelShader50)] +[AutoConstructor] +public partial struct InvertWithThresholdEffect : ID2D1PixelShader +{ + public float number; + + /// + public float4 Execute() + { + float4 color = D2D.GetInput(0); + float3 rgb = Hlsl.Saturate(this.number - color.RGB); + + return new(rgb, 1); + } +} diff --git a/tests/ComputeSharp.D2D1.Tests/EndToEndTests.cs b/tests/ComputeSharp.D2D1.Tests/EndToEndTests.cs index 851d4e07..47e91438 100644 --- a/tests/ComputeSharp.D2D1.Tests/EndToEndTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/EndToEndTests.cs @@ -14,9 +14,15 @@ namespace ComputeSharp.D2D1.Tests; public class EndToEndTests { [TestMethod] - public unsafe void InvertWithCustomThreshold() + public unsafe void Invert() { - RunAndCompareShader(new InvertEffect(1), null, "Landscape.png", "Landscape_Inverted.png"); + RunAndCompareShader(new InvertEffect(), null, "Landscape.png", "Landscape_Inverted.png"); + } + + [TestMethod] + public unsafe void InvertWithThreshold() + { + RunAndCompareShader(new InvertWithThresholdEffect(1), null, "Landscape.png", "Landscape_Inverted.png"); } [TestMethod] From bd64c50ce29ab7fd7f8798b3d8d09b9f679714ea Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 29 Apr 2022 02:10:11 +0200 Subject: [PATCH 3/3] Fix ID2D1Effect.LoadConstantBuffer crash with empty buffers --- .../Shaders/Dispatching/D2D1EffectDispatchDataLoader.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/ComputeSharp.D2D1/Shaders/Dispatching/D2D1EffectDispatchDataLoader.cs b/src/ComputeSharp.D2D1/Shaders/Dispatching/D2D1EffectDispatchDataLoader.cs index e5697042..69b06622 100644 --- a/src/ComputeSharp.D2D1/Shaders/Dispatching/D2D1EffectDispatchDataLoader.cs +++ b/src/ComputeSharp.D2D1/Shaders/Dispatching/D2D1EffectDispatchDataLoader.cs @@ -32,6 +32,11 @@ internal readonly unsafe struct D2D1EffectDispatchDataLoader : ID2D1DispatchData /// public void LoadConstantBuffer(ReadOnlySpan data) { + if (data.IsEmpty) + { + return; + } + this.d2D1Effect->SetValue( index: 0, type: D2D1_PROPERTY_TYPE.D2D1_PROPERTY_TYPE_BLOB,