From cd9d7273fe5367b316eb3a29c670ca2f87a37d89 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 30 Jan 2022 13:04:26 +0100 Subject: [PATCH] Refactor sample to use top level statements --- .../ComputeSharp.Sample.Shared.projitems | 1 + .../Helpers/Formatting.cs | 45 ++++++++++ samples/ComputeSharp.Sample.Shared/Program.cs | 90 ++++++------------- 3 files changed, 74 insertions(+), 62 deletions(-) create mode 100644 samples/ComputeSharp.Sample.Shared/Helpers/Formatting.cs diff --git a/samples/ComputeSharp.Sample.Shared/ComputeSharp.Sample.Shared.projitems b/samples/ComputeSharp.Sample.Shared/ComputeSharp.Sample.Shared.projitems index f30f1996..ea973d4d 100644 --- a/samples/ComputeSharp.Sample.Shared/ComputeSharp.Sample.Shared.projitems +++ b/samples/ComputeSharp.Sample.Shared/ComputeSharp.Sample.Shared.projitems @@ -9,6 +9,7 @@ ComputeSharp.Sample.Shared + \ No newline at end of file diff --git a/samples/ComputeSharp.Sample.Shared/Helpers/Formatting.cs b/samples/ComputeSharp.Sample.Shared/Helpers/Formatting.cs new file mode 100644 index 00000000..6fbe1b6e --- /dev/null +++ b/samples/ComputeSharp.Sample.Shared/Helpers/Formatting.cs @@ -0,0 +1,45 @@ +using System; +using System.Globalization; +using System.Linq; +using CommunityToolkit.Diagnostics; + +namespace ComputeSharp.Sample; + +/// +/// Internal helpers for the sample to format values. +/// +internal static class Formatting +{ + /// + /// Prints a matrix in a properly formatted way. + /// + /// The input array representing the matrix to print. + /// The width of the array to print. + /// The height of the array to print. + /// The name of the matrix to print. + public static void PrintMatrix(float[] array, int width, int height, string name) + { + Guard.IsNotNull(array); + Guard.IsGreaterThan(width, 0); + Guard.IsGreaterThan(height, 0); + Guard.IsEqualTo(array.Length, (long)width * height, nameof(array)); + Guard.IsNotNullOrEmpty(name); + + int pad = 48 - name.Length; + string title = $"{new string('=', pad / 2)} {name} {new string('=', (pad + 1) / 2)}"; + + Console.WriteLine(title); + + int numberWidth = Math.Max(array.Max().ToString(CultureInfo.InvariantCulture).Length, 4); + + for (int i = 0; i < height; i++) + { + var row = array.AsSpan(i * width, width).ToArray(); + var text = string.Join(",", row.Select(x => x.ToString(CultureInfo.InvariantCulture).PadLeft(numberWidth))); + + Console.WriteLine(text); + } + + Console.WriteLine(new string('=', 50)); + } +} diff --git a/samples/ComputeSharp.Sample.Shared/Program.cs b/samples/ComputeSharp.Sample.Shared/Program.cs index d52553d1..c1962f0a 100644 --- a/samples/ComputeSharp.Sample.Shared/Program.cs +++ b/samples/ComputeSharp.Sample.Shared/Program.cs @@ -1,70 +1,36 @@ using System; -using System.Globalization; using System.Linq; +using ComputeSharp; +using ComputeSharp.Sample; -namespace ComputeSharp.Sample; +float[] array = Enumerable.Range(1, 100).Select(static i => (float)i).ToArray(); -partial class Program +// Create the graphics buffer +using ReadWriteBuffer gpuBuffer = GraphicsDevice.Default.AllocateReadWriteBuffer(array); + +// Run the shader +GraphicsDevice.Default.For(100, new MultiplyByTwo(gpuBuffer)); + +// Print the initial matrix +Formatting.PrintMatrix(array, 10, 10, "BEFORE"); + +// Get the data back +gpuBuffer.CopyTo(array); + +// Print the updated matrix +Formatting.PrintMatrix(array, 10, 10, "AFTER"); + +/// +/// The sample kernel that multiples all items by two. +/// +[AutoConstructor] +internal readonly partial struct MultiplyByTwo : IComputeShader { - static void Main() + public readonly ReadWriteBuffer buffer; + + /// + public void Execute() { - float[] array = Enumerable.Range(1, 100).Select(static i => (float)i).ToArray(); - - // Create the graphics buffer - using ReadWriteBuffer gpuBuffer = GraphicsDevice.Default.AllocateReadWriteBuffer(array); - - // Run the shader - GraphicsDevice.Default.For(100, new MainKernel(gpuBuffer)); - - // Print the initial matrix - PrintMatrix(array, 10, 10, "BEFORE"); - - // Get the data back - gpuBuffer.CopyTo(array); - - // Print the updated matrix - PrintMatrix(array, 10, 10, "AFTER"); - } - - /// - /// Kernel for . - /// - [AutoConstructor] - internal readonly partial struct MainKernel : IComputeShader - { - public readonly ReadWriteBuffer buffer; - - /// - public void Execute() - { - buffer[ThreadIds.X] *= 2; - } - } - - /// - /// Prints a matrix in a properly formatted way. - /// - /// The input array representing the matrix to print. - /// The width of the array to print. - /// The height of the array to print. - /// The name of the matrix to print. - private static void PrintMatrix(float[] array, int width, int height, string name) - { - int pad = 48 - name.Length; - string title = $"{new string('=', pad / 2)} {name} {new string('=', (pad + 1) / 2)}"; - - Console.WriteLine(title); - - int numberWidth = Math.Max(array.Max().ToString(CultureInfo.InvariantCulture).Length, 4); - - for (int i = 0; i < height; i++) - { - var row = array.AsSpan(i * width, width).ToArray(); - var text = string.Join(",", row.Select(x => x.ToString(CultureInfo.InvariantCulture).PadLeft(numberWidth))); - - Console.WriteLine(text); - } - - Console.WriteLine(new string('=', 50)); + buffer[ThreadIds.X] *= 2; } }