Example #1
0
        // Schedules a set of jobs to iterate the provided dispatch pairs and create contacts based on them.
        internal static SimulationJobHandles ScheduleCreateContactsJobs(ref PhysicsWorld world, sfloat timeStep,
                                                                        ref NativeStream contacts, ref NativeStream jacobians, ref NativeList <DispatchPairSequencer.DispatchPair> dispatchPairs,
                                                                        JobHandle inputDeps, ref DispatchPairSequencer.SolverSchedulerInfo solverSchedulerInfo, bool multiThreaded = true)
        {
            SimulationJobHandles returnHandles = default;

            if (!multiThreaded)
            {
                contacts  = new NativeStream(1, Allocator.TempJob);
                jacobians = new NativeStream(1, Allocator.TempJob);
                returnHandles.FinalExecutionHandle = new CreateContactsJob
                {
                    World          = world,
                    TimeStep       = timeStep,
                    DispatchPairs  = dispatchPairs.AsDeferredJobArray(),
                    ContactsWriter = contacts.AsWriter()
                }.Schedule(inputDeps);
            }
            else
            {
                var numWorkItems    = solverSchedulerInfo.NumWorkItems;
                var contactsHandle  = NativeStream.ScheduleConstruct(out contacts, numWorkItems, inputDeps, Allocator.TempJob);
                var jacobiansHandle = NativeStream.ScheduleConstruct(out jacobians, numWorkItems, inputDeps, Allocator.TempJob);

                var processHandle = new ParallelCreateContactsJob
                {
                    World               = world,
                    TimeStep            = timeStep,
                    DispatchPairs       = dispatchPairs.AsDeferredJobArray(),
                    SolverSchedulerInfo = solverSchedulerInfo,
                    ContactsWriter      = contacts.AsWriter()
                }.ScheduleUnsafeIndex0(numWorkItems, 1, JobHandle.CombineDependencies(contactsHandle, jacobiansHandle));


                returnHandles.FinalExecutionHandle = processHandle;
            }

            return(returnHandles);
        }
Example #2
0
        /// <summary>
        /// Schedule a set of jobs to build the static tree of the broadphase based on the given world.
        /// </summary>
        public JobHandle ScheduleStaticTreeBuildJobs(
            ref PhysicsWorld world, int numThreadsHint, NativeArray <int> shouldDoWork, JobHandle inputDeps)
        {
            Assert.AreEqual(world.NumStaticBodies, m_StaticTree.NumBodies);
            if (world.NumStaticBodies == 0)
            {
                return(inputDeps);
            }

            var aabbs  = new NativeArray <Aabb>(world.NumStaticBodies, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);
            var points = new NativeArray <PointAndIndex>(world.NumStaticBodies, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);

            var       numStaticBodiesArray = new NativeArray <int>(1, Allocator.TempJob);
            JobHandle handle = new PrepareNumStaticBodiesJob
            {
                NumStaticBodies      = world.NumStaticBodies,
                BuildStaticTree      = shouldDoWork,
                NumStaticBodiesArray = numStaticBodiesArray
            }.Schedule(inputDeps);

            handle = new PrepareStaticBodyDataJob
            {
                RigidBodies            = world.StaticBodies,
                Aabbs                  = aabbs,
                Points                 = points,
                FiltersOut             = m_StaticTree.BodyFilters,
                RespondsToCollisionOut = m_StaticTree.RespondsToCollision,
                AabbMargin             = world.CollisionWorld.CollisionTolerance * (sfloat)0.5f, // each body contributes half
            }.ScheduleUnsafeIndex0(numStaticBodiesArray, 32, handle);

            var buildHandle = m_StaticTree.BoundingVolumeHierarchy.ScheduleBuildJobs(
                points, aabbs, m_StaticTree.BodyFilters, shouldDoWork, numThreadsHint, handle,
                m_StaticTree.Nodes.Length, m_StaticTree.Ranges, m_StaticTree.BranchCount);

            return(JobHandle.CombineDependencies(buildHandle, numStaticBodiesArray.Dispose(handle)));
        }
Example #3
0
        internal static unsafe JobHandle ScheduleUnityPhysicsBodyPairsJob <T>(T jobData, ISimulation simulation, ref PhysicsWorld world, JobHandle inputDeps)
            where T : struct, IBodyPairsJobBase
        {
            SafetyChecks.CheckAreEqualAndThrow(SimulationType.UnityPhysics, simulation.Type);

            var data = new BodyPairsJobData <T>
            {
                UserJobData         = jobData,
                PhasedDispatchPairs = ((Simulation)simulation).StepContext.PhasedDispatchPairs.AsDeferredJobArray(),
                Bodies = world.Bodies
            };
            var parameters = new JobsUtility.JobScheduleParameters(
#if UNITY_2020_2_OR_NEWER
                UnsafeUtility.AddressOf(ref data), BodyPairsJobProcess <T> .Initialize(), inputDeps, ScheduleMode.Single);
#else
                UnsafeUtility.AddressOf(ref data), BodyPairsJobProcess <T> .Initialize(), inputDeps, ScheduleMode.Batched);
