public static unsafe PhysicsShapeHandle CreateConcaveHullShape(IEnumerable <Vector3> vertices, IEnumerable <int> indices, CollisionShapeOptionsDesc shapeOptions, string acdFilePath)
 {
     return(LosgapSystem.InvokeOnMaster(() => {
         AlignedAllocation <CollisionShapeOptionsDesc> shapeOptionsAligned = new AlignedAllocation <CollisionShapeOptionsDesc>(16L, (uint)sizeof(CollisionShapeOptionsDesc));
         *((CollisionShapeOptionsDesc *)shapeOptionsAligned.AlignedPointer) = shapeOptions;
         Vector3 *verticesLocal = stackalloc Vector3[vertices.Count()];
         int *indicesLocal = stackalloc int[indices.Count()];
         int numVertices = 0;
         int numIndices = 0;
         foreach (Vector3 vertex in vertices)
         {
             verticesLocal[numVertices++] = vertex;
         }
         foreach (int index in indices)
         {
             indicesLocal[numIndices++] = index;
         }
         PhysicsShapeHandle result;
         InteropUtils.CallNative(
             NativeMethods.PhysicsManager_CreateConcaveHullShape,
             (IntPtr)verticesLocal,
             numVertices,
             (IntPtr)indicesLocal,
             numIndices,
             shapeOptionsAligned.AlignedPointer,
             acdFilePath,
             (IntPtr)(&result)
             ).ThrowOnFailure();
         shapeOptionsAligned.Dispose();
         return result;
     }));
 }
 internal unsafe static void AddTorqueImpulseToBody(PhysicsBodyHandle body, Vector3 torque)
 {
     LosgapSystem.InvokeOnMasterAsync(() => {
         AlignedAllocation <Vector4> vec4Aligned  = new AlignedAllocation <Vector4>(16L, (uint)sizeof(Vector4));
         *((Vector4 *)vec4Aligned.AlignedPointer) = torque;
         InteropUtils.CallNative(NativeMethods.PhysicsManager_AddTorqueImpulseToBody,
                                 body,
                                 vec4Aligned.AlignedPointer
                                 ).ThrowOnFailure();
         vec4Aligned.Dispose();
     });
 }
示例#3
0
 public virtual void Dispose()
 {
     lock (InstanceMutationLock) {
         if (isDisposed)
         {
             return;
         }
         isDisposed = true;
         // ReSharper disable once ImpureMethodCallOnReadonlyValueField Resharper bug, Dispose is not impure
         viewMatrix.Dispose();
     }
 }
