BepuPhysics/CollisionDetection/ConstraintRemover.cs
/// <summary>
/// Processes enqueued constraint removals and prepares removal jobs.
/// </summary>
/// <returns>The number of removal jobs created. To complete the jobs, execute RemoveConstraintsFromTypeBatch for every index from 0 to the returned job count.</returns>
Processes enqueued constraint removals and prepares removal jobs.
Processes enqueued constraint removals and prepares removal jobs .
public int CreateFlushJobs()
        {
            Batches.Create(pool.SpecializeFor<TypeBatchIndex>(), pool.SpecializeFor<QuickList<WorkerBatchReference, Buffer<WorkerBatchReference>>>(), pool.SpecializeFor<int>(),
                7, 3, out batches);
            var typeBatchIndexPool = pool.SpecializeFor<TypeBatchIndex>();
            var quickListPool = pool.SpecializeFor<QuickList<WorkerBatchReference, Buffer<WorkerBatchReference>>>();
            var intPool = pool.SpecializeFor<int>();
            var removedConstraintCount = 0;
            for (int i = 0; i < threadCount; ++i)
            {
                ref var cache = ref workerCaches[i];
                for (int j = 0; j < cache.Batches.Count; ++j)
                {
                    removedConstraintCount += cache.BatchHandles[j].Count;
                    var batchIndex = batches.IndexOf(cache.Batches[j]);
                    if (batchIndex >= 0)
                    {
                        ref var slot = ref batches.Values[batchIndex].AllocateUnsafely();
                        slot.WorkerIndex = (short)i;
                        slot.WorkerBatchIndex = (short)j;
                    }
                    else
                    {
                        QuickList<WorkerBatchReference, Buffer<WorkerBatchReference>>.Create(pool.SpecializeFor<WorkerBatchReference>(), threadCount - i, out var references);
                        WorkerBatchReference reference;
                        reference.WorkerIndex = (short)i;
                        reference.WorkerBatchIndex = (short)j;
                        references.AddUnsafely(reference);
                        batches.Add(ref cache.Batches[j], ref references, typeBatchIndexPool, quickListPool, intPool);
                    }
                }
            }
            solver.HandlePool.EnsureCapacity(solver.HandlePool.AvailableIds.Count + removedConstraintCount, intPool);
            int typeBatchCount = 0;
            ref var activeSet = ref solver.ActiveSet;
            for (int i = 0; i < activeSet.Batches.Count; ++i)
            {
                typeBatchCount += activeSet.Batches[i].TypeBatches.Count;
            }
            QuickList<TypeBatchIndex, Buffer<TypeBatchIndex>>.Create(pool.SpecializeFor<TypeBatchIndex>(), typeBatchCount, out removedTypeBatches);
            return batches.Count;
        }
public int CreateFlushJobs ( ) { Batches . Create ( pool . SpecializeFor < TypeBatchIndex > ( ) , pool . SpecializeFor < QuickList < WorkerBatchReference , Buffer < WorkerBatchReference > > > ( ) , pool . SpecializeFor < int > ( ) , 7 , 3 , out batches ) ; var typeBatchIndexPool = pool . SpecializeFor < TypeBatchIndex > ( ) ; var quickListPool = pool . SpecializeFor < QuickList < WorkerBatchReference , Buffer < WorkerBatchReference > > > ( ) ; var intPool = pool . SpecializeFor < int > ( ) ; var removedConstraintCount = 0 ; for ( int i = 0 ; i < threadCount ; ++ i ) { ref var cache = ref workerCaches [ i ] ; for ( int j = 0 ; j < cache . Batches . Count ; ++ j ) { removedConstraintCount += cache . BatchHandles [ j ] . Count ; var batchIndex = batches . IndexOf ( cache . Batches [ j ] ) ; if ( batchIndex >= 0 ) { ref var slot = ref batches . Values [ batchIndex ] . AllocateUnsafely ( ) ; slot . WorkerIndex = ( short ) i ; slot . WorkerBatchIndex = ( short ) j ; } else { QuickList < WorkerBatchReference , Buffer < WorkerBatchReference > > . Create ( pool . SpecializeFor < WorkerBatchReference > ( ) , threadCount - i , out var references ) ; WorkerBatchReference reference ; reference . WorkerIndex = ( short ) i ; reference . WorkerBatchIndex = ( short ) j ; references . AddUnsafely ( reference ) ; batches . Add ( ref cache . Batches [ j ] , ref references , typeBatchIndexPool , quickListPool , intPool ) ; } } } solver . HandlePool . EnsureCapacity ( solver . HandlePool . AvailableIds . Count + removedConstraintCount , intPool ) ; int typeBatchCount = 0 ; ref var activeSet = ref solver . ActiveSet ; for ( int i = 0 ; i < activeSet . Batches . Count ; ++ i ) { typeBatchCount += activeSet . Batches [ i ] . TypeBatches . Count ; } QuickList < TypeBatchIndex , Buffer < TypeBatchIndex > > . Create ( pool . SpecializeFor < TypeBatchIndex > ( ) , typeBatchCount , out removedTypeBatches ) ; return batches . Count ; }
Processes enqueued constraint removals and prepares removal jobs.
Processes enqueued constraint removals and prepares removal jobs .
//Accumulate the set of unique type batches in a contiguous list so we can easily execute multithreaded jobs over them. //Note that we're not actually copying over the contents of the per-worker lists here- just storing a reference to the per-worker lists. //This worker batch doesn't exist in the combined set. Add it now. //Ensure that the solver's id pool is large enough to hold all constraint handles being removed. //(Note that we do this even if we end up using this for sleeping, where we don't actually return the handles. //There's no functional reason for that- it's just simpler to not have a conditional API, and it has no significant impact on performance. Might change later.) //Ensure that the removal list is large enough to hold every single type batch in the worst case. This prevents the need to resize during execution. //That's valuable because every access to the main thread's buffer pool is a potential race condition when other tasks are also using it. //We don't want to have to lock every use of the buffer pool in other tasks just because we didn't preallocate a trivial amount here.
The number of removal jobs created. To complete the jobs, execute RemoveConstraintsFromTypeBatch for every index from 0 to the returned job count.
The number of removal jobs created . To complete the jobs execute RemoveConstraintsFromTypeBatch for every index from 0 to the returned job count .