diff --git a/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.cs b/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.cs index 16378891..6d65673e 100644 --- a/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.cs @@ -341,7 +341,13 @@ namespace ComputeSharp where T : unmanaged, IUnorm where TPixel : unmanaged { - return WICHelper.Instance.LoadTexture(device, filename); + using UploadTexture2D upload = WICHelper.Instance.LoadTexture(device, filename); + + ReadOnlyTexture2D texture = device.AllocateReadOnlyTexture2D(upload.Width, upload.Height); + + upload.CopyTo(texture); + + return texture; } /// @@ -357,7 +363,13 @@ namespace ComputeSharp where T : unmanaged, IUnorm where TPixel : unmanaged { - return WICHelper.Instance.LoadTexture(device, span); + using UploadTexture2D upload = WICHelper.Instance.LoadTexture(device, span); + + ReadOnlyTexture2D texture = device.AllocateReadOnlyTexture2D(upload.Width, upload.Height); + + upload.CopyTo(texture); + + return texture; } /// @@ -809,6 +821,50 @@ namespace ComputeSharp return texture; } + /// + /// Allocates a new writeable 2D texture with the contents of the 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 static ReadWriteTexture2D AllocateReadWriteTexture2D(this GraphicsDevice device, ReadOnlySpan filename) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + using UploadTexture2D upload = WICHelper.Instance.LoadTexture(device, filename); + + ReadWriteTexture2D texture = device.AllocateReadWriteTexture2D(upload.Width, upload.Height); + + upload.CopyTo(texture); + + return texture; + } + + /// + /// Allocates a new writeable 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 ReadWriteTexture2D AllocateReadWriteTexture2D(this GraphicsDevice device, ReadOnlySpan span) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + using UploadTexture2D upload = WICHelper.Instance.LoadTexture(device, span); + + ReadWriteTexture2D texture = device.AllocateReadWriteTexture2D(upload.Width, upload.Height); + + upload.CopyTo(texture); + + return texture; + } + /// /// Allocates a new writeable 3D texture with the specified parameters. /// diff --git a/src/ComputeSharp/Graphics/Helpers/WICHelper.cs b/src/ComputeSharp/Graphics/Helpers/WICHelper.cs index 83d46a75..7bde9bf5 100644 --- a/src/ComputeSharp/Graphics/Helpers/WICHelper.cs +++ b/src/ComputeSharp/Graphics/Helpers/WICHelper.cs @@ -1,12 +1,9 @@ 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 { /// @@ -51,17 +48,15 @@ namespace ComputeSharp.Graphics.Helpers public static WICHelper Instance { get; } = new(); /// - /// Loads a from a specified file. + /// Loads an 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. + /// An instance with the contents of the specified file. [Pure] - public ReadOnlyTexture2D LoadTexture(GraphicsDevice device, ReadOnlySpan filename) - where T : unmanaged, IUnorm - where TPixel : unmanaged + public UploadTexture2D LoadTexture(GraphicsDevice device, ReadOnlySpan filename) + where T : unmanaged { using ComPtr wicBitmapDecoder = default; @@ -76,21 +71,19 @@ namespace ComputeSharp.Graphics.Helpers wicBitmapDecoder.GetAddressOf()).Assert(); } - return LoadTexture(device, wicBitmapDecoder.Get()); + return LoadTexture(device, wicBitmapDecoder.Get()); } /// - /// Loads a from a specified buffer. + /// Loads an 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. + /// An instance with the contents of the specified file. [Pure] - public ReadOnlyTexture2D LoadTexture(GraphicsDevice device, ReadOnlySpan span) - where T : unmanaged, IUnorm - where TPixel : unmanaged + public UploadTexture2D LoadTexture(GraphicsDevice device, ReadOnlySpan span) + where T : unmanaged { using ComPtr wicBitmapDecoder = default; using ComPtr wicStream = default; @@ -108,7 +101,7 @@ namespace ComputeSharp.Graphics.Helpers WICDecodeOptions.WICDecodeMetadataCacheOnDemand, wicBitmapDecoder.GetAddressOf()).Assert(); - return LoadTexture(device, wicBitmapDecoder.Get()); + return LoadTexture(device, wicBitmapDecoder.Get()); } } @@ -213,17 +206,15 @@ namespace ComputeSharp.Graphics.Helpers } /// - /// Loads a from a specified object. + /// Loads an 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. + /// An instance with the contents of the specified file. [Pure] - private ReadOnlyTexture2D LoadTexture(GraphicsDevice device, IWICBitmapDecoder* wicBitmapDecoder) - where T : unmanaged, IUnorm - where TPixel : unmanaged + private UploadTexture2D LoadTexture(GraphicsDevice device, IWICBitmapDecoder* wicBitmapDecoder) + where T : unmanaged { using ComPtr wicBitmapFrameDecode = default; @@ -250,7 +241,7 @@ namespace ComputeSharp.Graphics.Helpers 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); + UploadTexture2D upload = device.AllocateUploadTexture2D((int)width, (int)height); T* data = upload.View.DangerousGetAddressAndByteStride(out int strideInBytes); @@ -261,12 +252,7 @@ namespace ComputeSharp.Graphics.Helpers 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; + return upload; } } }