From 90c7538639b6366149c355719cf59d285b930270 Mon Sep 17 00:00:00 2001
From: Youmu Liu <105042599+Nitload-NSI@users.noreply.github.com>
Date: Mon, 7 Sep 2026 17:51:15 +0800
Subject: [PATCH 1/3] Graphic mat 44 is now generic; TextRegex source gen will
use goto instead of do{}while()
---
.cr/team/Editor/Formatting/Braces.CSharp.xml | 23 ++
.editorconfig | 12 -
src/Nitload.Graphics/Camera.cs | 64 ++---
src/Nitload.Graphics/OpenGL/Buffer/EBO.cs | 21 --
src/Nitload.Graphics/OpenGL/Buffer/FBO.cs | 21 --
src/Nitload.Graphics/OpenGL/Buffer/RBO.cs | 21 --
.../OpenGL/Buffer/TextureGL.cs | 54 ----
src/Nitload.Graphics/OpenGL/Buffer/VAO.cs | 21 --
src/Nitload.Graphics/OpenGL/Buffer/VBO.cs | 21 --
.../OpenGL/Generated/OpenGLFunction.Base.g.cs | 11 +-
.../OpenGL/Generated/OpenGLFunction.v4_1.g.cs | 11 +-
.../Coordination/CoordinationBase.cs | 42 ++-
.../Coordination/PlainCoordinate.cs | 8 +-
.../Geometries/IInstancing.cs | 12 +-
.../Numeric/Graphics/GMatrix44_T.cs | 190 +++++++++++++
.../Numeric/Graphics/GraphicsMatrix.cs | 261 ------------------
.../Numeric/Graphics/Vec3.cs | 131 ++++++---
.../Numeric/Graphics/Vec3_T.cs | 121 +++++---
.../Numeric/Graphics/Vec4.cs | 87 ++++--
.../Numeric/Graphics/Vec4_T.cs | 183 +++++++++---
.../Text/TextRegexSourceGen.ExitLabel.cs | 47 ++++
.../Text/TextRegexSourceGen.GenClass.cs | 111 +++++---
.../Text/TextRegexSourceGen.GenContext.cs | 30 +-
.../Text/TextRegexSourceGen.GenFile.cs | 7 +-
.../Generation/GlOverloadApiFilter.cs | 4 +-
.../Generation/OpenGlGenerator.cs | 2 +-
tools/Nitload.GenerateGL/README.md | 2 +-
27 files changed, 805 insertions(+), 713 deletions(-)
create mode 100644 .cr/team/Editor/Formatting/Braces.CSharp.xml
delete mode 100644 .editorconfig
delete mode 100644 src/Nitload.Graphics/OpenGL/Buffer/EBO.cs
delete mode 100644 src/Nitload.Graphics/OpenGL/Buffer/FBO.cs
delete mode 100644 src/Nitload.Graphics/OpenGL/Buffer/RBO.cs
delete mode 100644 src/Nitload.Graphics/OpenGL/Buffer/TextureGL.cs
delete mode 100644 src/Nitload.Graphics/OpenGL/Buffer/VAO.cs
delete mode 100644 src/Nitload.Graphics/OpenGL/Buffer/VBO.cs
create mode 100644 src/Nitload.Mathematics/Numeric/Graphics/GMatrix44_T.cs
delete mode 100644 src/Nitload.Mathematics/Numeric/Graphics/GraphicsMatrix.cs
create mode 100644 src/Nitload.RegularAnalysis/Text/TextRegexSourceGen.ExitLabel.cs
diff --git a/.cr/team/Editor/Formatting/Braces.CSharp.xml b/.cr/team/Editor/Formatting/Braces.CSharp.xml
new file mode 100644
index 00000000..1c194ef8
--- /dev/null
+++ b/.cr/team/Editor/Formatting/Braces.CSharp.xml
@@ -0,0 +1,23 @@
+
+
+
+
+ -
+
+
+
+
+
+
+
+ -
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.editorconfig b/.editorconfig
deleted file mode 100644
index 3ea7454f..00000000
--- a/.editorconfig
+++ /dev/null
@@ -1,12 +0,0 @@
-[*]
-end_of_line = lf
-charset = utf-8
-insert_final_newline = true
-trim_trailing_whitespace = true
-
-[*.{bat,cmd,ps1}]
-end_of_line = crlf
-
-[*.cs]
-# CS1591: 缺少对公共可见类型或成员的 XML 注释
-dotnet_diagnostic.CS1591.severity = suggestion
\ No newline at end of file
diff --git a/src/Nitload.Graphics/Camera.cs b/src/Nitload.Graphics/Camera.cs
index 8af8bb60..6a387a7d 100644
--- a/src/Nitload.Graphics/Camera.cs
+++ b/src/Nitload.Graphics/Camera.cs
@@ -15,11 +15,11 @@ namespace Nitload.Mathematics.Numeric.Graphics
{
private bool _isLookAtAvailable;
- internal GraphicsMatrix _lookAt;
- internal GraphicsMatrix _projection;
- internal GraphicsMatrix cameraAtt;
- internal GraphicsMatrix rotation;
- internal GraphicsMatrix rotationAtt;
+ internal GMatrix44 _lookAt;
+ internal GMatrix44 _projection;
+ internal GMatrix44 cameraAtt;
+ internal GMatrix44 rotation;
+ internal GMatrix44 rotationAtt;
internal Radius _pitch;
internal Radius _row;
internal Radius _yaw;
@@ -27,13 +27,13 @@ namespace Nitload.Mathematics.Numeric.Graphics
public Camera()
{
- cameraAtt = GraphicsMatrix.Unit;
+ cameraAtt = GMatrix44.Unit;
_target = Vec3.Zero;
up = Vec3.Zero;
_isLookAtAvailable = false;
- _lookAt = GraphicsMatrix.Unit;
- rotationAtt = GraphicsMatrix.Unit;
- _projection = new GraphicsMatrix();
+ _lookAt = GMatrix44.Unit;
+ rotationAtt = GMatrix44.Unit;
+ _projection = new GMatrix44();
}
public Camera(
@@ -43,32 +43,32 @@ namespace Nitload.Mathematics.Numeric.Graphics
)
: this()
{
- rotationAtt = GraphicsMatrix.Unit;
- cameraAtt = new GraphicsMatrix();
+ rotationAtt = GMatrix44.Unit;
+ cameraAtt = new GMatrix44();
SetViewDirection(position, target, up);
_pitch = Radius.Zero;
_row = Radius.Zero;
_yaw = Radius.Zero;
}
- public GraphicsMatrix Projection => _projection;
+ public GMatrix44 Projection => _projection;
- public GraphicsMatrix View
+ public GMatrix44 View
{
get
{
if (_isLookAtAvailable) {
return _lookAt;
}
- GraphicsMatrix move = GraphicsMatrix.Unit;
- move.column[3] -= cameraAtt.column[3];
+ GMatrix44 move = GMatrix44.Unit;
+ move[3] = move[3] - cameraAtt[3];
_lookAt = rotationAtt.Transpose * move;
_isLookAtAvailable = true;
return _lookAt;
}
}
- public GraphicsMatrix CameraMatrix()
+ public GMatrix44 CameraMatrix()
{
return Projection * View;
}
@@ -77,7 +77,7 @@ namespace Nitload.Mathematics.Numeric.Graphics
float distance
)
{
- cameraAtt.column[3].Z -= distance;
+ cameraAtt[3, 2] -= distance;
_isLookAtAvailable = false;
}
@@ -85,7 +85,7 @@ namespace Nitload.Mathematics.Numeric.Graphics
float distance
)
{
- cameraAtt.column[3].X -= distance;
+ cameraAtt[3, 0] -= distance;
_isLookAtAvailable = false;
}
@@ -93,7 +93,7 @@ namespace Nitload.Mathematics.Numeric.Graphics
float distance
)
{
- cameraAtt.column[3].Y -= distance;
+ cameraAtt[3, 1] -= distance;
_isLookAtAvailable = false;
}
@@ -104,9 +104,9 @@ namespace Nitload.Mathematics.Numeric.Graphics
)
{
Vec3 direction = position - target;
- if ((position == cameraAtt.column[3].XYZ) &&
+ if ((position == new Vec3(cameraAtt[3, 0], cameraAtt[3, 1], cameraAtt[3, 2])) &&
(this.up == up) &&
- (cameraAtt.column[3].XYZ == direction)) {
+ (new Vec3(cameraAtt[3, 0], cameraAtt[3, 1], cameraAtt[3, 2]) == direction)) {
return;
}
if (direction.GetLength() == 0)
@@ -126,10 +126,10 @@ namespace Nitload.Mathematics.Numeric.Graphics
Vec3 right = Linear.Orthogonalize(Vec3.Cross(up, direction));
- cameraAtt.column[0].XYZ = right;
- cameraAtt.column[1].XYZ = up;
- cameraAtt.column[2].XYZ = direction;
- cameraAtt.column[3].XYZ = position;
+ cameraAtt[0] = new Vec4(right, 0);
+ cameraAtt[1] = new Vec4(up, 0);
+ cameraAtt[2] = new Vec4(direction, 0);
+ cameraAtt[3] = new Vec4(position, 1);
InternalPitch();
InternalRow();
@@ -152,13 +152,13 @@ namespace Nitload.Mathematics.Numeric.Graphics
width = MathF.Abs(GeometryScaler.Tan(fovRadius / 2) * near * 2),
height = (width * size.Y) / size.X;
- _projection.column[0].X = (2 * near) / width;
- _projection.column[1].Y = (2 * near) / height;
- _projection.column[2].X = (2 * offsetX) / width;
- _projection.column[2].Y = (2 * offsetY) / height;
- _projection.column[2].Z = (far + near) / (near - far);
- _projection.column[2].W = -1;
- _projection.column[3].Z = (2 * near * far) / (near - far);
+ _projection[0].X = (2 * near) / width;
+ _projection[1].Y = (2 * near) / height;
+ _projection[2].X = (2 * offsetX) / width;
+ _projection[2].Y = (2 * offsetY) / height;
+ _projection[2].Z = (far + near) / (near - far);
+ _projection[2].W = -1;
+ _projection[3].Z = (2 * near * far) / (near - far);
}
public void Test(
diff --git a/src/Nitload.Graphics/OpenGL/Buffer/EBO.cs b/src/Nitload.Graphics/OpenGL/Buffer/EBO.cs
deleted file mode 100644
index e5cec88b..00000000
--- a/src/Nitload.Graphics/OpenGL/Buffer/EBO.cs
+++ /dev/null
@@ -1,21 +0,0 @@
-// -----------------------------------------------------------------------------
-// file="EBO"
-// Project: StgSharp
-// Copyright (c) Nitload.
-// SPDX-License-Identifier: MIT
-// -----------------------------------------------------------------------------
-
-namespace Nitload.Graphics.OpenGL
-{
- /// A set of OpenGL element-buffer handles.
- public sealed record class ElementBuffer(
- GlHandle[] Handles
- )
- {
-
- public int Count => Handles.Length;
-
- public GlHandle this[int index] => Handles[index];
-
- }
-}
diff --git a/src/Nitload.Graphics/OpenGL/Buffer/FBO.cs b/src/Nitload.Graphics/OpenGL/Buffer/FBO.cs
deleted file mode 100644
index 82355654..00000000
--- a/src/Nitload.Graphics/OpenGL/Buffer/FBO.cs
+++ /dev/null
@@ -1,21 +0,0 @@
-// -----------------------------------------------------------------------------
-// file="FBO"
-// Project: StgSharp
-// Copyright (c) Nitload.
-// SPDX-License-Identifier: MIT
-// -----------------------------------------------------------------------------
-
-namespace Nitload.Graphics.OpenGL
-{
- /// A set of OpenGL framebuffer handles.
- public sealed record class FrameBuffer(
- GlHandle[] Handles
- )
- {
-
- public int Count => Handles.Length;
-
- public GlHandle this[int index] => Handles[index];
-
- }
-}
diff --git a/src/Nitload.Graphics/OpenGL/Buffer/RBO.cs b/src/Nitload.Graphics/OpenGL/Buffer/RBO.cs
deleted file mode 100644
index 82712b88..00000000
--- a/src/Nitload.Graphics/OpenGL/Buffer/RBO.cs
+++ /dev/null
@@ -1,21 +0,0 @@
-// -----------------------------------------------------------------------------
-// file="RBO"
-// Project: StgSharp
-// Copyright (c) Nitload.
-// SPDX-License-Identifier: MIT
-// -----------------------------------------------------------------------------
-
-namespace Nitload.Graphics.OpenGL
-{
- /// A set of OpenGL renderbuffer handles.
- public sealed record class RenderBuffer(
- GlHandle[] Handles
- )
- {
-
- public int Count => Handles.Length;
-
- public GlHandle this[int index] => Handles[index];
-
- }
-}
diff --git a/src/Nitload.Graphics/OpenGL/Buffer/TextureGL.cs b/src/Nitload.Graphics/OpenGL/Buffer/TextureGL.cs
deleted file mode 100644
index 75f62bf2..00000000
--- a/src/Nitload.Graphics/OpenGL/Buffer/TextureGL.cs
+++ /dev/null
@@ -1,54 +0,0 @@
-// -----------------------------------------------------------------------------
-// file="TextureGL"
-// Project: StgSharp
-// Copyright (c) Nitload.
-// SPDX-License-Identifier: MIT
-// -----------------------------------------------------------------------------
-
-namespace Nitload.Graphics.OpenGL
-{
- /// A set of OpenGL texture handles.
- public sealed record class TextureGL(
- GlHandle[] Handles
- )
- {
-
- public int Count => Handles.Length;
-
- public GlHandle this[int index] => Handles[index];
-
- }
-
- public readonly record struct TextureProperty(
- TextureFilter FilterOnMagnify,
- TextureFilter FilterOnMinify,
- TextureWrap WrapOnHorizontal,
- TextureWrap WrapOnVertical
- );
-
- #pragma warning disable CA1008
- public enum TextureFilter
- #pragma warning restore CA1008
- {
-
- Nearest = glConst.NEAREST,
- Linear = glConst.LINEAR,
- NearestLinearMipmap = glConst.NEAREST_MIPMAP_LINEAR,
- LinearLinearMipmap = glConst.LINEAR_MIPMAP_LINEAR,
- NearestNearestMipmap = glConst.NEAREST_MIPMAP_NEAREST,
- LinearNearestMipmap = glConst.LINEAR_MIPMAP_NEAREST,
-
- }
-
- #pragma warning disable CA1008
- public enum TextureWrap
- #pragma warning restore CA1008
- {
-
- Repeat = glConst.REPEAT,
- MirroredRepeat = glConst.MIRRORED_REPEAT,
- ClampToEdge = glConst.CLAMP_TO_EDGE,
- ClampToBorder = glConst.CLAMP_TO_BORDER,
-
- }
-}
diff --git a/src/Nitload.Graphics/OpenGL/Buffer/VAO.cs b/src/Nitload.Graphics/OpenGL/Buffer/VAO.cs
deleted file mode 100644
index 297d9a04..00000000
--- a/src/Nitload.Graphics/OpenGL/Buffer/VAO.cs
+++ /dev/null
@@ -1,21 +0,0 @@
-// -----------------------------------------------------------------------------
-// file="VAO"
-// Project: StgSharp
-// Copyright (c) Nitload.
-// SPDX-License-Identifier: MIT
-// -----------------------------------------------------------------------------
-
-namespace Nitload.Graphics.OpenGL
-{
- /// A set of OpenGL vertex-array handles.
- public sealed record class VertexArray(
- GlHandle[] Handles
- )
- {
-
- public int Count => Handles.Length;
-
- public GlHandle this[int index] => Handles[index];
-
- }
-}
diff --git a/src/Nitload.Graphics/OpenGL/Buffer/VBO.cs b/src/Nitload.Graphics/OpenGL/Buffer/VBO.cs
deleted file mode 100644
index 26117906..00000000
--- a/src/Nitload.Graphics/OpenGL/Buffer/VBO.cs
+++ /dev/null
@@ -1,21 +0,0 @@
-// -----------------------------------------------------------------------------
-// file="VBO"
-// Project: StgSharp
-// Copyright (c) Nitload.
-// SPDX-License-Identifier: MIT
-// -----------------------------------------------------------------------------
-
-namespace Nitload.Graphics.OpenGL
-{
- /// A set of OpenGL vertex-buffer handles.
- public sealed record class VertexBuffer(
- GlHandle[] Handles
- )
- {
-
- public int Count => Handles.Length;
-
- public GlHandle this[int index] => Handles[index];
-
- }
-}
diff --git a/src/Nitload.Graphics/OpenGL/Generated/OpenGLFunction.Base.g.cs b/src/Nitload.Graphics/OpenGL/Generated/OpenGLFunction.Base.g.cs
index 292d8f00..6889c48b 100644
--- a/src/Nitload.Graphics/OpenGL/Generated/OpenGLFunction.Base.g.cs
+++ b/src/Nitload.Graphics/OpenGL/Generated/OpenGLFunction.Base.g.cs
@@ -1783,9 +1783,12 @@ namespace Nitload.Graphics.OpenGL
/// See docs.gl.
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- public void Uniform(int location, bool transpose, GraphicsMatrix value)
+ public void Uniform(int location, bool transpose, GMatrix44 value)
{
- __context->glUniformMatrix4fv(location, 1, transpose ? (byte)1 : (byte)0, (float*)&value);
+ fixed (GMatrix44* __value = &value)
+ {
+ __context->glUniformMatrix4fv(location, 1, transpose ? (byte)1 : (byte)0, (float*)__value);
+ }
}
///
@@ -1794,9 +1797,9 @@ namespace Nitload.Graphics.OpenGL
/// See docs.gl.
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- public void Uniform(int location, bool transpose, ReadOnlySpan values)
+ public void Uniform(int location, bool transpose, ReadOnlySpan> values)
{
- fixed (GraphicsMatrix* __value = values)
+ fixed (GMatrix44* __value = values)
{
__context->glUniformMatrix4fv(location, values.Length, transpose ? (byte)1 : (byte)0, (float*)__value);
}
diff --git a/src/Nitload.Graphics/OpenGL/Generated/OpenGLFunction.v4_1.g.cs b/src/Nitload.Graphics/OpenGL/Generated/OpenGLFunction.v4_1.g.cs
index d823b332..512d07be 100644
--- a/src/Nitload.Graphics/OpenGL/Generated/OpenGLFunction.v4_1.g.cs
+++ b/src/Nitload.Graphics/OpenGL/Generated/OpenGLFunction.v4_1.g.cs
@@ -631,9 +631,12 @@ namespace Nitload.Graphics.OpenGL
/// See docs.gl.
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- public void ProgramUniform(GlHandle program, int location, bool transpose, GraphicsMatrix value)
+ public void ProgramUniform(GlHandle program, int location, bool transpose, GMatrix44 value)
{
- __context->glProgramUniformMatrix4fv(program.Value, location, 1, transpose ? (byte)1 : (byte)0, (float*)&value);
+ fixed (GMatrix44* __value = &value)
+ {
+ __context->glProgramUniformMatrix4fv(program.Value, location, 1, transpose ? (byte)1 : (byte)0, (float*)__value);
+ }
}
///
@@ -642,9 +645,9 @@ namespace Nitload.Graphics.OpenGL
/// See docs.gl.
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- public void ProgramUniform(GlHandle program, int location, bool transpose, ReadOnlySpan values)
+ public void ProgramUniform(GlHandle program, int location, bool transpose, ReadOnlySpan> values)
{
- fixed (GraphicsMatrix* __value = values)
+ fixed (GMatrix44* __value = values)
{
__context->glProgramUniformMatrix4fv(program.Value, location, values.Length, transpose ? (byte)1 : (byte)0, (float*)__value);
}
diff --git a/src/Nitload.Mathematics/Geometries/Coordination/CoordinationBase.cs b/src/Nitload.Mathematics/Geometries/Coordination/CoordinationBase.cs
index 8d8c25d5..fe89098f 100644
--- a/src/Nitload.Mathematics/Geometries/Coordination/CoordinationBase.cs
+++ b/src/Nitload.Mathematics/Geometries/Coordination/CoordinationBase.cs
@@ -16,41 +16,61 @@ namespace Nitload.Geometries
private bool globalCoordAvailable;
private CoordinationBase localCoordination;
- private GraphicsMatrix coordMat;
+ private GMatrix44 coordMat;
public CoordinationBase(
CoordinationBase localCoordination
)
{
this.localCoordination = localCoordination;
- coordMat = new GraphicsMatrix();
+ coordMat = new GMatrix44();
}
public virtual Point LocalOrigin
{
- get => new Point(coordMat.column[3].XYZ);
- internal set => coordMat.column[3].XYZ = value.Coord;
+ get => new Point(new Vec3(coordMat[3, 0], coordMat[3, 1], coordMat[3, 2]));
+ internal set
+ {
+ coordMat[3, 0] = value.Coord.X;
+ coordMat[3, 1] = value.Coord.Y;
+ coordMat[3, 2] = value.Coord.Z;
+ }
}
public virtual Vec3 LocalX
{
- get => coordMat.column[3].XYZ;
- internal set => coordMat.column[3].XYZ = value;
+ get => new Vec3(coordMat[3, 0], coordMat[3, 1], coordMat[3, 2]);
+ internal set
+ {
+ coordMat[3, 0] = value.X;
+ coordMat[3, 1] = value.Y;
+ coordMat[3, 2] = value.Z;
+ }
}
public virtual Vec3 LocalY
{
- get => coordMat.column[3].XYZ;
- internal set => coordMat.column[3].XYZ = value;
+ get => new Vec3(coordMat[3, 0], coordMat[3, 1], coordMat[3, 2]);
+ internal set
+ {
+ coordMat[3, 0] = value.X;
+ coordMat[3, 1] = value.Y;
+ coordMat[3, 2] = value.Z;
+ }
}
public virtual Vec3 LocalZ
{
- get => coordMat.column[3].XYZ;
- set => coordMat.column[3].XYZ = value;
+ get => new Vec3(coordMat[3, 0], coordMat[3, 1], coordMat[3, 2]);
+ set
+ {
+ coordMat[3, 0] = value.X;
+ coordMat[3, 1] = value.Y;
+ coordMat[3, 2] = value.Z;
+ }
}
- protected ref GraphicsMatrix CoordMat => ref coordMat;
+ protected ref GMatrix44 CoordMat => ref coordMat;
}
}
diff --git a/src/Nitload.Mathematics/Geometries/Coordination/PlainCoordinate.cs b/src/Nitload.Mathematics/Geometries/Coordination/PlainCoordinate.cs
index d3bd8002..5262d425 100644
--- a/src/Nitload.Mathematics/Geometries/Coordination/PlainCoordinate.cs
+++ b/src/Nitload.Mathematics/Geometries/Coordination/PlainCoordinate.cs
@@ -26,11 +26,11 @@ namespace Nitload.Geometries
)
: base(local)
{
- CoordMat = GraphicsMatrix.Unit;
+ CoordMat = GMatrix44.Unit;
- CoordMat.column[0] = new Vec4(xAxis, 0);
- CoordMat.column[1] = new Vec4(yAxis, 0);
- CoordMat.column[2] = new Vec4(Linear.Normalize(Vec3.Cross(xAxis, yAxis)), 0);
+ CoordMat[0] = new Vec4(xAxis, 0);
+ CoordMat[1] = new Vec4(yAxis, 0);
+ CoordMat[2] = new Vec4(Linear.Normalize(Vec3.Cross(xAxis, yAxis)), 0);
LocalOrigin = origin;
}
diff --git a/src/Nitload.Mathematics/Geometries/IInstancing.cs b/src/Nitload.Mathematics/Geometries/IInstancing.cs
index 1fa00c3d..8d50e54d 100644
--- a/src/Nitload.Mathematics/Geometries/IInstancing.cs
+++ b/src/Nitload.Mathematics/Geometries/IInstancing.cs
@@ -41,13 +41,11 @@ namespace Nitload.Geometries
public Vec3 CenterPositionGlobal { get; internal set; }
- public Vec3 Coord//
- { get => GlobalBuffer.CoordAndRotationList[BufferId].XYZ;//
- set
- {
- Vec4 v = new(value, 0);
- GlobalBuffer.CoordAndRotationList[BufferId] = v;
- } }
+ public Vec3 Coord { get => GlobalBuffer.CoordAndRotationList[BufferId].XYZ; set
+ {
+ Vec4 v = new(value, 0);
+ GlobalBuffer.CoordAndRotationList[BufferId] = v;
+ } }
public void Move()
{
diff --git a/src/Nitload.Mathematics/Numeric/Graphics/GMatrix44_T.cs b/src/Nitload.Mathematics/Numeric/Graphics/GMatrix44_T.cs
new file mode 100644
index 00000000..4824e1e2
--- /dev/null
+++ b/src/Nitload.Mathematics/Numeric/Graphics/GMatrix44_T.cs
@@ -0,0 +1,190 @@
+// -----------------------------------------------------------------------------
+// file="GMatrix44_T"
+// Project: StgSharp
+// Copyright (c) Nitload.
+// SPDX-License-Identifier: MIT
+// -----------------------------------------------------------------------------
+
+using Nitload.Mathematics;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Numerics;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+using System.Runtime.Intrinsics;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Nitload.Mathematics.Numeric.Graphics
+{
+ [StructLayout(LayoutKind.Sequential, Pack = 16)]
+ public struct GMatrix44 where T : unmanaged, INumber
+ {
+
+ private Buffer _buffer;
+
+ public Vec4 this[
+ int x
+ ]
+ {
+ readonly get => _buffer[x];
+ set => _buffer[x] = value;
+ }
+
+ public T this[
+ int x,
+ int y
+ ]
+ {
+ readonly get => _buffer[x][y];
+ set => _buffer[x][y] = value;
+ }
+
+ public static GMatrix44 Unit
+ {
+ get
+ {
+ GMatrix44 result = new();
+ result[0] = new Vec4(T.One, T.Zero, T.Zero, T.Zero);
+ result[1] = new Vec4(T.Zero, T.One, T.Zero, T.Zero);
+ result[2] = new Vec4(T.Zero, T.Zero, T.One, T.Zero);
+ result[3] = new Vec4(T.Zero, T.Zero, T.Zero, T.One);
+ return result;
+ }
+ }
+
+ public GMatrix44 Transpose
+ {
+ get
+ {
+ GMatrix44 transpose = new();
+
+ // Native SK transpose is intentionally deferred until the module
+ // startup path can guarantee that GlobalContext has been initialized.
+
+ if (Unsafe.SizeOf() == 4)
+ {
+ Vector128 row0Source = _buffer[0].AsVector128Unsafe();
+ Vector128 row1Source = _buffer[1].AsVector128Unsafe();
+ Vector128 row2Source = _buffer[2].AsVector128Unsafe();
+ Vector128 row3Source = _buffer[3].AsVector128Unsafe();
+ Vector128 row0 = Unsafe.As, Vector128>(ref row0Source);
+ Vector128 row1 = Unsafe.As, Vector128>(ref row1Source);
+ Vector128 row2 = Unsafe.As, Vector128>(ref row2Source);
+ Vector128 row3 = Unsafe.As, Vector128>(ref row3Source);
+
+ Vector128 pairMask = Vector128.Create(-1, 0, -1, 0);
+ Vector128 firstPairMask = Vector128.Create(-1, -1, 0, 0);
+ Vector128 lowShuffle = Vector128.Create(0, 0, 1, 1);
+ Vector128 highShuffle = Vector128.Create(2, 2, 3, 3);
+ Vector128 secondLaneShuffle = Vector128.Create(2, 3, 2, 3);
+
+ Vector128 pair01Low = Vector128.ConditionalSelect(
+ pairMask,
+ Vector128.Shuffle(row0, lowShuffle),
+ Vector128.Shuffle(row1, lowShuffle));
+ Vector128 pair23Low = Vector128.ConditionalSelect(
+ pairMask,
+ Vector128.Shuffle(row2, lowShuffle),
+ Vector128.Shuffle(row3, lowShuffle));
+ Vector128 pair01High = Vector128.ConditionalSelect(
+ pairMask,
+ Vector128.Shuffle(row0, highShuffle),
+ Vector128.Shuffle(row1, highShuffle));
+ Vector128 pair23High = Vector128.ConditionalSelect(
+ pairMask,
+ Vector128.Shuffle(row2, highShuffle),
+ Vector128.Shuffle(row3, highShuffle));
+
+ Vector128 result0 = Vector128.ConditionalSelect(
+ firstPairMask, pair01Low, pair23Low);
+ Vector128 result1 = Vector128.ConditionalSelect(
+ firstPairMask,
+ Vector128.Shuffle(pair01Low, secondLaneShuffle),
+ Vector128.Shuffle(pair23Low, secondLaneShuffle));
+ Vector128 result2 = Vector128.ConditionalSelect(
+ firstPairMask, pair01High, pair23High);
+ Vector128 result3 = Vector128.ConditionalSelect(
+ firstPairMask,
+ Vector128.Shuffle(pair01High, secondLaneShuffle),
+ Vector128.Shuffle(pair23High, secondLaneShuffle));
+ transpose._buffer[0] = Vec4.FromVector128Unsafe(
+ Unsafe.As, Vector128>(ref result0));
+ transpose._buffer[1] = Vec4.FromVector128Unsafe(
+ Unsafe.As, Vector128>(ref result1));
+ transpose._buffer[2] = Vec4.FromVector128Unsafe(
+ Unsafe.As, Vector128>(ref result2));
+ transpose._buffer[3] = Vec4.FromVector128Unsafe(
+ Unsafe.As, Vector128>(ref result3));
+ return transpose;
+ }
+
+ if (Unsafe.SizeOf() == 8)
+ {
+ Vector256 row0Source = _buffer[0].AsVector256Unsafe();
+ Vector256 row1Source = _buffer[1].AsVector256Unsafe();
+ Vector256 row2Source = _buffer[2].AsVector256Unsafe();
+ Vector256 row3Source = _buffer[3].AsVector256Unsafe();
+ Vector256 row0 = Unsafe.As, Vector256>(ref row0Source);
+ Vector256 row1 = Unsafe.As, Vector256>(ref row1Source);
+ Vector256 row2 = Unsafe.As, Vector256>(ref row2Source);
+ Vector256 row3 = Unsafe.As, Vector256>(ref row3Source);
+
+ Vector256 pairMask = Vector256.Create(-1L, 0L, -1L, 0L);
+ Vector256 firstPairMask = Vector256.Create(-1L, -1L, 0L, 0L);
+ Vector256 firstShuffle = Vector256.Create(0L, 0L, 1L, 1L);
+ Vector256 secondShuffle = Vector256.Create(2L, 2L, 3L, 3L);
+ Vector256 pair01First = Vector256.ConditionalSelect(
+ pairMask,
+ Vector256.Shuffle(row0, firstShuffle),
+ Vector256.Shuffle(row1, firstShuffle));
+ Vector256 pair23First = Vector256.ConditionalSelect(
+ pairMask,
+ Vector256.Shuffle(row2, firstShuffle),
+ Vector256.Shuffle(row3, firstShuffle));
+ Vector256 pair01Second = Vector256.ConditionalSelect(
+ pairMask,
+ Vector256.Shuffle(row0, secondShuffle),
+ Vector256.Shuffle(row1, secondShuffle));
+ Vector256 pair23Second = Vector256.ConditionalSelect(
+ pairMask,
+ Vector256.Shuffle(row2, secondShuffle),
+ Vector256.Shuffle(row3, secondShuffle));
+ Vector256 result0 = Vector256.ConditionalSelect(
+ firstPairMask, pair01First, pair23First);
+ Vector256 result1 = Vector256.ConditionalSelect(
+ firstPairMask,
+ Vector256.Shuffle(pair01First, secondShuffle),
+ Vector256.Shuffle(pair23First, secondShuffle));
+ Vector256 result2 = Vector256.ConditionalSelect(
+ firstPairMask, pair01Second, pair23Second);
+ Vector256 result3 = Vector256.ConditionalSelect(
+ firstPairMask,
+ Vector256.Shuffle(pair01Second, secondShuffle),
+ Vector256.Shuffle(pair23Second, secondShuffle));
+ transpose._buffer[0] = Vec4.FromVector256Unsafe(
+ Unsafe.As, Vector256>(ref result0));
+ transpose._buffer[1] = Vec4.FromVector256Unsafe(
+ Unsafe.As, Vector256>(ref result1));
+ transpose._buffer[2] = Vec4.FromVector256Unsafe(
+ Unsafe.As, Vector256>(ref result2));
+ transpose._buffer[3] = Vec4.FromVector256Unsafe(
+ Unsafe.As, Vector256>(ref result3));
+ return transpose;
+ }
+
+ throw GraphicVector.ThrowUnsupportedTypeException();
+ }
+ }
+
+ [InlineArray(4)]
+ private struct Buffer
+ {
+
+ public Vec4 _column;
+
+ }
+
+ }
+}
diff --git a/src/Nitload.Mathematics/Numeric/Graphics/GraphicsMatrix.cs b/src/Nitload.Mathematics/Numeric/Graphics/GraphicsMatrix.cs
deleted file mode 100644
index 2054a690..00000000
--- a/src/Nitload.Mathematics/Numeric/Graphics/GraphicsMatrix.cs
+++ /dev/null
@@ -1,261 +0,0 @@
-// -----------------------------------------------------------------------------
-// file="GraphicsMatrix"
-// Project: StgSharp
-// Copyright (c) Nitload.
-// SPDX-License-Identifier: MIT
-// -----------------------------------------------------------------------------
-
-using Nitload.Mathematics;
-using Nitload.Mathematics.Internal;
-using Nitload.Mathematics.Numeric;
-using System;
-using System.Collections;
-using System.Collections.Generic;
-using System.Numerics;
-using System.Runtime.CompilerServices;
-using System.Runtime.InteropServices;
-using System.Runtime.Intrinsics.X86;
-
-namespace Nitload.Mathematics.Numeric.Graphics
-{
- [StructLayout(LayoutKind.Explicit, Size = 16 * sizeof(float), Pack = 16)]
- public unsafe struct GraphicsMatrix : IEquatable
- {
-
- [FieldOffset(0 * sizeof(float))] internal Column column;
-
- [FieldOffset(0)] internal ColumnSet4 mat;
- [FieldOffset(0)] internal MatrixKernel kernel;
-
- internal GraphicsMatrix(
- ColumnSet4 mat
- )
- {
- Unsafe.SkipInit(out this);
- this.mat = mat;
- }
-
- internal GraphicsMatrix(
- Vector4 vec0,
- Vector4 vec1,
- Vector4 vec2,
- Vector4 vec3
- )
- {
- Unsafe.SkipInit(out this);
- mat.colum0 = vec0;
- mat.colum1 = vec1;
- mat.colum2 = vec2;
- mat.colum3 = vec3;
- }
-
- public GraphicsMatrix()
- {
- Unsafe.SkipInit(out this);
- }
-
- public unsafe GraphicsMatrix(
- float a00,
- float a01,
- float a02,
- float a03,
- float a10,
- float a11,
- float a12,
- float a13,
- float a20,
- float a21,
- float a22,
- float a23,
- float a30,
- float a31,
- float a32,
- float a33
- )
- {
- Unsafe.SkipInit(out this);
- mat.colum0 = new Vector4(a00, a10, a20, a30);
- mat.colum1 = new Vector4(a01, a11, a21, a31);
- mat.colum2 = new Vector4(a02, a12, a22, a32);
- mat.colum3 = new Vector4(a03, a13, a23, a33);
- }
-
- public unsafe float this[
- int rowNum,
- int columNum
- ]
- {
- get
- {
-#if DEBUG
- if (rowNum is > 3 or < 0)
- {
- throw new ArgumentOutOfRangeException(nameof(columNum));
- }
- if (columNum is > 3 or < 0) {
- throw new ArgumentOutOfRangeException(nameof(columNum));
- }
-#endif
- return kernel[rowNum + (4 * columNum)];
- }
- set
- {
-#if DEBUG
- if (rowNum is > 3 or < 0)
- {
- throw new ArgumentOutOfRangeException(nameof(columNum));
- }
- if (columNum is > 3 or < 0) {
- throw new ArgumentOutOfRangeException(nameof(columNum));
- }
-#endif
- kernel[rowNum + (4 * columNum)] = value;
- }
- }
-
- public GraphicsMatrix Transpose
- {
- get
- {
- GraphicsMatrix transpose = new();
- InternalTranspose(ref transpose);
- return transpose;
- }
- }
-
- public static GraphicsMatrix Unit => new GraphicsMatrix(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0,
- 0, 0, 0, 1);
-
- public static GraphicsMatrix WNegativeUnit => new GraphicsMatrix(1, 0, 0, 0, 0, 1, 0, 0, 0,
- 0, 1, 0, 0, 0, 0, -1);
-
- public static GraphicsMatrix XNegativeUnit => new GraphicsMatrix(-1, 0, 0, 0, 0, 1, 0, 0, 0,
- 0, 1, 0, 0, 0, 0, 1);
-
- public static GraphicsMatrix YNegativeUnit => new GraphicsMatrix(1, 0, 0, 0, 0, -1, 0, 0, 0,
- 0, 1, 0, 0, 0, 0, 1);
-
- public static GraphicsMatrix ZNegativeUnit => new GraphicsMatrix(1, 0, 0, 0, 0, 1, 0, 0, 0,
- 0, -1, 0, 0, 0, 0, 1);
-
- public ReadOnlySpan AsSpan()
- {
- return MemoryMarshal.CreateReadOnlySpan(ref Unsafe.As(ref mat), 16);
- }
-
- public override bool Equals(
- object? obj
- )
- {
- return (obj is GraphicsMatrix x) && Equals(x);
- }
-
- public bool Equals(
- GraphicsMatrix other
- )
- {
- return mat.Equals(other.mat);
- }
-
- public override int GetHashCode()
- {
- return HashCode.Combine(mat.colum0.GetHashCode(), mat.colum1.GetHashCode(),
- mat.colum2.GetHashCode(), mat.colum3.GetHashCode());
- }
-
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- internal unsafe void InternalTranspose(
- ref GraphicsMatrix transpose
- )
- {
- const int f32 = 0;
- fixed (MatrixKernel* source = &this.kernel, target = &transpose.kernel) {
- NumericalModule.GlobalContext.mat_sk[f32].Call(MatrixIntrinsicHandle.Transpose,
- null, (MatrixKernel*)source,
- (MatrixKernel*)target, 0);
- }
- }
-
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static unsafe GraphicsMatrix operator -(
- GraphicsMatrix left,
- GraphicsMatrix right
- )
- {
- GraphicsMatrix ret = new();
- int f32 = MatrixElementType.F32.IntrinsicNode;
- NumericalModule.GlobalContext.mat_sk[f32].Call(MatrixIntrinsicHandle.Sub,
- (MatrixKernel*)&left.kernel,
- (MatrixKernel*)&right.kernel,
- (MatrixKernel*)&ret.kernel, 1);
- return ret;
- }
-
- public static bool operator !=(
- GraphicsMatrix left,
- GraphicsMatrix right
- )
- {
- return !(left == right);
- }
-
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static unsafe GraphicsMatrix operator *(
- GraphicsMatrix mat,
- float value
- )
- {
- int f32 = MatrixElementType.F32.IntrinsicNode;
- return new GraphicsMatrix(mat.mat.colum0 * value, mat.mat.colum1 * value,
- mat.mat.colum2 * value, mat.mat.colum3 * value);
- }
-
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static GraphicsMatrix operator *(
- GraphicsMatrix left,
- GraphicsMatrix right
- )
- {
- GraphicsMatrix ret = new();
- int f32 = MatrixElementType.F32.IntrinsicNode;
- NumericalModule.GlobalContext.mat_sk[f32].Call(MatrixIntrinsicHandle.Fma,
- (MatrixKernel*)&left,
- (MatrixKernel*)&right,
- (MatrixKernel*)&ret, 0);
- return ret;
- }
-
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static unsafe GraphicsMatrix operator +(
- GraphicsMatrix left,
- GraphicsMatrix right
- )
- {
- GraphicsMatrix ret = new();
- int f32 = MatrixElementType.F32.IntrinsicNode;
- NumericalModule.GlobalContext.mat_sk[f32].Call(MatrixIntrinsicHandle.Add,
- (MatrixKernel*)&left,
- (MatrixKernel*)&right,
- (MatrixKernel*)&ret, 1);
- return ret;
- }
-
- public static bool operator ==(
- GraphicsMatrix left,
- GraphicsMatrix right
- )
- {
- return left.Equals(right);
- }
-
- [InlineArray(4)]
- internal struct Column
- {
-
- private Vec4 value;
-
- }
-
- }
-}
-
diff --git a/src/Nitload.Mathematics/Numeric/Graphics/Vec3.cs b/src/Nitload.Mathematics/Numeric/Graphics/Vec3.cs
index 8837fb93..8389f47e 100644
--- a/src/Nitload.Mathematics/Numeric/Graphics/Vec3.cs
+++ b/src/Nitload.Mathematics/Numeric/Graphics/Vec3.cs
@@ -15,13 +15,61 @@ namespace Nitload.Mathematics.Numeric.Graphics
public static class Vec3
{
- public static bool IsParallel(Vec3 left, Vec3 right) where T: unmanaged,INumber
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static Vec3 ConvertToDouble(
+ Vec3 source
+ )
+ {
+ return FromVector256Unsafe(Vector256.ConvertToDouble(source.AsVector256Safe()));
+ }
+
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static Vec3 ConvertToDouble(
+ Vec3 source
+ )
+ {
+ return FromVector256Unsafe(Vector256.ConvertToDouble(source.AsVector256Safe()));
+ }
+
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static Vec3 ConvertToSingle(
+ Vec3 source
+ )
+ {
+ return FromVector128Unsafe(Vector128.ConvertToSingle(source.AsVector128Safe()));
+ }
+
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static Vec3 ConvertToSingle(
+ Vec3 source
+ )
+ {
+ return FromVector128Unsafe(Vector128.ConvertToSingle(source.AsVector128Safe()));
+ }
+
+ public static Vec3 FromSpan(
+ ReadOnlySpan span
+ ) where T : unmanaged, INumber
+ {
+ if (span.Length < 3) {
+ throw new ArgumentException("Span length must be at least 3.", nameof(span));
+ }
+
+ return new(span[0], span[1], span[2]);
+ }
+
+ public static bool IsParallel(
+ Vec3 left,
+ Vec3 right
+ ) where T : unmanaged,INumber
{
return Cross(left, right).Equals(T.Zero);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- internal static Vector128 AsVector128Safe(this Vec3 source) where T: unmanaged, INumber
+ internal static Vector128 AsVector128Safe(
+ this Vec3 source
+ ) where T : unmanaged, INumber
{
Vector128 result = Vector128.Zero;
Unsafe.WriteUnaligned(ref Unsafe.As, byte>(ref result), source);
@@ -29,7 +77,9 @@ namespace Nitload.Mathematics.Numeric.Graphics
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- internal static Vector128 AsVector128Unsafe(this Vec3 source) where T: unmanaged, INumber
+ internal static Vector128 AsVector128Unsafe(
+ this Vec3 source
+ ) where T : unmanaged, INumber
{
Unsafe.SkipInit(out Vector128 result);
Unsafe.WriteUnaligned(ref Unsafe.As, byte>(ref result), source);
@@ -37,7 +87,9 @@ namespace Nitload.Mathematics.Numeric.Graphics
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- internal static Vector256 AsVector256Safe(this Vec3 source) where T: unmanaged, INumber
+ internal static Vector256 AsVector256Safe(
+ this Vec3 source
+ ) where T : unmanaged, INumber
{
Vector256 result = Vector256.Zero;
Unsafe.WriteUnaligned(ref Unsafe.As, byte>(ref result), source);
@@ -45,7 +97,9 @@ namespace Nitload.Mathematics.Numeric.Graphics
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- internal static Vector256 AsVector256Unsafe(this Vec3 source) where T: unmanaged, INumber
+ internal static Vector256 AsVector256Unsafe(
+ this Vec3 source
+ ) where T : unmanaged, INumber
{
Unsafe.SkipInit(out Vector256 result);
Unsafe.WriteUnaligned(ref Unsafe.As, byte>(ref result), source);
@@ -53,45 +107,28 @@ namespace Nitload.Mathematics.Numeric.Graphics
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- internal static Vec3 FromVector128Unsafe(Vector128 source) where T: unmanaged, INumber
+ internal static Vec3 FromVector128Unsafe(
+ Vector128 source
+ ) where T : unmanaged, INumber
{
return Unsafe.As, Vec3>(ref source);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- internal static Vec3 FromVector256Unsafe(Vector256 source) where T: unmanaged, INumber
+ internal static Vec3 FromVector256Unsafe(
+ Vector256 source
+ ) where T : unmanaged, INumber
{
return Unsafe.As, Vec3>(ref source);
}
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vec3 ConvertToSingle(Vec3 source)
- {
- return FromVector128Unsafe(Vector128.ConvertToSingle(source.AsVector128Safe()));
- }
-
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vec3 ConvertToSingle(Vec3 source)
- {
- return FromVector128Unsafe(Vector128.ConvertToSingle(source.AsVector128Safe()));
- }
-
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vec3 ConvertToDouble(Vec3 source)
- {
- return FromVector256Unsafe(Vector256.ConvertToDouble(source.AsVector256Safe()));
- }
-
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vec3 ConvertToDouble(Vec3 source)
- {
- return FromVector256Unsafe(Vector256.ConvertToDouble(source.AsVector256Safe()));
- }
-
#region cross
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vec3 Cross(Vec3 left, Vec3 right) where T: unmanaged, INumber
+ public static Vec3 Cross(
+ Vec3 left,
+ Vec3 right
+ ) where T : unmanaged, INumber
{
if (typeof(T) == typeof(float))
{
@@ -140,7 +177,10 @@ namespace Nitload.Mathematics.Numeric.Graphics
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- private static Vec3 CrossDouble(Vec3 left, Vec3