From 06aef8247065416f89db3e21a8e397a44850d542 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 13 May 2021 15:03:12 +0200 Subject: [PATCH] Renamed texture loading extensions, code refactoring --- samples/ComputeSharp.SwapChain/Program.cs | 2 +- .../GraphicsDeviceExtensions.Dispatching.cs | 104 ++++++++ ... => GraphicsDeviceExtensions.Resources.cs} | 248 ++++++------------ tests/ComputeSharp.Tests/ImagingTests.cs | 16 +- tests/ComputeSharp.Tests/ShadersTests.cs | 2 +- .../Texture2DTests.Pixels.cs | 4 +- 6 files changed, 194 insertions(+), 182 deletions(-) create mode 100644 src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Dispatching.cs rename src/ComputeSharp/Graphics/Extensions/{GraphicsDeviceExtensions.cs => GraphicsDeviceExtensions.Resources.cs} (90%) diff --git a/samples/ComputeSharp.SwapChain/Program.cs b/samples/ComputeSharp.SwapChain/Program.cs index cdc982cf..7e7332f2 100644 --- a/samples/ComputeSharp.SwapChain/Program.cs +++ b/samples/ComputeSharp.SwapChain/Program.cs @@ -68,7 +68,7 @@ namespace ComputeSharp.SwapChain { string filename = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Textures", $"{name}.png"); - return Gpu.Default.AllocateReadOnlyTexture2D(filename); + return Gpu.Default.LoadReadOnlyTexture2D(filename); } } } diff --git a/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Dispatching.cs b/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Dispatching.cs new file mode 100644 index 00000000..543ec018 --- /dev/null +++ b/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Dispatching.cs @@ -0,0 +1,104 @@ +using System; +using System.Diagnostics.Contracts; +using ComputeSharp.__Internals; +using ComputeSharp.Graphics.Helpers; +using ComputeSharp.Resources; +using ComputeSharp.Shaders; + +namespace ComputeSharp +{ + /// + /// A that contains extension methods for the type, used to run compute shaders. + /// + public static partial class GraphicsDeviceExtensions + { + /// + /// Compiles and runs the input shader on a target instance, with the specified parameters. + /// + /// The type of compute shader to run. + /// The to use to run the shader. + /// The number of iterations to run on the X axis. + /// The input instance representing the compute shader to run. + public static void For(this GraphicsDevice device, int x, in T shader) + where T : struct, IComputeShader + { + ShaderRunner.Run(device, x, 1, 1, in shader); + } + + /// + /// Compiles and runs the input shader on a target instance, with the specified parameters. + /// + /// The type of compute shader to run. + /// The to use to run the shader. + /// The number of iterations to run on the X axis. + /// The number of iterations to run on the Y axis. + /// The input instance representing the compute shader to run. + public static void For(this GraphicsDevice device, int x, int y, in T shader) + where T : struct, IComputeShader + { + ShaderRunner.Run(device, x, y, 1, in shader); + } + + /// + /// Compiles and runs the input shader on a target instance, with the specified parameters. + /// + /// The type of compute shader to run. + /// The to use to run the shader. + /// The number of iterations to run on the X axis. + /// The number of iterations to run on the Y axis. + /// The number of iterations to run on the Z axis. + /// The input instance representing the compute shader to run. + public static void For(this GraphicsDevice device, int x, int y, int z, in T shader) + where T : struct, IComputeShader + { + ShaderRunner.Run(device, x, y, z, in shader); + } + + /// + /// Compiles and runs the input shader on a target instance, with the specified parameters. + /// + /// The type of compute shader to run. + /// The to use to run the shader. + /// The number of iterations to run on the X axis. + /// The number of iterations to run on the Y axis. + /// The number of iterations to run on the Z axis. + /// The number of threads in each thread group for the X axis. + /// The number of threads in each thread group for the Y axis. + /// The number of threads in each thread group for the Z axis. + /// The input instance representing the compute shader to run. + public static void For(this GraphicsDevice device, int x, int y, int z, int threadsX, int threadsY, int threadsZ, in T shader) + where T : struct, IComputeShader + { + ShaderRunner.Run(device, x, y, z, threadsX, threadsY, threadsZ, in shader); + } + + /// + /// Compiles and runs the input shader on a target instance, with the specified parameters. + /// + /// The type of pixel shader to run. + /// The type of pixels being processed by the shader. + /// The to use to run the shader. + /// The target texture to apply the pixel shader to. + public static void ForEach(this GraphicsDevice device, IReadWriteTexture2D texture) + where T : struct, IPixelShader + where TPixel : unmanaged + { + ShaderRunner.Run(device, texture, default); + } + + /// + /// Compiles and runs the input shader on a target instance, with the specified parameters. + /// + /// The type of pixel shader to run. + /// The type of pixels being processed by the shader. + /// The to use to run the shader. + /// The target texture to apply the pixel shader to. + /// The input instance representing the pixel shader to run. + public static void ForEach(this GraphicsDevice device, IReadWriteTexture2D texture, in T shader) + where T : struct, IPixelShader + where TPixel : unmanaged + { + ShaderRunner.Run(device, texture, in shader); + } + } +} diff --git a/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.cs b/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Resources.cs similarity index 90% rename from src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.cs rename to src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Resources.cs index 6d65673e..99f2f3f2 100644 --- a/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Resources.cs @@ -3,16 +3,13 @@ using System.Diagnostics.Contracts; using ComputeSharp.__Internals; using ComputeSharp.Graphics.Helpers; using ComputeSharp.Resources; -using ComputeSharp.Shaders; #pragma warning disable CS0618 namespace ComputeSharp { - /// - /// A that contains extension methods for the type, used to run compute shaders. - /// - public static class GraphicsDeviceExtensions + /// + public static partial class GraphicsDeviceExtensions { /// /// Allocates a new constant buffer with the specified parameters. @@ -328,50 +325,6 @@ namespace ComputeSharp return texture; } - /// - /// Allocates a new readonly 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 ReadOnlyTexture2D AllocateReadOnlyTexture2D(this GraphicsDevice device, ReadOnlySpan filename) - where T : unmanaged, IUnorm - where TPixel : unmanaged - { - using UploadTexture2D upload = WICHelper.Instance.LoadTexture(device, filename); - - ReadOnlyTexture2D texture = device.AllocateReadOnlyTexture2D(upload.Width, upload.Height); - - upload.CopyTo(texture); - - return texture; - } - - /// - /// 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 - { - using UploadTexture2D upload = WICHelper.Instance.LoadTexture(device, span); - - ReadOnlyTexture2D texture = device.AllocateReadOnlyTexture2D(upload.Width, upload.Height); - - upload.CopyTo(texture); - - return texture; - } - /// /// Allocates a new readonly 3D texture with the specified parameters. /// @@ -821,50 +774,6 @@ 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. /// @@ -1162,92 +1071,91 @@ namespace ComputeSharp } /// - /// Compiles and runs the input shader on a target instance, with the specified parameters. + /// Loads a new readonly 2D texture with the contents of the specified file. /// - /// The type of compute shader to run. - /// The to use to run the shader. - /// The number of iterations to run on the X axis. - /// The input instance representing the compute shader to run. - public static void For(this GraphicsDevice device, int x, in T shader) - where T : struct, IComputeShader - { - ShaderRunner.Run(device, x, 1, 1, in shader); - } - - /// - /// Compiles and runs the input shader on a target instance, with the specified parameters. - /// - /// The type of compute shader to run. - /// The to use to run the shader. - /// The number of iterations to run on the X axis. - /// The number of iterations to run on the Y axis. - /// The input instance representing the compute shader to run. - public static void For(this GraphicsDevice device, int x, int y, in T shader) - where T : struct, IComputeShader - { - ShaderRunner.Run(device, x, y, 1, in shader); - } - - /// - /// Compiles and runs the input shader on a target instance, with the specified parameters. - /// - /// The type of compute shader to run. - /// The to use to run the shader. - /// The number of iterations to run on the X axis. - /// The number of iterations to run on the Y axis. - /// The number of iterations to run on the Z axis. - /// The input instance representing the compute shader to run. - public static void For(this GraphicsDevice device, int x, int y, int z, in T shader) - where T : struct, IComputeShader - { - ShaderRunner.Run(device, x, y, z, in shader); - } - - /// - /// Compiles and runs the input shader on a target instance, with the specified parameters. - /// - /// The type of compute shader to run. - /// The to use to run the shader. - /// The number of iterations to run on the X axis. - /// The number of iterations to run on the Y axis. - /// The number of iterations to run on the Z axis. - /// The number of threads in each thread group for the X axis. - /// The number of threads in each thread group for the Y axis. - /// The number of threads in each thread group for the Z axis. - /// The input instance representing the compute shader to run. - public static void For(this GraphicsDevice device, int x, int y, int z, int threadsX, int threadsY, int threadsZ, in T shader) - where T : struct, IComputeShader - { - ShaderRunner.Run(device, x, y, z, threadsX, threadsY, threadsZ, in shader); - } - - /// - /// Compiles and runs the input shader on a target instance, with the specified parameters. - /// - /// The type of pixel shader to run. - /// The type of pixels being processed by the shader. - /// The to use to run the shader. - /// The target texture to apply the pixel shader to. - public static void ForEach(this GraphicsDevice device, IReadWriteTexture2D texture) - where T : struct, IPixelShader + /// 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 ReadOnlyTexture2D LoadReadOnlyTexture2D(this GraphicsDevice device, ReadOnlySpan filename) + where T : unmanaged, IUnorm where TPixel : unmanaged { - ShaderRunner.Run(device, texture, default); + using UploadTexture2D upload = WICHelper.Instance.LoadTexture(device, filename); + + ReadOnlyTexture2D texture = device.AllocateReadOnlyTexture2D(upload.Width, upload.Height); + + upload.CopyTo(texture); + + return texture; } /// - /// Compiles and runs the input shader on a target instance, with the specified parameters. + /// Loads a new readonly 2D texture with the contents of the specified buffer. /// - /// The type of pixel shader to run. - /// The type of pixels being processed by the shader. - /// The to use to run the shader. - /// The target texture to apply the pixel shader to. - /// The input instance representing the pixel shader to run. - public static void ForEach(this GraphicsDevice device, IReadWriteTexture2D texture, in T shader) - where T : struct, IPixelShader + /// 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 LoadReadOnlyTexture2D(this GraphicsDevice device, ReadOnlySpan span) + where T : unmanaged, IUnorm where TPixel : unmanaged { - ShaderRunner.Run(device, texture, in shader); + using UploadTexture2D upload = WICHelper.Instance.LoadTexture(device, span); + + ReadOnlyTexture2D texture = device.AllocateReadOnlyTexture2D(upload.Width, upload.Height); + + upload.CopyTo(texture); + + return texture; + } + + /// + /// Loads 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 LoadReadWriteTexture2D(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; + } + + /// + /// Loads 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 LoadReadWriteTexture2D(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; } } } diff --git a/tests/ComputeSharp.Tests/ImagingTests.cs b/tests/ComputeSharp.Tests/ImagingTests.cs index f3682bbf..632e6fd2 100644 --- a/tests/ComputeSharp.Tests/ImagingTests.cs +++ b/tests/ComputeSharp.Tests/ImagingTests.cs @@ -26,7 +26,7 @@ namespace ComputeSharp.Tests { string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg"); - using ReadOnlyTexture2D texture = device.Get().AllocateReadOnlyTexture2D(path); + using ReadOnlyTexture2D texture = device.Get().LoadReadOnlyTexture2D(path); using Image loaded = texture.ToImage(); using Image original = Image.Load(path); @@ -40,7 +40,7 @@ namespace ComputeSharp.Tests { string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg"); - using ReadOnlyTexture2D texture = device.Get().AllocateReadOnlyTexture2D(path); + using ReadOnlyTexture2D texture = device.Get().LoadReadOnlyTexture2D(path); using Image loaded = texture.ToImage(); using Image original = Image.Load(path); @@ -54,7 +54,7 @@ namespace ComputeSharp.Tests { string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Assets", "CityAfter1024x1024Sampling.png"); - using ReadOnlyTexture2D texture = device.Get().AllocateReadOnlyTexture2D(path); + using ReadOnlyTexture2D texture = device.Get().LoadReadOnlyTexture2D(path); using Image loaded = texture.ToImage(); using Image original = Image.Load(path); @@ -68,7 +68,7 @@ namespace ComputeSharp.Tests { string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg"); - using ReadOnlyTexture2D texture = device.Get().AllocateReadOnlyTexture2D(path); + using ReadOnlyTexture2D texture = device.Get().LoadReadOnlyTexture2D(path); using Image loaded = texture.ToImage(); using Image original = Image.Load(path); @@ -84,7 +84,7 @@ namespace ComputeSharp.Tests byte[] buffer = File.ReadAllBytes(path); - using ReadOnlyTexture2D texture = device.Get().AllocateReadOnlyTexture2D(buffer); + using ReadOnlyTexture2D texture = device.Get().LoadReadOnlyTexture2D(buffer); using Image loaded = texture.ToImage(); using Image original = Image.Load(path); @@ -101,7 +101,7 @@ namespace ComputeSharp.Tests expectedPath = Path.Combine(path, "city.jpg"), actualPath = Path.Combine(path, "city_rgba32_saved.jpg"); - using ReadOnlyTexture2D texture = device.Get().AllocateReadOnlyTexture2D(expectedPath); + using ReadOnlyTexture2D texture = device.Get().LoadReadOnlyTexture2D(expectedPath); texture.Save(actualPath); @@ -117,7 +117,7 @@ namespace ComputeSharp.Tests expectedPath = Path.Combine(path, "city.jpg"), actualPath = Path.Combine(path, "city_bgra32_saved.jpg"); - using ReadOnlyTexture2D texture = device.Get().AllocateReadOnlyTexture2D(expectedPath); + using ReadOnlyTexture2D texture = device.Get().LoadReadOnlyTexture2D(expectedPath); texture.Save(actualPath); @@ -134,7 +134,7 @@ namespace ComputeSharp.Tests expectedPath = Path.Combine(path, "city_r8_reference.jpg"), actualPath = Path.Combine(path, "city_r8_saved.jpg"); - using ReadOnlyTexture2D texture = device.Get().AllocateReadOnlyTexture2D(sourcePath); + using ReadOnlyTexture2D texture = device.Get().LoadReadOnlyTexture2D(sourcePath); texture.Save(actualPath); diff --git a/tests/ComputeSharp.Tests/ShadersTests.cs b/tests/ComputeSharp.Tests/ShadersTests.cs index 2a3bdf14..f8bad618 100644 --- a/tests/ComputeSharp.Tests/ShadersTests.cs +++ b/tests/ComputeSharp.Tests/ShadersTests.cs @@ -105,7 +105,7 @@ namespace ComputeSharp.Tests { string filename = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Shaders", "Textures", "RustyMetal.png"); - using ReadOnlyTexture2D background = device.Get().AllocateReadOnlyTexture2D(filename); + using ReadOnlyTexture2D background = device.Get().LoadReadOnlyTexture2D(filename); RunAndCompareShader( device, diff --git a/tests/ComputeSharp.Tests/Texture2DTests.Pixels.cs b/tests/ComputeSharp.Tests/Texture2DTests.Pixels.cs index 37fa62fe..63236271 100644 --- a/tests/ComputeSharp.Tests/Texture2DTests.Pixels.cs +++ b/tests/ComputeSharp.Tests/Texture2DTests.Pixels.cs @@ -76,7 +76,7 @@ namespace ComputeSharp.Tests using var sampled = Image.Load(Path.Combine(assetsPath, "CityAfter1024x1024Sampling.png")); - using ReadOnlyTexture2D source = device.Get().AllocateReadOnlyTexture2D(Path.Combine(imagingPath, "city.jpg")); + using ReadOnlyTexture2D source = device.Get().LoadReadOnlyTexture2D(Path.Combine(imagingPath, "city.jpg")); using ReadWriteTexture2D destination = device.Get().AllocateReadWriteTexture2D(sampled.Width, sampled.Height); device.Get().For(sampled.Width, sampled.Height, new SamplingComputeShader(source, destination)); @@ -109,7 +109,7 @@ namespace ComputeSharp.Tests using var sampled = Image.Load(Path.Combine(assetsPath, "CityAfter1024x1024Sampling.png")); - using ReadOnlyTexture2D source = device.Get().AllocateReadOnlyTexture2D(Path.Combine(imagingPath, "city.jpg")); + using ReadOnlyTexture2D source = device.Get().LoadReadOnlyTexture2D(Path.Combine(imagingPath, "city.jpg")); using ReadWriteTexture2D destination = device.Get().AllocateReadWriteTexture2D(sampled.Width, sampled.Height); device.Get().ForEach(destination, new SamplingPixelShader(source));