LargeIslandSplitter.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. // SPDX-FileCopyrightText: 2023 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <Jolt/Jolt.h>
  4. #include <Jolt/Physics/LargeIslandSplitter.h>
  5. #include <Jolt/Physics/IslandBuilder.h>
  6. #include <Jolt/Physics/Constraints/CalculateSolverSteps.h>
  7. #include <Jolt/Physics/Constraints/Constraint.h>
  8. #include <Jolt/Physics/Constraints/ContactConstraintManager.h>
  9. #include <Jolt/Physics/Body/BodyManager.h>
  10. #include <Jolt/Core/Profiler.h>
  11. #include <Jolt/Core/TempAllocator.h>
  12. //#define JPH_LARGE_ISLAND_SPLITTER_DEBUG
  13. JPH_NAMESPACE_BEGIN
  14. LargeIslandSplitter::EStatus LargeIslandSplitter::Splits::FetchNextBatch(uint32 &outConstraintsBegin, uint32 &outConstraintsEnd, uint32 &outContactsBegin, uint32 &outContactsEnd, bool &outFirstIteration)
  15. {
  16. {
  17. // First check if we can get a new batch (doing a relaxed read to avoid hammering an atomic with an atomic subtract)
  18. // Note this also avoids overflowing the status counter if we're done but there's still one thread processing items
  19. uint64 status = mStatus.load(memory_order_relaxed);
  20. if (sGetIteration(status) >= mNumIterations)
  21. return EStatus::AllBatchesDone;
  22. // Check for special value that indicates that the splits are still being built
  23. // (note we do not check for this condition again below as we reset all splits before kicking off jobs that fetch batches of work)
  24. if (status == StatusItemMask)
  25. return EStatus::WaitingForBatch;
  26. uint item = sGetItem(status);
  27. uint split_index = sGetSplit(status);
  28. if (split_index == cNonParallelSplitIdx)
  29. {
  30. // Non parallel split needs to be taken as a single batch, only the thread that takes element 0 will do it
  31. if (item != 0)
  32. return EStatus::WaitingForBatch;
  33. }
  34. else
  35. {
  36. // Parallel split is split into batches
  37. JPH_ASSERT(split_index < mNumSplits);
  38. const Split &split = mSplits[split_index];
  39. if (item >= split.GetNumItems())
  40. return EStatus::WaitingForBatch;
  41. }
  42. }
  43. // Then try to actually get the batch
  44. uint64 status = mStatus.fetch_add(cBatchSize, memory_order_acquire);
  45. int iteration = sGetIteration(status);
  46. if (iteration >= mNumIterations)
  47. return EStatus::AllBatchesDone;
  48. uint split_index = sGetSplit(status);
  49. JPH_ASSERT(split_index < mNumSplits || split_index == cNonParallelSplitIdx);
  50. const Split &split = mSplits[split_index];
  51. uint item_begin = sGetItem(status);
  52. if (split_index == cNonParallelSplitIdx)
  53. {
  54. if (item_begin == 0)
  55. {
  56. // Non-parallel split always goes as a single batch
  57. outConstraintsBegin = split.mConstraintBufferBegin;
  58. outConstraintsEnd = split.mConstraintBufferEnd;
  59. outContactsBegin = split.mContactBufferBegin;
  60. outContactsEnd = split.mContactBufferEnd;
  61. outFirstIteration = iteration == 0;
  62. return EStatus::BatchRetrieved;
  63. }
  64. else
  65. {
  66. // Otherwise we're done with this split
  67. return EStatus::WaitingForBatch;
  68. }
  69. }
  70. // Parallel split is split into batches
  71. uint num_constraints = split.GetNumConstraints();
  72. uint num_contacts = split.GetNumContacts();
  73. uint num_items = num_constraints + num_contacts;
  74. if (item_begin >= num_items)
  75. return EStatus::WaitingForBatch;
  76. uint item_end = min(item_begin + cBatchSize, num_items);
  77. if (item_end >= num_constraints)
  78. {
  79. if (item_begin < num_constraints)
  80. {
  81. // Partially from constraints and partially from contacts
  82. outConstraintsBegin = split.mConstraintBufferBegin + item_begin;
  83. outConstraintsEnd = split.mConstraintBufferEnd;
  84. }
  85. else
  86. {
  87. // Only contacts
  88. outConstraintsBegin = 0;
  89. outConstraintsEnd = 0;
  90. }
  91. outContactsBegin = split.mContactBufferBegin + (max(item_begin, num_constraints) - num_constraints);
  92. outContactsEnd = split.mContactBufferBegin + (item_end - num_constraints);
  93. }
  94. else
  95. {
  96. // Only constraints
  97. outConstraintsBegin = split.mConstraintBufferBegin + item_begin;
  98. outConstraintsEnd = split.mConstraintBufferBegin + item_end;
  99. outContactsBegin = 0;
  100. outContactsEnd = 0;
  101. }
  102. outFirstIteration = iteration == 0;
  103. return EStatus::BatchRetrieved;
  104. }
  105. void LargeIslandSplitter::Splits::MarkBatchProcessed(uint inNumProcessed, bool &outLastIteration, bool &outFinalBatch)
  106. {
  107. // We fetched this batch, nobody should change the split and or iteration until we mark the last batch as processed so we can safely get the current status
  108. uint64 status = mStatus.load(memory_order_relaxed);
  109. uint split_index = sGetSplit(status);
  110. JPH_ASSERT(split_index < mNumSplits || split_index == cNonParallelSplitIdx);
  111. const Split &split = mSplits[split_index];
  112. uint num_items_in_split = split.GetNumItems();
  113. // Determine if this is the last iteration before possibly incrementing it
  114. int iteration = sGetIteration(status);
  115. outLastIteration = iteration == mNumIterations - 1;
  116. // Add the number of items we processed to the total number of items processed
  117. // Note: This needs to happen after we read the status as other threads may update the status after we mark items as processed
  118. JPH_ASSERT(inNumProcessed > 0); // Logic will break if we mark a block of 0 items as processed
  119. uint total_items_processed = mItemsProcessed.fetch_add(inNumProcessed, memory_order_acq_rel) + inNumProcessed;
  120. // Check if we're at the end of the split
  121. if (total_items_processed >= num_items_in_split)
  122. {
  123. JPH_ASSERT(total_items_processed == num_items_in_split); // Should not overflow, that means we're retiring more items than we should process
  124. // Set items processed back to 0 for the next split/iteration
  125. mItemsProcessed.store(0, memory_order_release);
  126. // Determine next split
  127. do
  128. {
  129. if (split_index == cNonParallelSplitIdx)
  130. {
  131. // At start of next iteration
  132. split_index = 0;
  133. ++iteration;
  134. }
  135. else
  136. {
  137. // At start of next split
  138. ++split_index;
  139. }
  140. // If we're beyond the end of splits, go to the non-parallel split
  141. if (split_index >= mNumSplits)
  142. split_index = cNonParallelSplitIdx;
  143. }
  144. while (iteration < mNumIterations
  145. && mSplits[split_index].GetNumItems() == 0); // We don't support processing empty splits, skip to the next split in this case
  146. mStatus.store((uint64(iteration) << StatusIterationShift) | (uint64(split_index) << StatusSplitShift), memory_order_release);
  147. }
  148. // Track if this is the final batch
  149. outFinalBatch = iteration >= mNumIterations;
  150. }
  151. LargeIslandSplitter::~LargeIslandSplitter()
  152. {
  153. JPH_ASSERT(mSplitMasks == nullptr);
  154. JPH_ASSERT(mContactAndConstraintsSplitIdx == nullptr);
  155. JPH_ASSERT(mContactAndConstraintIndices == nullptr);
  156. JPH_ASSERT(mSplitIslands == nullptr);
  157. }
  158. void LargeIslandSplitter::Prepare(const IslandBuilder &inIslandBuilder, uint32 inNumActiveBodies, TempAllocator *inTempAllocator)
  159. {
  160. JPH_PROFILE_FUNCTION();
  161. // Count the total number of constraints and contacts that we will be putting in splits
  162. mContactAndConstraintsSize = 0;
  163. for (uint32 island = 0; island < inIslandBuilder.GetNumIslands(); ++island)
  164. {
  165. // Get the contacts in this island
  166. uint32 *contacts_start, *contacts_end;
  167. inIslandBuilder.GetContactsInIsland(island, contacts_start, contacts_end);
  168. uint num_contacts_in_island = uint(contacts_end - contacts_start);
  169. // Get the constraints in this island
  170. uint32 *constraints_start, *constraints_end;
  171. inIslandBuilder.GetConstraintsInIsland(island, constraints_start, constraints_end);
  172. uint num_constraints_in_island = uint(constraints_end - constraints_start);
  173. uint island_size = num_contacts_in_island + num_constraints_in_island;
  174. if (island_size >= cLargeIslandTreshold)
  175. {
  176. mNumSplitIslands++;
  177. mContactAndConstraintsSize += island_size;
  178. }
  179. else
  180. break; // If this island doesn't have enough constraints, the next islands won't either since they're sorted from big to small
  181. }
  182. if (mContactAndConstraintsSize > 0)
  183. {
  184. mNumActiveBodies = inNumActiveBodies;
  185. // Allocate split mask buffer
  186. mSplitMasks = (SplitMask *)inTempAllocator->Allocate(mNumActiveBodies * sizeof(SplitMask));
  187. // Allocate contact and constraint buffer
  188. uint contact_and_constraint_indices_size = mContactAndConstraintsSize * sizeof(uint32);
  189. mContactAndConstraintsSplitIdx = (uint32 *)inTempAllocator->Allocate(contact_and_constraint_indices_size);
  190. mContactAndConstraintIndices = (uint32 *)inTempAllocator->Allocate(contact_and_constraint_indices_size);
  191. // Allocate island split buffer
  192. mSplitIslands = (Splits *)inTempAllocator->Allocate(mNumSplitIslands * sizeof(Splits));
  193. // Prevent any of the splits from being picked up as work
  194. for (uint i = 0; i < mNumSplitIslands; ++i)
  195. mSplitIslands[i].ResetStatus();
  196. }
  197. }
  198. uint LargeIslandSplitter::AssignSplit(const Body *inBody1, const Body *inBody2)
  199. {
  200. uint32 idx1 = inBody1->GetIndexInActiveBodiesInternal();
  201. uint32 idx2 = inBody2->GetIndexInActiveBodiesInternal();
  202. // Test if either index is negative
  203. if (idx1 == Body::cInactiveIndex || !inBody1->IsDynamic())
  204. {
  205. // Body 1 is not active or a kinematic body, so we only need to set 1 body
  206. JPH_ASSERT(idx2 < mNumActiveBodies);
  207. SplitMask &mask = mSplitMasks[idx2];
  208. uint split = min(CountTrailingZeros(~uint32(mask)), cNonParallelSplitIdx);
  209. mask |= SplitMask(1U << split);
  210. return split;
  211. }
  212. else if (idx2 == Body::cInactiveIndex || !inBody2->IsDynamic())
  213. {
  214. // Body 2 is not active or a kinematic body, so we only need to set 1 body
  215. JPH_ASSERT(idx1 < mNumActiveBodies);
  216. SplitMask &mask = mSplitMasks[idx1];
  217. uint split = min(CountTrailingZeros(~uint32(mask)), cNonParallelSplitIdx);
  218. mask |= SplitMask(1U << split);
  219. return split;
  220. }
  221. else
  222. {
  223. // If both bodies are active, we need to set 2 bodies
  224. JPH_ASSERT(idx1 < mNumActiveBodies);
  225. JPH_ASSERT(idx2 < mNumActiveBodies);
  226. SplitMask &mask1 = mSplitMasks[idx1];
  227. SplitMask &mask2 = mSplitMasks[idx2];
  228. uint split = min(CountTrailingZeros((~uint32(mask1)) & (~uint32(mask2))), cNonParallelSplitIdx);
  229. SplitMask mask = SplitMask(1U << split);
  230. mask1 |= mask;
  231. mask2 |= mask;
  232. return split;
  233. }
  234. }
  235. uint LargeIslandSplitter::AssignToNonParallelSplit(const Body *inBody)
  236. {
  237. uint32 idx = inBody->GetIndexInActiveBodiesInternal();
  238. if (idx != Body::cInactiveIndex)
  239. {
  240. JPH_ASSERT(idx < mNumActiveBodies);
  241. mSplitMasks[idx] |= 1U << cNonParallelSplitIdx;
  242. }
  243. return cNonParallelSplitIdx;
  244. }
  245. bool LargeIslandSplitter::SplitIsland(uint32 inIslandIndex, const IslandBuilder &inIslandBuilder, const BodyManager &inBodyManager, const ContactConstraintManager &inContactManager, Constraint **inActiveConstraints, CalculateSolverSteps &ioStepsCalculator)
  246. {
  247. JPH_PROFILE_FUNCTION();
  248. // Get the contacts in this island
  249. uint32 *contacts_start, *contacts_end;
  250. inIslandBuilder.GetContactsInIsland(inIslandIndex, contacts_start, contacts_end);
  251. uint num_contacts_in_island = uint(contacts_end - contacts_start);
  252. // Get the constraints in this island
  253. uint32 *constraints_start, *constraints_end;
  254. inIslandBuilder.GetConstraintsInIsland(inIslandIndex, constraints_start, constraints_end);
  255. uint num_constraints_in_island = uint(constraints_end - constraints_start);
  256. // Check if it exceeds the threshold
  257. uint island_size = num_contacts_in_island + num_constraints_in_island;
  258. if (island_size < cLargeIslandTreshold)
  259. return false;
  260. // Get bodies in this island
  261. BodyID *bodies_start, *bodies_end;
  262. inIslandBuilder.GetBodiesInIsland(inIslandIndex, bodies_start, bodies_end);
  263. // Reset the split mask for all bodies in this island
  264. Body const * const *bodies = inBodyManager.GetBodies().data();
  265. for (const BodyID *b = bodies_start; b < bodies_end; ++b)
  266. mSplitMasks[bodies[b->GetIndex()]->GetIndexInActiveBodiesInternal()] = 0;
  267. // Count the number of contacts and constraints per split
  268. uint num_contacts_in_split[cNumSplits] = { };
  269. uint num_constraints_in_split[cNumSplits] = { };
  270. // Get space to store split indices
  271. uint offset = mContactAndConstraintsNextFree.fetch_add(island_size, memory_order_relaxed);
  272. uint32 *contact_split_idx = mContactAndConstraintsSplitIdx + offset;
  273. uint32 *constraint_split_idx = contact_split_idx + num_contacts_in_island;
  274. // Assign the contacts to a split
  275. uint32 *cur_contact_split_idx = contact_split_idx;
  276. for (const uint32 *c = contacts_start; c < contacts_end; ++c)
  277. {
  278. const Body *body1, *body2;
  279. inContactManager.GetAffectedBodies(*c, body1, body2);
  280. uint split = AssignSplit(body1, body2);
  281. num_contacts_in_split[split]++;
  282. *cur_contact_split_idx++ = split;
  283. if (body1->IsDynamic())
  284. ioStepsCalculator(body1->GetMotionPropertiesUnchecked());
  285. if (body2->IsDynamic())
  286. ioStepsCalculator(body2->GetMotionPropertiesUnchecked());
  287. }
  288. // Assign the constraints to a split
  289. uint32 *cur_constraint_split_idx = constraint_split_idx;
  290. for (const uint32 *c = constraints_start; c < constraints_end; ++c)
  291. {
  292. const Constraint *constraint = inActiveConstraints[*c];
  293. uint split = constraint->BuildIslandSplits(*this);
  294. num_constraints_in_split[split]++;
  295. *cur_constraint_split_idx++ = split;
  296. ioStepsCalculator(constraint);
  297. }
  298. ioStepsCalculator.Finalize();
  299. // Start with 0 splits
  300. uint split_remap_table[cNumSplits];
  301. uint new_split_idx = mNextSplitIsland.fetch_add(1, memory_order_relaxed);
  302. JPH_ASSERT(new_split_idx < mNumSplitIslands);
  303. Splits &splits = mSplitIslands[new_split_idx];
  304. splits.mIslandIndex = inIslandIndex;
  305. splits.mNumSplits = 0;
  306. splits.mNumIterations = ioStepsCalculator.GetNumVelocitySteps() + 1; // Iteration 0 is used for warm starting
  307. splits.mNumVelocitySteps = ioStepsCalculator.GetNumVelocitySteps();
  308. splits.mNumPositionSteps = ioStepsCalculator.GetNumPositionSteps();
  309. splits.mItemsProcessed.store(0, memory_order_release);
  310. // Allocate space to store the sorted constraint and contact indices per split
  311. uint32 *constraint_buffer_cur[cNumSplits], *contact_buffer_cur[cNumSplits];
  312. for (uint s = 0; s < cNumSplits; ++s)
  313. {
  314. // If this split doesn't contain enough constraints and contacts, we will combine it with the non parallel split
  315. if (num_constraints_in_split[s] + num_contacts_in_split[s] < cSplitCombineTreshold
  316. && s < cNonParallelSplitIdx) // The non-parallel split cannot merge into itself
  317. {
  318. // Remap it
  319. split_remap_table[s] = cNonParallelSplitIdx;
  320. // Add the counts to the non parallel split
  321. num_contacts_in_split[cNonParallelSplitIdx] += num_contacts_in_split[s];
  322. num_constraints_in_split[cNonParallelSplitIdx] += num_constraints_in_split[s];
  323. }
  324. else
  325. {
  326. // This split is valid, map it to the next empty slot
  327. uint target_split;
  328. if (s < cNonParallelSplitIdx)
  329. target_split = splits.mNumSplits++;
  330. else
  331. target_split = cNonParallelSplitIdx;
  332. Split &split = splits.mSplits[target_split];
  333. split_remap_table[s] = target_split;
  334. // Allocate space for contacts
  335. split.mContactBufferBegin = offset;
  336. split.mContactBufferEnd = split.mContactBufferBegin + num_contacts_in_split[s];
  337. // Allocate space for constraints
  338. split.mConstraintBufferBegin = split.mContactBufferEnd;
  339. split.mConstraintBufferEnd = split.mConstraintBufferBegin + num_constraints_in_split[s];
  340. // Store start for each split
  341. contact_buffer_cur[target_split] = mContactAndConstraintIndices + split.mContactBufferBegin;
  342. constraint_buffer_cur[target_split] = mContactAndConstraintIndices + split.mConstraintBufferBegin;
  343. // Update offset
  344. offset = split.mConstraintBufferEnd;
  345. }
  346. }
  347. // Split the contacts
  348. for (uint c = 0; c < num_contacts_in_island; ++c)
  349. {
  350. uint split = split_remap_table[contact_split_idx[c]];
  351. *contact_buffer_cur[split]++ = contacts_start[c];
  352. }
  353. // Split the constraints
  354. for (uint c = 0; c < num_constraints_in_island; ++c)
  355. {
  356. uint split = split_remap_table[constraint_split_idx[c]];
  357. *constraint_buffer_cur[split]++ = constraints_start[c];
  358. }
  359. #ifdef JPH_LARGE_ISLAND_SPLITTER_DEBUG
  360. // Trace the size of all splits
  361. uint sum = 0;
  362. String stats;
  363. for (uint s = 0; s < cNumSplits; ++s)
  364. {
  365. // If we've processed all splits, jump to the non-parallel split
  366. if (s >= splits.GetNumSplits())
  367. s = cNonParallelSplitIdx;
  368. const Split &split = splits.mSplits[s];
  369. stats += StringFormat("g:%d:%d:%d, ", s, split.GetNumContacts(), split.GetNumConstraints());
  370. sum += split.GetNumItems();
  371. }
  372. stats += StringFormat("sum: %d", sum);
  373. Trace(stats.c_str());
  374. #endif // JPH_LARGE_ISLAND_SPLITTER_DEBUG
  375. #ifdef JPH_ENABLE_ASSERTS
  376. for (uint s = 0; s < cNumSplits; ++s)
  377. {
  378. // If there are no more splits, process the non-parallel split
  379. if (s >= splits.mNumSplits)
  380. s = cNonParallelSplitIdx;
  381. // Check that we wrote all elements
  382. Split &split = splits.mSplits[s];
  383. JPH_ASSERT(contact_buffer_cur[s] == mContactAndConstraintIndices + split.mContactBufferEnd);
  384. JPH_ASSERT(constraint_buffer_cur[s] == mContactAndConstraintIndices + split.mConstraintBufferEnd);
  385. }
  386. #ifdef _DEBUG
  387. // Validate that the splits are indeed not touching the same body
  388. for (uint s = 0; s < splits.mNumSplits; ++s)
  389. {
  390. Array<bool> body_used(mNumActiveBodies, false);
  391. // Validate contacts
  392. uint32 split_contacts_begin, split_contacts_end;
  393. splits.GetContactsInSplit(s, split_contacts_begin, split_contacts_end);
  394. for (uint32 *c = mContactAndConstraintIndices + split_contacts_begin; c < mContactAndConstraintIndices + split_contacts_end; ++c)
  395. {
  396. const Body *body1, *body2;
  397. inContactManager.GetAffectedBodies(*c, body1, body2);
  398. uint32 idx1 = body1->GetIndexInActiveBodiesInternal();
  399. if (idx1 != Body::cInactiveIndex && body1->IsDynamic())
  400. {
  401. JPH_ASSERT(!body_used[idx1]);
  402. body_used[idx1] = true;
  403. }
  404. uint32 idx2 = body2->GetIndexInActiveBodiesInternal();
  405. if (idx2 != Body::cInactiveIndex && body2->IsDynamic())
  406. {
  407. JPH_ASSERT(!body_used[idx2]);
  408. body_used[idx2] = true;
  409. }
  410. }
  411. }
  412. #endif // _DEBUG
  413. #endif // JPH_ENABLE_ASSERTS
  414. // Allow other threads to pick up this split island now
  415. splits.StartFirstBatch();
  416. return true;
  417. }
  418. LargeIslandSplitter::EStatus LargeIslandSplitter::FetchNextBatch(uint &outSplitIslandIndex, uint32 *&outConstraintsBegin, uint32 *&outConstraintsEnd, uint32 *&outContactsBegin, uint32 *&outContactsEnd, bool &outFirstIteration)
  419. {
  420. // We can't be done when all islands haven't been submitted yet
  421. uint num_splits_created = mNextSplitIsland.load(memory_order_acquire);
  422. bool all_done = num_splits_created == mNumSplitIslands;
  423. // Loop over all split islands to find work
  424. uint32 constraints_begin, constraints_end, contacts_begin, contacts_end;
  425. for (Splits *s = mSplitIslands; s < mSplitIslands + num_splits_created; ++s)
  426. switch (s->FetchNextBatch(constraints_begin, constraints_end, contacts_begin, contacts_end, outFirstIteration))
  427. {
  428. case EStatus::AllBatchesDone:
  429. break;
  430. case EStatus::WaitingForBatch:
  431. all_done = false;
  432. break;
  433. case EStatus::BatchRetrieved:
  434. outSplitIslandIndex = uint(s - mSplitIslands);
  435. outConstraintsBegin = mContactAndConstraintIndices + constraints_begin;
  436. outConstraintsEnd = mContactAndConstraintIndices + constraints_end;
  437. outContactsBegin = mContactAndConstraintIndices + contacts_begin;
  438. outContactsEnd = mContactAndConstraintIndices + contacts_end;
  439. return EStatus::BatchRetrieved;
  440. }
  441. return all_done? EStatus::AllBatchesDone : EStatus::WaitingForBatch;
  442. }
  443. void LargeIslandSplitter::MarkBatchProcessed(uint inSplitIslandIndex, const uint32 *inConstraintsBegin, const uint32 *inConstraintsEnd, const uint32 *inContactsBegin, const uint32 *inContactsEnd, bool &outLastIteration, bool &outFinalBatch)
  444. {
  445. uint num_items_processed = uint(inConstraintsEnd - inConstraintsBegin) + uint(inContactsEnd - inContactsBegin);
  446. JPH_ASSERT(inSplitIslandIndex < mNextSplitIsland.load(memory_order_relaxed));
  447. Splits &splits = mSplitIslands[inSplitIslandIndex];
  448. splits.MarkBatchProcessed(num_items_processed, outLastIteration, outFinalBatch);
  449. }
  450. void LargeIslandSplitter::PrepareForSolvePositions()
  451. {
  452. for (Splits *s = mSplitIslands, *s_end = mSplitIslands + mNumSplitIslands; s < s_end; ++s)
  453. {
  454. // Set the number of iterations to the number of position steps
  455. s->mNumIterations = s->mNumPositionSteps;
  456. // We can start again from the first batch
  457. s->StartFirstBatch();
  458. }
  459. }
  460. void LargeIslandSplitter::Reset(TempAllocator *inTempAllocator)
  461. {
  462. JPH_PROFILE_FUNCTION();
  463. // Everything should have been used
  464. JPH_ASSERT(mContactAndConstraintsNextFree.load(memory_order_relaxed) == mContactAndConstraintsSize);
  465. JPH_ASSERT(mNextSplitIsland.load(memory_order_relaxed) == mNumSplitIslands);
  466. // Free split islands
  467. if (mNumSplitIslands > 0)
  468. {
  469. inTempAllocator->Free(mSplitIslands, mNumSplitIslands * sizeof(Splits));
  470. mSplitIslands = nullptr;
  471. mNumSplitIslands = 0;
  472. mNextSplitIsland.store(0, memory_order_relaxed);
  473. }
  474. // Free contact and constraint buffers
  475. if (mContactAndConstraintsSize > 0)
  476. {
  477. inTempAllocator->Free(mContactAndConstraintIndices, mContactAndConstraintsSize * sizeof(uint32));
  478. mContactAndConstraintIndices = nullptr;
  479. inTempAllocator->Free(mContactAndConstraintsSplitIdx, mContactAndConstraintsSize * sizeof(uint32));
  480. mContactAndConstraintsSplitIdx = nullptr;
  481. mContactAndConstraintsSize = 0;
  482. mContactAndConstraintsNextFree.store(0, memory_order_relaxed);
  483. }
  484. // Free split masks
  485. if (mSplitMasks != nullptr)
  486. {
  487. inTempAllocator->Free(mSplitMasks, mNumActiveBodies * sizeof(SplitMask));
  488. mSplitMasks = nullptr;
  489. mNumActiveBodies = 0;
  490. }
  491. }
  492. JPH_NAMESPACE_END