Merge pull request #365 from Sergio0694/dev/fix-bool-marshalling
Fix bool types marshalling and rewriter handling
このコミットが含まれているのは:
17ファイル変更
+785
-571
検索条件に一致するファイルはありません
@@ -6,14 +6,14 @@ namespace ComputeSharp;
|
||||
/// <summary>
|
||||
/// A <see langword="struct"/> that can be used in place of the <see cref="bool"/> type in HLSL shaders.
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Explicit, Size = sizeof(int), Pack = 4)]
|
||||
[StructLayout(LayoutKind.Explicit, Size = 4, Pack = 4)]
|
||||
public readonly struct Bool
|
||||
{
|
||||
/// <summary>
|
||||
/// The wrapped <see cref="bool"/> value for the current instance.
|
||||
/// The wrapped <see cref="int"/> value for the current instance.
|
||||
/// </summary>
|
||||
[FieldOffset(0)]
|
||||
private readonly bool Value;
|
||||
private readonly int Value;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="Bool"/> instance for a given <see cref="bool"/> value.
|
||||
@@ -21,7 +21,7 @@ public readonly struct Bool
|
||||
/// <param name="value">.</param>
|
||||
private Bool(bool value)
|
||||
{
|
||||
Value = value;
|
||||
Value = value ? 1 : 0;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
@@ -39,20 +39,20 @@ public readonly struct Bool
|
||||
/// <inheritdoc/>
|
||||
public override string ToString()
|
||||
{
|
||||
return Value.ToString();
|
||||
return ((bool)this).ToString();
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="bool.ToString(IFormatProvider?)"/>
|
||||
public string ToString(IFormatProvider? formatProvider)
|
||||
{
|
||||
return Value.ToString();
|
||||
return ((bool)this).ToString(formatProvider);
|
||||
}
|
||||
|
||||
#if NET6_0_OR_GREATER
|
||||
/// <inheritdoc cref="bool.TryFormat(Span{char}, out int)"/>
|
||||
public bool TryFormat(Span<char> destination, out int charsWritten)
|
||||
{
|
||||
return Value.TryFormat(destination, out charsWritten);
|
||||
return ((bool)this).TryFormat(destination, out charsWritten);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -60,7 +60,7 @@ public readonly struct Bool
|
||||
/// 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(!x.Value);
|
||||
public static Bool operator !(Bool x) => new(!(bool)x);
|
||||
|
||||
/// <summary>
|
||||
/// Ands two <see cref="Bool"/> values.
|
||||
@@ -107,7 +107,7 @@ public readonly struct Bool
|
||||
/// 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;
|
||||
public static implicit operator bool(Bool x) => x.Value != 0;
|
||||
|
||||
/// <summary>
|
||||
/// Converts a <see cref="bool"/> value to a corresponding <see cref="Bool"/> instance.
|
||||
|
||||
@@ -14,8 +14,8 @@ public partial struct Bool2
|
||||
/// <param name="y">The value to assign to the second vector component.</param>
|
||||
public Bool2(bool x, bool y)
|
||||
{
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.x = x ? 1 : 0;
|
||||
this.y = y ? 1 : 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -20,10 +20,10 @@ public unsafe partial struct Bool2
|
||||
private static readonly void* UndefinedData = (void*)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(Bool2), sizeof(Bool4));
|
||||
|
||||
[FieldOffset(0)]
|
||||
private bool x;
|
||||
private int x;
|
||||
|
||||
[FieldOffset(4)]
|
||||
private bool y;
|
||||
private int y;
|
||||
|
||||
/// <summary>
|
||||
/// Gets a reference to a specific component in the current <see cref="Bool2"/> instance.
|
||||
@@ -55,12 +55,12 @@ public unsafe partial struct Bool2
|
||||
/// <summary>
|
||||
/// Gets a reference to the <see cref="bool"/> value representing the <c>X</c> component.
|
||||
/// </summary>
|
||||
public readonly ref bool X => ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Unsafe.AsRef(in this.x), 1));
|
||||
public readonly ref bool X => ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Unsafe.As<int, bool>(ref Unsafe.AsRef(in this.x)), 1));
|
||||
|
||||
/// <summary>
|
||||
/// Gets a reference to the <see cref="bool"/> value representing the <c>Y</c> component.
|
||||
/// </summary>
|
||||
public readonly ref bool Y => ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Unsafe.AsRef(in this.y), 1));
|
||||
public readonly ref bool Y => ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Unsafe.As<int, bool>(ref Unsafe.AsRef(in this.y)), 1));
|
||||
|
||||
/// <summary>
|
||||
/// Gets a readonly reference to the <see cref="Bool2"/> value with the components <see cref="X"/>, <see cref="X"/>.
|
||||
@@ -233,12 +233,12 @@ public unsafe partial struct Bool2
|
||||
/// <summary>
|
||||
/// Gets a reference to the <see cref="bool"/> value representing the <c>R</c> component.
|
||||
/// </summary>
|
||||
public readonly ref bool R => ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Unsafe.AsRef(in this.x), 1));
|
||||
public readonly ref bool R => ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Unsafe.As<int, bool>(ref Unsafe.AsRef(in this.x)), 1));
|
||||
|
||||
/// <summary>
|
||||
/// Gets a reference to the <see cref="bool"/> value representing the <c>G</c> component.
|
||||
/// </summary>
|
||||
public readonly ref bool G => ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Unsafe.AsRef(in this.y), 1));
|
||||
public readonly ref bool G => ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Unsafe.As<int, bool>(ref Unsafe.AsRef(in this.y)), 1));
|
||||
|
||||
/// <summary>
|
||||
/// Gets a readonly reference to the <see cref="Bool2"/> value with the components <see cref="R"/>, <see cref="R"/>.
|
||||
@@ -414,9 +414,9 @@ public unsafe partial struct Bool2
|
||||
public override readonly string ToString()
|
||||
{
|
||||
#if NET6_0_OR_GREATER
|
||||
return string.Create(null, stackalloc char[32], $"<{this.x}, {this.y}>");
|
||||
return string.Create(null, stackalloc char[32], $"<{this.x != 0}, {this.y != 0}>");
|
||||
#else
|
||||
return $"<{this.x}, {this.y}>";
|
||||
return $"<{this.x != 0}, {this.y != 0}>";
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
@@ -15,9 +15,9 @@ public partial struct Bool3
|
||||
/// <param name="z">The value to assign to the third vector component.</param>
|
||||
public Bool3(bool x, bool y, bool z)
|
||||
{
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
this.x = x ? 1 : 0;
|
||||
this.y = y ? 1 : 0;
|
||||
this.z = z ? 1 : 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -27,9 +27,9 @@ public partial struct Bool3
|
||||
/// <param name="z">The value to assign to the third vector component.</param>
|
||||
public Bool3(Bool2 xy, bool z)
|
||||
{
|
||||
this.x = xy.X;
|
||||
this.y = xy.Y;
|
||||
this.z = z;
|
||||
this.x = xy.X ? 1 : 0;
|
||||
this.y = xy.Y ? 1 : 0;
|
||||
this.z = z ? 1 : 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -39,9 +39,9 @@ public partial struct Bool3
|
||||
/// <param name="yz">The value to assign to the second and thirt vector components.</param>
|
||||
public Bool3(bool x, Bool2 yz)
|
||||
{
|
||||
this.x = x;
|
||||
this.y = yz.X;
|
||||
this.z = yz.Y;
|
||||
this.x = x ? 1 : 0;
|
||||
this.y = yz.X ? 1 : 0;
|
||||
this.z = yz.Y ? 1 : 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -20,13 +20,13 @@ public unsafe partial struct Bool3
|
||||
private static readonly void* UndefinedData = (void*)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(Bool3), sizeof(Bool4));
|
||||
|
||||
[FieldOffset(0)]
|
||||
private bool x;
|
||||
private int x;
|
||||
|
||||
[FieldOffset(4)]
|
||||
private bool y;
|
||||
private int y;
|
||||
|
||||
[FieldOffset(8)]
|
||||
private bool z;
|
||||
private int z;
|
||||
|
||||
/// <summary>
|
||||
/// Gets a reference to a specific component in the current <see cref="Bool3"/> instance.
|
||||
@@ -63,17 +63,17 @@ public unsafe partial struct Bool3
|
||||
/// <summary>
|
||||
/// Gets a reference to the <see cref="bool"/> value representing the <c>X</c> component.
|
||||
/// </summary>
|
||||
public readonly ref bool X => ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Unsafe.AsRef(in this.x), 1));
|
||||
public readonly ref bool X => ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Unsafe.As<int, bool>(ref Unsafe.AsRef(in this.x)), 1));
|
||||
|
||||
/// <summary>
|
||||
/// Gets a reference to the <see cref="bool"/> value representing the <c>Y</c> component.
|
||||
/// </summary>
|
||||
public readonly ref bool Y => ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Unsafe.AsRef(in this.y), 1));
|
||||
public readonly ref bool Y => ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Unsafe.As<int, bool>(ref Unsafe.AsRef(in this.y)), 1));
|
||||
|
||||
/// <summary>
|
||||
/// Gets a reference to the <see cref="bool"/> value representing the <c>Z</c> component.
|
||||
/// </summary>
|
||||
public readonly ref bool Z => ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Unsafe.AsRef(in this.z), 1));
|
||||
public readonly ref bool Z => ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Unsafe.As<int, bool>(ref Unsafe.AsRef(in this.z)), 1));
|
||||
|
||||
/// <summary>
|
||||
/// Gets a readonly reference to the <see cref="Bool2"/> value with the components <see cref="X"/>, <see cref="X"/>.
|
||||
@@ -780,17 +780,17 @@ public unsafe partial struct Bool3
|
||||
/// <summary>
|
||||
/// Gets a reference to the <see cref="bool"/> value representing the <c>R</c> component.
|
||||
/// </summary>
|
||||
public readonly ref bool R => ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Unsafe.AsRef(in this.x), 1));
|
||||
public readonly ref bool R => ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Unsafe.As<int, bool>(ref Unsafe.AsRef(in this.x)), 1));
|
||||
|
||||
/// <summary>
|
||||
/// Gets a reference to the <see cref="bool"/> value representing the <c>G</c> component.
|
||||
/// </summary>
|
||||
public readonly ref bool G => ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Unsafe.AsRef(in this.y), 1));
|
||||
public readonly ref bool G => ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Unsafe.As<int, bool>(ref Unsafe.AsRef(in this.y)), 1));
|
||||
|
||||
/// <summary>
|
||||
/// Gets a reference to the <see cref="bool"/> value representing the <c>B</c> component.
|
||||
/// </summary>
|
||||
public readonly ref bool B => ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Unsafe.AsRef(in this.z), 1));
|
||||
public readonly ref bool B => ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Unsafe.As<int, bool>(ref Unsafe.AsRef(in this.z)), 1));
|
||||
|
||||
/// <summary>
|
||||
/// Gets a readonly reference to the <see cref="Bool2"/> value with the components <see cref="R"/>, <see cref="R"/>.
|
||||
@@ -1500,9 +1500,9 @@ public unsafe partial struct Bool3
|
||||
public override readonly string ToString()
|
||||
{
|
||||
#if NET6_0_OR_GREATER
|
||||
return string.Create(null, stackalloc char[32], $"<{this.x}, {this.y}, {this.z}>");
|
||||
return string.Create(null, stackalloc char[32], $"<{this.x != 0}, {this.y != 0}, {this.z != 0}>");
|
||||
#else
|
||||
return $"<{this.x}, {this.y}, {this.z}>";
|
||||
return $"<{this.x != 0}, {this.y != 0}, {this.z != 0}>";
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
@@ -16,10 +16,10 @@ public partial struct Bool4
|
||||
/// <param name="w">The value to assign to the fourth vector component.</param>
|
||||
public Bool4(bool x, bool y, bool z, bool w)
|
||||
{
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
this.w = w;
|
||||
this.x = x ? 1 : 0;
|
||||
this.y = y ? 1 : 0;
|
||||
this.z = z ? 1 : 0;
|
||||
this.w = w ? 1 : 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -30,10 +30,10 @@ public partial struct Bool4
|
||||
/// <param name="w">The value to assign to the fourth vector component.</param>
|
||||
public Bool4(Bool2 xy, bool z, bool w)
|
||||
{
|
||||
this.x = xy.X;
|
||||
this.y = xy.Y;
|
||||
this.z = z;
|
||||
this.w = w;
|
||||
this.x = xy.X ? 1 : 0;
|
||||
this.y = xy.Y ? 1 : 0;
|
||||
this.z = z ? 1 : 0;
|
||||
this.w = w ? 1 : 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -44,10 +44,10 @@ public partial struct Bool4
|
||||
/// <param name="zw">The value to assign to the third and fourth vector components.</param>
|
||||
public Bool4(bool x, bool y, Bool2 zw)
|
||||
{
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = zw.X;
|
||||
this.w = zw.Y;
|
||||
this.x = x ? 1 : 0;
|
||||
this.y = y ? 1 : 0;
|
||||
this.z = zw.X ? 1 : 0;
|
||||
this.w = zw.Y ? 1 : 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -57,10 +57,10 @@ public partial struct Bool4
|
||||
/// <param name="zw">The value to assign to the third and fourth vector components.</param>
|
||||
public Bool4(Bool2 xy, Bool2 zw)
|
||||
{
|
||||
this.x = xy.X;
|
||||
this.y = xy.Y;
|
||||
this.z = zw.X;
|
||||
this.w = zw.Y;
|
||||
this.x = xy.X ? 1 : 0;
|
||||
this.y = xy.Y ? 1 : 0;
|
||||
this.z = zw.X ? 1 : 0;
|
||||
this.w = zw.Y ? 1 : 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -70,10 +70,10 @@ public partial struct Bool4
|
||||
/// <param name="w">The value to assign to the fourth vector component.</param>
|
||||
public Bool4(Bool3 xyz, bool w)
|
||||
{
|
||||
this.x = xyz.X;
|
||||
this.y = xyz.Y;
|
||||
this.z = xyz.Z;
|
||||
this.w = w;
|
||||
this.x = xyz.X ? 1 : 0;
|
||||
this.y = xyz.Y ? 1 : 0;
|
||||
this.z = xyz.Z ? 1 : 0;
|
||||
this.w = w ? 1 : 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -83,10 +83,10 @@ public partial struct Bool4
|
||||
/// <param name="yzw">The value to assign to the second, third and fourth vector components.</param>
|
||||
public Bool4(bool x, Bool3 yzw)
|
||||
{
|
||||
this.x = x;
|
||||
this.y = yzw.X;
|
||||
this.z = yzw.Y;
|
||||
this.w = yzw.Z;
|
||||
this.x = x ? 1 : 0;
|
||||
this.y = yzw.X ? 1 : 0;
|
||||
this.z = yzw.Y ? 1 : 0;
|
||||
this.w = yzw.Z ? 1 : 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -20,16 +20,16 @@ public unsafe partial struct Bool4
|
||||
private static readonly void* UndefinedData = (void*)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(Bool4), sizeof(Bool4));
|
||||
|
||||
[FieldOffset(0)]
|
||||
private bool x;
|
||||
private int x;
|
||||
|
||||
[FieldOffset(4)]
|
||||
private bool y;
|
||||
private int y;
|
||||
|
||||
[FieldOffset(8)]
|
||||
private bool z;
|
||||
private int z;
|
||||
|
||||
[FieldOffset(12)]
|
||||
private bool w;
|
||||
private int w;
|
||||
|
||||
/// <summary>
|
||||
/// Gets a reference to a specific component in the current <see cref="Bool4"/> instance.
|
||||
@@ -71,22 +71,22 @@ public unsafe partial struct Bool4
|
||||
/// <summary>
|
||||
/// Gets a reference to the <see cref="bool"/> value representing the <c>X</c> component.
|
||||
/// </summary>
|
||||
public readonly ref bool X => ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Unsafe.AsRef(in this.x), 1));
|
||||
public readonly ref bool X => ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Unsafe.As<int, bool>(ref Unsafe.AsRef(in this.x)), 1));
|
||||
|
||||
/// <summary>
|
||||
/// Gets a reference to the <see cref="bool"/> value representing the <c>Y</c> component.
|
||||
/// </summary>
|
||||
public readonly ref bool Y => ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Unsafe.AsRef(in this.y), 1));
|
||||
public readonly ref bool Y => ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Unsafe.As<int, bool>(ref Unsafe.AsRef(in this.y)), 1));
|
||||
|
||||
/// <summary>
|
||||
/// Gets a reference to the <see cref="bool"/> value representing the <c>Z</c> component.
|
||||
/// </summary>
|
||||
public readonly ref bool Z => ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Unsafe.AsRef(in this.z), 1));
|
||||
public readonly ref bool Z => ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Unsafe.As<int, bool>(ref Unsafe.AsRef(in this.z)), 1));
|
||||
|
||||
/// <summary>
|
||||
/// Gets a reference to the <see cref="bool"/> value representing the <c>W</c> component.
|
||||
/// </summary>
|
||||
public readonly ref bool W => ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Unsafe.AsRef(in this.w), 1));
|
||||
public readonly ref bool W => ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Unsafe.As<int, bool>(ref Unsafe.AsRef(in this.w)), 1));
|
||||
|
||||
/// <summary>
|
||||
/// Gets a readonly reference to the <see cref="Bool2"/> value with the components <see cref="X"/>, <see cref="X"/>.
|
||||
@@ -2107,22 +2107,22 @@ public unsafe partial struct Bool4
|
||||
/// <summary>
|
||||
/// Gets a reference to the <see cref="bool"/> value representing the <c>R</c> component.
|
||||
/// </summary>
|
||||
public readonly ref bool R => ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Unsafe.AsRef(in this.x), 1));
|
||||
public readonly ref bool R => ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Unsafe.As<int, bool>(ref Unsafe.AsRef(in this.x)), 1));
|
||||
|
||||
/// <summary>
|
||||
/// Gets a reference to the <see cref="bool"/> value representing the <c>G</c> component.
|
||||
/// </summary>
|
||||
public readonly ref bool G => ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Unsafe.AsRef(in this.y), 1));
|
||||
public readonly ref bool G => ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Unsafe.As<int, bool>(ref Unsafe.AsRef(in this.y)), 1));
|
||||
|
||||
/// <summary>
|
||||
/// Gets a reference to the <see cref="bool"/> value representing the <c>B</c> component.
|
||||
/// </summary>
|
||||
public readonly ref bool B => ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Unsafe.AsRef(in this.z), 1));
|
||||
public readonly ref bool B => ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Unsafe.As<int, bool>(ref Unsafe.AsRef(in this.z)), 1));
|
||||
|
||||
/// <summary>
|
||||
/// Gets a reference to the <see cref="bool"/> value representing the <c>A</c> component.
|
||||
/// </summary>
|
||||
public readonly ref bool A => ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Unsafe.AsRef(in this.w), 1));
|
||||
public readonly ref bool A => ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Unsafe.As<int, bool>(ref Unsafe.AsRef(in this.w)), 1));
|
||||
|
||||
/// <summary>
|
||||
/// Gets a readonly reference to the <see cref="Bool2"/> value with the components <see cref="R"/>, <see cref="R"/>.
|
||||
@@ -4146,9 +4146,9 @@ public unsafe partial struct Bool4
|
||||
public override readonly string ToString()
|
||||
{
|
||||
#if NET6_0_OR_GREATER
|
||||
return string.Create(null, stackalloc char[32], $"<{this.x}, {this.y}, {this.z}, {this.w}>");
|
||||
return string.Create(null, stackalloc char[32], $"<{this.x != 0}, {this.y != 0}, {this.z != 0}, {this.w != 0}>");
|
||||
#else
|
||||
return $"<{this.x}, {this.y}, {this.z}, {this.w}>";
|
||||
return $"<{this.x != 0}, {this.y != 0}, {this.z != 0}, {this.w != 0}>";
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
ファイル差分が大きすぎるため表示されません。
差分を読み込み
@@ -35,6 +35,8 @@ namespace ComputeSharp;
|
||||
void GenerateMatrixProperties(string typeName, int rows, int columns, int elementSize)
|
||||
{
|
||||
string elementTypeName = typeName.ToLower();
|
||||
bool isBoolType = elementTypeName == "bool";
|
||||
string fieldElementTypeName = isBoolType ? "int" : elementTypeName;
|
||||
string fullTypeName = $"{typeName}{rows}x{columns}";
|
||||
string rowTypeName = columns > 1 ? $"{typeName}{columns}" : elementTypeName;
|
||||
List<string> fieldNames = new List<string>();
|
||||
@@ -56,7 +58,7 @@ public unsafe partial struct <#=fullTypeName#>
|
||||
for (int j = 1; j <= columns; j++)
|
||||
{
|
||||
WriteLine($"[FieldOffset({((i - 1) * columns + j - 1) * elementSize})]");
|
||||
WriteLine($"private {elementTypeName} m{i}{j};");
|
||||
WriteLine($"private {fieldElementTypeName} m{i}{j};");
|
||||
WriteLine("");
|
||||
|
||||
fieldNames.Add($"m{i}{j}");
|
||||
@@ -79,7 +81,15 @@ public unsafe partial struct <#=fullTypeName#>
|
||||
|
||||
foreach (string fieldName in fieldNames)
|
||||
{
|
||||
WriteLine($"this.{fieldName} = {fieldName};");
|
||||
if (isBoolType)
|
||||
{
|
||||
// For bools, normalize from bool to int
|
||||
WriteLine($"this.{fieldName} = {fieldName} ? 1 : 0;");
|
||||
}
|
||||
else
|
||||
{
|
||||
WriteLine($"this.{fieldName} = {fieldName};");
|
||||
}
|
||||
}
|
||||
|
||||
PopIndent();
|
||||
@@ -113,7 +123,15 @@ public unsafe partial struct <#=fullTypeName#>
|
||||
for (int i = 1; i <= rows; i++)
|
||||
for (int j = 1; j <= columns; j++)
|
||||
{
|
||||
WriteLine($"this.m{i}{j} = row{i}.{"XYZW"[j - 1]};");
|
||||
if (isBoolType)
|
||||
{
|
||||
// Also normalize bools here as well
|
||||
WriteLine($"this.m{i}{j} = row{i}.{"XYZW"[j - 1]} ? 1 : 0;");
|
||||
}
|
||||
else
|
||||
{
|
||||
WriteLine($"this.m{i}{j} = row{i}.{"XYZW"[j - 1]};");
|
||||
}
|
||||
}
|
||||
|
||||
PopIndent();
|
||||
@@ -165,7 +183,20 @@ public unsafe partial struct <#=fullTypeName#>
|
||||
/// <summary>
|
||||
/// Gets a reference to the <see cref="<#=elementTypeName#>"/> value representing the component at position [<#=i#>, <#=j#>].
|
||||
/// </summary>
|
||||
<#
|
||||
if (isBoolType)
|
||||
{
|
||||
#>
|
||||
public readonly ref <#=elementTypeName#> M<#=i#><#=j#> => ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Unsafe.As<int, bool>(ref Unsafe.AsRef(in this.m<#=i#><#=j#>)), 1));
|
||||
<#
|
||||
}
|
||||
else
|
||||
{
|
||||
#>
|
||||
public readonly ref <#=elementTypeName#> M<#=i#><#=j#> => ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Unsafe.AsRef(in this.m<#=i#><#=j#>), 1));
|
||||
<#
|
||||
}
|
||||
#>
|
||||
<#
|
||||
}
|
||||
|
||||
@@ -187,7 +218,14 @@ public unsafe partial struct <#=fullTypeName#>
|
||||
for (int i = 1; i <= rows; i++)
|
||||
for (int j = 1; j <= columns; j++)
|
||||
{
|
||||
WriteLine($"matrix.m{i}{j} = x;");
|
||||
if (isBoolType)
|
||||
{
|
||||
WriteLine($"matrix.m{i}{j} = x ? 1 : 0;");
|
||||
}
|
||||
else
|
||||
{
|
||||
WriteLine($"matrix.m{i}{j} = x;");
|
||||
}
|
||||
}
|
||||
|
||||
WriteLine("");
|
||||
|
||||
@@ -49,6 +49,8 @@ void GenerateVectorProperties(string typeName, int elementSize)
|
||||
{
|
||||
string elementTypeName = Regex.Match(typeName, @"^[A-Za-z]+").Value;
|
||||
string hlslElementTypeName = elementTypeName.ToLower();
|
||||
bool isBoolType = hlslElementTypeName == "bool";
|
||||
string fieldElementTypeName = isBoolType ? "int" : hlslElementTypeName;
|
||||
int i = int.Parse(Regex.Match(typeName, @"\d+$").Value);
|
||||
string formattable = elementTypeName == "Bool" ? "" : "ISpanFormattable";
|
||||
|
||||
@@ -96,7 +98,7 @@ public unsafe partial struct <#=typeName#>
|
||||
foreach (char name in "xyzw".Substring(0, i))
|
||||
{
|
||||
WriteLine($"[FieldOffset({"xyzw".IndexOf(name) * elementSize})]");
|
||||
WriteLine($"private {hlslElementTypeName} {name};");
|
||||
WriteLine($"private {fieldElementTypeName} {name};");
|
||||
WriteLine("");
|
||||
}
|
||||
|
||||
@@ -201,7 +203,16 @@ public unsafe partial struct <#=typeName#>
|
||||
|
||||
// Property
|
||||
Write($"public readonly {refType} {propertyType} {propertyName} ");
|
||||
WriteLine($"=> ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Unsafe.AsRef(in this.{fieldName}), 1));");
|
||||
|
||||
if (isBoolType)
|
||||
{
|
||||
// For bool types, also reinterpret cast from int to bool (the backing field is int to ensure all 4 bytes are initialized)
|
||||
WriteLine($"=> ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Unsafe.As<int, bool>(ref Unsafe.AsRef(in this.{fieldName})), 1));");
|
||||
}
|
||||
else
|
||||
{
|
||||
WriteLine($"=> ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Unsafe.AsRef(in this.{fieldName}), 1));");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -233,9 +244,9 @@ public unsafe partial struct <#=typeName#>
|
||||
public override readonly string ToString()
|
||||
{
|
||||
#if NET6_0_OR_GREATER
|
||||
return string.Create(null, stackalloc char[32], $"<<#=string.Join(", ", "xyzw".Take(i).Select(c => $"{{this.{c}}}"))#>>");
|
||||
return string.Create(null, stackalloc char[32], $"<<#=string.Join(", ", "xyzw".Take(i).Select(c => $"{{this.{c} != 0}}"))#>>");
|
||||
#else
|
||||
return $"<<#=string.Join(", ", "xyzw".Take(i).Select(c => $"{{this.{c}}}"))#>>";
|
||||
return $"<<#=string.Join(", ", "xyzw".Take(i).Select(c => $"{{this.{c} != 0}}"))#>>";
|
||||
#endif
|
||||
}
|
||||
<#
|
||||
|
||||
@@ -578,11 +578,11 @@ partial class DiagnosticDescriptors
|
||||
public static readonly DiagnosticDescriptor InvalidDiscoveredType = new DiagnosticDescriptor(
|
||||
id: "CMPSD2D0041",
|
||||
title: "Invalid discovered type",
|
||||
messageFormat: "The D2D1 shader of type {0} uses the invalid type {1} (only some .NET primitives and vector types, HLSL primitive, vector and matrix types, and custom types containing these types can be used)",
|
||||
messageFormat: "The D2D1 shader of type {0} uses the invalid type {1} (only some .NET primitives and vector types, HLSL primitive, vector and matrix types, and custom types containing these types can be used, and custom types containing these types can be used, and bool fields in custom struct types have to be replaced with the ComputeSharp.Bool type for alignment reasons)",
|
||||
category: "ComputeSharp.D2D1.Shaders",
|
||||
defaultSeverity: DiagnosticSeverity.Error,
|
||||
isEnabledByDefault: true,
|
||||
description: "D2D1 shaders can only use supported types (some .NET primitives and vector types, HLSL primitive, vector and matrix types, and custom types containing these types can be used).",
|
||||
description: "D2D1 shaders can only use supported types (some .NET primitives and vector types, HLSL primitive, vector and matrix types, and custom types containing these types can be used, and custom types containing these types can be used, and bool fields in custom struct types have to be replaced with the ComputeSharp.Bool type for alignment reasons).",
|
||||
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
|
||||
|
||||
/// <summary>
|
||||
|
||||
+12
@@ -70,6 +70,18 @@ partial class ID2D1ShaderGenerator
|
||||
{
|
||||
switch (fieldInfo)
|
||||
{
|
||||
case FieldInfo.Primitive { TypeName: "System.Boolean" } primitive:
|
||||
|
||||
// Read a boolean value and cast it to Bool first, which will apply the correct size expansion. This will generate the following:
|
||||
//
|
||||
// global::System.Runtime.CompilerServices.Unsafe.As<byte, global::ComputeSharp.Bool>(
|
||||
// ref global::System.Runtime.CompilerServices.Unsafe.AddByteOffset(ref r0, (nint)<OFFSET>)) = (global::ComputeSharp.Bool)<FIELD_PATH>
|
||||
statements.Add(ExpressionStatement(
|
||||
AssignmentExpression(
|
||||
SyntaxKind.SimpleAssignmentExpression,
|
||||
ParseExpression($"global::System.Runtime.CompilerServices.Unsafe.As<byte, global::ComputeSharp.Bool>(ref global::System.Runtime.CompilerServices.Unsafe.AddByteOffset(ref r0, (nint){primitive.Offset}))"),
|
||||
ParseExpression($"(global::ComputeSharp.Bool){string.Join(".", primitive.FieldPath)}"))));
|
||||
break;
|
||||
case FieldInfo.Primitive primitive:
|
||||
|
||||
// Read a primitive value and serialize it into the target buffer. This will generate:
|
||||
|
||||
@@ -67,6 +67,7 @@ internal static partial class HlslKnownTypes
|
||||
Dictionary<string, string> knownTypes = new()
|
||||
{
|
||||
[typeof(bool).FullName] = "bool",
|
||||
[typeof(Bool).FullName] = "bool",
|
||||
[typeof(int).FullName] = "int",
|
||||
[typeof(uint).FullName] = "uint",
|
||||
[typeof(float).FullName] = "float",
|
||||
@@ -218,6 +219,14 @@ internal static partial class HlslKnownTypes
|
||||
// Local function to recursively gather nested types
|
||||
static void ExploreTypes(INamedTypeSymbol type, HashSet<INamedTypeSymbol> customTypes, HashSet<INamedTypeSymbol> invalidTypes)
|
||||
{
|
||||
// Explicitly prevent bool from being a field in a custom struct
|
||||
if (type.SpecialType == SpecialType.System_Boolean)
|
||||
{
|
||||
invalidTypes.Add(type);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (KnownHlslTypes.ContainsKey(type.GetFullMetadataName())) return;
|
||||
|
||||
// Check if the type is unsupported
|
||||
@@ -248,6 +257,15 @@ internal static partial class HlslKnownTypes
|
||||
// Explore all input types and their nested types
|
||||
foreach (INamedTypeSymbol type in discoveredTypes)
|
||||
{
|
||||
// Special case for bool types. These types are blocked if they appear as fields in custom struct types,
|
||||
// but are otherwise allowed. For instance, it is fine to use them in captured values for a shader (as
|
||||
// the dispatch data loader will perform the correct marshalling) as well as in locals/parameters. This
|
||||
// branch prevents crawling a processed type if it's just bool at the top level (ie. not a custom struct).
|
||||
if (type.SpecialType == SpecialType.System_Boolean)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
ExploreTypes(type, customTypes, invalidTypes2);
|
||||
}
|
||||
|
||||
|
||||
@@ -707,10 +707,10 @@ partial class DiagnosticDescriptors
|
||||
public static readonly DiagnosticDescriptor InvalidDiscoveredType = new DiagnosticDescriptor(
|
||||
id: "CMPS0050",
|
||||
title: "Invalid discovered type",
|
||||
messageFormat: "The compute shader or method {0} uses the invalid type {1} (only some .NET primitives and vector types, HLSL primitive, vector and matrix types, and custom types containing these types can be used)",
|
||||
messageFormat: "The compute shader or method {0} uses the invalid type {1} (only some .NET primitives and vector types, HLSL primitive, vector and matrix types, and custom types containing these types can be used, and bool fields in custom struct types have to be replaced with the ComputeSharp.Bool type for alignment reasons)",
|
||||
category: "ComputeSharp.Shaders",
|
||||
defaultSeverity: DiagnosticSeverity.Error,
|
||||
isEnabledByDefault: true,
|
||||
description: "Shaders and shader methods can only use supported types (some .NET primitives and vector types, HLSL primitive, vector and matrix types, and custom types containing these types can be used).",
|
||||
description: "Shaders and shader methods can only use supported types (some .NET primitives and vector types, HLSL primitive, vector and matrix types, and custom types containing these types can be used, and bool fields in custom struct types have to be replaced with the ComputeSharp.Bool type for alignment reasons).",
|
||||
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
|
||||
}
|
||||
+12
@@ -121,6 +121,18 @@ partial class IShaderGenerator
|
||||
ParseExpression($"global::System.Runtime.CompilerServices.Unsafe.Add(ref r1, {resource.Offset})"),
|
||||
ParseExpression($"global::ComputeSharp.__Internals.GraphicsResourceHelper.ValidateAndGetGpuDescriptorHandle({resource.FieldName}, device)"))));
|
||||
break;
|
||||
case FieldInfo.Primitive { TypeName: "System.Boolean" } primitive:
|
||||
|
||||
// Read a boolean value and cast it to Bool first, which will apply the correct size expansion. This will generate the following:
|
||||
//
|
||||
// global::System.Runtime.CompilerServices.Unsafe.As<uint, global::ComputeSharp.Bool>(
|
||||
// ref global::System.Runtime.CompilerServices.Unsafe.AddByteOffset(ref r0, (nint)<OFFSET>)) = (global::ComputeSharp.Bool)<FIELD_PATH>
|
||||
statements.Add(ExpressionStatement(
|
||||
AssignmentExpression(
|
||||
SyntaxKind.SimpleAssignmentExpression,
|
||||
ParseExpression($"global::System.Runtime.CompilerServices.Unsafe.As<uint, global::ComputeSharp.Bool>(ref global::System.Runtime.CompilerServices.Unsafe.AddByteOffset(ref r0, (nint){primitive.Offset}))"),
|
||||
ParseExpression($"(global::ComputeSharp.Bool){string.Join(".", primitive.FieldPath)}"))));
|
||||
break;
|
||||
case FieldInfo.Primitive primitive:
|
||||
|
||||
// Read a primitive value and serialize it into the target buffer. This will generate:
|
||||
|
||||
@@ -1697,6 +1697,38 @@ public class DiagnosticsTests
|
||||
VerifyGeneratedDiagnostics<IShaderGenerator>(source, "CMPS0047", "CMPS0050");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void InvalidDiscoveredType_CustomType_SystemBoolean()
|
||||
{
|
||||
string source = @"
|
||||
using System;
|
||||
using ComputeSharp;
|
||||
|
||||
namespace ComputeSharp
|
||||
{
|
||||
public class ReadWriteBuffer<T> { }
|
||||
}
|
||||
|
||||
namespace MyFancyApp.Sample
|
||||
{
|
||||
public struct Foo
|
||||
{
|
||||
public bool bar;
|
||||
}
|
||||
|
||||
public struct MyShader : IComputeShader
|
||||
{
|
||||
public readonly ReadWriteBuffer<Foo> buffer;
|
||||
|
||||
public void Execute()
|
||||
{
|
||||
}
|
||||
}
|
||||
}";
|
||||
|
||||
VerifyGeneratedDiagnostics<IShaderGenerator>(source, "CMPS0047", "CMPS0050");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies the output of a source generator.
|
||||
/// </summary>
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using ComputeSharp.Tests.Attributes;
|
||||
using System.Linq;
|
||||
using ComputeSharp.Tests.Attributes;
|
||||
using ComputeSharp.Tests.Extensions;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
|
||||
@@ -83,4 +84,94 @@ public partial class ShaderMembersTests
|
||||
buffer[ThreadIds.X] = total;
|
||||
}
|
||||
}
|
||||
|
||||
// See: https://github.com/Sergio0694/ComputeSharp/issues/193
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
public void BoolInstanceFields(Device device)
|
||||
{
|
||||
using ReadWriteBuffer<int> buffer = device.Get().AllocateReadWriteBuffer<int>(13, AllocationMode.Clear);
|
||||
|
||||
device.Get().For(1, new BoolInstanceFieldsShader(buffer, true, false, 42, false, true, new int3(1, 2, 3), true, new bool2(false, true), 123, true));
|
||||
|
||||
int[] results = buffer.ToArray();
|
||||
|
||||
CollectionAssert.AreEqual(
|
||||
expected: new[] { 1, 0, 42, 0, 1, 1, 2, 3, 1, 0, 1, 123, 1 },
|
||||
actual: results);
|
||||
}
|
||||
|
||||
[AutoConstructor]
|
||||
internal readonly partial struct BoolInstanceFieldsShader : IComputeShader
|
||||
{
|
||||
public readonly ReadWriteBuffer<int> buffer;
|
||||
|
||||
public readonly bool a;
|
||||
public readonly bool b;
|
||||
public readonly int c;
|
||||
public readonly bool d;
|
||||
public readonly bool e;
|
||||
public readonly int3 f;
|
||||
public readonly bool g;
|
||||
public readonly bool2 h;
|
||||
public readonly int i;
|
||||
public readonly bool j;
|
||||
|
||||
public void Execute()
|
||||
{
|
||||
buffer[0] = Hlsl.BoolToInt(a);
|
||||
buffer[1] = Hlsl.BoolToInt(b);
|
||||
buffer[2] = c;
|
||||
buffer[3] = Hlsl.BoolToInt(d);
|
||||
buffer[4] = Hlsl.BoolToInt(e);
|
||||
buffer[5] = f.X;
|
||||
buffer[6] = f.Y;
|
||||
buffer[7] = f.Z;
|
||||
buffer[8] = Hlsl.BoolToInt(g);
|
||||
buffer[9] = Hlsl.BoolToInt(h.X);
|
||||
buffer[10] = Hlsl.BoolToInt(h.Y);
|
||||
buffer[11] = i;
|
||||
buffer[12] = Hlsl.BoolToInt(j);
|
||||
}
|
||||
}
|
||||
|
||||
// See: https://github.com/Sergio0694/ComputeSharp/issues/193
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
public void BoolInstanceFieldInCustomStruct(Device device)
|
||||
{
|
||||
Int2AndBoolField[] data = Enumerable.Range(1, 100).Select(i => new Int2AndBoolField { P = new Int2(i, i) }).ToArray();
|
||||
|
||||
using ReadWriteBuffer<Int2AndBoolField> buffer = device.Get().AllocateReadWriteBuffer(data);
|
||||
|
||||
device.Get().For(100, new BoolInstanceFieldInCustomStructShader(buffer));
|
||||
|
||||
Int2AndBoolField[] results = buffer.ToArray();
|
||||
|
||||
for (int i = 0; i < results.Length; i++)
|
||||
{
|
||||
ref Int2AndBoolField value = ref results[i];
|
||||
|
||||
Assert.AreEqual(i + 1, value.P.X);
|
||||
Assert.AreEqual(i + 1, value.P.Y);
|
||||
Assert.AreEqual(true, (bool)value.B);
|
||||
}
|
||||
}
|
||||
|
||||
public struct Int2AndBoolField
|
||||
{
|
||||
public Int2 P;
|
||||
public Bool B;
|
||||
}
|
||||
|
||||
[AutoConstructor]
|
||||
internal readonly partial struct BoolInstanceFieldInCustomStructShader : IComputeShader
|
||||
{
|
||||
public readonly ReadWriteBuffer<Int2AndBoolField> buffer;
|
||||
|
||||
public void Execute()
|
||||
{
|
||||
buffer[ThreadIds.X].B = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする