using System; using System.Globalization; using System.Linq; using System.Runtime.CompilerServices; namespace ComputeSharp.Sample { partial class Program { static void Main() { // Create the graphics buffer int width = 10; int height = 10; float[] array = new float[width * height]; for (int i = 0; i < array.Length; i++) { array[i] = i + 1; } // Print the initial matrix Console.WriteLine("===================== BEFORE ====================="); PrintMatrix(array, width, height); Console.WriteLine("=================================================="); Console.WriteLine(); using ReadWriteBuffer gpuBuffer = Gpu.Default.AllocateReadWriteBuffer(array); // Run the shader Gpu.Default.For(100, new MainKernel(gpuBuffer)); // Get the data back gpuBuffer.GetData(array); // Print the updated matrix Console.WriteLine("===================== AFTER ======================"); PrintMatrix(array, width, height); Console.WriteLine("=================================================="); } /// /// Kernel for . /// [AutoConstructor] private readonly partial struct MainKernel : IComputeShader { private readonly ReadWriteBuffer buffer; /// public void Execute(ThreadIds ids) { buffer[ids.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. private static void PrintMatrix(float[] array, int width, int height) { int length = width * height; int numberWidth = array.Max().ToString(CultureInfo.InvariantCulture).Length; ref float r = ref array[0]; for (int i = 0; i < length; i++) { Console.Write(r.ToString(CultureInfo.InvariantCulture).PadLeft(numberWidth)); r = ref Unsafe.Add(ref r, 1); if (i < length - 1) Console.Write(", "); if (i > 0 && (i + 1) % width == 0) Console.WriteLine(); } } } }