示例#4
0
        private unsafe void ResizeList()
        {
            uint newListLen = CurListLen + LIST_SIZE_INCREMENT;

            GC.RemoveMemoryPressure(sizeof(RenderCommand) * CurListLen);
            AlignedAllocation <RenderCommand> newListSpace = AlignedAllocation <RenderCommand> .AllocArray(LIST_ALIGNMENT, newListLen);

            GC.AddMemoryPressure(sizeof(RenderCommand) * CurListLen);
            UnsafeUtils.MemCopy(RenderCommandList.AlignedPointer, newListSpace.AlignedPointer, (uint)sizeof(RenderCommand) * CurListLen);
            CurListLen = newListLen;

            RenderCommandList.Dispose();

            RenderCommandList = newListSpace;
        }
 public static unsafe void SetGravityOnAllBodies(Vector3 gravity)
 {
     LosgapSystem.InvokeOnMasterAsync(() => {
         AlignedAllocation <Vector4> vec4Aligned  = new AlignedAllocation <Vector4>(16L, (uint)sizeof(Vector4));
         *((Vector4 *)vec4Aligned.AlignedPointer) = gravity;
         try {
             InteropUtils.CallNative(
                 NativeMethods.PhysicsManager_SetGravity,
                 vec4Aligned.AlignedPointer
                 ).ThrowOnFailure();
         }
         finally {
             vec4Aligned.Dispose();
         }
     });
 }
 public static unsafe PhysicsShapeHandle CreateSimpleSphereShape(float radius, CollisionShapeOptionsDesc shapeOptions)
 {
     return(LosgapSystem.InvokeOnMaster(() => {
         AlignedAllocation <CollisionShapeOptionsDesc> shapeOptionsAligned = new AlignedAllocation <CollisionShapeOptionsDesc>(16L, (uint)sizeof(CollisionShapeOptionsDesc));
         *((CollisionShapeOptionsDesc *)shapeOptionsAligned.AlignedPointer) = shapeOptions;
         PhysicsShapeHandle result;
         InteropUtils.CallNative(
             NativeMethods.PhysicsManager_CreateSimpleSphereShape,
             radius,
             shapeOptionsAligned.AlignedPointer,
             (IntPtr)(&result)
             ).ThrowOnFailure();
         shapeOptionsAligned.Dispose();
         return result;
     }));
 }
 internal unsafe static Vector3 GetBodyAngularVelocity(PhysicsBodyHandle body)
 {
     return(LosgapSystem.InvokeOnMaster(() => {
         AlignedAllocation <Vector4> vec4Aligned = new AlignedAllocation <Vector4>(16L, (uint)sizeof(Vector4));
         InteropUtils.CallNative(NativeMethods.PhysicsManager_GetBodyAngularVelocity,
                                 body,
                                 vec4Aligned.AlignedPointer
                                 ).ThrowOnFailure();
         try {
             return (Vector3)(*((Vector4 *)vec4Aligned.AlignedPointer));
         }
         finally {
             vec4Aligned.Dispose();
         }
     }));
 }
        public unsafe void TestAllocArray()
        {
            // Define variables and constants
            const int  NUM_ALLOCS      = 2000;
            const long ALLOC_ALIGNMENT = 2L;

            // Set up context
            AlignedAllocation <Vector4> alignedArray = AlignedAllocation <Vector4> .AllocArray(ALLOC_ALIGNMENT, NUM_ALLOCS);

            // Execute
            *((Vector4 *)(alignedArray.AlignedPointer + NUM_ALLOCS - 1)) = new Vector4(1, 2, 3, 4);

            // Assert outcome
            Assert.AreEqual(new Vector4(1, 2, 3, 4), *((Vector4 *)(alignedArray.AlignedPointer + NUM_ALLOCS - 1)));
            Assert.AreEqual(0L, (long)alignedArray.AlignedPointer % ALLOC_ALIGNMENT);

            alignedArray.Dispose();
        }
        internal static unsafe IEnumerable <RayTestCollisionDesc> RayTestAll(Vector3 startPoint, Vector3 endPoint, uint maxCollisions)
        {
            RayTestCollisionDesc *collisionArr      = stackalloc RayTestCollisionDesc[(int)maxCollisions];
            uint maxCollisionsLocal                 = maxCollisions;
            RayTestCollisionDesc *collisionArrLocal = collisionArr;
            uint outNumCollisions;

            AlignedAllocation <Vector4> startPointAligned = new AlignedAllocation <Vector4>(16L, (uint)sizeof(Vector4));

            *((Vector4 *)startPointAligned.AlignedPointer) = startPoint;
            AlignedAllocation <Vector4> endPointAligned = new AlignedAllocation <Vector4>(16L, (uint)sizeof(Vector4));

            *((Vector4 *)endPointAligned.AlignedPointer) = endPoint;

            // WARNING: No longer thread-safe
            unsafe {
                char *failReason = stackalloc char[InteropUtils.MAX_INTEROP_FAIL_REASON_STRING_LENGTH + 1];
                bool  success    = NativeMethods.PhysicsManager_RayTestAll(
                    (IntPtr)failReason,
                    startPointAligned.AlignedPointer,
                    endPointAligned.AlignedPointer,
                    (IntPtr)collisionArrLocal,
                    maxCollisionsLocal,
                    (IntPtr)(&outNumCollisions)
                    );
                if (!success)
                {
                    throw new NativeOperationFailedException(Marshal.PtrToStringUni((IntPtr)failReason));
                }
            }

            List <RayTestCollisionDesc> result = new List <RayTestCollisionDesc>((int)outNumCollisions);

            for (uint i = 0U; i < outNumCollisions; ++i)
            {
                result.Add(collisionArr[i]);
            }

            startPointAligned.Dispose();
            endPointAligned.Dispose();

            return(result);
        }
 public static unsafe PhysicsShapeHandle CreateBoxShape(float width, float height, float depth, CollisionShapeOptionsDesc shapeOptions)
 {
     return(LosgapSystem.InvokeOnMaster(() => {
         AlignedAllocation <CollisionShapeOptionsDesc> shapeOptionsAligned = new AlignedAllocation <CollisionShapeOptionsDesc>(16L, (uint)sizeof(CollisionShapeOptionsDesc));
         *((CollisionShapeOptionsDesc *)shapeOptionsAligned.AlignedPointer) = shapeOptions;
         AlignedAllocation <Vector4> boxExtents = new AlignedAllocation <Vector4>(16L, (uint)sizeof(Vector4));
         *((Vector4 *)boxExtents.AlignedPointer) = new Vector4(width * 0.5f, height * 0.5f, depth * 0.5f, 0f);
         PhysicsShapeHandle result;
         InteropUtils.CallNative(
             NativeMethods.PhysicsManager_CreateBoxShape,
             boxExtents.AlignedPointer,
             shapeOptionsAligned.AlignedPointer,
             (IntPtr)(&result)
             ).ThrowOnFailure();
         shapeOptionsAligned.Dispose();
         boxExtents.Dispose();
         return result;
     }));
 }
 public static unsafe PhysicsShapeHandle CreateConeShape(float radius, float height, CollisionShapeOptionsDesc shapeOptions, out Vector3 translationOffset)
 {
     //translationOffset = Vector3.DOWN * (height * 0.5f);
     translationOffset = Vector3.ZERO;
     return(LosgapSystem.InvokeOnMaster(() => {
         AlignedAllocation <CollisionShapeOptionsDesc> shapeOptionsAligned = new AlignedAllocation <CollisionShapeOptionsDesc>(16L, (uint)sizeof(CollisionShapeOptionsDesc));
         *((CollisionShapeOptionsDesc *)shapeOptionsAligned.AlignedPointer) = shapeOptions;
         PhysicsShapeHandle result;
         InteropUtils.CallNative(
             NativeMethods.PhysicsManager_CreateConeShape,
             radius,
             height,
             shapeOptionsAligned.AlignedPointer,
             (IntPtr)(&result)
             ).ThrowOnFailure();
         shapeOptionsAligned.Dispose();
         return result;
     }));
 }
