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
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,
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]