internal static void DestroyConstraint(FixedConstraintHandle constraint)
 {
     LosgapSystem.InvokeOnMasterAsync(() => InteropUtils.CallNative(
                                          NativeMethods.PhysicsManager_DestroyConstraint,
                                          constraint
                                          ).ThrowOnFailure());
 }
 internal static void DestroyRigidBody(PhysicsBodyHandle body)
 {
     LosgapSystem.InvokeOnMasterAsync(() => InteropUtils.CallNative(
                                          NativeMethods.PhysicsManager_DestroyRigidBody,
                                          body
                                          ).ThrowOnFailure());
 }
 internal static void DestroyShape(PhysicsShapeHandle shape)
 {
     LosgapSystem.InvokeOnMasterAsync(() => InteropUtils.CallNative(
                                          NativeMethods.PhysicsManager_DestroyShape,
                                          shape
                                          ).ThrowOnFailure());
 }
Example #4
0
        void IDisposable.Dispose()
        {
            lock (WindowMutationLock) {
                if (isDisposed)
                {
                    return;
                }
                isDisposed = true;

                lock (staticMutationLock) {
                    openWindows.Remove(this);
                }

                LosgapSystem.InvokeOnMasterAsync(() => {
                    if (!*windowClosureFlagPtr)
                    {
                        InteropUtils.CallNative(NativeMethods.WindowFactory_CloseWindow, WindowHandle).ThrowOnFailure();
                    }
                    InteropUtils.CallNative(NativeMethods.WindowFactory_CleanUpWindowResources, WindowHandle).ThrowOnFailure();
                });

                ClearViewports().ForEach(vp => vp.Dispose());

                OnWindowClosed();
            }
        }
 internal unsafe static void RemoveAllForceAndTorqueFromBody(PhysicsBodyHandle body)
 {
     LosgapSystem.InvokeOnMasterAsync(() => {
         InteropUtils.CallNative(NativeMethods.PhysicsManager_RemoveAllForceAndTorqueFromBody,
                                 body
                                 ).ThrowOnFailure();
     });
 }
 internal static void SetBodyMass(PhysicsBodyHandle body, float newMass)
 {
     LosgapSystem.InvokeOnMasterAsync(() => InteropUtils.CallNative(
                                          NativeMethods.PhysicsManager_SetBodyMass,
                                          body,
                                          newMass
                                          ).ThrowOnFailure());
 }
 internal static void SetBodyCCD(PhysicsBodyHandle body, float minSpeed, float ccdRadius)
 {
     LosgapSystem.InvokeOnMasterAsync(() => InteropUtils.CallNative(
                                          NativeMethods.PhysicsManager_SetBodyCCD,
                                          body,
                                          minSpeed,
                                          ccdRadius
                                          ));
 }
        /// <summary>
        /// Performs a <see cref="ResourceUsage.Write"/> on this buffer at the given <paramref name="writeOffset"/>;
        /// by copying <paramref name="numBytesToWrite"/> from <paramref name="data"/>.
        /// </summary>
        /// <param name="data">Where to copy the data from to the resource.</param>
        /// <param name="numBytesToWrite">The number of bytes to write.</param>
        /// <param name="writeOffset">The first byte in the buffer to begin writing to.</param>
        /// <exception cref="ResourceOperationUnavailableException">Thrown if <see cref="BaseResource.CanWrite"/>
        /// is <c>false</c>.</exception>
        protected void MapWrite(IntPtr data, uint numBytesToWrite, uint writeOffset)
        {
            Assure.LessThanOrEqualTo(
                numBytesToWrite + writeOffset,
                Size.InBytes,
                "Buffer overrun. Please ensure that you are not attempting to write past the end of the resource."
                );

            ThrowIfCannotWrite();
            LosgapSystem.InvokeOnMasterAsync(() => Mutate_MapWrite(data, numBytesToWrite, writeOffset, ResourceMapping.Write));
        }
 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();
     });
 }
Example #10
0
 public void DisposePhysicsBody()
 {
     if (physicsBody != PhysicsBodyHandle.NULL)
     {
         if (collisionDetected != null)
         {
             EntityModule.RemoveCollisionCallbackReportingForEntity(this);
         }
         LosgapSystem.InvokeOnMasterAsync(() => {
             physicsBody.Dispose();
             physicsBody = PhysicsBodyHandle.NULL;
         });
     }
 }
 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();
         }
     });
 }
 internal static void SetBodyProperties(PhysicsBodyHandle body,
                                        float restitution,
                                        float linearDamping,
                                        float angularDamping,
                                        float friction,
                                        float rollingFriction)
 {
     LosgapSystem.InvokeOnMasterAsync(() => InteropUtils.CallNative(
                                          NativeMethods.PhysicsManager_SetBodyProperties,
                                          body,
                                          restitution,
                                          linearDamping,
                                          angularDamping,
                                          friction,
                                          rollingFriction
                                          ).ThrowOnFailure());
 }
