From 82a14c2e5c60284405db2ccc2eecd82b76833ba5 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 17 Aug 2019 23:23:26 +0200 Subject: [PATCH] Code tweaks to the sample project --- samples/ComputeSharp.Sample/Program.cs | 44 +++++++++++-------- .../Translation/ShaderLoader.cs | 2 +- 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/samples/ComputeSharp.Sample/Program.cs b/samples/ComputeSharp.Sample/Program.cs index bfbf7acf..27ce5170 100644 --- a/samples/ComputeSharp.Sample/Program.cs +++ b/samples/ComputeSharp.Sample/Program.cs @@ -1,12 +1,14 @@ using System; +using System.Globalization; using System.Linq; +using System.Runtime.CompilerServices; using ComputeSharp.Graphics.Buffers; namespace ComputeSharp.Sample { - public class Program + class Program { - private static void Main() + static void Main() { // Create the graphics buffer int width = 10; @@ -18,42 +20,48 @@ namespace ComputeSharp.Sample } // Print the initial matrix - Console.WriteLine("Before:"); + Console.WriteLine("===================== BEFORE ====================="); PrintMatrix(array, width, height); + Console.WriteLine("=================================================="); + Console.WriteLine(); using ReadWriteBuffer gpuBuffer = Gpu.Default.AllocateReadWriteBuffer(array); - // Shader body - Action action = id => + // Run the shader + Gpu.Default.For(100, id => { uint offset = id.X + id.Y * (uint)width; gpuBuffer[offset] *= 2; - }; - - // Run the shader - Gpu.Default.For(100, action); + }); // Get the data back gpuBuffer.GetData(array); // Print the updated matrix - Console.WriteLine("After:"); + Console.WriteLine("===================== AFTER ======================"); PrintMatrix(array, width, height); + Console.WriteLine("=================================================="); } + /// + /// 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 numberWidth = array.Max().ToString().Length; + int length = width * height; + int numberWidth = array.Max().ToString(CultureInfo.InvariantCulture).Length; + ref float r = ref array[0]; - for (int y = 0; y < height; y++) + for (int i = 0; i < length; i++) { - for (int x = 0; x < width; x++) - { - Console.Write(array[x + y * width].ToString().PadLeft(numberWidth)); - Console.Write(", "); - } + Console.Write(r.ToString(CultureInfo.InvariantCulture).PadLeft(numberWidth)); + r = ref Unsafe.Add(ref r, 1); - Console.WriteLine(); + if (i < length - 1) Console.Write(", "); + if (i > 0 && (i + 1) % width == 0) Console.WriteLine(); } } } diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs index 4bbb7a80..14c8395a 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs @@ -192,7 +192,7 @@ namespace ComputeSharp.Shaders.Translation MethodBody = root.Body.ToFullString(); // Additional preprocessing - MethodBody = Regex.Replace(MethodBody, @"\d+[fFdD]", m => m.Value.Replace("f", "")); + MethodBody = Regex.Replace(MethodBody, @"(?<=\W)(\d+)[fFdD]", m => m.Groups[1].Value); MethodBody = MethodBody.TrimEnd('\n', '\r', ' '); MethodBody = Regex.Replace(MethodBody, @"(?