Skip to content

Commit f3f11b6

Browse files
committed
wip 2
1 parent 78d8e06 commit f3f11b6

19 files changed

Lines changed: 123 additions & 123 deletions

src/Box2D.NET.Samples/Graphics/SolidPolygonRender.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public struct SolidPolygonRender
1919

2020
public SolidPolygonRender()
2121
{
22-
polygons = new List<PolygonData>();
22+
polygons = new List<PolygonData>(10 * SolidPolygons.e_batchSize);
2323
vaoId = new uint[1];
2424
vboIds = new uint[2];
2525
}

src/Box2D.NET.Samples/Samples/Stackings/CircleImpulse.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace Box2D.NET.Samples.Samples.Stackings;
1818

1919
public class CircleImpulse : Sample
2020
{
21-
private static readonly int SampleCircleImpulse = SampleFactory.Shared.RegisterSample("Stacking", "Circle Impulse", Create);
21+
private static readonly int SampleCircleImpulse = SampleFactory.Shared.RegisterSample("Events", "Circle Impulse", Create);
2222

2323
public struct Event
2424
{
@@ -161,4 +161,4 @@ public override void Step()
161161
DrawTextLine($"hit speed = {e.speed}, hit momentum = {m_mass * e.speed}, final impulse = {e.impulse}, total impulse = {e.totalImpulse}");
162162
}
163163
}
164-
}
164+
}

