From c66ac3fe199e19c90b3bbd885d6f8e914ad29f8f Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 11 May 2021 23:24:24 +0200 Subject: [PATCH] Added extension to load a texture from a buffer --- .../Extensions/GraphicsDeviceExtensions.cs | 16 ++++++ .../Graphics/Helpers/WICHelper.cs | 51 ++++++++++++++++++- tests/ComputeSharp.Tests/ImagingTests.cs | 21 ++++++-- 3 files changed, 84 insertions(+), 4 deletions(-) diff --git a/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.cs b/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.cs index 6ea3e81d..16378891 100644 --- a/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.cs @@ -344,6 +344,22 @@ namespace ComputeSharp return WICHelper.Instance.LoadTexture(device, filename); } + /// + /// Allocates a new readonly 2D texture with the contents of the specified buffer. + /// + /// 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 buffer with the image data to load and decode into the texture. + /// A instance with the contents of the specified file. + [Pure] + public static ReadOnlyTexture2D AllocateReadOnlyTexture2D(this GraphicsDevice device, ReadOnlySpan span) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + return WICHelper.Instance.LoadTexture(device, span); + } + /// /// Allocates a new readonly 3D texture with the specified parameters. /// diff --git a/src/ComputeSharp/Graphics/Helpers/WICHelper.cs b/src/ComputeSharp/Graphics/Helpers/WICHelper.cs index 97ebeb9d..61cee873 100644 --- a/src/ComputeSharp/Graphics/Helpers/WICHelper.cs +++ b/src/ComputeSharp/Graphics/Helpers/WICHelper.cs @@ -76,10 +76,59 @@ namespace ComputeSharp.Graphics.Helpers wicBitmapDecoder.GetAddressOf()).Assert(); } + return LoadTexture(device, wicBitmapDecoder.Get()); + } + + /// + /// Loads a from a specified buffer. + /// + /// 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 buffer with the image data to load and decode into the texture. + /// A instance with the contents of the specified file. + [Pure] + public ReadOnlyTexture2D LoadTexture(GraphicsDevice device, ReadOnlySpan span) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + using ComPtr wicBitmapDecoder = default; + using ComPtr 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(); + + return LoadTexture(device, wicBitmapDecoder.Get()); + } + } + + /// + /// Loads a from a specified object. + /// + /// 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 object in use. + /// A instance with the contents of the specified file. + [Pure] + public ReadOnlyTexture2D LoadTexture(GraphicsDevice device, IWICBitmapDecoder* wicBitmapDecoder) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { 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(); + wicBitmapDecoder->GetFrame(0, wicBitmapFrameDecode.GetAddressOf()).Assert(); using ComPtr wicFormatConverter = default; Guid wicPixelFormatGuid = WICFormatHelper.GetForType(); diff --git a/tests/ComputeSharp.Tests/ImagingTests.cs b/tests/ComputeSharp.Tests/ImagingTests.cs index 14f2764b..752a2cb6 100644 --- a/tests/ComputeSharp.Tests/ImagingTests.cs +++ b/tests/ComputeSharp.Tests/ImagingTests.cs @@ -9,7 +9,6 @@ using ComputeSharp.Tests.Extensions; using Microsoft.VisualStudio.TestTools.UnitTesting; using SixLabors.ImageSharp; using ImageSharpRgba32 = SixLabors.ImageSharp.PixelFormats.Rgba32; -using ImageSharpBgra32 = SixLabors.ImageSharp.PixelFormats.Bgra32; namespace ComputeSharp.Tests { @@ -19,11 +18,27 @@ namespace ComputeSharp.Tests { [CombinatorialTestMethod] [AllDevices] - public void LoadImageAsRgba32(Device device) + public void LoadAsRgba32FromFile(Device device) { string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg"); - using ReadOnlyTexture2D texture = Gpu.Default.AllocateReadOnlyTexture2D(path); + using ReadOnlyTexture2D texture = device.Get().AllocateReadOnlyTexture2D(path); + + using Image loaded = texture.ToImage(); + using Image original = Image.Load(path); + + TolerantImageComparer.AssertEqual(original, loaded, 0.0000032f); + } + + [CombinatorialTestMethod] + [AllDevices] + public void LoadAsRgba32FromBuffer(Device device) + { + string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg"); + + byte[] buffer = File.ReadAllBytes(path); + + using ReadOnlyTexture2D texture = device.Get().AllocateReadOnlyTexture2D(buffer); using Image loaded = texture.ToImage(); using Image original = Image.Load(path);