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()); D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), transformMapperFactory, out _); using ComPtr d2D1Effect = default; D2D1PixelShaderEffect.CreateFromD2D1DeviceContext(d2D1DeviceContext.Get(), (void**)d2D1Effect.GetAddressOf()); D2D1PixelShaderEffect.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); } /// /// Executes a pixel shader to produce an image. /// /// The shader type to execute. /// The shader to run. /// The resulting width. /// The resulting height. /// A custom factory for the effect. /// The destination path for the result. public static unsafe void ExecutePixelShaderAndCompareResults( in T shader, Func>? transformMapperFactory, int width, int height, 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()); D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), transformMapperFactory, out _); using ComPtr d2D1Effect = default; D2D1PixelShaderEffect.CreateFromD2D1DeviceContext(d2D1DeviceContext.Get(), (void**)d2D1Effect.GetAddressOf()); D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(in shader, d2D1Effect.Get()); using ComPtr d2D1BitmapTarget = D2D1Helper.CreateD2D1BitmapAndSetAsTarget(d2D1DeviceContext.Get(), (uint)width, (uint)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, (uint)width, (uint)height, d2D1MappedRect.pitch, d2D1MappedRect.bits); } }