Code tweaks to the sample project
このコミットが含まれているのは:
1 親
f0acc27098
コミット
82a14c2e5c
2ファイル変更
+27
-19
検索条件に一致するファイルはありません
@@ -1,12 +1,14 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using ComputeSharp.Graphics.Buffers;
|
||||
|
||||
namespace ComputeSharp.Sample
|
||||
{
|
||||
public class Program
|
||||
class Program
|
||||
{
|
||||
private static void Main()
|
||||
static void Main()
|
||||
{
|
||||
// Create the graphics buffer
|
||||
int width = 10;
|
||||
@@ -18,42 +20,48 @@ namespace ComputeSharp.Sample
|
||||
}
|
||||
|
||||
// Print the initial matrix
|
||||
Console.WriteLine("Before:");
|
||||
Console.WriteLine("===================== BEFORE =====================");
|
||||
PrintMatrix(array, width, height);
|
||||
Console.WriteLine("==================================================");
|
||||
Console.WriteLine();
|
||||
|
||||
using ReadWriteBuffer<float> gpuBuffer = Gpu.Default.AllocateReadWriteBuffer(array);
|
||||
|
||||
// Shader body
|
||||
Action<ThreadIds> action = id =>
|
||||
// Run the shader
|
||||
Gpu.Default.For(100, id =>
|
||||
{
|
||||
uint offset = id.X + id.Y * (uint)width;
|
||||
gpuBuffer[offset] *= 2;
|
||||
};
|
||||
|
||||
// Run the shader
|
||||
Gpu.Default.For(100, action);
|
||||
});
|
||||
|
||||
// Get the data back
|
||||
gpuBuffer.GetData(array);
|
||||
|
||||
// Print the updated matrix
|
||||
Console.WriteLine("After:");
|
||||
Console.WriteLine("===================== AFTER ======================");
|
||||
PrintMatrix(array, width, height);
|
||||
Console.WriteLine("==================================================");
|
||||
}
|
||||
|
||||
/// <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>
|
||||
private static void PrintMatrix(float[] array, int width, int height)
|
||||
{
|
||||
int numberWidth = array.Max().ToString().Length;
|
||||
int length = width * height;
|
||||
int numberWidth = array.Max().ToString(CultureInfo.InvariantCulture).Length;
|
||||
ref float r = ref array[0];
|
||||
|
||||
for (int y = 0; y < height; y++)
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
for (int x = 0; x < width; x++)
|
||||
{
|
||||
Console.Write(array[x + y * width].ToString().PadLeft(numberWidth));
|
||||
Console.Write(", ");
|
||||
}
|
||||
Console.Write(r.ToString(CultureInfo.InvariantCulture).PadLeft(numberWidth));
|
||||
r = ref Unsafe.Add(ref r, 1);
|
||||
|
||||
Console.WriteLine();
|
||||
if (i < length - 1) Console.Write(", ");
|
||||
if (i > 0 && (i + 1) % width == 0) Console.WriteLine();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする