Skip to content
Merged

Scatter #1140

Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ fn compileBox2d(b: *Build, target: ResolvedTarget, optimize: OptimizeMode, optio
"src/motor_joint.c",
"src/mover_joint.c",
"src/mover.c",
"src/parallel_for.c",
"src/physics_world.c",
"src/pogo_joint.c",
"src/prismatic_joint.c",
Expand All @@ -146,6 +145,7 @@ fn compileBox2d(b: *Build, target: ResolvedTarget, optimize: OptimizeMode, optio
"src/solver_set.c",
"src/solver.c",
"src/table.c",
"src/task.c",
"src/timer.c",
"src/types.c",
"src/weld_joint.c",
Expand Down
4 changes: 2 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ set(BOX2D_SOURCE_FILES
motor_joint.c
mover_joint.c
mover.c
parallel_for.c
parallel_for.h
physics_world.c
physics_world.h
platform.h
Expand Down Expand Up @@ -73,6 +71,8 @@ set(BOX2D_SOURCE_FILES
solver.h
table.c
table.h
task.c
task.h
timer.c
types.c
verstable.h
Expand Down
15 changes: 9 additions & 6 deletions src/broad_phase.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
#include "contact.h"
#include "core.h"
#include "dynamic_tree.h"
#include "parallel_for.h"
#include "physics_world.h"
#include "platform.h"
#include "shape.h"
#include "simd.h"
#include "task.h"

#include <stdbool.h>
#include <string.h>
Expand Down Expand Up @@ -53,8 +53,8 @@ void b2DestroyBroadPhase( b2BroadPhase* bp )
memset( bp, 0, sizeof( b2BroadPhase ) );
}

int b2BroadPhase_CreateProxy( b2BroadPhase* bp, b2BodyType proxyType, b2AABB aabb, uint64_t categoryBits, int shapeIndex,
bool forcePairCreation )
int b2BroadPhase_CreateProxy( b2BroadPhase* bp, b2BodyType proxyType, b2AABB aabb, uint64_t categoryBits,
int shapeIndex, bool forcePairCreation )
{
B2_ASSERT( 0 <= proxyType && proxyType < b2_bodyTypeCount );

Expand Down Expand Up @@ -269,7 +269,8 @@ B2_FORCE_INLINE bool b2TestPair( const b2TreeNode* a, const b2TreeNode* b )
return b2AABB_Overlaps( a->aabb, b->aabb );
}

static void b2CollideProxyAndSubtree( const b2TreeNode* proxy, const b2TreeNode* nodes, int pair, b2PairContext* context )
static void b2CollideProxyAndSubtree( const b2TreeNode* proxy, const b2TreeNode* nodes, int pair,
b2PairContext* context )
{
uint32_t proxyMark = proxy->flagIndex & B2_MOVED_NODE;
b2AABBV boxv = b2LoadAABBV( &proxy->aabb );
Expand Down Expand Up @@ -317,7 +318,8 @@ static void b2CollideProxyAndSubtree( const b2TreeNode* proxy, const b2TreeNode*
}

B2_FORCE_INLINE void b2ExpandPair( const b2TreeNode* arrayA, const b2TreeNode* arrayB, const b2TreeNode* nodeA,
const b2TreeNode* nodeB, b2IndexPair* stack, int* stackCount, b2PairContext* context )
const b2TreeNode* nodeB, b2IndexPair* stack, int* stackCount,
b2PairContext* context )
{
bool leafA = b2IsLeaf( nodeA );
bool leafB = b2IsLeaf( nodeB );
Expand Down Expand Up @@ -634,7 +636,8 @@ bool b2BeginPairs( b2World* world )
void b2StageSelfPairs( b2World* world )
{
// Collide the dynamic body tree against itself.
b2StageParallelFor( world, b2_taskPairs, &b2SelfPairsTask, world->broadPhase.crossContext->dynamicMoveCount, 64, world );
b2StageParallelFor( world, b2_taskPairs, &b2SelfPairsTask, world->broadPhase.crossContext->dynamicMoveCount, 64,
world );
}

void b2EndPairs( b2World* world )
Expand Down
4 changes: 2 additions & 2 deletions src/constraint_graph.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,8 @@ void b2RemoveContactFromGraph( b2World* world, int bodyIdA, int bodyIdB, int col
}
}

// Notice that a joint cannot share the same color as a contact between the same two bodies. This means I can solve contacts and
// joints in parallel with each other within each color.
// Notice that a joint cannot share the same color as a contact between the same two bodies. This means I can solve
// contacts and joints in parallel with each other within each color.
static int b2AssignJointColor( b2ConstraintGraph* graph, int bodyIdA, int bodyIdB, b2BodyType typeA, b2BodyType typeB )
{
B2_ASSERT( typeA == b2_dynamicBody || typeB == b2_dynamicBody );
Expand Down
5 changes: 5 additions & 0 deletions src/contact.c
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,11 @@ void b2CreateContact( b2World* world, b2Shape* shapeA, b2Shape* shapeB )
{
contactSim->simFlags |= b2_simEnablePreSolveEvents;
}

if ( shapeA->type == b2_chainSegmentShape || shapeB->type == b2_chainSegmentShape )
{
contactSim->simFlags |= b2_simChainSegment;
}
}

// A contact is destroyed when:
Expand Down
3 changes: 3 additions & 0 deletions src/contact.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ enum b2ContactFlags

// Did a body on this contact have a time of impact event?
b2_simHadTimeOfImpact = 0x00800000,

// One of the shapes is a chain segment.
b2_simChainSegment = 0x01000000,
};

// A contact edge is used to connect bodies and contacts together
Expand Down
126 changes: 93 additions & 33 deletions src/contact_solver_wide.inl
Original file line number Diff line number Diff line change
Expand Up @@ -799,28 +799,26 @@ B2_FORCE_INLINE b2BodyStateW b2GatherBodies( const b2BodyState* B2_RESTRICT stat
return simdBody;
}

// This writes everything back to the solver bodies but only the velocities change
// This writes back only the velocities (and the unchanged flags). The contact solver does not modify dp and dq,
// so only the low 16 bytes of each b2BodyState are stored.
B2_FORCE_INLINE void b2ScatterBodies( b2BodyState* B2_RESTRICT states, int* B2_RESTRICT indices,
const b2BodyStateW* B2_RESTRICT simdBody )
{
_Static_assert( sizeof( b2BodyState ) == 32, "b2BodyState not 32 bytes" );
_Static_assert( offsetof( b2BodyState, flags ) == 12, "b2BodyState layout" );
B2_ASSERT( ( (uintptr_t)states & 0x1F ) == 0 );

// 4x4 transpose within each 128-bit lane
b2FloatW t0 = _mm256_unpacklo_ps( simdBody->v.X, simdBody->v.Y );
b2FloatW t1 = _mm256_unpackhi_ps( simdBody->v.X, simdBody->v.Y );
b2FloatW t2 = _mm256_unpacklo_ps( simdBody->w, simdBody->flags );
b2FloatW t3 = _mm256_unpackhi_ps( simdBody->w, simdBody->flags );
b2FloatW t4 = _mm256_unpacklo_ps( simdBody->dp.X, simdBody->dp.Y );
b2FloatW t5 = _mm256_unpackhi_ps( simdBody->dp.X, simdBody->dp.Y );
b2FloatW t6 = _mm256_unpacklo_ps( simdBody->dq.C, simdBody->dq.S );
b2FloatW t7 = _mm256_unpackhi_ps( simdBody->dq.C, simdBody->dq.S );
b2FloatW tt0 = _mm256_shuffle_ps( t0, t2, _MM_SHUFFLE( 1, 0, 1, 0 ) );
b2FloatW tt1 = _mm256_shuffle_ps( t0, t2, _MM_SHUFFLE( 3, 2, 3, 2 ) );
b2FloatW tt2 = _mm256_shuffle_ps( t1, t3, _MM_SHUFFLE( 1, 0, 1, 0 ) );
b2FloatW tt3 = _mm256_shuffle_ps( t1, t3, _MM_SHUFFLE( 3, 2, 3, 2 ) );
b2FloatW tt4 = _mm256_shuffle_ps( t4, t6, _MM_SHUFFLE( 1, 0, 1, 0 ) );
b2FloatW tt5 = _mm256_shuffle_ps( t4, t6, _MM_SHUFFLE( 3, 2, 3, 2 ) );
b2FloatW tt6 = _mm256_shuffle_ps( t5, t7, _MM_SHUFFLE( 1, 0, 1, 0 ) );
b2FloatW tt7 = _mm256_shuffle_ps( t5, t7, _MM_SHUFFLE( 3, 2, 3, 2 ) );

// [body0 | body4], [body1 | body5], [body2 | body6], [body3 | body7]
b2FloatW b04 = _mm256_shuffle_ps( t0, t2, _MM_SHUFFLE( 1, 0, 1, 0 ) );
b2FloatW b15 = _mm256_shuffle_ps( t0, t2, _MM_SHUFFLE( 3, 2, 3, 2 ) );
b2FloatW b26 = _mm256_shuffle_ps( t1, t3, _MM_SHUFFLE( 1, 0, 1, 0 ) );
b2FloatW b37 = _mm256_shuffle_ps( t1, t3, _MM_SHUFFLE( 3, 2, 3, 2 ) );

_Static_assert( b2_dynamicFlag == ( 1 << 9 ), "dynamic flag must be bit 9" );
int dynamicMask =
Expand All @@ -830,56 +828,95 @@ B2_FORCE_INLINE void b2ScatterBodies( b2BodyState* B2_RESTRICT states, int* B2_R
// associated cache flushing.

// zero means null
int i1 = indices[0] - 1;
int i2 = indices[1] - 1;
int i3 = indices[2] - 1;
int i4 = indices[3] - 1;
int i5 = indices[4] - 1;
int i6 = indices[5] - 1;
int i7 = indices[6] - 1;
int i8 = indices[7] - 1;

if ( dynamicMask & 1 )
{
_mm256_store_ps( (float*)( states + i1 ), _mm256_permute2f128_ps( tt0, tt4, 0x20 ) );
_mm_store_ps( (float*)( states + indices[0] - 1 ), _mm256_castps256_ps128( b04 ) );
}

if ( dynamicMask & 2 )
{
_mm256_store_ps( (float*)( states + i2 ), _mm256_permute2f128_ps( tt1, tt5, 0x20 ) );
_mm_store_ps( (float*)( states + indices[1] - 1 ), _mm256_castps256_ps128( b15 ) );
}

if ( dynamicMask & 4 )
{
_mm256_store_ps( (float*)( states + i3 ), _mm256_permute2f128_ps( tt2, tt6, 0x20 ) );
_mm_store_ps( (float*)( states + indices[2] - 1 ), _mm256_castps256_ps128( b26 ) );
}

if ( dynamicMask & 8 )
{
_mm256_store_ps( (float*)( states + i4 ), _mm256_permute2f128_ps( tt3, tt7, 0x20 ) );
_mm_store_ps( (float*)( states + indices[3] - 1 ), _mm256_castps256_ps128( b37 ) );
}

if ( dynamicMask & 16 )
{
_mm256_store_ps( (float*)( states + i5 ), _mm256_permute2f128_ps( tt0, tt4, 0x31 ) );
_mm_store_ps( (float*)( states + indices[4] - 1 ), _mm256_extractf128_ps( b04, 1 ) );
}

if ( dynamicMask & 32 )
{
_mm256_store_ps( (float*)( states + i6 ), _mm256_permute2f128_ps( tt1, tt5, 0x31 ) );
_mm_store_ps( (float*)( states + indices[5] - 1 ), _mm256_extractf128_ps( b15, 1 ) );
}

if ( dynamicMask & 64 )
{
_mm256_store_ps( (float*)( states + i7 ), _mm256_permute2f128_ps( tt2, tt6, 0x31 ) );
_mm_store_ps( (float*)( states + indices[6] - 1 ), _mm256_extractf128_ps( b26, 1 ) );
}

if ( dynamicMask & 128 )
{
_mm256_store_ps( (float*)( states + i8 ), _mm256_permute2f128_ps( tt3, tt7, 0x31 ) );
_mm_store_ps( (float*)( states + indices[7] - 1 ), _mm256_extractf128_ps( b37, 1 ) );
}
}

// Gather only the velocities and flags (low 16 bytes of b2BodyState). Used where dp and dq are not needed.
B2_FORCE_INLINE b2BodyStateW b2GatherVelocities( const b2BodyState* B2_RESTRICT states, int* B2_RESTRICT indices )
{
B2_ASSERT( ( (uintptr_t)states & 0x1F ) == 0 );

// b2_identityBodyState velocity part: v = 0, w = 0, flags = 0
__m128 zero = _mm_setzero_ps();

// zero means null
int i0 = indices[0] - 1;
int i1 = indices[1] - 1;
int i2 = indices[2] - 1;
int i3 = indices[3] - 1;
int i4 = indices[4] - 1;
int i5 = indices[5] - 1;
int i6 = indices[6] - 1;
int i7 = indices[7] - 1;

__m128 a0 = i0 == B2_NULL_INDEX ? zero : _mm_load_ps( (float*)( states + i0 ) );
__m128 a1 = i1 == B2_NULL_INDEX ? zero : _mm_load_ps( (float*)( states + i1 ) );
__m128 a2 = i2 == B2_NULL_INDEX ? zero : _mm_load_ps( (float*)( states + i2 ) );
__m128 a3 = i3 == B2_NULL_INDEX ? zero : _mm_load_ps( (float*)( states + i3 ) );
__m128 a4 = i4 == B2_NULL_INDEX ? zero : _mm_load_ps( (float*)( states + i4 ) );
__m128 a5 = i5 == B2_NULL_INDEX ? zero : _mm_load_ps( (float*)( states + i5 ) );
__m128 a6 = i6 == B2_NULL_INDEX ? zero : _mm_load_ps( (float*)( states + i6 ) );
__m128 a7 = i7 == B2_NULL_INDEX ? zero : _mm_load_ps( (float*)( states + i7 ) );

b2FloatW b04 = _mm256_insertf128_ps( _mm256_castps128_ps256( a0 ), a4, 1 );
b2FloatW b15 = _mm256_insertf128_ps( _mm256_castps128_ps256( a1 ), a5, 1 );
b2FloatW b26 = _mm256_insertf128_ps( _mm256_castps128_ps256( a2 ), a6, 1 );
b2FloatW b37 = _mm256_insertf128_ps( _mm256_castps128_ps256( a3 ), a7, 1 );

// 4x4 transpose within each 128-bit lane
b2FloatW t0 = _mm256_unpacklo_ps( b04, b15 ); // vx0 vx1 vy0 vy1
b2FloatW t1 = _mm256_unpackhi_ps( b04, b15 ); // w0 w1 f0 f1
b2FloatW t2 = _mm256_unpacklo_ps( b26, b37 ); // vx2 vx3 vy2 vy3
b2FloatW t3 = _mm256_unpackhi_ps( b26, b37 ); // w2 w3 f2 f3

b2BodyStateW simdBody;
simdBody.v.X = _mm256_shuffle_ps( t0, t2, _MM_SHUFFLE( 1, 0, 1, 0 ) );
simdBody.v.Y = _mm256_shuffle_ps( t0, t2, _MM_SHUFFLE( 3, 2, 3, 2 ) );
simdBody.w = _mm256_shuffle_ps( t1, t3, _MM_SHUFFLE( 1, 0, 1, 0 ) );
simdBody.flags = _mm256_shuffle_ps( t1, t3, _MM_SHUFFLE( 3, 2, 3, 2 ) );
simdBody.dp = ( b2Vec2W ){ _mm256_setzero_ps(), _mm256_setzero_ps() };
simdBody.dq = ( b2RotW ){ _mm256_set1_ps( 1.0f ), _mm256_setzero_ps() };
return simdBody;
}

#elif defined( B2_SIMD_NEON )

// This is a load and transpose
Expand Down Expand Up @@ -1192,6 +1229,21 @@ B2_FORCE_INLINE void b2ScatterBodies( b2BodyState* B2_RESTRICT states, int* B2_R

#endif

#if B2_SIMD_WIDTH != 8
// Other SIMD widths gather the full body state
#define b2GatherVelocities b2GatherBodies
#endif

// Prefetch the body states of a wide constraint. The indices are base 1.
static inline void b2PrefetchBodies( const b2BodyState* states, const int* indices )
{
uintptr_t base = (uintptr_t)states - sizeof( b2BodyState );
for ( int k = 0; k < B2_SIMD_WIDTH; ++k )
{
b2Prefetch( base + (uintptr_t)indices[k] * sizeof( b2BodyState ) );
}
}

static b2ContactSim b2_zeroContactSim = { 0 };

// Macro for gathering contact data across lanes.
Expand Down Expand Up @@ -1626,8 +1678,16 @@ void B2_WIDE( b2WarmStartContacts_Wide )( b2SolverBlock block, b2StepContext* co
for ( int i = block.startIndex; i < block.startIndex + block.count; ++i )
{
b2ContactConstraintWide* c = constraints + i;
b2BodyStateW bA = b2GatherBodies( states, c->indexA );
b2BodyStateW bB = b2GatherBodies( states, c->indexB );

if ( i + 1 < block.startIndex + block.count )
{
b2PrefetchBodies( states, c[1].indexA );
b2PrefetchBodies( states, c[1].indexB );
}

// Warm starting only needs the velocities
b2BodyStateW bA = b2GatherVelocities( states, c->indexA );
b2BodyStateW bB = b2GatherVelocities( states, c->indexB );

b2FloatW tangentX = c->normal.Y;
b2FloatW tangentY = b2SubW( b2ZeroW(), c->normal.X );
Expand Down
12 changes: 8 additions & 4 deletions src/physics_world.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include "dynamic_tree.h"
#include "island.h"
#include "joint.h"
#include "parallel_for.h"
#include "platform.h"
#include "recording.h"
#include "scheduler.h"
Expand All @@ -27,6 +26,7 @@
#include "simd.h"
#include "solver.h"
#include "solver_set.h"
#include "task.h"

#include "box2d/box2d.h"
#include "box2d/constants.h"
Expand Down Expand Up @@ -528,11 +528,14 @@ static void b2CollideTask( int startIndex, int endIndex, int workerIndex, void*

bool wasTouching = ( contactSim->simFlags & b2_simTouchingFlag );

// Update contact respecting shape/body order (A,B)
// Use these encoded body sim indices to avoid cache misses.
int encodedA = contactSim->encodedBodySimA;
int encodedB = contactSim->encodedBodySimB;

// These only load the body id from the shape if the body is sleeping.
b2BodySim* bodySimA = b2ResolveContactBodySim( world, awakeSims, staticSims, encodedA, shapeA->bodyId );
b2BodySim* bodySimB = b2ResolveContactBodySim( world, awakeSims, staticSims, encodedB, shapeB->bodyId );

b2WorldTransform transformA = bodySimA->transform;
b2WorldTransform transformB = bodySimB->transform;

Expand All @@ -557,8 +560,9 @@ static void b2CollideTask( int startIndex, int endIndex, int workerIndex, void*
// as PhysX. However, this allows larger relative motion and has fewer tuning parameters
// (just one).

// It is risky to recycle non-touching chain segment contact.
bool chainSegment = shapeA->type == b2_chainSegmentShape || shapeB->type == b2_chainSegmentShape;
// It is risky to recycle non-touching chain segment contact. Use a cached flag to avoid
// touching the shape data.
bool chainSegment = ( contactSim->simFlags & b2_simChainSegment ) != 0;

if ( ( hadTimeOfImpact == false || wasTouching ) && ( chainSegment == false || wasTouching ) &&
recycleDistance > 0.0f && ( contactSim->simFlags & b2_simRelativeTransformValid ) &&
Expand Down
14 changes: 8 additions & 6 deletions src/sensor.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
#include "sensor.h"

#include "body.h"
#include "contact.h"
#include "platform.h"
#include "parallel_for.h"
#include "physics_world.h"
#include "platform.h"
#include "qsort.h"
#include "shape.h"
#include "solver_set.h"
#include "task.h"

#include "box2d/collision.h"

Expand Down Expand Up @@ -175,9 +174,12 @@ static void b2SensorTask( int startIndex, int endIndex, int threadIndex, void* c
b2AABB queryBounds = sensorShape->aabb;

// Query all trees
b2DynamicTree_OverlapAABB( trees + 0, queryBounds, sensorShape->filter.maskBits, b2SensorQueryCallback, &queryContext );
b2DynamicTree_OverlapAABB( trees + 1, queryBounds, sensorShape->filter.maskBits, b2SensorQueryCallback, &queryContext );
b2DynamicTree_OverlapAABB( trees + 2, queryBounds, sensorShape->filter.maskBits, b2SensorQueryCallback, &queryContext );
b2DynamicTree_OverlapAABB( trees + 0, queryBounds, sensorShape->filter.maskBits, b2SensorQueryCallback,
&queryContext );
b2DynamicTree_OverlapAABB( trees + 1, queryBounds, sensorShape->filter.maskBits, b2SensorQueryCallback,
&queryContext );
b2DynamicTree_OverlapAABB( trees + 2, queryBounds, sensorShape->filter.maskBits, b2SensorQueryCallback,
&queryContext );

// Sort the overlaps to enable finding begin and end events.
{
Expand Down
Loading
Loading