add a pre-kP evaluation function in c

This commit is contained in:
Youmu Liu
2026-04-24 15:46:58 +08:00
parent 2f6617447c
commit 41350b0487
13 changed files with 169 additions and 154 deletions
+24 -24
View File
@@ -36,26 +36,26 @@ namespace StgSharp.Mathematics.Graphics
{
private bool _isLookAtAvailable;
internal Matrix44 _lookAt;
internal Matrix44 _projection;
internal Matrix44 cameraAtt;
internal Matrix44 rotation;
internal Matrix44 rotationAtt;
internal GraphicsMatrix _lookAt;
internal GraphicsMatrix _projection;
internal GraphicsMatrix cameraAtt;
internal GraphicsMatrix rotation;
internal GraphicsMatrix rotationAtt;
internal Radius _pitch;
internal Radius _row;
internal Radius _yaw;
internal Uniform<Matrix44> convertedUniform;
internal Uniform<GraphicsMatrix> convertedUniform;
internal Vec3 _target, up;
public Camera()
{
cameraAtt = Matrix44.Unit;
cameraAtt = GraphicsMatrix.Unit;
_target = Vec3.Zero;
up = Vec3.Zero;
_isLookAtAvailable = false;
_lookAt = Matrix44.Unit;
rotationAtt = Matrix44.Unit;
_projection = new Matrix44();
_lookAt = GraphicsMatrix.Unit;
rotationAtt = GraphicsMatrix.Unit;
_projection = new GraphicsMatrix();
}
public Camera(
@@ -65,24 +65,24 @@ namespace StgSharp.Mathematics.Graphics
)
: this()
{
rotationAtt = Matrix44.Unit;
cameraAtt = new Matrix44();
rotationAtt = GraphicsMatrix.Unit;
cameraAtt = new GraphicsMatrix();
SetViewDirection(position, target, up);
_pitch = Radius.Zero;
_row = Radius.Zero;
_yaw = Radius.Zero;
}
public Matrix44 Projection => _projection;
public GraphicsMatrix Projection => _projection;
public Matrix44 View
public GraphicsMatrix View
{
get
{
if (_isLookAtAvailable) {
return _lookAt;
}
Matrix44 move = Matrix44.Unit;
GraphicsMatrix move = GraphicsMatrix.Unit;
move.column[3].vec -= cameraAtt.column[3].vec;
_lookAt = rotationAtt.Transpose * move;
_isLookAtAvailable = true;
@@ -90,7 +90,7 @@ namespace StgSharp.Mathematics.Graphics
}
}
public Matrix44 CameraMatrix()
public GraphicsMatrix CameraMatrix()
{
return Projection * View;
}
@@ -118,11 +118,11 @@ namespace StgSharp.Mathematics.Graphics
params string[] uniformName
)
{
if (uniformName is null || uniformName.Length != 1) {
if ((uniformName is null) || (uniformName.Length != 1)) {
throw new ArgumentException(
"Camera needs exactly one uniform.", nameof(uniformName));
}
convertedUniform = source.GetUniform<Matrix44>(uniformName[0]);
convertedUniform = source.GetUniform<GraphicsMatrix>(uniformName[0]);
}
public void MoveNear(
@@ -208,15 +208,15 @@ namespace StgSharp.Mathematics.Graphics
offsetX = offset.X,
offsetY = offset.Y,
width = MathF.Abs(GeometryScaler.Tan(fovRadius / 2) * near * 2),
height = width * size.Y / size.X;
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[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.column[3].Z = (2 * near * far) / (near - far);
}
public void Test(
@@ -145,8 +145,8 @@ namespace StgSharp.Graphics.OpenGL
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetUniformValue(
[NotNull] Uniform<Matrix44> uniform,
Matrix44 mat
[NotNull] Uniform<GraphicsMatrix> uniform,
GraphicsMatrix mat
)
{
Context.glUniformMatrix4fv(uniform.id.SignedValue, 1, (byte)0, (float*)&mat);
+1 -1
View File
@@ -137,7 +137,7 @@ namespace StgSharp.Graphics.OpenGL
CustomizeDeinit();
}
protected sealed override Uniform<Matrix44> NativeCameraUniform(
protected sealed override Uniform<GraphicsMatrix> NativeCameraUniform(
ShaderProgram source,
string name
)
+15 -49
View File
@@ -50,24 +50,13 @@ namespace StgSharp.Graphics
private protected ViewPort primeArgs;
internal TimeSpanProvider _timeProvider;
public (int width, int height) Size
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => (Width, Height);
}
public (int width, int height) Size => (Width, Height);
public abstract bool IsContextSharable { get; }
public int Height
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => primeArgs.Height;
}
public int Height => primeArgs.Height;
public int Width
{
get => primeArgs.Width;
}
public int Width => primeArgs.Width;
public IntPtr Monitor
{
@@ -75,10 +64,7 @@ namespace StgSharp.Graphics
internal set => primeArgs.Monitor = value;
}
public string Name
{
get => primeArgs.Name;
}
public string Name => primeArgs.Name;
public ViewPort BindedViewPortContext
{
@@ -86,11 +72,7 @@ namespace StgSharp.Graphics
internal set => primeArgs = value;
}
internal IntPtr CanvasHandle
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => primeArgs.ViewPortHandle;
}
internal IntPtr CanvasHandle => primeArgs.ViewPortHandle;
internal IntPtr ContextHandle
{
@@ -104,29 +86,13 @@ namespace StgSharp.Graphics
internal set => nativeCamera = value;
}
protected float FramePassed
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _timeProvider.CurrentSpan;
}
protected float FramePassed => _timeProvider.CurrentSpan;
protected int PassedFrames
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _timeProvider.CurrentSpan;
}
protected int PassedFrames => _timeProvider.CurrentSpan;
protected float TimePassed
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => (float)_timeProvider.CurrentSecond;
}
protected float TimePassed => (float)_timeProvider.CurrentSecond;
protected TimeSpanProvider TimeProvider
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _timeProvider;
}
protected TimeSpanProvider TimeProvider => _timeProvider;
/// <summary>
/// Set the Size limit of current form
@@ -188,9 +154,9 @@ namespace StgSharp.Graphics
);
/// <summary>
/// Get <see cref="Uniform{T}" />(TView is <see cref="Matrix44" />) uniform from a shader.
/// This uniform will be used as projection matrix of this camera. Additionally, for
/// multi- shader rendering, each shader requires a uniform, so do not ignore return
/// Get <see cref="Uniform{T}" />(TView is <see cref="GraphicsMatrix" />) uniform from a
/// shader. This uniform will be used as projection matrix of this camera. Additionally,
/// for multi- shader rendering, each shader requires a uniform, so do not ignore return
/// value of this method.
/// </summary>
/// <param _label="source">
@@ -200,10 +166,10 @@ namespace StgSharp.Graphics
/// ContextName of projection camera named in shader
/// </param>
/// <returns>
/// <see cref="Uniform{T}" />(TView is <see cref="Matrix44" />) representing projection
/// matrix in shader program.
/// <see cref="Uniform{T}" />(TView is <see cref="GraphicsMatrix" />) representing
/// projection matrix in shader program.
/// </returns>
protected abstract Uniform<Matrix44> NativeCameraUniform(
protected abstract Uniform<GraphicsMatrix> NativeCameraUniform(
ShaderProgram source,
string name
);
@@ -36,12 +36,14 @@ namespace StgSharp.Geometries
private bool globalCoordAvailable;
private CoordinationBase localCoordination;
private Matrix44 coordMat;
private GraphicsMatrix coordMat;
public CoordinationBase(CoordinationBase localCoordination)
public CoordinationBase(
CoordinationBase localCoordination
)
{
this.localCoordination = localCoordination;
coordMat = new Matrix44();
coordMat = new GraphicsMatrix();
}
public virtual Point LocalOrigin
@@ -68,11 +70,7 @@ namespace StgSharp.Geometries
set => coordMat.column[3].XYZ = value;
}
protected ref Matrix44 CoordMat
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => ref coordMat;
}
protected ref GraphicsMatrix CoordMat => ref coordMat;
}
}
@@ -38,10 +38,15 @@ namespace StgSharp.Geometries
public class PlainCoordinate : CoordinationBase
{
public PlainCoordinate(CoordinationBase local, Point origin, Vec3 xAxis, Vec3 yAxis)
public PlainCoordinate(
CoordinationBase local,
Point origin,
Vec3 xAxis,
Vec3 yAxis
)
: base(local)
{
CoordMat = Matrix44.Unit;
CoordMat = GraphicsMatrix.Unit;
CoordMat.column[0] = new Vec4(xAxis, 0);
CoordMat.column[1] = new Vec4(yAxis, 0);
@@ -50,8 +55,10 @@ namespace StgSharp.Geometries
LocalOrigin = origin;
}
public static PlainCoordinate StandardPlainCoordination { get; } = new(null, new Point(0, 0, 0),
new Vec3(1, 0, 0), new Vec3(0, 1, 0));
public static PlainCoordinate StandardPlainCoordination { get; } = new(null,
new Point(0, 0, 0),
new Vec3(1, 0, 0),
new Vec3(0, 1, 0));
}
}
@@ -1,6 +1,6 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Matrix44"
// file="GraphicsMatrix"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
@@ -38,8 +38,8 @@ using System.Runtime.Intrinsics.X86;
namespace StgSharp.Mathematics.Graphics
{
[StructLayout(LayoutKind.Explicit, Size = (16 * sizeof(float)), Pack = 16)]
public unsafe struct Matrix44 : IEquatable<Matrix44>
[StructLayout(LayoutKind.Explicit, Size = 16 * sizeof(float), Pack = 16)]
public unsafe struct GraphicsMatrix : IEquatable<GraphicsMatrix>
{
[FieldOffset(0 * sizeof(float))] internal Column column;
@@ -47,7 +47,7 @@ namespace StgSharp.Mathematics.Graphics
[FieldOffset(0)] internal ColumnSet4 mat;
[FieldOffset(0)] internal MatrixKernel<float> kernel;
internal Matrix44(
internal GraphicsMatrix(
ColumnSet4 mat
)
{
@@ -55,7 +55,7 @@ namespace StgSharp.Mathematics.Graphics
this.mat = mat;
}
internal Matrix44(
internal GraphicsMatrix(
Vector4 vec0,
Vector4 vec1,
Vector4 vec2,
@@ -69,12 +69,12 @@ namespace StgSharp.Mathematics.Graphics
mat.colum3 = vec3;
}
public Matrix44()
public GraphicsMatrix()
{
Unsafe.SkipInit(out this);
}
public unsafe Matrix44(
public unsafe GraphicsMatrix(
float a00,
float a01,
float a02,
@@ -116,7 +116,7 @@ namespace StgSharp.Mathematics.Graphics
throw new ArgumentOutOfRangeException(nameof(columNum));
}
#endif
return kernel[rowNum + 4 * columNum];
return kernel[rowNum + (4 * columNum)];
}
set
{
@@ -129,33 +129,34 @@ namespace StgSharp.Mathematics.Graphics
throw new ArgumentOutOfRangeException(nameof(columNum));
}
#endif
kernel[rowNum + 4 * columNum] = value;
kernel[rowNum + (4 * columNum)] = value;
}
}
public Matrix44 Transpose
public GraphicsMatrix Transpose
{
get
{
Matrix44 transpose = new();
GraphicsMatrix transpose = new();
InternalTranspose(ref transpose);
return transpose;
}
}
public static Matrix44 Unit => new Matrix44(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
public static GraphicsMatrix Unit => new GraphicsMatrix(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 GraphicsMatrix WNegativeUnit => new GraphicsMatrix(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 GraphicsMatrix XNegativeUnit => new GraphicsMatrix(-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 GraphicsMatrix YNegativeUnit => new GraphicsMatrix(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 GraphicsMatrix ZNegativeUnit => new GraphicsMatrix(1, 0, 0, 0, 0, 1, 0, 0, 0,
0, -1, 0, 0, 0, 0, 1);
public ReadOnlySpan<float> AsSpan()
{
@@ -166,11 +167,11 @@ namespace StgSharp.Mathematics.Graphics
object? obj
)
{
return obj is Matrix44 x && Equals(x);
return (obj is GraphicsMatrix x) && Equals(x);
}
public bool Equals(
Matrix44 other
GraphicsMatrix other
)
{
return mat.Equals(other.mat);
@@ -184,7 +185,7 @@ namespace StgSharp.Mathematics.Graphics
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal unsafe void InternalTranspose(
ref Matrix44 transpose
ref GraphicsMatrix transpose
)
{
const int f32 = 0;
@@ -196,46 +197,46 @@ namespace StgSharp.Mathematics.Graphics
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe Matrix44 operator -(
Matrix44 left,
Matrix44 right
public static unsafe GraphicsMatrix operator -(
GraphicsMatrix left,
GraphicsMatrix right
)
{
Matrix44 ret = new();
GraphicsMatrix ret = new();
int f32 = MatrixElementType.F32.IntrinsicNode;
NumericalModule.GlobalContext.mat_sk[f32].Call(MatrixIntrinsicHandle.Sub,
(MatrixKernel*)&left,
(MatrixKernel*)&right,
(MatrixKernel*)&ret, 1);
(MatrixKernel*)&left.kernel,
(MatrixKernel*)&right.kernel,
(MatrixKernel*)&ret.kernel, 1);
return ret;
}
public static bool operator !=(
Matrix44 left,
Matrix44 right
GraphicsMatrix left,
GraphicsMatrix right
)
{
return !(left == right);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe Matrix44 operator *(
Matrix44 mat,
public static unsafe GraphicsMatrix operator *(
GraphicsMatrix mat,
float value
)
{
int f32 = MatrixElementType.F32.IntrinsicNode;
return new Matrix44(mat.mat.colum0 * value, mat.mat.colum1 * value,
return new GraphicsMatrix(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 GraphicsMatrix operator *(
GraphicsMatrix left,
GraphicsMatrix right
)
{
Matrix44 ret = new();
GraphicsMatrix ret = new();
int f32 = MatrixElementType.F32.IntrinsicNode;
NumericalModule.GlobalContext.mat_sk[f32].Call(MatrixIntrinsicHandle.Fma,
(MatrixKernel*)&left,
@@ -245,12 +246,12 @@ namespace StgSharp.Mathematics.Graphics
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe Matrix44 operator +(
Matrix44 left,
Matrix44 right
public static unsafe GraphicsMatrix operator +(
GraphicsMatrix left,
GraphicsMatrix right
)
{
Matrix44 ret = new();
GraphicsMatrix ret = new();
int f32 = MatrixElementType.F32.IntrinsicNode;
NumericalModule.GlobalContext.mat_sk[f32].Call(MatrixIntrinsicHandle.Add,
(MatrixKernel*)&left,
@@ -260,8 +261,8 @@ namespace StgSharp.Mathematics.Graphics
}
public static bool operator ==(
Matrix44 left,
Matrix44 right
GraphicsMatrix left,
GraphicsMatrix right
)
{
return left.Equals(right);
+5 -13
View File
@@ -122,17 +122,9 @@ namespace StgSharp.Mathematics.Graphics
}
}
public static Vec4 One
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => new Vec4(1, 1, 1, 1);
}
public static Vec4 One => new Vec4(1, 1, 1, 1);
public static Vec4 Zero
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => new Vec4(0, 0, 0, 0);
}
public static Vec4 Zero => new Vec4(0, 0, 0, 0);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vec4 Add(
@@ -152,7 +144,7 @@ namespace StgSharp.Mathematics.Graphics
object obj
)
{
return obj is Vec4 v && v == this;
return (obj is Vec4 v) && (v == this);
}
public static Vec4 FromSpan(
@@ -212,11 +204,11 @@ namespace StgSharp.Mathematics.Graphics
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vec4 operator *(
Matrix44 mat,
GraphicsMatrix mat,
Vec4 vec
)
{
Matrix44 transpose = mat.Transpose;
GraphicsMatrix 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),
@@ -66,7 +66,7 @@ namespace StgSharp.Mathematics.Numeric
for (int i = 0; i < _numaMeasurements.Length; i++)
{
MatrixParallelThread[] numa = _numaThreads[i];
if ((numa is null) | (numa.Length == 0))
if ((numa is null) || (numa.Length == 0))
{
continue;
}
@@ -30,7 +30,6 @@ using StgSharp.HighPerformance.ProcessorAbstraction;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
namespace StgSharp.Mathematics.Numeric
{
@@ -81,7 +80,7 @@ namespace StgSharp.Mathematics.Numeric
}
contextMap = [];
int count = Environment.ProcessorCount;
SIMDID globalSimd = new SIMDID(0);
SIMDID globalSimd = new(0);
_retiredThreads = new(RESERVED_THREAD_COUNT);
// init thread on all logical cores
@@ -140,6 +140,8 @@
INTERNAL void f32_normalize(sn_vec_f32 * source, sn_vec_f32 * target);
SN_MK_PROC_DECL_STD(float, sse, , k_quality);
SN_MK_PROC_DECL_STD(float, sse, , add);
SN_MK_PROC_DECL_STD(float, avx, , add);
SN_MK_PROC_DECL_STD(float, 512, , add);
@@ -74,7 +74,6 @@
#pragma region mem layout
#define _SN_MATPROC_LAYOUT_BUFFER 1
#define _SN_MATPROC_LAYOUT_TILE 1
#define _SN_MATPROC_LAYOUT_KERNEL 1
+51
View File
@@ -0,0 +1,51 @@
#include "sn_target.h"
#include <pmmintrin.h>
#if SN_IS_ARCH(SN_ARCH_X86_64)
#include "sn_internal.h"
#include "sn_intrinsic.h"
#include "sn_intrinsic.std.h"
#include "sn_intrinsic.context.x86.h"
#include "sn_intrinsic.matkernel.h"
#include <emmintrin.h>
#include <immintrin.h>
SN_MK_PROC_DECL_STD(float, sse, , k_quality)
{
__mk_param_std(TILE, RIGHT_ANS_SCALAR, float);
}
INTERNAL SN_STD_TARGET_ATTR(sse, ) void kp_check_f32(mat_kernel *kernel)
{
typedef MAT_KERNEL(float) SN_MAT;
SN_MAT const *k = (SN_MAT const *)kernel;
float cross[4];
cross[0] = k->m[0][0];
cross[1] = k->m[1][1];
cross[2] = k->m[2][2];
cross[3] = k->m[3][3];
float evaluation[4];
register __m128 cross_vec = _mm_loadu_ps((float *)cross);
register __m128 abs_mask = _mm_castsi128_ps(_mm_set1_epi32(0x7fffffff));
cross_vec = _mm_and_ps(cross_vec, abs_mask);
register __m128 average_vec = _mm_hadd_ps(cross_vec, cross_vec);
average_vec = _mm_hadd_ps(average_vec, average_vec);
register __m128 quarter_vec = _mm_set1_ps(0.25f);
average_vec = _mm_mul_ps(average_vec, quarter_vec);
register __m128 stddev_vec = _mm_sub_ps(cross_vec, average_vec);
stddev_vec = _mm_mul_ps(stddev_vec, stddev_vec);
stddev_vec = _mm_hadd_ps(stddev_vec, stddev_vec);
stddev_vec = _mm_hadd_ps(stddev_vec, stddev_vec);
stddev_vec = _mm_mul_ps(stddev_vec, quarter_vec);
stddev_vec = _mm_sqrt_ps(stddev_vec);
evaluation[0] = _mm_cvtss_f32(average_vec);
evaluation[1] = _mm_cvtss_f32(stddev_vec);
}
#endif