Refactor sample to use top level statements
このコミットが含まれているのは:
1 親
284b632c45
コミット
cd9d7273fe
3ファイル変更
+74
-62
検索条件に一致するファイルはありません
@@ -9,6 +9,7 @@
|
||||
<Import_RootNamespace>ComputeSharp.Sample.Shared</Import_RootNamespace>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Helpers\Formatting.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Program.cs" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,45 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using CommunityToolkit.Diagnostics;
|
||||
|
||||
namespace ComputeSharp.Sample;
|
||||
|
||||
/// <summary>
|
||||
/// Internal helpers for the sample to format values.
|
||||
/// </summary>
|
||||
internal static class Formatting
|
||||
{
|
||||
/// <summary>
|
||||
/// Prints a matrix in a properly formatted way.
|
||||
/// </summary>
|
||||
/// <param name="array">The input <see langword="float"/> array representing the matrix to print.</param>
|
||||
/// <param name="width">The width of the array to print.</param>
|
||||
/// <param name="height">The height of the array to print.</param>
|
||||
/// <param name="name">The name of the matrix to print.</param>
|
||||
public static void PrintMatrix(float[] array, int width, int height, string name)
|
||||
{
|
||||
Guard.IsNotNull(array);
|
||||
Guard.IsGreaterThan(width, 0);
|
||||
Guard.IsGreaterThan(height, 0);
|
||||
Guard.IsEqualTo(array.Length, (long)width * height, nameof(array));
|
||||
Guard.IsNotNullOrEmpty(name);
|
||||
|
||||
int pad = 48 - name.Length;
|
||||
string title = $"{new string('=', pad / 2)} {name} {new string('=', (pad + 1) / 2)}";
|
||||
|
||||
Console.WriteLine(title);
|
||||
|
||||
int numberWidth = Math.Max(array.Max().ToString(CultureInfo.InvariantCulture).Length, 4);
|
||||
|
||||
for (int i = 0; i < height; i++)
|
||||
{
|
||||
var row = array.AsSpan(i * width, width).ToArray();
|
||||
var text = string.Join(",", row.Select(x => x.ToString(CultureInfo.InvariantCulture).PadLeft(numberWidth)));
|
||||
|
||||
Console.WriteLine(text);
|
||||
}
|
||||
|
||||
Console.WriteLine(new string('=', 50));
|
||||
}
|
||||
}
|
||||
@@ -1,70 +1,36 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using ComputeSharp;
|
||||
using ComputeSharp.Sample;
|
||||
|
||||
namespace ComputeSharp.Sample;
|
||||
float[] array = Enumerable.Range(1, 100).Select(static i => (float)i).ToArray();
|
||||
|
||||
partial class Program
|
||||
// Create the graphics buffer
|
||||
using ReadWriteBuffer<float> gpuBuffer = GraphicsDevice.Default.AllocateReadWriteBuffer(array);
|
||||
|
||||
// Run the shader
|
||||
GraphicsDevice.Default.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
|
||||
{
|
||||
static void Main()
|
||||
public readonly ReadWriteBuffer<float> buffer;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Execute()
|
||||
{
|
||||
float[] array = Enumerable.Range(1, 100).Select(static i => (float)i).ToArray();
|
||||
|
||||
// Create the graphics buffer
|
||||
using ReadWriteBuffer<float> gpuBuffer = GraphicsDevice.Default.AllocateReadWriteBuffer(array);
|
||||
|
||||
// Run the shader
|
||||
GraphicsDevice.Default.For(100, new MainKernel(gpuBuffer));
|
||||
|
||||
// Print the initial matrix
|
||||
PrintMatrix(array, 10, 10, "BEFORE");
|
||||
|
||||
// Get the data back
|
||||
gpuBuffer.CopyTo(array);
|
||||
|
||||
// Print the updated matrix
|
||||
PrintMatrix(array, 10, 10, "AFTER");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Kernel for <see cref="Main"/>.
|
||||
/// </summary>
|
||||
[AutoConstructor]
|
||||
internal readonly partial struct MainKernel : IComputeShader
|
||||
{
|
||||
public readonly ReadWriteBuffer<float> buffer;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Execute()
|
||||
{
|
||||
buffer[ThreadIds.X] *= 2;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Prints a matrix in a properly formatted way.
|
||||
/// </summary>
|
||||
/// <param name="array">The input <see langword="float"/> array representing the matrix to print.</param>
|
||||
/// <param name="width">The width of the array to print.</param>
|
||||
/// <param name="height">The height of the array to print.</param>
|
||||
/// <param name="name">The name of the matrix to print.</param>
|
||||
private static void PrintMatrix(float[] array, int width, int height, string name)
|
||||
{
|
||||
int pad = 48 - name.Length;
|
||||
string title = $"{new string('=', pad / 2)} {name} {new string('=', (pad + 1) / 2)}";
|
||||
|
||||
Console.WriteLine(title);
|
||||
|
||||
int numberWidth = Math.Max(array.Max().ToString(CultureInfo.InvariantCulture).Length, 4);
|
||||
|
||||
for (int i = 0; i < height; i++)
|
||||
{
|
||||
var row = array.AsSpan(i * width, width).ToArray();
|
||||
var text = string.Join(",", row.Select(x => x.ToString(CultureInfo.InvariantCulture).PadLeft(numberWidth)));
|
||||
|
||||
Console.WriteLine(text);
|
||||
}
|
||||
|
||||
Console.WriteLine(new string('=', 50));
|
||||
buffer[ThreadIds.X] *= 2;
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする