From 2a45fbcf057567227b47ca6b73a71d6071aa7a84 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 11 Mar 2021 00:24:28 +0100 Subject: [PATCH] Minor tweaks to the C# and F# local samples --- README.md | 4 +-- samples/ComputeSharp.Sample.FSharp/Program.fs | 2 +- samples/ComputeSharp.Sample.Shared/Program.cs | 28 ++++++------------- 3 files changed, 12 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 6ba4bd06..783fcff8 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ Let's suppose we want to run a simple compute shader that multiplies all items i ```csharp // Get some sample data -int[] array = Enumerable.Range(0, 100).ToArray(); +int[] array = Enumerable.Range(1, 100).ToArray(); // Allocate a GPU buffer and copy the data to it. // We want the shader to modify the items in-place, so we @@ -301,7 +301,7 @@ There is a caveat though: since the HLSL shader rewriter specifically works on C Assuming we had the `MultiplyByTwo` shader defined in the [Quick start](#quick-start) paragraph referenced from a C# project, here is how we can actually use it directly from our F# project, with the same sample described above: ```fsharp -let array = [| for i in 0 .. 99 -> (float32)i |] +let array = [| for i in 1 .. 100 -> (float32)i |] // Create the graphics buffer use buffer = Gpu.Default.AllocateReadWriteBuffer(array) diff --git a/samples/ComputeSharp.Sample.FSharp/Program.fs b/samples/ComputeSharp.Sample.FSharp/Program.fs index e07e00f8..397d0079 100644 --- a/samples/ComputeSharp.Sample.FSharp/Program.fs +++ b/samples/ComputeSharp.Sample.FSharp/Program.fs @@ -29,7 +29,7 @@ let printMatrix (array : float32[]) width height (name : string) = [] let main _ = - let array = [| for i in 0 .. 99 -> (float32)i |] + let array = [| for i in 1 .. 100 -> (float32)i |] // Create the graphics buffer use buffer = Gpu.Default.AllocateReadWriteBuffer(array) diff --git a/samples/ComputeSharp.Sample.Shared/Program.cs b/samples/ComputeSharp.Sample.Shared/Program.cs index ee01f2a1..f87c1678 100644 --- a/samples/ComputeSharp.Sample.Shared/Program.cs +++ b/samples/ComputeSharp.Sample.Shared/Program.cs @@ -1,7 +1,6 @@ using System; using System.Globalization; using System.Linq; -using System.Runtime.CompilerServices; namespace ComputeSharp.Sample { @@ -9,28 +8,22 @@ namespace ComputeSharp.Sample { 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; - } + float[] array = Enumerable.Range(1, 100).Select(static i => (float)i).ToArray(); + // Create the graphics buffer using ReadWriteBuffer gpuBuffer = Gpu.Default.AllocateReadWriteBuffer(array); // Run the shader Gpu.Default.For(100, new MainKernel(gpuBuffer)); // Print the initial matrix - PrintMatrix(array, width, height, "BEFORE"); + PrintMatrix(array, 10, 10, "BEFORE"); // Get the data back gpuBuffer.CopyTo(array); // Print the updated matrix - PrintMatrix(array, width, height, "AFTER"); + PrintMatrix(array, 10, 10, "AFTER"); } /// @@ -62,17 +55,14 @@ namespace ComputeSharp.Sample Console.WriteLine(title); - int length = width * height; - int numberWidth = array.Max().ToString(CultureInfo.InvariantCulture).Length; - ref float r = ref array[0]; + int numberWidth = Math.Max(array.Max().ToString(CultureInfo.InvariantCulture).Length, 4); - for (int i = 0; i < length; i++) + for (int i = 0; i < height; i++) { - Console.Write(r.ToString(CultureInfo.InvariantCulture).PadLeft(numberWidth)); - r = ref Unsafe.Add(ref r, 1); + var row = array[(i * width)..((i + 1) * width)]; + var text = string.Join(",", row.Select(x => x.ToString(CultureInfo.InvariantCulture).PadLeft(numberWidth))); - if (i < length - 1) Console.Write(", "); - if (i > 0 && (i + 1) % width == 0) Console.WriteLine(); + Console.WriteLine(text); } Console.WriteLine(new string('=', 50));