diff --git a/StgSharp.Common/Geometries/PlainGeometry/Cordinate.cs b/StgSharp.Common/Geometries/PlainGeometry/Cordinate.cs index b0268ff3..cd9e5035 100644 --- a/StgSharp.Common/Geometries/PlainGeometry/Cordinate.cs +++ b/StgSharp.Common/Geometries/PlainGeometry/Cordinate.cs @@ -1,33 +1,33 @@ //----------------------------------------------------------------------- -//----------------------------------------------------------------------- -// file="Cordinate.cs" -// Project: StgSharp -// AuthorGroup: Nitload Space -// Copyright (c) Nitload Space. All rights reserved. +// ----------------------------------------------------------------------- +// file="Cordinate.cs" +// Project: StgSharp +// AuthorGroup: Nitload Space +// Copyright (c) Nitload Space. 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 in the Software without restriction, -// including without limitation the rights to use, copy, modify, merge, -// publish, distribute, sublicense, and/or sell copies of the Software, -// and to permit persons to whom the Software is furnished to do so, -// subject to the following conditions: +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the “Software”), to deal in the Software without restriction, +// including without limitation the rights to use, copy, modify, merge, +// publish, distribute, sublicense, and/or sell copies of the Software, +// and to permit persons to whom the Software is furnished to do so, +// subject to the following conditions: // -// The above copyright notice and -// this permission notice shall be included in all copies -// or substantial portions of the Software. +// The above copyright notice and +// this permission notice shall be included in all copies +// or substantial portions of the Software. // -// THE SOFTWARE IS PROVIDED “AS IS”, -// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, -// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -// ARISING FROM, OUT OF OR IN CONNECTION WITH -// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// THE SOFTWARE IS PROVIDED “AS IS”, +// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH +// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // -//----------------------------------------------------------------------- -//----------------------------------------------------------------------- +// ----------------------------------------------------------------------- +// ----------------------------------------------------------------------- using StgSharp.Mathematics; using System; @@ -40,10 +40,10 @@ using System.Threading.Tasks; namespace StgSharp.Geometries { - [StructLayout( LayoutKind.Sequential )] + [StructLayout(LayoutKind.Sequential)] public struct PartialPlainCoordinate { - + /* internal Matrix32 axis; /// @@ -55,9 +55,7 @@ namespace StgSharp.Geometries { this.origin = origin; axis = new Matrix32(); - #if NET5_0_OR_GREATER Unsafe.SkipInit( out axis.mat ); - #endif axis.colum0 = horiziontial; axis.colum1 = vertical; } @@ -77,6 +75,7 @@ namespace StgSharp.Geometries { return origin + axis.VerticalVecMultiply( raletiveLocation ); } + */ } } diff --git a/StgSharp.Common/Graphics/OpenGL/ShaderEdit/Uniform.cs b/StgSharp.Common/Graphics/OpenGL/ShaderEdit/Uniform.cs index c7239f7f..ea3269b8 100644 --- a/StgSharp.Common/Graphics/OpenGL/ShaderEdit/Uniform.cs +++ b/StgSharp.Common/Graphics/OpenGL/ShaderEdit/Uniform.cs @@ -1,33 +1,33 @@ //----------------------------------------------------------------------- -//----------------------------------------------------------------------- -// file="Uniform.cs" -// Project: StepVisualizer -// AuthorGroup: Nitload Space -// Copyright (c) Nitload Space. All rights reserved. +// ----------------------------------------------------------------------- +// file="Uniform.cs" +// Project: StgSharp +// AuthorGroup: Nitload Space +// Copyright (c) Nitload Space. 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 in the Software without restriction, -// including without limitation the rights to use, copy, modify, merge, -// publish, distribute, sublicense, and/or sell copies of the Software, -// and to permit persons to whom the Software is furnished to do so, -// subject to the following conditions: +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the “Software”), to deal in the Software without restriction, +// including without limitation the rights to use, copy, modify, merge, +// publish, distribute, sublicense, and/or sell copies of the Software, +// and to permit persons to whom the Software is furnished to do so, +// subject to the following conditions: // -// The above copyright notice and -// this permission notice shall be included in all copies -// or substantial portions of the Software. +// The above copyright notice and +// this permission notice shall be included in all copies +// or substantial portions of the Software. // -// THE SOFTWARE IS PROVIDED “AS IS”, -// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, -// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -// ARISING FROM, OUT OF OR IN CONNECTION WITH -// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// THE SOFTWARE IS PROVIDED “AS IS”, +// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH +// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // -//----------------------------------------------------------------------- -//----------------------------------------------------------------------- +// ----------------------------------------------------------------------- +// ----------------------------------------------------------------------- using StgSharp.Graphics; using StgSharp.Mathematics; @@ -47,24 +47,21 @@ namespace StgSharp.Graphics.OpenGL internal GlHandle id; - public GlHandle Handle - { - get => id; - } + public GlHandle Handle => id; } - public sealed class Uniform : Uniform where T:unmanaged + public sealed class Uniform : Uniform where T: unmanaged { internal Uniform() { } - internal unsafe Uniform( GlHandle id ) + internal unsafe Uniform(GlHandle id) { this.id = id; } - internal static Uniform FromHandle( GlHandle handle ) + internal static Uniform FromHandle(GlHandle handle) { return new Uniform { @@ -74,17 +71,17 @@ namespace StgSharp.Graphics.OpenGL } - public sealed class Uniform : Uniform where T:unmanaged where U: struct + public sealed class Uniform : Uniform where T: unmanaged where U: struct { internal Uniform() { } - internal unsafe Uniform( GlHandle id ) + internal unsafe Uniform(GlHandle id) { this.id = id; } - internal static Uniform FromHandle( GlHandle handle ) + internal static Uniform FromHandle(GlHandle handle) { return new Uniform { @@ -94,17 +91,17 @@ namespace StgSharp.Graphics.OpenGL } - public sealed class Uniform : Uniform where T:unmanaged where U: struct where V: struct + public sealed class Uniform : Uniform where T: unmanaged where U: struct where V: struct { internal Uniform() { } - internal unsafe Uniform( GlHandle id ) + internal unsafe Uniform(GlHandle id) { this.id = id; } - internal static Uniform FromHandle( GlHandle handle ) + internal static Uniform FromHandle(GlHandle handle) { return new Uniform { @@ -114,19 +111,17 @@ namespace StgSharp.Graphics.OpenGL } - public sealed class Uniform : Uniform where T:unmanaged where U: struct - where V: struct - where W: struct + public sealed class Uniform : Uniform where T: unmanaged where U: struct where V: struct where W: struct { internal Uniform() { } - internal unsafe Uniform( GlHandle id ) + internal unsafe Uniform(GlHandle id) { this.id = id; } - internal static Uniform FromHandle( GlHandle handle ) + internal static Uniform FromHandle(GlHandle handle) { return new Uniform { @@ -139,66 +134,60 @@ namespace StgSharp.Graphics.OpenGL public unsafe partial class OpenGLFunction { - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public void SetUniformValue( [NotNull] Uniform uniform, Matrix44 mat ) + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void SetUniformValue([NotNull] Uniform uniform, Matrix44 mat) { Matrix44 temp = mat; - Context.glUniformMatrix4fv( uniform.id.SignedValue, 1, false, ( float* )&temp ); + Context.glUniformMatrix4fv(uniform.id.SignedValue, 1, false, (float*)&temp); } - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public void SetUniformValue( - [NotNull]Uniform uniform, - Vec4 vec ) + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void SetUniformValue([NotNull]Uniform uniform, Vec4 vec) { Vec4 temp = vec; - Context.glUniform1fv( uniform.id.SignedValue, 4, &temp.reg ); + Context.glUniform1fv(uniform.id.SignedValue, 4, &temp.reg); } - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public void SetUniformValue( [NotNull]Uniform uniform, Vec4 vec ) + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void SetUniformValue([NotNull]Uniform uniform, Vec4 vec) { Vec4 temp = vec; - Context.glUniform1fv( uniform.id.SignedValue, 4, &temp.reg ); + Context.glUniform1fv(uniform.id.SignedValue, 4, &temp.reg); } - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public void SetUniformValue( [NotNull]Uniform uniform, float v0 ) + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void SetUniformValue([NotNull]Uniform uniform, float v0) { - Context.glUniform1f( uniform.id.SignedValue, v0 ); + Context.glUniform1f(uniform.id.SignedValue, v0); } - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public void SetUniformValue( [NotNull]Uniform uniform, int i0 ) + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void SetUniformValue([NotNull]Uniform uniform, int i0) { - Context.glUniform1i( uniform.id.SignedValue, i0 ); + Context.glUniform1i(uniform.id.SignedValue, i0); } - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public void SetUniformValue( [NotNull] Uniform uniform, float v0, float v1 ) + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void SetUniformValue([NotNull] Uniform uniform, float v0, float v1) { - Context.glUniform2f( uniform.id.SignedValue, v0, v1 ); + Context.glUniform2f(uniform.id.SignedValue, v0, v1); } - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public void SetUniformValue( - [NotNull] Uniform uniform, - float v0, - float v1, - float v2 ) + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void SetUniformValue([NotNull] Uniform uniform, float v0, float v1, float v2) { - Context.glUniform3f( uniform.id.SignedValue, v0, v1, v2 ); + Context.glUniform3f(uniform.id.SignedValue, v0, v1, v2); } - [MethodImpl( MethodImplOptions.AggressiveInlining )] + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void SetUniformValue( [NotNull] Uniform uniform, float v0, float v1, float v2, - float v3 ) + float v3) { - Context.glUniform4f( uniform.id.SignedValue, v0, v1, v2, v3 ); + Context.glUniform4f(uniform.id.SignedValue, v0, v1, v2, v3); } } diff --git a/StgSharp.Common/Internal/Intrinsic/IntrinsicContext.cs b/StgSharp.Common/Internal/Intrinsic/IntrinsicContext.cs index dfb30aac..61b8c30e 100644 --- a/StgSharp.Common/Internal/Intrinsic/IntrinsicContext.cs +++ b/StgSharp.Common/Internal/Intrinsic/IntrinsicContext.cs @@ -28,16 +28,10 @@ // // ----------------------------------------------------------------------- // ----------------------------------------------------------------------- -using StgSharp; using StgSharp.HighPerformance; -using System; -using System.Collections.Generic; -using System.Linq; using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -using System.Text; -using System.Threading.Tasks; namespace StgSharp.Internal.Intrinsic { @@ -45,32 +39,20 @@ namespace StgSharp.Internal.Intrinsic internal unsafe struct IntrinsicContext { - public delegate* unmanaged[Cdecl] add_mat_2; - public delegate* unmanaged[Cdecl] add_mat_3; - public delegate* unmanaged[Cdecl] add_mat_4; public delegate* unmanaged[Cdecl] city_hash_simplify; - public delegate* unmanaged[Cdecl] det_mat_3; - public delegate* unmanaged[Cdecl] det_mat_4; - public delegate* unmanaged[Cdecl] dot_31; - public delegate* unmanaged[Cdecl] dot_32; - public delegate* unmanaged[Cdecl] dot_41; - public delegate* unmanaged[Cdecl] dot_42; - public delegate* unmanaged[Cdecl] dot_43; - public delegate* unmanaged[Cdecl] factorial_simd; - public delegate* unmanaged[Cdecl] fma_44; + public delegate* unmanaged[Cdecl] factorial_simd; public delegate* unmanaged[Cdecl] index_pair; - public delegate* unmanaged[Cdecl] normalize_v3; - public delegate* unmanaged[Cdecl] sub_mat_2; - public delegate* unmanaged[Cdecl] sub_mat_3; - public delegate* unmanaged[Cdecl] sub_mat_4; - public delegate* unmanaged[Cdecl] transpose23; - public delegate* unmanaged[Cdecl] transpose24; - public delegate* unmanaged[Cdecl] transpose32; - public delegate* unmanaged[Cdecl] transpose33; - public delegate* unmanaged[Cdecl] transpose34; - public delegate* unmanaged[Cdecl] transpose42; - public delegate* unmanaged[Cdecl] transpose43; - public delegate* unmanaged[Cdecl] transpose44; + public delegate* unmanaged[Cdecl] kernel_add; + public delegate* unmanaged[Cdecl] kernel_fma; + public delegate* unmanaged[Cdecl] kernel_scalar_mul; + public delegate* unmanaged[Cdecl] kernel_sub; + public delegate* unmanaged[Cdecl] kernel_transpose; + public delegate* unmanaged[Cdecl] normalize_3; + + public IntrinsicContext() + { + Unsafe.SkipInit(out this); + } } } diff --git a/StgSharp.Common/Internal/Intrinsic/intrinsicContextProvider.cs b/StgSharp.Common/Internal/Intrinsic/intrinsicContextProvider.cs index 516f03df..e73f08eb 100644 --- a/StgSharp.Common/Internal/Intrinsic/intrinsicContextProvider.cs +++ b/StgSharp.Common/Internal/Intrinsic/intrinsicContextProvider.cs @@ -40,7 +40,7 @@ namespace StgSharp.Internal internal static unsafe partial class InternalIO { - private static readonly IntrinsicContext _intrinsicContext = new IntrinsicContext(); + private static readonly IntrinsicContext _intrinsicContext = new(); internal static IntrinsicContext Intrinsic { diff --git a/StgSharp.Common/Mathematics/Graphic/Camera.cs b/StgSharp.Common/Mathematics/Graphic/Camera.cs index 5f86e3ed..a856f786 100644 --- a/StgSharp.Common/Mathematics/Graphic/Camera.cs +++ b/StgSharp.Common/Mathematics/Graphic/Camera.cs @@ -47,11 +47,11 @@ namespace StgSharp.Graphics { private bool _isLookAtAvailable; - internal MatrixKernel _lookAt; - internal MatrixKernel _projection; - internal MatrixKernel cameraAtt; - internal MatrixKernel rotation; - internal MatrixKernel rotationAtt; + internal Matrix44 _lookAt; + internal Matrix44 _projection; + internal Matrix44 cameraAtt; + internal Matrix44 rotation; + internal Matrix44 rotationAtt; internal Radius _pitch; internal Radius _row; internal Radius _yaw; @@ -60,20 +60,20 @@ namespace StgSharp.Graphics public Camera() { - cameraAtt = MatrixKernel.Fp32Unit; + cameraAtt = Matrix44.Unit; _target = Vec3.Zero; up = Vec3.Zero; _isLookAtAvailable = false; - _lookAt = MatrixKernel.Fp32Unit; - rotationAtt = MatrixKernel.Fp32Unit; - _projection = new MatrixKernel(true); + _lookAt = Matrix44.Unit; + rotationAtt = Matrix44.Unit; + _projection = new Matrix44(); } public Camera(Vec3 position, Vec3 target, Vec3 up) : this() { - rotationAtt = MatrixKernel.Fp32Unit; - cameraAtt = new MatrixKernel(true); + rotationAtt = Matrix44.Unit; + cameraAtt = new Matrix44(); SetViewDirection(position, target, up); _pitch = Radius.Zero; _row = Radius.Zero; @@ -89,8 +89,8 @@ namespace StgSharp.Graphics if (_isLookAtAvailable) { return _lookAt; } - Matrix44 move = MatrixKernel.Fp32Unit; - move.colum3.vec -= cameraAtt.colum3; + Matrix44 move = Matrix44.Unit; + move.colum3.vec -= cameraAtt.colum3.vec; _lookAt = rotationAtt.Transpose * move; _isLookAtAvailable = true; return _lookAt; @@ -255,6 +255,7 @@ namespace StgSharp.Graphics internal void InternalPitch() { + /* _isLookAtAvailable = false; Matrix32 partialCoord = new Matrix32(cameraAtt.colum1.vec, cameraAtt.colum2.vec); @@ -267,10 +268,12 @@ namespace StgSharp.Graphics rotationAtt.colum1.reg = partialCoord.colum0.reg; rotationAtt.colum2.reg = partialCoord.colum1.reg; rotationAtt.isTransposed = false; + /**/ } internal void InternalRow() { + /* _isLookAtAvailable = false; Matrix32 partialCoord = new Matrix32(cameraAtt.colum0.vec, cameraAtt.colum1.vec); @@ -283,10 +286,12 @@ namespace StgSharp.Graphics rotationAtt.colum0.reg = partialCoord.colum0.reg; rotationAtt.colum1.reg = partialCoord.colum1.reg; rotationAtt.isTransposed = false; + */ } internal void InternalYaw() { + /* _isLookAtAvailable = false; Matrix32 partialCoord = new Matrix32(cameraAtt.colum0.vec, cameraAtt.colum2.vec); @@ -298,6 +303,7 @@ namespace StgSharp.Graphics rotationAtt.colum0.reg = partialCoord.colum0.reg; rotationAtt.colum2.reg = partialCoord.colum1.reg; rotationAtt.isTransposed = false; + */ } #endregion diff --git a/StgSharp.Common/Mathematics/Linear/Matrix.cs b/StgSharp.Common/Mathematics/Linear/Matrix.cs index d6824bd6..ad421f3d 100644 --- a/StgSharp.Common/Mathematics/Linear/Matrix.cs +++ b/StgSharp.Common/Mathematics/Linear/Matrix.cs @@ -29,7 +29,6 @@ // ----------------------------------------------------------------------- // ----------------------------------------------------------------------- using StgSharp.HighPerformance; -using StgSharp.HighPerformance.Memory; using System; using System.Numerics; @@ -66,17 +65,28 @@ namespace StgSharp.Mathematics protected int KernelRowCount { get; init; } - public abstract MatrixKernel* GetMatrixKernel(int column, int row); + public abstract ref MatrixKernel GetMatrixKernel(int column, int row); [Obsolete("This method does not contain boarder check, so be careful on size or use indexer instead.", false)] - public abstract T* UnsafeIndex(int column, int row); + public abstract ref T UnsafeIndex(int column, int row); + + private protected void AssertIndex(int column, int row) + { + if (column < 0 || column >= ColumnCount) { + throw new ArgumentOutOfRangeException(nameof(column)); + } + + if (row < 0 || row >= RowCount) { + throw new ArgumentOutOfRangeException(nameof(row)); + } + } } internal unsafe class GCMatrix : Matrix where T: unmanaged, INumber { - private MatrixKernel[,] buffer; + private readonly MatrixKernel[,] buffer; public GCMatrix(int column, int row) { @@ -89,6 +99,7 @@ namespace StgSharp.Mathematics { get { + AssertIndex(column, row); int kernelColumn = column / 4, c = column % 4; int kernelRow = row / t_count, r = row % t_count; ref MatrixKernel kernel = ref buffer[kernelColumn, kernelRow]; @@ -102,14 +113,20 @@ namespace StgSharp.Mathematics set => throw new NotImplementedException(); } - public override unsafe MatrixKernel* GetMatrixKernel(int column, int row) + public override unsafe ref MatrixKernel GetMatrixKernel(int column, int row) { - throw new NotImplementedException(); + AssertIndex(column, row); + int kernelColumn = column / 4; + int kernelRow = row / t_count; + return ref buffer[kernelColumn, kernelRow]; } - public override unsafe T* UnsafeIndex(int column, int row) + public override ref T UnsafeIndex(int column, int row) { - throw new NotImplementedException(); + int kernelColumn = column / 4, c = column % 4; + int kernelRow = row / t_count, r = row % t_count; + ref MatrixKernel kernel = ref buffer[kernelColumn, kernelRow]; + return ref kernel.UnsafeIndex(c, r); } } @@ -117,16 +134,28 @@ namespace StgSharp.Mathematics internal unsafe class HlsfMatrix : Matrix where T: unmanaged, INumber { - private hlsfHandle handle; + private readonly MatrixKernel* k_buffer; + private readonly 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))); + k_buffer = (MatrixKernel*)handle.Pointer; } - public override ref T this[int column, int row] => throw new NotImplementedException(); + public override ref T this[int column, int row] + { + get + { + AssertIndex(column, row); + int kernelColumn = column / 4, c = column % 4; + int kernelRow = row / t_count, r = row % t_count; + ref MatrixKernel kernel = ref k_buffer[(kernelColumn * kernelColumn) + kernelRow]; + return ref kernel.UnsafeIndex(c, r); + } + } public override Matrix this[ReadOnlySpan column, ReadOnlySpan row] { @@ -134,14 +163,20 @@ namespace StgSharp.Mathematics set => throw new NotImplementedException(); } - public override unsafe MatrixKernel* GetMatrixKernel(int column, int row) + public override unsafe ref MatrixKernel GetMatrixKernel(int column, int row) { - throw new NotImplementedException(); + AssertIndex(column, row); + int kernelColumn = column / 4, c = column % 4; + int kernelRow = row / t_count, r = row % t_count; + return ref k_buffer[(kernelColumn * kernelColumn) + kernelRow]; } - public override unsafe T* UnsafeIndex(int column, int row) + public override ref T UnsafeIndex(int column, int row) { - throw new NotImplementedException(); + int kernelColumn = column / 4, c = column % 4; + int kernelRow = row / t_count, r = row % t_count; + ref MatrixKernel kernel = ref k_buffer[(kernelColumn * kernelColumn) + kernelRow]; + return ref kernel.UnsafeIndex(c, r); } } diff --git a/StgSharp.Common/Mathematics/Linear/Matrix22.cs b/StgSharp.Common/Mathematics/Linear/Matrix22.cs deleted file mode 100644 index 896db406..00000000 --- a/StgSharp.Common/Mathematics/Linear/Matrix22.cs +++ /dev/null @@ -1,177 +0,0 @@ -//----------------------------------------------------------------------- -// ----------------------------------------------------------------------- -// file="Matrix22.cs" -// Project: StgSharp -// AuthorGroup: Nitload Space -// Copyright (c) Nitload Space. 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 in the Software without restriction, -// including without limitation the rights to use, copy, modify, merge, -// publish, distribute, sublicense, and/or sell copies of the Software, -// and to permit persons to whom the Software is furnished to do so, -// subject to the following conditions: -// -// The above copyright notice and -// this permission notice shall be included in all copies -// or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED “AS IS”, -// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, -// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -// ARISING FROM, OUT OF OR IN CONNECTION WITH -// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -// -// ----------------------------------------------------------------------- -// ----------------------------------------------------------------------- -using StgSharp.Internal.Intrinsic; - -using System.Numerics; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -namespace StgSharp.Mathematics -{ - [StructLayout(LayoutKind.Explicit, Size = (2 * 2 * 4 * sizeof(float)) + sizeof(bool), Pack = 16)] - public struct Matrix22 : IMatrix - { - - [FieldOffset(2 * 2 * 4 * sizeof(float))] - internal bool isTransposed = false; - - [FieldOffset(0)] - internal ColumnSet2 _mat; - [FieldOffset(2 * 4 * sizeof(float))] - internal ColumnSet2 _transpose; - - internal Matrix22(ColumnSet2 mat) - { - this._mat = mat; - } - - internal Matrix22(Vector4 c0, Vector4 c1) - { - Unsafe.SkipInit(out this); - isTransposed = false; - _mat.colum0 = c0; - _mat.colum1 = c1; - } - - public Matrix22() - { - Unsafe.SkipInit(out this); - isTransposed = false; - } - - public Matrix22(float a00, float a01, float a10, float a11) - { - Unsafe.SkipInit(out this); - isTransposed = false; - _mat.colum0 = new Vector4(a00, a10, 0, 0); - _mat.colum1 = new Vector4(a01, a11, 0, 0); - } - - public Matrix22 Transpose - { - get - { - InternalTranspose(); - return new Matrix22(this._transpose); - } - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public float Det() - { - return (_mat.m00 * _mat.m11) - (_mat.m10 * _mat.m01); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - internal void InternalTranspose() - { - if (!isTransposed) - { - _transpose = _mat; - float cache = _transpose.m10; - _transpose.m10 = _transpose.m01; - _transpose.m01 = cache; - isTransposed = true; - } - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe Matrix22 operator -(Matrix22 left, Matrix22 right) - { - Matrix22 ret = new Matrix22(); - InternalIO.Intrinsic.sub_mat_2(&left._mat, &right._mat, &ret._mat); - return ret; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Matrix22 operator *(Matrix22 mat, float value) - { - return new Matrix22(mat._mat.colum0 * value, mat._mat.colum1 * value); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Matrix22 operator *(Matrix22 left, Matrix22 right) - { - left.InternalTranspose(); - return new Matrix22( - Vector4.Dot(left._transpose.colum0, right._mat.colum0), - Vector4.Dot(left._transpose.colum0, right._mat.colum1), - - Vector4.Dot(left._transpose.colum1, right._mat.colum0), - Vector4.Dot(left._transpose.colum1, right._mat.colum1)); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Matrix23 operator *(Matrix22 left, Matrix23 right) - { - left.InternalTranspose(); - return new Matrix23( - Vector4.Dot(left._transpose.colum0, right._mat.colum0), - Vector4.Dot(left._transpose.colum0, right._mat.colum1), - Vector4.Dot(left._transpose.colum0, right._mat.colum2), - - Vector4.Dot(left._transpose.colum1, right._mat.colum0), - Vector4.Dot(left._transpose.colum1, right._mat.colum1), - Vector4.Dot(left._transpose.colum1, right._mat.colum2)); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Matrix24 operator *(Matrix22 left, Matrix24 right) - { - left.InternalTranspose(); - return new Matrix24( - Vector4.Dot(left._transpose.colum0, right.mat.colum0), - Vector4.Dot(left._transpose.colum0, right.mat.colum1), - Vector4.Dot(left._transpose.colum0, right.mat.colum2), - Vector4.Dot(left._transpose.colum0, right.mat.colum3), - - Vector4.Dot(left._transpose.colum1, right.mat.colum0), - Vector4.Dot(left._transpose.colum1, right.mat.colum1), - Vector4.Dot(left._transpose.colum1, right.mat.colum2), - Vector4.Dot(left._transpose.colum1, right.mat.colum3)); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Matrix22 operator /(Matrix22 mat, float value) - { - return new Matrix22(mat._mat.colum0 / value, mat._mat.colum1 / value); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe Matrix22 operator +(Matrix22 left, Matrix22 right) - { - Matrix22 ret = new Matrix22(); - InternalIO.Intrinsic.add_mat_2(&left._mat, &right._mat, &ret._mat); - return ret; - } - - } -} diff --git a/StgSharp.Common/Mathematics/Linear/Matrix23.cs b/StgSharp.Common/Mathematics/Linear/Matrix23.cs deleted file mode 100644 index 63585ece..00000000 --- a/StgSharp.Common/Mathematics/Linear/Matrix23.cs +++ /dev/null @@ -1,195 +0,0 @@ -//----------------------------------------------------------------------- -//----------------------------------------------------------------------- -// file="Matrix23.cs" -// Project: StgSharp -// AuthorGroup: Nitload Space -// Copyright (c) Nitload Space. 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 in the Software without restriction, -// including without limitation the rights to use, copy, modify, merge, -// publish, distribute, sublicense, and/or sell copies of the Software, -// and to permit persons to whom the Software is furnished to do so, -// subject to the following conditions: -// -// The above copyright notice and -// this permission notice shall be included in all copies -// or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED “AS IS”, -// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, -// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -// ARISING FROM, OUT OF OR IN CONNECTION WITH -// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -// -//----------------------------------------------------------------------- -//----------------------------------------------------------------------- -using StgSharp.Internal.Intrinsic; - -using System; -using System.Numerics; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -namespace StgSharp.Mathematics -{ - [StructLayout( - LayoutKind.Explicit, - Size = ( ( 3 + 2 ) * 4 * sizeof( float ) ) + sizeof( bool ), - Pack = 16 )] - public struct Matrix23 : IMatrix - { - - [FieldOffset( 5 * 4 * sizeof( float ) )] internal bool isTransposed = false; - [FieldOffset( 3 * 4 * sizeof( float ) )] internal ColumnSet2 _transpose; - [FieldOffset( 0 )] internal ColumnSet3 _mat; - - internal Matrix23( Vector4 c0, Vector4 c1, Vector4 c2 ) - { - _mat.colum0 = c0; - _mat.colum1 = c1; - _mat.colum2 = c2; - } - - public Matrix23() - { - Unsafe.SkipInit( out _mat ); - Unsafe.SkipInit( out _transpose ); - } - - public Matrix23( float a00, float a01, float a02, float a10, float a11, float a12 ) - { - _mat.colum0 = new Vector4( a00, a10, 0, 0 ); - _mat.colum1 = new Vector4( a01, a11, 0, 0 ); - _mat.colum2 = new Vector4( a02, a12, 0, 0 ); - } - - public unsafe float this[ int rowNum, int columNum ] - { - get - { - if( ( rowNum > 1 ) || ( rowNum < 0 ) ) { - throw new ArgumentOutOfRangeException( nameof( columNum ) ); - } - if( ( columNum > 2 ) || ( columNum < 0 ) ) { - throw new ArgumentOutOfRangeException( nameof( columNum ) ); - } - InternalTranspose(); - fixed( float* p = &this._transpose.m00 ) - { - ulong pbit = ( ( ulong )p ) + ( ( ( ulong )sizeof( Vector4 ) ) * ( ( ulong )rowNum ) ) + ( ( ( ulong )sizeof( float ) ) * ( ( ulong )columNum ) ); - return *( float* )pbit; - } - } - set - { - if( ( rowNum > 1 ) || ( rowNum < 0 ) ) { - throw new ArgumentOutOfRangeException( nameof( columNum ) ); - } - if( ( columNum > 2 ) || ( columNum < 0 ) ) { - throw new ArgumentOutOfRangeException( nameof( columNum ) ); - } - InternalTranspose(); - fixed( float* p = &this._transpose.m00 ) - { - ulong pbit = ( ( ulong )p ) + ( ( ( ulong )sizeof( Vector4 ) ) * ( ( ulong )rowNum ) ) + ( ( ( ulong )sizeof( float ) ) * ( ( ulong )columNum ) ); - *( float* )pbit = value; - } - isTransposed = false; - } - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - internal unsafe void InternalTranspose() - { - if( !isTransposed ) - { - fixed( ColumnSet3* source = &this._mat ) - { - fixed( ColumnSet2* target = &this._transpose ) { - InternalIO.Intrinsic.transpose32( source, target ); - } - } - - isTransposed = true; - } - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static unsafe Matrix23 operator -( Matrix23 left, Matrix23 right ) - { - Matrix23 ret = new Matrix23(); - InternalIO.Intrinsic.sub_mat_3( &left._mat, &right._mat, &ret._mat ); - return ret; - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static Matrix23 operator *( Matrix23 mat, float value ) - { - return new Matrix23( - mat._mat.colum0 * value, mat._mat.colum1 * value, mat._mat.colum2 * value ); - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static Matrix22 operator *( Matrix23 left, Matrix32 right ) - { - left.InternalTranspose(); - return new Matrix22( - Vector4.Dot( left._transpose.colum0, right.mat.colum0 ), - Vector4.Dot( left._transpose.colum0, right.mat.colum1 ), - - Vector4.Dot( left._transpose.colum0, right.mat.colum0 ), - Vector4.Dot( left._transpose.colum0, right.mat.colum1 ) ); - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static Matrix23 operator *( Matrix23 left, Matrix33 right ) - { - left.InternalTranspose(); - return new Matrix23( - Vector4.Dot( left._transpose.colum0, right.mat.colum0 ), - Vector4.Dot( left._transpose.colum0, right.mat.colum1 ), - Vector4.Dot( left._transpose.colum0, right.mat.colum2 ), - - Vector4.Dot( left._transpose.colum0, right.mat.colum0 ), - Vector4.Dot( left._transpose.colum0, right.mat.colum1 ), - Vector4.Dot( left._transpose.colum0, right.mat.colum2 ) ); - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static Matrix24 operator *( Matrix23 left, Matrix34 right ) - { - left.InternalTranspose(); - return new Matrix24( - Vector4.Dot( left._transpose.colum0, right.mat.colum0 ), - Vector4.Dot( left._transpose.colum0, right.mat.colum1 ), - Vector4.Dot( left._transpose.colum0, right.mat.colum2 ), - Vector4.Dot( left._transpose.colum0, right.mat.colum3 ), - - Vector4.Dot( left._transpose.colum0, right.mat.colum0 ), - Vector4.Dot( left._transpose.colum0, right.mat.colum1 ), - Vector4.Dot( left._transpose.colum0, right.mat.colum2 ), - Vector4.Dot( left._transpose.colum0, right.mat.colum3 ) ); - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static Matrix23 operator /( Matrix23 mat, float value ) - { - return new Matrix23( - mat._mat.colum0 / value, mat._mat.colum1 / value, mat._mat.colum2 / value ); - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static unsafe Matrix23 operator +( Matrix23 left, Matrix23 right ) - { - Matrix23 ret = new Matrix23(); - InternalIO.Intrinsic.add_mat_3( &left._mat, &right._mat, &ret._mat ); - return ret; - } - - } -} diff --git a/StgSharp.Common/Mathematics/Linear/Matrix24.cs b/StgSharp.Common/Mathematics/Linear/Matrix24.cs deleted file mode 100644 index 0fe4c100..00000000 --- a/StgSharp.Common/Mathematics/Linear/Matrix24.cs +++ /dev/null @@ -1,223 +0,0 @@ -//----------------------------------------------------------------------- -//----------------------------------------------------------------------- -// file="Matrix24.cs" -// Project: World -// AuthorGroup: Nitload Space -// Copyright (c) Nitload Space. 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 in the Software without restriction, -// including without limitation the rights to use, copy, modify, merge, -// publish, distribute, sublicense, and/or sell copies of the Software, -// and to permit persons to whom the Software is furnished to do so, -// subject to the following conditions: -// -// The above copyright notice and -// this permission notice shall be included in all copies -// or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED “AS IS”, -// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, -// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -// ARISING FROM, OUT OF OR IN CONNECTION WITH -// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -// -//----------------------------------------------------------------------- -//----------------------------------------------------------------------- -using StgSharp.Internal.Intrinsic; - -using System; -using System.Numerics; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -namespace StgSharp.Mathematics -{ - [StructLayout( - LayoutKind.Explicit, - Size = ( ( 4 + 2 ) * 4 * sizeof( float ) ) + sizeof( bool ), - Pack = 16 )] - public struct Matrix24 : IMatrix - { - - [FieldOffset( 6 * 4 * sizeof( float ) )] internal bool isTransposed; - [FieldOffset( 4 * 4 * sizeof( float ) )] internal ColumnSet2 transpose; - - [FieldOffset( 0 )] internal ColumnSet4 mat; - - [FieldOffset( 0 )] public Vec2 Colum0; - [FieldOffset( 16 )] public Vec2 Colum1; - [FieldOffset( 32 )] public Vec2 Colum2; - [FieldOffset( 48 )] public Vec2 Colum3; - - internal Matrix24( Vector4 c0, Vector4 c1, Vector4 c2, Vector4 c3 ) - { - Unsafe.SkipInit( out this ); - isTransposed = false; - mat.colum0 = c0; - mat.colum1 = c1; - mat.colum2 = c2; - mat.colum3 = c3; - } - - public Matrix24() - { - Unsafe.SkipInit( out this ); - isTransposed = false; - } - - public Matrix24( - float a00, - float a01, - float a02, - float a03, - float a10, - float a11, - float a12, - float a13 ) - { - Unsafe.SkipInit( out this ); - isTransposed = false; - mat.colum0 = new Vector4( a00, a10, 0, 0 ); - mat.colum1 = new Vector4( a01, a11, 0, 0 ); - mat.colum2 = new Vector4( a02, a12, 0, 0 ); - mat.colum3 = new Vector4( a03, a13, 0, 0 ); - } - - public unsafe float this[ int rowNum, int columNum ] - { - get - { - if( ( rowNum > 1 ) || ( rowNum < 0 ) ) { - throw new ArgumentOutOfRangeException( nameof( columNum ) ); - } - if( ( columNum > 3 ) || ( columNum < 0 ) ) { - throw new ArgumentOutOfRangeException( nameof( columNum ) ); - } - InternalTranspose(); - fixed( float* p = &this.transpose.m00 ) { - ulong pbit = ( ( ulong )p ) + ( ( ( ulong )sizeof( Vector4 ) ) * ( ( ulong )rowNum ) ) + ( ( ( ulong )sizeof( float ) ) * ( ( ulong )columNum ) ); - return *( float* )pbit; - } - } - set - { - if( ( rowNum > 1 ) || ( rowNum < 0 ) ) { - throw new ArgumentOutOfRangeException( nameof( columNum ) ); - } - if( ( columNum > 3 ) || ( columNum < 0 ) ) { - throw new ArgumentOutOfRangeException( nameof( columNum ) ); - } - InternalTranspose(); - fixed( float* p = &this.transpose.m00 ) { - ulong pbit = ( ( ulong )p ) + ( ( ( ulong )sizeof( Vector4 ) ) * ( ( ulong )rowNum ) ) + ( ( ( ulong )sizeof( float ) ) * ( ( ulong )columNum ) ); - *( float* )pbit = value; - } - isTransposed = false; - } - } - - public override string ToString() - { - InternalTranspose(); - return $"{$"[{mat.m00},{mat.m01},{mat.m02},{mat.m03}],"}{$"[{mat.m10},{mat.m11},{mat.m12},{mat.m13}]"}"; - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - internal unsafe void InternalTranspose() - { - if( !isTransposed ) { - fixed( ColumnSet4* source = &this.mat ) { - fixed( ColumnSet2* target = &this.transpose ) { - InternalIO.Intrinsic.transpose42( source, target ); - } - } - - isTransposed = true; - } - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static unsafe Matrix24 operator -( - Matrix24 left, - Matrix24 right ) - { - Matrix24 ret = new Matrix24(); - InternalIO.Intrinsic.sub_mat_4( &left.mat, &right.mat, &ret.mat ); - return ret; - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static Matrix24 operator *( Matrix24 mat, float value ) - { - return new Matrix24( - mat.mat.colum0 * value, mat.mat.colum1 * value, - mat.mat.colum2 * value, mat.mat.colum3 * value ); - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static Matrix22 operator *( Matrix24 left, Matrix42 right ) - { - left.InternalTranspose(); - return new Matrix22( - Vector4.Dot( left.transpose.colum0, right.mat.colum0 ), - Vector4.Dot( left.transpose.colum0, right.mat.colum1 ), - - Vector4.Dot( left.transpose.colum1, right.mat.colum0 ), - Vector4.Dot( left.transpose.colum1, right.mat.colum1 ) ); - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static Matrix23 operator *( Matrix24 left, Matrix43 right ) - { - left.InternalTranspose(); - return new Matrix23( - Vector4.Dot( left.transpose.colum0, right.mat.colum0 ), - Vector4.Dot( left.transpose.colum0, right.mat.colum1 ), - Vector4.Dot( left.transpose.colum0, right.mat.colum2 ), - - Vector4.Dot( left.transpose.colum1, right.mat.colum0 ), - Vector4.Dot( left.transpose.colum1, right.mat.colum1 ), - Vector4.Dot( left.transpose.colum1, right.mat.colum2 ) ); - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static Matrix24 operator *( Matrix24 left, Matrix44 right ) - { - left.InternalTranspose(); - return new Matrix24( - Vector4.Dot( left.transpose.colum0, right.mat.colum0 ), - Vector4.Dot( left.transpose.colum0, right.mat.colum1 ), - Vector4.Dot( left.transpose.colum0, right.mat.colum2 ), - Vector4.Dot( left.transpose.colum0, right.mat.colum3 ), - - Vector4.Dot( left.transpose.colum1, right.mat.colum0 ), - Vector4.Dot( left.transpose.colum1, right.mat.colum1 ), - Vector4.Dot( left.transpose.colum1, right.mat.colum2 ), - Vector4.Dot( left.transpose.colum1, right.mat.colum3 ) ); - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static Matrix24 operator /( Matrix24 mat, float value ) - { - return new Matrix24( - mat.mat.colum0 / value, mat.mat.colum1 / value, - mat.mat.colum2 / value, mat.mat.colum3 / value ); - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static unsafe Matrix24 operator +( - Matrix24 left, - Matrix24 right ) - { - Matrix24 ret = new Matrix24(); - InternalIO.Intrinsic.add_mat_4( &left.mat, &right.mat, &ret.mat ); - return ret; - } - - } -} diff --git a/StgSharp.Common/Mathematics/Linear/Matrix32.cs b/StgSharp.Common/Mathematics/Linear/Matrix32.cs deleted file mode 100644 index 7bfee222..00000000 --- a/StgSharp.Common/Mathematics/Linear/Matrix32.cs +++ /dev/null @@ -1,215 +0,0 @@ -//----------------------------------------------------------------------- -//----------------------------------------------------------------------- -// file="Matrix32.cs" -// Project: StgSharp -// AuthorGroup: Nitload Space -// Copyright (c) Nitload Space. 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 in the Software without restriction, -// including without limitation the rights to use, copy, modify, merge, -// publish, distribute, sublicense, and/or sell copies of the Software, -// and to permit persons to whom the Software is furnished to do so, -// subject to the following conditions: -// -// The above copyright notice and -// this permission notice shall be included in all copies -// or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED “AS IS”, -// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, -// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -// ARISING FROM, OUT OF OR IN CONNECTION WITH -// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -// -//----------------------------------------------------------------------- -//----------------------------------------------------------------------- -using StgSharp.Internal.Intrinsic; - -using System; -using System.Numerics; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -namespace StgSharp.Mathematics -{ - [StructLayout( - LayoutKind.Explicit, - Size = ( 5 * 4 * sizeof( float ) ) + sizeof( bool ), - Pack = 16 )] - public struct Matrix32 : IMatrix - { - - [FieldOffset( 5 * 4 * sizeof( float ) )] internal bool isTransposed; - - [FieldOffset( 0 )] internal ColumnSet2 mat; - [FieldOffset( 2 * 4 * sizeof( float ) )] internal ColumnSet3 transpose; - - [FieldOffset( 0 )] internal Vec3 colum0; - [FieldOffset( 4 * 4 )] internal Vec3 colum1; - - internal Matrix32( Vector4 c0, Vector4 c1 ) - { - Unsafe.SkipInit( out this ); - isTransposed = false; - mat.colum0 = c0; - mat.colum1 = c1; - } - - public Matrix32() - { - Unsafe.SkipInit( out this ); - isTransposed = false; - } - - public unsafe Matrix32( float a00, float a01, float a10, float a11, float a20, float a21 ) - { - Unsafe.SkipInit( out this ); - isTransposed = false; - mat.colum0 = new Vector4( a00, a10, a20, 0 ); - mat.colum1 = new Vector4( a01, a11, a21, 0 ); - } - - public unsafe float this[ int rowNum, int columNum ] - { - get - { - if( ( rowNum > 2 ) || ( rowNum < 0 ) ) { - throw new ArgumentOutOfRangeException( nameof( columNum ) ); - } - if( ( columNum > 1 ) || ( columNum < 0 ) ) { - throw new ArgumentOutOfRangeException( nameof( columNum ) ); - } - InternalTranspose(); - fixed( float* p = &this.transpose.m00 ) - { - ulong pbit = ( ( ulong )p ) + ( ( ( ulong )sizeof( Vector4 ) ) * ( ( ulong )rowNum ) ) + ( ( ( ulong )sizeof( float ) ) * ( ( ulong )columNum ) ); - return *( float* )pbit; - } - } - set - { - if( ( rowNum > 2 ) || ( rowNum < 0 ) ) { - throw new ArgumentOutOfRangeException( nameof( columNum ) ); - } - if( ( columNum > 1 ) || ( columNum < 0 ) ) { - throw new ArgumentOutOfRangeException( nameof( columNum ) ); - } - InternalTranspose(); - fixed( float* p = &this.transpose.m00 ) - { - ulong pbit = ( ( ulong )p ) + ( ( ( ulong )sizeof( Vector4 ) ) * ( ( ulong )rowNum ) ) + ( ( ( ulong )sizeof( float ) ) * ( ( ulong )columNum ) ); - *( float* )pbit = value; - } - isTransposed = false; - } - } - - public Vec3 VerticalVecMultiply( Vec2 vec ) - { - return ( colum0 * vec.X ) + ( colum1 * vec.Y ); - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - internal unsafe void InternalTranspose() - { - if( !isTransposed ) - { - fixed( ColumnSet2* source = &this.mat ) - { - fixed( ColumnSet3* target = &this.transpose ) { - InternalIO.Intrinsic.transpose23( source, target ); - } - } - - isTransposed = true; - } - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static Matrix32 operator -( Matrix32 left, Matrix32 right ) - { - return new Matrix32( - left.mat.colum0 - right.mat.colum0, left.mat.colum1 - right.mat.colum1 ); - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static Matrix32 operator *( Matrix32 mat, float value ) - { - return new Matrix32( mat.mat.colum0 * value, mat.mat.colum1 * value ); - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static Matrix32 operator *( Matrix32 left, Matrix22 right ) - { - left.InternalTranspose(); - return new Matrix32( - Vector4.Dot( left.transpose.colum0, right._mat.colum0 ), - Vector4.Dot( left.transpose.colum0, right._mat.colum1 ), - - Vector4.Dot( left.transpose.colum1, right._mat.colum0 ), - Vector4.Dot( left.transpose.colum1, right._mat.colum1 ), - - Vector4.Dot( left.transpose.colum2, right._mat.colum0 ), - Vector4.Dot( left.transpose.colum2, right._mat.colum1 ) ); - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static Matrix33 operator *( Matrix32 left, Matrix23 right ) - { - left.InternalTranspose(); - return new Matrix33( - Vector4.Dot( left.transpose.colum0, right._mat.colum0 ), - Vector4.Dot( left.transpose.colum0, right._mat.colum1 ), - Vector4.Dot( left.transpose.colum0, right._mat.colum2 ), - - Vector4.Dot( left.transpose.colum1, right._mat.colum0 ), - Vector4.Dot( left.transpose.colum1, right._mat.colum1 ), - Vector4.Dot( left.transpose.colum1, right._mat.colum2 ), - - Vector4.Dot( left.transpose.colum2, right._mat.colum0 ), - Vector4.Dot( left.transpose.colum2, right._mat.colum1 ), - Vector4.Dot( left.transpose.colum2, right._mat.colum2 ) ); - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static Matrix34 operator *( Matrix32 left, Matrix24 right ) - { - left.InternalTranspose(); - return new Matrix34( - Vector4.Dot( left.transpose.colum0, right.mat.colum0 ), - Vector4.Dot( left.transpose.colum0, right.mat.colum1 ), - Vector4.Dot( left.transpose.colum0, right.mat.colum2 ), - Vector4.Dot( left.transpose.colum0, right.mat.colum3 ), - - Vector4.Dot( left.transpose.colum1, right.mat.colum0 ), - Vector4.Dot( left.transpose.colum1, right.mat.colum1 ), - Vector4.Dot( left.transpose.colum1, right.mat.colum2 ), - Vector4.Dot( left.transpose.colum1, right.mat.colum3 ), - - Vector4.Dot( left.transpose.colum2, right.mat.colum0 ), - Vector4.Dot( left.transpose.colum2, right.mat.colum1 ), - Vector4.Dot( left.transpose.colum2, right.mat.colum2 ), - Vector4.Dot( left.transpose.colum2, right.mat.colum3 ) ); - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static Matrix32 operator /( Matrix32 mat, float value ) - { - return new Matrix32( mat.mat.colum0 / value, mat.mat.colum1 / value ); - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static unsafe Matrix32 operator +( Matrix32 left, Matrix32 right ) - { - Matrix32 ret = new Matrix32(); - InternalIO.Intrinsic.add_mat_2( &left.mat, &right.mat, &ret.mat ); - return ret; - } - - } -} diff --git a/StgSharp.Common/Mathematics/Linear/Matrix33.cs b/StgSharp.Common/Mathematics/Linear/Matrix33.cs deleted file mode 100644 index 8fbc7310..00000000 --- a/StgSharp.Common/Mathematics/Linear/Matrix33.cs +++ /dev/null @@ -1,240 +0,0 @@ -//----------------------------------------------------------------------- -//----------------------------------------------------------------------- -// file="Matrix33.cs" -// Project: StgSharp -// AuthorGroup: Nitload Space -// Copyright (c) Nitload Space. 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 in the Software without restriction, -// including without limitation the rights to use, copy, modify, merge, -// publish, distribute, sublicense, and/or sell copies of the Software, -// and to permit persons to whom the Software is furnished to do so, -// subject to the following conditions: -// -// The above copyright notice and -// this permission notice shall be included in all copies -// or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED “AS IS”, -// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, -// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -// ARISING FROM, OUT OF OR IN CONNECTION WITH -// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -// -//----------------------------------------------------------------------- -//----------------------------------------------------------------------- -using StgSharp.Internal.Intrinsic; - -using System; -using System.Numerics; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -namespace StgSharp.Mathematics -{ - [StructLayout( - LayoutKind.Explicit, - Size = ( 6 * 4 * sizeof( float ) ) + sizeof( bool ), - Pack = 16 )] - public struct Matrix33 : IMatrix - { - - [FieldOffset( 6 * 4 * sizeof( float ) )] internal bool isTransposed; - - [FieldOffset( 0 )] internal ColumnSet3 mat; - [FieldOffset( 3 * 4 * sizeof( float ) )] internal ColumnSet3 transpose; - [FieldOffset( 0 * 4 * sizeof( float ) )] public Vec3 Colum0; - [FieldOffset( 1 * 4 * sizeof( float ) )] public Vec3 Colum1; - [FieldOffset( 2 * 4 * sizeof( float ) )] public Vec3 Colum2; - - internal Matrix33( Vector4 c0, Vector4 c1, Vector4 c2 ) - { - Unsafe.SkipInit( out this ); - isTransposed = false; - mat.colum0 = c0; - mat.colum1 = c1; - mat.colum2 = c2; - } - - public Matrix33() - { - Unsafe.SkipInit( out this ); - isTransposed = false; - } - - public Matrix33( - float a00, - float a01, - float a02, - float a10, - float a11, - float a12, - float a20, - float a21, - float a22 ) - { - Unsafe.SkipInit( out this ); - isTransposed = false; - mat.colum0 = new Vector4( a00, a10, a20, 0 ); - mat.colum1 = new Vector4( a01, a11, a21, 0 ); - mat.colum2 = new Vector4( a02, a12, a22, 0 ); - } - - public unsafe float this[ int rowNum, int columNum ] - { - get - { - if( ( rowNum > 2 ) || ( rowNum < 0 ) ) { - throw new ArgumentOutOfRangeException( nameof( columNum ) ); - } - if( ( columNum > 2 ) || ( columNum < 0 ) ) { - throw new ArgumentOutOfRangeException( nameof( columNum ) ); - } - InternalTranspose(); - fixed( float* p = &this.transpose.m00 ) - { - ulong pbit = ( ( ulong )p ) + ( ( ( ulong )sizeof( Vector4 ) ) * ( ( ulong )rowNum ) ) + ( ( ( ulong )sizeof( float ) ) * ( ( ulong )columNum ) ); - return *( float* )pbit; - } - } - set - { - if( ( rowNum > 2 ) || ( rowNum < 0 ) ) { - throw new ArgumentOutOfRangeException( nameof( columNum ) ); - } - if( ( columNum > 2 ) || ( columNum < 0 ) ) { - InternalIO.InternalWriteLog( - $"Attempt to write unused space in {GetType().Name} {nameof(mat)}", - LogType.Error ); - } - InternalTranspose(); - fixed( float* p = &this.transpose.m00 ) - { - ulong pbit = ( ( ulong )p ) + ( ( ( ulong )sizeof( Vector4 ) ) * ( ( ulong )rowNum ) ) + ( ( ( ulong )sizeof( float ) ) * ( ( ulong )columNum ) ); - *( float* )pbit = value; - } - isTransposed = false; - } - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public unsafe float Det() - { - fixed( ColumnSet3* mat = &this.mat ) { - return InternalIO.Intrinsic.det_mat_3( mat, ( ColumnSet3* )0 ); - } - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - internal unsafe void InternalTranspose() - { - if( !isTransposed ) - { - fixed( ColumnSet3* source = &this.mat, target = &this.transpose ) { - InternalIO.Intrinsic.transpose33( source, target ); - } - isTransposed = true; - } - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static unsafe Matrix33 operator -( Matrix33 left, Matrix33 right ) - { - Matrix33 ret = new Matrix33(); - InternalIO.Intrinsic.sub_mat_3( &left.mat, &right.mat, &ret.mat ); - return ret; - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static Matrix33 operator *( Matrix33 mat, float value ) - { - return new Matrix33( - mat.mat.colum0 * value, mat.mat.colum1 * value, mat.mat.colum2 * value ); - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static Vec3 operator *( Matrix33 left, Vec3 right ) - { - left.InternalTranspose(); - return new Vec3( - Vector4.Dot( left.transpose.colum0, right.reg.AsVector4() ), - Vector4.Dot( left.transpose.colum1, right.reg.AsVector4() ), - Vector4.Dot( left.transpose.colum2, right.reg.AsVector4() ) ); - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static Matrix32 operator *( Matrix33 left, Matrix32 right ) - { - left.InternalTranspose(); - return new Matrix32( - Vector4.Dot( left.transpose.colum0, right.mat.colum0 ), - Vector4.Dot( left.transpose.colum0, right.mat.colum1 ), - - Vector4.Dot( left.transpose.colum1, right.mat.colum0 ), - Vector4.Dot( left.transpose.colum1, right.mat.colum1 ), - - Vector4.Dot( left.transpose.colum2, right.mat.colum0 ), - Vector4.Dot( left.transpose.colum2, right.mat.colum1 ) ); - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static Matrix33 operator *( Matrix33 left, Matrix33 right ) - { - left.InternalTranspose(); - return new Matrix33( - Vector4.Dot( left.transpose.colum0, right.mat.colum0 ), - Vector4.Dot( left.transpose.colum0, right.mat.colum1 ), - Vector4.Dot( left.transpose.colum0, right.mat.colum2 ), - - Vector4.Dot( left.transpose.colum1, right.mat.colum0 ), - Vector4.Dot( left.transpose.colum1, right.mat.colum1 ), - Vector4.Dot( left.transpose.colum1, right.mat.colum2 ), - - Vector4.Dot( left.transpose.colum2, right.mat.colum0 ), - Vector4.Dot( left.transpose.colum2, right.mat.colum1 ), - Vector4.Dot( left.transpose.colum2, right.mat.colum2 ) ); - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static Matrix34 operator *( Matrix33 left, Matrix34 right ) - { - left.InternalTranspose(); - return new Matrix34( - Vector4.Dot( left.transpose.colum0, right.mat.colum0 ), - Vector4.Dot( left.transpose.colum0, right.mat.colum1 ), - Vector4.Dot( left.transpose.colum0, right.mat.colum2 ), - Vector4.Dot( left.transpose.colum0, right.mat.colum3 ), - - Vector4.Dot( left.transpose.colum1, right.mat.colum0 ), - Vector4.Dot( left.transpose.colum1, right.mat.colum1 ), - Vector4.Dot( left.transpose.colum1, right.mat.colum2 ), - Vector4.Dot( left.transpose.colum1, right.mat.colum3 ), - - Vector4.Dot( left.transpose.colum2, right.mat.colum0 ), - Vector4.Dot( left.transpose.colum2, right.mat.colum1 ), - Vector4.Dot( left.transpose.colum2, right.mat.colum2 ), - Vector4.Dot( left.transpose.colum2, right.mat.colum3 ) ); - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static Matrix33 operator /( Matrix33 mat, float value ) - { - return new Matrix33( - mat.mat.colum0 / value, mat.mat.colum1 / value, mat.mat.colum2 / value ); - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static unsafe Matrix33 operator +( Matrix33 left, Matrix33 right ) - { - Matrix33 ret = new Matrix33(); - InternalIO.Intrinsic.add_mat_3( &left.mat, &right.mat, &ret.mat ); - return ret; - } - - } -} diff --git a/StgSharp.Common/Mathematics/Linear/Matrix34.cs b/StgSharp.Common/Mathematics/Linear/Matrix34.cs deleted file mode 100644 index 19091200..00000000 --- a/StgSharp.Common/Mathematics/Linear/Matrix34.cs +++ /dev/null @@ -1,240 +0,0 @@ -//----------------------------------------------------------------------- -//----------------------------------------------------------------------- -// file="Matrix34.cs" -// Project: StgSharp -// AuthorGroup: Nitload Space -// Copyright (c) Nitload Space. 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 in the Software without restriction, -// including without limitation the rights to use, copy, modify, merge, -// publish, distribute, sublicense, and/or sell copies of the Software, -// and to permit persons to whom the Software is furnished to do so, -// subject to the following conditions: -// -// The above copyright notice and -// this permission notice shall be included in all copies -// or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED “AS IS”, -// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, -// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -// ARISING FROM, OUT OF OR IN CONNECTION WITH -// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -// -//----------------------------------------------------------------------- -//----------------------------------------------------------------------- -using StgSharp.Internal.Intrinsic; - -using System; -using System.Drawing; -using System.Numerics; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -namespace StgSharp.Mathematics -{ - [StructLayout( - LayoutKind.Explicit, - Size = ( 7 * 4 * sizeof( float ) ) + sizeof( bool ), - Pack = 16 )] - public struct Matrix34 : IMatrix - { - - [FieldOffset( 7 * 4 * sizeof( float ) )] internal bool isTransposed; - [FieldOffset( 4 * 4 * sizeof( float ) )] internal ColumnSet3 transpose; - - [FieldOffset( 0 )] internal ColumnSet4 mat; - - [FieldOffset( 0 * 4 * sizeof( float ) )] public Vec3 Colum0; - [FieldOffset( 1 * 4 * sizeof( float ) )] public Vec3 Colum1; - [FieldOffset( 2 * 4 * sizeof( float ) )] public Vec3 Colum2; - [FieldOffset( 3 * 4 * sizeof( float ) )] public Vec3 Colum3; - - internal Matrix34( ColumnSet4 mat ) - { - Unsafe.SkipInit( out this ); - isTransposed = false; - this.mat = mat; - } - - internal Matrix34( Vector4 c0, Vector4 c1, Vector4 c2, Vector4 c3 ) - { - Unsafe.SkipInit( out this ); - isTransposed = false; - mat.colum0 = c0; - mat.colum1 = c1; - mat.colum2 = c2; - mat.colum3 = c3; - } - - public Matrix34() - { - Unsafe.SkipInit( out this ); - isTransposed = false; - } - - public Matrix34( - float a00, - float a01, - float a02, - float a03, - float a10, - float a11, - float a12, - float a13, - float a20, - float a21, - float a22, - float a23 ) - { - Unsafe.SkipInit( out this ); - isTransposed = false; - mat.colum0 = new Vector4( a00, a10, a20, 0 ); - mat.colum0 = new Vector4( a01, a11, a21, 0 ); - mat.colum0 = new Vector4( a02, a12, a22, 0 ); - mat.colum0 = new Vector4( a03, a13, a23, 0 ); - } - - public unsafe float this[ int rowNum, int columNum ] - { - get - { - if( ( rowNum > 2 ) || ( rowNum < 0 ) ) { - throw new ArgumentOutOfRangeException( nameof( columNum ) ); - } - if( ( columNum > 3 ) || ( columNum < 0 ) ) { - throw new ArgumentOutOfRangeException( nameof( columNum ) ); - } - InternalTranspose(); - fixed( float* p = &this.transpose.m00 ) - { - ulong pbit = ( ( ulong )p ) + ( ( ( ulong )sizeof( Vector4 ) ) * ( ( ulong )rowNum ) ) + ( ( ( ulong )sizeof( float ) ) * ( ( ulong )columNum ) ); - return *( float* )pbit; - } - } - set - { - if( ( rowNum > 2 ) || ( rowNum < 0 ) ) { - throw new ArgumentOutOfRangeException( nameof( columNum ) ); - } - if( ( columNum > 3 ) || ( columNum < 0 ) ) { - throw new ArgumentOutOfRangeException( nameof( columNum ) ); - } - InternalTranspose(); - fixed( float* p = &this.transpose.m00 ) - { - ulong pbit = ( ( ulong )p ) + ( ( ( ulong )sizeof( Vector4 ) ) * ( ( ulong )rowNum ) ) + ( ( ( ulong )sizeof( float ) ) * ( ( ulong )columNum ) ); - *( float* )pbit = value; - } - isTransposed = false; - } - } - - internal unsafe void InternalTranspose() - { - if( !isTransposed ) - { - fixed( ColumnSet4* source = &this.mat ) - { - fixed( ColumnSet3* target = &this.transpose ) { - InternalIO.Intrinsic.transpose43( source, target ); - } - } - - isTransposed = true; - } - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static unsafe Matrix34 operator -( Matrix34 left, Matrix34 right ) - { - Matrix34 ret = new Matrix34(); - InternalIO.Intrinsic.sub_mat_4( &left.mat, &right.mat, &ret.mat ); - return ret; - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static Matrix34 operator *( Matrix34 mat, float value ) - { - return new Matrix34( - mat.mat.colum0 * value, mat.mat.colum1 * value, mat.mat.colum2 * value, - mat.mat.colum3 * value ); - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static Matrix32 operator *( Matrix34 left, Matrix42 right ) - { - left.InternalTranspose(); - return new Matrix32( - Vector4.Dot( left.transpose.colum0, right.mat.colum0 ), - Vector4.Dot( left.transpose.colum0, right.mat.colum1 ), - - Vector4.Dot( left.transpose.colum1, right.mat.colum0 ), - Vector4.Dot( left.transpose.colum1, right.mat.colum1 ), - - Vector4.Dot( left.transpose.colum2, right.mat.colum0 ), - Vector4.Dot( left.transpose.colum2, right.mat.colum1 ) ); - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static Matrix33 operator *( Matrix34 left, Matrix43 right ) - { - left.InternalTranspose(); - return new Matrix33( - Vector4.Dot( left.transpose.colum0, right.mat.colum0 ), - Vector4.Dot( left.transpose.colum0, right.mat.colum1 ), - Vector4.Dot( left.transpose.colum0, right.mat.colum2 ), - - Vector4.Dot( left.transpose.colum1, right.mat.colum0 ), - Vector4.Dot( left.transpose.colum1, right.mat.colum1 ), - Vector4.Dot( left.transpose.colum1, right.mat.colum2 ), - - Vector4.Dot( left.transpose.colum2, right.mat.colum0 ), - Vector4.Dot( left.transpose.colum2, right.mat.colum1 ), - Vector4.Dot( left.transpose.colum2, right.mat.colum2 ) ); - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static Matrix34 operator *( Matrix34 left, Matrix44 right ) - { - left.InternalTranspose(); - return new Matrix34( - Vector4.Dot( left.transpose.colum0, right.mat.colum0 ), - Vector4.Dot( left.transpose.colum0, right.mat.colum1 ), - Vector4.Dot( left.transpose.colum0, right.mat.colum2 ), - Vector4.Dot( left.transpose.colum0, right.mat.colum3 ), - - Vector4.Dot( left.transpose.colum1, right.mat.colum0 ), - Vector4.Dot( left.transpose.colum1, right.mat.colum1 ), - Vector4.Dot( left.transpose.colum1, right.mat.colum2 ), - Vector4.Dot( left.transpose.colum1, right.mat.colum3 ), - - Vector4.Dot( left.transpose.colum2, right.mat.colum0 ), - Vector4.Dot( left.transpose.colum2, right.mat.colum1 ), - Vector4.Dot( left.transpose.colum2, right.mat.colum2 ), - Vector4.Dot( left.transpose.colum2, right.mat.colum3 ) ); - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static Matrix34 operator /( Matrix34 mat, float value ) - { - return new Matrix34( - mat.mat.colum0 / value, mat.mat.colum1 / value, mat.mat.colum2 / value, - mat.mat.colum3 / value ); - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static unsafe Matrix34 operator +( Matrix34 left, Matrix34 right ) - { - Matrix34 ret = new Matrix34(); - InternalIO.Intrinsic.add_mat_4( &left.mat, &right.mat, &ret.mat ); - return ret; - } - - } -} diff --git a/StgSharp.Common/Mathematics/Linear/Matrix42.cs b/StgSharp.Common/Mathematics/Linear/Matrix42.cs deleted file mode 100644 index f19d4079..00000000 --- a/StgSharp.Common/Mathematics/Linear/Matrix42.cs +++ /dev/null @@ -1,240 +0,0 @@ -//----------------------------------------------------------------------- -//----------------------------------------------------------------------- -// file="Matrix42.cs" -// Project: StgSharp -// AuthorGroup: Nitload Space -// Copyright (c) Nitload Space. 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 in the Software without restriction, -// including without limitation the rights to use, copy, modify, merge, -// publish, distribute, sublicense, and/or sell copies of the Software, -// and to permit persons to whom the Software is furnished to do so, -// subject to the following conditions: -// -// The above copyright notice and -// this permission notice shall be included in all copies -// or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED “AS IS”, -// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, -// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -// ARISING FROM, OUT OF OR IN CONNECTION WITH -// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -// -//----------------------------------------------------------------------- -//----------------------------------------------------------------------- -using StgSharp.Internal.Intrinsic; - -using System; -using System.Numerics; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -namespace StgSharp.Mathematics -{ - [StructLayout( - LayoutKind.Explicit, - Size = ( ( 16 + 8 ) * sizeof( float ) ) + sizeof( bool ), - Pack = 16 )] - public struct Matrix42 : IMatrix - { - - [FieldOffset( ( 16 + 8 ) * sizeof( float ) )] internal bool isTransposed; - - [FieldOffset( 0 )] internal ColumnSet2 mat; - [FieldOffset( 8 * sizeof( float ) )] internal ColumnSet4 transpose; - - internal Matrix42( ColumnSet2 mat ) - { - Unsafe.SkipInit( out this ); - isTransposed = false; - this.mat = mat; - } - - internal Matrix42( Vector4 c0, Vector4 c1 ) - { - Unsafe.SkipInit( out this ); - isTransposed = false; - mat.colum0 = c0; - mat.colum1 = c1; - } - - public Matrix42() - { - Unsafe.SkipInit( out this ); - isTransposed = false; - } - - public Matrix42( - float a00, - float a01, - float a10, - float a11, - float a20, - float a21, - float a30, - float a31 ) - { - Unsafe.SkipInit( out this ); - isTransposed = false; - mat.colum0 = new Vector4( a00, a10, a20, a30 ); - mat.colum1 = new Vector4( a01, a11, a21, a31 ); - } - - public unsafe float this[ int rowNum, int columNum ] - { - get - { - if( ( rowNum > 3 ) || ( rowNum < 0 ) ) { - throw new ArgumentOutOfRangeException( nameof( columNum ) ); - } - if( ( columNum > 1 ) || ( columNum < 0 ) ) { - throw new ArgumentOutOfRangeException( nameof( columNum ) ); - } - InternalTranspose(); - fixed( float* p = &this.transpose.m00 ) - { - ulong pbit = ( ( ulong )p ) + ( ( ( ulong )sizeof( Vector4 ) ) * ( ( ulong )rowNum ) ) + ( ( ( ulong )sizeof( float ) ) * ( ( ulong )columNum ) ); - return *( float* )pbit; - } - } - set - { - if( ( rowNum > 3 ) || ( rowNum < 0 ) ) { - throw new ArgumentOutOfRangeException( nameof( columNum ) ); - } - if( ( columNum > 1 ) || ( columNum < 0 ) ) { - throw new ArgumentOutOfRangeException( nameof( columNum ) ); - } - InternalTranspose(); - fixed( float* p = &this.transpose.m00 ) - { - ulong pbit = ( ( ulong )p ) + ( ( ( ulong )sizeof( Vector4 ) ) * ( ( ulong )rowNum ) ) + ( ( ( ulong )sizeof( float ) ) * ( ( ulong )columNum ) ); - *( float* )pbit = value; - } - isTransposed = false; - } - } - - public override int GetHashCode() - { - return HashCode.Combine( mat.colum0.GetHashCode(), mat.colum1.GetHashCode() ); - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - internal unsafe void InternalTranspose() - { - if( !isTransposed ) - { - fixed( ColumnSet2* source = &this.mat ) - { - fixed( ColumnSet4* target = &this.transpose ) { - InternalIO.Intrinsic.transpose24( source, target ); - } - } - - isTransposed = true; - } - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static unsafe Matrix42 operator -( Matrix42 left, Matrix42 right ) - { - Matrix42 ret = new Matrix42(); - InternalIO.Intrinsic.sub_mat_2( &left.mat, &right.mat, &ret.mat ); - return ret; - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static Matrix42 operator *( Matrix42 mat, float value ) - { - return new Matrix42( mat.mat.colum0 * value, mat.mat.colum1 * value ); - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static Matrix42 operator *( Matrix42 left, Matrix22 right ) - { - left.InternalTranspose(); - return new Matrix42( - Vector4.Dot( left.transpose.colum0, right._mat.colum0 ), - Vector4.Dot( left.transpose.colum0, right._mat.colum1 ), - - Vector4.Dot( left.transpose.colum1, right._mat.colum0 ), - Vector4.Dot( left.transpose.colum1, right._mat.colum1 ), - - Vector4.Dot( left.transpose.colum2, right._mat.colum0 ), - Vector4.Dot( left.transpose.colum2, right._mat.colum1 ), - - Vector4.Dot( left.transpose.colum3, right._mat.colum0 ), - Vector4.Dot( left.transpose.colum3, right._mat.colum1 ) ); - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static Matrix43 operator *( Matrix42 left, Matrix23 right ) - { - left.InternalTranspose(); - return new Matrix43( - Vector4.Dot( left.transpose.colum0, right._mat.colum0 ), - Vector4.Dot( left.transpose.colum0, right._mat.colum1 ), - Vector4.Dot( left.transpose.colum0, right._mat.colum2 ), - - Vector4.Dot( left.transpose.colum1, right._mat.colum0 ), - Vector4.Dot( left.transpose.colum1, right._mat.colum1 ), - Vector4.Dot( left.transpose.colum1, right._mat.colum2 ), - - Vector4.Dot( left.transpose.colum2, right._mat.colum0 ), - Vector4.Dot( left.transpose.colum2, right._mat.colum1 ), - Vector4.Dot( left.transpose.colum2, right._mat.colum2 ), - - Vector4.Dot( left.transpose.colum3, right._mat.colum0 ), - Vector4.Dot( left.transpose.colum3, right._mat.colum1 ), - Vector4.Dot( left.transpose.colum3, right._mat.colum2 ) ); - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static Matrix44 operator *( Matrix42 left, Matrix24 right ) - { - left.InternalTranspose(); - return new Matrix44( - Vector4.Dot( left.transpose.colum0, right.mat.colum0 ), - Vector4.Dot( left.transpose.colum0, right.mat.colum1 ), - Vector4.Dot( left.transpose.colum0, right.mat.colum2 ), - Vector4.Dot( left.transpose.colum0, right.mat.colum2 ), - - Vector4.Dot( left.transpose.colum1, right.mat.colum0 ), - Vector4.Dot( left.transpose.colum1, right.mat.colum1 ), - Vector4.Dot( left.transpose.colum1, right.mat.colum2 ), - Vector4.Dot( left.transpose.colum1, right.mat.colum2 ), - - Vector4.Dot( left.transpose.colum2, right.mat.colum0 ), - Vector4.Dot( left.transpose.colum2, right.mat.colum1 ), - Vector4.Dot( left.transpose.colum2, right.mat.colum2 ), - Vector4.Dot( left.transpose.colum2, right.mat.colum2 ), - - Vector4.Dot( left.transpose.colum3, right.mat.colum0 ), - Vector4.Dot( left.transpose.colum3, right.mat.colum1 ), - Vector4.Dot( left.transpose.colum3, right.mat.colum2 ), - Vector4.Dot( left.transpose.colum3, right.mat.colum2 ) ); - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static Matrix42 operator /( Matrix42 mat, float value ) - { - return new Matrix42( mat.mat.colum0 / value, mat.mat.colum1 / value ); - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static unsafe Matrix42 operator +( Matrix42 left, Matrix42 right ) - { - Matrix42 ret = new Matrix42(); - InternalIO.Intrinsic.add_mat_2( &left.mat, &right.mat, &ret.mat ); - return ret; - } - - } -} diff --git a/StgSharp.Common/Mathematics/Linear/Matrix43.cs b/StgSharp.Common/Mathematics/Linear/Matrix43.cs deleted file mode 100644 index 56c28113..00000000 --- a/StgSharp.Common/Mathematics/Linear/Matrix43.cs +++ /dev/null @@ -1,267 +0,0 @@ -//----------------------------------------------------------------------- -//----------------------------------------------------------------------- -// file="Matrix43.cs" -// Project: StgSharp -// AuthorGroup: Nitload Space -// Copyright (c) Nitload Space. 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 in the Software without restriction, -// including without limitation the rights to use, copy, modify, merge, -// publish, distribute, sublicense, and/or sell copies of the Software, -// and to permit persons to whom the Software is furnished to do so, -// subject to the following conditions: -// -// The above copyright notice and -// this permission notice shall be included in all copies -// or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED “AS IS”, -// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, -// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -// ARISING FROM, OUT OF OR IN CONNECTION WITH -// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -// -//----------------------------------------------------------------------- -//----------------------------------------------------------------------- -using StgSharp.Internal.Intrinsic; - -using System; -using System.Collections.Generic; -using System.Numerics; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -using System.Security.AccessControl; - -namespace StgSharp.Mathematics -{ - [StructLayout( - layoutKind: LayoutKind.Explicit, - Size = ( 7 * 4 * sizeof( float ) ) + sizeof( bool ), - Pack = 16 )] - public struct Matrix43 : IEquatable, IMatrix - { - - [FieldOffset( 7 * 4 * sizeof( float ) )] - internal bool isTransposed; - - [FieldOffset( 0 )] - internal ColumnSet3 mat; - [FieldOffset( 3 * 4 * sizeof( float ) )] - internal ColumnSet4 transpose; - - internal Matrix43( Vector4 c0, Vector4 c1, Vector4 c2 ) - { - Unsafe.SkipInit( out this ); - isTransposed = false; - mat.colum0 = c0; - mat.colum1 = c1; - mat.colum2 = c2; - } - - public Matrix43() - { - Unsafe.SkipInit( out this ); - isTransposed = false; - } - - public Matrix43( - float a00, - float a01, - float a02, - float a10, - float a11, - float a12, - float a20, - float a21, - float a22, - float a30, - float a31, - float a32 ) - { - Unsafe.SkipInit( out this ); - isTransposed = false; - mat.colum0 = new Vector4( a00, a10, a20, a30 ); - mat.colum1 = new Vector4( a01, a11, a21, a31 ); - mat.colum2 = new Vector4( a02, a12, a22, a32 ); - } - - public unsafe float this[ int rowNum, int columNum ] - { - get - { - if( ( rowNum > 3 ) || ( rowNum < 0 ) ) { - throw new ArgumentOutOfRangeException( nameof( columNum ) ); - } - if( ( columNum > 2 ) || ( columNum < 0 ) ) { - throw new ArgumentOutOfRangeException( nameof( columNum ) ); - } - InternalTranspose(); - fixed( float* p = &this.transpose.m00 ) - { - ulong pbit = ( ( ulong )p ) + ( ( ( ulong )sizeof( Vector4 ) ) * ( ( ulong )rowNum ) ) + ( ( ( ulong )sizeof( float ) ) * ( ( ulong )columNum ) ); - return *( float* )pbit; - } - } - set - { - if( ( rowNum > 3 ) || ( rowNum < 0 ) ) { - throw new ArgumentOutOfRangeException( nameof( columNum ) ); - } - if( ( columNum > 2 ) || ( columNum < 0 ) ) { - throw new ArgumentOutOfRangeException( nameof( columNum ) ); - } - InternalTranspose(); - fixed( float* p = &this.transpose.m00 ) - { - ulong pbit = ( ( ulong )p ) + ( ( ( ulong )sizeof( Vector4 ) ) * ( ( ulong )rowNum ) ) + ( ( ( ulong )sizeof( float ) ) * ( ( ulong )columNum ) ); - *( float* )pbit = value; - } - isTransposed = false; - } - } - - public override bool Equals( object? obj ) - { - return ( obj is Matrix43 x ) && Equals( x ); - } - - public bool Equals( Matrix43 other ) - { - return EqualityComparer.Default.Equals( mat, other.mat ); - } - - public override int GetHashCode() - { - return HashCode.Combine( - mat.colum0.GetHashCode(), mat.colum1.GetHashCode(), mat.colum2.GetHashCode() ); - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - internal unsafe void InternalTranspose() - { - if( !isTransposed ) - { - fixed( ColumnSet3* source = &this.mat ) - { - fixed( ColumnSet4* target = &this.transpose ) { - InternalIO.Intrinsic.transpose34( source, target ); - } - } - - isTransposed = true; - } - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static Matrix43 operator -( Matrix43 left, Matrix43 right ) - { - return new Matrix43( - left.mat.colum0 - right.mat.colum0, left.mat.colum1 - right.mat.colum1, - left.mat.colum2 - right.mat.colum2 ); - } - - public static bool operator !=( Matrix43 left, Matrix43 right ) - { - return !( left == right ); - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static unsafe Matrix43 operator *( Matrix43 mat, float value ) - { - return new Matrix43( - mat.mat.colum0 * value, mat.mat.colum1 * value, mat.mat.colum2 * value ); - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static Matrix42 operator *( Matrix43 left, Matrix32 right ) - { - left.InternalTranspose(); - return new Matrix42( - Vector4.Dot( left.transpose.colum0, right.mat.colum0 ), - Vector4.Dot( left.transpose.colum0, right.mat.colum1 ), - - Vector4.Dot( left.transpose.colum1, right.mat.colum0 ), - Vector4.Dot( left.transpose.colum1, right.mat.colum1 ), - - Vector4.Dot( left.transpose.colum2, right.mat.colum0 ), - Vector4.Dot( left.transpose.colum2, right.mat.colum1 ), - - Vector4.Dot( left.transpose.colum3, right.mat.colum0 ), - Vector4.Dot( left.transpose.colum3, right.mat.colum1 ) ); - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static Matrix43 operator *( Matrix43 left, Matrix33 right ) - { - left.InternalTranspose(); - return new Matrix43( - Vector4.Dot( left.transpose.colum0, right.mat.colum0 ), - Vector4.Dot( left.transpose.colum0, right.mat.colum1 ), - Vector4.Dot( left.transpose.colum0, right.mat.colum2 ), - - Vector4.Dot( left.transpose.colum1, right.mat.colum0 ), - Vector4.Dot( left.transpose.colum1, right.mat.colum1 ), - Vector4.Dot( left.transpose.colum1, right.mat.colum2 ), - - Vector4.Dot( left.transpose.colum2, right.mat.colum0 ), - Vector4.Dot( left.transpose.colum2, right.mat.colum1 ), - Vector4.Dot( left.transpose.colum2, right.mat.colum2 ), - - Vector4.Dot( left.transpose.colum3, right.mat.colum0 ), - Vector4.Dot( left.transpose.colum3, right.mat.colum1 ), - Vector4.Dot( left.transpose.colum3, right.mat.colum2 ) ); - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static Matrix44 operator *( Matrix43 left, Matrix34 right ) - { - left.InternalTranspose(); - return new Matrix44( - Vector4.Dot( left.transpose.colum0, right.mat.colum0 ), - Vector4.Dot( left.transpose.colum0, right.mat.colum1 ), - Vector4.Dot( left.transpose.colum0, right.mat.colum2 ), - Vector4.Dot( left.transpose.colum0, right.mat.colum3 ), - - Vector4.Dot( left.transpose.colum1, right.mat.colum0 ), - Vector4.Dot( left.transpose.colum1, right.mat.colum1 ), - Vector4.Dot( left.transpose.colum1, right.mat.colum2 ), - Vector4.Dot( left.transpose.colum1, right.mat.colum3 ), - - Vector4.Dot( left.transpose.colum2, right.mat.colum0 ), - Vector4.Dot( left.transpose.colum2, right.mat.colum1 ), - Vector4.Dot( left.transpose.colum2, right.mat.colum2 ), - Vector4.Dot( left.transpose.colum2, right.mat.colum3 ), - - Vector4.Dot( left.transpose.colum3, right.mat.colum0 ), - Vector4.Dot( left.transpose.colum3, right.mat.colum1 ), - Vector4.Dot( left.transpose.colum3, right.mat.colum2 ), - Vector4.Dot( left.transpose.colum3, right.mat.colum3 ) ); - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static Matrix43 operator /( Matrix43 mat, float value ) - { - return new Matrix43( - mat.mat.colum0 / value, mat.mat.colum1 / value, mat.mat.colum2 / value ); - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static unsafe Matrix43 operator +( Matrix43 left, Matrix43 right ) - { - Matrix43 ret = new Matrix43(); - InternalIO.Intrinsic.add_mat_3( &left.mat, &right.mat, &ret.mat ); - return ret; - } - - public static bool operator ==( Matrix43 left, Matrix43 right ) - { - return left.Equals( right ); - } - - } -} \ No newline at end of file diff --git a/StgSharp.Common/Mathematics/Linear/Matrix44.cs b/StgSharp.Common/Mathematics/Linear/Matrix44.cs index e4ba7d03..e5bfa1a2 100644 --- a/StgSharp.Common/Mathematics/Linear/Matrix44.cs +++ b/StgSharp.Common/Mathematics/Linear/Matrix44.cs @@ -1,33 +1,33 @@ //----------------------------------------------------------------------- -//----------------------------------------------------------------------- -// file="Matrix44.cs" -// Project: StgSharp -// AuthorGroup: Nitload Space -// Copyright (c) Nitload Space. All rights reserved. +// ----------------------------------------------------------------------- +// file="Matrix44.cs" +// Project: StgSharp +// AuthorGroup: Nitload Space +// Copyright (c) Nitload Space. 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 in the Software without restriction, -// including without limitation the rights to use, copy, modify, merge, -// publish, distribute, sublicense, and/or sell copies of the Software, -// and to permit persons to whom the Software is furnished to do so, -// subject to the following conditions: +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the “Software”), to deal in the Software without restriction, +// including without limitation the rights to use, copy, modify, merge, +// publish, distribute, sublicense, and/or sell copies of the Software, +// and to permit persons to whom the Software is furnished to do so, +// subject to the following conditions: // -// The above copyright notice and -// this permission notice shall be included in all copies -// or substantial portions of the Software. +// The above copyright notice and +// this permission notice shall be included in all copies +// or substantial portions of the Software. // -// THE SOFTWARE IS PROVIDED “AS IS”, -// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, -// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -// ARISING FROM, OUT OF OR IN CONNECTION WITH -// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// THE SOFTWARE IS PROVIDED “AS IS”, +// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH +// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // -//----------------------------------------------------------------------- -//----------------------------------------------------------------------- +// ----------------------------------------------------------------------- +// ----------------------------------------------------------------------- using StgSharp.Internal.Intrinsic; using System; @@ -39,32 +39,29 @@ using System.Runtime.InteropServices; namespace StgSharp.Mathematics { - [StructLayout( - LayoutKind.Explicit, - Size = ( 2 * 16 * sizeof( float ) ) + sizeof( bool ), - Pack = 16 )] + [StructLayout(LayoutKind.Explicit, Size = (2 * 16 * sizeof(float)) + sizeof(bool), Pack = 16)] public unsafe struct Matrix44 : IEquatable, IMatrix { - [FieldOffset( 2 * 64 * sizeof( float ) )] internal bool isTransposed; + [FieldOffset(2 * 64 * sizeof(float))] internal bool isTransposed; - [FieldOffset( 0 )] internal ColumnSet4 mat; - [FieldOffset( 16 * sizeof( float ) )] internal ColumnSet4 transpose; - [FieldOffset( 0 * sizeof( float ) )] internal Vec4 colum0; - [FieldOffset( 4 * sizeof( float ) )] internal Vec4 colum1; - [FieldOffset( 8 * sizeof( float ) )] internal Vec4 colum2; - [FieldOffset( 12 * sizeof( float ) )] internal Vec4 colum3; + [FieldOffset(0)] internal ColumnSet4 mat; + [FieldOffset(16 * sizeof(float))] internal ColumnSet4 transpose; + [FieldOffset(0 * sizeof(float))] internal Vec4 colum0; + [FieldOffset(4 * sizeof(float))] internal Vec4 colum1; + [FieldOffset(8 * sizeof(float))] internal Vec4 colum2; + [FieldOffset(12 * sizeof(float))] internal Vec4 colum3; - internal Matrix44( ColumnSet4 mat ) + internal Matrix44(ColumnSet4 mat) { - Unsafe.SkipInit( out this ); + Unsafe.SkipInit(out this); isTransposed = false; 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 ); + Unsafe.SkipInit(out this); isTransposed = false; mat.colum0 = vec0; mat.colum1 = vec1; @@ -74,7 +71,7 @@ namespace StgSharp.Mathematics public Matrix44() { - Unsafe.SkipInit( out this ); + Unsafe.SkipInit(out this); isTransposed = false; } @@ -94,52 +91,52 @@ namespace StgSharp.Mathematics float a30, float a31, float a32, - float a33 ) + float a33) { - Unsafe.SkipInit( out this ); + Unsafe.SkipInit(out this); isTransposed = false; - 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 ); + 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 ] + public unsafe float this[int rowNum, int columNum] { get { #if DEBUG - if( ( rowNum > 3 ) || ( rowNum < 0 ) ) + if (rowNum is > 3 or < 0) { - throw new ArgumentOutOfRangeException( nameof( columNum ) ); + throw new ArgumentOutOfRangeException(nameof(columNum)); } - if( ( columNum > 3 ) || ( columNum < 0 ) ) { - throw new ArgumentOutOfRangeException( nameof( columNum ) ); + if (columNum is > 3 or < 0) { + throw new ArgumentOutOfRangeException(nameof(columNum)); } #endif InternalTranspose(); - fixed( float* p = &this.transpose.m00 ) + fixed (float* p = &this.transpose.m00) { - ulong pbit = ( ( ulong )p ) + ( ( ( ulong )sizeof( Vector4 ) ) * ( ( ulong )rowNum ) ) + ( ( ( ulong )sizeof( float ) ) * ( ( ulong )columNum ) ); - return *( float* )pbit; + ulong pBit = ((ulong)p) + (((ulong)sizeof(Vector4)) * ((ulong)rowNum)) + (((ulong)sizeof(float)) * ((ulong)columNum)); + return *(float*)pBit; } } set { #if DEBUG - if( ( rowNum > 3 ) || ( rowNum < 0 ) ) + if (rowNum is > 3 or < 0) { - throw new ArgumentOutOfRangeException( nameof( columNum ) ); + throw new ArgumentOutOfRangeException(nameof(columNum)); } - if( ( columNum > 3 ) || ( columNum < 0 ) ) { - throw new ArgumentOutOfRangeException( nameof( columNum ) ); + if (columNum is > 3 or < 0) { + throw new ArgumentOutOfRangeException(nameof(columNum)); } #endif InternalTranspose(); - fixed( float* p = &this.transpose.m00 ) + fixed (float* p = &this.transpose.m00) { - ulong pbit = ( ( ulong )p ) + ( ( ( ulong )sizeof( Vector4 ) ) * ( ( ulong )rowNum ) ) + ( ( ( ulong )sizeof( float ) ) * ( ( ulong )columNum ) ); - *( float* )pbit = value; + ulong pbit = ((ulong)p) + (((ulong)sizeof(Vector4)) * ((ulong)rowNum)) + (((ulong)sizeof(float)) * ((ulong)columNum)); + *(float*)pbit = value; } isTransposed = false; } @@ -150,80 +147,41 @@ namespace StgSharp.Mathematics get { InternalTranspose(); - return new Matrix44( this.transpose ); + return new Matrix44(this.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 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); - [MethodImpl( MethodImplOptions.AggressiveInlining )] + [MethodImpl(MethodImplOptions.AggressiveInlining)] public unsafe float Det() { - InternalTranspose(); - fixed( ColumnSet4* _this = &mat, _trans = &transpose ) { - return InternalIO.Intrinsic.det_mat_4( _this, _trans ); - } + return 0; } - public override bool Equals( object? obj ) + public override bool Equals(object? obj) { - return ( obj is Matrix44 x ) && Equals( x ); + return (obj is Matrix44 x) && Equals(x); } - public bool Equals( Matrix44 other ) + public bool Equals(Matrix44 other) { - return mat.Equals( other.mat ); + 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 )] - public unsafe Matrix42 Multiple( in Matrix42 right ) - { - InternalTranspose(); - Matrix42 ans = new Matrix42(); - fixed( ColumnSet4* lptr = &transpose ) - { - fixed( Vector4* rptr = &right.mat.colum0 ) { - InternalIO.Intrinsic.dot_42( lptr, rptr, &ans.mat.colum0 ); - } - } - - return ans; - } - - public unsafe Matrix43 Multiple( in Matrix43 right ) - { - InternalTranspose(); - - Matrix43 ans = new Matrix43(); - - fixed( Vector4* rptr = &right.mat.colum0 ) - { - fixed( ColumnSet4* lptr = &transpose ) { - InternalIO.Intrinsic.dot_43( lptr, rptr, &ans.mat.colum0 ); - } - } - - return ans; + mat.colum3.GetHashCode()); } public override string ToString() @@ -232,94 +190,78 @@ namespace StgSharp.Mathematics return $"{transpose.colum0}{transpose.colum1}{transpose.colum2}{transpose.colum3}"; } - [MethodImpl( MethodImplOptions.AggressiveInlining )] + [MethodImpl(MethodImplOptions.AggressiveInlining)] internal unsafe void InternalTranspose() { - if( !isTransposed ) + if (!isTransposed) { - fixed( ColumnSet4* source = &mat, target = &transpose ) { - InternalIO.Intrinsic.transpose44( source, target ); + fixed (ColumnSet4* source = &mat, target = &transpose) + { + // InternalIO.Intrinsic.transpose44(source, target); } isTransposed = true; } } - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static unsafe Matrix44 operator -( Matrix44 left, Matrix44 right ) + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static unsafe Matrix44 operator -(Matrix44 left, Matrix44 right) { return new Matrix44( right.mat.colum0 - left.mat.colum0, right.mat.colum1 - left.mat.colum1, - right.mat.colum2 - left.mat.colum2, right.mat.colum3 - left.mat.colum3 ); + right.mat.colum2 - left.mat.colum2, right.mat.colum3 - left.mat.colum3); } - public static bool operator !=( Matrix44 left, Matrix44 right ) + public static bool operator !=(Matrix44 left, Matrix44 right) { - return !( left == right ); + return !(left == right); } - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static unsafe Matrix44 operator *( Matrix44 mat, float value ) + [MethodImpl(MethodImplOptions.AggressiveInlining)] + 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 ); + mat.mat.colum3 * value); } - [Obsolete( - "The operand may cause performance loss, use Matrix.Multiple() method instead.", - false )] - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static unsafe Matrix42 operator *( Matrix44 left, Matrix42 right ) - { - return left.Multiple( right ); - } - - [Obsolete( - "The operand may cause performance loss, use Matrix.Multiple() method instead.", - false )] - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static Matrix43 operator *( Matrix44 left, Matrix43 right ) - { - return left.Multiple( in right ); - } - - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static Matrix44 operator *( Matrix44 left, Matrix44 right ) + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Matrix44 operator *(Matrix44 left, Matrix44 right) { left.InternalTranspose(); return new Matrix44( - Vector4.Dot( left.transpose.colum0, right.mat.colum0 ), - Vector4.Dot( left.transpose.colum0, right.mat.colum1 ), - Vector4.Dot( left.transpose.colum0, right.mat.colum2 ), - Vector4.Dot( left.transpose.colum0, right.mat.colum3 ), + Vector4.Dot(left.transpose.colum0, right.mat.colum0), + Vector4.Dot(left.transpose.colum0, right.mat.colum1), + Vector4.Dot(left.transpose.colum0, right.mat.colum2), + Vector4.Dot(left.transpose.colum0, right.mat.colum3), - Vector4.Dot( left.transpose.colum1, right.mat.colum0 ), - Vector4.Dot( left.transpose.colum1, right.mat.colum1 ), - Vector4.Dot( left.transpose.colum1, right.mat.colum2 ), - Vector4.Dot( left.transpose.colum1, right.mat.colum3 ), + Vector4.Dot(left.transpose.colum1, right.mat.colum0), + Vector4.Dot(left.transpose.colum1, right.mat.colum1), + Vector4.Dot(left.transpose.colum1, right.mat.colum2), + Vector4.Dot(left.transpose.colum1, right.mat.colum3), - Vector4.Dot( left.transpose.colum2, right.mat.colum0 ), - Vector4.Dot( left.transpose.colum2, right.mat.colum1 ), - Vector4.Dot( left.transpose.colum2, right.mat.colum2 ), - Vector4.Dot( left.transpose.colum2, right.mat.colum3 ), + Vector4.Dot(left.transpose.colum2, right.mat.colum0), + Vector4.Dot(left.transpose.colum2, right.mat.colum1), + Vector4.Dot(left.transpose.colum2, right.mat.colum2), + Vector4.Dot(left.transpose.colum2, right.mat.colum3), - Vector4.Dot( left.transpose.colum3, right.mat.colum0 ), - Vector4.Dot( left.transpose.colum3, right.mat.colum1 ), - Vector4.Dot( left.transpose.colum3, right.mat.colum2 ), - Vector4.Dot( left.transpose.colum3, right.mat.colum3 ) ); + Vector4.Dot(left.transpose.colum3, right.mat.colum0), + Vector4.Dot(left.transpose.colum3, right.mat.colum1), + Vector4.Dot(left.transpose.colum3, right.mat.colum2), + Vector4.Dot(left.transpose.colum3, right.mat.colum3)); } - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static unsafe Matrix44 operator +( Matrix44 left, Matrix44 right ) + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static unsafe Matrix44 operator +(Matrix44 left, Matrix44 right) { Matrix44 ret = new Matrix44(); - InternalIO.Intrinsic.add_mat_4( &left.mat, &right.mat, &ret.mat ); + + // InternalIO.Intrinsic.add_mat_4(&left.mat, &right.mat, &ret.mat); return ret; } - public static bool operator ==( Matrix44 left, Matrix44 right ) + public static bool operator ==(Matrix44 left, Matrix44 right) { - return left.Equals( right ); + return left.Equals(right); } } diff --git a/StgSharp.Native/StgSharp.Native.vcxproj b/StgSharp.Native/StgSharp.Native.vcxproj index ad230355..c338677d 100644 --- a/StgSharp.Native/StgSharp.Native.vcxproj +++ b/StgSharp.Native/StgSharp.Native.vcxproj @@ -159,18 +159,16 @@ - + - - - + + + + - - - - - + + diff --git a/StgSharp.Native/sn-head/sn_intrinsic.h b/StgSharp.Native/sn-head/sn_intrinsic.h index 3efe9d3e..3e0994d3 100644 --- a/StgSharp.Native/sn-head/sn_intrinsic.h +++ b/StgSharp.Native/sn-head/sn_intrinsic.h @@ -32,12 +32,12 @@ typedef union uint64_u { uint32_t u32[2]; } uint64_u; -typedef union mat_kernel{ +typedef union mat_kernel { __m128 xmm[4]; __m256 ymm[2]; __m512 zmm[1]; float m[4][4]; -}mat_kernel; +} mat_kernel; typedef union column_2 { __m256 stream; @@ -64,66 +64,33 @@ typedef void(SN_DECL *MATRIXTRANSPOSEPROC)(void *source, void *target); typedef void(SN_DECL *MATRIXDETPROC)(void *mat, void *transpose); typedef void(SN_DECL *MATARITHMETIC)(void *left, void *right, void *ans); typedef void(SN_DECL *SCALERMATARITHMETIC)(void *left, void *right, void *ans); -typedef void(SN_DECL* MATFMAPROC)(mat_kernel* left, mat_kernel* right, mat_kernel* ans); +typedef void(SN_DECL *MATFMAPROC)(mat_kernel *left, mat_kernel *right, mat_kernel *ans); +typedef void(SN_DECL *SCALARMATARITHMETIC)(mat_kernel *matrix, float scalar, mat_kernel *ans); +typedef void(SN_DECL *MATTRANSPOSEPROC)(mat_kernel *source, mat_kernel *target); typedef uint64_t(SN_DECL *FACTORIALROC)(int n); - #pragma region matix function INTERNAL void normalize(__m128 *source, __m128 *target); -INTERNAL void SN_DECL addmatrix2_sse(__2_columnset *left, __2_columnset *right, __2_columnset *ans); -INTERNAL void SN_DECL addmatrix2_avx(__2_columnset *left, __2_columnset *right, __2_columnset *ans); -INTERNAL void SN_DECL submatrix2_sse(__2_columnset *left, __2_columnset *right, __2_columnset *ans); -INTERNAL void SN_DECL addmatrix2_avx(__2_columnset *left, __2_columnset *right, __2_columnset *ans); -INTERNAL void SN_DECL transpose23(__2_columnset const *source, __3_columnset *target); -INTERNAL void SN_DECL transpose24(__2_columnset const *source, __4_columnset *target); - -INTERNAL void SN_DECL addmatrix3_sse(__3_columnset *left, __3_columnset *right, __3_columnset *ans); -INTERNAL void SN_DECL addmatrix3_avx(__3_columnset *left, __3_columnset *right, __3_columnset *ans); -INTERNAL void SN_DECL addmatrix3_512(__3_columnset *left, __3_columnset *right, __3_columnset *ans); -INTERNAL void SN_DECL submatrix3_sse(__3_columnset *left, __3_columnset *right, __3_columnset *ans); -INTERNAL void SN_DECL submatrix3_avx(__3_columnset *left, __3_columnset *right, __3_columnset *ans); -INTERNAL void SN_DECL submatrix3_512(__3_columnset *left, __3_columnset *right, __3_columnset *ans); -INTERNAL void SN_DECL transpose32(__3_columnset const *source, __2_columnset *target); -INTERNAL void SN_DECL transpose33(__3_columnset const *source, __3_columnset *target); -INTERNAL void SN_DECL transpose34(__3_columnset const *source, __4_columnset *target); -INTERNAL float SN_DECL det_mat3(__3_columnset *matrix, __3_columnset *tranpose); - -INTERNAL void SN_DECL addmatrix4_sse(__4_columnset *left, __4_columnset *right, __4_columnset *ans); -INTERNAL void SN_DECL addmatrix4_avx(__4_columnset *left, __4_columnset *right, __4_columnset *ans); -INTERNAL void SN_DECL addmatrix4_512(__4_columnset *left, __4_columnset *right, __4_columnset *ans); -INTERNAL void SN_DECL submatrix4_sse(__4_columnset *left, __4_columnset *right, __4_columnset *ans); -INTERNAL void SN_DECL submatrix4_avx(__4_columnset *left, __4_columnset *right, __4_columnset *ans); -INTERNAL void SN_DECL submatrix4_512(__4_columnset *left, __4_columnset *right, __4_columnset *ans); -INTERNAL void SN_DECL transpose42(__4_columnset const *source, __2_columnset *target); -INTERNAL void SN_DECL transpose43(__4_columnset const *source, __3_columnset *target); -INTERNAL void SN_DECL transpose44(__4_columnset const *source, __4_columnset *target); -INTERNAL float SN_DECL det_mat4(__4_columnset *matrix, __4_columnset *transpose); +INTERNAL void SN_DECL kernel_add_sse(mat_kernel const *left, mat_kernel const *right, mat_kernel *ans); +INTERNAL void SN_DECL kernel_add_avx(mat_kernel const *left, mat_kernel const *right, mat_kernel *ans); +INTERNAL void SN_DECL kernel_add_512(mat_kernel const *left, mat_kernel const *right, mat_kernel *ans); +INTERNAL void SN_DECL kernel_sub_sse(mat_kernel const *left, mat_kernel const *right, mat_kernel *ans); +INTERNAL void SN_DECL kernel_sub_avx(mat_kernel const *left, mat_kernel const *right, mat_kernel *ans); +INTERNAL void SN_DECL kernel_sub_512(mat_kernel const *left, mat_kernel const *right, mat_kernel *ans); +INTERNAL void SN_DECL kernel_transpose_sse(mat_kernel const *source, mat_kernel *target); +INTERNAL void SN_DECL kernel_transpose_avx(mat_kernel const *source, mat_kernel *target); +INTERNAL void SN_DECL kernel_transpose_512(mat_kernel const *source, mat_kernel *target); +INTERNAL void SN_DECL kernel_scalar_mul_sse(mat_kernel const *matrix, float const scalar, mat_kernel *ans); +INTERNAL void SN_DECL kernel_scalar_mul_avx(mat_kernel const *matrix, float const scalar, mat_kernel *ans); +INTERNAL void SN_DECL kernel_scalar_mul_512(mat_kernel const *matrix, float const scalar, mat_kernel *ans); #pragma endregion #pragma region fma -INTERNAL void SN_DECL fma_44_sse(mat_kernel* left, mat_kernel* right); - -#pragma endregion - -#pragma region dot - -INTERNAL void SN_DECL dot_41_sse(__4_columnset *transpose, __m128 *vector, __m128 *ans); -INTERNAL void SN_DECL dot_31_sse(__3_columnset *transpose, __m128 *vector, __m128 *ans); -INTERNAL void SN_DECL dot_41_avx(__4_columnset *transpose, __m128 *vector, __m128 *ans); -INTERNAL void SN_DECL dot_31_avx(__3_columnset *transpose, __m128 *vector, __m128 *ans); - -INTERNAL void SN_DECL dot_32_sse(__3_columnset *transpose, __m128 *vector, __m128 *ans); -INTERNAL void SN_DECL dot_32_avx(__3_columnset *transpose, __m128 *vector, __m128 *ans); -INTERNAL void SN_DECL dot_42_sse(__4_columnset *transpose, __m128 *vector, __m128 *ans); -INTERNAL void SN_DECL dot_42_avx(__4_columnset *transpose, __m128 *vector, __m128 *ans); - -INTERNAL void SN_DECL dot_43_sse(__4_columnset *transpose, __m128 *vector, __m128 *ans); -INTERNAL void SN_DECL dot_43_avx(__4_columnset *transpose, __m128 *vector, __m128 *ans); -//INTERNAL void SN_DECL dot_43_512(__4_columnset *transpose, __m128 *vector, __m128 *ans); +INTERNAL void SN_DECL kernel_fma_sse(mat_kernel *left, mat_kernel *right); #pragma endregion @@ -138,34 +105,17 @@ INTERNAL int SN_DECL index_pair_sse(short const *str, uint32_t target, int lengt #pragma endregion -typedef struct ssc_intrinsic { - MATARITHMETIC add_m2; - MATARITHMETIC add_m3; - MATARITHMETIC add_m4; +typedef struct sn_intrinsic { int (*city_hash_simplify)(byte *str, int length); - MATRIXDETPROC det_matrix33; - MATRIXDETPROC det_matrix44; - DOTPROC dot_31; - DOTPROC dot_32; - DOTPROC dot_41; - DOTPROC dot_42; - DOTPROC dot_43; FACTORIALROC factorial_simd; - void (*fma_44)(void* left, void* right, void* ans); int (*index_pair)(short const *str, uint32_t target, int length); + MATARITHMETIC kernel_add; + void (*kernel_fma)(void *left, void *right, void *ans); + SCALARMATARITHMETIC kernel_scalar_mul; + MATARITHMETIC kernel_sub; + MATTRANSPOSEPROC kernel_transpose; VECTORNORMALIZEPROC normalize_3; - MATARITHMETIC sub_m2; - MATARITHMETIC sub_m3; - MATARITHMETIC sub_m4; - MATRIXTRANSPOSEPROC transpose_23; - MATRIXTRANSPOSEPROC transpose_24; - MATRIXTRANSPOSEPROC transpose_32; - MATRIXTRANSPOSEPROC transpose_33; - MATRIXTRANSPOSEPROC transpose_34; - MATRIXTRANSPOSEPROC transpose_42; - MATRIXTRANSPOSEPROC transpose_43; - MATRIXTRANSPOSEPROC transpose_44; -} ssc_intrinsic; +} sn_intrinsic; typedef enum most_advanced_instruction { #ifdef _MSC_VER diff --git a/StgSharp.Native/sn-src/FactorialSIMD.c b/StgSharp.Native/sn-src/FactorialSIMD.c deleted file mode 100644 index 4f48b347..00000000 --- a/StgSharp.Native/sn-src/FactorialSIMD.c +++ /dev/null @@ -1,23 +0,0 @@ -#include "sn_internal.h" -#include "sn_intrinsic.h" - -INTERNAL __m128i facIncrement = { 4, 4, 4, 4 }; -INTERNAL __m128i facBase = { 1, 2, 3, 4 }; - -INTERNAL uint64_t SN_DECL factorial_simd_sse(int n) -{ - __m128i baseResult = facBase; - __m128i facBaseLocal = facBase; - const int cycle = (n - 3) >> 2; - const uint32_t rest = (cycle << 2) + 1; - for (size_t i = 0; i < cycle; i++) { - facBaseLocal = _mm_add_epi32(facBaseLocal, facIncrement); - baseResult = _mm_mullo_epi32(baseResult, facBaseLocal); - } - uint64_t result = (uint64_t)baseResult.m128i_i32[0] * (uint64_t)baseResult.m128i_i32[1] * - (uint64_t)baseResult.m128i_i32[2] * (uint64_t)baseResult.m128i_i32[3]; - for (uint64_t i = rest; i < n; i++) { - result *= i ; - } - return result; -} \ No newline at end of file diff --git a/StgSharp.Native/sn-src/hash.c b/StgSharp.Native/sn-src/hash.c deleted file mode 100644 index 89794198..00000000 --- a/StgSharp.Native/sn-src/hash.c +++ /dev/null @@ -1,45 +0,0 @@ -#include "sn_intrinsic.h" -#include "StgSharpNative.h" - -INTERNAL int SN_DECL city_hash_simplify_sse(byte const *str, int const length) -{ - uint32_t hash = length, size = length << 1; - byte *end = str + size, *ptr = str; - - if (size < 4) { - for (; ptr < end; ptr += 2) { - hash = _mm_crc32_u16(hash, *ptr); - } - } else if (size < 16) { - byte *last = end - 4; - for (; ptr < end; ptr += 4) { - hash = _mm_crc32_u32(hash, *(uint32_t *)ptr); - } - for (; ptr < end; ptr++) { - hash = _mm_crc32_u16(hash, *ptr); - } - } else { - byte *last_128 = end - 16; - byte *last_32 = end - 4; - uint64_t hash64 = size; - for (; ptr < last_128; ptr += 16) { - __m128i vec = _mm_loadu_si32((int *)ptr); - hash64 = _mm_crc32_u64(hash64, _mm_extract_epi64(vec, 0)); - hash64 = _mm_crc32_u64(hash64, _mm_extract_epi64(vec, 1)); - } - hash = _mm_crc32_u8(hash, hash64); - hash = _mm_crc32_u8(hash, hash64 >> 32); - for (; ptr < last_32; ptr += 4) { - hash = _mm_crc32_u32(hash,*(uint32_t *)ptr); - } - for (; ptr < end; ptr += 2) { - hash = _mm_crc32_u8(hash,*ptr); - } - } - hash ^= hash >> 16; - hash *= 0x85ebca6b; - hash ^= hash >> 13; - hash *= 0xc2b2ae35; - hash ^= hash >> 16; - return hash; -} diff --git a/StgSharp.Native/sn-src/intrinsic.c b/StgSharp.Native/sn-src/intrinsic.c deleted file mode 100644 index 684f415f..00000000 --- a/StgSharp.Native/sn-src/intrinsic.c +++ /dev/null @@ -1,146 +0,0 @@ -#include "StgSharpNative.h" -#include "sn_intrinsic.h" - - -#if defined(_MSC_VER) && !defined(__clang__) -#include -#include -#else -#include -#endif // _MSC_VER - -PRIVATE void load_intrinsic_sse(ssc_intrinsic *context); -PRIVATE void load_intrinsic_avx(ssc_intrinsic *context); -PRIVATE void load_intrinsic_512(ssc_intrinsic *context); - -PRIVATE int check_isa(most_advanced_instruction instruction_family) -{ - if (instruction_family != NEON) { -#if defined(_MSC_VER) && !defined(__clang__) - return __check_isa_support(instruction_family, 0); -#else - switch (instruction_family) { - case SSE: - return __builtin_cpu_supports("sse4.2"); - case AVX256: - return __builtin_cpu_supports("avx2"); - case AVX512: - return __builtin_cpu_supports("avx512f"); - default: - return 0; - } -#endif // _MSC_VER - } - -#if defined(__ARM_NEON) || defined(__ARM_NEON__) - return 1; -#else - return 0; -#endif -} - -SN_API int load_intrinsic_function(void *intrinsic_context) -{ - sn_null_assert(intrinsic_context); - - ssc_intrinsic *context = (ssc_intrinsic *)intrinsic_context; - - if (check_isa(AVX512)) { - load_intrinsic_512(context); - return 3; - } else if (check_isa(AVX256)) { - load_intrinsic_avx(context); - return 2; - } else if (check_isa(SSE)) { - load_intrinsic_sse(context); - return 1; - } else if (check_isa(NEON)) { - return -1; - } - - return 0; -} - -void load_intrinsic_sse(ssc_intrinsic *context) -{ - context->add_m2 = addmatrix2_sse; - context->add_m3 = addmatrix3_sse; - context->add_m4 = addmatrix4_sse; - context->transpose_23 = transpose23; - context->transpose_24 = transpose24; - context->transpose_32 = transpose32; - context->transpose_33 = transpose33; - context->transpose_34 = transpose34; - context->transpose_42 = transpose42; - context->transpose_43 = transpose43; - context->transpose_44 = transpose44; - context->normalize_3 = normalize; - context->det_matrix33 = det_mat3; - context->det_matrix44 = det_mat4; - context->dot_31 = dot_31_sse; - context->dot_32 = dot_32_sse; - context->dot_41 = dot_41_sse; - context->dot_42 = dot_42_sse; - context->dot_43 = dot_43_sse; - context->city_hash_simplify = city_hash_simplify_sse; - context->index_pair = index_pair_sse; - context->sub_m2 = submatrix2_sse; - context->sub_m3 = submatrix3_sse; - context->sub_m4 = submatrix4_sse; - context->factorial_simd = factorial_simd_sse; - context->fma_44 = fma_44_sse; -} - -void load_intrinsic_avx(ssc_intrinsic *context) -{ - context->add_m2 = addmatrix2_avx; - context->add_m3 = addmatrix3_avx; - context->add_m4 = addmatrix4_avx; - context->transpose_23 = transpose23; - context->transpose_24 = transpose24; - context->transpose_32 = transpose32; - context->transpose_33 = transpose33; - context->transpose_34 = transpose34; - context->transpose_42 = transpose42; - context->transpose_43 = transpose43; - context->transpose_44 = transpose44; - context->normalize_3 = normalize; - context->det_matrix33 = det_mat3; - context->det_matrix44 = det_mat4; - context->dot_31 = dot_31_avx; - context->dot_32 = dot_32_avx; - context->dot_41 = dot_41_avx; - context->dot_42 = dot_42_avx; - context->dot_43 = dot_43_avx; - context->city_hash_simplify = city_hash_simplify_sse; - context->index_pair = index_pair_sse; - context->factorial_simd = factorial_simd_sse; - context->fma_44 = fma_44_sse; -} - -void load_intrinsic_512(ssc_intrinsic *context) -{ - context->add_m2 = addmatrix2_avx; - context->add_m3 = addmatrix3_512; - context->add_m4 = addmatrix4_512; - context->transpose_23 = transpose23; - context->transpose_24 = transpose24; - context->transpose_32 = transpose32; - context->transpose_33 = transpose33; - context->transpose_34 = transpose34; - context->transpose_42 = transpose42; - context->transpose_43 = transpose43; - context->transpose_44 = transpose44; - context->normalize_3 = normalize; - context->det_matrix33 = det_mat3; - context->det_matrix44 = det_mat4; - context->dot_31 = dot_31_avx; - context->dot_32 = dot_32_avx; - context->dot_41 = dot_41_avx; - context->dot_42 = dot_42_avx; - context->dot_43 = dot_43_avx; - context->city_hash_simplify = city_hash_simplify_sse; - context->index_pair = index_pair_sse; - context->factorial_simd = factorial_simd_sse; - context->fma_44 = fma_44_sse; -} diff --git a/StgSharp.Native/sn-src/mat2.c b/StgSharp.Native/sn-src/mat2.c deleted file mode 100644 index 1f840270..00000000 --- a/StgSharp.Native/sn-src/mat2.c +++ /dev/null @@ -1,57 +0,0 @@ -#include "StgSharpNative.h" -#include "sn_intrinsic.h" - -INTERNAL void __cdecl transpose23(__2_columnset *source, __3_columnset *target) -{ - __m128 t0 = _mm_shuffle_ps(ALIGN(source->column[0]), ALIGN(source->column[1]), - _MM_SHUFFLE(1, 0, 1, 0)); - __m128 t1 = _mm_shuffle_ps(ALIGN(source->column[0]), ALIGN(source->column[1]), - _MM_SHUFFLE(3, 2, 3, 2)); - - target->column[0] = _mm_shuffle_ps(t0, zero_vec, _MM_SHUFFLE(2, 0, 2, 0)); - target->column[1] = _mm_shuffle_ps(t0, zero_vec, _MM_SHUFFLE(3, 1, 3, 1)); - target->column[2] = _mm_shuffle_ps(t1, zero_vec, _MM_SHUFFLE(2, 0, 2, 0)); -} - -INTERNAL void __cdecl transpose24(__2_columnset *source, __4_columnset *target) -{ - __m128 t0 = _mm_shuffle_ps(ALIGN(source->column[0]), ALIGN(source->column[1]), - _MM_SHUFFLE(1, 0, 1, 0)); - __m128 t1 = _mm_shuffle_ps(ALIGN(source->column[0]), ALIGN(source->column[1]), - _MM_SHUFFLE(3, 2, 3, 2)); - - target->column[0] = _mm_shuffle_ps(t0, zero_vec, _MM_SHUFFLE(2, 0, 2, 0)); - target->column[1] = _mm_shuffle_ps(t0, zero_vec, _MM_SHUFFLE(3, 1, 3, 1)); - target->column[2] = _mm_shuffle_ps(t1, zero_vec, _MM_SHUFFLE(2, 0, 2, 0)); - target->column[3] = _mm_shuffle_ps(t1, zero_vec, _MM_SHUFFLE(3, 1, 3, 1)); -} - -INTERNAL void __cdecl addmatrix2_sse(__2_columnset *left, __2_columnset *right, __2_columnset *ans) -{ - __m128 temp0 = _mm_add_ps(ALIGN(left->column[0]), ALIGN(right->column[0])); - __m128 temp1 = _mm_add_ps(ALIGN(left->column[1]), ALIGN(right->column[1])); - - _mm_storeu_ps(&ans->m[0][0], temp0); - _mm_storeu_ps(&ans->m[0][1], temp1); -} - -INTERNAL void __cdecl addmatrix2_avx(__2_columnset *left, __2_columnset *right, __2_columnset *ans) -{ - __m256 temp = _mm256_add_ps(ALIGN256(left->column[0]), ALIGN256(right->column[0])); - _mm256_storeu_ps(&ans->m[0][0], temp); -} - -INTERNAL void __cdecl submatrix2_sse(__2_columnset *left, __2_columnset *right, __2_columnset *ans) -{ - __m128 temp0 = _mm_sub_ps(ALIGN(left->column[0]), ALIGN(right->column[0])); - __m128 temp1 = _mm_sub_ps(ALIGN(left->column[1]), ALIGN(right->column[1])); - - _mm_storeu_ps(&ans->m[0][0], temp0); - _mm_storeu_ps(&ans->m[0][1], temp1); -} - -INTERNAL void __cdecl submatrix2_avx(__2_columnset *left, __2_columnset *right, __2_columnset *ans) -{ - __m256 temp = _mm256_sub_ps(ALIGN256(left->column[0]), ALIGN256(right->column[0])); - _mm256_storeu_ps(&ans->m[0][0], temp); -} diff --git a/StgSharp.Native/sn-src/mat3.c b/StgSharp.Native/sn-src/mat3.c deleted file mode 100644 index dc918fe2..00000000 --- a/StgSharp.Native/sn-src/mat3.c +++ /dev/null @@ -1,130 +0,0 @@ -#include "StgSharpNative.h" -#include "sn_intrinsic.h" -#include - -INTERNAL void _cdecl transpose32(__3_columnset *source, __2_columnset *target) -{ - __m128 t0 = _mm_shuffle_ps(ALIGN(source->column[0]), ALIGN(source->column[1]), - _MM_SHUFFLE(1, 0, 1, 0)); - __m128 t2 = _mm_shuffle_ps(ALIGN(source->column[2]), zero_vec, _MM_SHUFFLE(1, 0, 1, 0)); - - target->column[0] = _mm_shuffle_ps(t0, t2, _MM_SHUFFLE(2, 0, 2, 0)); - target->column[1] = _mm_shuffle_ps(t0, t2, _MM_SHUFFLE(3, 1, 3, 1)); -} - -INTERNAL void _cdecl transpose33(__3_columnset *source, __3_columnset *target) -{ - __m128 t0 = _mm_shuffle_ps(ALIGN(source->column[0]), ALIGN(source->column[1]), - _MM_SHUFFLE(1, 0, 1, 0)); - __m128 t1 = _mm_shuffle_ps(ALIGN(source->column[0]), ALIGN(source->column[1]), - _MM_SHUFFLE(3, 2, 3, 2)); - __m128 t2 = _mm_shuffle_ps(ALIGN(source->column[2]), zero_vec, _MM_SHUFFLE(1, 0, 1, 0)); - __m128 t3 = _mm_shuffle_ps(ALIGN(source->column[2]), zero_vec, _MM_SHUFFLE(3, 2, 3, 2)); - - target->column[0] = _mm_shuffle_ps(t0, t2, _MM_SHUFFLE(2, 0, 2, 0)); - target->column[1] = _mm_shuffle_ps(t0, t2, _MM_SHUFFLE(3, 1, 3, 1)); - target->column[2] = _mm_shuffle_ps(t1, t3, _MM_SHUFFLE(2, 0, 2, 0)); -} - -INTERNAL void _cdecl transpose34(__3_columnset *source, __4_columnset *target) -{ - __m128 t0 = _mm_shuffle_ps(ALIGN(source->column[0]), ALIGN(source->column[1]), - _MM_SHUFFLE(1, 0, 1, 0)); - __m128 t1 = _mm_shuffle_ps(ALIGN(source->column[0]), ALIGN(source->column[1]), - _MM_SHUFFLE(3, 2, 3, 2)); - __m128 t2 = _mm_shuffle_ps(ALIGN(source->column[2]), zero_vec, _MM_SHUFFLE(1, 0, 1, 0)); - __m128 t3 = _mm_shuffle_ps(ALIGN(source->column[2]), zero_vec, _MM_SHUFFLE(3, 2, 3, 2)); - - target->column[0] = _mm_shuffle_ps(t0, t2, _MM_SHUFFLE(2, 0, 2, 0)); - target->column[1] = _mm_shuffle_ps(t0, t2, _MM_SHUFFLE(3, 1, 3, 1)); - target->column[2] = _mm_shuffle_ps(t1, t3, _MM_SHUFFLE(2, 0, 2, 0)); - target->column[3] = _mm_shuffle_ps(t1, t3, _MM_SHUFFLE(3, 1, 3, 1)); -} - -INTERNAL float _cdecl det_mat3_internal(__m128 const c0, __m128 const c1, __m128 const c2) -{ - //let adding row aligned - __m128 addc1 = _mm_shuffle_ps(c1, c1, _MM_SHUFFLE(3, 0, 2, 1)); - __m128 addc2 = _mm_shuffle_ps(c2, c2, _MM_SHUFFLE(3, 1, 0, 2)); - - //let sub row aligned - __m128 subc1 = _mm_shuffle_ps(c1, c1, _MM_SHUFFLE(3, 1, 0, 2)); - __m128 subc2 = _mm_shuffle_ps(c2, c2, _MM_SHUFFLE(3, 0, 2, 1)); - - //multple all add row - __m128 cache = _mm_mul_ps(c0, addc1); - cache = _mm_mul_ps(cache, addc2); - - //multiple all sub row - __m128 sub = _mm_mul_ps(c0, subc1); - sub = _mm_mul_ps(sub, subc2); - - //main-corner sub sub-corner - cache = _mm_sub_ps(cache, sub); - - __m128 ret = _mm_hadd_ps(cache, cache); //element 1 add element 2 - ret = _mm_add_ps(ret, cache); //(e1 add e2) add element 3 - - return ret.m128_f32[2]; -} - -INTERNAL float _cdecl det_mat3(__3_columnset *source, __3_columnset *transpose) -{ - return det_mat3_internal(source->column[0], source->column[1], source->column[2]); -} - -#pragma region add - -INTERNAL void __cdecl addmatrix3_sse(__3_columnset *left, __3_columnset *right, __3_columnset *ans) -{ - __m128 temp0 = _mm_add_ps(ALIGN(left->column[0]), ALIGN(right->column[0])); - __m128 temp1 = _mm_add_ps(ALIGN(left->column[1]), ALIGN(right->column[1])); - __m128 temp2 = _mm_add_ps(ALIGN(left->column[2]), ALIGN(right->column[2])); - - _mm_storeu_ps(&ans->m[0][0], temp0); - _mm_storeu_ps(&ans->m[0][1], temp1); - _mm_storeu_ps(&ans->m[0][2], temp2); -} - -INTERNAL void __cdecl addmatrix3_avx(__3_columnset *left, __3_columnset *right, __3_columnset *ans) -{ - __m256 temp0 = _mm256_add_ps(ALIGN256(left->part), ALIGN256(right->part)); - __m128 temp1 = _mm_add_ps(ALIGN(left->column[2]), ALIGN(right->column[2])); - - _mm256_storeu_ps(&ans->m[0][0], temp0); - _mm_storeu_ps(&ans->m[0][2], temp1); -} - -INTERNAL void __cdecl addmatrix3_512(__3_columnset *left, __3_columnset *right, __3_columnset *ans) -{ - __m512 temp = _mm512_add_ps(_mm512_loadu_ps(left), _mm512_loadu_ps(right)); - _mm512_mask_store_ps(ans, 0x0fff, temp); -} - -INTERNAL void __cdecl submatrix3_sse(__3_columnset *left, __3_columnset *right, __3_columnset *ans) -{ - __m128 temp0 = _mm_sub_ps(ALIGN(left->column[0]), ALIGN(right->column[0])); - __m128 temp1 = _mm_sub_ps(ALIGN(left->column[1]), ALIGN(right->column[1])); - __m128 temp2 = _mm_sub_ps(ALIGN(left->column[2]), ALIGN(right->column[2])); - - _mm_storeu_ps(&ans->m[0][0], temp0); - _mm_storeu_ps(&ans->m[0][1], temp1); - _mm_storeu_ps(&ans->m[0][2], temp2); -} - -INTERNAL void __cdecl submatrix3_avx(__3_columnset *left, __3_columnset *right, __3_columnset *ans) -{ - __m256 temp0 = _mm256_sub_ps(ALIGN256(left->part), ALIGN256(right->part)); - __m128 temp1 = _mm_sub_ps(ALIGN(left->column[2]), ALIGN(right->column[2])); - - _mm256_storeu_ps(&ans->m[0][0], temp0); - _mm_storeu_ps(&ans->m[0][2], temp1); -} - -INTERNAL void __cdecl submatrix3_512(__3_columnset *left, __3_columnset *right, __3_columnset *ans) -{ - __m512 temp = _mm512_sub_ps(_mm512_loadu_ps(left), _mm512_loadu_ps(right)); - _mm512_mask_store_ps(ans, 0x0fff, temp); -} - -#pragma endregion diff --git a/StgSharp.Native/sn-src/mat4.c b/StgSharp.Native/sn-src/mat4.c deleted file mode 100644 index 26d612b9..00000000 --- a/StgSharp.Native/sn-src/mat4.c +++ /dev/null @@ -1,103 +0,0 @@ - -#include "StgSharpNative.h" -#include "sn_intrinsic.h" -#include -#include - -INTERNAL void __cdecl transpose42(__4_columnset const *source, __2_columnset *target) -{ - __m128 t0 = _mm_shuffle_ps(ALIGN(source->column[0]), ALIGN(source->column[1]), - _MM_SHUFFLE(1, 0, 1, 0)); - __m128 t2 = _mm_shuffle_ps(ALIGN(source->column[2]), ALIGN(source->column[3]), - _MM_SHUFFLE(1, 0, 1, 0)); - - target->column[0] = _mm_shuffle_ps(t0, t2, _MM_SHUFFLE(2, 0, 2, 0)); - target->column[1] = _mm_shuffle_ps(t0, t2, _MM_SHUFFLE(3, 1, 3, 1)); -} - -INTERNAL void __cdecl transpose43(__4_columnset const *source, __3_columnset *target) -{ - __m128 t0 = _mm_shuffle_ps(ALIGN(source->column[0]), ALIGN(source->column[1]), - _MM_SHUFFLE(1, 0, 1, 0)); - __m128 t1 = _mm_shuffle_ps(ALIGN(source->column[0]), ALIGN(source->column[1]), - _MM_SHUFFLE(3, 2, 3, 2)); - __m128 t2 = _mm_shuffle_ps(ALIGN(source->column[2]), ALIGN(source->column[3]), - _MM_SHUFFLE(1, 0, 1, 0)); - __m128 t3 = _mm_shuffle_ps(ALIGN(source->column[2]), ALIGN(source->column[3]), - _MM_SHUFFLE(3, 2, 3, 2)); - - target->column[0] = _mm_shuffle_ps(t0, t2, _MM_SHUFFLE(2, 0, 2, 0)); - target->column[1] = _mm_shuffle_ps(t0, t2, _MM_SHUFFLE(3, 1, 3, 1)); - target->column[2] = _mm_shuffle_ps(t1, t3, _MM_SHUFFLE(2, 0, 2, 0)); -} - -INTERNAL void __cdecl transpose44(__4_columnset const *source, __4_columnset *target) -{ - __m128 t0 = _mm_shuffle_ps(ALIGN(source->column[0]), ALIGN(source->column[1]), - _MM_SHUFFLE(1, 0, 1, 0)); - __m128 t1 = _mm_shuffle_ps(ALIGN(source->column[0]), ALIGN(source->column[1]), - _MM_SHUFFLE(3, 2, 3, 2)); - __m128 t2 = _mm_shuffle_ps(ALIGN(source->column[2]), ALIGN(source->column[3]), - _MM_SHUFFLE(1, 0, 1, 0)); - __m128 t3 = _mm_shuffle_ps(ALIGN(source->column[2]), ALIGN(source->column[3]), - _MM_SHUFFLE(3, 2, 3, 2)); - - target->column[0] = _mm_shuffle_ps(t0, t2, _MM_SHUFFLE(2, 0, 2, 0)); - target->column[1] = _mm_shuffle_ps(t0, t2, _MM_SHUFFLE(3, 1, 3, 1)); - target->column[2] = _mm_shuffle_ps(t1, t3, _MM_SHUFFLE(2, 0, 2, 0)); - target->column[3] = _mm_shuffle_ps(t1, t3, _MM_SHUFFLE(3, 1, 3, 1)); -} - -INTERNAL float __cdecl det_mat4(__4_columnset const *mat_ptr, __4_columnset const *transpose) -{ - __m128 mc0 = ALIGN(mat_ptr->column[0]); - __m128 mc1 = ALIGN(mat_ptr->column[1]); - __m128 mc2 = ALIGN(mat_ptr->column[2]); - __m128 mc3 = ALIGN(mat_ptr->column[3]); - - __m128 cogeneration = - _mm_set_ps(det_mat3_internal(mc0, mc1, mc2), det_mat3_internal(mc0, mc1, mc3), - det_mat3_internal(mc0, mc2, mc3), det_mat3_internal(mc1, mc2, mc3)); - __m128 retvec = _mm_mul_ps(ALIGN(transpose->column[3]), cogeneration); - retvec = _mm_hsub_ps(retvec, retvec); - retvec = _mm_hadd_ps(retvec, retvec); - return -retvec.m128_f32[0]; -} - -INTERNAL void SN_DECL addmatrix4_sse(__4_columnset *left, __4_columnset *right, __4_columnset *ans) -{ - ans->column[0] = _mm_add_ps(ALIGN(left->column[0]), ALIGN(right->column[0])); - ans->column[1] = _mm_add_ps(ALIGN(left->column[1]), ALIGN(right->column[1])); - ans->column[2] = _mm_add_ps(ALIGN(left->column[2]), ALIGN(right->column[2])); - ans->column[3] = _mm_add_ps(ALIGN(left->column[3]), ALIGN(right->column[3])); -} - -INTERNAL void SN_DECL addmatrix4_avx(__4_columnset *left, __4_columnset *right, __4_columnset *ans) -{ - ans->half[0] = _mm256_add_ps(ALIGN256(left->half[0]), ALIGN256(right->half[0])); - ans->half[1] = _mm256_add_ps(ALIGN256(left->half[1]), ALIGN256(right->half[1])); -} - -INTERNAL void SN_DECL addmatrix4_512(__4_columnset *left, __4_columnset *right, __4_columnset *ans) -{ - ans->stream = _mm512_add_ps(ALIGN512(left->stream), ALIGN512(right->stream)); -} - -INTERNAL void SN_DECL submatrix4_sse(__4_columnset *left, __4_columnset *right, __4_columnset *ans) -{ - ans->column[0] = _mm_sub_ps(ALIGN(left->column[0]), ALIGN(right->column[0])); - ans->column[1] = _mm_sub_ps(ALIGN(left->column[1]), ALIGN(right->column[1])); - ans->column[2] = _mm_sub_ps(ALIGN(left->column[2]), ALIGN(right->column[2])); - ans->column[3] = _mm_sub_ps(ALIGN(left->column[3]), ALIGN(right->column[3])); -} - -INTERNAL void SN_DECL submatrix4_avx(__4_columnset *left, __4_columnset *right, __4_columnset *ans) -{ - ans->half[0] = _mm256_sub_ps(ALIGN256(left->half[0]), ALIGN256(right->half[0])); - ans->half[1] = _mm256_sub_ps(ALIGN256(left->half[1]), ALIGN256(right->half[1])); -} - -INTERNAL void SN_DECL submatrix4_512(__4_columnset *left, __4_columnset *right, __4_columnset *ans) -{ - ans->stream = _mm512_sub_ps(ALIGN512(left->stream), ALIGN512(right->stream)); -} diff --git a/StgSharp.Native/sn-src/stgsharp_native.c b/StgSharp.Native/sn-src/stgsharp_native.c deleted file mode 100644 index 987a86dc..00000000 --- a/StgSharp.Native/sn-src/stgsharp_native.c +++ /dev/null @@ -1,75 +0,0 @@ -#include "StgSharpNative.h" -#include "gl.h" -#include "../glfw/glfw3.h" -#include -#include - -extern GladGLContext *currentContext; -extern char infolog[512]; - -SN_API int SN_DECL glCheckShaderStat(GladGLContext *context, uint64_t shaderHandle, int key, - char **logRef) -{ - int stat = 0; - context->GetShaderiv(shaderHandle, key, &stat); - if (!stat) { - context->GetShaderInfoLog(shaderHandle, 512, NULL, infolog); - } - *logRef = infolog; - return stat; -} - -SN_API void SN_DECL initGL(int majorVersion, int minorVersion) -{ - glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, majorVersion); - glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, minorVersion); - glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); - glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); -} - -SN_API GLFWglproc SN_DECL loadGlfuncDefault(char *procName) -{ - void *ret = glfwGetProcAddress(procName); - // printf("%llu\n", (uint64_t)ret); - if (ret == NULL) { - strcpy_s(infolog,30, "Failed to load OpenGL api:"); - for (size_t i = 0; i < sizeof(procName); i++) { - infolog[30 + i] = procName[i]; - } - //printf("%s\n", infolog); - } - return (GLFWglproc)ret; -} - -SN_API unsigned int linkShaderProgram(GladGLContext *context, GLuint shaderProgram) -{ - context->LinkProgram(shaderProgram); - - int success; - context->GetProgramiv(shaderProgram, GL_LINK_STATUS, &success); - if (!success) { - context->GetProgramInfoLog(shaderProgram, 512, NULL, infolog); - return 0; - } else { - return shaderProgram; - } -} - -SN_API void SN_DECL loadImageData(char *location, Image *out, imageLoader loader) -{ - if (loader == NULL) { - loader = stbi_load; - } - out->pixelPtr = loader(location, &(out->width), &(out->height), &(out->channel), 0); -} - -SN_API void SN_DECL unloadImageData(Image *out) -{ - free(out->pixelPtr); -} - -SN_API char *SN_DECL readLog() -{ - infolog[511] = '\0'; - return infolog; -} diff --git a/StgSharp.Native/sn-src/string.c b/StgSharp.Native/sn-src/string.c deleted file mode 100644 index 21efc77e..00000000 --- a/StgSharp.Native/sn-src/string.c +++ /dev/null @@ -1,53 +0,0 @@ -#include "sn_intrinsic.h" - -int index_pair_sse(short const *str, uint32_t target, int length) -{ - int index = 0, result = 0, size = length, end = size - 8; - __m128i s = { - target, - target, - target, - target, - }; - __m128i vec, com; - - while (index < end) { - vec = _mm_loadu_epi32(str + index); - com = _mm_cmpeq_epi16(vec, s); - result = _mm_movemask_epi8(com); - if (result) { - for (size_t i = 0; i < 7; i++) { - if (((result & 0xF) == 0xF)) { - return index + i; - } - if (result == 0) { - break; - } - result >>= 2; - } - } - index += 1; - vec = _mm_loadu_epi32(str + index); - com = _mm_cmpeq_epi16(vec, s); - result = _mm_movemask_epi8(com); - if (result) { - for (size_t i = 0; i < 7; i++) { - if (((result & 0xF) == 0xF)) { - return index + i; - } - if (result == 0) { - break; - } - result >>= 2; - } - } - index += 7; - } - for (; (index < size - 1) && (!result); index += 2) { - result = *(int *)(str + index) == target; - } - if (result) { - return index; - } - return -1; -}