From 3e0e6ae426f184d2744200985a94c16576495639 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 11 Dec 2021 13:42:31 +0100 Subject: [PATCH 1/2] Add [Upload]Texture2D.Save overload for IBufferWriter --- .../IWICStreamExtensions.IBufferWriter.cs | 451 ++++++++++++++++++ ...ions.cs => IWICStreamExtensions.Stream.cs} | 2 +- .../Extensions/ReadBackTexture2DExtensions.cs | 16 + .../Extensions/Texture2DExtensions.cs | 20 + .../Graphics/Helpers/WICHelper.cs | 35 ++ 5 files changed, 523 insertions(+), 1 deletion(-) create mode 100644 src/ComputeSharp/Graphics/Extensions/Interop/IWICStreamExtensions.IBufferWriter.cs rename src/ComputeSharp/Graphics/Extensions/Interop/{IWICStreamExtensions.cs => IWICStreamExtensions.Stream.cs} (99%) diff --git a/src/ComputeSharp/Graphics/Extensions/Interop/IWICStreamExtensions.IBufferWriter.cs b/src/ComputeSharp/Graphics/Extensions/Interop/IWICStreamExtensions.IBufferWriter.cs new file mode 100644 index 00000000..71945a6c --- /dev/null +++ b/src/ComputeSharp/Graphics/Extensions/Interop/IWICStreamExtensions.IBufferWriter.cs @@ -0,0 +1,451 @@ +using System; +using System.Buffers; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Threading; +using TerraFX.Interop.Windows; +using static TerraFX.Interop.Windows.E; +using static TerraFX.Interop.Windows.IID; +using static TerraFX.Interop.Windows.S; +using static TerraFX.Interop.Windows.STATFLAG; +using static TerraFX.Interop.Windows.STG; +using static TerraFX.Interop.Windows.STGM; +using static TerraFX.Interop.Windows.STGTY; +#if NET6_0_OR_GREATER +using RuntimeHelpers = System.Runtime.CompilerServices.RuntimeHelpers; +#else +using RuntimeHelpers = ComputeSharp.NetStandard.System.Runtime.CompilerServices.RuntimeHelpers; +using UnmanagedCallersOnlyAttribute = ComputeSharp.NetStandard.System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute; +#endif + +namespace ComputeSharp.Graphics.Extensions; + +/// +internal static unsafe partial class IWICStreamExtensions +{ + /// + /// Initializes an input wrapping a given instance. + /// + /// The target object to initialize. + /// The input instance to wrap. + /// An value indicating the operation result. + public static HRESULT InitializeFromBufferWriter(this ref IWICStream stream, IBufferWriter destination) + { + using ComPtr streamWrapper = default; + + IBufferWriterWrapper.Create(destination, streamWrapper.GetAddressOf()); + + return stream.InitializeFromIStream((IStream*)streamWrapper.Get()); + } + + /// + /// A manual CCW implementation for an object wrapping an instance. + /// + internal unsafe partial struct IBufferWriterWrapper +#if NET6_0_OR_GREATER + : IUnknown.Interface +#endif + { +#if !NET6_0_OR_GREATER + /// + private delegate int QueryInterfaceDelegate(IBufferWriterWrapper* @this, Guid* riid, void** ppvObject); + + /// + private delegate uint AddRefDelegate(IBufferWriterWrapper* @this); + + /// + private delegate uint ReleaseDelegate(IBufferWriterWrapper* @this); + + /// + private delegate HRESULT ReadDelegate(IBufferWriterWrapper* @this, void* pv, uint cb, uint* pcbRead); + + /// + private delegate HRESULT WriteDelegate(IBufferWriterWrapper* @this, void* pv, uint cb, uint* pcbWritten); + + /// + private delegate HRESULT SeekDelegate(IBufferWriterWrapper* @this, LARGE_INTEGER dlibMove, uint dwOrigin, ULARGE_INTEGER* plibNewPosition); + + /// + private delegate HRESULT SetSizeDelegate(IBufferWriterWrapper* @this, ULARGE_INTEGER libNewSize); + + /// + private delegate HRESULT CopyToDelegate(IBufferWriterWrapper* @this, IStream* pstm, ULARGE_INTEGER cb, ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten); + + /// + private delegate HRESULT CommitDelegate(IBufferWriterWrapper* @this, uint grfCommitFlags); + + /// + private delegate HRESULT RevertDelegate(IBufferWriterWrapper* @this); + + /// + private delegate HRESULT LockRegionDelegate(IBufferWriterWrapper* @this, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, uint dwLockType); + + /// + private delegate HRESULT UnlockRegionDelegate(IBufferWriterWrapper* @this, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, uint dwLockType); + + /// + private delegate HRESULT StatDelegate(IBufferWriterWrapper* @this, STATSTG* pstatstg, uint grfStatFlag); + + /// + private delegate HRESULT CloneDelegate(IBufferWriterWrapper* @this, IStream** ppstm); + + /// + /// A cached instance wrapping . + /// + private static readonly QueryInterfaceDelegate QueryInterfaceWrapper = QueryInterface; + + /// + /// A cached instance wrapping . + /// + private static readonly AddRefDelegate AddRefWrapper = AddRef; + + /// + /// A cached instance wrapping . + /// + private static readonly ReleaseDelegate ReleaseWrapper = Release; + + /// + /// A cached instance wrapping . + /// + private static readonly ReadDelegate ReadWrapper = Read; + + /// + /// A cached instance wrapping . + /// + private static readonly WriteDelegate WriteWrapper = Write; + + /// + /// A cached instance wrapping . + /// + private static readonly SeekDelegate SeekWrapper = Seek; + + /// + /// A cached instance wrapping . + /// + private static readonly SetSizeDelegate SetSizeWrapper = SetSize; + + /// + /// A cached instance wrapping . + /// + private static readonly CopyToDelegate CopyToWrapper = CopyTo; + + /// + /// A cached instance wrapping . + /// + private static readonly CommitDelegate CommitWrapper = Commit; + + /// + /// A cached instance wrapping . + /// + private static readonly RevertDelegate RevertWrapper = Revert; + + /// + /// A cached instance wrapping . + /// + private static readonly LockRegionDelegate LockRegionWrapper = LockRegion; + + /// + /// A cached instance wrapping . + /// + private static readonly UnlockRegionDelegate UnlockRegionWrapper = UnlockRegion; + + /// + /// A cached instance wrapping . + /// + private static readonly StatDelegate StatWrapper = Stat; + + /// + /// A cached instance wrapping . + /// + private static readonly CloneDelegate CloneWrapper = Clone; +#endif + + /// + /// The shared vtable pointer for instances. + /// + private static readonly void** Vtbl = InitVtbl(); + + /// + /// Setups the vtable pointer for . + /// + /// The initialized vtable pointer for . + private static void** InitVtbl() + { + void** lpVtbl = (void**)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(IBufferWriterWrapper), sizeof(void*) * 14); + +#if NET6_0_OR_GREATER + lpVtbl[0] = (delegate* unmanaged)&QueryInterface; + lpVtbl[1] = (delegate* unmanaged)&AddRef; + lpVtbl[2] = (delegate* unmanaged)&Release; + lpVtbl[3] = (delegate* unmanaged)&Read; + lpVtbl[4] = (delegate* unmanaged)&Write; + lpVtbl[5] = (delegate* unmanaged)&Seek; + lpVtbl[6] = (delegate* unmanaged)&SetSize; + lpVtbl[7] = (delegate* unmanaged)&CopyTo; + lpVtbl[8] = (delegate* unmanaged)&Commit; + lpVtbl[9] = (delegate* unmanaged)&Revert; + lpVtbl[10] = (delegate* unmanaged)&LockRegion; + lpVtbl[11] = (delegate* unmanaged)&UnlockRegion; + lpVtbl[12] = (delegate* unmanaged)&Stat; + lpVtbl[13] = (delegate* unmanaged)&Clone; +#else + lpVtbl[0] = (void*)Marshal.GetFunctionPointerForDelegate(QueryInterfaceWrapper); + lpVtbl[1] = (void*)Marshal.GetFunctionPointerForDelegate(AddRefWrapper); + lpVtbl[2] = (void*)Marshal.GetFunctionPointerForDelegate(ReleaseWrapper); + lpVtbl[3] = (void*)Marshal.GetFunctionPointerForDelegate(ReadWrapper); + lpVtbl[4] = (void*)Marshal.GetFunctionPointerForDelegate(WriteWrapper); + lpVtbl[5] = (void*)Marshal.GetFunctionPointerForDelegate(SeekWrapper); + lpVtbl[6] = (void*)Marshal.GetFunctionPointerForDelegate(SetSizeWrapper); + lpVtbl[7] = (void*)Marshal.GetFunctionPointerForDelegate(CopyToWrapper); + lpVtbl[8] = (void*)Marshal.GetFunctionPointerForDelegate(CommitWrapper); + lpVtbl[9] = (void*)Marshal.GetFunctionPointerForDelegate(RevertWrapper); + lpVtbl[10] = (void*)Marshal.GetFunctionPointerForDelegate(LockRegionWrapper); + lpVtbl[11] = (void*)Marshal.GetFunctionPointerForDelegate(UnlockRegionWrapper); + lpVtbl[12] = (void*)Marshal.GetFunctionPointerForDelegate(StatWrapper); + lpVtbl[13] = (void*)Marshal.GetFunctionPointerForDelegate(CloneWrapper); +#endif + + return lpVtbl; + } + + /// + /// The vtable pointer for the current instance. + /// + public void** lpVtbl; + + /// + /// The current reference count for the object (from IUnknown). + /// + public volatile int referenceCount; + + /// + /// The to the captured . + /// + public GCHandle writerHandle; + + /// + /// Creates and initializes a new instance. + /// + /// The input instance to wrap. + /// The target instance to initialize. + public static void Create(IBufferWriter writer, IBufferWriterWrapper** streamWrapper) + { + IBufferWriterWrapper* @this = (IBufferWriterWrapper*)NativeMemory.Alloc((nuint)sizeof(IBufferWriterWrapper)); + + @this->lpVtbl = Vtbl; + @this->referenceCount = 1; + @this->writerHandle = GCHandle.Alloc(writer); + + *streamWrapper = @this; + } + + /// + /// Gets the captured instance. + /// + /// The captured instance + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private IBufferWriter GetWriter() + { + return Unsafe.As>(this.writerHandle.Target!); + } + +#if NET6_0_OR_GREATER + /// + public HRESULT QueryInterface(Guid* riid, void** ppvObject) + { + return ((delegate* unmanaged)lpVtbl[0])((IBufferWriterWrapper*)Unsafe.AsPointer(ref this), riid, ppvObject); + } + + /// + public uint AddRef() + { + return ((delegate* unmanaged)lpVtbl[1])((IBufferWriterWrapper*)Unsafe.AsPointer(ref this)); + } + + /// + public uint Release() + { + return ((delegate* unmanaged)lpVtbl[2])((IBufferWriterWrapper*)Unsafe.AsPointer(ref this)); + } +#endif + + /// + [UnmanagedCallersOnly] + private static int QueryInterface(IBufferWriterWrapper* @this, Guid* riid, void** ppvObject) + { + if (ppvObject is null) + { + return E_POINTER; + } + + if (riid->Equals(IID_IUnknown) || + riid->Equals(IID_ISequentialStream) || + riid->Equals(IID_IStream)) + { + _ = Interlocked.Increment(ref @this->referenceCount); + + *ppvObject = @this; + + return S_OK; + } + + *ppvObject = null; + + return E_NOINTERFACE; + } + + /// + [UnmanagedCallersOnly] + private static uint AddRef(IBufferWriterWrapper* @this) + { + return (uint)Interlocked.Increment(ref @this->referenceCount); + } + + /// + [UnmanagedCallersOnly] + private static uint Release(IBufferWriterWrapper* @this) + { + uint referenceCount = (uint)Interlocked.Decrement(ref @this->referenceCount); + + if (referenceCount == 0) + { + @this->writerHandle.Free(); + + NativeMemory.Free(@this); + } + + return referenceCount; + } + + /// + [UnmanagedCallersOnly] + private static HRESULT Read(IBufferWriterWrapper* @this, void* pv, uint cb, uint* pcbRead) + { + return STG_E_INVALIDFUNCTION; + } + + /// + [UnmanagedCallersOnly] + private static HRESULT Write(IBufferWriterWrapper* @this, void* pv, uint cb, uint* pcbWritten) + { + if (pv == null) + { + return E_POINTER; + } + + if (cb > int.MaxValue) + { + return E_INVALIDARG; + } + + if (pcbWritten != null) + { + *pcbWritten = 0; + } + + try + { + @this->GetWriter().Write(new ReadOnlySpan(pv, (int)cb)); + + if (pcbWritten != null) + { + *pcbWritten = cb; + } + + return S_OK; + } + catch (Exception ex) + { + return ex.HResult; + } + } + + /// + [UnmanagedCallersOnly] + private static HRESULT Seek(IBufferWriterWrapper* @this, LARGE_INTEGER dlibMove, uint dwOrigin, ULARGE_INTEGER* plibNewPosition) + { + return STG_E_INVALIDFUNCTION; + } + + /// + [UnmanagedCallersOnly] + private static HRESULT SetSize(IBufferWriterWrapper* @this, ULARGE_INTEGER libNewSize) + { + return STG_E_INVALIDFUNCTION; + } + + /// + [UnmanagedCallersOnly] + private static HRESULT CopyTo(IBufferWriterWrapper* @this, IStream* pstm, ULARGE_INTEGER cb, ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten) + { + return E_NOTIMPL; + } + + /// + [UnmanagedCallersOnly] + private static HRESULT Commit(IBufferWriterWrapper* @this, uint grfCommitFlags) + { + return S_OK; + } + + /// + [UnmanagedCallersOnly] + private static HRESULT Revert(IBufferWriterWrapper* @this) + { + return STG_E_INVALIDFUNCTION; + } + + /// + [UnmanagedCallersOnly] + private static HRESULT LockRegion(IBufferWriterWrapper* @this, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, uint dwLockType) + { + return STG_E_INVALIDFUNCTION; + } + + /// + [UnmanagedCallersOnly] + private static HRESULT UnlockRegion(IBufferWriterWrapper* @this, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, uint dwLockType) + { + return STG_E_INVALIDFUNCTION; + } + + /// + [UnmanagedCallersOnly] + private static HRESULT Stat(IBufferWriterWrapper* @this, STATSTG* pstatstg, uint grfStatFlag) + { + if (pstatstg == null) + { + return E_POINTER; + } + + *pstatstg = default; + + STATFLAG statFlag = (STATFLAG)grfStatFlag; + + if ((statFlag & (STATFLAG_DEFAULT | STATFLAG_NONAME | STATFLAG_NOOPEN)) != statFlag) + { + return E_INVALIDARG; + } + + try + { + pstatstg->grfMode = STGM_WRITE; + pstatstg->type = (uint)STGTY_STREAM; + pstatstg->cbSize.QuadPart = ulong.MaxValue; + + return S_OK; + } + catch (Exception ex) + { + return ex.HResult; + } + } + + /// + [UnmanagedCallersOnly] + private static HRESULT Clone(IBufferWriterWrapper* @this, IStream** ppstm) + { + return STG_E_INVALIDFUNCTION; + } + } +} diff --git a/src/ComputeSharp/Graphics/Extensions/Interop/IWICStreamExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Interop/IWICStreamExtensions.Stream.cs similarity index 99% rename from src/ComputeSharp/Graphics/Extensions/Interop/IWICStreamExtensions.cs rename to src/ComputeSharp/Graphics/Extensions/Interop/IWICStreamExtensions.Stream.cs index b2e0c83f..e5560e33 100644 --- a/src/ComputeSharp/Graphics/Extensions/Interop/IWICStreamExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Interop/IWICStreamExtensions.Stream.cs @@ -24,7 +24,7 @@ namespace ComputeSharp.Graphics.Extensions; /// /// A with extensions for the type. /// -internal static unsafe class IWICStreamExtensions +internal static unsafe partial class IWICStreamExtensions { /// /// Initializes an input wrapping a given instance. diff --git a/src/ComputeSharp/Graphics/Extensions/ReadBackTexture2DExtensions.cs b/src/ComputeSharp/Graphics/Extensions/ReadBackTexture2DExtensions.cs index 4cfc2758..5d949928 100644 --- a/src/ComputeSharp/Graphics/Extensions/ReadBackTexture2DExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/ReadBackTexture2DExtensions.cs @@ -1,4 +1,5 @@ using System; +using System.Buffers; using System.IO; using ComputeSharp.Graphics.Helpers; using ComputeSharp.Resources; @@ -97,4 +98,19 @@ public static class ReadBackTexture2DExtensions WICHelper.Instance.SaveTexture(texture.View, stream, format); } + + /// + /// Saves a texture to a specified buffer writer. + /// + /// The type of items to store in the texture. + /// The texture to save to an image. + /// The buffer writer to save the image to. + /// The target image format to use. + public static void Save(this ReadBackTexture2D texture, IBufferWriter writer, ImageFormat format) + where T : unmanaged + { + Guard.IsNotNull(writer, nameof(writer)); + + WICHelper.Instance.SaveTexture(texture.View, writer, format); + } } diff --git a/src/ComputeSharp/Graphics/Extensions/Texture2DExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Texture2DExtensions.cs index d365be74..5b599769 100644 --- a/src/ComputeSharp/Graphics/Extensions/Texture2DExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Texture2DExtensions.cs @@ -1,4 +1,5 @@ using System; +using System.Buffers; using System.Diagnostics.Contracts; using System.IO; using System.Runtime.InteropServices; @@ -584,4 +585,23 @@ public static class Texture2DExtensions WICHelper.Instance.SaveTexture(readback.View, stream, format); } + + /// + /// Saves a texture to a specified buffer writer. + /// + /// The type of items to store in the texture. + /// The texture to save to an image. + /// The buffer writer to save the image to. + /// The target image format to use. + public static void Save(this Texture2D texture, IBufferWriter writer, ImageFormat format) + where T : unmanaged + { + Guard.IsNotNull(writer, nameof(writer)); + + using ReadBackTexture2D readback = texture.GraphicsDevice.AllocateReadBackTexture2D(texture.Width, texture.Height); + + texture.CopyTo(readback); + + WICHelper.Instance.SaveTexture(readback.View, writer, format); + } } diff --git a/src/ComputeSharp/Graphics/Helpers/WICHelper.cs b/src/ComputeSharp/Graphics/Helpers/WICHelper.cs index d1d07e44..5a1ca4a3 100644 --- a/src/ComputeSharp/Graphics/Helpers/WICHelper.cs +++ b/src/ComputeSharp/Graphics/Helpers/WICHelper.cs @@ -1,4 +1,5 @@ using System; +using System.Buffers; using System.Diagnostics.Contracts; using System.IO; using System.Runtime.CompilerServices; @@ -429,6 +430,40 @@ internal sealed unsafe class WICHelper SaveTexture(texture, wicBitmapEncoder.Get(), containerGuid); } + /// + /// Saves a texture to a specified buffer writer. + /// + /// The type of items to store in the texture. + /// The texture data to save to a stream. + /// The target buffer writer to write to. + /// The target image format to use. + public void SaveTexture(TextureView2D texture, IBufferWriter writer, ImageFormat format) + where T : unmanaged + { + using ComPtr wicBitmapEncoder = default; + Guid containerGuid = WICFormatHelper.GetForFormat(format); + + // Create the image encoder + this.wicImagingFactory2.Get()->CreateEncoder( + &containerGuid, + null, + wicBitmapEncoder.GetAddressOf()).Assert(); + + using ComPtr wicStream = default; + + // Create and initialize a stream + this.wicImagingFactory2.Get()->CreateStream(wicStream.GetAddressOf()).Assert(); + + wicStream.Get()->InitializeFromBufferWriter(writer); + + // Initialize the encoder + wicBitmapEncoder.Get()->Initialize( + (IStream*)wicStream.Get(), + WICBitmapEncoderCacheOption.WICBitmapEncoderNoCache).Assert(); + + SaveTexture(texture, wicBitmapEncoder.Get(), containerGuid); + } + /// /// Saves a texture to a specified object. /// From f27d8256e4d71463337e6099ba568ffb958ad73b Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 11 Dec 2021 13:42:47 +0100 Subject: [PATCH 2/2] Add unit tests for new Save() overloads for IBufferWriter --- tests/ComputeSharp.Tests/ImagingTests.cs | 68 +++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/tests/ComputeSharp.Tests/ImagingTests.cs b/tests/ComputeSharp.Tests/ImagingTests.cs index 216083bc..867993c9 100644 --- a/tests/ComputeSharp.Tests/ImagingTests.cs +++ b/tests/ComputeSharp.Tests/ImagingTests.cs @@ -1,4 +1,5 @@ using System; +using System.Buffers; using System.Collections.Generic; using System.IO; using System.Reflection; @@ -14,6 +15,7 @@ using SixLabors.ImageSharp.PixelFormats; using ImageSharpRgba32 = SixLabors.ImageSharp.PixelFormats.Rgba32; using ImageSharpBgra32 = SixLabors.ImageSharp.PixelFormats.Bgra32; using ImageSharpL8 = SixLabors.ImageSharp.PixelFormats.L8; +using Microsoft.Toolkit.HighPerformance.Buffers; namespace ComputeSharp.Tests; @@ -329,6 +331,28 @@ public class ImagingTests TolerantImageComparer.AssertEqual(expectedPath, actualPath, 0.00001023f); } + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ReadOnlyTexture2D<,>))] + [Resource(typeof(ReadWriteTexture2D<,>))] + public void SaveRgba32AsJpeg_ToBufferWriter(Device device, Type textureType) + { + string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging"); + string expectedPath = Path.Combine(path, "city.jpg"); + string actualPath = Path.Combine(path, "city_rgba32_saved.jpg"); + + using Texture2D texture = device.Get().LoadTexture2D(textureType, typeof(string), expectedPath); + + using (ArrayPoolBufferWriter writer = new()) + { + texture.Save(writer, ImageFormat.Jpeg); + + File.WriteAllBytes(actualPath, writer.WrittenSpan.ToArray()); + } + + TolerantImageComparer.AssertEqual(expectedPath, actualPath, 0.00001023f); + } + [CombinatorialTestMethod] [AllDevices] [Resource(typeof(ReadOnlyTexture2D<,>))] @@ -374,6 +398,31 @@ public class ImagingTests TolerantImageComparer.AssertEqual(expectedPath, actualPath, 0.00001023f); } + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ReadOnlyTexture2D<,>))] + [Resource(typeof(ReadWriteTexture2D<,>))] + public void SaveRgba32AsJpeg_ToBufferWriter_WithReadBackTexture(Device device, Type textureType) + { + string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging"); + string expectedPath = Path.Combine(path, "city.jpg"); + string actualPath = Path.Combine(path, "city_rgba32_saved.jpg"); + + using Texture2D texture = device.Get().LoadTexture2D(textureType, typeof(string), expectedPath); + using ReadBackTexture2D readback = device.Get().AllocateReadBackTexture2D(texture.Width, texture.Height); + + texture.CopyTo(readback); + + using (ArrayPoolBufferWriter writer = new()) + { + readback.Save(writer, ImageFormat.Jpeg); + + File.WriteAllBytes(actualPath, writer.WrittenSpan.ToArray()); + } + + TolerantImageComparer.AssertEqual(expectedPath, actualPath, 0.00001023f); + } + [CombinatorialTestMethod] [Device(Device.Warp)] [Resource(typeof(ReadOnlyTexture2D<,>))] @@ -405,7 +454,24 @@ public class ImagingTests texture.CopyTo(readback); - readback.Save(null!, ImageFormat.Jpeg); + readback.Save((Stream)null!, ImageFormat.Jpeg); + } + + [CombinatorialTestMethod] + [Device(Device.Warp)] + [Resource(typeof(ReadOnlyTexture2D<,>))] + [Resource(typeof(ReadWriteTexture2D<,>))] + [ExpectedException(typeof(ArgumentNullException))] + public void SaveRgba32AsJpeg_ToBufferWriter_WithReadBackTexture_WithNullBufferWriter(Device device, Type textureType) + { + string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg"); + + using Texture2D texture = device.Get().LoadTexture2D(textureType, typeof(string), path); + using ReadBackTexture2D readback = device.Get().AllocateReadBackTexture2D(texture.Width, texture.Height); + + texture.CopyTo(readback); + + readback.Save((IBufferWriter)null!, ImageFormat.Jpeg); } [CombinatorialTestMethod]