Example #13
0
        private void Mutate_MapWrite(GCHandle pinnedDataHandle, SubresourceBox dataDesc, uint mipIndex)
        {
            LosgapSystem.InvokeOnMasterAsync(() => {
                IntPtr dstDataPtr;
                uint outRowStrideBytes, outSliceStrideBytes;
                InteropUtils.CallNative(
                    NativeMethods.ResourceFactory_MapSubresource,
                    RenderingModule.DeviceContext,
                    ResourceHandle,
                    GetSubresourceIndex(mipIndex),
                    ResourceMapping.Write,
                    (IntPtr)(&dstDataPtr),
                    (IntPtr)(&outRowStrideBytes),
                    (IntPtr)(&outSliceStrideBytes)
                    ).ThrowOnFailure();

                try {
                    IntPtr srcDataPtr = pinnedDataHandle.AddrOfPinnedObject();
                    for (uint srcSlice = 0U, dstSliceStart = outSliceStrideBytes * dataDesc.Front;
                         srcSlice < dataDesc.Depth;
                         ++srcSlice, dstSliceStart += outSliceStrideBytes)
                    {
                        for (uint srcRow = 0U, dstRowStart = outRowStrideBytes * dataDesc.Top;
                             srcRow < dataDesc.Height;
                             ++srcRow, dstRowStart += outRowStrideBytes)
                        {
                            UnsafeUtils.MemCopy(srcDataPtr + (int)((dataDesc.Width * srcRow + dataDesc.Height * srcSlice) * TexelSizeBytes),
                                                dstDataPtr + (int)(dataDesc.Left + dstRowStart + dstSliceStart), dataDesc.Width * TexelSizeBytes);
                        }
                    }
                }
                finally {
                    InteropUtils.CallNative(
                        NativeMethods.ResourceFactory_UnmapSubresource,
                        RenderingModule.DeviceContext,
                        ResourceHandle,
                        GetSubresourceIndex(mipIndex)
                        ).ThrowOnFailure();
                }
            });
        }
        public override void ResetEntityPhysics(bool fullReset)
        {
            LosgapSystem.InvokeOnMasterAsync(() => {             // Because creating geometryEntity shapes invoke on master and it can create locking deadlocks
                Vector3 physicsShapeOffset;
                lock (instanceMutationLock) {
                    if (fullReset)
                    {
                        foreach (PhysicsShapeHandle shapeHandle in activePhysicsShapes.Values.Distinct())
                        {
                            if (!WorldModelCache.CacheContainsHandle(shapeHandle))
                            {
                                shapeHandle.Dispose();
                            }
                        }
                        activePhysicsShapes.Clear();
                    }
                    List <PhysicsShapeHandle> unusedHandles = activePhysicsShapes.Values.Distinct().ToList();
                    foreach (KeyValuePair <LevelGeometryEntity, GeometryEntity> kvp in currentGeometryEntities)
                    {
                        PhysicsShapeHandle physicsShape = PhysicsShapeHandle.NULL;
                        if (kvp.Key.Geometry is LevelGeometry_Model)
                        {
                            LevelGeometry_Model geomAsModel = (LevelGeometry_Model)kvp.Key.Geometry;
                            var matchingEntity = activePhysicsShapes.Keys.FirstOrDefault(e => e.Geometry is LevelGeometry_Model && ((LevelGeometry_Model)e.Geometry).HasIdenticalPhysicsShape(geomAsModel));
                            if (matchingEntity != null)
                            {
                                physicsShape = activePhysicsShapes[matchingEntity];
                            }
                        }
                        else
                        {
                            if (activePhysicsShapes.ContainsKey(kvp.Key))
                            {
                                physicsShape = activePhysicsShapes[kvp.Key];
                            }
                        }
                        if (physicsShape == PhysicsShapeHandle.NULL)
                        {
                            physicsShape = kvp.Key.Geometry.CreatePhysicsShape(out physicsShapeOffset);
                        }
                        if (physicsShape == PhysicsShapeHandle.NULL)
                        {
                            continue;                                                                  // Some geometry is non-collidable (i.e. planes)
                        }
                        kvp.Value.SetPhysicsShape(physicsShape, Vector3.ZERO, LevelGeometry.GEOMETRY_MASS, forceIntransigence: true);
                        activePhysicsShapes[kvp.Key] = physicsShape;
                        if (!fullReset && unusedHandles.Contains(physicsShape))
                        {
                            unusedHandles.Remove(physicsShape);
                        }
                    }

                    if (!fullReset)
                    {
                        foreach (var unusedHandle in unusedHandles)
                        {
                            activePhysicsShapes.RemoveWhere(kvp => kvp.Value == unusedHandle);
                            if (!WorldModelCache.CacheContainsHandle(unusedHandle))
                            {
                                unusedHandle.Dispose();
                            }
                        }
                    }
                }
            });
        }
