Generate every HLSL swizzle and mixed constructor; fix intrinsic types
tools/generate-swizzles.ps1 writes the checked-in src/CultMath/Swizzles.g.cs as partial structs plus a partial math: for float2/3/4, int2/3/4 and bool2/3/4 every swizzle of length 2-4 including repeats, in xyzw and rgba naming, with setters only on non-repeating masks, plus single-component r/g/b/a. double2/3 get lengths 2-3 (there is no double4). Every HLSL mixed constructor (float4(float2, float2), float4(float, float2, float), copy forms, ...) exists as a struct constructor and, for float/int/bool, a math function. The hand-written swizzles and mixed constructors it supersedes are deleted. Matrices store private rows and return ref float2/float3 from the row indexer ([UnscopedRef], polyfilled for netstandard2.1), so m[1][2] = s compiles as in HLSL; _mRC are get/set properties over the rows. Equality and hashing stay value-based. Intrinsics follow HLSL: sign returns int/intN and yields 0 for NaN; step is x >= y ? 1 : 0 so NaN yields 0; any/all accept float and int vectors; min/max/abs on int vectors return int vectors, and abs(int) returns int. Strips the UTF-8 BOMs this branch added to math.cs, CultMath.csproj and HlslSemanticsTests.cs. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
parent
2ad8dbc9b3
commit
5d8ef0c3de
23 changed files with 3490 additions and 276 deletions
|
|
@ -1,4 +1,4 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>net8.0;netstandard2.1</TargetFrameworks>
|
||||
|
|
|
|||
3137
packages/cultmath/src/CultMath/Swizzles.g.cs
Normal file
3137
packages/cultmath/src/CultMath/Swizzles.g.cs
Normal file
File diff suppressed because it is too large
Load diff
2
packages/cultmath/src/CultMath/Swizzles.g.cs.meta
Normal file
2
packages/cultmath/src/CultMath/Swizzles.g.cs.meta
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: e89d6089b354459788eb6f0faebfd6c0
|
||||
10
packages/cultmath/src/CultMath/UnscopedRefAttribute.cs
Normal file
10
packages/cultmath/src/CultMath/UnscopedRefAttribute.cs
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
#if !NET7_0_OR_GREATER
|
||||
namespace System.Diagnostics.CodeAnalysis;
|
||||
|
||||
// Compiler-recognized polyfill so netstandard2.1 builds can return refs into a struct's
|
||||
// own fields (the matrix row indexer). net7.0+ supplies the real attribute.
|
||||
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)]
|
||||
internal sealed class UnscopedRefAttribute : Attribute
|
||||
{
|
||||
}
|
||||
#endif
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 3f204000f9854517a8dd754e2d9dc975
|
||||
|
|
@ -2,7 +2,7 @@ using System;
|
|||
|
||||
namespace CultMath;
|
||||
|
||||
public struct bool2 : IEquatable<bool2>
|
||||
public partial struct bool2 : IEquatable<bool2>
|
||||
{
|
||||
public static readonly bool2 @false = new(false, false);
|
||||
public static readonly bool2 @true = new(true, true);
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ using System;
|
|||
|
||||
namespace CultMath;
|
||||
|
||||
public struct bool3 : IEquatable<bool3>
|
||||
public partial struct bool3 : IEquatable<bool3>
|
||||
{
|
||||
public static readonly bool3 @false = new(false, false, false);
|
||||
public static readonly bool3 @true = new(true, true, true);
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ using System;
|
|||
|
||||
namespace CultMath;
|
||||
|
||||
public struct bool4 : IEquatable<bool4>
|
||||
public partial struct bool4 : IEquatable<bool4>
|
||||
{
|
||||
public static readonly bool4 @false = new(false, false, false, false);
|
||||
public static readonly bool4 @true = new(true, true, true, true);
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ using System;
|
|||
|
||||
namespace CultMath;
|
||||
|
||||
public struct double2 : IEquatable<double2>
|
||||
public partial struct double2 : IEquatable<double2>
|
||||
{
|
||||
public static readonly double2 zero = new(0.0, 0.0);
|
||||
public static readonly double2 one = new(1.0, 1.0);
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ using System;
|
|||
|
||||
namespace CultMath;
|
||||
|
||||
public struct double3 : IEquatable<double3>
|
||||
public partial struct double3 : IEquatable<double3>
|
||||
{
|
||||
public static readonly double3 zero = new(0.0, 0.0, 0.0);
|
||||
public static readonly double3 one = new(1.0, 1.0, 1.0);
|
||||
|
|
@ -18,16 +18,6 @@ public struct double3 : IEquatable<double3>
|
|||
this.z = z;
|
||||
}
|
||||
|
||||
public double2 xy
|
||||
{
|
||||
readonly get => new(x, y);
|
||||
set
|
||||
{
|
||||
x = value.x;
|
||||
y = value.y;
|
||||
}
|
||||
}
|
||||
|
||||
public double this[int index]
|
||||
{
|
||||
readonly get => index switch
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ using System;
|
|||
|
||||
namespace CultMath;
|
||||
|
||||
public struct float2 : IEquatable<float2>
|
||||
public partial struct float2 : IEquatable<float2>
|
||||
{
|
||||
public static readonly float2 zero = new(0.0f, 0.0f);
|
||||
public static readonly float2 one = new(1.0f, 1.0f);
|
||||
|
|
@ -16,16 +16,6 @@ public struct float2 : IEquatable<float2>
|
|||
this.y = y;
|
||||
}
|
||||
|
||||
public float2 yx
|
||||
{
|
||||
readonly get => new(y, x);
|
||||
set
|
||||
{
|
||||
y = value.x;
|
||||
x = value.y;
|
||||
}
|
||||
}
|
||||
|
||||
public float this[int index]
|
||||
{
|
||||
readonly get => index switch
|
||||
|
|
|
|||
|
|
@ -1,49 +1,48 @@
|
|||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace CultMath;
|
||||
|
||||
/// <summary>
|
||||
/// HLSL float2x2: row-major. Constructors take rows, <c>m[i]</c> is row i, and
|
||||
/// <c>_mRC</c> names row R, column C exactly as HLSL does. Use <see cref="math.mul(float2x2, float2)"/>
|
||||
/// for products; there is no <c>*</c> matrix product because HLSL's <c>*</c> is component-wise.
|
||||
/// HLSL float2x2: row-major. Constructors take rows, <c>m[i]</c> is a writable reference to
|
||||
/// row i (so <c>m[1][0] = s</c> works), and <c>_mRC</c> names row R, column C exactly as HLSL
|
||||
/// does. Use <see cref="math.mul(float2x2, float2)"/> for products; there is no <c>*</c>
|
||||
/// matrix product because HLSL's <c>*</c> is component-wise.
|
||||
/// </summary>
|
||||
public struct float2x2 : IEquatable<float2x2>
|
||||
{
|
||||
public static readonly float2x2 identity = new(1.0f, 0.0f, 0.0f, 1.0f);
|
||||
public static readonly float2x2 zero = new(0.0f, 0.0f, 0.0f, 0.0f);
|
||||
|
||||
public float _m00;
|
||||
public float _m01;
|
||||
public float _m10;
|
||||
public float _m11;
|
||||
private float2 _row0;
|
||||
private float2 _row1;
|
||||
|
||||
public float2x2(float m00, float m01, float m10, float m11)
|
||||
{
|
||||
_m00 = m00;
|
||||
_m01 = m01;
|
||||
_m10 = m10;
|
||||
_m11 = m11;
|
||||
_row0 = new float2(m00, m01);
|
||||
_row1 = new float2(m10, m11);
|
||||
}
|
||||
|
||||
public float2x2(float2 row0, float2 row1)
|
||||
: this(row0.x, row0.y, row1.x, row1.y)
|
||||
{
|
||||
_row0 = row0;
|
||||
_row1 = row1;
|
||||
}
|
||||
|
||||
public float2 this[int row]
|
||||
public float _m00 { readonly get => _row0.x; set => _row0.x = value; }
|
||||
public float _m01 { readonly get => _row0.y; set => _row0.y = value; }
|
||||
public float _m10 { readonly get => _row1.x; set => _row1.x = value; }
|
||||
public float _m11 { readonly get => _row1.y; set => _row1.y = value; }
|
||||
|
||||
[UnscopedRef]
|
||||
public ref float2 this[int row]
|
||||
{
|
||||
readonly get => row switch
|
||||
{
|
||||
0 => new float2(_m00, _m01),
|
||||
1 => new float2(_m10, _m11),
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(row)),
|
||||
};
|
||||
set
|
||||
get
|
||||
{
|
||||
switch (row)
|
||||
{
|
||||
case 0: _m00 = value.x; _m01 = value.y; break;
|
||||
case 1: _m10 = value.x; _m11 = value.y; break;
|
||||
case 0: return ref _row0;
|
||||
case 1: return ref _row1;
|
||||
default: throw new ArgumentOutOfRangeException(nameof(row));
|
||||
}
|
||||
}
|
||||
|
|
@ -60,10 +59,8 @@ public struct float2x2 : IEquatable<float2x2>
|
|||
return new float2x2(c, -s, s, c);
|
||||
}
|
||||
|
||||
public readonly bool Equals(float2x2 other) =>
|
||||
_m00.Equals(other._m00) && _m01.Equals(other._m01) && _m10.Equals(other._m10) && _m11.Equals(other._m11);
|
||||
|
||||
public readonly bool Equals(float2x2 other) => _row0.Equals(other._row0) && _row1.Equals(other._row1);
|
||||
public override readonly bool Equals(object? obj) => obj is float2x2 other && Equals(other);
|
||||
public override readonly int GetHashCode() => HashCode.Combine(_m00, _m01, _m10, _m11);
|
||||
public override readonly string ToString() => FormattableString.Invariant($"float2x2({_m00:R}, {_m01:R}, {_m10:R}, {_m11:R})");
|
||||
public override readonly int GetHashCode() => HashCode.Combine(_row0, _row1);
|
||||
public override readonly string ToString() => FormattableString.Invariant($"float2x2({_row0.x:R}, {_row0.y:R}, {_row1.x:R}, {_row1.y:R})");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ using System;
|
|||
|
||||
namespace CultMath;
|
||||
|
||||
public struct float3 : IEquatable<float3>
|
||||
public partial struct float3 : IEquatable<float3>
|
||||
{
|
||||
public static readonly float3 zero = new(0.0f, 0.0f, 0.0f);
|
||||
public static readonly float3 one = new(1.0f, 1.0f, 1.0f);
|
||||
|
|
@ -18,105 +18,6 @@ public struct float3 : IEquatable<float3>
|
|||
this.z = z;
|
||||
}
|
||||
|
||||
public float3(float2 xy, float z)
|
||||
: this(xy.x, xy.y, z)
|
||||
{
|
||||
}
|
||||
|
||||
public float2 xy
|
||||
{
|
||||
readonly get => new(x, y);
|
||||
set
|
||||
{
|
||||
x = value.x;
|
||||
y = value.y;
|
||||
}
|
||||
}
|
||||
|
||||
public float2 xz
|
||||
{
|
||||
readonly get => new(x, z);
|
||||
set
|
||||
{
|
||||
x = value.x;
|
||||
z = value.y;
|
||||
}
|
||||
}
|
||||
|
||||
public float2 yx
|
||||
{
|
||||
readonly get => new(y, x);
|
||||
set
|
||||
{
|
||||
y = value.x;
|
||||
x = value.y;
|
||||
}
|
||||
}
|
||||
|
||||
public float2 yz
|
||||
{
|
||||
readonly get => new(y, z);
|
||||
set
|
||||
{
|
||||
y = value.x;
|
||||
z = value.y;
|
||||
}
|
||||
}
|
||||
|
||||
public float2 zy
|
||||
{
|
||||
readonly get => new(z, y);
|
||||
set
|
||||
{
|
||||
z = value.x;
|
||||
y = value.y;
|
||||
}
|
||||
}
|
||||
|
||||
public float3 xyz
|
||||
{
|
||||
readonly get => new(x, y, z);
|
||||
set
|
||||
{
|
||||
x = value.x;
|
||||
y = value.y;
|
||||
z = value.z;
|
||||
}
|
||||
}
|
||||
|
||||
public float3 xzy
|
||||
{
|
||||
readonly get => new(x, z, y);
|
||||
set
|
||||
{
|
||||
x = value.x;
|
||||
z = value.y;
|
||||
y = value.z;
|
||||
}
|
||||
}
|
||||
|
||||
public float3 yzx
|
||||
{
|
||||
readonly get => new(y, z, x);
|
||||
set
|
||||
{
|
||||
y = value.x;
|
||||
z = value.y;
|
||||
x = value.z;
|
||||
}
|
||||
}
|
||||
|
||||
public float3 zyx
|
||||
{
|
||||
readonly get => new(z, y, x);
|
||||
set
|
||||
{
|
||||
z = value.x;
|
||||
y = value.y;
|
||||
x = value.z;
|
||||
}
|
||||
}
|
||||
|
||||
public float this[int index]
|
||||
{
|
||||
readonly get => index switch
|
||||
|
|
|
|||
|
|
@ -1,61 +1,57 @@
|
|||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace CultMath;
|
||||
|
||||
/// <summary>
|
||||
/// HLSL float3x3: row-major. Constructors take rows, <c>m[i]</c> is row i, and
|
||||
/// <c>_mRC</c> names row R, column C exactly as HLSL does. Use <see cref="math.mul(float3x3, float3)"/>
|
||||
/// for products; there is no <c>*</c> matrix product because HLSL's <c>*</c> is component-wise.
|
||||
/// HLSL float3x3: row-major. Constructors take rows, <c>m[i]</c> is a writable reference to
|
||||
/// row i (so <c>m[1][2] = s</c> works), and <c>_mRC</c> names row R, column C exactly as HLSL
|
||||
/// does. Use <see cref="math.mul(float3x3, float3)"/> for products; there is no <c>*</c>
|
||||
/// matrix product because HLSL's <c>*</c> is component-wise.
|
||||
/// </summary>
|
||||
public struct float3x3 : IEquatable<float3x3>
|
||||
{
|
||||
public static readonly float3x3 identity = new(1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f);
|
||||
public static readonly float3x3 zero = new(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f);
|
||||
|
||||
public float _m00;
|
||||
public float _m01;
|
||||
public float _m02;
|
||||
public float _m10;
|
||||
public float _m11;
|
||||
public float _m12;
|
||||
public float _m20;
|
||||
public float _m21;
|
||||
public float _m22;
|
||||
private float3 _row0;
|
||||
private float3 _row1;
|
||||
private float3 _row2;
|
||||
|
||||
public float3x3(float m00, float m01, float m02, float m10, float m11, float m12, float m20, float m21, float m22)
|
||||
{
|
||||
_m00 = m00;
|
||||
_m01 = m01;
|
||||
_m02 = m02;
|
||||
_m10 = m10;
|
||||
_m11 = m11;
|
||||
_m12 = m12;
|
||||
_m20 = m20;
|
||||
_m21 = m21;
|
||||
_m22 = m22;
|
||||
_row0 = new float3(m00, m01, m02);
|
||||
_row1 = new float3(m10, m11, m12);
|
||||
_row2 = new float3(m20, m21, m22);
|
||||
}
|
||||
|
||||
public float3x3(float3 row0, float3 row1, float3 row2)
|
||||
: this(row0.x, row0.y, row0.z, row1.x, row1.y, row1.z, row2.x, row2.y, row2.z)
|
||||
{
|
||||
_row0 = row0;
|
||||
_row1 = row1;
|
||||
_row2 = row2;
|
||||
}
|
||||
|
||||
public float3 this[int row]
|
||||
public float _m00 { readonly get => _row0.x; set => _row0.x = value; }
|
||||
public float _m01 { readonly get => _row0.y; set => _row0.y = value; }
|
||||
public float _m02 { readonly get => _row0.z; set => _row0.z = value; }
|
||||
public float _m10 { readonly get => _row1.x; set => _row1.x = value; }
|
||||
public float _m11 { readonly get => _row1.y; set => _row1.y = value; }
|
||||
public float _m12 { readonly get => _row1.z; set => _row1.z = value; }
|
||||
public float _m20 { readonly get => _row2.x; set => _row2.x = value; }
|
||||
public float _m21 { readonly get => _row2.y; set => _row2.y = value; }
|
||||
public float _m22 { readonly get => _row2.z; set => _row2.z = value; }
|
||||
|
||||
[UnscopedRef]
|
||||
public ref float3 this[int row]
|
||||
{
|
||||
readonly get => row switch
|
||||
{
|
||||
0 => new float3(_m00, _m01, _m02),
|
||||
1 => new float3(_m10, _m11, _m12),
|
||||
2 => new float3(_m20, _m21, _m22),
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(row)),
|
||||
};
|
||||
set
|
||||
get
|
||||
{
|
||||
switch (row)
|
||||
{
|
||||
case 0: _m00 = value.x; _m01 = value.y; _m02 = value.z; break;
|
||||
case 1: _m10 = value.x; _m11 = value.y; _m12 = value.z; break;
|
||||
case 2: _m20 = value.x; _m21 = value.y; _m22 = value.z; break;
|
||||
case 0: return ref _row0;
|
||||
case 1: return ref _row1;
|
||||
case 2: return ref _row2;
|
||||
default: throw new ArgumentOutOfRangeException(nameof(row));
|
||||
}
|
||||
}
|
||||
|
|
@ -110,16 +106,10 @@ public struct float3x3 : IEquatable<float3x3>
|
|||
public static float3x3 Euler(float x, float y, float z, math.RotationOrder order = math.RotationOrder.Default) =>
|
||||
Euler(new float3(x, y, z), order);
|
||||
|
||||
public readonly bool Equals(float3x3 other) =>
|
||||
_m00.Equals(other._m00) && _m01.Equals(other._m01) && _m02.Equals(other._m02) &&
|
||||
_m10.Equals(other._m10) && _m11.Equals(other._m11) && _m12.Equals(other._m12) &&
|
||||
_m20.Equals(other._m20) && _m21.Equals(other._m21) && _m22.Equals(other._m22);
|
||||
|
||||
public readonly bool Equals(float3x3 other) => _row0.Equals(other._row0) && _row1.Equals(other._row1) && _row2.Equals(other._row2);
|
||||
public override readonly bool Equals(object? obj) => obj is float3x3 other && Equals(other);
|
||||
|
||||
public override readonly int GetHashCode() =>
|
||||
HashCode.Combine(HashCode.Combine(_m00, _m01, _m02), HashCode.Combine(_m10, _m11, _m12), HashCode.Combine(_m20, _m21, _m22));
|
||||
public override readonly int GetHashCode() => HashCode.Combine(_row0, _row1, _row2);
|
||||
|
||||
public override readonly string ToString() =>
|
||||
FormattableString.Invariant($"float3x3({_m00:R}, {_m01:R}, {_m02:R}, {_m10:R}, {_m11:R}, {_m12:R}, {_m20:R}, {_m21:R}, {_m22:R})");
|
||||
FormattableString.Invariant($"float3x3({_row0.x:R}, {_row0.y:R}, {_row0.z:R}, {_row1.x:R}, {_row1.y:R}, {_row1.z:R}, {_row2.x:R}, {_row2.y:R}, {_row2.z:R})");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ using System;
|
|||
|
||||
namespace CultMath;
|
||||
|
||||
public struct float4 : IEquatable<float4>
|
||||
public partial struct float4 : IEquatable<float4>
|
||||
{
|
||||
public static readonly float4 zero = new(0.0f, 0.0f, 0.0f, 0.0f);
|
||||
public static readonly float4 one = new(1.0f, 1.0f, 1.0f, 1.0f);
|
||||
|
|
@ -20,64 +20,6 @@ public struct float4 : IEquatable<float4>
|
|||
this.w = w;
|
||||
}
|
||||
|
||||
public float4(float2 xy, float z, float w)
|
||||
: this(xy.x, xy.y, z, w)
|
||||
{
|
||||
}
|
||||
|
||||
public float4(float3 xyz, float w)
|
||||
: this(xyz.x, xyz.y, xyz.z, w)
|
||||
{
|
||||
}
|
||||
|
||||
public float4(float x, float3 yzw)
|
||||
: this(x, yzw.x, yzw.y, yzw.z)
|
||||
{
|
||||
}
|
||||
|
||||
public float2 xy
|
||||
{
|
||||
readonly get => new(x, y);
|
||||
set
|
||||
{
|
||||
x = value.x;
|
||||
y = value.y;
|
||||
}
|
||||
}
|
||||
|
||||
public float3 xyz
|
||||
{
|
||||
readonly get => new(x, y, z);
|
||||
set
|
||||
{
|
||||
x = value.x;
|
||||
y = value.y;
|
||||
z = value.z;
|
||||
}
|
||||
}
|
||||
|
||||
public float3 xyw
|
||||
{
|
||||
readonly get => new(x, y, w);
|
||||
set
|
||||
{
|
||||
x = value.x;
|
||||
y = value.y;
|
||||
w = value.z;
|
||||
}
|
||||
}
|
||||
|
||||
public float3 yzx
|
||||
{
|
||||
readonly get => new(y, z, x);
|
||||
set
|
||||
{
|
||||
y = value.x;
|
||||
z = value.y;
|
||||
x = value.z;
|
||||
}
|
||||
}
|
||||
|
||||
public float this[int index]
|
||||
{
|
||||
readonly get => index switch
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ using System;
|
|||
|
||||
namespace CultMath;
|
||||
|
||||
public struct int2 : IEquatable<int2>
|
||||
public partial struct int2 : IEquatable<int2>
|
||||
{
|
||||
public static readonly int2 zero = new(0, 0);
|
||||
public static readonly int2 one = new(1, 1);
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ using System;
|
|||
|
||||
namespace CultMath;
|
||||
|
||||
public struct int3 : IEquatable<int3>
|
||||
public partial struct int3 : IEquatable<int3>
|
||||
{
|
||||
public static readonly int3 zero = new(0, 0, 0);
|
||||
public static readonly int3 one = new(1, 1, 1);
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ using System;
|
|||
|
||||
namespace CultMath;
|
||||
|
||||
public struct int4 : IEquatable<int4>
|
||||
public partial struct int4 : IEquatable<int4>
|
||||
{
|
||||
public static readonly int4 zero = new(0, 0, 0, 0);
|
||||
public static readonly int4 one = new(1, 1, 1, 1);
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
using System;
|
||||
using System;
|
||||
|
||||
namespace CultMath;
|
||||
|
||||
public static class math
|
||||
public static partial class math
|
||||
{
|
||||
public const float PI = MathF.PI;
|
||||
public const float TAU = MathF.PI * 2.0f;
|
||||
|
|
@ -30,13 +30,10 @@ public static class math
|
|||
public static bool2 bool2(bool value) => new(value, value);
|
||||
public static int2 int2(int x, int y) => new(x, y);
|
||||
public static int2 int2(int value) => new(value, value);
|
||||
// Mixed constructor functions (float4(float2, float2), ...) are generated in Swizzles.g.cs.
|
||||
public static float3 float3(float x, float y, float z) => new(x, y, z);
|
||||
public static float3 float3(float2 xy, float z) => new(xy, z);
|
||||
public static float3 float3(float value) => new(value, value, value);
|
||||
public static float4 float4(float x, float y, float z, float w) => new(x, y, z, w);
|
||||
public static float4 float4(float2 xy, float z, float w) => new(xy, z, w);
|
||||
public static float4 float4(float3 xyz, float w) => new(xyz, w);
|
||||
public static float4 float4(float x, float3 yzw) => new(x, yzw);
|
||||
public static float4 float4(float value) => new(value, value, value, value);
|
||||
public static bool3 bool3(bool x, bool y, bool z) => new(x, y, z);
|
||||
public static bool3 bool3(bool value) => new(value, value, value);
|
||||
|
|
@ -79,11 +76,20 @@ public static class math
|
|||
public static float2 abs(float2 value) => new(abs(value.x), abs(value.y));
|
||||
public static float3 abs(float3 value) => new(abs(value.x), abs(value.y), abs(value.z));
|
||||
public static float4 abs(float4 value) => new(abs(value.x), abs(value.y), abs(value.z), abs(value.w));
|
||||
public static int abs(int value) => Math.Abs(value);
|
||||
public static int2 abs(int2 value) => new(abs(value.x), abs(value.y));
|
||||
public static int3 abs(int3 value) => new(abs(value.x), abs(value.y), abs(value.z));
|
||||
public static int4 abs(int4 value) => new(abs(value.x), abs(value.y), abs(value.z), abs(value.w));
|
||||
|
||||
public static float sign(float value) => MathF.Sign(value);
|
||||
public static float2 sign(float2 value) => new(sign(value.x), sign(value.y));
|
||||
public static float3 sign(float3 value) => new(sign(value.x), sign(value.y), sign(value.z));
|
||||
public static float4 sign(float4 value) => new(sign(value.x), sign(value.y), sign(value.z), sign(value.w));
|
||||
// HLSL sign returns int. NaN compares false both ways, so it yields 0 instead of throwing.
|
||||
public static int sign(float value) => value > 0.0f ? 1 : value < 0.0f ? -1 : 0;
|
||||
public static int2 sign(float2 value) => new(sign(value.x), sign(value.y));
|
||||
public static int3 sign(float3 value) => new(sign(value.x), sign(value.y), sign(value.z));
|
||||
public static int4 sign(float4 value) => new(sign(value.x), sign(value.y), sign(value.z), sign(value.w));
|
||||
public static int sign(int value) => value > 0 ? 1 : value < 0 ? -1 : 0;
|
||||
public static int2 sign(int2 value) => new(sign(value.x), sign(value.y));
|
||||
public static int3 sign(int3 value) => new(sign(value.x), sign(value.y), sign(value.z));
|
||||
public static int4 sign(int4 value) => new(sign(value.x), sign(value.y), sign(value.z), sign(value.w));
|
||||
|
||||
public static float floor(float value) => MathF.Floor(value);
|
||||
public static float2 floor(float2 value) => new(floor(value.x), floor(value.y));
|
||||
|
|
@ -105,12 +111,18 @@ public static class math
|
|||
public static float2 min(float2 left, float2 right) => new(min(left.x, right.x), min(left.y, right.y));
|
||||
public static float3 min(float3 left, float3 right) => new(min(left.x, right.x), min(left.y, right.y), min(left.z, right.z));
|
||||
public static float4 min(float4 left, float4 right) => new(min(left.x, right.x), min(left.y, right.y), min(left.z, right.z), min(left.w, right.w));
|
||||
public static int2 min(int2 left, int2 right) => new(min(left.x, right.x), min(left.y, right.y));
|
||||
public static int3 min(int3 left, int3 right) => new(min(left.x, right.x), min(left.y, right.y), min(left.z, right.z));
|
||||
public static int4 min(int4 left, int4 right) => new(min(left.x, right.x), min(left.y, right.y), min(left.z, right.z), min(left.w, right.w));
|
||||
|
||||
public static float max(float left, float right) => MathF.Max(left, right);
|
||||
public static int max(int left, int right) => Math.Max(left, right);
|
||||
public static float2 max(float2 left, float2 right) => new(max(left.x, right.x), max(left.y, right.y));
|
||||
public static float3 max(float3 left, float3 right) => new(max(left.x, right.x), max(left.y, right.y), max(left.z, right.z));
|
||||
public static float4 max(float4 left, float4 right) => new(max(left.x, right.x), max(left.y, right.y), max(left.z, right.z), max(left.w, right.w));
|
||||
public static int2 max(int2 left, int2 right) => new(max(left.x, right.x), max(left.y, right.y));
|
||||
public static int3 max(int3 left, int3 right) => new(max(left.x, right.x), max(left.y, right.y), max(left.z, right.z));
|
||||
public static int4 max(int4 left, int4 right) => new(max(left.x, right.x), max(left.y, right.y), max(left.z, right.z), max(left.w, right.w));
|
||||
|
||||
public static float clamp(float value, float minimum, float maximum) => min(max(value, minimum), maximum);
|
||||
public static float2 clamp(float2 value, float2 minimum, float2 maximum) => min(max(value, minimum), maximum);
|
||||
|
|
@ -138,7 +150,8 @@ public static class math
|
|||
public static double2 unlerp(double2 start, double2 end, double2 value) => (value - start) / (end - start);
|
||||
public static double3 unlerp(double3 start, double3 end, double3 value) => (value - start) / (end - start);
|
||||
|
||||
public static float step(float edge, float value) => value < edge ? 0.0f : 1.0f;
|
||||
// HLSL: step(y, x) = x >= y ? 1 : 0, so a NaN on either side yields 0.
|
||||
public static float step(float edge, float value) => value >= edge ? 1.0f : 0.0f;
|
||||
public static float2 step(float2 edge, float2 value) => new(step(edge.x, value.x), step(edge.y, value.y));
|
||||
public static float3 step(float3 edge, float3 value) => new(step(edge.x, value.x), step(edge.y, value.y), step(edge.z, value.z));
|
||||
public static float4 step(float4 edge, float4 value) => new(step(edge.x, value.x), step(edge.y, value.y), step(edge.z, value.z), step(edge.w, value.w));
|
||||
|
|
@ -396,6 +409,20 @@ public static class math
|
|||
public static bool all(bool3 value) => value.x && value.y && value.z;
|
||||
public static bool all(bool4 value) => value.x && value.y && value.z && value.w;
|
||||
|
||||
// HLSL any/all on numeric vectors test components against zero (NaN != 0 counts as true).
|
||||
public static bool any(float2 value) => value.x != 0.0f || value.y != 0.0f;
|
||||
public static bool any(float3 value) => value.x != 0.0f || value.y != 0.0f || value.z != 0.0f;
|
||||
public static bool any(float4 value) => value.x != 0.0f || value.y != 0.0f || value.z != 0.0f || value.w != 0.0f;
|
||||
public static bool all(float2 value) => value.x != 0.0f && value.y != 0.0f;
|
||||
public static bool all(float3 value) => value.x != 0.0f && value.y != 0.0f && value.z != 0.0f;
|
||||
public static bool all(float4 value) => value.x != 0.0f && value.y != 0.0f && value.z != 0.0f && value.w != 0.0f;
|
||||
public static bool any(int2 value) => value.x != 0 || value.y != 0;
|
||||
public static bool any(int3 value) => value.x != 0 || value.y != 0 || value.z != 0;
|
||||
public static bool any(int4 value) => value.x != 0 || value.y != 0 || value.z != 0 || value.w != 0;
|
||||
public static bool all(int2 value) => value.x != 0 && value.y != 0;
|
||||
public static bool all(int3 value) => value.x != 0 && value.y != 0 && value.z != 0;
|
||||
public static bool all(int4 value) => value.x != 0 && value.y != 0 && value.z != 0 && value.w != 0;
|
||||
|
||||
// HLSL 2021 argument order: select(condition, whenTrue, whenFalse).
|
||||
// Unity.Mathematics' select(falseValue, trueValue, condition) is the reverse.
|
||||
public static float select(bool condition, float whenTrue, float whenFalse) => condition ? whenTrue : whenFalse;
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
using System.Text.Json;
|
||||
using System.Text.Json;
|
||||
using Xunit;
|
||||
|
||||
namespace CultMath.Tests;
|
||||
|
|
@ -84,6 +84,93 @@ public sealed class HlslSemanticsTests
|
|||
Assert.Equal(new float4(9.0f, 7.0f, 8.0f, 4.0f), d);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GeneratedSwizzlesCoverRepeatsColorNamesAndAllVectorFamilies()
|
||||
{
|
||||
var v = new float4(1.0f, 2.0f, 3.0f, 4.0f);
|
||||
Assert.Equal(new float4(4.0f, 4.0f, 1.0f, 2.0f), v.wwxy);
|
||||
Assert.Equal(new float3(3.0f, 2.0f, 1.0f), v.bgr);
|
||||
Assert.Equal(4.0f, v.a);
|
||||
Assert.Equal(new float4(2.0f, 2.0f, 2.0f, 2.0f), new float2(1.0f, 2.0f).yyyy);
|
||||
|
||||
v.wx = new float2(9.0f, 8.0f);
|
||||
Assert.Equal(new float4(8.0f, 2.0f, 3.0f, 9.0f), v);
|
||||
v.r = 0.5f;
|
||||
v.gb += new float2(1.0f, 1.0f);
|
||||
Assert.Equal(new float4(0.5f, 3.0f, 4.0f, 9.0f), v);
|
||||
|
||||
Assert.Equal(new int3(3, 1, 1), new int3(1, 2, 3).zxx);
|
||||
Assert.Equal(new bool2(false, true), new bool4(true, false, false, true).zw);
|
||||
Assert.Equal(new double2(2.0, 1.0), new double3(1.0, 2.0, 3.0).yx);
|
||||
|
||||
var cell = new int4(1, 2, 3, 4);
|
||||
cell.xyz = new int3(7, 8, 9);
|
||||
Assert.Equal(new int4(7, 8, 9, 4), cell);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MixedConstructorsMatchHlsl()
|
||||
{
|
||||
var xy = new float2(1.0f, 2.0f);
|
||||
var zw = new float2(3.0f, 4.0f);
|
||||
var expected = new float4(1.0f, 2.0f, 3.0f, 4.0f);
|
||||
|
||||
Assert.Equal(expected, math.float4(xy, zw));
|
||||
Assert.Equal(expected, math.float4(1.0f, new float3(2.0f, 3.0f, 4.0f)));
|
||||
Assert.Equal(expected, math.float4(new float3(1.0f, 2.0f, 3.0f), 4.0f));
|
||||
Assert.Equal(expected, math.float4(xy, 3.0f, 4.0f));
|
||||
Assert.Equal(expected, math.float4(1.0f, 2.0f, zw));
|
||||
Assert.Equal(expected, math.float4(1.0f, new float2(2.0f, 3.0f), 4.0f));
|
||||
Assert.Equal(expected, new float4(xy, zw));
|
||||
Assert.Equal(new float3(1.0f, 2.0f, 3.0f), math.float3(1.0f, zw - 1.0f));
|
||||
Assert.Equal(new int4(1, 2, 3, 4), math.int4(new int2(1, 2), new int2(3, 4)));
|
||||
Assert.Equal(new bool3(true, false, true), math.bool3(true, new bool2(false, true)));
|
||||
Assert.Equal(new double3(1.0, 2.0, 3.0), new double3(new double2(1.0, 2.0), 3.0));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MatrixRowElementsAreWritable()
|
||||
{
|
||||
var m = float3x3.identity;
|
||||
m[1][2] = 5.0f;
|
||||
m[2].x = 7.0f;
|
||||
m._m00 = 2.0f;
|
||||
|
||||
Assert.Equal(5.0f, m._m12);
|
||||
Assert.Equal(7.0f, m._m20);
|
||||
Assert.Equal(math.float3x3(2.0f, 0.0f, 0.0f, 0.0f, 1.0f, 5.0f, 7.0f, 0.0f, 1.0f), m);
|
||||
Assert.Equal(float3x3.identity, math.float3x3(1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f));
|
||||
|
||||
var r = float2x2.identity;
|
||||
r[0][1] = 3.0f;
|
||||
Assert.Equal(math.float2x2(1.0f, 3.0f, 0.0f, 1.0f), r);
|
||||
Assert.Equal(new float2(0.0f, 1.0f), float2x2.identity[1]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IntrinsicEdgeCasesFollowHlsl()
|
||||
{
|
||||
Assert.True(math.any(new float3(0.0f, 0.0f, -2.0f)));
|
||||
Assert.False(math.any(float4.zero));
|
||||
Assert.True(math.all(new int2(1, -1)));
|
||||
Assert.False(math.all(new int3(1, 0, 1)));
|
||||
Assert.True(math.any(new float2(float.NaN, 0.0f)));
|
||||
|
||||
Assert.Equal(new int3(1, 2, 3), math.min(new int3(1, 5, 3), new int3(4, 2, 6)));
|
||||
Assert.Equal(new int2(4, 5), math.max(new int2(1, 5), new int2(4, 2)));
|
||||
Assert.Equal(new int4(1, 2, 0, 3), math.abs(new int4(-1, 2, 0, -3)));
|
||||
Assert.Equal(7, math.abs(-7));
|
||||
|
||||
Assert.Equal(0.0f, math.step(1.0f, float.NaN));
|
||||
Assert.Equal(0.0f, math.step(float.NaN, 1.0f));
|
||||
Assert.Equal(1.0f, math.step(1.0f, 1.0f));
|
||||
|
||||
Assert.Equal(0, math.sign(float.NaN));
|
||||
Assert.Equal(-1, math.sign(-0.5f));
|
||||
Assert.Equal(new int3(-1, 0, 1), math.sign(new float3(-2.0f, 0.0f, 4.0f)));
|
||||
Assert.Equal(new int2(1, -1), math.sign(new int2(9, -9)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ComparisonOperatorsReturnComponentWiseBoolVectors()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -170,10 +170,10 @@ public sealed class MathTests
|
|||
[Fact]
|
||||
public void SignIsComponentWiseForSimulationVectors()
|
||||
{
|
||||
Assert.Equal(-1.0f, math.sign(-12.0f));
|
||||
Assert.Equal(0.0f, math.sign(0.0f));
|
||||
Assert.Equal(1.0f, math.sign(12.0f));
|
||||
Assert.Equal(new float3(-1.0f, 0.0f, 1.0f), math.sign(new float3(-2.0f, 0.0f, 4.0f)));
|
||||
Assert.Equal(-1, math.sign(-12.0f));
|
||||
Assert.Equal(0, math.sign(0.0f));
|
||||
Assert.Equal(1, math.sign(12.0f));
|
||||
Assert.Equal(new int3(-1, 0, 1), math.sign(new float3(-2.0f, 0.0f, 4.0f)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
|
|
|||
132
packages/cultmath/tools/generate-swizzles.ps1
Normal file
132
packages/cultmath/tools/generate-swizzles.ps1
Normal file
|
|
@ -0,0 +1,132 @@
|
|||
param(
|
||||
[string]$OutputPath = ""
|
||||
)
|
||||
|
||||
# Writes src\CultMath\Swizzles.g.cs: HLSL swizzles and mixed constructors for
|
||||
# CultMath's vector types. The output is checked in; rerun after changing the
|
||||
# family table below and commit both files together.
|
||||
|
||||
$ErrorActionPreference = "Stop"
|
||||
|
||||
if (-not $OutputPath) {
|
||||
$OutputPath = Join-Path $PSScriptRoot "..\src\CultMath\Swizzles.g.cs"
|
||||
}
|
||||
|
||||
# Functions: emit math.<type>(...) constructor functions. double has none because
|
||||
# a math.double3 method group would hide the double3 type name under
|
||||
# `using static CultMath.math;` and HLSL shader code does not construct doubles.
|
||||
$families = @(
|
||||
@{ Scalar = "float"; Sizes = @(2, 3, 4); Functions = $true },
|
||||
@{ Scalar = "int"; Sizes = @(2, 3, 4); Functions = $true },
|
||||
@{ Scalar = "bool"; Sizes = @(2, 3, 4); Functions = $true },
|
||||
@{ Scalar = "double"; Sizes = @(2, 3); Functions = $false }
|
||||
)
|
||||
$componentFields = @("x", "y", "z", "w")
|
||||
$namings = @(@("x", "y", "z", "w"), @("r", "g", "b", "a"))
|
||||
|
||||
function Get-Compositions([int]$total) {
|
||||
# Ordered part sizes summing to $total, excluding the all-scalar form that the
|
||||
# hand-written base constructor already owns.
|
||||
$results = [System.Collections.Generic.List[object]]::new()
|
||||
function Expand([int]$remaining, [int[]]$prefix) {
|
||||
if ($remaining -eq 0) {
|
||||
if (@($prefix | Where-Object { $_ -ne 1 }).Count -gt 0) {
|
||||
$results.Add($prefix)
|
||||
}
|
||||
return
|
||||
}
|
||||
for ($part = 1; $part -le $remaining; $part++) {
|
||||
Expand ($remaining - $part) ($prefix + $part)
|
||||
}
|
||||
}
|
||||
Expand $total @()
|
||||
return , $results
|
||||
}
|
||||
|
||||
$builder = [System.Text.StringBuilder]::new()
|
||||
function Line([string]$text = "") { [void]$builder.Append($text).Append("`n") }
|
||||
|
||||
Line "// <auto-generated>"
|
||||
Line "// Generated by tools/generate-swizzles.ps1. Do not edit by hand."
|
||||
Line "// </auto-generated>"
|
||||
Line ""
|
||||
Line "namespace CultMath;"
|
||||
|
||||
$functionLines = [System.Collections.Generic.List[string]]::new()
|
||||
|
||||
foreach ($family in $families) {
|
||||
$scalar = $family.Scalar
|
||||
$maxSize = ($family.Sizes | Measure-Object -Maximum).Maximum
|
||||
foreach ($size in $family.Sizes) {
|
||||
$type = "$scalar$size"
|
||||
Line ""
|
||||
Line "public partial struct $type"
|
||||
Line "{"
|
||||
|
||||
foreach ($composition in (Get-Compositions $size)) {
|
||||
$parameters = @()
|
||||
$arguments = @()
|
||||
$offset = 0
|
||||
foreach ($part in $composition) {
|
||||
$name = -join $componentFields[$offset..($offset + $part - 1)]
|
||||
if ($part -eq 1) {
|
||||
$parameters += "$scalar $name"
|
||||
$arguments += $name
|
||||
} else {
|
||||
$parameters += "$scalar$part $name"
|
||||
for ($i = 0; $i -lt $part; $i++) { $arguments += "$name.$($componentFields[$i])" }
|
||||
}
|
||||
$offset += $part
|
||||
}
|
||||
$parameterList = $parameters -join ", "
|
||||
Line " public $type($parameterList) : this($($arguments -join ', ')) { }"
|
||||
if ($family.Functions) {
|
||||
$names = @($parameters | ForEach-Object { ($_ -split " ")[1] })
|
||||
$functionLines.Add(" public static $type $type($parameterList) => new($($names -join ', '));")
|
||||
}
|
||||
}
|
||||
|
||||
# Single-component color aliases; x/y/z/w are the fields themselves.
|
||||
$letters = $namings[1]
|
||||
for ($i = 0; $i -lt $size; $i++) {
|
||||
Line " public $scalar $($letters[$i]) { readonly get => $($componentFields[$i]); set => $($componentFields[$i]) = value; }"
|
||||
}
|
||||
|
||||
foreach ($letters in $namings) {
|
||||
for ($length = 2; $length -le $maxSize; $length++) {
|
||||
$count = [math]::Pow($size, $length)
|
||||
for ($index = 0; $index -lt $count; $index++) {
|
||||
[int[]]$digits = @()
|
||||
[int]$remainder = $index
|
||||
for ($d = 0; $d -lt $length; $d++) {
|
||||
$digits = @([int]($remainder % $size)) + $digits
|
||||
$remainder = [int][math]::Floor($remainder / $size)
|
||||
}
|
||||
$name = -join ($digits | ForEach-Object { $letters[$_] })
|
||||
$getter = ($digits | ForEach-Object { $componentFields[$_] }) -join ", "
|
||||
$resultType = "$scalar$length"
|
||||
$unique = @($digits | Select-Object -Unique).Count -eq $length
|
||||
if ($unique) {
|
||||
$assignments = for ($d = 0; $d -lt $length; $d++) { "$($componentFields[$digits[$d]]) = value.$($componentFields[$d]);" }
|
||||
Line " public $resultType $name { readonly get => new($getter); set { $($assignments -join ' ') } }"
|
||||
} else {
|
||||
Line " public readonly $resultType $name => new($getter);"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Line "}"
|
||||
}
|
||||
}
|
||||
|
||||
Line ""
|
||||
Line "public static partial class math"
|
||||
Line "{"
|
||||
foreach ($functionLine in $functionLines) { Line $functionLine }
|
||||
Line "}"
|
||||
|
||||
$fullPath = [System.IO.Path]::GetFullPath($OutputPath)
|
||||
$content = $builder.ToString() -replace "`n", "`r`n"
|
||||
[System.IO.File]::WriteAllText($fullPath, $content, [System.Text.UTF8Encoding]::new($false))
|
||||
Write-Host "Wrote $fullPath ($((Get-Item -LiteralPath $fullPath).Length) bytes)"
|
||||
7
packages/cultmath/tools/generate-swizzles.ps1.meta
Normal file
7
packages/cultmath/tools/generate-swizzles.ps1.meta
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
fileFormatVersion: 2
|
||||
guid: d7a796fd2d87489c9f055fd6e73513b5
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Loading…
Reference in a new issue