Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 23 additions & 34 deletions src/LitMotion/Assets/LitMotion/Runtime/Internal/MotionStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,48 +169,37 @@ public unsafe MotionHandle Create(ref MotionBuilder<TValue, TOptions, TAdapter>
}


[MethodImpl(MethodImplOptions.AggressiveInlining)]
void RemoveAt(int denseIndex)
public void RemoveAll(NativeList<int> denseIndexList)
{
tail--;

// swap elements
unmanagedDataArray[denseIndex] = unmanagedDataArray[tail];
unmanagedDataArray[tail] = default;
managedDataArray[denseIndex] = managedDataArray[tail];
managedDataArray[tail] = default;

// swap sparse index
var prevSparseIndex = sparseIndexLookup[denseIndex];
var currentSparseIndex = sparseIndexLookup[denseIndex] = sparseIndexLookup[tail];
sparseIndexLookup[tail] = default;

// update slot
if (currentSparseIndex.Version != 0)
{
ref var slot = ref sparseSetCore.GetSlotRefUnchecked(currentSparseIndex.Index);
slot.DenseIndex = denseIndex;
}
if (denseIndexList.Length == 0) return;

// free slot
if (prevSparseIndex.Version != 0)
// Mark removals before moving data; the job's indices may be unordered.
var newTail = tail;
for (int i = 0; i < denseIndexList.Length; i++)
{
sparseSetCore.Free(prevSparseIndex);
var denseIndex = denseIndexList[i];
sparseSetCore.Free(sparseIndexLookup[denseIndex]);
sparseIndexLookup[denseIndex] = default;
newTail = Math.Min(newTail, denseIndex);
}
}

public void RemoveAll(NativeList<int> denseIndexList)
{
var list = new NativeArray<SparseIndex>(denseIndexList.Length, Allocator.Temp, NativeArrayOptions.UninitializedMemory);
for (int i = 0; i < list.Length; i++)
// Preserve binding order, including motions appended by completion callbacks.
for (int i = newTail + 1; i < tail; i++)
{
list[i] = sparseIndexLookup[denseIndexList[i]];
var sparseIndex = sparseIndexLookup[i];
if (sparseIndex.Version == 0) continue;

unmanagedDataArray[newTail] = unmanagedDataArray[i];
managedDataArray[newTail] = managedDataArray[i];
sparseIndexLookup[newTail] = sparseIndex;
sparseSetCore.GetSlotRefUnchecked(sparseIndex.Index).DenseIndex = newTail;
newTail++;
}

for (int i = 0; i < list.Length; i++)
{
RemoveAt(sparseSetCore.GetSlotRefUnchecked(list[i].Index).DenseIndex);
}
unmanagedDataArray.AsSpan(newTail, tail - newTail).Clear();
managedDataArray.AsSpan(newTail, tail - newTail).Clear();
sparseIndexLookup.AsSpan(newTail, tail - newTail).Clear();
tail = newTail;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down