src/Box2D.NET/B2ArenaAllocators.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ namespace Box2D.NET
1111
{
1212
public static class B2ArenaAllocators
1313
{
14-
public static B2ArenaAllocator b2CreateArenaAllocator(int capacity)
14+
public static B2StackAllocator b2CreateStackAllocator(int capacity)
1515
{
16-
var allocator = new B2ArenaAllocator(capacity);
16+
var allocator = new B2StackAllocator(capacity);
1717
return allocator;
1818
}
1919

20-
public static void b2DestroyArenaAllocator(B2ArenaAllocator allocator)
20+
public static void b2DestroyStackAllocator(B2StackAllocator allocator)
2121
{
2222
var allocs = allocator.AsSpan();
2323
for (int i = 0; i < allocs.Length; ++i)
@@ -26,26 +26,26 @@ public static void b2DestroyArenaAllocator(B2ArenaAllocator allocator)
2626
}
2727
}
2828

29-
public static B2ArenaAllocatorTyped<T> b2CreateArenaAllocator<T>(int capacity) where T : new()
29+
public static B2Stack<T> b2CreateStack<T>(int capacity) where T : new()
3030
{
3131
B2_ASSERT(capacity >= 0);
32-
B2ArenaAllocatorTyped<T> allocatorImpl = new B2ArenaAllocatorTyped<T>();
32+
B2Stack<T> allocatorImpl = new B2Stack<T>();
3333
allocatorImpl.capacity = capacity;
3434
allocatorImpl.data = b2Alloc<T>(capacity);
3535
allocatorImpl.allocation = 0;
3636
allocatorImpl.maxAllocation = 0;
3737
allocatorImpl.index = 0;
38-
allocatorImpl.entries = b2Array_Create<B2ArenaEntry<T>>(capacity);
38+
allocatorImpl.entries = b2Array_Create<B2StackEntry<T>>(capacity);
3939
return allocatorImpl;
4040
}
4141

42-
public static ArraySegment<T> b2AllocateArenaItem<T>(B2ArenaAllocator allocator, int size, string name) where T : new()
42+
public static ArraySegment<T> b2StackAlloc<T>(B2StackAllocator allocator, int size, string name) where T : new()
4343
{
4444
var alloc = allocator.GetOrCreateFor<T>();
4545
// ensure allocation is 32 byte aligned to support 256-bit SIMD
4646
int size32 = ((size - 1) | 0x1F) + 1;
4747

48-
B2ArenaEntry<T> entry = new B2ArenaEntry<T>();
48+
B2StackEntry<T> entry = new B2StackEntry<T>();
4949
entry.size = size32;
5050
entry.name = name;
5151
if (alloc.index + size32 > alloc.capacity)
@@ -75,12 +75,12 @@ public static void b2DestroyArenaAllocator(B2ArenaAllocator allocator)
7575
return entry.data;
7676
}
7777

78-
public static void b2FreeArenaItem<T>(B2ArenaAllocator allocator, ArraySegment<T> mem) where T : new()
78+
public static void b2StackFree<T>(B2StackAllocator allocator, ArraySegment<T> mem) where T : new()
7979
{
8080
var alloc = allocator.GetOrCreateFor<T>();
8181
int entryCount = alloc.entries.count;
8282
B2_ASSERT(entryCount > 0);
83-
ref B2ArenaEntry<T> entry = ref alloc.entries.data[entryCount - 1];
83+
ref B2StackEntry<T> entry = ref alloc.entries.data[entryCount - 1];
8484
B2_ASSERT(mem == entry.data);
8585
if (entry.usedMalloc)
8686
{
@@ -94,8 +94,8 @@ public static void b2DestroyArenaAllocator(B2ArenaAllocator allocator)
9494
alloc.allocation -= entry.size;
9595
b2Array_Pop(ref alloc.entries);
9696
}
97-
// Grow the arena based on usage
98-
public static void b2GrowArena(B2ArenaAllocator allocator)
97+
// Grow the stack based on usage
98+
public static void b2GrowStack(B2StackAllocator allocator)
9999
{
100100
var allocSpan = allocator.AsSpan();
101101

@@ -106,8 +106,8 @@ public static void b2GrowArena(B2ArenaAllocator allocator)
106106
}
107107
}
108108

109-
// Grow the arena based on usage
110-
public static int b2GetArenaCapacity(B2ArenaAllocator allocator)
109+
// Grow the stack based on usage
110+
public static int b2GetStackCapacity(B2StackAllocator allocator)
111111
{
112112
int capacity = 0;
113113
var allocSpan = allocator.AsSpan();
@@ -119,7 +119,7 @@ public static int b2GetArenaCapacity(B2ArenaAllocator allocator)
119119
return capacity;
120120
}
121121

122-
public static int b2GetArenaAllocation(B2ArenaAllocator allocator)
122+
public static int b2GetStackAllocation(B2StackAllocator allocator)
123123
{
124124
int allocation = 0;
125125
var allocSpan = allocator.AsSpan();
@@ -131,7 +131,7 @@ public static int b2GetArenaAllocation(B2ArenaAllocator allocator)
131131
return allocation;
132132
}
133133

134-
public static int b2GetMaxArenaAllocation(B2ArenaAllocator allocator)
134+
public static int b2GetMaxStackAllocation(B2StackAllocator allocator)
135135
{
136136
int maxAllocation = 0;
137137
var allocSpan = allocator.AsSpan();

src/Box2D.NET/B2Arrays.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ public static ref T b2Array_Get<T>(ref B2Array<T> a, int index)
5656
return ref a.data[index];
5757
}
5858

59-
/* Add */
59+
/* Emplace */
6060
[MethodImpl(MethodImplOptions.AggressiveInlining)]
61-
public static ref T b2Array_Add<T>(ref B2Array<T> a) where T : new()
61+
public static ref T b2Array_Emplace<T>(ref B2Array<T> a) where T : new()
6262
{
63-
if (a.count == a.capacity)
63+
if (a.count >= a.capacity)
6464
{
65-
int newCapacity = a.capacity < 2 ? 2 : a.capacity + (a.capacity >> 1);
65+
int newCapacity = a.capacity == 0 ? 8 : 2 * a.capacity;
6666
b2Array_Reserve(ref a, newCapacity);
6767
}
6868

@@ -74,9 +74,9 @@ public static ref T b2Array_Get<T>(ref B2Array<T> a, int index)
7474
[MethodImpl(MethodImplOptions.AggressiveInlining)]
7575
public static void b2Array_Push<T>(ref B2Array<T> a, T value) where T : new()
7676
{
77-
if (a.count == a.capacity)
77+
if (a.count >= a.capacity)
7878
{
79-
int newCapacity = a.capacity < 2 ? 2 : a.capacity + (a.capacity >> 1);
79+
int newCapacity = a.capacity == 0 ? 8 : 2 * a.capacity;
8080
b2Array_Reserve(ref a, newCapacity);
8181
}
8282

@@ -211,4 +211,4 @@ public static void b2Array_Destroy<T>(ref B2Array<T> a)
211211
a.count = n;
212212
}
213213
}
214-
}
214+
}

