ファイル
ComputeSharp/samples/ComputeSharp.Sample/Program.cs
T

36 行
899 B
C#

using System;
using System.Linq;
using ComputeSharp;
using ComputeSharp.Sample;
float[] array = Enumerable.Range(1, 100).Select(static i => (float)i).ToArray();
// Create the graphics buffer
using ReadWriteBuffer<float> gpuBuffer = GraphicsDevice.GetDefault().AllocateReadWriteBuffer(array);
// Run the shader
GraphicsDevice.GetDefault().For(100, new MultiplyByTwo(gpuBuffer));
// Print the initial matrix
Formatting.PrintMatrix(array, 10, 10, "BEFORE");
// Get the data back
gpuBuffer.CopyTo(array);
// Print the updated matrix
Formatting.PrintMatrix(array, 10, 10, "AFTER");
/// <summary>
/// The sample kernel that multiples all items by two.
/// </summary>
[AutoConstructor]
internal readonly partial struct MultiplyByTwo : IComputeShader
{
public readonly ReadWriteBuffer<float> buffer;
/// <inheritdoc/>
public void Execute()
{
buffer[ThreadIds.X] *= 2;
}
}