Merge pull request #177 from Sergio0694/feature/stream-wic-interop
Add Stream overloads to save/load textures
このコミットが含まれているのは:
33ファイル変更
+1733
-141
検索条件に一致するファイルはありません
@@ -15,6 +15,7 @@
|
||||
<Compile Include="$(MSBuildThisFileDirectory)System\GC.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)System\IntPtrExtensions.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)System\FormatExtensions.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)System\IO\StreamExtensions.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)System\Path.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\InterpolatedStringHandlerArgumentAttribute.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\InterpolatedStringHandlerAttribute.cs" />
|
||||
@@ -22,6 +23,7 @@
|
||||
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\SkipLocalsInitAttribute.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\InteropServices\MemoryMarshal.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\RuntimeHelpers.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\InteropServices\UnmanagedCallersOnlyAttribute.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\InteropServices\NativeMemory.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\Versioning\OSPlatformAttribute.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\Versioning\SupportedOSVersionAttribute.cs" />
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
using System.Buffers;
|
||||
|
||||
namespace System.IO;
|
||||
|
||||
/// <summary>
|
||||
/// A polyfill type with extensions for <see cref="Stream"/>.
|
||||
/// </summary>
|
||||
internal static class StreamExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
|
||||
/// </summary>
|
||||
/// <param name="stream">The input <see cref="Stream"/> instance.</param>
|
||||
/// <param name="span">A region of memory. When this method returns, the contents of this region are replaced by the bytes read from the current source.</param>
|
||||
/// <returns>The total number of bytes read into the buffer.</returns>
|
||||
public static int Read(this Stream stream, Span<byte> span)
|
||||
{
|
||||
if (span.IsEmpty)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
byte[] array = ArrayPool<byte>.Shared.Rent(span.Length);
|
||||
|
||||
int bytesRead = stream.Read(array, 0, span.Length);
|
||||
|
||||
array.AsSpan(0, bytesRead).CopyTo(span);
|
||||
|
||||
ArrayPool<byte>.Shared.Return(array);
|
||||
|
||||
return bytesRead;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
|
||||
/// </summary>
|
||||
/// <param name="stream">The input <see cref="Stream"/> instance.</param>
|
||||
/// <param name="span">A region of memory. This method copies the contents of this region to the current stream.</param>
|
||||
public static void Write(this Stream stream, ReadOnlySpan<byte> span)
|
||||
{
|
||||
if (span.IsEmpty)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
byte[] array = ArrayPool<byte>.Shared.Rent(span.Length);
|
||||
|
||||
span.CopyTo(array);
|
||||
|
||||
stream.Write(array, 0, span.Length);
|
||||
|
||||
ArrayPool<byte>.Shared.Return(array);
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
|
||||
namespace ComputeSharp.NetStandard.System.Runtime.InteropServices;
|
||||
|
||||
/// <summary>
|
||||
/// Any method marked with <see cref="UnmanagedCallersOnlyAttribute" /> can be directly called from native code.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This polyfill is deliberately in the wrong namespace to avoid the C# compiler failing to build below .NET 5.
|
||||
/// </remarks>
|
||||
[AttributeUsage(AttributeTargets.Method, Inherited = false)]
|
||||
public sealed class UnmanagedCallersOnlyAttribute : Attribute
|
||||
{
|
||||
}
|
||||
差分なしでファイル名を変更。
@@ -0,0 +1,37 @@
|
||||
namespace ComputeSharp;
|
||||
|
||||
/// <summary>
|
||||
/// An <see langword="enum"/> representing a supported image format.
|
||||
/// </summary>
|
||||
public enum ImageFormat
|
||||
{
|
||||
/// <summary>
|
||||
/// The BMP format.
|
||||
/// </summary>
|
||||
Bmp,
|
||||
|
||||
/// <summary>
|
||||
/// The PNG format.
|
||||
/// </summary>
|
||||
Png,
|
||||
|
||||
/// <summary>
|
||||
/// The JPEG format.
|
||||
/// </summary>
|
||||
Jpeg,
|
||||
|
||||
/// <summary>
|
||||
/// The WMP format (also used for .jxr, .hdp, .wdp and .wmp files).
|
||||
/// </summary>
|
||||
Wmp,
|
||||
|
||||
/// <summary>
|
||||
/// The TIFF format.
|
||||
/// </summary>
|
||||
Tiff,
|
||||
|
||||
/// <summary>
|
||||
/// The DDS format.
|
||||
/// </summary>
|
||||
Dds
|
||||
}
|
||||
差分なしでファイル名を変更。
差分なしでファイル名を変更。
差分なしでファイル名を変更。
差分なしでファイル名を変更。
差分なしでファイル名を変更。
差分なしでファイル名を変更。
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.IO;
|
||||
using ComputeSharp.__Internals;
|
||||
using ComputeSharp.Graphics.Helpers;
|
||||
using ComputeSharp.Resources;
|
||||
@@ -1133,6 +1134,30 @@ public static partial class GraphicsDeviceExtensions
|
||||
return texture;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads a new readonly 2D texture with the contents of the specified stream.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
|
||||
/// <typeparam name="TPixel">The type of pixels used on the GPU side.</typeparam>
|
||||
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the texture.</param>
|
||||
/// <param name="stream">The stream with the image data to load and decode into the texture.</param>
|
||||
/// <returns>A <see cref="ReadOnlyTexture2D{T, TPixel}"/> instance with the contents of the specified file.</returns>
|
||||
[Pure]
|
||||
public static ReadOnlyTexture2D<T, TPixel> LoadReadOnlyTexture2D<T, TPixel>(this GraphicsDevice device, Stream stream)
|
||||
where T : unmanaged, IUnorm<TPixel>
|
||||
where TPixel : unmanaged
|
||||
{
|
||||
Guard.IsNotNull(stream, nameof(stream));
|
||||
|
||||
using UploadTexture2D<T> upload = WICHelper.Instance.LoadTexture<T>(device, stream);
|
||||
|
||||
ReadOnlyTexture2D<T, TPixel> texture = device.AllocateReadOnlyTexture2D<T, TPixel>(upload.Width, upload.Height);
|
||||
|
||||
upload.CopyTo(texture);
|
||||
|
||||
return texture;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads a new writeable 2D texture with the contents of the specified file.
|
||||
/// </summary>
|
||||
@@ -1194,4 +1219,28 @@ public static partial class GraphicsDeviceExtensions
|
||||
|
||||
return texture;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads a new writeable 2D texture with the contents of the specified stream.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
|
||||
/// <typeparam name="TPixel">The type of pixels used on the GPU side.</typeparam>
|
||||
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the texture.</param>
|
||||
/// <param name="stream">The stream with the image data to load and decode into the texture.</param>
|
||||
/// <returns>A <see cref="ReadWriteTexture2D{T, TPixel}"/> instance with the contents of the specified file.</returns>
|
||||
[Pure]
|
||||
public static ReadWriteTexture2D<T, TPixel> LoadReadWriteTexture2D<T, TPixel>(this GraphicsDevice device, Stream stream)
|
||||
where T : unmanaged, IUnorm<TPixel>
|
||||
where TPixel : unmanaged
|
||||
{
|
||||
Guard.IsNotNull(stream, nameof(stream));
|
||||
|
||||
using UploadTexture2D<T> upload = WICHelper.Instance.LoadTexture<T>(device, stream);
|
||||
|
||||
ReadWriteTexture2D<T, TPixel> texture = device.AllocateReadWriteTexture2D<T, TPixel>(upload.Width, upload.Height);
|
||||
|
||||
upload.CopyTo(texture);
|
||||
|
||||
return texture;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
|
||||
|
||||
// Ported from um/coml2api.h in the Windows SDK for Windows 10.0.20348.0
|
||||
// Original source is Copyright © Microsoft. All rights reserved.
|
||||
|
||||
#if NET6_0_OR_GREATER
|
||||
|
||||
namespace TerraFX.Interop.Windows;
|
||||
|
||||
// TODO: remove this once TerraFX.Interop.Windows is updated
|
||||
internal static partial class STGM
|
||||
{
|
||||
public const int STGM_READ = 0x00000000;
|
||||
|
||||
public const int STGM_WRITE = 0x00000001;
|
||||
|
||||
public const int STGM_READWRITE = 0x00000002;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,585 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading;
|
||||
using Microsoft.Toolkit.Diagnostics;
|
||||
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;
|
||||
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> with extensions for the <see cref="IWICStream"/> type.
|
||||
/// </summary>
|
||||
internal static unsafe class IWICStreamExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes an input <see cref="IWICStream"/> wrapping a given <see cref="Stream"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="stream">The target <see cref="IWICStream"/> object to initialize.</param>
|
||||
/// <param name="source">The input <see cref="Stream"/> instance to wrap.</param>
|
||||
/// <returns>An <see cref="HRESULT"/> value indicating the operation result.</returns>
|
||||
public static HRESULT InitializeFromStream(this ref IWICStream stream, Stream source)
|
||||
{
|
||||
using ComPtr<IStreamWrapper> streamWrapper = default;
|
||||
|
||||
IStreamWrapper.Create(source, streamWrapper.GetAddressOf());
|
||||
|
||||
return stream.InitializeFromIStream((IStream*)streamWrapper.Get());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A manual CCW implementation for an <see cref="IStream"/> object wrapping a <see cref="Stream"/> instance.
|
||||
/// </summary>
|
||||
internal unsafe partial struct IStreamWrapper
|
||||
#if NET6_0_OR_GREATER
|
||||
: IUnknown.Interface
|
||||
#endif
|
||||
{
|
||||
#if !NET6_0_OR_GREATER
|
||||
/// <inheritdoc cref="QueryInterface"/>
|
||||
private delegate int QueryInterfaceDelegate(IStreamWrapper* @this, Guid* riid, void** ppvObject);
|
||||
|
||||
/// <inheritdoc cref="AddRef"/>
|
||||
private delegate uint AddRefDelegate(IStreamWrapper* @this);
|
||||
|
||||
/// <inheritdoc cref="Release"/>
|
||||
private delegate uint ReleaseDelegate(IStreamWrapper* @this);
|
||||
|
||||
/// <inheritdoc cref="Read"/>
|
||||
private delegate HRESULT ReadDelegate(IStreamWrapper* @this, void* pv, uint cb, uint* pcbRead);
|
||||
|
||||
/// <inheritdoc cref="Write"/>
|
||||
private delegate HRESULT WriteDelegate(IStreamWrapper* @this, void* pv, uint cb, uint* pcbWritten);
|
||||
|
||||
/// <inheritdoc cref="Seek"/>
|
||||
private delegate HRESULT SeekDelegate(IStreamWrapper* @this, LARGE_INTEGER dlibMove, uint dwOrigin, ULARGE_INTEGER* plibNewPosition);
|
||||
|
||||
/// <inheritdoc cref="SetSize"/>
|
||||
private delegate HRESULT SetSizeDelegate(IStreamWrapper* @this, ULARGE_INTEGER libNewSize);
|
||||
|
||||
/// <inheritdoc cref="CopyTo"/>
|
||||
private delegate HRESULT CopyToDelegate(IStreamWrapper* @this, IStream* pstm, ULARGE_INTEGER cb, ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten);
|
||||
|
||||
/// <inheritdoc cref="Commit"/>
|
||||
private delegate HRESULT CommitDelegate(IStreamWrapper* @this, uint grfCommitFlags);
|
||||
|
||||
/// <inheritdoc cref="Revert"/>
|
||||
private delegate HRESULT RevertDelegate(IStreamWrapper* @this);
|
||||
|
||||
/// <inheritdoc cref="LockRegion"/>
|
||||
private delegate HRESULT LockRegionDelegate(IStreamWrapper* @this, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, uint dwLockType);
|
||||
|
||||
/// <inheritdoc cref="UnlockRegion"/>
|
||||
private delegate HRESULT UnlockRegionDelegate(IStreamWrapper* @this, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, uint dwLockType);
|
||||
|
||||
/// <inheritdoc cref="Stat"/>
|
||||
private delegate HRESULT StatDelegate(IStreamWrapper* @this, STATSTG* pstatstg, uint grfStatFlag);
|
||||
|
||||
/// <inheritdoc cref="Clone"/>
|
||||
private delegate HRESULT CloneDelegate(IStreamWrapper* @this, IStream** ppstm);
|
||||
|
||||
/// <summary>
|
||||
/// A cached <see cref="QueryInterfaceDelegate"/> instance wrapping <see cref="QueryInterface"/>.
|
||||
/// </summary>
|
||||
private static readonly QueryInterfaceDelegate QueryInterfaceWrapper = QueryInterface;
|
||||
|
||||
/// <summary>
|
||||
/// A cached <see cref="AddRefDelegate"/> instance wrapping <see cref="AddRef"/>.
|
||||
/// </summary>
|
||||
private static readonly AddRefDelegate AddRefWrapper = AddRef;
|
||||
|
||||
/// <summary>
|
||||
/// A cached <see cref="ReleaseDelegate"/> instance wrapping <see cref="Release"/>.
|
||||
/// </summary>
|
||||
private static readonly ReleaseDelegate ReleaseWrapper = Release;
|
||||
|
||||
/// <summary>
|
||||
/// A cached <see cref="ReadDelegate"/> instance wrapping <see cref="Read"/>.
|
||||
/// </summary>
|
||||
private static readonly ReadDelegate ReadWrapper = Read;
|
||||
|
||||
/// <summary>
|
||||
/// A cached <see cref="WriteDelegate"/> instance wrapping <see cref="Write"/>.
|
||||
/// </summary>
|
||||
private static readonly WriteDelegate WriteWrapper = Write;
|
||||
|
||||
/// <summary>
|
||||
/// A cached <see cref="SeekDelegate"/> instance wrapping <see cref="Seek"/>.
|
||||
/// </summary>
|
||||
private static readonly SeekDelegate SeekWrapper = Seek;
|
||||
|
||||
/// <summary>
|
||||
/// A cached <see cref="SetSizeDelegate"/> instance wrapping <see cref="SetSize"/>.
|
||||
/// </summary>
|
||||
private static readonly SetSizeDelegate SetSizeWrapper = SetSize;
|
||||
|
||||
/// <summary>
|
||||
/// A cached <see cref="CopyToDelegate"/> instance wrapping <see cref="CopyTo"/>.
|
||||
/// </summary>
|
||||
private static readonly CopyToDelegate CopyToWrapper = CopyTo;
|
||||
|
||||
/// <summary>
|
||||
/// A cached <see cref="CommitDelegate"/> instance wrapping <see cref="Commit"/>.
|
||||
/// </summary>
|
||||
private static readonly CommitDelegate CommitWrapper = Commit;
|
||||
|
||||
/// <summary>
|
||||
/// A cached <see cref="RevertDelegate"/> instance wrapping <see cref="Revert"/>.
|
||||
/// </summary>
|
||||
private static readonly RevertDelegate RevertWrapper = Revert;
|
||||
|
||||
/// <summary>
|
||||
/// A cached <see cref="LockRegionDelegate"/> instance wrapping <see cref="LockRegion"/>.
|
||||
/// </summary>
|
||||
private static readonly LockRegionDelegate LockRegionWrapper = LockRegion;
|
||||
|
||||
/// <summary>
|
||||
/// A cached <see cref="UnlockRegionDelegate"/> instance wrapping <see cref="UnlockRegion"/>.
|
||||
/// </summary>
|
||||
private static readonly UnlockRegionDelegate UnlockRegionWrapper = UnlockRegion;
|
||||
|
||||
/// <summary>
|
||||
/// A cached <see cref="StatDelegate"/> instance wrapping <see cref="Stat"/>.
|
||||
/// </summary>
|
||||
private static readonly StatDelegate StatWrapper = Stat;
|
||||
|
||||
/// <summary>
|
||||
/// A cached <see cref="CloneDelegate"/> instance wrapping <see cref="Clone"/>.
|
||||
/// </summary>
|
||||
private static readonly CloneDelegate CloneWrapper = Clone;
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// The shared vtable pointer for <see cref="IStreamWrapper"/> instances.
|
||||
/// </summary>
|
||||
private static readonly void** Vtbl = InitVtbl();
|
||||
|
||||
/// <summary>
|
||||
/// Setups the vtable pointer for <see cref="IStreamWrapper"/>.
|
||||
/// </summary>
|
||||
/// <returns>The initialized vtable pointer for <see cref="IStreamWrapper"/>.</returns>
|
||||
private static void** InitVtbl()
|
||||
{
|
||||
void** lpVtbl = (void**)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(IStreamWrapper), sizeof(void*) * 14);
|
||||
|
||||
#if NET6_0_OR_GREATER
|
||||
lpVtbl[0] = (delegate* unmanaged<IStreamWrapper*, Guid*, void**, int>)&QueryInterface;
|
||||
lpVtbl[1] = (delegate* unmanaged<IStreamWrapper*, uint>)&AddRef;
|
||||
lpVtbl[2] = (delegate* unmanaged<IStreamWrapper*, uint>)&Release;
|
||||
lpVtbl[3] = (delegate* unmanaged<IStreamWrapper*, void*, uint, uint*, HRESULT>)&Read;
|
||||
lpVtbl[4] = (delegate* unmanaged<IStreamWrapper*, void*, uint, uint*, HRESULT>)&Write;
|
||||
lpVtbl[5] = (delegate* unmanaged<IStreamWrapper*, LARGE_INTEGER, uint, ULARGE_INTEGER*, HRESULT>)&Seek;
|
||||
lpVtbl[6] = (delegate* unmanaged<IStreamWrapper*, ULARGE_INTEGER, HRESULT>)&SetSize;
|
||||
lpVtbl[7] = (delegate* unmanaged<IStreamWrapper*, IStream*, ULARGE_INTEGER, ULARGE_INTEGER*, ULARGE_INTEGER*, HRESULT>)&CopyTo;
|
||||
lpVtbl[8] = (delegate* unmanaged<IStreamWrapper*, uint, HRESULT>)&Commit;
|
||||
lpVtbl[9] = (delegate* unmanaged<IStreamWrapper*, HRESULT>)&Revert;
|
||||
lpVtbl[10] = (delegate* unmanaged<IStreamWrapper*, ULARGE_INTEGER, ULARGE_INTEGER, uint, HRESULT>)&LockRegion;
|
||||
lpVtbl[11] = (delegate* unmanaged<IStreamWrapper*, ULARGE_INTEGER, ULARGE_INTEGER, uint, HRESULT>)&UnlockRegion;
|
||||
lpVtbl[12] = (delegate* unmanaged<IStreamWrapper*, STATSTG*, uint, HRESULT>)&Stat;
|
||||
lpVtbl[13] = (delegate* unmanaged<IStreamWrapper*, IStream**, HRESULT>)&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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The vtable pointer for the current instance.
|
||||
/// </summary>
|
||||
public void** lpVtbl;
|
||||
|
||||
/// <summary>
|
||||
/// The current reference count for the object (from <c>IUnknown</c>).
|
||||
/// </summary>
|
||||
public volatile int referenceCount;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="GCHandle"/> to the captured <see cref="Stream"/>.
|
||||
/// </summary>
|
||||
public GCHandle streamHandle;
|
||||
|
||||
/// <summary>
|
||||
/// Creates and initializes a new <see cref="IStreamWrapper"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="stream">The input <see cref="Stream"/> instance to wrap.</param>
|
||||
/// <param name="streamWrapper">The target <see cref="IStreamWrapper"/> instance to initialize.</param>
|
||||
public static void Create(Stream stream, IStreamWrapper** streamWrapper)
|
||||
{
|
||||
IStreamWrapper* @this = (IStreamWrapper*)NativeMemory.Alloc((nuint)sizeof(IStreamWrapper));
|
||||
|
||||
@this->lpVtbl = Vtbl;
|
||||
@this->referenceCount = 1;
|
||||
@this->streamHandle = GCHandle.Alloc(stream);
|
||||
|
||||
*streamWrapper = @this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the captured <see cref="Stream"/> instance.
|
||||
/// </summary>
|
||||
/// <returns>The captured <see cref="Stream"/> instance</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private Stream GetStream()
|
||||
{
|
||||
return Unsafe.As<Stream>(this.streamHandle.Target!);
|
||||
}
|
||||
|
||||
#if NET6_0_OR_GREATER
|
||||
/// <inheritdoc/>
|
||||
public HRESULT QueryInterface(Guid* riid, void** ppvObject)
|
||||
{
|
||||
return ((delegate* unmanaged<IStreamWrapper*, Guid*, void**, HRESULT>)lpVtbl[0])((IStreamWrapper*)Unsafe.AsPointer(ref this), riid, ppvObject);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public uint AddRef()
|
||||
{
|
||||
return ((delegate* unmanaged<IStreamWrapper*, uint>)lpVtbl[1])((IStreamWrapper*)Unsafe.AsPointer(ref this));
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public uint Release()
|
||||
{
|
||||
return ((delegate* unmanaged<IStreamWrapper*, uint>)lpVtbl[2])((IStreamWrapper*)Unsafe.AsPointer(ref this));
|
||||
}
|
||||
#endif
|
||||
|
||||
/// <inheritdoc cref="IStream.QueryInterface(Guid*, void**)"/>
|
||||
[UnmanagedCallersOnly]
|
||||
private static int QueryInterface(IStreamWrapper* @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;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IStream.AddRef"/>
|
||||
[UnmanagedCallersOnly]
|
||||
private static uint AddRef(IStreamWrapper* @this)
|
||||
{
|
||||
return (uint)Interlocked.Increment(ref @this->referenceCount);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IStream.Release"/>
|
||||
[UnmanagedCallersOnly]
|
||||
private static uint Release(IStreamWrapper* @this)
|
||||
{
|
||||
uint referenceCount = (uint)Interlocked.Decrement(ref @this->referenceCount);
|
||||
|
||||
if (referenceCount == 0)
|
||||
{
|
||||
@this->streamHandle.Free();
|
||||
|
||||
NativeMemory.Free(@this);
|
||||
}
|
||||
|
||||
return referenceCount;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IStream.Read(void*, uint, uint*)"/>
|
||||
[UnmanagedCallersOnly]
|
||||
private static HRESULT Read(IStreamWrapper* @this, void* pv, uint cb, uint* pcbRead)
|
||||
{
|
||||
if (pv == null)
|
||||
{
|
||||
return E_POINTER;
|
||||
}
|
||||
|
||||
if (cb > int.MaxValue)
|
||||
{
|
||||
return E_INVALIDARG;
|
||||
}
|
||||
|
||||
if (pcbRead != null)
|
||||
{
|
||||
*pcbRead = 0;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
Span<byte> dst = new(pv, (int)cb);
|
||||
|
||||
while (dst.Length > 0)
|
||||
{
|
||||
int bytesRead = @this->GetStream().Read(dst);
|
||||
|
||||
if (bytesRead == 0)
|
||||
{
|
||||
return S_FALSE;
|
||||
}
|
||||
|
||||
if (pcbRead != null)
|
||||
{
|
||||
*pcbRead += (uint)bytesRead;
|
||||
}
|
||||
|
||||
dst = dst.Slice(bytesRead);
|
||||
}
|
||||
|
||||
return S_OK;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return ex.HResult;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IStream.Write(void*, uint, uint*)"/>
|
||||
[UnmanagedCallersOnly]
|
||||
private static HRESULT Write(IStreamWrapper* @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->GetStream().Write(new ReadOnlySpan<byte>(pv, (int)cb));
|
||||
|
||||
if (pcbWritten != null)
|
||||
{
|
||||
*pcbWritten = cb;
|
||||
}
|
||||
|
||||
return S_OK;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return ex.HResult;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IStream.Seek(LARGE_INTEGER, uint, ULARGE_INTEGER*)"/>
|
||||
[UnmanagedCallersOnly]
|
||||
private static HRESULT Seek(IStreamWrapper* @this, LARGE_INTEGER dlibMove, uint dwOrigin, ULARGE_INTEGER* plibNewPosition)
|
||||
{
|
||||
SeekOrigin origin = (SeekOrigin)dwOrigin;
|
||||
|
||||
switch (origin)
|
||||
{
|
||||
case SeekOrigin.Begin:
|
||||
case SeekOrigin.Current:
|
||||
case SeekOrigin.End:
|
||||
break;
|
||||
default:
|
||||
return E_INVALIDARG;
|
||||
}
|
||||
|
||||
if (plibNewPosition != null)
|
||||
{
|
||||
*plibNewPosition = default;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
long newPosition = @this->GetStream().Seek(dlibMove.QuadPart, origin);
|
||||
|
||||
if (plibNewPosition != null)
|
||||
{
|
||||
plibNewPosition->QuadPart = (ulong)newPosition;
|
||||
}
|
||||
|
||||
return S_OK;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return ex.HResult;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IStream.SetSize(ULARGE_INTEGER)"/>
|
||||
[UnmanagedCallersOnly]
|
||||
private static HRESULT SetSize(IStreamWrapper* @this, ULARGE_INTEGER libNewSize)
|
||||
{
|
||||
if (libNewSize.QuadPart > long.MaxValue)
|
||||
{
|
||||
return E_INVALIDARG;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
@this->GetStream().SetLength((long)libNewSize.QuadPart);
|
||||
|
||||
return S_OK;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return ex.HResult;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IStream.CopyTo(IStream*, ULARGE_INTEGER, ULARGE_INTEGER*, ULARGE_INTEGER*)"/>
|
||||
[UnmanagedCallersOnly]
|
||||
private static HRESULT CopyTo(IStreamWrapper* @this, IStream* pstm, ULARGE_INTEGER cb, ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten)
|
||||
{
|
||||
return E_NOTIMPL;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IStream.Commit(uint)"/>
|
||||
[UnmanagedCallersOnly]
|
||||
private static HRESULT Commit(IStreamWrapper* @this, uint grfCommitFlags)
|
||||
{
|
||||
try
|
||||
{
|
||||
@this->GetStream().Flush();
|
||||
|
||||
return S_OK;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return ex.HResult;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IStream.Revert"/>
|
||||
[UnmanagedCallersOnly]
|
||||
private static HRESULT Revert(IStreamWrapper* @this)
|
||||
{
|
||||
return STG_E_INVALIDFUNCTION;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IStream.LockRegion(ULARGE_INTEGER, ULARGE_INTEGER, uint)"/>
|
||||
[UnmanagedCallersOnly]
|
||||
private static HRESULT LockRegion(IStreamWrapper* @this, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, uint dwLockType)
|
||||
{
|
||||
return STG_E_INVALIDFUNCTION;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IStream.UnlockRegion(ULARGE_INTEGER, ULARGE_INTEGER, uint)"/>
|
||||
[UnmanagedCallersOnly]
|
||||
private static HRESULT UnlockRegion(IStreamWrapper* @this, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, uint dwLockType)
|
||||
{
|
||||
return STG_E_INVALIDFUNCTION;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IStream.Stat(STATSTG*, uint)"/>
|
||||
[UnmanagedCallersOnly]
|
||||
private static HRESULT Stat(IStreamWrapper* @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
|
||||
{
|
||||
Stream stream = @this->GetStream();
|
||||
|
||||
if (!statFlag.HasFlag(STATFLAG_NONAME))
|
||||
{
|
||||
if (stream is FileStream asFileStream)
|
||||
{
|
||||
pstatstg->pwcsName = (ushort*)Marshal.StringToCoTaskMemUni(asFileStream.Name);
|
||||
}
|
||||
}
|
||||
|
||||
bool canRead = stream.CanRead;
|
||||
bool canWrite = stream.CanWrite;
|
||||
|
||||
if (canRead && !canWrite)
|
||||
{
|
||||
pstatstg->grfMode = STGM_READ;
|
||||
}
|
||||
else if (canRead && canWrite)
|
||||
{
|
||||
pstatstg->grfMode = STGM_READWRITE;
|
||||
}
|
||||
else if (!canRead && canWrite)
|
||||
{
|
||||
pstatstg->grfMode = STGM_WRITE;
|
||||
}
|
||||
else
|
||||
{
|
||||
ThrowHelper.ThrowNotSupportedException();
|
||||
}
|
||||
|
||||
pstatstg->type = (uint)STGTY_STREAM;
|
||||
pstatstg->cbSize.QuadPart = (ulong)stream.Length;
|
||||
|
||||
return S_OK;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (pstatstg->pwcsName != null)
|
||||
{
|
||||
Marshal.FreeCoTaskMem((IntPtr)pstatstg->pwcsName);
|
||||
|
||||
pstatstg->pwcsName = null;
|
||||
}
|
||||
|
||||
return ex.HResult;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IStream.Clone(IStream**)"/>
|
||||
[UnmanagedCallersOnly]
|
||||
private static HRESULT Clone(IStreamWrapper* @this, IStream** ppstm)
|
||||
{
|
||||
return STG_E_INVALIDFUNCTION;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using ComputeSharp.Graphics.Helpers;
|
||||
using ComputeSharp.Resources;
|
||||
using Microsoft.Toolkit.Diagnostics;
|
||||
@@ -81,4 +82,19 @@ public static class ReadBackTexture2DExtensions
|
||||
{
|
||||
WICHelper.Instance.SaveTexture(texture.View, filename);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves a texture to a specified stream.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
|
||||
/// <param name="texture">The texture to save to an image.</param>
|
||||
/// <param name="stream">The stream to save the image to.</param>
|
||||
/// <param name="format">The target image format to use.</param>
|
||||
public static void Save<T>(this ReadBackTexture2D<T> texture, Stream stream, ImageFormat format)
|
||||
where T : unmanaged
|
||||
{
|
||||
Guard.IsNotNull(stream, nameof(stream));
|
||||
|
||||
WICHelper.Instance.SaveTexture(texture.View, stream, format);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
using ComputeSharp.Graphics.Helpers;
|
||||
using ComputeSharp.Resources;
|
||||
@@ -564,4 +565,23 @@ public static class Texture2DExtensions
|
||||
|
||||
WICHelper.Instance.SaveTexture(readback.View, filename);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves a texture to a specified stream.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
|
||||
/// <param name="texture">The texture to save to an image.</param>
|
||||
/// <param name="stream">The stream to save the image to.</param>
|
||||
/// <param name="format">The target image format to use.</param>
|
||||
public static void Save<T>(this Texture2D<T> texture, Stream stream, ImageFormat format)
|
||||
where T : unmanaged
|
||||
{
|
||||
Guard.IsNotNull(stream, nameof(stream));
|
||||
|
||||
using ReadBackTexture2D<T> readback = texture.GraphicsDevice.AllocateReadBackTexture2D<T>(texture.Width, texture.Height);
|
||||
|
||||
texture.CopyTo(readback);
|
||||
|
||||
WICHelper.Instance.SaveTexture(readback.View, stream, format);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,8 @@
|
||||
using ComputeSharp.Resources;
|
||||
using System;
|
||||
using System.IO;
|
||||
using ComputeSharp.Graphics.Helpers;
|
||||
using ComputeSharp.Resources;
|
||||
using Microsoft.Toolkit.Diagnostics;
|
||||
|
||||
namespace ComputeSharp;
|
||||
|
||||
@@ -52,4 +56,57 @@ public static class UploadTexture2DExtensions
|
||||
{
|
||||
destination.CopyFrom(source, sourceOffsetX, sourceOffsetY, destinationOffsetX, destinationOffsetY, width, height);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads a texture from a specified file.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
|
||||
/// <param name="texture">The texture to decode the image into.</param>
|
||||
/// <param name="filename">The filename of the image file to load.</param>
|
||||
public static void Load<T>(this UploadTexture2D<T> texture, string filename)
|
||||
where T : unmanaged
|
||||
{
|
||||
Guard.IsNotNull(filename, nameof(filename));
|
||||
|
||||
texture. Load(filename.AsSpan());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads a texture from a specified file.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
|
||||
/// <param name="texture">The texture to decode the image into.</param>
|
||||
/// <param name="filename">The filename of the image file to load.</param>
|
||||
public static void Load<T>(this UploadTexture2D<T> texture, ReadOnlySpan<char> filename)
|
||||
where T : unmanaged
|
||||
{
|
||||
WICHelper.Instance.LoadTexture(texture.View, filename);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// LLoads a texture from a specified buffer.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
|
||||
/// <param name="texture">The texture to decode the image into.</param>
|
||||
/// <param name="span">The buffer with the image data to load and decode into the texture.</param>
|
||||
/// <returns>A <see cref="ReadOnlyTexture2D{T, TPixel}"/> instance with the contents of the specified file.</returns>
|
||||
public static void Load<T>(this UploadTexture2D<T> texture, ReadOnlySpan<byte> span)
|
||||
where T : unmanaged
|
||||
{
|
||||
WICHelper.Instance.LoadTexture(texture.View, span);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads a texture from a specified stream.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
|
||||
/// <param name="texture">The texture to decode the image into.</param>
|
||||
/// <param name="stream">The stream to load the image from.</param>
|
||||
public static void Load<T>(this UploadTexture2D<T> texture, Stream stream)
|
||||
where T : unmanaged
|
||||
{
|
||||
Guard.IsNotNull(stream, nameof(stream));
|
||||
|
||||
WICHelper.Instance.LoadTexture(texture.View, stream);
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ using TerraFX.Interop.Windows;
|
||||
using RuntimeHelpers = System.Runtime.CompilerServices.RuntimeHelpers;
|
||||
#else
|
||||
using RuntimeHelpers = ComputeSharp.NetStandard.System.Runtime.CompilerServices.RuntimeHelpers;
|
||||
using UnmanagedCallersOnlyAttribute = ComputeSharp.NetStandard.System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute;
|
||||
#endif
|
||||
|
||||
#pragma warning disable CA1416
|
||||
@@ -85,6 +86,32 @@ internal static partial class DeviceHelper
|
||||
/// </summary>
|
||||
private unsafe struct IDXGIFactory4As6Backcompat
|
||||
{
|
||||
#if !NET6_0_OR_GREATER
|
||||
/// <inheritdoc cref="Release"/>
|
||||
private delegate uint ReleaseDelegate(IDXGIFactory4As6Backcompat* @this);
|
||||
|
||||
/// <inheritdoc cref="EnumWarpAdapter"/>
|
||||
private delegate int EnumWarpAdapterDelegate(IDXGIFactory4As6Backcompat* @this, Guid* riid, void** ppvAdapter);
|
||||
|
||||
/// <inheritdoc cref="EnumAdapterByGpuPreference"/>
|
||||
private delegate int EnumAdapterByGpuPreferenceDelegate(IDXGIFactory4As6Backcompat* @this, uint Adapter, DXGI_GPU_PREFERENCE GpuPreference, Guid* riid, void** ppvAdapter);
|
||||
|
||||
/// <summary>
|
||||
/// A cached <see cref="ReleaseDelegate"/> instance wrapping <see cref="Release"/>.
|
||||
/// </summary>
|
||||
private static readonly ReleaseDelegate ReleaseWrapper = Release;
|
||||
|
||||
/// <summary>
|
||||
/// A cached <see cref="EnumWarpAdapterDelegate"/> instance wrapping <see cref="EnumWarpAdapter"/>.
|
||||
/// </summary>
|
||||
private static readonly EnumWarpAdapterDelegate EnumWarpAdapterWrapper = EnumWarpAdapter;
|
||||
|
||||
/// <summary>
|
||||
/// A cached <see cref="EnumAdapterByGpuPreferenceDelegate"/> instance wrapping <see cref="EnumAdapterByGpuPreference"/>.
|
||||
/// </summary>
|
||||
private static readonly EnumAdapterByGpuPreferenceDelegate EnumAdapterByGpuPreferenceWrapper = EnumAdapterByGpuPreference;
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// The shared method table pointer for all <see cref="IDXGIFactory4As6Backcompat"/> instances.
|
||||
/// </summary>
|
||||
@@ -138,20 +165,8 @@ internal static partial class DeviceHelper
|
||||
*dxgiFactory6 = (IDXGIFactory6*)@this;
|
||||
}
|
||||
|
||||
#if !NET6_0_OR_GREATER
|
||||
/// <inheritdoc cref="Release"/>
|
||||
private delegate uint ReleaseDelegate(IDXGIFactory4As6Backcompat* @this);
|
||||
|
||||
/// <summary>
|
||||
/// A cached <see cref="ReleaseDelegate"/> instance wrapping <see cref="Release"/>.
|
||||
/// </summary>
|
||||
private static readonly ReleaseDelegate ReleaseWrapper = Release;
|
||||
#endif
|
||||
|
||||
/// <inheritdoc cref="IUnknown.Release"/>
|
||||
#if NET6_0_OR_GREATER
|
||||
[UnmanagedCallersOnly]
|
||||
#endif
|
||||
public static uint Release(IDXGIFactory4As6Backcompat* @this)
|
||||
{
|
||||
@this->dxgiFactory4->Release();
|
||||
@@ -161,39 +176,15 @@ internal static partial class DeviceHelper
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if !NET6_0_OR_GREATER
|
||||
/// <inheritdoc cref="EnumWarpAdapter"/>
|
||||
private delegate int EnumWarpAdapterDelegate(IDXGIFactory4As6Backcompat* @this, Guid* riid, void** ppvAdapter);
|
||||
|
||||
/// <summary>
|
||||
/// A cached <see cref="EnumWarpAdapterDelegate"/> instance wrapping <see cref="EnumWarpAdapter"/>.
|
||||
/// </summary>
|
||||
private static readonly EnumWarpAdapterDelegate EnumWarpAdapterWrapper = EnumWarpAdapter;
|
||||
#endif
|
||||
|
||||
/// <inheritdoc cref="IDXGIFactory6.EnumWarpAdapter(Guid*, void**)"/>
|
||||
#if NET6_0_OR_GREATER
|
||||
[UnmanagedCallersOnly]
|
||||
#endif
|
||||
public static int EnumWarpAdapter(IDXGIFactory4As6Backcompat* @this, Guid* riid, void** ppvAdapter)
|
||||
{
|
||||
return @this->dxgiFactory4->EnumWarpAdapter(riid, ppvAdapter);
|
||||
}
|
||||
|
||||
#if !NET6_0_OR_GREATER
|
||||
/// <inheritdoc cref="EnumAdapterByGpuPreference"/>
|
||||
private delegate int EnumAdapterByGpuPreferenceDelegate(IDXGIFactory4As6Backcompat* @this, uint Adapter, DXGI_GPU_PREFERENCE GpuPreference, Guid* riid, void** ppvAdapter);
|
||||
|
||||
/// <summary>
|
||||
/// A cached <see cref="EnumAdapterByGpuPreferenceDelegate"/> instance wrapping <see cref="EnumAdapterByGpuPreference"/>.
|
||||
/// </summary>
|
||||
private static readonly EnumAdapterByGpuPreferenceDelegate EnumAdapterByGpuPreferenceWrapper = EnumAdapterByGpuPreference;
|
||||
#endif
|
||||
|
||||
/// <inheritdoc cref="IDXGIFactory6.EnumAdapterByGpuPreference(uint, DXGI_GPU_PREFERENCE, Guid*, void**)"/>
|
||||
#if NET6_0_OR_GREATER
|
||||
[UnmanagedCallersOnly]
|
||||
#endif
|
||||
public static int EnumAdapterByGpuPreference(IDXGIFactory4As6Backcompat* @this, uint Adapter, DXGI_GPU_PREFERENCE GpuPreference, Guid* riid, void** ppvAdapter)
|
||||
{
|
||||
return @this->dxgiFactory4->EnumAdapters1(Adapter, (IDXGIAdapter1**)ppvAdapter);
|
||||
|
||||
@@ -132,4 +132,25 @@ internal static class WICFormatHelper
|
||||
_ => ThrowHelper.ThrowArgumentException<Guid>("Invalid filename"),
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the appropriate WIC container format <see cref="Guid"/> value for the input format.
|
||||
/// </summary>
|
||||
/// <param name="format">The target format to get the container format for.</param>
|
||||
/// <returns>The WIC format container <see cref="Guid"/> value matching <paramref name="format"/>.</returns>
|
||||
/// <exception cref="ArgumentException">Thrown when the input format isn't valid.</exception>
|
||||
[Pure]
|
||||
public static Guid GetForFormat(ImageFormat format)
|
||||
{
|
||||
return format switch
|
||||
{
|
||||
ImageFormat.Bmp => GUID.GUID_ContainerFormatBmp,
|
||||
ImageFormat.Png => GUID.GUID_ContainerFormatPng,
|
||||
ImageFormat.Jpeg => GUID.GUID_ContainerFormatJpeg,
|
||||
ImageFormat.Wmp => GUID.GUID_ContainerFormatWmp,
|
||||
ImageFormat.Tiff => GUID.GUID_ContainerFormatTiff,
|
||||
ImageFormat.Dds => GUID.GUID_ContainerFormatDds,
|
||||
_ => ThrowHelper.ThrowArgumentException<Guid>("Invalid format"),
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,10 @@
|
||||
using System;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.IO;
|
||||
using System.Runtime.CompilerServices;
|
||||
using ComputeSharp.Core.Extensions;
|
||||
using ComputeSharp.Graphics.Extensions;
|
||||
using Microsoft.Toolkit.Diagnostics;
|
||||
using TerraFX.Interop.Windows;
|
||||
|
||||
#pragma warning disable CA1416
|
||||
@@ -107,103 +110,30 @@ internal sealed unsafe class WICHelper
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves a texture to a specified file.
|
||||
/// Loads an <see cref="UploadTexture2D{T}"/> from a specified stream.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
|
||||
/// <param name="texture">The texture data to save to an image.</param>
|
||||
/// <param name="filename">The filename of the image file to save.</param>
|
||||
public void SaveTexture<T>(TextureView2D<T> texture, ReadOnlySpan<char> filename)
|
||||
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the texture.</param>
|
||||
/// <param name="stream">The stream with the image data to load and decode into the texture.</param>
|
||||
/// <returns>An <see cref="UploadTexture2D{T}"/> instance with the contents of the specified file.</returns>
|
||||
[Pure]
|
||||
public UploadTexture2D<T> LoadTexture<T>(GraphicsDevice device, Stream stream)
|
||||
where T : unmanaged
|
||||
{
|
||||
using ComPtr<IWICBitmapEncoder> wicBitmapEncoder = default;
|
||||
Guid containerGuid = WICFormatHelper.GetForFilename(filename);
|
||||
|
||||
// Create the image encoder
|
||||
this.wicImagingFactory2.Get()->CreateEncoder(
|
||||
&containerGuid,
|
||||
null,
|
||||
wicBitmapEncoder.GetAddressOf()).Assert();
|
||||
|
||||
using ComPtr<IWICBitmapDecoder> wicBitmapDecoder = default;
|
||||
using ComPtr<IWICStream> wicStream = default;
|
||||
|
||||
// Create and initialize a stream to the target file
|
||||
this.wicImagingFactory2.Get()->CreateStream(wicStream.GetAddressOf()).Assert();
|
||||
|
||||
fixed (char* p = filename)
|
||||
{
|
||||
wicStream.Get()->InitializeFromFilename(
|
||||
(ushort*)p,
|
||||
Windows.GENERIC_WRITE);
|
||||
}
|
||||
wicStream.Get()->InitializeFromStream(stream).Assert();
|
||||
|
||||
// Initialize the encoder
|
||||
wicBitmapEncoder.Get()->Initialize(
|
||||
this.wicImagingFactory2.Get()->CreateDecoderFromStream(
|
||||
(IStream*)wicStream.Get(),
|
||||
WICBitmapEncoderCacheOption.WICBitmapEncoderNoCache).Assert();
|
||||
null,
|
||||
WICDecodeOptions.WICDecodeMetadataCacheOnDemand,
|
||||
wicBitmapDecoder.GetAddressOf()).Assert();
|
||||
|
||||
using ComPtr<IWICBitmapFrameEncode> wicBitmapFrameEncode = default;
|
||||
|
||||
// Create the image frame and initialize it
|
||||
wicBitmapEncoder.Get()->CreateNewFrame(wicBitmapFrameEncode.GetAddressOf(), null).Assert();
|
||||
|
||||
wicBitmapFrameEncode.Get()->Initialize(null).Assert();
|
||||
wicBitmapFrameEncode.Get()->SetSize((uint)texture.Width, (uint)texture.Height).Assert();
|
||||
|
||||
// Depending on the target format and the current pixel type, we need to check whether
|
||||
// an intermediate encoding step is necessary. This is because not all pixel formats
|
||||
// are directly supported by the native image encoders present in Windows.
|
||||
if (WICFormatHelper.TryGetIntermediateFormatForType<T>(containerGuid, out Guid intermediateGuid))
|
||||
{
|
||||
T* data = texture.DangerousGetAddressAndByteStride(out int strideInBytes);
|
||||
|
||||
using ComPtr<IWICBitmap> wicBitmap = default;
|
||||
Guid initialGuid = WICFormatHelper.GetForType<T>();
|
||||
|
||||
// Create a bitmap wrapping the input texture
|
||||
this.wicImagingFactory2.Get()->CreateBitmapFromMemory(
|
||||
(uint)texture.Width,
|
||||
(uint)texture.Height,
|
||||
&initialGuid,
|
||||
(uint)strideInBytes,
|
||||
(uint)(strideInBytes * texture.Height),
|
||||
(byte*)data,
|
||||
wicBitmap.GetAddressOf()).Assert();
|
||||
|
||||
using ComPtr<IWICFormatConverter> wicFormatConverter = default;
|
||||
|
||||
this.wicImagingFactory2.Get()->CreateFormatConverter(wicFormatConverter.GetAddressOf()).Assert();
|
||||
|
||||
// Get a format converter to encode the pixel data
|
||||
wicFormatConverter.Get()->Initialize(
|
||||
(IWICBitmapSource*)wicBitmap.Get(),
|
||||
&intermediateGuid,
|
||||
WICBitmapDitherType.WICBitmapDitherTypeNone,
|
||||
null,
|
||||
0,
|
||||
WICBitmapPaletteType.WICBitmapPaletteTypeMedianCut).Assert();
|
||||
|
||||
// Write the encoded image data to the frame
|
||||
wicBitmapFrameEncode.Get()->SetPixelFormat(&intermediateGuid).Assert();
|
||||
wicBitmapFrameEncode.Get()->WriteSource(
|
||||
(IWICBitmapSource*)wicBitmap.Get(),
|
||||
null).Assert();
|
||||
}
|
||||
else
|
||||
{
|
||||
T* data = texture.DangerousGetAddressAndByteStride(out int strideInBytes);
|
||||
Guid initialGuid = WICFormatHelper.GetForType<T>();
|
||||
|
||||
// Write the image data to the frame
|
||||
wicBitmapFrameEncode.Get()->SetPixelFormat(&initialGuid).Assert();
|
||||
wicBitmapFrameEncode.Get()->WritePixels(
|
||||
lineCount: (uint)texture.Height,
|
||||
cbStride: (uint)strideInBytes,
|
||||
cbBufferSize: (uint)(strideInBytes * texture.Height),
|
||||
pbPixels: (byte*)data).Assert();
|
||||
}
|
||||
|
||||
wicBitmapFrameEncode.Get()->Commit();
|
||||
wicBitmapEncoder.Get()->Commit();
|
||||
return LoadTexture<T>(device, wicBitmapDecoder.Get());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -282,4 +212,295 @@ internal sealed unsafe class WICHelper
|
||||
return upload;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads an image from a specified file.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
|
||||
/// <param name="texture">The target <see cref="TextureView2D{T}"/> instance to write data to.</param>
|
||||
/// <param name="filename">The filename of the image file to load and decode into the texture.</param>
|
||||
[Pure]
|
||||
public void LoadTexture<T>(TextureView2D<T> texture, ReadOnlySpan<char> filename)
|
||||
where T : unmanaged
|
||||
{
|
||||
using ComPtr<IWICBitmapDecoder> wicBitmapDecoder = default;
|
||||
|
||||
fixed (char* p = filename)
|
||||
{
|
||||
this.wicImagingFactory2.Get()->CreateDecoderFromFilename(
|
||||
(ushort*)p,
|
||||
null,
|
||||
Windows.GENERIC_READ,
|
||||
WICDecodeOptions.WICDecodeMetadataCacheOnDemand,
|
||||
wicBitmapDecoder.GetAddressOf()).Assert();
|
||||
}
|
||||
|
||||
LoadTexture(texture, wicBitmapDecoder.Get());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads an image from a specified buffer.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
|
||||
/// <param name="texture">The target <see cref="TextureView2D{T}"/> instance to write data to.</param>
|
||||
/// <param name="span">The buffer with the image data to load and decode into the texture.</param>
|
||||
[Pure]
|
||||
public void LoadTexture<T>(TextureView2D<T> texture, ReadOnlySpan<byte> span)
|
||||
where T : unmanaged
|
||||
{
|
||||
using ComPtr<IWICBitmapDecoder> wicBitmapDecoder = default;
|
||||
using ComPtr<IWICStream> wicStream = default;
|
||||
|
||||
// Get the bitmap decoder for the target buffer
|
||||
fixed (byte* p = span)
|
||||
{
|
||||
this.wicImagingFactory2.Get()->CreateStream(wicStream.GetAddressOf()).Assert();
|
||||
|
||||
wicStream.Get()->InitializeFromMemory(p, (uint)span.Length).Assert();
|
||||
|
||||
this.wicImagingFactory2.Get()->CreateDecoderFromStream(
|
||||
(IStream*)wicStream.Get(),
|
||||
null,
|
||||
WICDecodeOptions.WICDecodeMetadataCacheOnDemand,
|
||||
wicBitmapDecoder.GetAddressOf()).Assert();
|
||||
|
||||
LoadTexture(texture, wicBitmapDecoder.Get());
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads an image from a specified stream.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
|
||||
/// <param name="texture">The target <see cref="TextureView2D{T}"/> instance to write data to.</param>
|
||||
/// <param name="stream">The stream with the image data to load and decode into the texture.</param>
|
||||
[Pure]
|
||||
public void LoadTexture<T>(TextureView2D<T> texture, Stream stream)
|
||||
where T : unmanaged
|
||||
{
|
||||
using ComPtr<IWICBitmapDecoder> wicBitmapDecoder = default;
|
||||
using ComPtr<IWICStream> wicStream = default;
|
||||
|
||||
this.wicImagingFactory2.Get()->CreateStream(wicStream.GetAddressOf()).Assert();
|
||||
|
||||
wicStream.Get()->InitializeFromStream(stream).Assert();
|
||||
|
||||
this.wicImagingFactory2.Get()->CreateDecoderFromStream(
|
||||
(IStream*)wicStream.Get(),
|
||||
null,
|
||||
WICDecodeOptions.WICDecodeMetadataCacheOnDemand,
|
||||
wicBitmapDecoder.GetAddressOf()).Assert();
|
||||
|
||||
LoadTexture(texture, wicBitmapDecoder.Get());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads image data from a specified <see cref="IWICBitmapDecoder"/> object into a target texture.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
|
||||
/// <param name="texture">The target <see cref="TextureView2D{T}"/> instance to write data to.</param>
|
||||
/// <param name="wicBitmapDecoder">The <see cref="IWICBitmapDecoder"/> object in use.</param>
|
||||
[Pure]
|
||||
private void LoadTexture<T>(TextureView2D<T> texture, IWICBitmapDecoder* wicBitmapDecoder)
|
||||
where T : unmanaged
|
||||
{
|
||||
using ComPtr<IWICBitmapFrameDecode> wicBitmapFrameDecode = default;
|
||||
|
||||
wicBitmapDecoder->GetFrame(0, wicBitmapFrameDecode.GetAddressOf()).Assert();
|
||||
|
||||
uint width;
|
||||
uint height;
|
||||
|
||||
// Extract the image size info
|
||||
wicBitmapFrameDecode.Get()->GetSize(&width, &height).Assert();
|
||||
|
||||
// Validate the target texture has the right size
|
||||
Guard.IsEqualTo((uint)texture.Width, width, nameof(texture.Width));
|
||||
Guard.IsEqualTo((uint)texture.Height, height, nameof(texture.Height));
|
||||
|
||||
Guid wicTargetPixelFormatGuid = WICFormatHelper.GetForType<T>();
|
||||
Guid wicActualPixelFormatGuid;
|
||||
|
||||
wicBitmapFrameDecode.Get()->GetPixelFormat(&wicActualPixelFormatGuid).Assert();
|
||||
|
||||
T* data = texture.DangerousGetAddressAndByteStride(out int strideInBytes);
|
||||
|
||||
// Copy the decoded pixels directly from the loaded image
|
||||
if (wicTargetPixelFormatGuid == wicActualPixelFormatGuid)
|
||||
{
|
||||
wicBitmapFrameDecode.Get()->CopyPixels(
|
||||
prc: null,
|
||||
cbStride: (uint)strideInBytes,
|
||||
cbBufferSize: (uint)strideInBytes * height,
|
||||
pbBuffer: (byte*)data).Assert();
|
||||
}
|
||||
else
|
||||
{
|
||||
using ComPtr<IWICFormatConverter> wicFormatConverter = default;
|
||||
|
||||
this.wicImagingFactory2.Get()->CreateFormatConverter(wicFormatConverter.GetAddressOf()).Assert();
|
||||
|
||||
wicFormatConverter.Get()->Initialize(
|
||||
(IWICBitmapSource*)wicBitmapFrameDecode.Get(),
|
||||
&wicTargetPixelFormatGuid,
|
||||
WICBitmapDitherType.WICBitmapDitherTypeNone,
|
||||
null,
|
||||
0,
|
||||
WICBitmapPaletteType.WICBitmapPaletteTypeMedianCut).Assert();
|
||||
|
||||
// Decode the pixel data into the upload buffer
|
||||
wicFormatConverter.Get()->CopyPixels(
|
||||
prc: null,
|
||||
cbStride: (uint)strideInBytes,
|
||||
cbBufferSize: (uint)strideInBytes * height,
|
||||
pbBuffer: (byte*)data).Assert();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves a texture to a specified file.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
|
||||
/// <param name="texture">The texture data to save to an image.</param>
|
||||
/// <param name="filename">The filename of the image file to save.</param>
|
||||
public void SaveTexture<T>(TextureView2D<T> texture, ReadOnlySpan<char> filename)
|
||||
where T : unmanaged
|
||||
{
|
||||
using ComPtr<IWICBitmapEncoder> wicBitmapEncoder = default;
|
||||
Guid containerGuid = WICFormatHelper.GetForFilename(filename);
|
||||
|
||||
// Create the image encoder
|
||||
this.wicImagingFactory2.Get()->CreateEncoder(
|
||||
&containerGuid,
|
||||
null,
|
||||
wicBitmapEncoder.GetAddressOf()).Assert();
|
||||
|
||||
using ComPtr<IWICStream> wicStream = default;
|
||||
|
||||
// Create and initialize a stream to the target file
|
||||
this.wicImagingFactory2.Get()->CreateStream(wicStream.GetAddressOf()).Assert();
|
||||
|
||||
fixed (char* p = filename)
|
||||
{
|
||||
wicStream.Get()->InitializeFromFilename(
|
||||
(ushort*)p,
|
||||
Windows.GENERIC_WRITE);
|
||||
}
|
||||
|
||||
// Initialize the encoder
|
||||
wicBitmapEncoder.Get()->Initialize(
|
||||
(IStream*)wicStream.Get(),
|
||||
WICBitmapEncoderCacheOption.WICBitmapEncoderNoCache).Assert();
|
||||
|
||||
SaveTexture(texture, wicBitmapEncoder.Get(), containerGuid);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves a texture to a specified stream.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
|
||||
/// <param name="texture">The texture data to save to a stream.</param>
|
||||
/// <param name="stream">The target stream to write to.</param>
|
||||
/// <param name="format">The target image format to use.</param>
|
||||
public void SaveTexture<T>(TextureView2D<T> texture, Stream stream, ImageFormat format)
|
||||
where T : unmanaged
|
||||
{
|
||||
using ComPtr<IWICBitmapEncoder> wicBitmapEncoder = default;
|
||||
Guid containerGuid = WICFormatHelper.GetForFormat(format);
|
||||
|
||||
// Create the image encoder
|
||||
this.wicImagingFactory2.Get()->CreateEncoder(
|
||||
&containerGuid,
|
||||
null,
|
||||
wicBitmapEncoder.GetAddressOf()).Assert();
|
||||
|
||||
using ComPtr<IWICStream> wicStream = default;
|
||||
|
||||
// Create and initialize a stream
|
||||
this.wicImagingFactory2.Get()->CreateStream(wicStream.GetAddressOf()).Assert();
|
||||
|
||||
wicStream.Get()->InitializeFromStream(stream);
|
||||
|
||||
// Initialize the encoder
|
||||
wicBitmapEncoder.Get()->Initialize(
|
||||
(IStream*)wicStream.Get(),
|
||||
WICBitmapEncoderCacheOption.WICBitmapEncoderNoCache).Assert();
|
||||
|
||||
SaveTexture(texture, wicBitmapEncoder.Get(), containerGuid);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves a texture to a specified <see cref="IWICBitmapEncoder"/> object.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
|
||||
/// <param name="texture">The texture data to save to an image.</param>
|
||||
/// <param name="wicBitmapEncoder">The <see cref="IWICBitmapEncoder"/> object in use.</param>
|
||||
/// <param name="format">The <see cref="Guid"/> identifying the target format.</param>
|
||||
private void SaveTexture<T>(TextureView2D<T> texture, IWICBitmapEncoder* wicBitmapEncoder, Guid format)
|
||||
where T : unmanaged
|
||||
{
|
||||
using ComPtr<IWICBitmapFrameEncode> wicBitmapFrameEncode = default;
|
||||
|
||||
// Create the image frame and initialize it
|
||||
wicBitmapEncoder->CreateNewFrame(wicBitmapFrameEncode.GetAddressOf(), null).Assert();
|
||||
|
||||
wicBitmapFrameEncode.Get()->Initialize(null).Assert();
|
||||
wicBitmapFrameEncode.Get()->SetSize((uint)texture.Width, (uint)texture.Height).Assert();
|
||||
|
||||
// Depending on the target format and the current pixel type, we need to check whether
|
||||
// an intermediate encoding step is necessary. This is because not all pixel formats
|
||||
// are directly supported by the native image encoders present in Windows.
|
||||
if (WICFormatHelper.TryGetIntermediateFormatForType<T>(format, out Guid intermediateGuid))
|
||||
{
|
||||
T* data = texture.DangerousGetAddressAndByteStride(out int strideInBytes);
|
||||
|
||||
using ComPtr<IWICBitmap> wicBitmap = default;
|
||||
Guid initialGuid = WICFormatHelper.GetForType<T>();
|
||||
|
||||
// Create a bitmap wrapping the input texture
|
||||
this.wicImagingFactory2.Get()->CreateBitmapFromMemory(
|
||||
(uint)texture.Width,
|
||||
(uint)texture.Height,
|
||||
&initialGuid,
|
||||
(uint)strideInBytes,
|
||||
(uint)(strideInBytes * texture.Height),
|
||||
(byte*)data,
|
||||
wicBitmap.GetAddressOf()).Assert();
|
||||
|
||||
using ComPtr<IWICFormatConverter> wicFormatConverter = default;
|
||||
|
||||
this.wicImagingFactory2.Get()->CreateFormatConverter(wicFormatConverter.GetAddressOf()).Assert();
|
||||
|
||||
// Get a format converter to encode the pixel data
|
||||
wicFormatConverter.Get()->Initialize(
|
||||
(IWICBitmapSource*)wicBitmap.Get(),
|
||||
&intermediateGuid,
|
||||
WICBitmapDitherType.WICBitmapDitherTypeNone,
|
||||
null,
|
||||
0,
|
||||
WICBitmapPaletteType.WICBitmapPaletteTypeMedianCut).Assert();
|
||||
|
||||
// Write the encoded image data to the frame
|
||||
wicBitmapFrameEncode.Get()->SetPixelFormat(&intermediateGuid).Assert();
|
||||
wicBitmapFrameEncode.Get()->WriteSource(
|
||||
(IWICBitmapSource*)wicBitmap.Get(),
|
||||
null).Assert();
|
||||
}
|
||||
else
|
||||
{
|
||||
T* data = texture.DangerousGetAddressAndByteStride(out int strideInBytes);
|
||||
Guid initialGuid = WICFormatHelper.GetForType<T>();
|
||||
|
||||
// Write the image data to the frame
|
||||
wicBitmapFrameEncode.Get()->SetPixelFormat(&initialGuid).Assert();
|
||||
wicBitmapFrameEncode.Get()->WritePixels(
|
||||
lineCount: (uint)texture.Height,
|
||||
cbStride: (uint)strideInBytes,
|
||||
cbBufferSize: (uint)(strideInBytes * texture.Height),
|
||||
pbPixels: (byte*)data).Assert();
|
||||
}
|
||||
|
||||
wicBitmapFrameEncode.Get()->Commit();
|
||||
wicBitmapEncoder->Commit();
|
||||
}
|
||||
}
|
||||
@@ -217,17 +217,25 @@
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Windows\other\helper-types\NativeTypeNameAttribute.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Windows\other\helper-types\VtblIndexAttribute.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Windows\other\helper-types\WPARAM.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Windows\shared\minwindef\FILETIME.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Windows\shared\minwindef\Windows.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Windows\shared\uuids\Windows.Manual.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Windows\shared\winerror\DXGI.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Windows\shared\winerror\E.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Windows\shared\winerror\S.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Windows\shared\winerror\STG.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Windows\shared\winerror\Windows.Manual.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Windows\shared\wtypesbase\CLSCTX.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Windows\shared\wtypes\STATFLAG.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Windows\um\combaseapi\Windows.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Windows\um\coml2api\STGM.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Windows\um\minwinbase\SECURITY_ATTRIBUTES.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Windows\um\objidlbase\IID.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Windows\um\objidlbase\IStream.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Windows\um\objidlbase\STATSTG.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Windows\um\objidlbase\STGTY.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Windows\um\synchapi\Windows.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Windows\um\Unknwnbase\IID.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Windows\um\Unknwnbase\IUnknown.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Windows\um\wincodec\CLSID.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Windows\um\wincodec\GUID.cs" />
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
|
||||
|
||||
// Ported from shared/minwindef.h in the Windows SDK for Windows 10.0.20348.0
|
||||
// Original source is Copyright © Microsoft. All rights reserved.
|
||||
|
||||
namespace TerraFX.Interop.Windows;
|
||||
|
||||
internal partial struct FILETIME
|
||||
{
|
||||
[NativeTypeName("DWORD")]
|
||||
public uint dwLowDateTime;
|
||||
|
||||
[NativeTypeName("DWORD")]
|
||||
public uint dwHighDateTime;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
|
||||
|
||||
// Ported from shared/winerror.h in the Windows SDK for Windows 10.0.20348.0
|
||||
// Original source is Copyright © Microsoft. All rights reserved.
|
||||
|
||||
namespace TerraFX.Interop.Windows;
|
||||
|
||||
internal static partial class STG
|
||||
{
|
||||
[NativeTypeName("#define STG_E_INVALIDFUNCTION _HRESULT_TYPEDEF_(0x80030001L)")]
|
||||
public const int STG_E_INVALIDFUNCTION = unchecked((int)(0x80030001));
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
|
||||
|
||||
// Ported from shared/wtypes.h in the Windows SDK for Windows 10.0.20348.0
|
||||
// Original source is Copyright © Microsoft. All rights reserved.
|
||||
|
||||
namespace TerraFX.Interop.Windows;
|
||||
|
||||
internal enum STATFLAG
|
||||
{
|
||||
STATFLAG_DEFAULT = 0,
|
||||
|
||||
STATFLAG_NONAME = 1,
|
||||
|
||||
STATFLAG_NOOPEN = 2,
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
|
||||
|
||||
// Ported from um/Unknwnbase.h in the Windows SDK for Windows 10.0.20348.0
|
||||
// Original source is Copyright © Microsoft. All rights reserved.
|
||||
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace TerraFX.Interop.Windows;
|
||||
|
||||
internal static partial class IID
|
||||
{
|
||||
public static ref readonly Guid IID_IUnknown
|
||||
{
|
||||
get
|
||||
{
|
||||
ReadOnlySpan<byte> data = new byte[] {
|
||||
0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00,
|
||||
0x00, 0x00,
|
||||
0xC0,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x46
|
||||
};
|
||||
|
||||
Debug.Assert(data.Length == Unsafe.SizeOf<Guid>());
|
||||
return ref Unsafe.As<byte, Guid>(ref MemoryMarshal.GetReference(data));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
|
||||
|
||||
// Ported from um/coml2api.h in the Windows SDK for Windows 10.0.20348.0
|
||||
// Original source is Copyright © Microsoft. All rights reserved.
|
||||
|
||||
namespace TerraFX.Interop.Windows;
|
||||
|
||||
internal static partial class STGM
|
||||
{
|
||||
[NativeTypeName("#define STGM_READ 0x00000000L")]
|
||||
public const int STGM_READ = 0x00000000;
|
||||
|
||||
[NativeTypeName("#define STGM_WRITE 0x00000001L")]
|
||||
public const int STGM_WRITE = 0x00000001;
|
||||
|
||||
[NativeTypeName("#define STGM_READWRITE 0x00000002L")]
|
||||
public const int STGM_READWRITE = 0x00000002;
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
|
||||
|
||||
// Ported from um/objidlbase.h in the Windows SDK for Windows 10.0.20348.0
|
||||
// Original source is Copyright © Microsoft. All rights reserved.
|
||||
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace TerraFX.Interop.Windows;
|
||||
|
||||
internal static partial class IID
|
||||
{
|
||||
public static ref readonly Guid IID_ISequentialStream
|
||||
{
|
||||
get
|
||||
{
|
||||
ReadOnlySpan<byte> data = new byte[] {
|
||||
0x30, 0x3A, 0x73, 0x0C,
|
||||
0x1C, 0x2A,
|
||||
0xCE, 0x11,
|
||||
0xAD,
|
||||
0xE5,
|
||||
0x00,
|
||||
0xAA,
|
||||
0x00,
|
||||
0x44,
|
||||
0x77,
|
||||
0x3D
|
||||
};
|
||||
|
||||
Debug.Assert(data.Length == Unsafe.SizeOf<Guid>());
|
||||
return ref Unsafe.As<byte, Guid>(ref MemoryMarshal.GetReference(data));
|
||||
}
|
||||
}
|
||||
|
||||
public static ref readonly Guid IID_IStream
|
||||
{
|
||||
get
|
||||
{
|
||||
ReadOnlySpan<byte> data = new byte[] {
|
||||
0x0C, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00,
|
||||
0x00, 0x00,
|
||||
0xC0,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x46
|
||||
};
|
||||
|
||||
Debug.Assert(data.Length == Unsafe.SizeOf<Guid>());
|
||||
return ref Unsafe.As<byte, Guid>(ref MemoryMarshal.GetReference(data));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -102,6 +102,13 @@ namespace TerraFX.Interop.Windows
|
||||
return ((delegate* unmanaged[Stdcall]<IStream*, ULARGE_INTEGER, ULARGE_INTEGER, uint, int>)(lpVtbl[11]))((IStream*)Unsafe.AsPointer(ref this), libOffset, cb, dwLockType);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(12)]
|
||||
public HRESULT Stat(STATSTG* pstatstg, [NativeTypeName("DWORD")] uint grfStatFlag)
|
||||
{
|
||||
return ((delegate* unmanaged[Stdcall]<IStream*, STATSTG*, uint, int>)(lpVtbl[12]))((IStream*)Unsafe.AsPointer(ref this), pstatstg, grfStatFlag);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(13)]
|
||||
public HRESULT Clone(IStream** ppstm)
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
|
||||
|
||||
// Ported from um/objidlbase.h in the Windows SDK for Windows 10.0.20348.0
|
||||
// Original source is Copyright © Microsoft. All rights reserved.
|
||||
|
||||
using System;
|
||||
|
||||
namespace TerraFX.Interop.Windows;
|
||||
|
||||
internal unsafe partial struct STATSTG
|
||||
{
|
||||
[NativeTypeName("LPOLESTR")]
|
||||
public ushort* pwcsName;
|
||||
|
||||
[NativeTypeName("DWORD")]
|
||||
public uint type;
|
||||
|
||||
public ULARGE_INTEGER cbSize;
|
||||
|
||||
public FILETIME mtime;
|
||||
|
||||
public FILETIME ctime;
|
||||
|
||||
public FILETIME atime;
|
||||
|
||||
[NativeTypeName("DWORD")]
|
||||
public uint grfMode;
|
||||
|
||||
[NativeTypeName("DWORD")]
|
||||
public uint grfLocksSupported;
|
||||
|
||||
[NativeTypeName("CLSID")]
|
||||
public Guid clsid;
|
||||
|
||||
[NativeTypeName("DWORD")]
|
||||
public uint grfStateBits;
|
||||
|
||||
[NativeTypeName("DWORD")]
|
||||
public uint reserved;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
|
||||
|
||||
// Ported from um/objidlbase.h in the Windows SDK for Windows 10.0.20348.0
|
||||
// Original source is Copyright © Microsoft. All rights reserved.
|
||||
|
||||
namespace TerraFX.Interop.Windows;
|
||||
|
||||
internal enum STGTY
|
||||
{
|
||||
STGTY_STORAGE = 1,
|
||||
|
||||
STGTY_STREAM = 2,
|
||||
|
||||
STGTY_LOCKBYTES = 3,
|
||||
|
||||
STGTY_PROPERTY = 4,
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.IO;
|
||||
using ComputeSharp.__Internals;
|
||||
using ComputeSharp.Resources;
|
||||
|
||||
@@ -301,6 +302,30 @@ public static class GraphicsDeviceExtensions
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads an image from a specific file into a target <see cref="UploadTexture2D{T}"/> instance.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
|
||||
/// <param name="texture">The target <see cref="UploadTexture2D{T}"/> instance.</param>
|
||||
/// <param name="inputType">The type of filepath to use.</param>
|
||||
/// <param name="filename">The filename of the image file to load and decode into the texture.</param>
|
||||
[Pure]
|
||||
public static void Load<T>(this UploadTexture2D<T> texture, Type inputType, string filename)
|
||||
where T : unmanaged
|
||||
{
|
||||
switch (inputType)
|
||||
{
|
||||
case var _ when inputType == typeof(string):
|
||||
texture.Load(filename);
|
||||
break;
|
||||
case var _ when inputType == typeof(ReadOnlySpan<char>):
|
||||
texture.Load(filename);
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentException($"Invalid input type: {inputType}", nameof(inputType));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads a new readonly 2D texture with the contents of the specified file.
|
||||
/// </summary>
|
||||
@@ -335,14 +360,14 @@ public static class GraphicsDeviceExtensions
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads a new readonly 2D texture with the contents of the specified file.
|
||||
/// Loads a new readonly 2D texture with the contents of the specified buffer.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
|
||||
/// <typeparam name="TPixel">The type of pixels used on the GPU side.</typeparam>
|
||||
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the texture.</param>
|
||||
/// <param name="type">The type of texture to allocate.</param>
|
||||
/// <param name="buffer">The buffer with the image data to load and decode into the texture.</param>
|
||||
/// <returns>A <see cref="Texture2D{T}"/> instance with the contents of the specified file.</returns>
|
||||
/// <returns>A <see cref="Texture2D{T}"/> instance with the contents of the specified buffer.</returns>
|
||||
[Pure]
|
||||
public static Texture2D<T> LoadTexture2D<T, TPixel>(this GraphicsDevice device, Type type, byte[] buffer)
|
||||
where T : unmanaged, IUnorm<TPixel>
|
||||
@@ -356,6 +381,28 @@ public static class GraphicsDeviceExtensions
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads a new readonly 2D texture with the contents of the specified stream.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
|
||||
/// <typeparam name="TPixel">The type of pixels used on the GPU side.</typeparam>
|
||||
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the texture.</param>
|
||||
/// <param name="type">The type of texture to allocate.</param>
|
||||
/// <param name="stream">The stream with the image data to load and decode into the texture.</param>
|
||||
/// <returns>A <see cref="Texture2D{T}"/> instance with the contents of the specified stream.</returns>
|
||||
[Pure]
|
||||
public static Texture2D<T> LoadTexture2D<T, TPixel>(this GraphicsDevice device, Type type, Stream stream)
|
||||
where T : unmanaged, IUnorm<TPixel>
|
||||
where TPixel : unmanaged
|
||||
{
|
||||
return type switch
|
||||
{
|
||||
_ when type == typeof(ReadOnlyTexture2D<,>) => device.LoadReadOnlyTexture2D<T, TPixel>(stream),
|
||||
_ when type == typeof(ReadWriteTexture2D<,>) => device.LoadReadWriteTexture2D<T, TPixel>(stream),
|
||||
_ => throw new ArgumentException($"Invalid type: {type}", nameof(type))
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves a texture to a specified file.
|
||||
/// </summary>
|
||||
|
||||
@@ -27,7 +27,7 @@ public class ImagingTests
|
||||
[Resource(typeof(ReadWriteTexture2D<,>))]
|
||||
[Data(typeof(string))]
|
||||
[Data(typeof(ReadOnlySpan<char>))]
|
||||
public void LoadAsRgba32FromFile(Device device, Type textureType, Type inputType)
|
||||
public void LoadAsRgba32_FromFile(Device device, Type textureType, Type inputType)
|
||||
{
|
||||
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg");
|
||||
|
||||
@@ -39,13 +39,136 @@ public class ImagingTests
|
||||
TolerantImageComparer.AssertEqual(original, loaded, 0.0000032f);
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
[Resource(typeof(ReadOnlyTexture2D<>))]
|
||||
[Resource(typeof(ReadWriteTexture2D<>))]
|
||||
[Data(typeof(string))]
|
||||
[Data(typeof(ReadOnlySpan<char>))]
|
||||
public void LoadAsRgba32_FromFile_WithUploadTexture(Device device, Type textureType, Type inputType)
|
||||
{
|
||||
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg");
|
||||
|
||||
IImageInfo imageInfo = Image.Identify(path);
|
||||
|
||||
using Texture2D<Rgba32> texture = device.Get().AllocateTexture2D<Rgba32>(textureType, imageInfo.Width, imageInfo.Height);
|
||||
using UploadTexture2D<Rgba32> upload = device.Get().AllocateUploadTexture2D<Rgba32>(imageInfo.Width, imageInfo.Height);
|
||||
|
||||
upload.Load(inputType, path);
|
||||
upload.CopyTo(texture);
|
||||
|
||||
using Image<ImageSharpRgba32> loaded = texture.ToImage<Rgba32, ImageSharpRgba32>();
|
||||
using Image<ImageSharpRgba32> original = Image.Load<ImageSharpRgba32>(path);
|
||||
|
||||
TolerantImageComparer.AssertEqual(original, loaded, 0.0000032f);
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
[Resource(typeof(ReadOnlyTexture2D<,>))]
|
||||
[Resource(typeof(ReadWriteTexture2D<,>))]
|
||||
public void LoadAsRgba32_FromBuffer(Device device, Type textureType)
|
||||
{
|
||||
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg");
|
||||
|
||||
byte[] buffer = File.ReadAllBytes(path);
|
||||
|
||||
using Texture2D<Rgba32> texture = device.Get().LoadTexture2D<Rgba32, float4>(textureType, buffer);
|
||||
|
||||
using Image<ImageSharpRgba32> loaded = texture.ToImage<Rgba32, ImageSharpRgba32>();
|
||||
using Image<ImageSharpRgba32> original = Image.Load<ImageSharpRgba32>(path);
|
||||
|
||||
TolerantImageComparer.AssertEqual(original, loaded, 0.0000032f);
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
[Resource(typeof(ReadOnlyTexture2D<>))]
|
||||
[Resource(typeof(ReadWriteTexture2D<>))]
|
||||
public void LoadAsRgba32_FromBuffer_WithUploadTexture(Device device, Type textureType)
|
||||
{
|
||||
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg");
|
||||
|
||||
IImageInfo imageInfo = Image.Identify(path);
|
||||
|
||||
using Texture2D<Rgba32> texture = device.Get().AllocateTexture2D<Rgba32>(textureType, imageInfo.Width, imageInfo.Height);
|
||||
using UploadTexture2D<Rgba32> upload = device.Get().AllocateUploadTexture2D<Rgba32>(imageInfo.Width, imageInfo.Height);
|
||||
|
||||
byte[] buffer = File.ReadAllBytes(path);
|
||||
|
||||
upload.Load(buffer);
|
||||
upload.CopyTo(texture);
|
||||
|
||||
using Image<ImageSharpRgba32> loaded = texture.ToImage<Rgba32, ImageSharpRgba32>();
|
||||
using Image<ImageSharpRgba32> original = Image.Load<ImageSharpRgba32>(path);
|
||||
|
||||
TolerantImageComparer.AssertEqual(original, loaded, 0.0000032f);
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
[Resource(typeof(ReadOnlyTexture2D<,>))]
|
||||
[Resource(typeof(ReadWriteTexture2D<,>))]
|
||||
public void LoadAsRgba32_FromStream(Device device, Type textureType)
|
||||
{
|
||||
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg");
|
||||
|
||||
using Stream stream = File.OpenRead(path);
|
||||
|
||||
using Texture2D<Rgba32> texture = device.Get().LoadTexture2D<Rgba32, float4>(textureType, stream);
|
||||
|
||||
using Image<ImageSharpRgba32> loaded = texture.ToImage<Rgba32, ImageSharpRgba32>();
|
||||
using Image<ImageSharpRgba32> original = Image.Load<ImageSharpRgba32>(path);
|
||||
|
||||
TolerantImageComparer.AssertEqual(original, loaded, 0.0000032f);
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
[Resource(typeof(ReadOnlyTexture2D<>))]
|
||||
[Resource(typeof(ReadWriteTexture2D<>))]
|
||||
public void LoadAsRgba32_FromStream_WithUploadTexture(Device device, Type textureType)
|
||||
{
|
||||
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg");
|
||||
|
||||
IImageInfo imageInfo = Image.Identify(path);
|
||||
|
||||
using Texture2D<Rgba32> texture = device.Get().AllocateTexture2D<Rgba32>(textureType, imageInfo.Width, imageInfo.Height);
|
||||
using UploadTexture2D<Rgba32> upload = device.Get().AllocateUploadTexture2D<Rgba32>(imageInfo.Width, imageInfo.Height);
|
||||
|
||||
using Stream stream = File.OpenRead(path);
|
||||
|
||||
upload.Load(stream);
|
||||
upload.CopyTo(texture);
|
||||
|
||||
using Image<ImageSharpRgba32> loaded = texture.ToImage<Rgba32, ImageSharpRgba32>();
|
||||
using Image<ImageSharpRgba32> original = Image.Load<ImageSharpRgba32>(path);
|
||||
|
||||
TolerantImageComparer.AssertEqual(original, loaded, 0.0000032f);
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
[Data(3840, 2562)]
|
||||
[Data(3839, 2560)]
|
||||
[Data(3844, 2564)]
|
||||
[ExpectedException(typeof(ArgumentException))]
|
||||
public void LoadAsRgba32_FromStream_WithUploadTexture_SizeMismatch(Device device, int width, int height)
|
||||
{
|
||||
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg");
|
||||
|
||||
using UploadTexture2D<Rgba32> upload = device.Get().AllocateUploadTexture2D<Rgba32>(width, height);
|
||||
|
||||
upload.Load(path);
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
[Resource(typeof(ReadOnlyTexture2D<,>))]
|
||||
[Resource(typeof(ReadWriteTexture2D<,>))]
|
||||
[Data(typeof(string))]
|
||||
[Data(typeof(ReadOnlySpan<char>))]
|
||||
public void LoadAsBgra32FromFile(Device device, Type textureType, Type inputType)
|
||||
public void LoadAsBgra32_FromFile(Device device, Type textureType, Type inputType)
|
||||
{
|
||||
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg");
|
||||
|
||||
@@ -57,13 +180,31 @@ public class ImagingTests
|
||||
TolerantImageComparer.AssertEqual(original, loaded, 0.00000132f);
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
[Resource(typeof(ReadOnlyTexture2D<,>))]
|
||||
[Resource(typeof(ReadWriteTexture2D<,>))]
|
||||
public void LoadAsBgra32_FromStream(Device device, Type textureType)
|
||||
{
|
||||
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg");
|
||||
|
||||
using Stream stream = File.OpenRead(path);
|
||||
|
||||
using Texture2D<Bgra32> texture = device.Get().LoadTexture2D<Bgra32, float4>(textureType, stream);
|
||||
|
||||
using Image<ImageSharpBgra32> loaded = texture.ToImage<Bgra32, ImageSharpBgra32>();
|
||||
using Image<ImageSharpBgra32> original = Image.Load<ImageSharpBgra32>(path);
|
||||
|
||||
TolerantImageComparer.AssertEqual(original, loaded, 0.00000132f);
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
[Resource(typeof(ReadOnlyTexture2D<,>))]
|
||||
[Resource(typeof(ReadWriteTexture2D<,>))]
|
||||
[Data(typeof(string))]
|
||||
[Data(typeof(ReadOnlySpan<char>))]
|
||||
public void LoadAsBgra32FromFileWithSameFormat(Device device, Type textureType, Type inputType)
|
||||
public void LoadAsBgra32_FromFile_WithSameFormat(Device device, Type textureType, Type inputType)
|
||||
{
|
||||
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Assets", "CityAfter1024x1024Sampling.png");
|
||||
|
||||
@@ -75,13 +216,31 @@ public class ImagingTests
|
||||
TolerantImageComparer.AssertEqual(original, loaded, 0.00000132f);
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
[Resource(typeof(ReadOnlyTexture2D<,>))]
|
||||
[Resource(typeof(ReadWriteTexture2D<,>))]
|
||||
public void LoadAsBgra32_FromStream_WithSameFormat(Device device, Type textureType)
|
||||
{
|
||||
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Assets", "CityAfter1024x1024Sampling.png");
|
||||
|
||||
using Stream stream = File.OpenRead(path);
|
||||
|
||||
using Texture2D<Bgra32> texture = device.Get().LoadTexture2D<Bgra32, float4>(textureType, stream);
|
||||
|
||||
using Image<ImageSharpBgra32> loaded = texture.ToImage<Bgra32, ImageSharpBgra32>();
|
||||
using Image<ImageSharpBgra32> original = Image.Load<ImageSharpBgra32>(path);
|
||||
|
||||
TolerantImageComparer.AssertEqual(original, loaded, 0.00000132f);
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
[Resource(typeof(ReadOnlyTexture2D<,>))]
|
||||
[Resource(typeof(ReadWriteTexture2D<,>))]
|
||||
[Data(typeof(string))]
|
||||
[Data(typeof(ReadOnlySpan<char>))]
|
||||
public void LoadAsR8FromFile(Device device, Type textureType, Type inputType)
|
||||
public void LoadAsR8_FromFile(Device device, Type textureType, Type inputType)
|
||||
{
|
||||
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg");
|
||||
|
||||
@@ -97,18 +256,18 @@ public class ImagingTests
|
||||
[AllDevices]
|
||||
[Resource(typeof(ReadOnlyTexture2D<,>))]
|
||||
[Resource(typeof(ReadWriteTexture2D<,>))]
|
||||
public void LoadAsRgba32FromBuffer(Device device, Type textureType)
|
||||
public void LoadAsR8_FromFile(Device device, Type textureType)
|
||||
{
|
||||
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg");
|
||||
|
||||
byte[] buffer = File.ReadAllBytes(path);
|
||||
using Stream stream = File.OpenRead(path);
|
||||
|
||||
using Texture2D<Rgba32> texture = device.Get().LoadTexture2D<Rgba32, float4>(textureType, buffer);
|
||||
using Texture2D<R8> texture = device.Get().LoadTexture2D<R8, float>(textureType, stream);
|
||||
|
||||
using Image<ImageSharpRgba32> loaded = texture.ToImage<Rgba32, ImageSharpRgba32>();
|
||||
using Image<ImageSharpRgba32> original = Image.Load<ImageSharpRgba32>(path);
|
||||
using Image<ImageSharpL8> loaded = texture.ToImage<R8, ImageSharpL8>();
|
||||
using Image<ImageSharpL8> original = Image.Load<ImageSharpL8>(path);
|
||||
|
||||
TolerantImageComparer.AssertEqual(original, loaded, 0.0000032f);
|
||||
TolerantImageComparer.AssertEqual(original, loaded, 0.000039f);
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
@@ -121,13 +280,23 @@ public class ImagingTests
|
||||
using Texture2D<Rgba32> texture = device.Get().LoadTexture2D<Rgba32, float4>(textureType, typeof(string), null!);
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[Device(Device.Warp)]
|
||||
[Resource(typeof(ReadOnlyTexture2D<,>))]
|
||||
[Resource(typeof(ReadWriteTexture2D<,>))]
|
||||
[ExpectedException(typeof(ArgumentNullException))]
|
||||
public void LoadAsRgba32_WithNullStream(Device device, Type textureType)
|
||||
{
|
||||
using Texture2D<Rgba32> texture = device.Get().LoadTexture2D<Rgba32, float4>(textureType, (Stream)null!);
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
[Resource(typeof(ReadOnlyTexture2D<,>))]
|
||||
[Resource(typeof(ReadWriteTexture2D<,>))]
|
||||
[Data(typeof(string))]
|
||||
[Data(typeof(ReadOnlySpan<char>))]
|
||||
public void SaveRgba32AsJpeg(Device device, Type textureType, Type inputType)
|
||||
public void SaveRgba32AsJpeg_ToFile(Device device, Type textureType, Type inputType)
|
||||
{
|
||||
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging");
|
||||
string expectedPath = Path.Combine(path, "city.jpg");
|
||||
@@ -140,13 +309,33 @@ public class ImagingTests
|
||||
TolerantImageComparer.AssertEqual(expectedPath, actualPath, 0.00001023f);
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
[Resource(typeof(ReadOnlyTexture2D<,>))]
|
||||
[Resource(typeof(ReadWriteTexture2D<,>))]
|
||||
public void SaveRgba32AsJpeg_ToStream(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<Rgba32> texture = device.Get().LoadTexture2D<Rgba32, float4>(textureType, typeof(string), expectedPath);
|
||||
|
||||
using (Stream stream = File.OpenWrite(actualPath))
|
||||
{
|
||||
texture.Save(stream, ImageFormat.Jpeg);
|
||||
}
|
||||
|
||||
TolerantImageComparer.AssertEqual(expectedPath, actualPath, 0.00001023f);
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
[Resource(typeof(ReadOnlyTexture2D<,>))]
|
||||
[Resource(typeof(ReadWriteTexture2D<,>))]
|
||||
[Data(typeof(string))]
|
||||
[Data(typeof(ReadOnlySpan<char>))]
|
||||
public void SaveRgba32AsJpeg_WithReadBackTexture(Device device, Type textureType, Type inputType)
|
||||
public void SaveRgba32AsJpeg_ToFile_WithReadBackTexture(Device device, Type textureType, Type inputType)
|
||||
{
|
||||
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging");
|
||||
string expectedPath = Path.Combine(path, "city.jpg");
|
||||
@@ -162,12 +351,35 @@ public class ImagingTests
|
||||
TolerantImageComparer.AssertEqual(expectedPath, actualPath, 0.00001023f);
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
[Resource(typeof(ReadOnlyTexture2D<,>))]
|
||||
[Resource(typeof(ReadWriteTexture2D<,>))]
|
||||
public void SaveRgba32AsJpeg_ToStream_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<Rgba32> texture = device.Get().LoadTexture2D<Rgba32, float4>(textureType, typeof(string), expectedPath);
|
||||
using ReadBackTexture2D<Rgba32> readback = device.Get().AllocateReadBackTexture2D<Rgba32>(texture.Width, texture.Height);
|
||||
|
||||
texture.CopyTo(readback);
|
||||
|
||||
using (Stream stream = File.OpenWrite(actualPath))
|
||||
{
|
||||
readback.Save(stream, ImageFormat.Jpeg);
|
||||
}
|
||||
|
||||
TolerantImageComparer.AssertEqual(expectedPath, actualPath, 0.00001023f);
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[Device(Device.Warp)]
|
||||
[Resource(typeof(ReadOnlyTexture2D<,>))]
|
||||
[Resource(typeof(ReadWriteTexture2D<,>))]
|
||||
[ExpectedException(typeof(ArgumentNullException))]
|
||||
public void SaveRgba32AsJpeg_WithReadBackTexture_WithNullPath(Device device, Type textureType)
|
||||
public void SaveRgba32AsJpeg_ToFile_WithReadBackTexture_WithNullPath(Device device, Type textureType)
|
||||
{
|
||||
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg");
|
||||
|
||||
@@ -179,13 +391,30 @@ public class ImagingTests
|
||||
readback.Save(typeof(string), null!);
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[Device(Device.Warp)]
|
||||
[Resource(typeof(ReadOnlyTexture2D<,>))]
|
||||
[Resource(typeof(ReadWriteTexture2D<,>))]
|
||||
[ExpectedException(typeof(ArgumentNullException))]
|
||||
public void SaveRgba32AsJpeg_ToStream_WithReadBackTexture_WithNullStream(Device device, Type textureType)
|
||||
{
|
||||
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg");
|
||||
|
||||
using Texture2D<Rgba32> texture = device.Get().LoadTexture2D<Rgba32, float4>(textureType, typeof(string), path);
|
||||
using ReadBackTexture2D<Rgba32> readback = device.Get().AllocateReadBackTexture2D<Rgba32>(texture.Width, texture.Height);
|
||||
|
||||
texture.CopyTo(readback);
|
||||
|
||||
readback.Save(null!, ImageFormat.Jpeg);
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
[Resource(typeof(ReadOnlyTexture2D<,>))]
|
||||
[Resource(typeof(ReadWriteTexture2D<,>))]
|
||||
[Data(typeof(string))]
|
||||
[Data(typeof(ReadOnlySpan<char>))]
|
||||
public void SaveBgra32AsJpeg(Device device, Type textureType, Type inputType)
|
||||
public void SaveBgra32AsJpeg_ToFile(Device device, Type textureType, Type inputType)
|
||||
{
|
||||
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging");
|
||||
string expectedPath = Path.Combine(path, "city.jpg");
|
||||
@@ -204,7 +433,7 @@ public class ImagingTests
|
||||
[Resource(typeof(ReadWriteTexture2D<,>))]
|
||||
[Data(typeof(string))]
|
||||
[Data(typeof(ReadOnlySpan<char>))]
|
||||
public void SaveR8AsJpeg(Device device, Type textureType, Type inputType)
|
||||
public void SaveR8AsJpeg_ToFile(Device device, Type textureType, Type inputType)
|
||||
{
|
||||
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging");
|
||||
string sourcePath = Path.Combine(path, "city.jpg");
|
||||
@@ -227,7 +456,7 @@ public class ImagingTests
|
||||
[Resource(typeof(ReadOnlyTexture2D<,>))]
|
||||
[Resource(typeof(ReadWriteTexture2D<,>))]
|
||||
[ExpectedException(typeof(ArgumentNullException))]
|
||||
public void SaveRgba32AsJpeg_WithNullPath(Device device, Type textureType)
|
||||
public void SaveRgba32AsJpeg_ToFile_WithNullPath(Device device, Type textureType)
|
||||
{
|
||||
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg");
|
||||
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする