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

35 行
906 B
C#

using System;
using System.Linq;
using ComputeSharp;
using ComputeSharp.Sample;
float[] array = [.. Enumerable.Range(1, 100)];
// 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>
[ThreadGroupSize(DefaultThreadGroupSizes.X)]
[GeneratedComputeShaderDescriptor]
internal readonly partial struct MultiplyByTwo(ReadWriteBuffer<float> buffer) : IComputeShader
{
/// <inheritdoc/>
public void Execute()
{
buffer[ThreadIds.X] *= 2;
}
}