#endif
            return(JobsUtility.Schedule(ref parameters));
        }
Example #4
0
 public static unsafe JobHandle Schedule <T>(this T jobData, ISimulation simulation, ref PhysicsWorld world, JobHandle inputDeps,
                                             HAVOK_PHYSICS_MISSING_FROM_ASMDEF _causeCompileError = HAVOK_PHYSICS_MISSING_FROM_ASMDEF.HAVOK_PHYSICS_MISSING_FROM_ASMDEF)
     where T : struct, IBodyPairsJobBase
 {
     return(new JobHandle());
 }
Example #5
0
        // Default Schedule() implementation for IBodyPairsJob
        public static unsafe JobHandle Schedule <T>(this T jobData, ISimulation simulation, ref PhysicsWorld world, JobHandle inputDeps)
            where T : struct, IBodyPairsJobBase
        {
            // Should work only for UnityPhysics
            if (simulation.Type != SimulationType.UnityPhysics)
            {
                return(inputDeps);
            }

            return(ScheduleUnityPhysicsBodyPairsJob(jobData, simulation, ref world, inputDeps));
        }
Example #6
0
 internal JobHandle Execute(Phase phase, ISimulation simulation, ref PhysicsWorld world, JobHandle inputDeps)
 {
     ref List <CallbackAndDependency> cbs = ref m_Callbacks[(int)phase];
Example #7
0
 public JobHandle ScheduleUpdateDynamicTree(ref PhysicsWorld world, sfloat timeStep, float3 gravity, JobHandle inputDeps, int threadCountHint = 0)
 {
     return(ScheduleUpdateDynamicTree(ref world, timeStep, gravity, inputDeps, threadCountHint > 0));
 }
Example #8
0
 // Schedule a set of jobs to build the broadphase based on the given world.
 public JobHandle ScheduleBuildBroadphaseJobs(ref PhysicsWorld world, sfloat timeStep, float3 gravity, NativeArray <int> buildStaticTree, JobHandle inputDeps, bool multiThreaded = true)
 {
     return(Broadphase.ScheduleBuildJobs(ref world, timeStep, gravity, buildStaticTree, inputDeps, multiThreaded));
 }
Example #9
0
 public JobHandle ScheduleBuildBroadphaseJobs(ref PhysicsWorld world, sfloat timeStep, float3 gravity, NativeArray <int> buildStaticTree, JobHandle inputDeps, int threadCountHint = 0)
 {
     return(ScheduleBuildBroadphaseJobs(ref world, timeStep, gravity, buildStaticTree, inputDeps, threadCountHint > 0));
 }
Example #10
0
 // Build the broadphase based on the given world.
 public void BuildBroadphase(ref PhysicsWorld world, sfloat timeStep, float3 gravity, bool buildStaticTree = true)
 {
     Broadphase.Build(world.StaticBodies, world.DynamicBodies, world.MotionVelocities,
                      world.CollisionWorld.CollisionTolerance, timeStep, gravity, buildStaticTree);
 }
Example #11
0
        // Schedules a trigger events job only for UnityPhysics simulation
        internal static unsafe JobHandle ScheduleUnityPhysicsTriggerEventsJob <T>(T jobData, ISimulation simulation, ref PhysicsWorld world, JobHandle inputDeps)
            where T : struct, ITriggerEventsJobBase
        {
            SafetyChecks.CheckAreEqualAndThrow(SimulationType.UnityPhysics, simulation.Type);

            var data = new TriggerEventJobData <T>
            {
                UserJobData = jobData,
                EventReader = ((Simulation)simulation).TriggerEvents
            };

            // Ensure the input dependencies include the end-of-simulation job, so events will have been generated
            inputDeps = JobHandle.CombineDependencies(inputDeps, simulation.FinalSimulationJobHandle);
#if UNITY_2020_2_OR_NEWER
            var parameters = new JobsUtility.JobScheduleParameters(UnsafeUtility.AddressOf(ref data), TriggerEventJobProcess <T> .Initialize(), inputDeps, ScheduleMode.Single);
#else
            var parameters = new JobsUtility.JobScheduleParameters(UnsafeUtility.AddressOf(ref data), TriggerEventJobProcess <T> .Initialize(), inputDeps, ScheduleMode.Batched);
#endif
            return(JobsUtility.Schedule(ref parameters));
        }