Merge pull request #59 from Sergio0694/feature_Bool-mapping

Bool mapping
このコミットが含まれているのは:
Sergio Pedri authored and GitHub committed 2019-09-20 13:24:42 +02:00
コミット 335bee52a9
18ファイル変更
+134 -21

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

+66
ファイルの表示
@@ -0,0 +1,66 @@
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace ComputeSharp
{
/// <summary>
/// A <see langword="struct"/> that can be used in place of the <see cref="bool"/> type in HLSL shaders
/// </summary>
[DebuggerDisplay("{Value}")]
[StructLayout(LayoutKind.Explicit, Size = sizeof(int))]
public readonly struct Bool
{
/// <summary>
/// The wrapped <see cref="bool"/> value for the current instance
/// </summary>
[FieldOffset(0)]
private readonly bool Value;
/// <summary>
/// Creates a new <see cref="Bool"/> instance for a given <see cref="bool"/> value
/// </summary>
/// <param name="value"></param>
private Bool(bool value) => Value = value;
/// <inheritdoc/>
public override bool Equals(object obj) => obj is Bool x && Value == x.Value;
/// <inheritdoc/>
public override int GetHashCode() => Value.GetHashCode();
/// <inheritdoc/>
public override string ToString() => Value.ToString();
/// <summary>
/// Checks whether or not two <see cref="Bool"/> instances represent the same <see cref="bool"/> value
/// </summary>
/// <param name="left">The left <see cref="Bool"/> instance</param>
/// <param name="right">The right <see cref="Bool"/> instance</param>
public static bool operator ==(Bool left, Bool right) => left.Value == right.Value;
/// <summary>
/// Checks whether or not two <see cref="Bool"/> instances represent a different <see cref="bool"/> value
/// </summary>
/// <param name="left">The left <see cref="Bool"/> instance</param>
/// <param name="right">The right <see cref="Bool"/> instance</param>
public static bool operator !=(Bool left, Bool right) => left.Value != right.Value;
/// <summary>
/// Inverts the <see cref="bool"/> value represented by a given <see cref="Bool"/> instance
/// </summary>
/// <param name="x">The input <see cref="Bool"/> instance</param>
public static Bool operator !(Bool x) => new Bool(!x.Value);
/// <summary>
/// Converts a given <see cref="Bool"/> instance to its corresponding <see cref="bool"/> value
/// </summary>
/// <param name="x">The input <see cref="Bool"/> instance</param>
public static implicit operator bool(Bool x) => x.Value;
/// <summary>
/// Converts a <see cref="bool"/> value to a corresponding <see cref="Bool"/> instance
/// </summary>
/// <param name="x">The input <see cref="bool"/> value</param>
public static implicit operator Bool(bool x) => new Bool(x);
}
}
+1 -1
ファイルの表示
@@ -5,7 +5,7 @@ using ComputeSharp.Exceptions;
namespace ComputeSharp
{
/// <summary>
/// A <see langword="struct"/> that maps the bool2 HLSL type
/// A <see langword="struct"/> that maps the <see langword="bool2"/> HLSL type
/// </summary>
[DebuggerDisplay("({X}, {Y})")]
[StructLayout(LayoutKind.Explicit, Size = sizeof(int) * 2)]
+1 -1
ファイルの表示
@@ -5,7 +5,7 @@ using ComputeSharp.Exceptions;
namespace ComputeSharp
{
/// <summary>
/// A <see langword="struct"/> that maps the bool3 HLSL type
/// A <see langword="struct"/> that maps the <see langword="bool3"/> HLSL type
/// </summary>
[DebuggerDisplay("({X}, {Y}, {Z})")]
[StructLayout(LayoutKind.Explicit, Size = sizeof(int) * 3)]
+1 -1
ファイルの表示
@@ -5,7 +5,7 @@ using ComputeSharp.Exceptions;
namespace ComputeSharp
{
/// <summary>
/// A <see langword="struct"/> that maps the bool4 HLSL type
/// A <see langword="struct"/> that maps the <see langword="bool4"/> HLSL type
/// </summary>
[DebuggerDisplay("({X}, {Y}, {Z}, {W})")]
[StructLayout(LayoutKind.Explicit, Size = sizeof(int) * 4)]
+1 -1
ファイルの表示
@@ -5,7 +5,7 @@ using ComputeSharp.Exceptions;
namespace ComputeSharp
{
/// <summary>
/// A <see langword="struct"/> that maps the double2 HLSL type
/// A <see langword="struct"/> that maps the <see langword="double2"/> HLSL type
/// </summary>
[DebuggerDisplay("({X}, {Y})")]
[StructLayout(LayoutKind.Sequential, Size = sizeof(double) * 2)]
+1 -1
ファイルの表示
@@ -5,7 +5,7 @@ using ComputeSharp.Exceptions;
namespace ComputeSharp
{
/// <summary>
/// A <see langword="struct"/> that maps the double3 HLSL type
/// A <see langword="struct"/> that maps the <see langword="double3"/> HLSL type
/// </summary>
[DebuggerDisplay("({X}, {Y}, {Z})")]
[StructLayout(LayoutKind.Sequential, Size = sizeof(double) * 3)]
+1 -1
ファイルの表示
@@ -5,7 +5,7 @@ using ComputeSharp.Exceptions;
namespace ComputeSharp
{
/// <summary>
/// A <see langword="struct"/> that maps the double4 HLSL type
/// A <see langword="struct"/> that maps the <see langword="double4"/> HLSL type
/// </summary>
[DebuggerDisplay("({X}, {Y}, {Z}, {W})")]
[StructLayout(LayoutKind.Sequential, Size = sizeof(double) * 4)]
+1 -1
ファイルの表示
@@ -5,7 +5,7 @@ using ComputeSharp.Exceptions;
namespace ComputeSharp
{
/// <summary>
/// A <see langword="struct"/> that maps the float2 HLSL type
/// A <see langword="struct"/> that maps the <see langword="float2"/> HLSL type
/// </summary>
[DebuggerDisplay("({X}, {Y})")]
[StructLayout(LayoutKind.Sequential, Size = sizeof(float) * 2)]
+1 -1
ファイルの表示
@@ -5,7 +5,7 @@ using ComputeSharp.Exceptions;
namespace ComputeSharp
{
/// <summary>
/// A <see langword="struct"/> that maps the float3 HLSL type
/// A <see langword="struct"/> that maps the <see langword="float3"/> HLSL type
/// </summary>
[DebuggerDisplay("({X}, {Y}, {Z})")]
[StructLayout(LayoutKind.Sequential, Size = sizeof(float) * 3)]
+1 -1
ファイルの表示
@@ -5,7 +5,7 @@ using ComputeSharp.Exceptions;
namespace ComputeSharp
{
/// <summary>
/// A <see langword="struct"/> that maps the float4 HLSL type
/// A <see langword="struct"/> that maps the <see langword="float4"/> HLSL type
/// </summary>
[DebuggerDisplay("({X}, {Y}, {Z}, {W})")]
[StructLayout(LayoutKind.Sequential, Size = sizeof(float) * 4)]
+1 -1
ファイルの表示
@@ -5,7 +5,7 @@ using ComputeSharp.Exceptions;
namespace ComputeSharp
{
/// <summary>
/// A <see langword="struct"/> that maps the int2 HLSL type
/// A <see langword="struct"/> that maps the <see langword="int2"/> HLSL type
/// </summary>
[DebuggerDisplay("({X}, {Y})")]
[StructLayout(LayoutKind.Sequential, Size = sizeof(int) * 2)]
+1 -1
ファイルの表示
@@ -5,7 +5,7 @@ using ComputeSharp.Exceptions;
namespace ComputeSharp
{
/// <summary>
/// A <see langword="struct"/> that maps the int3 HLSL type
/// A <see langword="struct"/> that maps the <see langword="int3"/> HLSL type
/// </summary>
[DebuggerDisplay("({X}, {Y}, {Z})")]
[StructLayout(LayoutKind.Sequential, Size = sizeof(int) * 3)]
+1 -1
ファイルの表示
@@ -5,7 +5,7 @@ using ComputeSharp.Exceptions;
namespace ComputeSharp
{
/// <summary>
/// A <see langword="struct"/> that maps the int4 HLSL type
/// A <see langword="struct"/> that maps the <see langword="int4"/> HLSL type
/// </summary>
[DebuggerDisplay("({X}, {Y}, {Z}, {W})")]
[StructLayout(LayoutKind.Sequential, Size = sizeof(int) * 4)]
+1 -1
ファイルの表示
@@ -5,7 +5,7 @@ using ComputeSharp.Exceptions;
namespace ComputeSharp
{
/// <summary>
/// A <see langword="struct"/> that maps the uint2 HLSL type
/// A <see langword="struct"/> that maps the <see langword="uint2"/> HLSL type
/// </summary>
[DebuggerDisplay("({X}, {Y})")]
[StructLayout(LayoutKind.Sequential, Size = sizeof(uint) * 2)]
+1 -1
ファイルの表示
@@ -5,7 +5,7 @@ using ComputeSharp.Exceptions;
namespace ComputeSharp
{
/// <summary>
/// A <see langword="struct"/> that maps the uint3 HLSL type
/// A <see langword="struct"/> that maps the <see langword="uint3"/> HLSL type
/// </summary>
[DebuggerDisplay("({X}, {Y}, {Z})")]
[StructLayout(LayoutKind.Sequential, Size = sizeof(uint) * 3)]
+1 -1
ファイルの表示
@@ -5,7 +5,7 @@ using ComputeSharp.Exceptions;
namespace ComputeSharp
{
/// <summary>
/// A <see langword="struct"/> that maps the uuint4 HLSL type
/// A <see langword="struct"/> that maps the <see langword="uint4"/> HLSL type
/// </summary>
[DebuggerDisplay("({X}, {Y}, {Z}, {W})")]
[StructLayout(LayoutKind.Sequential, Size = sizeof(uint) * 4)]
+2
ファイルの表示
@@ -17,6 +17,7 @@ namespace ComputeSharp.Shaders.Mappings
private static readonly IReadOnlyDictionary<string, string> KnownTypes = new Dictionary<string, string>
{
[typeof(bool).FullName] = "bool",
[typeof(Bool).FullName] = "bool",
[typeof(Bool2).FullName] = "bool2",
[typeof(Bool3).FullName] = "bool3",
[typeof(Bool4).FullName] = "bool4",
@@ -77,6 +78,7 @@ namespace ComputeSharp.Shaders.Mappings
/// <returns>A <see langword="bool"/> indicating whether the input <see cref="Type"/> is in fact a known HLSL scalar type</returns>
[Pure]
public static bool IsKnownScalarType(Type type) => type == typeof(bool) ||
type == typeof(Bool) ||
type == typeof(int) ||
type == typeof(uint) ||
type == typeof(float) ||
+51 -6
ファイルの表示
@@ -10,19 +10,64 @@ namespace ComputeSharp.Tests
[TestCategory("ShaderClosure")]
public class ShaderClosureTests
{
private static void LocalScalarAssignToBuffer<T>(T value, Func<T, T, bool> equals) where T : unmanaged
{
using ReadWriteBuffer<T> buffer = Gpu.Default.AllocateReadWriteBuffer<T>(4);
T zero = default;
Action<ThreadIds> action = id =>
{
if (id.X % 2 == 0) buffer[id.X] = value;
else buffer[id.X] = zero;
};
Gpu.Default.For(4, action);
T[] result = buffer.GetData();
Assert.IsTrue(equals(result[0], value));
Assert.IsFalse(equals(result[1], value));
Assert.IsTrue(equals(result[2], value));
Assert.IsFalse(equals(result[3], value));
}
[TestMethod]
public void LocalScalarAssignToBuffer()
{
float value = 10;
using ReadWriteBuffer<float> buffer = Gpu.Default.AllocateReadWriteBuffer<float>(1);
LocalScalarAssignToBuffer(10, (a, b) => a == b);
LocalScalarAssignToBuffer(3.14f, (a, b) => MathF.Abs(a - b) < 0.001f);
LocalScalarAssignToBuffer(6.28, (a, b) => Math.Abs(a - b) < 0.001f);
LocalScalarAssignToBuffer<Bool>(true, (a, b) => a == b);
}
Action<ThreadIds> action = id => buffer[0] = value;
[TestMethod]
public void ShaderWithBoolType()
{
using ReadWriteBuffer<Bool> bools = Gpu.Default.AllocateReadWriteBuffer(new Bool[] { true, false, true, true });
using ReadWriteBuffer<int> ints = Gpu.Default.AllocateReadWriteBuffer<int>(4);
Gpu.Default.For(1, action);
void Foo(ThreadIds id)
{
if (bools[id.X]) ints[id.X] = id.X + 1;
bools[id.X] = (id.X % 2) == 0;
}
float[] result = buffer.GetData();
Gpu.Default.For(4, Foo);
Assert.IsTrue(MathF.Abs(result[0] - value) < 0.0001f);
Bool[] boolResults = bools.GetData();
Assert.IsTrue(boolResults[0]);
Assert.IsFalse(boolResults[1]);
Assert.IsTrue(boolResults[2]);
Assert.IsFalse(boolResults[3]);
int[] intResults = ints.GetData();
Assert.IsTrue(intResults[0] == 1);
Assert.IsTrue(intResults[1] == 0);
Assert.IsTrue(intResults[2] == 3);
Assert.IsTrue(intResults[3] == 4);
}
[TestMethod]