using System.Diagnostics; using System.Linq; using ComputeSharp; 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 using (var context = GraphicsDevice.Default.CreateComputeContext()) { context.Log("Dispatching shader"); context.BeginEvent("Dispatching event"); context.For(100, new MultiplyByTwo(gpuBuffer)); context.EndEvent(); } // Get the data back float[] result = gpuBuffer.ToArray(); // Validate results for (int i = 0; i < array.Length; i++) { Trace.Assert(result[i] == array[i] * 2); } /// /// A sample kernel that is precompiled. /// [EmbeddedBytecode(DispatchAxis.X)] [AutoConstructor] internal readonly partial struct MultiplyByTwo : IComputeShader { public readonly ReadWriteBuffer buffer; /// public void Execute() { buffer[ThreadIds.X] *= 2; } }