Minor tweaks to the C# and F# local samples
このコミットが含まれているのは:
1 親
bf42e6d398
コミット
2a45fbcf05
3ファイル変更
+12
-22
検索条件に一致するファイルはありません
@@ -40,7 +40,7 @@ Let's suppose we want to run a simple compute shader that multiplies all items i
|
||||
|
||||
```csharp
|
||||
// Get some sample data
|
||||
int[] array = Enumerable.Range(0, 100).ToArray();
|
||||
int[] array = Enumerable.Range(1, 100).ToArray();
|
||||
|
||||
// Allocate a GPU buffer and copy the data to it.
|
||||
// We want the shader to modify the items in-place, so we
|
||||
@@ -301,7 +301,7 @@ There is a caveat though: since the HLSL shader rewriter specifically works on C
|
||||
Assuming we had the `MultiplyByTwo` shader defined in the [Quick start](#quick-start) paragraph referenced from a C# project, here is how we can actually use it directly from our F# project, with the same sample described above:
|
||||
|
||||
```fsharp
|
||||
let array = [| for i in 0 .. 99 -> (float32)i |]
|
||||
let array = [| for i in 1 .. 100 -> (float32)i |]
|
||||
|
||||
// Create the graphics buffer
|
||||
use buffer = Gpu.Default.AllocateReadWriteBuffer(array)
|
||||
|
||||
@@ -29,7 +29,7 @@ let printMatrix (array : float32[]) width height (name : string) =
|
||||
[<EntryPoint>]
|
||||
let main _ =
|
||||
|
||||
let array = [| for i in 0 .. 99 -> (float32)i |]
|
||||
let array = [| for i in 1 .. 100 -> (float32)i |]
|
||||
|
||||
// Create the graphics buffer
|
||||
use buffer = Gpu.Default.AllocateReadWriteBuffer(array)
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace ComputeSharp.Sample
|
||||
{
|
||||
@@ -9,28 +8,22 @@ namespace ComputeSharp.Sample
|
||||
{
|
||||
static void Main()
|
||||
{
|
||||
// Create the graphics buffer
|
||||
int width = 10;
|
||||
int height = 10;
|
||||
float[] array = new float[width * height];
|
||||
for (int i = 0; i < array.Length; i++)
|
||||
{
|
||||
array[i] = i + 1;
|
||||
}
|
||||
float[] array = Enumerable.Range(1, 100).Select(static i => (float)i).ToArray();
|
||||
|
||||
// Create the graphics buffer
|
||||
using ReadWriteBuffer<float> gpuBuffer = Gpu.Default.AllocateReadWriteBuffer(array);
|
||||
|
||||
// Run the shader
|
||||
Gpu.Default.For(100, new MainKernel(gpuBuffer));
|
||||
|
||||
// Print the initial matrix
|
||||
PrintMatrix(array, width, height, "BEFORE");
|
||||
PrintMatrix(array, 10, 10, "BEFORE");
|
||||
|
||||
// Get the data back
|
||||
gpuBuffer.CopyTo(array);
|
||||
|
||||
// Print the updated matrix
|
||||
PrintMatrix(array, width, height, "AFTER");
|
||||
PrintMatrix(array, 10, 10, "AFTER");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -62,17 +55,14 @@ namespace ComputeSharp.Sample
|
||||
|
||||
Console.WriteLine(title);
|
||||
|
||||
int length = width * height;
|
||||
int numberWidth = array.Max().ToString(CultureInfo.InvariantCulture).Length;
|
||||
ref float r = ref array[0];
|
||||
int numberWidth = Math.Max(array.Max().ToString(CultureInfo.InvariantCulture).Length, 4);
|
||||
|
||||
for (int i = 0; i < length; i++)
|
||||
for (int i = 0; i < height; i++)
|
||||
{
|
||||
Console.Write(r.ToString(CultureInfo.InvariantCulture).PadLeft(numberWidth));
|
||||
r = ref Unsafe.Add(ref r, 1);
|
||||
var row = array[(i * width)..((i + 1) * width)];
|
||||
var text = string.Join(",", row.Select(x => x.ToString(CultureInfo.InvariantCulture).PadLeft(numberWidth)));
|
||||
|
||||
if (i < length - 1) Console.Write(", ");
|
||||
if (i > 0 && (i + 1) % width == 0) Console.WriteLine();
|
||||
Console.WriteLine(text);
|
||||
}
|
||||
|
||||
Console.WriteLine(new string('=', 50));
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする