Better L4 and array storage design

This commit is contained in:
Youmu Liu
2026-03-04 18:12:58 +08:00
parent 7e970352ab
commit dfa6f4ba92
103 changed files with 853 additions and 6837 deletions
@@ -2,8 +2,8 @@
// -----------------------------------------------------------------------
// file="Matrix44"
// Project: StgSharp
// AuthorGroup: Nitload Space
// Copyright (c) Nitload Space. All rights reserved.
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
@@ -26,7 +26,7 @@
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using StgSharp.Mathematics;
using StgSharp.Mathematics.Intrinsic;
using StgSharp.Mathematics.Internal;
using StgSharp.Mathematics.Numeric;
using System;
using System.Collections;
@@ -34,6 +34,7 @@ using System.Collections.Generic;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics.X86;
namespace StgSharp.Mathematics.Graphics
{
@@ -46,13 +47,20 @@ namespace StgSharp.Mathematics.Graphics
[FieldOffset(0)] internal ColumnSet4 mat;
[FieldOffset(0)] internal MatrixKernel<float> kernel;
internal Matrix44(ColumnSet4 mat)
internal Matrix44(
ColumnSet4 mat
)
{
Unsafe.SkipInit(out this);
this.mat = mat;
}
internal Matrix44(Vector4 vec0, Vector4 vec1, Vector4 vec2, Vector4 vec3)
internal Matrix44(
Vector4 vec0,
Vector4 vec1,
Vector4 vec2,
Vector4 vec3
)
{
Unsafe.SkipInit(out this);
mat.colum0 = vec0;
@@ -82,7 +90,8 @@ namespace StgSharp.Mathematics.Graphics
float a30,
float a31,
float a32,
float a33)
float a33
)
{
Unsafe.SkipInit(out this);
mat.colum0 = new Vector4(a00, a10, a20, a30);
@@ -91,7 +100,10 @@ namespace StgSharp.Mathematics.Graphics
mat.colum3 = new Vector4(a03, a13, a23, a33);
}
public unsafe float this[int rowNum, int columNum]
public unsafe float this[
int rowNum,
int columNum
]
{
get
{
@@ -133,84 +145,124 @@ namespace StgSharp.Mathematics.Graphics
public static Matrix44 Unit => new Matrix44(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
public static Matrix44 WNegativeUnit => new Matrix44(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, -1);
public static Matrix44 WNegativeUnit => new Matrix44(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0,
0, 0, -1);
public static Matrix44 XNegativeUnit => new Matrix44(-1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
public static Matrix44 XNegativeUnit => new Matrix44(-1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0,
0, 0, 1);
public static Matrix44 YNegativeUnit => new Matrix44(1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
public static Matrix44 YNegativeUnit => new Matrix44(1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0,
0, 0, 1);
public static Matrix44 ZNegativeUnit => new Matrix44(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1);
public static Matrix44 ZNegativeUnit => new Matrix44(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, -1, 0, 0,
0, 0, 1);
public ReadOnlySpan<float> AsSpan()
{
return MemoryMarshal.CreateReadOnlySpan(ref Unsafe.As<ColumnSet4, float>(ref mat), 16);
}
public override bool Equals(object? obj)
public override bool Equals(
object? obj
)
{
return obj is Matrix44 x && Equals(x);
}
public bool Equals(Matrix44 other)
public bool Equals(
Matrix44 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());
return HashCode.Combine(mat.colum0.GetHashCode(), mat.colum1.GetHashCode(),
mat.colum2.GetHashCode(), mat.colum3.GetHashCode());
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal unsafe void InternalTranspose(ref Matrix44 transpose)
internal unsafe void InternalTranspose(
ref Matrix44 transpose
)
{
const int f32 = 0;
fixed (MatrixKernel<float>* source = &this.kernel, target = &transpose.kernel) {
MathematicsModule.GlobalContext.mat[f32].buffer_transpose(source, target);
MathematicsModule.GlobalContext.mat_sk[f32].Call(MatrixIntrinsicHandle.Transpose,
null, (MatrixKernel*)source,
(MatrixKernel*)target, 0);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe Matrix44 operator -(Matrix44 left, Matrix44 right)
public static unsafe Matrix44 operator -(
Matrix44 left,
Matrix44 right
)
{
Matrix44 ret = new();
const int f32 = 0;
MathematicsModule.GlobalContext.mat[f32].buffer_sub(&left, &right, &ret, 1);
int f32 = MatrixElementType.F32.IntrinsicNode;
MathematicsModule.GlobalContext.mat_sk[f32].Call(MatrixIntrinsicHandle.Sub,
(MatrixKernel*)&left,
(MatrixKernel*)&right,
(MatrixKernel*)&ret, 1);
return ret;
}
public static bool operator !=(Matrix44 left, Matrix44 right)
public static bool operator !=(
Matrix44 left,
Matrix44 right
)
{
return !(left == right);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe Matrix44 operator *(Matrix44 mat, float value)
public static unsafe Matrix44 operator *(
Matrix44 mat,
float value
)
{
return new Matrix44(mat.mat.colum0 * value, mat.mat.colum1 * value, mat.mat.colum2 * value,
mat.mat.colum3 * value);
int f32 = MatrixElementType.F32.IntrinsicNode;
return new Matrix44(mat.mat.colum0 * value, mat.mat.colum1 * value,
mat.mat.colum2 * value, mat.mat.colum3 * value);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix44 operator *(Matrix44 left, Matrix44 right)
public static Matrix44 operator *(
Matrix44 left,
Matrix44 right
)
{
Matrix44 ret = new();
int f32 = (int)MatrixElementType.F32;
MathematicsModule.GlobalContext.mat[f32].kernel_fma(&left, &right, &ret);
int f32 = MatrixElementType.F32.IntrinsicNode;
MathematicsModule.GlobalContext.mat_sk[f32].Call(MatrixIntrinsicHandle.Fma,
(MatrixKernel*)&left,
(MatrixKernel*)&right,
(MatrixKernel*)&ret, 0);
return ret;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe Matrix44 operator +(Matrix44 left, Matrix44 right)
public static unsafe Matrix44 operator +(
Matrix44 left,
Matrix44 right
)
{
Matrix44 ret = new();
const int f32 = 0;
MathematicsModule.GlobalContext.mat[f32].buffer_add(&left, &right, &ret, 1);
int f32 = MatrixElementType.F32.IntrinsicNode;
MathematicsModule.GlobalContext.mat_sk[f32].Call(MatrixIntrinsicHandle.Add,
(MatrixKernel*)&left,
(MatrixKernel*)&right,
(MatrixKernel*)&ret, 1);
return ret;
}
public static bool operator ==(Matrix44 left, Matrix44 right)
public static bool operator ==(
Matrix44 left,
Matrix44 right
)
{
return left.Equals(right);
}
+60 -19
View File
@@ -2,8 +2,8 @@
// -----------------------------------------------------------------------
// file="Vec4"
// Project: StgSharp
// AuthorGroup: Nitload Space
// Copyright (c) Nitload Space. All rights reserved.
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
@@ -26,7 +26,7 @@
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using StgSharp.Mathematics;
using StgSharp.Mathematics.Intrinsic;
using StgSharp.Mathematics.Internal;
using StgSharp.Mathematics.Numeric;
using System;
using System.Data.Common;
@@ -52,7 +52,9 @@ namespace StgSharp.Mathematics.Graphics
[FieldOffset(4)] public float Y;
[FieldOffset(8)] public float Z;
internal Vec4(Vector4 v)
internal Vec4(
Vector4 v
)
{
Unsafe.SkipInit(out X);
Unsafe.SkipInit(out Y);
@@ -63,7 +65,9 @@ namespace StgSharp.Mathematics.Graphics
vec = v;
}
internal Vec4(M128 v)
internal Vec4(
M128 v
)
{
Unsafe.SkipInit(out X);
Unsafe.SkipInit(out Y);
@@ -74,7 +78,10 @@ namespace StgSharp.Mathematics.Graphics
reg = v;
}
public Vec4(Vec3 v3, float w)
public Vec4(
Vec3 v3,
float w
)
{
Unsafe.SkipInit(out X);
Unsafe.SkipInit(out Y);
@@ -84,7 +91,12 @@ namespace StgSharp.Mathematics.Graphics
W = w;
}
public Vec4(float x, float y, float z, float w)
public Vec4(
float x,
float y,
float z,
float w
)
{
vec = new Vector4(x, y, z, w);
}
@@ -123,7 +135,10 @@ namespace StgSharp.Mathematics.Graphics
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vec4 Add(Vec4 left, Vec4 right)
public static Vec4 Add(
Vec4 left,
Vec4 right
)
{
return left + right;
}
@@ -133,12 +148,16 @@ namespace StgSharp.Mathematics.Graphics
return MemoryMarshal.CreateReadOnlySpan(ref reg.Member<float>(0), 4);
}
public override bool Equals(object obj)
public override bool Equals(
object obj
)
{
return obj is Vec4 v && v == this;
}
public static Vec4 FromSpan(ReadOnlySpan<float> span)
public static Vec4 FromSpan(
ReadOnlySpan<float> span
)
{
if (span.Length < 4) {
throw new ArgumentException("Span length must be at least 4.", nameof(span));
@@ -161,7 +180,10 @@ namespace StgSharp.Mathematics.Graphics
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vec4 Subtract(Vec4 left, Vec4 right)
public static Vec4 Subtract(
Vec4 left,
Vec4 right
)
{
return left - right;
}
@@ -172,36 +194,55 @@ namespace StgSharp.Mathematics.Graphics
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vec4 operator -(Vec4 left, Vec4 right)
public static Vec4 operator -(
Vec4 left,
Vec4 right
)
{
return new Vec4(left.vec - right.vec);
}
public static bool operator !=(Vec4 left, Vec4 right)
public static bool operator !=(
Vec4 left,
Vec4 right
)
{
return !(left == right);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vec4 operator *(Matrix44 mat, Vec4 vec)
public static Vec4 operator *(
Matrix44 mat,
Vec4 vec
)
{
Matrix44 transpose = mat.Transpose;
return new Vec4(Vector4.Dot(transpose.mat.colum0, vec.vec), Vector4.Dot(transpose.mat.colum1, vec.vec),
Vector4.Dot(transpose.mat.colum2, vec.vec), Vector4.Dot(transpose.mat.colum3, vec.vec));
return new Vec4(Vector4.Dot(transpose.mat.colum0, vec.vec),
Vector4.Dot(transpose.mat.colum1, vec.vec),
Vector4.Dot(transpose.mat.colum2, vec.vec),
Vector4.Dot(transpose.mat.colum3, vec.vec));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vec4 operator +(Vec4 left, Vec4 right)
public static Vec4 operator +(
Vec4 left,
Vec4 right
)
{
return new Vec4(left.vec + right.vec);
}
public static bool operator ==(Vec4 left, Vec4 right)
public static bool operator ==(
Vec4 left,
Vec4 right
)
{
return left.vec == right.vec;
}
bool IEquatable<Vec4>.Equals(Vec4 other)
bool IEquatable<Vec4>.Equals(
Vec4 other
)
{
return vec == other.vec;
}