From 32be78bbd04a160810844037ee12a477aa455dde Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 13 May 2021 13:19:14 +0200 Subject: [PATCH] Enabled fast path for WIC loading to/from same format --- .../Graphics/Helpers/WICHelper.cs | 77 +++++++++++++------ tests/ComputeSharp.Tests/ImagingTests.cs | 62 +++++++++++++-- 2 files changed, 109 insertions(+), 30 deletions(-) diff --git a/src/ComputeSharp/Graphics/Helpers/WICHelper.cs b/src/ComputeSharp/Graphics/Helpers/WICHelper.cs index 7bde9bf5..1f7d7df3 100644 --- a/src/ComputeSharp/Graphics/Helpers/WICHelper.cs +++ b/src/ComputeSharp/Graphics/Helpers/WICHelper.cs @@ -221,38 +221,65 @@ namespace ComputeSharp.Graphics.Helpers // Get the first frame of the loaded image (if more are present, they will be ignored) wicBitmapDecoder->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(); + wicBitmapFrameDecode.Get()->GetSize(&width, &height).Assert(); - // Allocate an upload texture to transfer the decoded pixel data - UploadTexture2D upload = device.AllocateUploadTexture2D((int)width, (int)height); + Guid + wicTargetPixelFormatGuid = WICFormatHelper.GetForType(), + wicActualPixelFormatGuid; - T* data = upload.View.DangerousGetAddressAndByteStride(out int strideInBytes); + // Get the current and target pixel format info + wicBitmapFrameDecode.Get()->GetPixelFormat(&wicActualPixelFormatGuid).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(); + // If the current pixel format is the same as the target one, we can just read the + // decoded pixel data directly. Otherwise, we need a pixel format conversion step. + if (wicTargetPixelFormatGuid == wicActualPixelFormatGuid) + { + // Allocate an upload texture to transfer the decoded pixel data + UploadTexture2D upload = device.AllocateUploadTexture2D((int)width, (int)height); - return upload; + T* data = upload.View.DangerousGetAddressAndByteStride(out int strideInBytes); + + // Copy the decoded pixels directly from the loaded image + wicBitmapFrameDecode.Get()->CopyPixels( + prc: null, + cbStride: (uint)strideInBytes, + cbBufferSize: (uint)strideInBytes * height, + pbBuffer: (byte*)data).Assert(); + + return upload; + } + else + { + using ComPtr wicFormatConverter = default; + + this.wicImagingFactory2.Get()->CreateFormatConverter(wicFormatConverter.GetAddressOf()).Assert(); + + // Get a format converter to decode the pixel data + wicFormatConverter.Get()->Initialize( + (IWICBitmapSource*)wicBitmapFrameDecode.Get(), + &wicTargetPixelFormatGuid, + WICBitmapDitherType.WICBitmapDitherTypeNone, + null, + 0, + WICBitmapPaletteType.WICBitmapPaletteTypeMedianCut).Assert(); + + // Allocate an upload texture to transfer the converted pixel data + 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(); + + return upload; + } } } } diff --git a/tests/ComputeSharp.Tests/ImagingTests.cs b/tests/ComputeSharp.Tests/ImagingTests.cs index 7d68e8a0..f3682bbf 100644 --- a/tests/ComputeSharp.Tests/ImagingTests.cs +++ b/tests/ComputeSharp.Tests/ImagingTests.cs @@ -8,7 +8,10 @@ using ComputeSharp.Tests.Attributes; using ComputeSharp.Tests.Extensions; using Microsoft.VisualStudio.TestTools.UnitTesting; using SixLabors.ImageSharp; +using SixLabors.ImageSharp.Advanced; +using SixLabors.ImageSharp.PixelFormats; using ImageSharpRgba32 = SixLabors.ImageSharp.PixelFormats.Rgba32; +using ImageSharpBgra32 = SixLabors.ImageSharp.PixelFormats.Bgra32; using ImageSharpL8 = SixLabors.ImageSharp.PixelFormats.L8; namespace ComputeSharp.Tests @@ -31,6 +34,48 @@ namespace ComputeSharp.Tests TolerantImageComparer.AssertEqual(original, loaded, 0.0000032f); } + [CombinatorialTestMethod] + [AllDevices] + public void LoadAsBgra32FromFile(Device device) + { + string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg"); + + using ReadOnlyTexture2D texture = device.Get().AllocateReadOnlyTexture2D(path); + + using Image loaded = texture.ToImage(); + using Image original = Image.Load(path); + + TolerantImageComparer.AssertEqual(original, loaded, 0.00000132f); + } + + [CombinatorialTestMethod] + [AllDevices] + public void LoadAsBgra32FromFileWithSameFormat(Device device) + { + string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Assets", "CityAfter1024x1024Sampling.png"); + + using ReadOnlyTexture2D texture = device.Get().AllocateReadOnlyTexture2D(path); + + using Image loaded = texture.ToImage(); + using Image original = Image.Load(path); + + TolerantImageComparer.AssertEqual(original, loaded, 0.00000132f); + } + + [CombinatorialTestMethod] + [AllDevices] + public void LoadAsR8FromFile(Device device) + { + string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg"); + + using ReadOnlyTexture2D texture = device.Get().AllocateReadOnlyTexture2D(path); + + using Image loaded = texture.ToImage(); + using Image original = Image.Load(path); + + TolerantImageComparer.AssertEqual(original, loaded, 0.000039f); + } + [CombinatorialTestMethod] [AllDevices] public void LoadAsRgba32FromBuffer(Device device) @@ -122,10 +167,12 @@ namespace ComputeSharp.Tests /// /// Asserts that two images are equal. /// + /// The type of image pixels to analyze. /// The reference image. /// The expected image. /// The allowed difference threshold for the normalized delta. - public static void AssertEqual(Image expected, Image actual, float threshold) + public static void AssertEqual(Image expected, Image actual, float threshold) + where TPixel : unmanaged, IPixel { if (expected.Size() != actual.Size()) { @@ -144,11 +191,16 @@ namespace ComputeSharp.Tests float totalDifference = 0F; var differences = new List(20); + Span aBuffer = new ImageSharpRgba32[actual.Width]; + Span bBuffer = new ImageSharpRgba32[actual.Width]; for (int y = 0; y < actual.Height; y++) { - Span aSpan = expected.GetPixelRowSpan(y); - Span bSpan = actual.GetPixelRowSpan(y); + Span aSpan = expected.GetPixelRowSpan(y); + Span bSpan = actual.GetPixelRowSpan(y); + + PixelOperations.Instance.ToRgba32(actual.GetConfiguration(), aSpan, aBuffer); + PixelOperations.Instance.ToRgba32(actual.GetConfiguration(), bSpan, bBuffer); for (int x = 0; x < width; x++) { @@ -161,13 +213,13 @@ namespace ComputeSharp.Tests return Diff(a.R, b.R) + Diff(a.G, b.G) + Diff(a.B, b.B) + Diff(a.A, b.A); } - int d = GetManhattanDistanceInRgbaSpace(ref aSpan[x], ref bSpan[x]); + int d = GetManhattanDistanceInRgbaSpace(ref aBuffer[x], ref bBuffer[x]); if (d > 0) { if (differences.Count < 20) { - var diff = new PixelDifference(new Point(x, y), aSpan[x], bSpan[x]); + var diff = new PixelDifference(new Point(x, y), aBuffer[x], bBuffer[x]); differences.Add(diff); }