Example #15
0
        /// <summary>
        /// Performs a <see cref="ResourceUsage.Write"/> on this texture, copying the supplied data to the resource.
        /// </summary>
        /// <param name="data">The data to write.
        /// <see cref="ArraySlice{T}.Length">Length</see> vertices will be copied from the given
        /// array slice. The copy will start from the specified <see cref="ArraySlice{T}.Offset">Offset</see> in the
        /// contained array.
        /// </param>
        /// <param name="writeOffsetX">The first texel in this texture to start copying the data to.</param>
        /// <param name="mipIndex">The mip level to write to. Only one mip level may be written to at a time. If this texture
        /// is not mipmapped, you must supply a value of <c>0U</c>.</param>
        /// <exception cref="ResourceOperationUnavailableException">Thrown if <see cref="BaseResource.CanDiscardWrite"/> is
        /// <c>false</c>.</exception>
        /// <exception cref="AssuranceFailedException">(Debug only) Thrown if the combination of supplied parameters would
        /// result in writing past the end of the texture in any dimension.</exception>
        public void Write(ArraySlice <TTexel> data, uint mipIndex = 0U, uint writeOffsetX = 0U)
        {
            Assure.LessThan(
                mipIndex, NumMips,
                "Can not write to mip level " + mipIndex + ": Only " + NumMips + " present in texture."
                );
            Assure.LessThan(
                data.Length + writeOffsetX,
                Width,
                "Buffer overrun: Please ensure you are not attempting to write past the end of the texture."
                );

            ThrowIfCannotWrite();

            lock (InstanceMutationLock) {
                if (IsDisposed)
                {
                    Logger.Warn("Attempted write manipulation on disposed resource of type: " + GetType().Name);
                    return;
                }

                if (Usage.ShouldUpdateSubresourceRegion())
                {
                    GCHandle pinnedDataHandle = GCHandle.Alloc(data.ContainingArray, GCHandleType.Pinned);
                    try {
                        SubresourceBox subBox = new SubresourceBox(writeOffsetX, writeOffsetX + data.Length);
                        InteropUtils.CallNative(
                            NativeMethods.ResourceFactory_UpdateSubresourceRegion,
                            RenderingModule.DeviceContext,
                            ResourceHandle,
                            GetSubresourceIndex(mipIndex),
                            (IntPtr)(&subBox),
                            pinnedDataHandle.AddrOfPinnedObject() + (int)(data.Offset * TexelSizeBytes),
                            (uint)Size,
                            (uint)Size
                            ).ThrowOnFailure();
                    }
                    finally {
                        pinnedDataHandle.Free();
                    }
                }
                else
                {
                    LosgapSystem.InvokeOnMasterAsync(() => {
                        IntPtr outDataPtr;
                        uint outRowStrideBytes, outSliceStrideBytes;
                        InteropUtils.CallNative(
                            NativeMethods.ResourceFactory_MapSubresource,
                            RenderingModule.DeviceContext,
                            ResourceHandle,
                            GetSubresourceIndex(mipIndex),
                            ResourceMapping.Write,
                            (IntPtr)(&outDataPtr),
                            (IntPtr)(&outRowStrideBytes),
                            (IntPtr)(&outSliceStrideBytes)
                            ).ThrowOnFailure();

                        try {
                            UnsafeUtils.CopyGenericArray(data, outDataPtr + (int)(writeOffsetX * TexelSizeBytes), TexelSizeBytes);
                        }
                        finally {
                            InteropUtils.CallNative(
                                NativeMethods.ResourceFactory_UnmapSubresource,
                                RenderingModule.DeviceContext,
                                ResourceHandle,
                                GetSubresourceIndex(mipIndex)
                                ).ThrowOnFailure();
                        }
                    });
                }
            }
        }