src/Box2D.NET/B2Bodies.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ public static B2BodyId b2CreateBody(B2WorldId worldId, in B2BodyDef def)
258258

259259

260260
B2SolverSet set = b2Array_Get(ref world.solverSets, setId);
261-
ref B2BodySim bodySim = ref b2Array_Add(ref set.bodySims);
261+
ref B2BodySim bodySim = ref b2Array_Emplace(ref set.bodySims);
262262
//*bodySim = ( b2BodySim ){ 0 };
263263
bodySim.Clear();
264264
bodySim.transform.p = def.position;
@@ -280,7 +280,7 @@ public static B2BodyId b2CreateBody(B2WorldId worldId, in B2BodyDef def)
280280

281281
if (setId == (int)B2SolverSetType.b2_awakeSet)
282282
{
283-
ref B2BodyState bodyState = ref b2Array_Add(ref set.bodyStates);
283+
ref B2BodyState bodyState = ref b2Array_Emplace(ref set.bodyStates);
284284
//B2_ASSERT( ( (uintptr_t)bodyState & 0x1F ) == 0 );
285285
//*bodyState = ( b2BodyState ){ 0 };
286286
bodyState.Clear();
@@ -581,7 +581,7 @@ internal static void b2UpdateBodyMassData(B2World world, B2Body body)
581581
}
582582

583583
int shapeCount = body.shapeCount;
584-
ArraySegment<B2MassData> masses = b2AllocateArenaItem<B2MassData>(world.arena, shapeCount, "mass data");
584+
ArraySegment<B2MassData> masses = b2StackAlloc<B2MassData>(world.stack, shapeCount, "mass data");
585585

586586
// Accumulate mass over all shapes.
587587
B2Vec2 localCenter = b2Vec2_zero;
@@ -629,7 +629,7 @@ internal static void b2UpdateBodyMassData(B2World world, B2Body body)
629629
body.inertia += inertia;
630630
}
631631

632-
b2FreeArenaItem(world.arena, masses);
632+
b2StackFree(world.stack, masses);
633633
masses = null;
634634

635635
B2_ASSERT(body.inertia >= 0.0f);

