diff --git a/ComputeSharp.sln b/ComputeSharp.sln
index 987e7491..b39cce02 100644
--- a/ComputeSharp.sln
+++ b/ComputeSharp.sln
@@ -484,6 +484,8 @@ Global
src\TerraFX.Interop.Windows\TerraFX.Interop.Windows.projitems*{314eb353-e7e3-4146-8111-685cc83ec1de}*SharedItemsImports = 5
tests\ComputeSharp.D2D1.UI.Tests\ComputeSharp.D2D1.UI.Tests.projitems*{346aeb10-a4ea-427e-8578-f60915db3053}*SharedItemsImports = 13
samples\ComputeSharp.SwapChain.Shaders.D2D1.Shared\ComputeSharp.SwapChain.Shaders.D2D1.Shared.projitems*{4d4bb2f6-5653-4db5-a8dd-90d58d8fe4d3}*SharedItemsImports = 4
+ src\ComputeSharp.NetStandard\ComputeSharp.NetStandard.projitems*{4d4bb2f6-5653-4db5-a8dd-90d58d8fe4d3}*SharedItemsImports = 4
+ src\TerraFX.Interop.Windows.D2D1\TerraFX.Interop.Windows.D2D1.projitems*{4d4bb2f6-5653-4db5-a8dd-90d58d8fe4d3}*SharedItemsImports = 4
tests\ComputeSharp.D2D1.UI.Tests\ComputeSharp.D2D1.UI.Tests.projitems*{4d4bb2f6-5653-4db5-a8dd-90d58d8fe4d3}*SharedItemsImports = 4
src\TerraFX.Interop.Windows.D2D1\TerraFX.Interop.Windows.D2D1.projitems*{4d7d9ba8-a821-4b1c-9ee2-cbcf5849906e}*SharedItemsImports = 5
src\TerraFX.Interop.Windows\TerraFX.Interop.Windows.projitems*{514ff00e-bbf0-4d55-a47c-eeeb6208adce}*SharedItemsImports = 13
diff --git a/tests/ComputeSharp.D2D1.UI.Tests/ComputeSharp.D2D1.UI.Tests.projitems b/tests/ComputeSharp.D2D1.UI.Tests/ComputeSharp.D2D1.UI.Tests.projitems
index 3d54cb41..826aaefe 100644
--- a/tests/ComputeSharp.D2D1.UI.Tests/ComputeSharp.D2D1.UI.Tests.projitems
+++ b/tests/ComputeSharp.D2D1.UI.Tests/ComputeSharp.D2D1.UI.Tests.projitems
@@ -9,6 +9,7 @@
ComputeSharp.D2D1.UI.Tests
+
diff --git a/tests/ComputeSharp.D2D1.UI.Tests/Helpers/TolerantImageComparer.cs b/tests/ComputeSharp.D2D1.UI.Tests/Helpers/TolerantImageComparer.cs
index fd008045..60cb3c99 100644
--- a/tests/ComputeSharp.D2D1.UI.Tests/Helpers/TolerantImageComparer.cs
+++ b/tests/ComputeSharp.D2D1.UI.Tests/Helpers/TolerantImageComparer.cs
@@ -1,10 +1,10 @@
using System;
using System.Collections.Generic;
+using System.Drawing;
using System.Text;
using System.Runtime.CompilerServices;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Graphics.Canvas;
-using System.Drawing;
using Color = Windows.UI.Color;
namespace ComputeSharp.D2D1.UI.Tests.Helpers;
diff --git a/tests/ComputeSharp.D2D1.UI.Tests/Helpers/Win2DHelper.cs b/tests/ComputeSharp.D2D1.UI.Tests/Helpers/Win2DHelper.cs
new file mode 100644
index 00000000..653fbfbd
--- /dev/null
+++ b/tests/ComputeSharp.D2D1.UI.Tests/Helpers/Win2DHelper.cs
@@ -0,0 +1,119 @@
+using System;
+using System.Runtime.InteropServices;
+using System.Runtime.InteropServices.WindowsRuntime;
+using Microsoft.Graphics.Canvas;
+using TerraFX.Interop.DirectX;
+using TerraFX.Interop.Windows;
+
+#nullable enable
+
+namespace ComputeSharp.D2D1.UI.Tests.Helpers;
+
+///
+/// A helper type to marshal managed objects in unit tests.
+///
+internal static class Win2DHelper
+{
+ ///
+ /// Gets the managed wrapper for a given object.
+ ///
+ /// The input object to get a wrapper for.
+ /// The realization instance to use.
+ /// The resulting managed wrapper for .
+ public static unsafe object GetOrCreate(ID2D1Image* d2D1Image, CanvasDevice? canvasDevice = null)
+ {
+ IActivationFactory activationFactory = WindowsRuntimeMarshal.GetActivationFactory(typeof(CanvasDevice));
+
+ using ComPtr activationFactoryUnknown = default;
+
+ activationFactoryUnknown.Attach((IUnknown*)Marshal.GetIUnknownForObject(activationFactory));
+
+ using ComPtr canvasFactoryNativeUnknown = default;
+
+ Guid uuidOfCanvasFactoryNativeUnknown = new("695C440D-04B3-4EDD-BFD9-63E51E9F7202");
+
+ //Get the ICanvasFactoryNative object
+ int hresult = activationFactoryUnknown.CopyTo(&uuidOfCanvasFactoryNativeUnknown, (void**)canvasFactoryNativeUnknown.GetAddressOf());
+
+ Marshal.ThrowExceptionForHR(hresult);
+
+ using ComPtr canvasDeviceUnknown = default;
+
+ if (canvasDevice is not null)
+ {
+ canvasDeviceUnknown.Attach((IUnknown*)Marshal.GetIUnknownForObject(canvasDevice));
+ }
+
+ using ComPtr canvasDeviceInterfaceUnknown = default;
+
+ if (canvasDevice is not null)
+ {
+ Guid uuidOfCanvasDeviceInterface = new("A27F0B5D-EC2C-4D4F-948F-0AA1E95E33E6");
+
+ // Get the ICanvasDevice object (as an IUnknown* as well)
+ hresult = canvasDeviceUnknown.CopyTo(&uuidOfCanvasDeviceInterface, (void**)canvasDeviceInterfaceUnknown.GetAddressOf());
+ }
+
+ using ComPtr wrapperUnknown = default;
+
+ // Invoke GetOrCreate to get the inspectable wrapper
+ hresult = ((delegate* unmanaged[Stdcall])(*(void***)canvasFactoryNativeUnknown.Get())[6])(
+ canvasFactoryNativeUnknown.Get(),
+ canvasDeviceInterfaceUnknown.Get(),
+ (IUnknown*)d2D1Image,
+ 0,
+ wrapperUnknown.GetAddressOf());
+
+ Marshal.ThrowExceptionForHR(hresult);
+
+ // Get or create a managed object for the native object
+ return Marshal.GetObjectForIUnknown((IntPtr)wrapperUnknown.Get());
+ }
+
+ ///
+ /// Gets the D2D image from a given object.
+ ///
+ /// The input object.
+ /// The realization instance to use.
+ /// The resulting object.
+ public static unsafe void GetD2DImage(ICanvasImage canvasImage, CanvasDevice canvasDevice, ID2D1Image** d2D1Image)
+ {
+ using ComPtr effectUnknown = default;
+
+ effectUnknown.Attach((IUnknown*)Marshal.GetIUnknownForObject(canvasImage));
+
+ using ComPtr canvasImageInteropUnknown = default;
+
+ Guid uuidOfCanvasImageInterop = new("E042D1F7-F9AD-4479-A713-67627EA31863");
+
+ // Get the ICanvasImageInterop object (as an IUnknown*)
+ int hresult = effectUnknown.CopyTo(&uuidOfCanvasImageInterop, (void**)canvasImageInteropUnknown.GetAddressOf());
+
+ Marshal.ThrowExceptionForHR(hresult);
+
+ using ComPtr canvasDeviceUnknown = default;
+
+ canvasDeviceUnknown.Attach((IUnknown*)Marshal.GetIUnknownForObject(canvasDevice));
+
+ using ComPtr canvasDeviceInterfaceUnknown = default;
+
+ Guid uuidOfCanvasDeviceInterface = new("A27F0B5D-EC2C-4D4F-948F-0AA1E95E33E6");
+
+ // Get the ICanvasDevice object (as an IUnknown* as well)
+ hresult = canvasDeviceUnknown.CopyTo(&uuidOfCanvasDeviceInterface, (void**)canvasDeviceInterfaceUnknown.GetAddressOf());
+
+ Marshal.ThrowExceptionForHR(hresult);
+
+ // Invoke GetD2DImage (passing NEVER_INSERT_DPI_COMPENSATION | MINIMAL_REALIZATION | ALLOW_NULL_EFFECT_INPUTS)
+ hresult = ((delegate* unmanaged[Stdcall])(*(void***)canvasImageInteropUnknown.Get())[4])(
+ canvasImageInteropUnknown.Get(),
+ canvasDeviceInterfaceUnknown.Get(),
+ null,
+ 4 | 8 | 16,
+ 0,
+ null,
+ d2D1Image);
+
+ Marshal.ThrowExceptionForHR(hresult);
+ }
+}
\ No newline at end of file
diff --git a/tests/ComputeSharp.D2D1.UI.Tests/PixelShaderEffectTests.cs b/tests/ComputeSharp.D2D1.UI.Tests/PixelShaderEffectTests.cs
index 47e06960..8176ff97 100644
--- a/tests/ComputeSharp.D2D1.UI.Tests/PixelShaderEffectTests.cs
+++ b/tests/ComputeSharp.D2D1.UI.Tests/PixelShaderEffectTests.cs
@@ -1,8 +1,12 @@
using System;
using System.Runtime.InteropServices;
+using ComputeSharp.D2D1.Interop;
+using ComputeSharp.D2D1.UI.Tests.Helpers;
using ComputeSharp.D2D1.Uwp;
using Microsoft.Graphics.Canvas;
using Microsoft.VisualStudio.TestTools.UnitTesting;
+using TerraFX.Interop.DirectX;
+using TerraFX.Interop.Windows;
namespace ComputeSharp.D2D1.UI.Tests;
@@ -164,6 +168,216 @@ public partial class PixelShaderEffectTests
Win2DHelper.GetD2DImage(effect5, new CanvasDevice(), d2D1Image.GetAddressOf());
}
+ [TestMethod]
+ public unsafe void Interop_EffectPropertiesAreForwardedCorrectly()
+ {
+ ShaderWithSomePropertiesAndInputs shader = new(-1234567);
+
+ PixelShaderEffect effect0 = new();
+ PixelShaderEffect effect1 = new()
+ {
+ ConstantBuffer = shader,
+ CacheOutput = true,
+ BufferPrecision = CanvasBufferPrecision.Precision32Float,
+ Sources = { [0] = effect0 }
+ };
+
+ CanvasDevice canvasDevice = new();
+
+ using ComPtr d2D1Image = default;
+
+ // Realize the effect and get the D2D effect (this will also apply all local properties)
+ Win2DHelper.GetD2DImage(effect1, canvasDevice, d2D1Image.GetAddressOf());
+
+ using ComPtr d2D1Effect = default;
+
+ Marshal.ThrowExceptionForHR(d2D1Image.CopyTo(d2D1Effect.GetAddressOf()));
+
+ int constantBufferDataSize = D2D1PixelShader.GetConstantBufferSize();
+ byte* constantBufferData = stackalloc byte[constantBufferDataSize];
+
+ int hresult = d2D1Effect.Get()->GetValue(
+ index: D2D1PixelShaderEffectProperty.ConstantBuffer,
+ type: D2D1_PROPERTY_TYPE.D2D1_PROPERTY_TYPE_BLOB,
+ data: constantBufferData,
+ dataSize: (uint)constantBufferDataSize);
+
+ Marshal.ThrowExceptionForHR(hresult);
+
+ ReadOnlyMemory expectedConstantBufferData = D2D1PixelShader.GetConstantBuffer(in shader);
+
+ Assert.IsTrue(expectedConstantBufferData.Span.SequenceEqual(new ReadOnlySpan(constantBufferData, constantBufferDataSize)));
+
+ int cacheOutput;
+
+ hresult = d2D1Effect.Get()->GetValue(
+ index: (uint)D2D1_PROPERTY.D2D1_PROPERTY_CACHED,
+ type: D2D1_PROPERTY_TYPE.D2D1_PROPERTY_TYPE_BOOL,
+ data: (byte*)&cacheOutput,
+ dataSize: sizeof(int));
+
+ Marshal.ThrowExceptionForHR(hresult);
+
+ Assert.AreEqual(1, cacheOutput);
+
+ D2D1_BUFFER_PRECISION d2D1BufferPrecision;
+
+ hresult = d2D1Effect.Get()->GetValue(
+ index: (uint)D2D1_PROPERTY.D2D1_PROPERTY_PRECISION,
+ type: D2D1_PROPERTY_TYPE.D2D1_PROPERTY_TYPE_UNKNOWN,
+ data: (byte*)&d2D1BufferPrecision,
+ dataSize: sizeof(D2D1_BUFFER_PRECISION));
+
+ Marshal.ThrowExceptionForHR(hresult);
+
+ Assert.AreEqual(D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_32BPC_FLOAT, d2D1BufferPrecision);
+
+ using ComPtr d2D1Image1 = default;
+ using ComPtr d2D1Image2 = default;
+
+ Win2DHelper.GetD2DImage(effect0, canvasDevice, d2D1Image1.GetAddressOf());
+
+ d2D1Effect.Get()->GetInput(0, d2D1Image2.GetAddressOf());
+
+ using ComPtr d2D1Image1Unknown = default;
+ using ComPtr d2D1Image2Unknown = default;
+
+ Marshal.ThrowExceptionForHR(d2D1Image1.CopyTo(d2D1Image1Unknown.GetAddressOf()));
+ Marshal.ThrowExceptionForHR(d2D1Image2.CopyTo(d2D1Image2Unknown.GetAddressOf()));
+
+ // The image from the effect input should be the same retrieved from the original source wrapper
+ Assert.IsTrue(d2D1Image1Unknown.Get() == d2D1Image2Unknown.Get());
+ }
+
+ [TestMethod]
+ public unsafe void Interop_EffectPropertiesAreUpdatedFromNativeEffectCorrectly()
+ {
+ ShaderWithSomePropertiesAndInputs shader = new(-1234567);
+
+ PixelShaderEffect effect0 = new();
+ PixelShaderEffect effect1 = new();
+
+ CanvasDevice canvasDevice = new();
+
+ using ComPtr d2D1Image0 = default;
+ using ComPtr d2D1Image1 = default;
+
+ // Realize the effects and get the D2D effects
+ Win2DHelper.GetD2DImage(effect0, canvasDevice, d2D1Image0.GetAddressOf());
+ Win2DHelper.GetD2DImage(effect1, canvasDevice, d2D1Image1.GetAddressOf());
+
+ using ComPtr d2D1Effect = default;
+
+ Marshal.ThrowExceptionForHR(d2D1Image1.CopyTo(d2D1Effect.GetAddressOf()));
+
+ D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(d2D1Effect.Get(), in shader);
+
+ int cacheOutput = 1;
+
+ int hresult = d2D1Effect.Get()->SetValue(
+ index: (uint)D2D1_PROPERTY.D2D1_PROPERTY_CACHED,
+ type: D2D1_PROPERTY_TYPE.D2D1_PROPERTY_TYPE_BOOL,
+ data: (byte*)&cacheOutput,
+ dataSize: sizeof(int));
+
+ Marshal.ThrowExceptionForHR(hresult);
+
+ Assert.AreEqual(1, cacheOutput);
+
+ D2D1_BUFFER_PRECISION d2D1BufferPrecision = D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_32BPC_FLOAT;
+
+ hresult = d2D1Effect.Get()->SetValue(
+ index: (uint)D2D1_PROPERTY.D2D1_PROPERTY_PRECISION,
+ type: D2D1_PROPERTY_TYPE.D2D1_PROPERTY_TYPE_UNKNOWN,
+ data: (byte*)&d2D1BufferPrecision,
+ dataSize: sizeof(D2D1_BUFFER_PRECISION));
+
+ Marshal.ThrowExceptionForHR(hresult);
+
+ d2D1Effect.Get()->SetInput(0, d2D1Image0.Get());
+
+ Assert.AreEqual(shader.Number, effect1.ConstantBuffer.Number);
+ Assert.IsTrue(effect1.CacheOutput);
+ Assert.AreEqual(CanvasBufferPrecision.Precision32Float, effect1.BufferPrecision);
+ Assert.AreSame(effect0, effect1.Sources[0]);
+ }
+
+ [TestMethod]
+ public unsafe void Interop_IsWrapperRegisteredCorrectly()
+ {
+ PixelShaderEffect effect = new();
+
+ using ComPtr d2D1Image = default;
+
+ Win2DHelper.GetD2DImage(effect, new CanvasDevice(), d2D1Image.GetAddressOf());
+
+ object wrapper = Win2DHelper.GetOrCreate(d2D1Image.Get());
+
+ Assert.AreSame(effect, wrapper);
+ }
+
+ [TestMethod]
+ public unsafe void Interop_IsWrapperFactoryRegisteredCorrectlyAndWorking()
+ {
+ PixelShaderEffect effect = new();
+ CanvasDevice canvasDevice1 = new();
+ CanvasDevice canvasDevice2 = new();
+
+ using ComPtr d2D1Image1 = default;
+ using ComPtr d2D1Image2 = default;
+
+ Win2DHelper.GetD2DImage(effect, canvasDevice1, d2D1Image1.GetAddressOf());
+ Win2DHelper.GetD2DImage(effect, canvasDevice2, d2D1Image2.GetAddressOf());
+
+ // The first resource is orphaned, so a new wrapper will be created
+ object wrapper1 = Win2DHelper.GetOrCreate(d2D1Image1.Get(), canvasDevice1);
+
+ Assert.IsTrue(wrapper1 is PixelShaderEffect);
+ Assert.AreNotSame(wrapper1, effect);
+
+ // Getting the same wrapper again should return the newly created one
+ object wrapper1FromCache = Win2DHelper.GetOrCreate(d2D1Image1.Get(), canvasDevice1);
+
+ Assert.AreSame(wrapper1, wrapper1FromCache);
+
+ using ComPtr d2D1Image1FromNewWrapper = default;
+
+ // Get the realized image from the newly created wrapper
+ Win2DHelper.GetD2DImage((ICanvasImage)wrapper1, canvasDevice1, d2D1Image1FromNewWrapper.GetAddressOf());
+
+ using ComPtr d2D1Image1Unknown = default;
+ using ComPtr d2D1Image1FromNewWrapperUnknown = default;
+
+ Marshal.ThrowExceptionForHR(d2D1Image1.CopyTo(d2D1Image1Unknown.GetAddressOf()));
+ Marshal.ThrowExceptionForHR(d2D1Image1FromNewWrapper.CopyTo(d2D1Image1FromNewWrapperUnknown.GetAddressOf()));
+
+ // The retrieved image should be the same one the new wrapper was created from
+ Assert.IsTrue(d2D1Image1Unknown.Get() == d2D1Image1FromNewWrapperUnknown.Get());
+
+ // The second resource is currently registered, so it returns the local wrapper
+ object wrapper2 = Win2DHelper.GetOrCreate(d2D1Image2.Get());
+
+ Assert.AreSame(effect, wrapper2);
+ }
+
+ [TestMethod]
+ [ExpectedException(typeof(ArgumentException))]
+ public unsafe void Interop_WrapperFactoryDetectsMismatchedDevices()
+ {
+ PixelShaderEffect effect = new();
+ CanvasDevice canvasDevice1 = new();
+ CanvasDevice canvasDevice2 = new();
+
+ using ComPtr d2D1Image1 = default;
+ using ComPtr d2D1Image2 = default;
+
+ Win2DHelper.GetD2DImage(effect, canvasDevice1, d2D1Image1.GetAddressOf());
+ Win2DHelper.GetD2DImage(effect, canvasDevice2, d2D1Image2.GetAddressOf());
+
+ // The factory should detect we're passing an incompatible device for the resource
+ _ = Win2DHelper.GetOrCreate(d2D1Image1.Get(), canvasDevice2);
+ }
+
[D2DInputCount(0)]
[AutoConstructor]
private partial struct ShaderWithSomeProperties : ID2D1PixelShader
@@ -180,6 +394,18 @@ public partial class PixelShaderEffectTests
}
}
+ [D2DInputCount(1)]
+ [AutoConstructor]
+ private partial struct ShaderWithSomePropertiesAndInputs : ID2D1PixelShader
+ {
+ public int Number;
+
+ public float4 Execute()
+ {
+ return D2D.GetInput(0);
+ }
+ }
+
[D2DInputCount(0)]
private partial struct ShaderWith0Inputs : ID2D1PixelShader
{
diff --git a/tests/ComputeSharp.D2D1.Uwp.Tests/ComputeSharp.D2D1.Uwp.Tests.csproj b/tests/ComputeSharp.D2D1.Uwp.Tests/ComputeSharp.D2D1.Uwp.Tests.csproj
index 797c5c22..041635c7 100644
--- a/tests/ComputeSharp.D2D1.Uwp.Tests/ComputeSharp.D2D1.Uwp.Tests.csproj
+++ b/tests/ComputeSharp.D2D1.Uwp.Tests/ComputeSharp.D2D1.Uwp.Tests.csproj
@@ -23,7 +23,7 @@
true
bin\ARM64\Debug\
DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP
- ;2008
+ $(NoWarn);2008
full
ARM64
false
@@ -35,7 +35,7 @@
bin\ARM64\Release\
TRACE;NETFX_CORE;WINDOWS_UWP
true
- ;2008
+ $(NoWarn);2008
pdbonly
ARM64
false
@@ -47,7 +47,7 @@
true
bin\x64\Debug\
DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP
- ;2008
+ $(NoWarn);2008
full
x64
false
@@ -58,7 +58,7 @@
bin\x64\Release\
TRACE;NETFX_CORE;WINDOWS_UWP
true
- ;2008
+ $(NoWarn);2008
pdbonly
x64
false
@@ -137,6 +137,10 @@
2.2.7
+
+ 1.13.1
+ all
+
@@ -147,6 +151,8 @@
+
+
14.0
diff --git a/tests/Directory.Build.props b/tests/Directory.Build.props
index d3430177..7fe8533e 100644
--- a/tests/Directory.Build.props
+++ b/tests/Directory.Build.props
@@ -6,6 +6,9 @@
false
+
+ $(NoWarn);CS0649
+
$(NoWarn);CS1591