Updated README.md and added a new test

このコミットが含まれているのは:
Sergio Pedri committed 2019-09-05 23:14:32 +02:00
1 親 3f12540e03
コミット fd07b6a89f
2ファイル変更
+28 -1

検索条件に一致するファイルはありません

+6 -1
ファイルの表示
@@ -59,6 +59,8 @@ If the shader in C# is capturing some local variable, those will be automaticall
✅ `Func<T>`s, local methods or delegates with no captured variables
✅ `static` fields and properties with a supported delegate type
## Advanced usage
**ComputeSharp** lets you dispatch compute shaders over thread groups from 1 to 3 dimensions, includes supports for constant and readonly buffers, and more. The shader body can both be declared inline, as a separate `Action<ThreadIds>` or as a local method. Additionally, most of the [HLSL intrinsic functions](https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-intrinsic-functions) are available through the `Hlsl` class. Here is a more advanced sample showcasing all these features.
@@ -71,11 +73,14 @@ float[] y = new float[height * width]; // Result array (assume both had some val
using ReadOnlyBuffer<float> xBuffer = Gpu.Default.AllocateReadOnlyBuffer(x);
using ReadWriteBuffer<float> yBuffer = Gpu.Default.AllocateReadWriteBuffer(y);
static float Sigmoid(float x) => 1 / (1 + Hlsl.Exp(-x));
// Shader body
void Kernel(ThreadIds id)
{
int offset = id.X + id.Y * width;
yBuffer[offset] = Hlsl.Pow(xBuffer[offset], 2);
float pow = Hlsl.Pow(xBuffer[offset], 2);
yBuffer[offset] = Sigmoid(pow);
}
// Run the shader
+22
ファイルの表示
@@ -175,5 +175,27 @@ namespace ComputeSharp.Tests
Assert.IsTrue(MathF.Abs(result[0] - 9) < 0.0001f);
}
[TestMethod]
public void ReadmeSample()
{
float[] buffer = { 3 };
using ReadWriteBuffer<float> xBuffer = Gpu.Default.AllocateReadWriteBuffer(buffer);
static float Sigmoid(float x) => 1 / (1 + Hlsl.Exp(-x));
void Kernel(ThreadIds id)
{
int offset = id.X + id.Y * 1;
float pow = Hlsl.Pow(xBuffer[offset], 2); // 9
xBuffer[offset] = Sigmoid(pow); // 0.9998766
}
Gpu.Default.For(1, Kernel);
float[] result = xBuffer.GetData();
Assert.IsTrue(MathF.Abs(result[0] - 0.9998766f) < 0.0001f);
}
}
}