From d4d813ca94401667d806032d51d59ad40a2f9f0a Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 19 Sep 2022 14:21:48 +0200 Subject: [PATCH 1/5] Rename HResultExtensions to HRESULTExtensions --- .../Extensions/{HResultExtensions.cs => HRESULTExtensions.cs} | 2 +- .../Interop/{HResultExtensions.cs => HRESULTExtensions.cs} | 2 +- .../Interop/{HResultExtensions.cs => HRESULTExtensions.cs} | 2 +- .../Extensions/{HResultExtensions.cs => HRESULTExtensions.cs} | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) rename src/ComputeSharp.D2D1/Extensions/{HResultExtensions.cs => HRESULTExtensions.cs} (97%) rename src/ComputeSharp.SourceGenerators/ComputeSharp/Graphics/Extensions/Interop/{HResultExtensions.cs => HRESULTExtensions.cs} (96%) rename src/ComputeSharp/Graphics/Extensions/Interop/{HResultExtensions.cs => HRESULTExtensions.cs} (98%) rename tests/ComputeSharp.D2D1.Tests/Extensions/{HResultExtensions.cs => HRESULTExtensions.cs} (97%) diff --git a/src/ComputeSharp.D2D1/Extensions/HResultExtensions.cs b/src/ComputeSharp.D2D1/Extensions/HRESULTExtensions.cs similarity index 97% rename from src/ComputeSharp.D2D1/Extensions/HResultExtensions.cs rename to src/ComputeSharp.D2D1/Extensions/HRESULTExtensions.cs index 1d5ef42c..6ed9aa4e 100644 --- a/src/ComputeSharp.D2D1/Extensions/HResultExtensions.cs +++ b/src/ComputeSharp.D2D1/Extensions/HRESULTExtensions.cs @@ -11,7 +11,7 @@ namespace ComputeSharp.D2D1.Extensions; /// /// Trimmed down version of the same file in ComputeSharp. [DebuggerStepThrough] -internal static class HResultExtensions +internal static class HRESULTExtensions { /// /// Throws a if represents an error. diff --git a/src/ComputeSharp.SourceGenerators/ComputeSharp/Graphics/Extensions/Interop/HResultExtensions.cs b/src/ComputeSharp.SourceGenerators/ComputeSharp/Graphics/Extensions/Interop/HRESULTExtensions.cs similarity index 96% rename from src/ComputeSharp.SourceGenerators/ComputeSharp/Graphics/Extensions/Interop/HResultExtensions.cs rename to src/ComputeSharp.SourceGenerators/ComputeSharp/Graphics/Extensions/Interop/HRESULTExtensions.cs index 8bcf0e29..bc125808 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeSharp/Graphics/Extensions/Interop/HResultExtensions.cs +++ b/src/ComputeSharp.SourceGenerators/ComputeSharp/Graphics/Extensions/Interop/HRESULTExtensions.cs @@ -8,7 +8,7 @@ namespace ComputeSharp.Core.Extensions; /// Helper methods to efficiently throw exceptions. /// [DebuggerStepThrough] -internal static class HResultExtensions +internal static class HRESULTExtensions { /// /// Throws a if represents an error. diff --git a/src/ComputeSharp/Graphics/Extensions/Interop/HResultExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Interop/HRESULTExtensions.cs similarity index 98% rename from src/ComputeSharp/Graphics/Extensions/Interop/HResultExtensions.cs rename to src/ComputeSharp/Graphics/Extensions/Interop/HRESULTExtensions.cs index 85b7f135..229c5ac4 100644 --- a/src/ComputeSharp/Graphics/Extensions/Interop/HResultExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Interop/HRESULTExtensions.cs @@ -12,7 +12,7 @@ namespace ComputeSharp.Core.Extensions; /// Helper methods to efficiently throw exceptions. /// [DebuggerStepThrough] -internal static class HResultExtensions +internal static class HRESULTExtensions { /// /// Throws a if represents an error. diff --git a/tests/ComputeSharp.D2D1.Tests/Extensions/HResultExtensions.cs b/tests/ComputeSharp.D2D1.Tests/Extensions/HRESULTExtensions.cs similarity index 97% rename from tests/ComputeSharp.D2D1.Tests/Extensions/HResultExtensions.cs rename to tests/ComputeSharp.D2D1.Tests/Extensions/HRESULTExtensions.cs index c0d233a6..c27c0f50 100644 --- a/tests/ComputeSharp.D2D1.Tests/Extensions/HResultExtensions.cs +++ b/tests/ComputeSharp.D2D1.Tests/Extensions/HRESULTExtensions.cs @@ -11,7 +11,7 @@ namespace ComputeSharp.D2D1.Tests.Extensions; /// /// Trimmed down version of the same file in ComputeSharp. [DebuggerStepThrough] -internal static class HResultExtensions +internal static class HRESULTExtensions { /// /// Throws a if represents an error. From b3a709b78bb6b9009c8aa5eb11f5b25e2704e41d Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 20 Sep 2022 15:03:07 +0200 Subject: [PATCH 2/5] Switch D2D1 unit tests to Vortice.Win32 --- .../ComputeSharp.D2D1.Tests.csproj | 2 +- .../D2D1PixelShaderEffectTests.cs | 4 +- .../D2D1ResourceTextureManagerTests.cs | 9 +- .../Effects/BokehBlurEffect.cs | 40 ++++--- .../Extensions/ComPtrExtensions.cs | 4 +- .../Extensions/HRESULTExtensions.cs | 3 +- .../Helpers/D2D1Helper.cs | 100 +++++++++++------- .../Helpers/D2D1TestRunner.cs | 6 +- 8 files changed, 102 insertions(+), 66 deletions(-) diff --git a/tests/ComputeSharp.D2D1.Tests/ComputeSharp.D2D1.Tests.csproj b/tests/ComputeSharp.D2D1.Tests/ComputeSharp.D2D1.Tests.csproj index 6f00c131..fc7d9be9 100644 --- a/tests/ComputeSharp.D2D1.Tests/ComputeSharp.D2D1.Tests.csproj +++ b/tests/ComputeSharp.D2D1.Tests/ComputeSharp.D2D1.Tests.csproj @@ -14,8 +14,8 @@ - + diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs index 63443f23..83119320 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs @@ -5,8 +5,8 @@ using ComputeSharp.D2D1.Interop; using ComputeSharp.D2D1.Tests.Effects; using ComputeSharp.D2D1.Tests.Helpers; using Microsoft.VisualStudio.TestTools.UnitTesting; -using TerraFX.Interop.DirectX; -using TerraFX.Interop.Windows; +using Win32; +using Win32.Graphics.Direct2D; namespace ComputeSharp.D2D1.Tests; diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureManagerTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureManagerTests.cs index 935f31a0..fb6fb057 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureManagerTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureManagerTests.cs @@ -10,13 +10,16 @@ using ComputeSharp.Tests.Helpers; using Microsoft.VisualStudio.TestTools.UnitTesting; using SixLabors.ImageSharp; using SixLabors.ImageSharp.PixelFormats; -using TerraFX.Interop.DirectX; -using TerraFX.Interop.Windows; +using Win32; +using Win32.Graphics.Direct2D; #pragma warning disable CS0649 namespace ComputeSharp.D2D1.Tests; +using HRESULT = Win32.HResult; +using D2D1_MAPPED_RECT = Win32.Graphics.Direct2D.MappedRect; + [TestClass] [TestCategory("D2D1ResourceTextureManager")] public partial class D2D1ResourceTextureManagerTests @@ -56,7 +59,7 @@ public partial class D2D1ResourceTextureManagerTests Guid uuidOfGarbage = Guid.NewGuid(); // Any other random QueryInterface should fail - Assert.AreEqual(E.E_NOINTERFACE, (int)resourceTextureManager.AsIID(&uuidOfGarbage, &garbage)); + Assert.AreEqual(HRESULT.NoInterface, resourceTextureManager.AsIID(&uuidOfGarbage, &garbage)); Assert.IsTrue(garbage.Get() is null); } diff --git a/tests/ComputeSharp.D2D1.Tests/Effects/BokehBlurEffect.cs b/tests/ComputeSharp.D2D1.Tests/Effects/BokehBlurEffect.cs index c19876d7..75534345 100644 --- a/tests/ComputeSharp.D2D1.Tests/Effects/BokehBlurEffect.cs +++ b/tests/ComputeSharp.D2D1.Tests/Effects/BokehBlurEffect.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; -using System.Drawing; using System.IO; using System.Numerics; using System.Runtime.CompilerServices; @@ -10,11 +9,22 @@ using ComputeSharp.D2D1; using ComputeSharp.D2D1.Interop; using ComputeSharp.D2D1.Tests.Extensions; using ComputeSharp.D2D1.Tests.Helpers; -using TerraFX.Interop.DirectX; -using TerraFX.Interop.Windows; +using Win32; +using Win32.Graphics.Direct2D; namespace ComputeSharp.BokehBlur.Processors; +using D2D1_BORDER_EDGE_MODE = Win32.Graphics.Direct2D.BorderEdgeMode; +using D2D1_BORDER_PROP = Win32.Graphics.Direct2D.BorderProp; +using D2D1_BUFFER_PRECISION = Win32.Graphics.Direct2D.BufferPrecision; +using D2D1_COMPOSITE_MODE = Win32.Graphics.Direct2D.Common.CompositeMode; +using D2D1_COMPOSITE_PROP = Win32.Graphics.Direct2D.CompositeProp; +using D2D1_INTERPOLATION_MODE = Win32.Graphics.Direct2D.InterpolationMode; +using D2D1_PROPERTY = Win32.Graphics.Direct2D.Property; +using D2D1_MAPPED_RECT = Win32.Graphics.Direct2D.MappedRect; +using D2D1_RENDERING_CONTROLS = Win32.Graphics.Direct2D.RenderingControls; +using D2D1 = Win32.Graphics.Direct2D.Apis; + /// /// A bokeh blur effect, using linearly separable convolutions in the complex space for better efficiency. /// @@ -298,7 +308,7 @@ public sealed partial class BokehBlurEffect // Set the buffer precision for the whole context to 32 bits per channel. This avoids // color banding issues due to intermediate buffers clamping to just 8 bits per channel. - d2D1RenderingControls.bufferPrecision = D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_32BPC_FLOAT; + d2D1RenderingControls.bufferPrecision = D2D1_BUFFER_PRECISION._32bpcFloat; d2D1DeviceContext.Get()->SetRenderingControls(&d2D1RenderingControls); @@ -334,31 +344,31 @@ public sealed partial class BokehBlurEffect // Create the border effect to clamp the input image d2D1DeviceContext.Get()->CreateEffect( - effectId: (Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(in CLSID.CLSID_D2D1Border)), + effectId: (Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(in D2D1.CLSID_D2D1Border)), effect: borderEffect.GetAddressOf()).Assert(); - D2D1_BORDER_EDGE_MODE d2D1BorderEdgeMode = D2D1_BORDER_EDGE_MODE.D2D1_BORDER_EDGE_MODE_CLAMP; + D2D1_BORDER_EDGE_MODE d2D1BorderEdgeMode = D2D1_BORDER_EDGE_MODE.Clamp; // Set the border mode to clamp on both axes - borderEffect.Get()->SetValue((uint)D2D1_BORDER_PROP.D2D1_BORDER_PROP_EDGE_MODE_X, (byte*)&d2D1BorderEdgeMode, sizeof(D2D1_BORDER_EDGE_MODE)).Assert(); - borderEffect.Get()->SetValue((uint)D2D1_BORDER_PROP.D2D1_BORDER_PROP_EDGE_MODE_Y, (byte*)&d2D1BorderEdgeMode, sizeof(D2D1_BORDER_EDGE_MODE)).Assert(); + borderEffect.Get()->SetValue((uint)D2D1_BORDER_PROP.EdgeModeX, (byte*)&d2D1BorderEdgeMode, sizeof(D2D1_BORDER_EDGE_MODE)).Assert(); + borderEffect.Get()->SetValue((uint)D2D1_BORDER_PROP.EdgeModeY, (byte*)&d2D1BorderEdgeMode, sizeof(D2D1_BORDER_EDGE_MODE)).Assert(); // If partials need to be summed, also create the composite effect if (numberOfComponents > 1) { d2D1DeviceContext.Get()->CreateEffect( - effectId: (Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(in CLSID.CLSID_D2D1Composite)), + effectId: (Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(in D2D1.CLSID_D2D1Composite)), effect: compositeEffect.GetAddressOf()).Assert(); - D2D1_BUFFER_PRECISION d2D1BufferPrecision = D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_32BPC_FLOAT; + D2D1_BUFFER_PRECISION d2D1BufferPrecision = D2D1_BUFFER_PRECISION._32bpcFloat; // Set the channel precision to 32 bits manually to avoid banding - compositeEffect.Get()->SetValue((uint)D2D1_PROPERTY.D2D1_PROPERTY_PRECISION, (byte*)&d2D1BufferPrecision, sizeof(D2D1_BUFFER_PRECISION)).Assert(); + compositeEffect.Get()->SetValue((uint)D2D1_PROPERTY.Precision, (byte*)&d2D1BufferPrecision, sizeof(D2D1_BUFFER_PRECISION)).Assert(); - D2D1_COMPOSITE_MODE d2D1CompositeMode = D2D1_COMPOSITE_MODE.D2D1_COMPOSITE_MODE_PLUS; + D2D1_COMPOSITE_MODE d2D1CompositeMode = D2D1_COMPOSITE_MODE.Plus; // Set the mode to plus - compositeEffect.Get()->SetValue((uint)D2D1_COMPOSITE_PROP.D2D1_COMPOSITE_PROP_MODE, (byte*)&d2D1CompositeMode, sizeof(D2D1_COMPOSITE_MODE)).Assert(); + compositeEffect.Get()->SetValue((uint)D2D1_COMPOSITE_PROP.Mode, (byte*)&d2D1CompositeMode, sizeof(D2D1_COMPOSITE_MODE)).Assert(); } ReadOnlyMemory pixels = ImageHelper.LoadBitmapFromFile(sourcePath, out uint width, out uint height); @@ -452,8 +462,8 @@ public sealed partial class BokehBlurEffect effect: inverseGammaExposureEffect.Get(), targetOffset: null, imageRectangle: null, - interpolationMode: D2D1_INTERPOLATION_MODE.D2D1_INTERPOLATION_MODE_NEAREST_NEIGHBOR, - compositeMode: D2D1_COMPOSITE_MODE.D2D1_COMPOSITE_MODE_SOURCE_COPY); + interpolationMode: D2D1_INTERPOLATION_MODE.NearestNeighbor, + compositeMode: D2D1_COMPOSITE_MODE.SourceCopy); d2D1DeviceContext.Get()->EndDraw().Assert(); diff --git a/tests/ComputeSharp.D2D1.Tests/Extensions/ComPtrExtensions.cs b/tests/ComputeSharp.D2D1.Tests/Extensions/ComPtrExtensions.cs index 27904fb6..93074a24 100644 --- a/tests/ComputeSharp.D2D1.Tests/Extensions/ComPtrExtensions.cs +++ b/tests/ComputeSharp.D2D1.Tests/Extensions/ComPtrExtensions.cs @@ -1,5 +1,5 @@ using System.Runtime.CompilerServices; -using TerraFX.Interop.Windows; +using Win32; namespace ComputeSharp.D2D1.Tests.Extensions; @@ -17,7 +17,7 @@ internal static class ComPtrExtensions /// The moved instance. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static unsafe ComPtr Move(this in ComPtr ptr) - where T : unmanaged, IUnknown.Interface + where T : unmanaged { ComPtr copy = default; diff --git a/tests/ComputeSharp.D2D1.Tests/Extensions/HRESULTExtensions.cs b/tests/ComputeSharp.D2D1.Tests/Extensions/HRESULTExtensions.cs index c27c0f50..a34085c7 100644 --- a/tests/ComputeSharp.D2D1.Tests/Extensions/HRESULTExtensions.cs +++ b/tests/ComputeSharp.D2D1.Tests/Extensions/HRESULTExtensions.cs @@ -2,10 +2,11 @@ using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; -using TerraFX.Interop.Windows; namespace ComputeSharp.D2D1.Tests.Extensions; +using HRESULT = Win32.HResult; + /// /// Helper methods to efficiently throw exceptions. /// diff --git a/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1Helper.cs b/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1Helper.cs index cb2431f1..656e7961 100644 --- a/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1Helper.cs +++ b/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1Helper.cs @@ -1,10 +1,33 @@ using System; +using System.Drawing; using ComputeSharp.D2D1.Tests.Extensions; -using TerraFX.Interop.DirectX; -using TerraFX.Interop.Windows; +using Win32; +using Win32.Graphics.Direct2D; +using Win32.Graphics.Direct3D11; +using Win32.Graphics.Dxgi; namespace ComputeSharp.D2D1.Tests.Helpers; +using D2D1_ALPHA_MODE = Win32.Graphics.Direct2D.Common.AlphaMode; +using D2D1_BITMAP_OPTIONS = Win32.Graphics.Direct2D.BitmapOptions; +using D2D1_DEVICE_CONTEXT_OPTIONS = Win32.Graphics.Direct2D.DeviceContextOptions; +using D2D1_FACTORY_OPTIONS = Win32.Graphics.Direct2D.FactoryOptions; +using D2D1_FACTORY_TYPE = Win32.Graphics.Direct2D.FactoryType; +using D2D1_INTERPOLATION_MODE = Win32.Graphics.Direct2D.InterpolationMode; +using D2D1_COMPOSITE_MODE = Win32.Graphics.Direct2D.Common.CompositeMode; +using D2D1_MAP_OPTIONS = Win32.Graphics.Direct2D.MapOptions; +using D2D_RECT_U = Win32.Graphics.Direct2D.Common.RectU; +using D2D1_MAPPED_RECT = Win32.Graphics.Direct2D.MappedRect; +using D2D1_BITMAP_PROPERTIES = Win32.Graphics.Direct2D.BitmapProperties; +using D2D1_BITMAP_PROPERTIES1 = Win32.Graphics.Direct2D.BitmapProperties1; +using D3D_FEATURE_LEVEL = Win32.Graphics.Direct3D.FeatureLevel; +using D3D_DRIVER_TYPE = Win32.Graphics.Direct3D.DriverType; +using D3D11_CREATE_DEVICE_FLAG = Win32.Graphics.Direct3D11.CreateDeviceFlags; +using DXGI_FORMAT = Win32.Graphics.Dxgi.Common.Format; +using Win32 = Win32.Apis; +using D2D1 = Win32.Graphics.Direct2D.Apis; +using D3D11 = Win32.Graphics.Direct3D11.Apis; + /// /// A that uses the D2D1 APIs to configure and run effects. /// @@ -22,9 +45,9 @@ internal static class D2D1Helper D2D1_FACTORY_OPTIONS d2D1FactoryOptions = default; // Create a Direct2D factory - DirectX.D2D1CreateFactory( - factoryType: singleThreaded ? D2D1_FACTORY_TYPE.D2D1_FACTORY_TYPE_SINGLE_THREADED : D2D1_FACTORY_TYPE.D2D1_FACTORY_TYPE_MULTI_THREADED, - riid: Windows.__uuidof(), + D2D1.D2D1CreateFactory( + factoryType: singleThreaded ? D2D1_FACTORY_TYPE.SingleThreaded : D2D1_FACTORY_TYPE.MultiThreaded, + riid: Win32.__uuidof(), pFactoryOptions: &d2D1FactoryOptions, ppIFactory: (void**)d2D1Factory2.GetAddressOf()).Assert(); @@ -40,25 +63,24 @@ internal static class D2D1Helper { using ComPtr d3D11Device = default; - uint creationFlags = (uint)D3D11_CREATE_DEVICE_FLAG.D3D11_CREATE_DEVICE_BGRA_SUPPORT; D3D_FEATURE_LEVEL* featureLevels = stackalloc[] { - D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_11_1, - D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_11_0, - D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_10_1, - D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_10_0, - D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_9_3, - D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_9_2, - D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_9_1 + D3D_FEATURE_LEVEL.Level_11_1, + D3D_FEATURE_LEVEL.Level_11_0, + D3D_FEATURE_LEVEL.Level_10_1, + D3D_FEATURE_LEVEL.Level_10_0, + D3D_FEATURE_LEVEL.Level_9_3, + D3D_FEATURE_LEVEL.Level_9_2, + D3D_FEATURE_LEVEL.Level_9_1 }; D3D_FEATURE_LEVEL d3DFeatureLevel; // Create the Direct3D 11 API device and context - DirectX.D3D11CreateDevice( + D3D11.D3D11CreateDevice( pAdapter: null, - DriverType: D3D_DRIVER_TYPE.D3D_DRIVER_TYPE_HARDWARE, - Software: HMODULE.NULL, - Flags: creationFlags, + DriverType: D3D_DRIVER_TYPE.Hardware, + Software: IntPtr.Zero, + Flags: D3D11_CREATE_DEVICE_FLAG.BgraSupport, pFeatureLevels: featureLevels, FeatureLevels: 7, SDKVersion: D3D11.D3D11_SDK_VERSION, @@ -92,7 +114,7 @@ internal static class D2D1Helper // Create a D2D1 device context d2D1Device->CreateDeviceContext( - options: D2D1_DEVICE_CONTEXT_OPTIONS.D2D1_DEVICE_CONTEXT_OPTIONS_NONE, + options: D2D1_DEVICE_CONTEXT_OPTIONS.None, deviceContext: d2D1DeviceContext.GetAddressOf()).Assert(); return d2D1DeviceContext.Move(); @@ -116,13 +138,11 @@ internal static class D2D1Helper { using ComPtr d2D1BitmapSource = default; - D2D_SIZE_U d2DSize = default; - d2DSize.width = width; - d2DSize.height = height; + Size d2DSize = new((int)width, (int)height); D2D1_BITMAP_PROPERTIES d2DBitmapProperties = default; - d2DBitmapProperties.pixelFormat.format = DXGI_FORMAT.DXGI_FORMAT_B8G8R8A8_UNORM; - d2DBitmapProperties.pixelFormat.alphaMode = D2D1_ALPHA_MODE.D2D1_ALPHA_MODE_PREMULTIPLIED; + d2DBitmapProperties.pixelFormat.format = DXGI_FORMAT.B8G8R8A8Unorm; + d2DBitmapProperties.pixelFormat.alphaMode = D2D1_ALPHA_MODE.Premultiplied; d2DBitmapProperties.dpiX = 96; d2DBitmapProperties.dpiY = 96; @@ -151,16 +171,12 @@ internal static class D2D1Helper /// A new instance. public static unsafe ComPtr CreateD2D1BitmapAndSetAsTarget(ID2D1DeviceContext* d2D1DeviceContext, uint width, uint height) { - D2D_SIZE_U d2DSize; - d2DSize.width = width; - d2DSize.height = height; + Size d2DSize = new((int)width, (int)height); D2D1_BITMAP_PROPERTIES1 d2DBitmapProperties1Target = default; - d2DBitmapProperties1Target.pixelFormat.format = DXGI_FORMAT.DXGI_FORMAT_B8G8R8A8_UNORM; - d2DBitmapProperties1Target.pixelFormat.alphaMode = D2D1_ALPHA_MODE.D2D1_ALPHA_MODE_PREMULTIPLIED; - d2DBitmapProperties1Target.bitmapOptions = - D2D1_BITMAP_OPTIONS.D2D1_BITMAP_OPTIONS_TARGET | - D2D1_BITMAP_OPTIONS.D2D1_BITMAP_OPTIONS_CANNOT_DRAW; + d2DBitmapProperties1Target.pixelFormat.format = DXGI_FORMAT.B8G8R8A8Unorm; + d2DBitmapProperties1Target.pixelFormat.alphaMode = D2D1_ALPHA_MODE.Premultiplied; + d2DBitmapProperties1Target.bitmapOptions = D2D1_BITMAP_OPTIONS.Target | D2D1_BITMAP_OPTIONS.CannotDraw; using ComPtr d2D1Bitmap1Target = default; @@ -186,12 +202,12 @@ internal static class D2D1Helper /// A new instance. public static unsafe ComPtr CreateD2D1Bitmap1Buffer(ID2D1DeviceContext* d2D1DeviceContext, ID2D1Bitmap* d2D1Bitmap, out D2D1_MAPPED_RECT d2D1MappedRect) { - D2D_SIZE_U d2DSize = d2D1Bitmap->GetPixelSize(); + Size d2DSize = d2D1Bitmap->GetPixelSize(); D2D1_BITMAP_PROPERTIES1 d2DBitmapProperties1Buffer = default; - d2DBitmapProperties1Buffer.pixelFormat.format = DXGI_FORMAT.DXGI_FORMAT_B8G8R8A8_UNORM; - d2DBitmapProperties1Buffer.pixelFormat.alphaMode = D2D1_ALPHA_MODE.D2D1_ALPHA_MODE_PREMULTIPLIED; - d2DBitmapProperties1Buffer.bitmapOptions = D2D1_BITMAP_OPTIONS.D2D1_BITMAP_OPTIONS_CPU_READ | D2D1_BITMAP_OPTIONS.D2D1_BITMAP_OPTIONS_CANNOT_DRAW; + d2DBitmapProperties1Buffer.pixelFormat.format = DXGI_FORMAT.B8G8R8A8Unorm; + d2DBitmapProperties1Buffer.pixelFormat.alphaMode = D2D1_ALPHA_MODE.Premultiplied; + d2DBitmapProperties1Buffer.bitmapOptions = D2D1_BITMAP_OPTIONS.CpuRead | D2D1_BITMAP_OPTIONS.CannotDraw; using ComPtr d2D1Bitmap1Buffer = default; @@ -203,8 +219,12 @@ internal static class D2D1Helper bitmapProperties: &d2DBitmapProperties1Buffer, bitmap: d2D1Bitmap1Buffer.GetAddressOf()).Assert(); - D2D_POINT_2U d2DPointDestination = default; - D2D_RECT_U d2DRectSource = new(0, 0, d2DSize.width, d2DSize.height); + Point d2DPointDestination = default; + D2D_RECT_U d2DRectSource = default; + d2DRectSource.top = 0; + d2DRectSource.left = 0; + d2DRectSource.right = (uint)d2DSize.Width; + d2DRectSource.bottom = (uint)d2DSize.Height; // Copy the image from the target to the readback bitmap d2D1Bitmap1Buffer.Get()->CopyFromBitmap( @@ -216,7 +236,7 @@ internal static class D2D1Helper { // Map the buffer bitmap d2D1Bitmap1Buffer.Get()->Map( - options: D2D1_MAP_OPTIONS.D2D1_MAP_OPTIONS_READ, + options: D2D1_MAP_OPTIONS.Read, mappedRect: d2D1MappedRectPtr).Assert(); } @@ -237,8 +257,8 @@ internal static class D2D1Helper effect: d2D1Effect, targetOffset: null, imageRectangle: null, - interpolationMode: D2D1_INTERPOLATION_MODE.D2D1_INTERPOLATION_MODE_NEAREST_NEIGHBOR, - compositeMode: D2D1_COMPOSITE_MODE.D2D1_COMPOSITE_MODE_SOURCE_COPY); + interpolationMode: D2D1_INTERPOLATION_MODE.NearestNeighbor, + compositeMode: D2D1_COMPOSITE_MODE.SourceCopy); d2D1DeviceContext->EndDraw().Assert(); } diff --git a/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1TestRunner.cs b/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1TestRunner.cs index f18b683a..7b414e7d 100644 --- a/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1TestRunner.cs +++ b/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1TestRunner.cs @@ -4,11 +4,13 @@ using System.Reflection; using System.Runtime.CompilerServices; using ComputeSharp.D2D1.Interop; using ComputeSharp.Tests.Helpers; -using TerraFX.Interop.DirectX; -using TerraFX.Interop.Windows; +using Win32; +using Win32.Graphics.Direct2D; namespace ComputeSharp.D2D1.Tests.Helpers; +using D2D1_MAPPED_RECT = Win32.Graphics.Direct2D.MappedRect; + /// /// A test helper for D2D1 pixel shaders. /// From c3baa741167028fc1478c156a8bee792b37767f6 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 20 Sep 2022 15:15:37 +0200 Subject: [PATCH 3/5] Enable ComputeSharp.D2D1.Tests multi-targeting --- ...aderEffect.ResourceTextureManagerBuffer.cs | 13 +----------- .../Interop/Effects/PixelShaderEffect.cs | 3 +-- .../ComputeSharp.D2D1.Tests.csproj | 2 +- .../D2D1ResourceTextureManagerTests.cs | 12 ++++++++--- .../D2D1ShaderCompilerTests.cs | 20 +++++++++---------- .../Effects/BokehBlurEffect.cs | 6 +++--- .../Effects/ZonePlateEffect.cs | 7 +++++++ .../Extensions/HRESULTExtensions.cs | 4 ++++ 8 files changed, 36 insertions(+), 31 deletions(-) diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ResourceTextureManagerBuffer.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ResourceTextureManagerBuffer.cs index 6d11ba20..cb5509a7 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ResourceTextureManagerBuffer.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ResourceTextureManagerBuffer.cs @@ -1,5 +1,4 @@ -using System; -using System.Runtime.CompilerServices; +using System.Runtime.CompilerServices; using ComputeSharp.D2D1.Shaders.Interop.Effects.ResourceManagers; #pragma warning disable CS0649 @@ -109,15 +108,5 @@ partial struct PixelShaderEffect return ref @this[index]; } } - - /// - /// Gets a with the items in the current instance. - /// - /// A with the items in the current instance. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public Span AsSpan() - { - return new((ID2D1ResourceTextureManager**)Unsafe.AsPointer(ref this), 16); - } } } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs index 249f0544..b7f146d6 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs @@ -286,8 +286,7 @@ internal unsafe partial struct PixelShaderEffect @this->d2D1TransformMapperHandle = GCHandle.Alloc(d2D1TransformMapper); @this->d2D1DrawInfo = null; @this->d2D1EffectContext = null; - - @this->resourceTextureManagerBuffer.AsSpan().Clear(); + @this->resourceTextureManagerBuffer = default; *effectImpl = (IUnknown*)@this; diff --git a/tests/ComputeSharp.D2D1.Tests/ComputeSharp.D2D1.Tests.csproj b/tests/ComputeSharp.D2D1.Tests/ComputeSharp.D2D1.Tests.csproj index fc7d9be9..c6b0d625 100644 --- a/tests/ComputeSharp.D2D1.Tests/ComputeSharp.D2D1.Tests.csproj +++ b/tests/ComputeSharp.D2D1.Tests/ComputeSharp.D2D1.Tests.csproj @@ -1,7 +1,7 @@ - net6.0 + net472;netcoreapp3.1;net6.0 false $(NoWarn);CS0419;CA1416 diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureManagerTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureManagerTests.cs index fb6fb057..ee6a3673 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureManagerTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureManagerTests.cs @@ -588,7 +588,9 @@ public partial class D2D1ResourceTextureManagerTests data: texture, strides: null); - byte[] data = RandomNumberGenerator.GetBytes(updateLength); + byte[] data = new byte[updateLength]; + + RandomNumberGenerator.Create().GetBytes(data); resourceTextureManager.Update( minimumExtents: stackalloc uint[] { (uint)startOffset }, @@ -674,7 +676,9 @@ public partial class D2D1ResourceTextureManagerTests data: texture, strides: stackalloc uint[] { (uint)width }); - byte[] data = RandomNumberGenerator.GetBytes(updateLengthX * updateLengthY); + byte[] data = new byte[updateLengthX * updateLengthY]; + + RandomNumberGenerator.Create().GetBytes(data); resourceTextureManager.Update( minimumExtents: stackalloc uint[] { (uint)startOffsetX, (uint)startOffsetY }, @@ -775,7 +779,9 @@ public partial class D2D1ResourceTextureManagerTests data: texture, strides: stackalloc uint[] { (uint)width, (uint)(width * height) }); - byte[] data = RandomNumberGenerator.GetBytes(updateLengthX * updateLengthY * updateLengthZ); + byte[] data = new byte[updateLengthX * updateLengthY * updateLengthZ]; + + RandomNumberGenerator.Create().GetBytes(data); resourceTextureManager.Update( minimumExtents: stackalloc uint[] { (uint)startOffsetX, (uint)startOffsetY, (uint)startOffsetZ }, diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1ShaderCompilerTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1ShaderCompilerTests.cs index 0feb56b8..f2f0e6c5 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1ShaderCompilerTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1ShaderCompilerTests.cs @@ -27,8 +27,8 @@ public partial class D2D1ShaderCompilerTests """; ReadOnlyMemory bytecode = D2D1ShaderCompiler.Compile( - source, - "PSMain", + source.AsSpan(), + "PSMain".AsSpan(), D2D1ShaderProfile.PixelShader40Level93, D2D1CompileOptions.Default); @@ -79,8 +79,8 @@ public partial class D2D1ShaderCompilerTests """; ReadOnlyMemory bytecode = D2D1ShaderCompiler.Compile( - source, - "PSMain", + source.AsSpan(), + "PSMain".AsSpan(), D2D1ShaderProfile.PixelShader40Level93, D2D1CompileOptions.Default | D2D1CompileOptions.EnableLinking); @@ -106,8 +106,8 @@ public partial class D2D1ShaderCompilerTests """; ReadOnlyMemory bytecode = D2D1ShaderCompiler.Compile( - source, - "Execute", + source.AsSpan(), + "Execute".AsSpan(), D2D1ShaderProfile.PixelShader40Level93, D2D1CompileOptions.Default); @@ -133,8 +133,8 @@ public partial class D2D1ShaderCompilerTests """; ReadOnlyMemory bytecode = D2D1ShaderCompiler.Compile( - source, - "PSMain", + source.AsSpan(), + "PSMain".AsSpan(), D2D1ShaderProfile.PixelShader40Level93, D2D1CompileOptions.Default); @@ -181,8 +181,8 @@ public partial class D2D1ShaderCompilerTests """; ReadOnlyMemory bytecode = D2D1ShaderCompiler.Compile( - source, - "PSMain", + source.AsSpan(), + "PSMain".AsSpan(), D2D1ShaderProfile.PixelShader40, D2D1CompileOptions.Default); diff --git a/tests/ComputeSharp.D2D1.Tests/Effects/BokehBlurEffect.cs b/tests/ComputeSharp.D2D1.Tests/Effects/BokehBlurEffect.cs index 75534345..462281f4 100644 --- a/tests/ComputeSharp.D2D1.Tests/Effects/BokehBlurEffect.cs +++ b/tests/ComputeSharp.D2D1.Tests/Effects/BokehBlurEffect.cs @@ -325,9 +325,9 @@ public sealed partial class BokehBlurEffect using ComPtr compositeEffect = default; using ComPtr borderEffect = default; - Span> verticalConvolutionEffectsForReals = stackalloc ComPtr[numberOfComponents]; - Span> verticalConvolutionEffectsForImaginaries = stackalloc ComPtr[numberOfComponents]; - Span> horizontalConvolutionEffects = stackalloc ComPtr[numberOfComponents]; + Span> verticalConvolutionEffectsForReals = new ComPtr[numberOfComponents]; + Span> verticalConvolutionEffectsForImaginaries = new ComPtr[numberOfComponents]; + Span> horizontalConvolutionEffects = new ComPtr[numberOfComponents]; try { diff --git a/tests/ComputeSharp.D2D1.Tests/Effects/ZonePlateEffect.cs b/tests/ComputeSharp.D2D1.Tests/Effects/ZonePlateEffect.cs index c60296ed..af460bfd 100644 --- a/tests/ComputeSharp.D2D1.Tests/Effects/ZonePlateEffect.cs +++ b/tests/ComputeSharp.D2D1.Tests/Effects/ZonePlateEffect.cs @@ -2,6 +2,13 @@ namespace ComputeSharp.D2D1.Tests.Effects; +#if !NETCOREAPP3_1_OR_GREATER +internal static class MathF +{ + public const float PI = (float)Math.PI; +} +#endif + [D2DInputCount(0)] [D2DRequiresScenePosition] [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] diff --git a/tests/ComputeSharp.D2D1.Tests/Extensions/HRESULTExtensions.cs b/tests/ComputeSharp.D2D1.Tests/Extensions/HRESULTExtensions.cs index a34085c7..e0fc8004 100644 --- a/tests/ComputeSharp.D2D1.Tests/Extensions/HRESULTExtensions.cs +++ b/tests/ComputeSharp.D2D1.Tests/Extensions/HRESULTExtensions.cs @@ -1,6 +1,8 @@ using System.ComponentModel; using System.Diagnostics; +#if NETCOREAPP3_1_OR_GREATER using System.Diagnostics.CodeAnalysis; +#endif using System.Runtime.CompilerServices; namespace ComputeSharp.D2D1.Tests.Extensions; @@ -46,7 +48,9 @@ internal static class HRESULTExtensions /// Throws a . /// /// The input return code. +#if NETCOREAPP3_1_OR_GREATER [DoesNotReturn] +#endif private static void ThrowWin32Exception(int result) { throw new Win32Exception(result); From 6ebd5d9c16297053733da035bee98aceb3db5556 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 20 Sep 2022 15:47:28 +0200 Subject: [PATCH 4/5] Enable ComputeSharp.D2D1.Tests.AssemblyLevelAttributes multi-targeting --- .../ComputeSharp.D2D1.Tests.AssemblyLevelAttributes.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ComputeSharp.D2D1.Tests.AssemblyLevelAttributes/ComputeSharp.D2D1.Tests.AssemblyLevelAttributes.csproj b/tests/ComputeSharp.D2D1.Tests.AssemblyLevelAttributes/ComputeSharp.D2D1.Tests.AssemblyLevelAttributes.csproj index 89388c26..617f0b20 100644 --- a/tests/ComputeSharp.D2D1.Tests.AssemblyLevelAttributes/ComputeSharp.D2D1.Tests.AssemblyLevelAttributes.csproj +++ b/tests/ComputeSharp.D2D1.Tests.AssemblyLevelAttributes/ComputeSharp.D2D1.Tests.AssemblyLevelAttributes.csproj @@ -1,7 +1,7 @@ - net6.0 + net472;netcoreapp3.1;net6.0 false From 75d681654ec2a7806437b3d8fcc23c959d55bdee Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 20 Sep 2022 15:49:52 +0200 Subject: [PATCH 5/5] Enable multi-target tests for D2D1 projects in CI --- .github/workflows/dotnet.yml | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/.github/workflows/dotnet.yml b/.github/workflows/dotnet.yml index 11461bca..26b1f7d0 100644 --- a/.github/workflows/dotnet.yml +++ b/.github/workflows/dotnet.yml @@ -231,15 +231,31 @@ jobs: steps: - name: Git checkout uses: actions/checkout@v2 + - name: Setup .NET Core 3.1 SDK + uses: actions/setup-dotnet@v1 + with: + dotnet-version: 3.1.x - name: Setup .NET 6 SDK uses: actions/setup-dotnet@v1 with: dotnet-version: 6.0.x - - name: Run ComputeSharp.D2D1.Tests - run: dotnet test tests\ComputeSharp.D2D1.Tests\ComputeSharp.D2D1.Tests.csproj -c Release -v n -l "console;verbosity=detailed" + - name: Run ComputeSharp.D2D1.Tests (.NET 6) + run: dotnet test tests\ComputeSharp.D2D1.Tests\ComputeSharp.D2D1.Tests.csproj -c Release -f net6.0 -v n -l "console;verbosity=detailed" shell: cmd - - name: Run ComputeSharp.D2D1.Tests.AssemblyLevelAttributes - run: dotnet test tests\ComputeSharp.D2D1.Tests.AssemblyLevelAttributes\ComputeSharp.D2D1.Tests.AssemblyLevelAttributes.csproj -c Release -v n -l "console;verbosity=detailed" + - name: Run ComputeSharp.D2D1.Tests.AssemblyLevelAttributes (.NET 6) + run: dotnet test tests\ComputeSharp.D2D1.Tests.AssemblyLevelAttributes\ComputeSharp.D2D1.Tests.AssemblyLevelAttributes.csproj -c Release -f net6.0 -v n -l "console;verbosity=detailed" + shell: cmd + - name: Run ComputeSharp.D2D1.Tests (.NET Core 3.1) + run: dotnet test tests\ComputeSharp.D2D1.Tests\ComputeSharp.D2D1.Tests.csproj -c Release -f netcoreapp3.1 -v n -l "console;verbosity=detailed" + shell: cmd + - name: Run ComputeSharp.D2D1.Tests.AssemblyLevelAttributes (.NET Core 3.1) + run: dotnet test tests\ComputeSharp.D2D1.Tests.AssemblyLevelAttributes\ComputeSharp.D2D1.Tests.AssemblyLevelAttributes.csproj -c Release -f netcoreapp3.1 -v n -l "console;verbosity=detailed" + shell: cmd + - name: Run ComputeSharp.D2D1.Tests (.NET Framework 4.7.2) + run: dotnet test tests\ComputeSharp.D2D1.Tests\ComputeSharp.D2D1.Tests.csproj -c Release -f net472 -v n -l "console;verbosity=detailed" + shell: cmd + - name: Run ComputeSharp.D2D1.Tests.AssemblyLevelAttributes (.NET Framework 4.7.2) + run: dotnet test tests\ComputeSharp.D2D1.Tests.AssemblyLevelAttributes\ComputeSharp.D2D1.Tests.AssemblyLevelAttributes.csproj -c Release -f net472 -v n -l "console;verbosity=detailed" shell: cmd # Run all the local samples to ensure they build and run with no errors