示例#12
0
        public void Dispose()
        {
            lock (TargetWindow.WindowMutationLock) {
                if (isDisposed)
                {
                    return;
                }

                projectionMatrix.Dispose();

                try {
                    InteropUtils.CallNative(NativeMethods.WindowFactory_DestroyViewport,
                                            ViewportHandle
                                            ).ThrowOnFailure();
                }
                finally {
                    isDisposed = true;
                }
            }
        }
        internal unsafe static void SetBodyGravity(PhysicsBodyHandle body, Vector3 gravity)
        {
            AlignedAllocation <Vector4> gravityAligned = new AlignedAllocation <Vector4>(16L, (uint)sizeof(Vector4));

            *((Vector4 *)gravityAligned.AlignedPointer) = gravity;

            // WARNING: No longer thread-safe
            try {
                unsafe {
                    char *failReason = stackalloc char[InteropUtils.MAX_INTEROP_FAIL_REASON_STRING_LENGTH + 1];
                    bool  success    = NativeMethods.PhysicsManager_SetBodyGravity((IntPtr)failReason, body, gravityAligned.AlignedPointer);
                    if (!success)
                    {
                        throw new NativeOperationFailedException(Marshal.PtrToStringUni((IntPtr)failReason));
                    }
                }
            }
            finally {
                gravityAligned.Dispose();
            }
        }
        internal unsafe static Vector3 GetBodyLinearVelocity(PhysicsBodyHandle body)
        {
            AlignedAllocation <Vector4> vec4Aligned = new AlignedAllocation <Vector4>(16L, (uint)sizeof(Vector4));

            // WARNING: No longer thread-safe
            unsafe {
                char *failReason = stackalloc char[InteropUtils.MAX_INTEROP_FAIL_REASON_STRING_LENGTH + 1];
                bool  success    = NativeMethods.PhysicsManager_GetBodyLinearVelocity((IntPtr)failReason, body, vec4Aligned.AlignedPointer);
                if (!success)
                {
                    throw new NativeOperationFailedException(Marshal.PtrToStringUni((IntPtr)failReason));
                }
            }

            try {
                return((Vector3)(*((Vector4 *)vec4Aligned.AlignedPointer)));
            }
            finally {
                vec4Aligned.Dispose();
            }
        }
