diff --git a/Source.Common/BeamSegDraw.cs b/Source.Common/BeamSegDraw.cs index 6e537e52..9040b8e2 100644 --- a/Source.Common/BeamSegDraw.cs +++ b/Source.Common/BeamSegDraw.cs @@ -34,11 +34,25 @@ public void Start(IMatRenderContext renderContext, int segs, IMaterial? material SegsDrawn = 0; TotalSegs = segs; MeshVertCount = 0; + pMeshBuilder = ref Unsafe.NullRef(); IMesh mesh = renderContext.GetDynamicMesh(true, null, null, material); Mesh.Begin(mesh, MaterialPrimitiveType.TriangleStrip, (segs - 1) * 2); } + public BeamSegDraw() { } + + public BeamSegDraw(ref MeshBuilder meshBuilder, int meshVertCount) { + pMeshBuilder = ref meshBuilder; + MeshVertCount = meshVertCount; + } + + public void StartWithMeshBuilder(IMatRenderContext renderContext, int segs) { + RenderContext = renderContext; + SegsDrawn = 0; + TotalSegs = segs; + } + public void NextSeg(ref BeamSeg seg) { RenderContext.GetWorldSpaceCameraPosition(out Vector3 vecCameraPos); @@ -90,25 +104,46 @@ public void SpecifySeg(in Vector3 vecCameraPos, in Vector3 vNormal) { MathLib.VectorMA(Seg.Pos, -Seg.Width * 0.5f, vNormal, out vPoint2); - // Specify the points. - Mesh.Position3fv(vPoint1); - Mesh.Color4f(Seg.Color, Seg.Alpha); - Mesh.TexCoord2f(0, 0, Seg.TexCoord); - Mesh.TexCoord2f(1, 0, Seg.TexCoord); - Mesh.TangentS3fv(vNormal); - Mesh.TangentT3fv(vTangentY); - Mesh.AdvanceVertex(); - - Mesh.Position3fv(vPoint2); - Mesh.Color4f(Seg.Color, Seg.Alpha); - Mesh.TexCoord2f(0, 1, Seg.TexCoord); - Mesh.TexCoord2f(1, 1, Seg.TexCoord); - Mesh.TangentS3fv(vNormal); - Mesh.TangentT3fv(vTangentY); - Mesh.AdvanceVertex(); + if (Unsafe.IsNullRef(ref pMeshBuilder)) { + EmitSegVerts(ref Mesh, in Seg, in vPoint1, in vPoint2, in vNormal, in vTangentY); + return; + } + + EmitSegVerts(ref pMeshBuilder, in Seg, in vPoint1, in vPoint2, in vNormal, in vTangentY); + if (SegsDrawn > 1) { + int nBase = ((SegsDrawn - 2) * 2) + MeshVertCount; + pMeshBuilder.FastIndex((ushort)nBase); + pMeshBuilder.FastIndex((ushort)(nBase + 1)); + pMeshBuilder.FastIndex((ushort)(nBase + 2)); + pMeshBuilder.FastIndex((ushort)(nBase + 1)); + pMeshBuilder.FastIndex((ushort)(nBase + 3)); + pMeshBuilder.FastIndex((ushort)(nBase + 2)); + } + } + + static void EmitSegVerts(ref MeshBuilder mesh, in BeamSeg seg, in Vector3 vPoint1, in Vector3 vPoint2, in Vector3 vNormal, in Vector3 vTangentY) { + mesh.Position3fv(vPoint1); + mesh.Color4f(seg.Color, seg.Alpha); + mesh.TexCoord2f(0, 0, seg.TexCoord); + mesh.TexCoord2f(1, 0, seg.TexCoord); + mesh.TangentS3fv(vNormal); + mesh.TangentT3fv(vTangentY); + mesh.AdvanceVertex(); + + mesh.Position3fv(vPoint2); + mesh.Color4f(seg.Color, seg.Alpha); + mesh.TexCoord2f(0, 1, seg.TexCoord); + mesh.TexCoord2f(1, 1, seg.TexCoord); + mesh.TangentS3fv(vNormal); + mesh.TangentT3fv(vTangentY); + mesh.AdvanceVertex(); } public void End() { + if (!Unsafe.IsNullRef(ref pMeshBuilder)) { + pMeshBuilder = ref Unsafe.NullRef(); + return; + } Mesh.End(false, true); } } diff --git a/Source.Common/Mathematics/NoiseSIMD.cs b/Source.Common/Mathematics/NoiseSIMD.cs new file mode 100644 index 00000000..205785cb --- /dev/null +++ b/Source.Common/Mathematics/NoiseSIMD.cs @@ -0,0 +1,211 @@ +using System.Numerics; + +namespace Source.Common.Mathematics; + +public static class NoiseSIMD +{ + static readonly int[] PermA = [ + 66, 147, 106, 213, 89, 115, 239, 25, 171, 175, 9, 114, 141, 226, 118, 128, 41, 208, 4, 56, 180, 248, 43, 82, 246, 219, 94, 245, 133, 131, 222, 103, + 160, 130, 168, 145, 238, 38, 23, 6, 236, 67, 99, 2, 70, 232, 80, 209, 1, 3, 68, 65, 102, 210, 13, 73, 55, 252, 187, 170, 22, 36, 52, 181, + 117, 163, 46, 79, 166, 224, 148, 75, 113, 95, 156, 185, 220, 164, 51, 142, 161, 35, 206, 251, 45, 136, 197, 190, 132, 32, 218, 127, 63, 27, 137, 93, + 242, 20, 189, 108, 183, 122, 139, 191, 249, 253, 87, 98, 69, 0, 144, 64, 24, 214, 97, 116, 158, 42, 107, 15, 53, 212, 83, 111, 152, 240, 74, 237, + 62, 77, 205, 149, 26, 151, 178, 204, 91, 176, 234, 49, 154, 203, 33, 221, 125, 134, 165, 124, 86, 39, 37, 60, 150, 157, 179, 109, 110, 44, 159, 153, + 5, 100, 10, 207, 40, 186, 96, 215, 143, 162, 230, 184, 101, 54, 174, 247, 76, 59, 241, 223, 192, 84, 104, 78, 169, 146, 138, 30, 48, 85, 233, 19, + 29, 92, 126, 17, 199, 250, 31, 81, 188, 225, 28, 112, 88, 11, 182, 173, 211, 129, 194, 172, 14, 120, 200, 167, 135, 12, 177, 227, 229, 155, 201, 61, + 105, 195, 193, 244, 235, 58, 8, 196, 123, 254, 16, 18, 50, 121, 71, 243, 90, 57, 202, 119, 255, 47, 7, 198, 228, 21, 217, 216, 231, 140, 72, 34, + ]; + + static readonly int[] PermB = [ + 123, 108, 201, 64, 40, 75, 24, 221, 137, 110, 191, 142, 9, 69, 230, 83, 7, 247, 51, 54, 115, 133, 180, 248, 109, 116, 62, 99, 251, 55, 89, 253, + 65, 106, 228, 167, 131, 132, 58, 143, 97, 102, 163, 202, 149, 234, 12, 117, 174, 94, 121, 74, 32, 113, 20, 60, 159, 182, 204, 29, 244, 118, 3, 178, + 255, 38, 6, 114, 36, 93, 30, 134, 213, 90, 245, 209, 88, 232, 162, 125, 84, 166, 70, 136, 208, 231, 27, 71, 157, 80, 76, 0, 170, 225, 203, 176, + 33, 161, 196, 128, 252, 236, 246, 2, 138, 1, 250, 197, 77, 243, 218, 242, 19, 164, 68, 212, 14, 237, 144, 63, 46, 103, 177, 188, 85, 223, 8, 160, + 222, 4, 216, 219, 35, 15, 44, 23, 126, 127, 100, 226, 235, 37, 168, 101, 49, 22, 11, 73, 61, 135, 111, 183, 72, 96, 185, 239, 82, 18, 50, 155, + 186, 153, 17, 233, 146, 156, 107, 5, 254, 10, 192, 198, 148, 207, 104, 13, 124, 48, 95, 129, 120, 206, 199, 81, 249, 91, 150, 210, 119, 240, 122, 194, + 92, 34, 28, 205, 175, 227, 179, 220, 140, 152, 79, 26, 195, 47, 66, 173, 169, 241, 53, 184, 187, 145, 112, 238, 214, 147, 98, 171, 229, 200, 151, 25, + 67, 78, 189, 217, 130, 224, 57, 172, 59, 41, 43, 16, 105, 158, 165, 21, 45, 56, 141, 139, 215, 190, 86, 42, 52, 39, 87, 181, 31, 154, 193, 211, + ]; + + static readonly int[] PermC = [ + 97, 65, 96, 25, 122, 26, 219, 85, 148, 251, 102, 0, 140, 130, 136, 213, 138, 60, 236, 52, 178, 131, 115, 183, 144, 78, 147, 168, 39, 45, 169, 70, + 57, 146, 67, 142, 252, 216, 28, 54, 86, 222, 194, 200, 48, 5, 205, 125, 214, 56, 181, 255, 196, 155, 37, 218, 153, 208, 66, 242, 73, 248, 206, 61, + 62, 246, 177, 2, 197, 107, 162, 152, 89, 41, 6, 160, 94, 8, 201, 38, 235, 228, 165, 93, 111, 239, 74, 231, 121, 47, 166, 221, 157, 64, 77, 244, + 29, 105, 150, 123, 190, 191, 225, 118, 133, 42, 10, 84, 185, 159, 124, 132, 240, 180, 44, 1, 9, 19, 99, 254, 12, 207, 186, 71, 234, 184, 11, 20, + 16, 193, 139, 175, 98, 59, 113, 27, 170, 230, 91, 187, 46, 156, 249, 108, 195, 171, 114, 14, 188, 82, 192, 233, 24, 32, 241, 87, 164, 90, 43, 163, + 245, 92, 40, 215, 55, 226, 15, 3, 112, 158, 250, 172, 22, 227, 137, 35, 128, 145, 247, 161, 119, 80, 217, 189, 81, 7, 63, 202, 120, 223, 83, 179, + 4, 106, 199, 229, 95, 53, 50, 33, 182, 72, 143, 23, 243, 75, 18, 173, 141, 167, 198, 204, 58, 174, 237, 17, 129, 238, 127, 31, 101, 176, 36, 30, + 110, 209, 34, 203, 135, 232, 68, 149, 49, 134, 126, 212, 79, 76, 117, 104, 210, 211, 224, 253, 100, 220, 109, 116, 88, 13, 151, 154, 69, 21, 51, 103, + ]; + + static readonly float[] ImpulseXCoords = [ + 0.788235f, 0.541176f, 0.972549f, 0.082353f, 0.352941f, 0.811765f, 0.286275f, 0.752941f, 0.203922f, 0.705882f, 0.537255f, 0.886275f, 0.580392f, 0.137255f, 0.8f, 0.533333f, + 0.117647f, 0.447059f, 0.129412f, 0.92549f, 0.086275f, 0.478431f, 0.666667f, 0.568627f, 0.678431f, 0.313725f, 0.321569f, 0.34902f, 0.988235f, 0.419608f, 0.898039f, 0.219608f, + 0.243137f, 0.623529f, 0.501961f, 0.772549f, 0.952941f, 0.517647f, 0.94902f, 0.701961f, 0.454902f, 0.505882f, 0.564706f, 0.960784f, 0.207843f, 0.007843f, 0.831373f, 0.184314f, + 0.576471f, 0.462745f, 0.572549f, 0.247059f, 0.262745f, 0.694118f, 0.615686f, 0.121569f, 0.384314f, 0.74902f, 0.145098f, 0.717647f, 0.415686f, 0.607843f, 0.105882f, 0.101961f, + 0.2f, 0.807843f, 0.521569f, 0.780392f, 0.466667f, 0.552941f, 0.996078f, 0.627451f, 0.992157f, 0.529412f, 0.407843f, 0.011765f, 0.709804f, 0.458824f, 0.058824f, 0.819608f, + 0.176471f, 0.317647f, 0.392157f, 0.223529f, 0.156863f, 0.490196f, 0.32549f, 0.07451f, 0.239216f, 0.164706f, 0.890196f, 0.603922f, 0.921569f, 0.839216f, 0.854902f, 0.098039f, + 0.686275f, 0.843137f, 0.152941f, 0.372549f, 0.062745f, 0.47451f, 0.486275f, 0.227451f, 0.4f, 0.298039f, 0.309804f, 0.27451f, 0.054902f, 0.815686f, 0.647059f, 0.635294f, + 0.662745f, 0.976471f, 0.094118f, 0.509804f, 0.65098f, 0.211765f, 0.180392f, 0.003922f, 0.827451f, 0.278431f, 0.023529f, 0.52549f, 0.45098f, 0.72549f, 0.690196f, 0.941176f, + 0.639216f, 0.560784f, 0.196078f, 0.364706f, 0.043137f, 0.494118f, 0.796078f, 0.113725f, 0.760784f, 0.729412f, 0.258824f, 0.290196f, 0.584314f, 0.67451f, 0.823529f, 0.905882f, + 0.917647f, 0.070588f, 0.862745f, 0.345098f, 0.913725f, 0.937255f, 0.031373f, 0.215686f, 0.768627f, 0.333333f, 0.411765f, 0.423529f, 0.945098f, 0.721569f, 0.039216f, 0.792157f, + 0.956863f, 0.266667f, 0.254902f, 0.047059f, 0.294118f, 0.658824f, 0.25098f, 1f, 0.984314f, 0.756863f, 0.027451f, 0.305882f, 0.835294f, 0.513725f, 0.360784f, 0.776471f, + 0.611765f, 0.192157f, 0.866667f, 0.858824f, 0.592157f, 0.803922f, 0.141176f, 0.435294f, 0.588235f, 0.619608f, 0.341176f, 0.109804f, 0.356863f, 0.270588f, 0.737255f, 0.847059f, + 0.05098f, 0.764706f, 0.019608f, 0.870588f, 0.933333f, 0.784314f, 0.54902f, 0.337255f, 0.631373f, 0.929412f, 0.231373f, 0.427451f, 0.078431f, 0.498039f, 0.968627f, 0.654902f, + 0.12549f, 0.698039f, 0.015686f, 0.878431f, 0.713725f, 0.368627f, 0.431373f, 0.87451f, 0.403922f, 0.556863f, 0.443137f, 0.964706f, 0.909804f, 0.301961f, 0.035294f, 0.85098f, + 0.882353f, 0.741176f, 0.380392f, 0.133333f, 0.470588f, 0.643137f, 0.282353f, 0.396078f, 0.980392f, 0.168627f, 0.14902f, 0.235294f, 0.670588f, 0.596078f, 0.733333f, 0.160784f, + 0.376471f, 0.682353f, 0.545098f, 0.482353f, 0.745098f, 0.894118f, 0.188235f, 0.329412f, 0.439216f, 0.901961f, 0f, 0.6f, 0.388235f, 0.172549f, 0.090196f, 0.066667f, + ]; + + static float GetLatticePointValue(int x, int y, int z) { + int idx = PermA[x & 0xff]; + idx = PermB[(y + idx) & 0xff]; + idx = PermC[(z + idx) & 0xff]; + return ImpulseXCoords[idx]; + } + + public static float Noise(float x, float y, float z) { + int xi = BitConverter.SingleToInt32Bits(x + 32768.0f) & 0xffff; + int yi = BitConverter.SingleToInt32Bits(y + 32768.0f) & 0xffff; + int zi = BitConverter.SingleToInt32Bits(z + 32768.0f) & 0xffff; + + float xFrac = (float)((xi & 0xff) * (1.0 / 256.0)); + float yFrac = (float)((yi & 0xff) * (1.0 / 256.0)); + float zFrac = (float)((zi & 0xff) * (1.0 / 256.0)); + xi >>= 8; + yi >>= 8; + zi >>= 8; + + float l000 = GetLatticePointValue(xi, yi, zi); + float l001 = GetLatticePointValue(xi, yi, zi + 1); + float l010 = GetLatticePointValue(xi, yi + 1, zi); + float l011 = GetLatticePointValue(xi, yi + 1, zi + 1); + float l100 = GetLatticePointValue(xi + 1, yi, zi); + float l101 = GetLatticePointValue(xi + 1, yi, zi + 1); + float l110 = GetLatticePointValue(xi + 1, yi + 1, zi); + float l111 = GetLatticePointValue(xi + 1, yi + 1, zi + 1); + + float l2d00 = l000 + xFrac * (l100 - l000); + float l2d01 = l001 + xFrac * (l101 - l001); + float l2d10 = l010 + xFrac * (l110 - l010); + float l2d11 = l011 + xFrac * (l111 - l011); + + float l1d0 = l2d00 + yFrac * (l2d10 - l2d00); + float l1d1 = l2d01 + yFrac * (l2d11 - l2d01); + + float result = l1d0 + zFrac * (l1d1 - l1d0); + return (result - 0.5f) * 2.0f; + } + + public static float Noise(in Vector3 v) => Noise(v.X, v.Y, v.Z); + + public static Vector4 Noise(in FourVectors v) + => new(Noise(v.x.X, v.y.X, v.z.X), Noise(v.x.Y, v.y.Y, v.z.Y), Noise(v.x.Z, v.y.Z, v.z.Z), Noise(v.x.W, v.y.W, v.z.W)); + + static Vector3 GetLatticePointVector(int x, int y, int z) { + int idx = PermA[x & 0xff]; + idx = PermB[(y + idx) & 0xff]; + idx = PermC[(z + idx) & 0xff] * 3; + return new Vector3(ImpulseVectors[idx], ImpulseVectors[idx + 1], ImpulseVectors[idx + 2]); + } + + public static Vector3 DNoise(float x, float y, float z) { + int xi = BitConverter.SingleToInt32Bits(x + 32768.0f) & 0xffff; + int yi = BitConverter.SingleToInt32Bits(y + 32768.0f) & 0xffff; + int zi = BitConverter.SingleToInt32Bits(z + 32768.0f) & 0xffff; + + float xFrac = (float)((xi & 0xff) * (1.0 / 256.0)); + float yFrac = (float)((yi & 0xff) * (1.0 / 256.0)); + float zFrac = (float)((zi & 0xff) * (1.0 / 256.0)); + xi >>= 8; + yi >>= 8; + zi >>= 8; + + Vector3 l000 = GetLatticePointVector(xi, yi, zi); + Vector3 l001 = GetLatticePointVector(xi, yi, zi + 1); + Vector3 l010 = GetLatticePointVector(xi, yi + 1, zi); + Vector3 l011 = GetLatticePointVector(xi, yi + 1, zi + 1); + Vector3 l100 = GetLatticePointVector(xi + 1, yi, zi); + Vector3 l101 = GetLatticePointVector(xi + 1, yi, zi + 1); + Vector3 l110 = GetLatticePointVector(xi + 1, yi + 1, zi); + Vector3 l111 = GetLatticePointVector(xi + 1, yi + 1, zi + 1); + + Vector3 l2d00 = l000 + xFrac * (l100 - l000); + Vector3 l2d01 = l001 + xFrac * (l101 - l001); + Vector3 l2d10 = l010 + xFrac * (l110 - l010); + Vector3 l2d11 = l011 + xFrac * (l111 - l011); + + Vector3 l1d0 = l2d00 + yFrac * (l2d10 - l2d00); + Vector3 l1d1 = l2d01 + yFrac * (l2d11 - l2d01); + + return l1d0 + zFrac * (l1d1 - l1d0); + } + + public static Vector3 DNoise(in Vector3 v) => DNoise(v.X, v.Y, v.Z); + + static readonly float[] ImpulseVectors = [ + -0.460087f, -0.887463f, -0.058594f, -0.458151f, 0.861646f, -0.430176f, -0.930437f, 0.316048f, -0.195496f, -0.883558f, -0.393287f, -0.27655f, + 0.171025f, -0.983455f, -0.329712f, -0.033573f, -0.941867f, -0.994995f, -0.476492f, 0.014764f, 0.87915f, 0.834786f, -0.454571f, 0.348755f, + -0.585801f, -0.782531f, -0.338745f, 0.97399f, -0.023774f, 0.225403f, -0.989659f, -0.011313f, -0.143005f, 0.507109f, -0.838016f, -0.369141f, + -0.609995f, -0.766277f, 0.314087f, 0.429987f, 0.59985f, -0.843323f, 0.089587f, -0.904071f, -0.977783f, -0.306997f, -0.901432f, 0.705078f, + 0.031606f, 0.994782f, -0.950806f, 0.797663f, -0.161508f, -0.588806f, 0.811569f, -0.50536f, 0.339783f, 0.93613f, -0.114223f, 0.334778f, + 0.21728f, -0.970264f, 0.440674f, 0.600976f, -0.712375f, -0.516418f, 0.197935f, 0.97926f, 0.213501f, 0.002956f, 0.999995f, -0.268127f, + -0.912763f, 0.084651f, -0.401062f, -0.193271f, -0.945607f, -0.804382f, 0.66248f, 0.640156f, -0.506348f, 0.363459f, -0.884439f, 0.627197f, + -0.433415f, 0.685363f, 0.803589f, -0.721652f, 0.416952f, -0.607971f, 0.647676f, 0.2967f, 0.734863f, 0.72304f, -0.444294f, 0.590454f, + -0.716318f, -0.420435f, -0.61377f, -0.039076f, -0.996459f, 0.885437f, 0.175225f, -0.969092f, 0.703918f, 0.116952f, -0.991832f, -0.399048f, + -0.504674f, -0.013997f, 0.863281f, -0.436364f, -0.817916f, 0.651733f, 0.09803f, -0.99509f, 0.137573f, 0.637157f, -0.766031f, -0.132263f, + -0.594718f, 0.583153f, -0.681213f, -0.625632f, 0.419913f, -0.724426f, -0.607341f, -0.394521f, 0.750427f, -0.312161f, 0.698925f, 0.899719f, + 0.101228f, -0.927363f, -0.962708f, -0.934241f, 0.041214f, -0.354553f, -0.826005f, -0.284775f, -0.507446f, -0.363751f, -0.929287f, -0.173584f, + -0.141266f, 0.983869f, -0.613525f, -0.436139f, -0.074329f, 0.899292f, -0.875355f, -0.480839f, 0.057556f, 0.250714f, 0.07127f, 0.967896f, + 0.182131f, 0.811467f, 0.950195f, -0.687696f, -0.66857f, -0.380554f, 0.785175f, -0.540171f, -0.359863f, 0.399774f, 0.848526f, 0.655151f, + -0.412243f, -0.004602f, 0.911072f, -0.132187f, -0.990485f, 0.278198f, 0.212421f, 0.764179f, 0.944214f, -0.694878f, 0.234042f, -0.699402f, + 0.404273f, 0.904644f, -0.316406f, 0.358393f, 0.087135f, 0.933044f, -0.473398f, 0.820774f, -0.559692f, 0.044667f, -0.997938f, 0.718201f, + 0.603896f, -0.046386f, 0.79657f, -0.968822f, 0.180966f, 0.172058f, -0.458206f, 0.886932f, -0.126221f, -0.656709f, -0.410319f, 0.693848f, + 0.999495f, -0.018023f, 0.026184f, -0.486069f, -0.740178f, -0.690979f, 0.942399f, -0.333819f, 0.022461f, -0.294545f, 0.867619f, 0.805664f, + 0.886791f, -0.416081f, -0.221252f, -0.797187f, 0.587661f, -0.171021f, -0.617708f, -0.762817f, -0.295654f, 0.449351f, -0.85366f, -0.505615f, + 0.065153f, -0.995535f, 0.723572f, 0.996518f, 0.0f, 0.083374f, 0.263346f, 0.088663f, -0.964417f, -0.221316f, -0.970864f, 0.383423f, + -0.51256f, 0.718804f, 0.675598f, 0.588859f, 0.406293f, -0.764648f, -0.803841f, -0.592769f, -0.061646f, 0.860199f, 0.492898f, -0.15033f, + -0.351871f, 0.858024f, 0.728455f, 0.515724f, -0.815149f, 0.455322f, -0.122322f, -0.960484f, 0.898254f, -0.52902f, 0.844443f, -0.156799f, + 0.530671f, -0.725304f, 0.637024f, -0.748915f, -0.248928f, -0.634094f, -0.188099f, 0.584087f, 0.972778f, 0.974165f, 0.222094f, -0.041992f, + 0.595326f, -0.701663f, -0.549438f, -0.060279f, -0.998047f, -0.262451f, -0.191682f, -0.782292f, -0.951477f, 0.528851f, -0.596315f, 0.752319f, + 0.612134f, 0.639567f, -0.604919f, 0.882803f, 0.200541f, 0.433594f, -0.936278f, -0.03949f, 0.349304f, 0.940848f, -0.121649f, 0.318604f, + -0.115022f, 0.048685f, -0.993347f, -0.324162f, -0.935726f, -0.394226f, -0.937457f, -0.294685f, 0.193909f, 0.894463f, -0.437237f, 0.104065f, + -0.861852f, -0.165102f, -0.486206f, -0.98048f, -0.139899f, 0.139526f, -0.024496f, 0.96075f, -0.996094f, -0.69976f, 0.714256f, -0.01886f, + 0.538575f, -0.792107f, 0.470581f, 0.309926f, -0.94372f, 0.349182f, 0.525671f, -0.77228f, 0.561523f, -0.793079f, 0.268745f, 0.567505f, + 0.697504f, -0.421131f, 0.639221f, -0.737871f, 0.672553f, -0.07666f, -0.390769f, -0.894942f, -0.482666f, -0.593469f, 0.191892f, 0.796448f, + 0.439379f, -0.896646f, 0.123108f, 0.337698f, -0.703709f, -0.879822f, -0.654687f, 0.749517f, 0.148071f, -0.48207f, -0.700569f, 0.737305f, + 0.626971f, 0.761948f, -0.25061f, 0.616585f, 0.015339f, -0.787231f, -0.175877f, -0.982f, 0.364624f, 0.891483f, -0.324585f, -0.334167f, + 0.858029f, 0.438272f, -0.297913f, 0.949369f, 0.258757f, 0.184448f, 0.105948f, -0.901183f, 0.969666f, -0.261581f, 0.943276f, -0.615845f, + -0.682063f, -0.528339f, -0.59552f, -0.810856f, 0.514103f, -0.32605f, -0.163757f, 0.986118f, 0.165527f, -0.595927f, -0.221907f, 0.791504f, + -0.160374f, -0.977354f, 0.652405f, -0.428837f, 0.641628f, -0.829102f, -0.634149f, -0.486378f, -0.687927f, -0.093271f, -0.995222f, -0.295654f, + 0.988659f, -0.150144f, -0.003357f, 0.730821f, -0.497396f, -0.538818f, -0.781913f, -0.62126f, -0.065674f, -0.655884f, -0.753313f, -0.073486f, + 0.845542f, -0.409094f, 0.375977f, -0.630041f, -0.514925f, -0.678101f, 0.205571f, 0.978634f, -0.019531f, 0.582841f, 0.763684f, -0.430054f, + 0.685084f, -0.728464f, 0.0f, -0.241437f, -0.95843f, -0.532898f, 0.741884f, 0.020899f, -0.670349f, 0.740273f, -0.318412f, 0.624634f, + -0.738068f, -0.539041f, 0.481812f, -0.965798f, -0.034508f, -0.257141f, 0.495184f, 0.805372f, 0.549683f, -0.572524f, 0.809558f, -0.221008f, + -0.537181f, 0.834652f, 0.220825f, -0.899741f, 0.097826f, -0.427368f, -0.370148f, 0.494066f, 0.904846f, 0.711387f, 0.577688f, 0.490356f, + 0.183324f, -0.722791f, -0.964172f, 0.552815f, -0.807753f, -0.347351f, -0.09605f, 0.994565f, -0.386047f, -0.884907f, 0.369536f, 0.305115f, + -0.832976f, -0.551898f, 0.047363f, 0.338883f, 0.641922f, 0.897034f, 0.805354f, 0.506187f, 0.357727f, -0.040128f, 0.998805f, -0.570923f, + 0.466918f, -0.602455f, 0.811035f, 0.139166f, -0.983697f, 0.633362f, -0.253765f, -0.340498f, -0.962891f, -0.448806f, 0.843929f, 0.547791f, + -0.859087f, -0.434649f, -0.30011f, 0.28757f, 0.957661f, 0.047729f, 0.3791f, 0.795023f, 0.78064f, 0.154245f, -0.987903f, -0.103088f, + -0.538067f, 0.794791f, -0.462524f, -0.466455f, -0.180966f, 0.880371f, -0.175736f, -0.983766f, 0.202576f, -0.891655f, 0.19208f, -0.417725f, + -0.688716f, -0.619004f, 0.480652f, 0.12079f, -0.987844f, -0.629456f, -0.07508f, 0.983385f, 0.910461f, 0.147032f, -0.960431f, -0.849304f, + 0.732309f, 0.671559f, 0.152283f, 0.804657f, 0.273913f, -0.547729f, 0.391462f, -0.913976f, 0.263184f, -0.5673f, 0.783128f, 0.409607f, + 0.214917f, 0.167182f, -0.975952f, 0.367428f, -0.789995f, -0.800537f, -0.320112f, 0.912727f, -0.621399f, 0.659247f, -0.647346f, -0.501892f, + 0.222842f, -0.696452f, -0.950562f, -0.697513f, -0.576278f, 0.521118f, 0.60226f, -0.756081f, 0.391418f, -0.116043f, 0.992942f, 0.206665f, + 0.220693f, -0.968855f, -0.453552f, 0.737991f, 0.670137f, 0.106812f, 0.198419f, -0.69659f, 0.960999f, -0.391866f, -0.883543f, 0.547668f, + 0.082067f, -0.996213f, 0.3302f, -0.806059f, 0.491897f, -0.377991f, -0.992265f, 0.120698f, 0.029236f, 0.406622f, -0.867524f, 0.575928f, + 0.789945f, 0.608406f, 0.096191f, -0.531904f, -0.004218f, -0.846802f, 0.558298f, -0.089427f, 0.828125f, -0.783155f, 0.363828f, -0.541382f, + 0.981706f, -0.183228f, 0.052673f, -0.388642f, 0.920618f, -0.096497f, -0.506403f, -0.044662f, -0.862f, -0.512421f, -0.852059f, -0.204163f, + 0.559542f, 0.339777f, 0.803772f, 0.527502f, -0.846389f, 0.137573f, -0.184315f, -0.952725f, 0.794983f, 0.125024f, -0.97711f, -0.809082f, + -0.643507f, 0.678632f, 0.482056f, -0.277474f, 0.954056f, 0.37738f, -0.622333f, -0.717603f, 0.448914f, 0.366846f, -0.110794f, -0.929382f, + 0.120402f, 0.992596f, 0.131653f, -0.982921f, 0.10355f, -0.152954f, -0.058333f, -0.997913f, -0.428894f, 0.132631f, 0.979299f, 0.755432f, + 0.326398f, 0.937806f, 0.340637f, 0.21172f, 0.976659f, 0.16864f, 0.957557f, -0.019174f, -0.287659f, -0.016554f, 0.99965f, 0.78009f, + -0.271222f, 0.827292f, -0.875732f, 0.85079f, -0.448069f, 0.307129f, 0.115949f, 0.600003f, -0.989441f, 0.285877f, -0.940896f, -0.536255f, + -0.321317f, -0.278336f, -0.942383f, -0.422133f, 0.754447f, 0.765747f, 0.669674f, -0.741852f, -0.051514f, 0.213604f, -0.949888f, 0.730103f, + 0.619681f, -0.751798f, -0.341797f, -0.223762f, 0.438616f, -0.968506f, -0.302925f, -0.945732f, 0.361877f, 0.121093f, -0.977151f, -0.821838f, + 0.127125f, 0.75871f, -0.980774f, 0.691682f, 0.695626f, 0.270203f, 0.241114f, 0.967463f, -0.30304f, -0.829705f, 0.422869f, 0.4021f, + -0.48417f, -0.741723f, 0.692017f, -0.431259f, -0.777492f, -0.727844f, 0.835756f, -0.211986f, 0.518311f, 0.297724f, 0.932993f, 0.561829f, + 0.633475f, -0.76492f, -0.181091f, -0.833849f, -0.453546f, -0.353027f, -0.369433f, 0.839581f, -0.733154f, 0.555847f, 0.392934f, -0.796631f, + -0.856065f, 0.028375f, 0.516296f, 0.067161f, 0.997565f, 0.269409f, -0.962279f, -0.051749f, 0.267456f, -0.738893f, 0.080065f, -0.671204f, + -0.764325f, 0.46224f, 0.507019f, 0.148758f, 0.751545f, 0.974243f, -0.15343f, -0.31823f, 0.986816f, -0.439372f, 0.776405f, 0.716919f, + ]; +} diff --git a/Source.Common/Mathematics/RandSIMD.cs b/Source.Common/Mathematics/RandSIMD.cs new file mode 100644 index 00000000..5d72e090 --- /dev/null +++ b/Source.Common/Mathematics/RandSIMD.cs @@ -0,0 +1,97 @@ +using System.Runtime.Intrinsics; +using System.Runtime.Intrinsics.X86; + +namespace Source.Common.Mathematics; + +public static class RandSIMD +{ + const int MAX_SIMULTANEOUS_RANDOM_STREAMS = 32; + + sealed class StreamContext + { + readonly Vector128[] RandY = new Vector128[55]; + int J; + int K; + + public void Seed(uint seed) { + J = 23; + K = 54; + Span lanes = stackalloc float[4]; + for (int i = 0; i < 55; i++) { + for (int j = 0; j < 4; j++) { + lanes[j] = (float)((seed >> 16) / 65536.0); + seed = unchecked((seed + 1) * 3141592621u); + } + RandY[i] = Vector128.Create(lanes[0], lanes[1], lanes[2], lanes[3]); + } + } + + public Vector128 Next() { + Vector128 ret = RandY[K] + RandY[J]; + Vector128 overflow = Vector128.GreaterThanOrEqual(ret, MathLib.Four_Ones); + ret -= MathLib.Four_Ones & overflow; + RandY[K] = ret; + if (--J < 0) + J = 54; + if (--K < 0) + K = 54; + return ret; + } + } + + static readonly StreamContext[] Contexts = CreateContexts(); + static readonly int[] ContextsInUse = new int[MAX_SIMULTANEOUS_RANDOM_STREAMS]; + + static StreamContext[] CreateContexts() { + StreamContext[] contexts = new StreamContext[MAX_SIMULTANEOUS_RANDOM_STREAMS]; + for (int i = 0; i < contexts.Length; i++) + contexts[i] = new StreamContext(); + return contexts; + } + + public static void Seed(uint seed) { + for (int i = 0; i < MAX_SIMULTANEOUS_RANDOM_STREAMS; i++) + Contexts[i].Seed(unchecked(seed + (uint)i)); + } + + public static int GetContext() { + while (true) { + for (int i = 0; i < MAX_SIMULTANEOUS_RANDOM_STREAMS; i++) { + if (ContextsInUse[i] == 0 && Interlocked.CompareExchange(ref ContextsInUse[i], 1, 0) == 0) + return i; + } + Thread.Sleep(0); + } + } + + public static void ReleaseContext(int context) => ContextsInUse[context] = 0; + + public static Vector128 Next(int context) => Contexts[context].Next(); + + public static Vector128 PowFixedPointExponent(Vector128 x, int exponent) { + Vector128 result = MathLib.Four_Ones; + int absExp = Math.Abs(exponent); + if ((absExp & 3) != 0) { + Vector128 sqrtX = Vector128.Sqrt(x); + if ((absExp & 1) != 0) + result = Vector128.Sqrt(sqrtX); + if ((absExp & 2) != 0) + result *= sqrtX; + } + absExp >>= 2; + Vector128 curPower = x; + while (true) { + if ((absExp & 1) != 0) + result *= curPower; + absExp >>= 1; + if (absExp == 0) + break; + curPower *= curPower; + } + if (exponent < 0) { + Vector128 saturated = result | (Vector128.Equals(result, Vector128.Zero) & MathLib.Four_Epsilons); + result = Sse.IsSupported ? Sse.Reciprocal(saturated) : MathLib.Four_Ones / saturated; + } + return result; + } +}