From 7576c506e9bbba2080d03ea98a4b7c83bf8ba081 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 19 Apr 2022 00:41:32 +0200 Subject: [PATCH] Code refactoring to D2D1 unit tests --- .../ComputeSharp.D2D1.Tests/EndToEndTests.cs | 2 +- .../Helpers/D2D1Helper.cs | 220 ++++++++++++++++++ .../Helpers/D2D1ShaderTestHelper.cs | 177 -------------- .../Helpers/D2D1TestRunner.cs | 53 +++++ 4 files changed, 274 insertions(+), 178 deletions(-) create mode 100644 tests/ComputeSharp.D2D1.Tests/Helpers/D2D1Helper.cs delete mode 100644 tests/ComputeSharp.D2D1.Tests/Helpers/D2D1ShaderTestHelper.cs create mode 100644 tests/ComputeSharp.D2D1.Tests/Helpers/D2D1TestRunner.cs diff --git a/tests/ComputeSharp.D2D1.Tests/EndToEndTests.cs b/tests/ComputeSharp.D2D1.Tests/EndToEndTests.cs index adff3beb..24c50f5c 100644 --- a/tests/ComputeSharp.D2D1.Tests/EndToEndTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/EndToEndTests.cs @@ -56,7 +56,7 @@ public class EndToEndTests string destinationPath = Path.Combine(temporaryPath, $"{destinationFileName}.png"); // Run the shader - D2D1ShaderTestHelper.ExecutePixelShaderAndCompareResults( + D2D1TestRunner.ExecutePixelShaderAndCompareResults( in shader, transformMapperFactory, originalPath, diff --git a/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1Helper.cs b/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1Helper.cs new file mode 100644 index 00000000..5a6b2ded --- /dev/null +++ b/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1Helper.cs @@ -0,0 +1,220 @@ +using ComputeSharp.D2D1.Tests.Extensions; +using TerraFX.Interop.DirectX; +using TerraFX.Interop.Windows; + +namespace ComputeSharp.D2D1.Tests.Helpers; + +/// +/// A that uses the D2D1 APIs to configure and run effects. +/// +internal static class D2D1Helper +{ + /// + /// Creates an instance. + /// + /// A new instance. + public static unsafe ComPtr CreateD2D1Factory2() + { + using ComPtr d2D1Factory2 = default; + + D2D1_FACTORY_OPTIONS d2D1FactoryOptions = default; + + // Create a Direct2D factory + DirectX.D2D1CreateFactory( + factoryType: D2D1_FACTORY_TYPE.D2D1_FACTORY_TYPE_SINGLE_THREADED, + riid: Windows.__uuidof(), + pFactoryOptions: &d2D1FactoryOptions, + ppIFactory: (void**)d2D1Factory2.GetAddressOf()).Assert(); + + return d2D1Factory2.Move(); + } + + /// + /// Creates an instance. + /// + /// The input instance to use to create the device. + /// A new instance. + public static unsafe ComPtr CreateD2D1Device(ID2D1Factory2* d2D1Factory2) + { + 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 d3DFeatureLevel; + + // Create the Direct3D 11 API device and context + DirectX.D3D11CreateDevice( + pAdapter: null, + DriverType: D3D_DRIVER_TYPE.D3D_DRIVER_TYPE_HARDWARE, + Software: HMODULE.NULL, + Flags: creationFlags, + pFeatureLevels: featureLevels, + FeatureLevels: 7, + SDKVersion: D3D11.D3D11_SDK_VERSION, + ppDevice: d3D11Device.GetAddressOf(), + pFeatureLevel: &d3DFeatureLevel, + ppImmediateContext: null).Assert(); + + using ComPtr dxgiDevice3 = default; + + // Get a DXGI device from the D3D11 device + d3D11Device.CopyTo(dxgiDevice3.GetAddressOf()).Assert(); + + using ComPtr d2D1Device = default; + + // Create a D2D1 device + d2D1Factory2->CreateDevice( + dxgiDevice: (IDXGIDevice*)dxgiDevice3.Get(), + d2dDevice: d2D1Device.GetAddressOf()); + + return d2D1Device.Move(); + } + + /// + /// Creates an instance. + /// + /// The input instance to use to create the context. + /// A new instance. + public static unsafe ComPtr CreateD2D1DeviceContext(ID2D1Device* d2D1Device) + { + using ComPtr d2D1DeviceContext = default; + + // Create a D2D1 device context + d2D1Device->CreateDeviceContext( + options: D2D1_DEVICE_CONTEXT_OPTIONS.D2D1_DEVICE_CONTEXT_OPTIONS_NONE, + deviceContext: d2D1DeviceContext.GetAddressOf()).Assert(); + + return d2D1DeviceContext.Move(); + } + + /// + /// Creates an instance. + /// + /// The input instance to use to create the bitmap source. + /// The input to use to create the bitmap source. + /// The input to set the source for. + /// A new instance. + public static unsafe ComPtr CreateD2D1BitmapAndSetAsSource(ID2D1DeviceContext* d2D1DeviceContext, IWICBitmap* wicBitmap, ID2D1Effect* d2D1Effect) + { + using ComPtr d2D1BitmapSource = default; + + // Create a source D2D1 bitmap from the WIC bitmap + d2D1DeviceContext->CreateBitmapFromWicBitmap( + wicBitmapSource: (IWICBitmapSource*)wicBitmap, + bitmap: d2D1BitmapSource.GetAddressOf()).Assert(); + + d2D1Effect->SetInput(0, (ID2D1Image*)d2D1BitmapSource.Get()); + + return d2D1BitmapSource.Move(); + } + + /// + /// Creates an instance. + /// + /// The input instance to use to create the bitmap source. + /// The width of the bitmap to create. + /// The height of the bitmap to create. + /// A new instance. + public static unsafe ComPtr CreateD2D1BitmapAndSetAsTarget(ID2D1DeviceContext* d2D1DeviceContext, uint width, uint height) + { + D2D_SIZE_U d2DSize; + d2DSize.width = width; + d2DSize.height = 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; + + using ComPtr d2D1Bitmap1Target = default; + + // Create a target D2D1 bitmap + d2D1DeviceContext->CreateBitmap( + size: d2DSize, + sourceData: null, + pitch: 0, + bitmapProperties: &d2DBitmapProperties1Target, + bitmap: (ID2D1Bitmap1**)d2D1Bitmap1Target.GetAddressOf()).Assert(); + + d2D1DeviceContext->SetTarget((ID2D1Image*)d2D1Bitmap1Target.Get()); + + return d2D1Bitmap1Target.Move(); + } + + /// + /// Creates an instance and copies data from a source bitmap. + /// + /// The input instance to use to create the bitmap. + /// The input to read data from. + /// The resulting for the bitmap. + /// A new instance. + public static unsafe ComPtr CreateD2D1Bitmap1Buffer(ID2D1DeviceContext* d2D1DeviceContext, ID2D1Bitmap* d2D1Bitmap, out D2D1_MAPPED_RECT d2D1MappedRect) + { + D2D_SIZE_U 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; + + using ComPtr d2D1Bitmap1Buffer = default; + + // Create a buffer D2D1 bitmap + d2D1DeviceContext->CreateBitmap( + size: d2DSize, + sourceData: null, + pitch: 0, + bitmapProperties: &d2DBitmapProperties1Buffer, + bitmap: d2D1Bitmap1Buffer.GetAddressOf()).Assert(); + + D2D_POINT_2U d2DPointDestination = default; + D2D_RECT_U d2DRectSource = new(0, 0, d2DSize.width, d2DSize.height); + + // Copy the image from the target to the readback bitmap + d2D1Bitmap1Buffer.Get()->CopyFromBitmap( + destPoint: &d2DPointDestination, + bitmap: d2D1Bitmap, + srcRect: &d2DRectSource); + + fixed (D2D1_MAPPED_RECT* d2D1MappedRectPtr = &d2D1MappedRect) + { + // Map the buffer bitmap + d2D1Bitmap1Buffer.Get()->Map( + options: D2D1_MAP_OPTIONS.D2D1_MAP_OPTIONS_READ, + mappedRect: d2D1MappedRectPtr).Assert(); + } + + return d2D1Bitmap1Buffer.Move(); + } + + /// + /// Draws a given effect onto a context. + /// + /// The input instance to use to draw the effect. + /// The input to draw. + public static unsafe void DrawEffect(ID2D1DeviceContext* d2D1DeviceContext, ID2D1Effect* d2D1Effect) + { + d2D1DeviceContext->BeginDraw(); + + // Draw the image with the effect + d2D1DeviceContext->DrawImage( + effect: d2D1Effect, + targetOffset: null, + imageRectangle: null, + interpolationMode: D2D1_INTERPOLATION_MODE.D2D1_INTERPOLATION_MODE_NEAREST_NEIGHBOR, + compositeMode: D2D1_COMPOSITE_MODE.D2D1_COMPOSITE_MODE_SOURCE_COPY); + + d2D1DeviceContext->EndDraw().Assert(); + } +} diff --git a/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1ShaderTestHelper.cs b/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1ShaderTestHelper.cs deleted file mode 100644 index cfa766ce..00000000 --- a/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1ShaderTestHelper.cs +++ /dev/null @@ -1,177 +0,0 @@ -using System; -using ComputeSharp.D2D1.Interop; -using ComputeSharp.D2D1.Tests.Extensions; -using TerraFX.Interop.DirectX; -using TerraFX.Interop.Windows; - -namespace ComputeSharp.D2D1.Tests.Helpers; - -/// -/// A test helper for D2D1 pixel shaders. -/// -internal static class D2D1ShaderTestHelper -{ - /// - /// Executes a pixel shader to produce an image. - /// - /// The shader type to execute. - /// The shader to run. - /// A custom factory for the effect. - /// The source path for the image to run the shader on. - /// The destination path for the result. - public static unsafe void ExecutePixelShaderAndCompareResults( - in T shader, - Func>? transformMapperFactory, - string sourcePath, - string destinationPath) - where T : unmanaged, ID2D1PixelShader - { - using ComPtr d2D1Factory2 = default; - - D2D1_FACTORY_OPTIONS d2D1FactoryOptions = default; - - // Create a Direct2D factory - DirectX.D2D1CreateFactory( - factoryType: D2D1_FACTORY_TYPE.D2D1_FACTORY_TYPE_SINGLE_THREADED, - riid: Windows.__uuidof(), - pFactoryOptions: &d2D1FactoryOptions, - ppIFactory: (void**)d2D1Factory2.GetAddressOf()).Assert(); - - 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 d3DFeatureLevel; - - // Create the Direct3D 11 API device and context - DirectX.D3D11CreateDevice( - pAdapter: null, - DriverType: D3D_DRIVER_TYPE.D3D_DRIVER_TYPE_HARDWARE, - Software: HMODULE.NULL, - Flags: creationFlags, - pFeatureLevels: featureLevels, - FeatureLevels: 7, - SDKVersion: D3D11.D3D11_SDK_VERSION, - ppDevice: d3D11Device.GetAddressOf(), - pFeatureLevel: &d3DFeatureLevel, - ppImmediateContext: null).Assert(); - - using ComPtr dxgiDevice3 = default; - - // Get a DXGI device from the D3D11 device - d3D11Device.CopyTo(dxgiDevice3.GetAddressOf()).Assert(); - - using ComPtr d2D1Device = default; - - // Create a D2D1 device - d2D1Factory2.Get()->CreateDevice( - dxgiDevice: (IDXGIDevice*)dxgiDevice3.Get(), - d2dDevice: d2D1Device.GetAddressOf()); - - using ComPtr d2D1DeviceContext = default; - - // Create a D2D1 device context - d2D1Device.Get()->CreateDeviceContext( - options: D2D1_DEVICE_CONTEXT_OPTIONS.D2D1_DEVICE_CONTEXT_OPTIONS_NONE, - deviceContext: d2D1DeviceContext.GetAddressOf()).Assert(); - - using ComPtr wicBitmap = WICHelper.LoadBitmapFromFile(sourcePath, out uint width, out uint height); - - D2D_SIZE_U d2DSize; - d2DSize.width = width; - d2DSize.height = 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; - - using ComPtr d2D1Bitmap1Target = default; - - // Create a target D2D1 bitmap - d2D1DeviceContext.Get()->CreateBitmap( - size: d2DSize, - sourceData: null, - pitch: 0, - bitmapProperties: &d2DBitmapProperties1Target, - bitmap: d2D1Bitmap1Target.GetAddressOf()).Assert(); - - d2D1DeviceContext.Get()->SetTarget((ID2D1Image*)d2D1Bitmap1Target.Get()); - - using ComPtr d2D1BitmapSource = default; - - // Create a source D2D1 bitmap from the WIC bitmap - d2D1DeviceContext.Get()->CreateBitmapFromWicBitmap( - wicBitmapSource: (IWICBitmapSource*)wicBitmap.Get(), - bitmap: d2D1BitmapSource.GetAddressOf()).Assert(); - - D2D1InteropServices.RegisterPixelShaderEffectForD2D1Factory1(d2D1Factory2.Get(), transformMapperFactory, out _); - - using ComPtr d2D1Effect = default; - - D2D1InteropServices.CreatePixelShaderEffectFromD2D1DeviceContext( - d2D1DeviceContext.Get(), - (void**)d2D1Effect.GetAddressOf()); - - D2D1InteropServices.SetConstantBufferForD2D1Effect(in shader, d2D1Effect.Get()); - - d2D1Effect.Get()->SetInput(0, (ID2D1Image*)d2D1BitmapSource.Get()); - - d2D1DeviceContext.Get()->BeginDraw(); - - // Draw the image with the effect - d2D1DeviceContext.Get()->DrawImage( - effect: d2D1Effect.Get(), - targetOffset: null, - imageRectangle: null, - interpolationMode: D2D1_INTERPOLATION_MODE.D2D1_INTERPOLATION_MODE_NEAREST_NEIGHBOR, - compositeMode: D2D1_COMPOSITE_MODE.D2D1_COMPOSITE_MODE_SOURCE_COPY); - - d2D1DeviceContext.Get()->EndDraw().Assert(); - - 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; - - using ComPtr d2D1Bitmap1Buffer = default; - - // Create a buffer D2D1 bitmap - d2D1DeviceContext.Get()->CreateBitmap( - size: d2DSize, - sourceData: null, - pitch: 0, - bitmapProperties: &d2DBitmapProperties1Buffer, - bitmap: d2D1Bitmap1Buffer.GetAddressOf()).Assert(); - - D2D_POINT_2U d2DPointDestination = default; - D2D_RECT_U d2DRectSource = new(0, 0, width, height); - - // Copy the image from the target to the readback bitmap - d2D1Bitmap1Buffer.Get()->CopyFromBitmap( - destPoint: &d2DPointDestination, - bitmap: (ID2D1Bitmap*)d2D1Bitmap1Target.Get(), - srcRect: &d2DRectSource); - - D2D1_MAPPED_RECT d2D1MappedRect; - - // Map the buffer bitmap - d2D1Bitmap1Buffer.Get()->Map( - options: D2D1_MAP_OPTIONS.D2D1_MAP_OPTIONS_READ, - mappedRect: &d2D1MappedRect).Assert(); - - // Save the image - WICHelper.SaveBitmapToFile(destinationPath, width, height, d2D1MappedRect.pitch, d2D1MappedRect.bits); - } -} diff --git a/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1TestRunner.cs b/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1TestRunner.cs new file mode 100644 index 00000000..6357a08a --- /dev/null +++ b/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1TestRunner.cs @@ -0,0 +1,53 @@ +using System; +using ComputeSharp.D2D1.Interop; +using TerraFX.Interop.DirectX; +using TerraFX.Interop.Windows; + +namespace ComputeSharp.D2D1.Tests.Helpers; + +/// +/// A test helper for D2D1 pixel shaders. +/// +internal static class D2D1TestRunner +{ + /// + /// Executes a pixel shader to produce an image. + /// + /// The shader type to execute. + /// The shader to run. + /// A custom factory for the effect. + /// The source path for the image to run the shader on. + /// The destination path for the result. + public static unsafe void ExecutePixelShaderAndCompareResults( + in T shader, + Func>? transformMapperFactory, + string sourcePath, + string destinationPath) + where T : unmanaged, ID2D1PixelShader + { + using ComPtr d2D1Factory2 = D2D1Helper.CreateD2D1Factory2(); + using ComPtr d2D1Device = D2D1Helper.CreateD2D1Device(d2D1Factory2.Get()); + using ComPtr d2D1DeviceContext = D2D1Helper.CreateD2D1DeviceContext(d2D1Device.Get()); + + D2D1InteropServices.RegisterPixelShaderEffectForD2D1Factory1(d2D1Factory2.Get(), transformMapperFactory, out _); + + using ComPtr d2D1Effect = default; + + D2D1InteropServices.CreatePixelShaderEffectFromD2D1DeviceContext( + d2D1DeviceContext.Get(), + (void**)d2D1Effect.GetAddressOf()); + + D2D1InteropServices.SetConstantBufferForD2D1Effect(in shader, d2D1Effect.Get()); + + using ComPtr wicBitmap = WICHelper.LoadBitmapFromFile(sourcePath, out uint width, out uint height); + using ComPtr d2D1BitmapSource = D2D1Helper.CreateD2D1BitmapAndSetAsSource(d2D1DeviceContext.Get(), wicBitmap.Get(), d2D1Effect.Get()); + using ComPtr d2D1BitmapTarget = D2D1Helper.CreateD2D1BitmapAndSetAsTarget(d2D1DeviceContext.Get(), width, height); + + D2D1Helper.DrawEffect(d2D1DeviceContext.Get(), d2D1Effect.Get()); + + using ComPtr d2D1Bitmap1Buffer = D2D1Helper.CreateD2D1Bitmap1Buffer(d2D1DeviceContext.Get(), d2D1BitmapTarget.Get(), out D2D1_MAPPED_RECT d2D1MappedRect); + + // Save the image + WICHelper.SaveBitmapToFile(destinationPath, width, height, d2D1MappedRect.pitch, d2D1MappedRect.bits); + } +}