示例#15
0
 public virtual void Dispose()
 {
     lock (InstanceMutationLock) {
         if (isDisposed)
         {
             return;
         }
         isDisposed = true;
         DisposePhysicsBody();
         EntityModule.RemoveActiveEntity(this);
         if (modelInstance != null)
         {
             modelInstance.Value.Dispose();
         }
         modelInstance = null;
         transform.Dispose();
         if (physicsShapeOffset != null)
         {
             physicsShapeOffset.Value.Dispose();
         }
     }
 }
 public static unsafe PhysicsShapeHandle CreateCompoundCurveShape(IEnumerable <Vector3> vertices, CollisionShapeOptionsDesc shapeOptions)
 {
     return(LosgapSystem.InvokeOnMaster(() => {
         AlignedAllocation <CollisionShapeOptionsDesc> shapeOptionsAligned = new AlignedAllocation <CollisionShapeOptionsDesc>(16L, (uint)sizeof(CollisionShapeOptionsDesc));
         *((CollisionShapeOptionsDesc *)shapeOptionsAligned.AlignedPointer) = shapeOptions;
         Vector3 *verticesLocal = stackalloc Vector3[vertices.Count()];
         int numVertices = 0;
         foreach (Vector3 vertex in vertices)
         {
             verticesLocal[numVertices++] = vertex;
         }
         PhysicsShapeHandle result;
         InteropUtils.CallNative(
             NativeMethods.PhysicsManager_CreateCompoundCurveShape,
             (IntPtr)verticesLocal,
             (uint)numVertices / 8U,
             shapeOptionsAligned.AlignedPointer,
             (IntPtr)(&result)
             ).ThrowOnFailure();
         shapeOptionsAligned.Dispose();
         return result;
     }));
 }
        internal static unsafe PhysicsBodyHandle RayTestNearest(Vector3 startPoint, Vector3 endPoint, out Vector3 hitPoint)
        {
            AlignedAllocation <Vector4> hitPointAligned = new AlignedAllocation <Vector4>(16L, (uint)sizeof(Vector4));
            Vector4 *hitPoint4Ptr = (Vector4 *)hitPointAligned.AlignedPointer;
            var      result       = LosgapSystem.InvokeOnMaster(() => {
                AlignedAllocation <Vector4> startPointAligned  = new AlignedAllocation <Vector4>(16L, (uint)sizeof(Vector4));
                *((Vector4 *)startPointAligned.AlignedPointer) = startPoint;
                AlignedAllocation <Vector4> endPointAligned    = new AlignedAllocation <Vector4>(16L, (uint)sizeof(Vector4));
                *((Vector4 *)endPointAligned.AlignedPointer)   = endPoint;
                Vector4 *hitPoint4PtrLocal = hitPoint4Ptr;
                PhysicsBodyHandle outPBH;
                InteropUtils.CallNative(NativeMethods.PhysicsManager_RayTestNearest,
                                        startPointAligned.AlignedPointer,
                                        endPointAligned.AlignedPointer,
                                        (IntPtr)(&outPBH),
                                        (IntPtr)(hitPoint4PtrLocal)
                                        ).ThrowOnFailure();
                startPointAligned.Dispose();
                endPointAligned.Dispose();
                return(outPBH);
            });

            try {
                if (result != PhysicsBodyHandle.NULL)
                {
                    hitPoint = (Vector3)(*((Vector4 *)hitPointAligned.AlignedPointer));
                }
                else
                {
                    hitPoint = Vector3.ZERO;
                }
                return(result);
            }
            finally {
                hitPointAligned.Dispose();
            }
        }
 /// <summary>
 /// Disposes this cache, making it no longer usable, and releasing all resources it created (including vertex/index buffers,
 /// instance buffers, transform buffers, etc).
 /// </summary>
 public void Dispose()
 {
     lock (staticMutationLock) {
         activeCaches.Remove(ID);
         activeCacheList.Remove(this);
     }
     lock (instanceMutationLock) {
         if (isDisposed)
         {
             return;
         }
         GC.RemoveMemoryPressure(sizeof(uint) * 2 * (NumModels + 1));
         vertexComponentBuffers.ForEach(buffer => buffer.Dispose());
         indices.Dispose();
         // ReSharper disable ImpureMethodCallOnReadonlyValueField R# bug
         componentStartPointsAlloc.Dispose();
         indexStartPointsAlloc.Dispose();
         // ReSharper restore ImpureMethodCallOnReadonlyValueField
         instanceManager.Dispose();
         isDisposed = true;
         assembledInputLayouts.ForEach(ail => ail.Value.Dispose());
         assembledInputLayouts.Clear();
     }
 }
        public void TestAlignment()
        {
            // Define variables and constants
            const int NUM_ALLOCATIONS_PER_TEST_ALIGNMENT = 350;

            int[] testAlignments = { 1, 2, 3, 4, 6, 8, 12, 16, 32 };

            // Set up context


            // Execute


            // Assert outcome
            for (int i = 0; i < testAlignments.Length; i++)
            {
                for (int j = 0; j < NUM_ALLOCATIONS_PER_TEST_ALIGNMENT; j++)
                {
                    AlignedAllocation <Matrix> alignedAlloc = new AlignedAllocation <Matrix>(testAlignments[i]);
                    Assert.IsTrue(((long)alignedAlloc.AlignedPointer) % testAlignments[i] == 0L);
                    alignedAlloc.Dispose();
                }
            }
        }