keep working on matrix. key is to remove old matrix types

Signed-off-by: Youmu Liu <105042599+Nitload-NSI@users.noreply.github.com>
This commit is contained in:
Youmu Liu
2025-08-26 22:02:38 +08:00
parent e5e4b1dd36
commit 9f6e3fe13a
15 changed files with 152 additions and 812 deletions
+16 -21
View File
@@ -31,6 +31,7 @@
using StgSharp.Graphics;
using StgSharp.Graphics.OpenGL;
using StgSharp.Graphics.ShaderEdit;
using StgSharp.HighPerformance;
using StgSharp.Mathematics;
using System;
@@ -46,11 +47,11 @@ namespace StgSharp.Graphics
{
private bool _isLookAtAvailable;
internal Matrix44 _lookAt;
internal Matrix44 _projection;
internal Matrix44 cameraAtt;
internal Matrix44 rotation;
internal Matrix44 rotationAtt;
internal MatrixKernel _lookAt;
internal MatrixKernel _projection;
internal MatrixKernel cameraAtt;
internal MatrixKernel rotation;
internal MatrixKernel rotationAtt;
internal Radius _pitch;
internal Radius _row;
internal Radius _yaw;
@@ -59,20 +60,20 @@ namespace StgSharp.Graphics
public Camera()
{
cameraAtt = Matrix44.Unit;
cameraAtt = MatrixKernel.Fp32Unit;
_target = Vec3.Zero;
up = Vec3.Zero;
_isLookAtAvailable = false;
_lookAt = Matrix44.Unit;
rotationAtt = Matrix44.Unit;
_projection = new Matrix44();
_lookAt = MatrixKernel.Fp32Unit;
rotationAtt = MatrixKernel.Fp32Unit;
_projection = new MatrixKernel(true);
}
public Camera(Vec3 position, Vec3 target, Vec3 up)
: this()
{
rotationAtt = Matrix44.Unit;
cameraAtt = new Matrix44();
rotationAtt = MatrixKernel.Fp32Unit;
cameraAtt = new MatrixKernel(true);
SetViewDirection(position, target, up);
_pitch = Radius.Zero;
_row = Radius.Zero;
@@ -88,8 +89,8 @@ namespace StgSharp.Graphics
if (_isLookAtAvailable) {
return _lookAt;
}
Matrix44 move = Matrix44.Unit;
move.colum3.vec -= cameraAtt.colum3.vec;
Matrix44 move = MatrixKernel.Fp32Unit;
move.colum3.vec -= cameraAtt.colum3;
_lookAt = rotationAtt.Transpose * move;
_isLookAtAvailable = true;
return _lookAt;
@@ -155,9 +156,7 @@ namespace StgSharp.Graphics
public void SetViewDirection(Vec3 position, Vec3 target, Vec3 up)
{
Vec3 direction = position - target;
if ((position.reg == cameraAtt.colum3.reg) &&
(this.up == up) &&
(cameraAtt.colum2.reg == direction.reg)) {
if ((position.reg == cameraAtt.colum3.reg) && (this.up == up) && (cameraAtt.colum2.reg == direction.reg)) {
return;
}
if (direction.GetLength() == 0)
@@ -187,11 +186,7 @@ namespace StgSharp.Graphics
InternalYaw();
}
public void SetViewRange(
Radius fovRadius,
Vec2 size,
Vec2 offset,
(float front, float back) dephRange)
public void SetViewRange(Radius fovRadius, Vec2 size, Vec2 offset, (float front, float back) dephRange)
{
float
distance = _target.GetLength(),
+27 -5
View File
@@ -29,10 +29,13 @@
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using StgSharp.HighPerformance;
using StgSharp.HighPerformance.Memory;
using System;
using System.Numerics;
using hlsfAllocator = global::StgSharp.HighPerformance.Memory.HybridLayerSegregatedFitAllocator;
using hlsfHandle = global::StgSharp.HighPerformance.Memory.HybridLayerSegregatedFitAllocationHandle;
namespace StgSharp.Mathematics
{
public abstract unsafe partial class Matrix<T> where T: unmanaged, INumber<T>
@@ -42,6 +45,8 @@ namespace StgSharp.Mathematics
private protected readonly int t_size = sizeof(T);
private protected Matrix() { }
public abstract ref T this[int column, int row]
{
get;
@@ -53,6 +58,14 @@ namespace StgSharp.Mathematics
set;
}
public int ColumnCount { get; init; }
public int RowCount { get; init; }
protected int KernelColumnCount { get; init; }
protected int KernelRowCount { get; init; }
public abstract MatrixKernel* GetMatrixKernel(int column, int row);
[Obsolete("This method does not contain boarder check, so be careful on size or use indexer instead.", false)]
@@ -67,9 +80,9 @@ namespace StgSharp.Mathematics
public GCMatrix(int column, int row)
{
int columnSet = (column + 3) / 4;
int rowSet = (row + 3) / 4;
buffer = new MatrixKernel[columnSet, rowSet];
KernelColumnCount = (column + 3) / 4;
KernelRowCount = (row + 3) / t_count;
buffer = new MatrixKernel[KernelColumnCount, KernelRowCount];
}
public override ref T this[int column, int row]
@@ -77,7 +90,7 @@ namespace StgSharp.Mathematics
get
{
int kernelColumn = column / 4, c = column % 4;
int kernelRow = row / t_count,r = row % t_count;
int kernelRow = row / t_count, r = row % t_count;
ref MatrixKernel kernel = ref buffer[kernelColumn, kernelRow];
return ref kernel.UnsafeIndex<T>(c, r);
}
@@ -104,6 +117,15 @@ namespace StgSharp.Mathematics
internal unsafe class HlsfMatrix<T> : Matrix<T> where T: unmanaged, INumber<T>
{
private hlsfHandle handle;
internal HlsfMatrix(int column, int row, hlsfAllocator allocator)
{
KernelColumnCount = (column + 3) / 4;
KernelRowCount = (row + 3) / t_count;
this.handle = allocator.Alloc((uint)(KernelColumnCount * KernelRowCount * sizeof(MatrixKernel)));
}
public override ref T this[int column, int row] => throw new NotImplementedException();
public override Matrix<T> this[ReadOnlySpan<MatrixIndex> column, ReadOnlySpan<MatrixIndex> row]