diff --git a/src/ComputeSharp/Graphics/Helpers/WICFormatHelper.cs b/src/ComputeSharp/Graphics/Helpers/WICFormatHelper.cs new file mode 100644 index 00000000..fe65f07b --- /dev/null +++ b/src/ComputeSharp/Graphics/Helpers/WICFormatHelper.cs @@ -0,0 +1,33 @@ +using System; +using System.Diagnostics.Contracts; +using System.Runtime.CompilerServices; +using Microsoft.Toolkit.Diagnostics; +using FX = TerraFX.Interop.Windows; + +namespace ComputeSharp.Graphics.Helpers +{ + /// + /// A helper type with utility methods for WIC formats. + /// + internal static class WICFormatHelper + { + /// + /// Gets the appropriate WIC format value for the input type argument. + /// + /// The input type argument to get the corresponding WIC format . + /// The WIC format value corresponding to . + /// Thrown when the input type is not supported. + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Guid GetForType() + where T : unmanaged + { + if (typeof(T) == typeof(Bgra32)) return FX.GUID_WICPixelFormat32bppBGRA; + else if (typeof(T) == typeof(Rgba32)) return FX.GUID_WICPixelFormat32bppRGBA; + else if (typeof(T) == typeof(Rgba64)) return FX.GUID_WICPixelFormat64bppRGBA; + else if (typeof(T) == typeof(R8)) return FX.GUID_WICPixelFormat8bppGray; + else if (typeof(T) == typeof(R16)) return FX.GUID_WICPixelFormat16bppGray; + else return ThrowHelper.ThrowArgumentException("Invalid texture type"); + } + } +} diff --git a/src/ComputeSharp/Graphics/Helpers/WICHelper.cs b/src/ComputeSharp/Graphics/Helpers/WICHelper.cs new file mode 100644 index 00000000..97ebeb9d --- /dev/null +++ b/src/ComputeSharp/Graphics/Helpers/WICHelper.cs @@ -0,0 +1,123 @@ +using System; +using System.Diagnostics.Contracts; +using ComputeSharp.__Internals; +using ComputeSharp.Core.Extensions; +using TerraFX.Interop; +using FX = TerraFX.Interop.Windows; + +#pragma warning disable CS0618 + +namespace ComputeSharp.Graphics.Helpers +{ + /// + /// A that uses the WIC APIs to load and decode images. + /// + internal sealed unsafe class WICHelper + { + /// + /// The instance to use to create decoders. + /// + private readonly ComPtr wicImagingFactory2; + + /// + /// Creates a new instance. + /// + private WICHelper() + { + using ComPtr wicImagingFactory2 = default; + Guid wicImagingFactor2Clsid = FX.CLSID_WICImagingFactory2; + + FX.CoCreateInstance( + &wicImagingFactor2Clsid, + null, + (uint)CLSCTX.CLSCTX_INPROC_SERVER, + FX.__uuidof(), + wicImagingFactory2.GetVoidAddressOf()).Assert(); + + this.wicImagingFactory2 = wicImagingFactory2.Move(); + } + + /// + /// Destroys the current instance. + /// + ~WICHelper() + { + this.wicImagingFactory2.Dispose(); + } + + /// + /// Gets a instance to use. + /// + public static WICHelper Instance { get; } = new(); + + /// + /// Loads a from a specified file. + /// + /// The type of items to store in the texture. + /// The type of pixels used on the GPU side. + /// The instance to use to allocate the texture. + /// The filename of the image file to load and decode into the texture. + /// A instance with the contents of the specified file. + [Pure] + public ReadOnlyTexture2D LoadTexture(GraphicsDevice device, ReadOnlySpan filename) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + using ComPtr wicBitmapDecoder = default; + + // Get the bitmap decoder for the target file + fixed (char* p = filename) + { + this.wicImagingFactory2.Get()->CreateDecoderFromFilename( + (ushort*)p, + null, + FX.GENERIC_READ, + WICDecodeOptions.WICDecodeMetadataCacheOnDemand, + wicBitmapDecoder.GetAddressOf()).Assert(); + } + + using ComPtr wicBitmapFrameDecode = default; + + // Get the first frame of the loaded image (if more are present, they will be ignored) + wicBitmapDecoder.Get()->GetFrame(0, wicBitmapFrameDecode.GetAddressOf()).Assert(); + + using ComPtr wicFormatConverter = default; + Guid wicPixelFormatGuid = WICFormatHelper.GetForType(); + + this.wicImagingFactory2.Get()->CreateFormatConverter(wicFormatConverter.GetAddressOf()).Assert(); + + // Get a format converter to decode the pixel data + wicFormatConverter.Get()->Initialize( + (IWICBitmapSource*)wicBitmapFrameDecode.Get(), + &wicPixelFormatGuid, + WICBitmapDitherType.WICBitmapDitherTypeNone, + null, + 0, + WICBitmapPaletteType.WICBitmapPaletteTypeMedianCut).Assert(); + + uint width, height; + + // Extract the image size info + wicFormatConverter.Get()->GetSize(&width, &height).Assert(); + + // Allocate an upload texture to transfer the decoded pixel data + using UploadTexture2D upload = device.AllocateUploadTexture2D((int)width, (int)height); + + T* data = upload.View.DangerousGetAddressAndByteStride(out int strideInBytes); + + // Decode the pixel data into the upload buffer + wicFormatConverter.Get()->CopyPixels( + prc: null, + cbStride: (uint)strideInBytes, + cbBufferSize: (uint)strideInBytes * height, + pbBuffer: (byte*)data).Assert(); + + // Create the final texture to return + ReadOnlyTexture2D texture = device.AllocateReadOnlyTexture2D((int)width, (int)height); + + upload.CopyTo(texture); + + return texture; + } + } +}