From 6d27a2c1c194e4ed53a413af4551f44b339c6689 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 13 Apr 2020 02:58:18 +0200 Subject: [PATCH] Refactored main sample --- samples/ComputeSharp.Sample/Program.cs | 30 +++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/samples/ComputeSharp.Sample/Program.cs b/samples/ComputeSharp.Sample/Program.cs index f94bc6d1..5d2cfcf5 100644 --- a/samples/ComputeSharp.Sample/Program.cs +++ b/samples/ComputeSharp.Sample/Program.cs @@ -27,11 +27,7 @@ namespace ComputeSharp.Sample using ReadWriteBuffer gpuBuffer = Gpu.Default.AllocateReadWriteBuffer(array); // Run the shader - Gpu.Default.For(100, id => - { - int offset = id.X + id.Y * width; - gpuBuffer[offset] *= 2; - }); + Gpu.Default.For(100, new MainKernel(width, gpuBuffer)); // Get the data back gpuBuffer.GetData(array); @@ -42,6 +38,30 @@ namespace ComputeSharp.Sample Console.WriteLine("=================================================="); } + /// + /// Kernel for + /// + private readonly struct MainKernel : IComputeShader + { + private readonly int width; + + private readonly ReadWriteBuffer buffer; + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public MainKernel(int width, ReadWriteBuffer buffer) + { + this.width = width; + this.buffer = buffer; + } + + /// + public void Execute(ThreadIds ids) + { + int offset = ids.X + ids.Y * width; + buffer[offset] *= 2; + } + } + /// /// Prints a matrix in a properly formatted way ///