src/Box2D.NET/B2ConstraintGraphs.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ internal static void b2AddContactToGraph(B2World world, B2ContactSim contactSim,
171171
contact.colorIndex = colorIndex;
172172
contact.localIndex = color0.contactSims.count;
173173

174-
ref B2ContactSim newContact = ref b2Array_Add(ref color0.contactSims);
174+
ref B2ContactSim newContact = ref b2Array_Emplace(ref color0.contactSims);
175175
//memcpy( newContact, contactSim, sizeof( b2ContactSim ) );
176176
newContact.CopyFrom(contactSim);
177177

@@ -317,7 +317,7 @@ internal static ref B2JointSim b2CreateJointInGraph(B2World world, B2Joint joint
317317

318318
int colorIndex = b2AssignJointColor(ref graph, bodyIdA, bodyIdB, bodyA.type, bodyB.type);
319319

320-
ref B2JointSim jointSim = ref b2Array_Add(ref graph.colors[colorIndex].jointSims);
320+
ref B2JointSim jointSim = ref b2Array_Emplace(ref graph.colors[colorIndex].jointSims);
321321
//memset( jointSim, 0, sizeof( b2JointSim ) );
322322
jointSim.Clear();
323323

src/Box2D.NET/B2Contacts.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ public static void b2CreateContact(B2World world, B2Shape shapeA, B2Shape shapeB
265265

266266
// Contacts are created as non-touching. Later if they are found to be touching
267267
// they will link islands and be moved into the constraint graph.
268-
ref B2ContactSim contactSim = ref b2Array_Add(ref set.contactSims);
268+
ref B2ContactSim contactSim = ref b2Array_Emplace(ref set.contactSims);
269269
contactSim.contactId = contactId;
270270

271271
#if DEBUG

src/Box2D.NET/B2Islands.cs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public static B2Island b2CreateIsland(B2World world, int setIndex)
5252
island.joints = b2Array_Create<B2JointLink>();
5353
island.constraintRemoveCount = 0;
5454

55-
ref B2IslandSim islandSim = ref b2Array_Add(ref set.islandSims);
55+
ref B2IslandSim islandSim = ref b2Array_Emplace(ref set.islandSims);
5656
islandSim.islandId = islandId;
5757

5858
return island;
@@ -430,14 +430,14 @@ public static void b2SplitIsland(B2World world, int baseId)
430430
B2JointLink[] baseJoints = baseIsland.joints.data;
431431
int baseJointCapacity = baseIsland.joints.capacity;
432432

433-
B2ArenaAllocator alloc = world.arena;
433+
B2StackAllocator alloc = world.stack;
434434

435435
// No lock is needed because I ensure the allocator is not used while this task is active.
436436
// Allocate contactCounts and jointCounts before ranks so ranks can be freed first (LIFO arena).
437-
ArraySegment<int> parents = b2AllocateArenaItem<int>(alloc, baseBodyCount, "parents");
438-
ArraySegment<int> contactCounts = b2AllocateArenaItem<int>(alloc, baseBodyCount, "contact counts");
439-
ArraySegment<int> jointCounts = b2AllocateArenaItem<int>(alloc, baseBodyCount, "joint counts");
440-
ArraySegment<int> ranks = b2AllocateArenaItem<int>(alloc, baseBodyCount, "ranks");
437+
ArraySegment<int> parents = b2StackAlloc<int>(alloc, baseBodyCount, "parents");
438+
ArraySegment<int> contactCounts = b2StackAlloc<int>(alloc, baseBodyCount, "contact counts");
439+
ArraySegment<int> jointCounts = b2StackAlloc<int>(alloc, baseBodyCount, "joint counts");
440+
ArraySegment<int> ranks = b2StackAlloc<int>(alloc, baseBodyCount, "ranks");
441441
for (int i = 0; i < baseBodyCount; ++i)
442442
{
443443
parents[i] = i;
@@ -507,7 +507,7 @@ public static void b2SplitIsland(B2World world, int baseId)
507507
}
508508

509509
// Done with ranks
510-
b2FreeArenaItem(alloc, ranks);
510+
b2StackFree(alloc, ranks);
511511
ranks = null;
512512

513513
// Flatten all parent indices and count connected components.
@@ -525,9 +525,9 @@ public static void b2SplitIsland(B2World world, int baseId)
525525
if (componentCount == 1)
526526
{
527527
baseIsland.constraintRemoveCount = 0;
528-
b2FreeArenaItem(alloc, jointCounts);
529-
b2FreeArenaItem(alloc, contactCounts);
530-
b2FreeArenaItem(alloc, parents);
528+
b2StackFree(alloc, jointCounts);
529+
b2StackFree(alloc, contactCounts);
530+
b2StackFree(alloc, parents);
531531
return;
532532
}
533533

@@ -548,15 +548,15 @@ public static void b2SplitIsland(B2World world, int baseId)
548548
baseIsland = null;
549549

550550
// Map from body index to new island index. Only set for root bodies.
551-
ArraySegment<int> rootMap = b2AllocateArenaItem<int>(alloc, baseBodyCount, "root map");
551+
ArraySegment<int> rootMap = b2StackAlloc<int>(alloc, baseBodyCount, "root map");
552552
for (int i = 0; i < baseBodyCount; ++i)
553553
{
554554
rootMap[i] = B2_NULL_INDEX;
555555
}
556556

557-
ArraySegment<int> componentBodyCounts = b2AllocateArenaItem<int>(alloc, componentCount, "component body counts");
558-
ArraySegment<int> componentContactCounts = b2AllocateArenaItem<int>(alloc, componentCount, "component contact counts");
559-
ArraySegment<int> componentJointCounts = b2AllocateArenaItem<int>(alloc, componentCount, "component joint counts");
557+
ArraySegment<int> componentBodyCounts = b2StackAlloc<int>(alloc, componentCount, "component body counts");
558+
ArraySegment<int> componentContactCounts = b2StackAlloc<int>(alloc, componentCount, "component contact counts");
559+
ArraySegment<int> componentJointCounts = b2StackAlloc<int>(alloc, componentCount, "component joint counts");
560560
int islandCount = 0;
561561

562562
// Find the root body for each body and create islands as needed.
@@ -579,7 +579,7 @@ public static void b2SplitIsland(B2World world, int baseId)
579579
B2_ASSERT(islandCount == componentCount);
580580

581581
// Map from new island index to island id
582-
ArraySegment<int> islandIds = b2AllocateArenaItem<int>(alloc, islandCount, "island ids");
582+
ArraySegment<int> islandIds = b2StackAlloc<int>(alloc, islandCount, "island ids");
583583

584584
// Create new islands and reserve body/contact/joint arrays
585585
for (int i = 0; i < islandCount; ++i)
@@ -661,14 +661,14 @@ public static void b2SplitIsland(B2World world, int baseId)
661661
b2Free(baseJoints, baseJointCapacity);
662662

663663
// Free arena items in LIFO order
664-
b2FreeArenaItem(alloc, islandIds);
665-
b2FreeArenaItem(alloc, componentJointCounts);
666-
b2FreeArenaItem(alloc, componentContactCounts);
667-
b2FreeArenaItem(alloc, componentBodyCounts);
668-
b2FreeArenaItem(alloc, rootMap);
669-
b2FreeArenaItem(alloc, jointCounts);
670-
b2FreeArenaItem(alloc, contactCounts);
671-
b2FreeArenaItem(alloc, parents);
664+
b2StackFree(alloc, islandIds);
665+
b2StackFree(alloc, componentJointCounts);
666+
b2StackFree(alloc, componentContactCounts);
667+
b2StackFree(alloc, componentBodyCounts);
668+
b2StackFree(alloc, rootMap);
669+
b2StackFree(alloc, jointCounts);
670+
b2StackFree(alloc, contactCounts);
671+
b2StackFree(alloc, parents);
672672
}
673673

674674
// Split an island because some contacts and/or joints have been removed.

src/Box2D.NET/B2Joints.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ public static B2JointPair b2CreateJoint(B2World world, in B2JointDef def, B2Join
280280
joint.setIndex = (int)B2SolverSetType.b2_disabledSet;
281281
joint.localIndex = set.jointSims.count;
282282

283-
jointSim = b2Array_Add(ref set.jointSims);
283+
jointSim = b2Array_Emplace(ref set.jointSims);
284284
//memset( jointSim, 0, sizeof( b2JointSim ) );
285285
jointSim.Clear();
286286

@@ -295,7 +295,7 @@ public static B2JointPair b2CreateJoint(B2World world, in B2JointDef def, B2Join
295295
joint.setIndex = (int)B2SolverSetType.b2_staticSet;
296296
joint.localIndex = set.jointSims.count;
297297

298-
jointSim = b2Array_Add(ref set.jointSims);
298+
jointSim = b2Array_Emplace(ref set.jointSims);
299299
//memset( jointSim, 0, sizeof( b2JointSim ) );
300300
jointSim.Clear();
301301

@@ -331,7 +331,7 @@ public static B2JointPair b2CreateJoint(B2World world, in B2JointDef def, B2Join
331331
joint.setIndex = setIndex;
332332
joint.localIndex = set.jointSims.count;
333333

334-
jointSim = b2Array_Add(ref set.jointSims);
334+
jointSim = b2Array_Emplace(ref set.jointSims);
335335
//memset( jointSim, 0, sizeof( b2JointSim ) );
336336
jointSim.Clear();
337337

src/Box2D.NET/B2Sensors.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ internal static bool b2SensorQueryCallback(int proxyId, ulong userData, ref B2Se
109109

110110
// Record the overlap
111111
B2Sensor sensor = queryContext.sensor;
112-
ref B2Visitor shapeRef = ref b2Array_Add(ref sensor.overlaps2);
112+
ref B2Visitor shapeRef = ref b2Array_Emplace(ref sensor.overlaps2);
113113
shapeRef.shapeId = shapeId;
114114
shapeRef.generation = otherShape.generation;
115115

0 commit comments

Comments
 (0)