diff --git a/benchmark/amd7950x_sse2/convex_pile.csv b/benchmark/amd7950x_sse2/convex_pile.csv index a5f0847..ed56bc5 100644 --- a/benchmark/amd7950x_sse2/convex_pile.csv +++ b/benchmark/amd7950x_sse2/convex_pile.csv @@ -1,9 +1,9 @@ threads,ms -1,13748 -2,6965.15 -3,4680.52 -4,3551.71 -5,2881.12 -6,2436.97 -7,2128.86 -8,1895.85 +1,5834.56 +2,3071.09 +3,2104.65 +4,1629.92 +5,1347.05 +6,1160.72 +7,1031.3 +8,938.203 diff --git a/benchmark/amd7950x_sse2/joint_grid.csv b/benchmark/amd7950x_sse2/joint_grid.csv index fe1c753..b1e7573 100644 --- a/benchmark/amd7950x_sse2/joint_grid.csv +++ b/benchmark/amd7950x_sse2/joint_grid.csv @@ -1,9 +1,9 @@ threads,ms -1,1367.75 -2,693.994 -3,464.069 -4,350.134 -5,282.582 -6,238.946 -7,207.608 -8,185.253 +1,1311.69 +2,684.485 +3,463.171 +4,351.927 +5,283.58 +6,239.664 +7,208.766 +8,185.503 diff --git a/benchmark/amd7950x_sse2/junkyard.csv b/benchmark/amd7950x_sse2/junkyard.csv index d5a3c79..da36de0 100644 --- a/benchmark/amd7950x_sse2/junkyard.csv +++ b/benchmark/amd7950x_sse2/junkyard.csv @@ -1,9 +1,9 @@ threads,ms -1,13672.8 -2,7175.79 -3,4913.8 -4,3784.71 -5,3108 -6,2664.34 -7,2355.17 -8,2130.17 +1,12295.2 +2,6543.68 +3,4507.87 +4,3479.32 +5,2860.52 +6,2465.78 +7,2181.07 +8,1976.01 diff --git a/benchmark/amd7950x_sse2/large_pyramid.csv b/benchmark/amd7950x_sse2/large_pyramid.csv index 291be77..b3ce44e 100644 --- a/benchmark/amd7950x_sse2/large_pyramid.csv +++ b/benchmark/amd7950x_sse2/large_pyramid.csv @@ -1,9 +1,9 @@ threads,ms -1,1494.53 -2,777.388 -3,534.802 -4,410.022 -5,336.317 -6,293.644 -7,255.579 -8,231.247 +1,1482.54 +2,779.924 +3,539.552 +4,412.933 +5,337.409 +6,296.132 +7,258.414 +8,234.912 diff --git a/benchmark/amd7950x_sse2/large_world.csv b/benchmark/amd7950x_sse2/large_world.csv index 0fd1d11..74537db 100644 --- a/benchmark/amd7950x_sse2/large_world.csv +++ b/benchmark/amd7950x_sse2/large_world.csv @@ -1,9 +1,9 @@ threads,ms -1,6.3724 -2,8.1894 -3,9.2282 -4,9.5863 -5,9.7645 -6,9.7135 -7,10.0949 -8,10.2191 +1,6.4003 +2,8.3039 +3,9.2919 +4,9.5648 +5,10.014 +6,9.8492 +7,9.9471 +8,10.1759 diff --git a/benchmark/amd7950x_sse2/many_pyramids.csv b/benchmark/amd7950x_sse2/many_pyramids.csv index a301ab3..eb5af42 100644 --- a/benchmark/amd7950x_sse2/many_pyramids.csv +++ b/benchmark/amd7950x_sse2/many_pyramids.csv @@ -1,9 +1,9 @@ threads,ms -1,1716.24 -2,863.727 -3,564.346 -4,431.892 -5,350.505 -6,294.929 -7,249.838 -8,217.224 +1,1589.75 +2,818.695 +3,552.764 +4,419.151 +5,335.022 +6,282.792 +7,244.754 +8,217.593 diff --git a/benchmark/amd7950x_sse2/rain.csv b/benchmark/amd7950x_sse2/rain.csv index b1ff82b..3ab8c71 100644 --- a/benchmark/amd7950x_sse2/rain.csv +++ b/benchmark/amd7950x_sse2/rain.csv @@ -1,9 +1,9 @@ threads,ms -1,2058.5 -2,1111.94 -3,774.168 -4,613.062 -5,506.603 -6,441.273 -7,393.697 -8,364.168 +1,2062.84 +2,1128.22 +3,787.495 +4,615.21 +5,510.128 +6,441.225 +7,393.722 +8,360.659 diff --git a/benchmark/amd7950x_sse2/sleep.csv b/benchmark/amd7950x_sse2/sleep.csv index 3c9c52b..410101e 100644 --- a/benchmark/amd7950x_sse2/sleep.csv +++ b/benchmark/amd7950x_sse2/sleep.csv @@ -1,9 +1,9 @@ threads,ms -1,841.682 -2,452.523 -3,317.294 -4,244.096 -5,209.411 -6,186.295 -7,169.097 -8,159.324 +1,844.093 +2,457.862 +3,318.846 +4,250.452 +5,209.426 +6,185.657 +7,169.441 +8,159.537 diff --git a/benchmark/amd7950x_sse2/spinner.csv b/benchmark/amd7950x_sse2/spinner.csv index 6ac177c..a76590f 100644 --- a/benchmark/amd7950x_sse2/spinner.csv +++ b/benchmark/amd7950x_sse2/spinner.csv @@ -1,9 +1,9 @@ threads,ms -1,3133.28 -2,1789.12 -3,1283.52 -4,1028.48 -5,879.763 -6,792.053 -7,725.787 -8,700.573 +1,3134.14 +2,1823.2 +3,1303 +4,1047.41 +5,895.276 +6,799.402 +7,736.174 +8,697.184 diff --git a/benchmark/amd7950x_sse2/trees100.csv b/benchmark/amd7950x_sse2/trees100.csv index 209bfb6..e806a76 100644 --- a/benchmark/amd7950x_sse2/trees100.csv +++ b/benchmark/amd7950x_sse2/trees100.csv @@ -1,9 +1,9 @@ threads,ms -1,169.469 -2,108.928 -3,87.0377 -4,78.2135 -5,74.7472 -6,72.3567 -7,71.2555 -8,69.4727 +1,167.791 +2,110.927 +3,87.9977 +4,78.3566 +5,73.6954 +6,71.5957 +7,70.7282 +8,69.9302 diff --git a/benchmark/amd7950x_sse2/trees25.csv b/benchmark/amd7950x_sse2/trees25.csv index 186e441..28f489d 100644 --- a/benchmark/amd7950x_sse2/trees25.csv +++ b/benchmark/amd7950x_sse2/trees25.csv @@ -1,9 +1,9 @@ threads,ms -1,705.33 -2,395.666 -3,288.224 -4,250.774 -5,227.915 -6,213.331 -7,199.236 -8,197.301 +1,659.503 +2,387.181 +3,283.605 +4,245.555 +5,222.887 +6,208.297 +7,198.029 +8,191.382 diff --git a/benchmark/amd7950x_sse2/trees50.csv b/benchmark/amd7950x_sse2/trees50.csv index bd82cba..d23b525 100644 --- a/benchmark/amd7950x_sse2/trees50.csv +++ b/benchmark/amd7950x_sse2/trees50.csv @@ -1,9 +1,9 @@ threads,ms -1,292.4 -2,176.437 -3,136.132 -4,119.305 -5,112.324 -6,104.356 -7,100.668 -8,101.573 +1,285.725 +2,176.941 +3,135.635 +4,119.013 +5,109.379 +6,104.485 +7,101.719 +8,99.5091 diff --git a/benchmark/amd7950x_sse2/washer.csv b/benchmark/amd7950x_sse2/washer.csv index 549fde1..010b74d 100644 --- a/benchmark/amd7950x_sse2/washer.csv +++ b/benchmark/amd7950x_sse2/washer.csv @@ -1,9 +1,9 @@ threads,ms -1,19731.4 -2,10458 -3,7085.75 -4,5383.4 -5,4383.12 -6,3769.83 -7,3321.09 -8,3064.85 +1,17570 +2,9467.24 +3,6511.07 +4,5051.22 +5,4128.07 +6,3545.32 +7,3142.81 +8,2844.68 diff --git a/src/convex_manifold.c b/src/convex_manifold.c index 00a5495..bcc34a0 100644 --- a/src/convex_manifold.c +++ b/src/convex_manifold.c @@ -1246,7 +1246,6 @@ static bool b3BuildEdgeContact( b3LocalManifold* manifold, const b3HullData* hul // Transform a SoA point/normal stream (already split into X/Y/Z) by out = -(R*v (+t)). // The inputs come straight from the hull's stored SoA arrays, so there's no transpose here. -// IsPoint is a template arg so the translation add is only emitted for points. static inline void b3NegativeTransformFromSoA( b3Matrix3* R, b3Vec3 p, const float* inX, const float* inY, const float* inZ, int n, float* outX, float* outY, float* outZ, bool isPoint ) { @@ -1352,9 +1351,88 @@ static inline void b3GetSupportWide( b3Vec3 normal, const float* vx, const float *support = normal.x * vx[vi] + normal.y * vy[vi] + normal.z * vz[vi]; } -#define NE ( B3_MAX_HULL_EDGES + 4 ) -#define NF ( B3_MAX_HULL_FACES + 4 ) -#define NV ( B3_MAX_HULL_VERTICES + 4 ) +static inline float b3GetFaceSeparation( b3Vec3 direction, float planeSeparation, const float* vx, const float* vy, + const float* vz, int n, b3Vec3 center, b3Vec3 extents, int* vertexIndex ) +{ + float bias = b3Dot( direction, center ) + 1.0625f * b3Dot( b3Abs( direction ), extents ); + float support; + b3GetSupportWide( direction, vx, vy, vz, n, bias, &support, vertexIndex ); + return planeSeparation - support; +} + +// Wide dot(n, d) for all face normals n of the hull, padded to the SIMD width. +static inline void b3GetFaceDots( const b3HullData* hull, b3Vec3 d, float* dots ) +{ + int soaFaceCount = ( hull->faceCount + 3 ) & ~3; + const float* nx = b3GetHullSoaNormals( hull ); + const float* ny = nx + soaFaceCount; + const float* nz = ny + soaFaceCount; + + b3FloatW dx = b3SplatW( d.x ); + b3FloatW dy = b3SplatW( d.y ); + b3FloatW dz = b3SplatW( d.z ); + + for ( int i = 0; i < soaFaceCount; i += 4 ) + { + // dot product per lane + b3FloatW m = b3Dot3W( b3LoadW( nx + i ), b3LoadW( ny + i ), b3LoadW( nz + i ), dx, dy, dz ); + b3StoreW( dots + i, m ); + } +} + +#define B3_PARALLEL_TOL 1e-4f + +// A hull edges is bounded by two face normals. On the Gauss map the edge becomes an arc between +// those two face normals. This edge can only build the best separating axis if a normal on that arc +// can beat the best separation value seen so far. This test determines of such a normal exists. +// This follows the upper bound used for hull faces: +// separation_upper_bound = dot(axis, centerB - centerA) - innerRadiusA - innerRadiusB +// +// Inputs: +// d the vector connecting the hull centers +// a = dot(u, d) +// b = dot(v, d) +// c = dot(u, v) +// length = length(d) +// bound = max_sep + radiusBound +// Note: this doesn't compute the separation, it just rules out potential candidates. So if some input +// is degenerate it just pass the candidate onto the next stage. +static inline int b3ArcCanReach( float a, float b, float c, float length, float bound ) +{ + // c = cos(theta), the angle between the normals. + // s = sin(theta)^2 >= 0 + float s = 1.0f - c * c; + + // This is coincidently the law of cosines. Break out your protractor. + float t = a * a + b * b - 2.0f * a * b * c; + + // Do either face normals beat the best separation? + int endpoint = b3MaxFloat( a, b ) >= bound; + + // Project d into the plane that holds both u and v, call that vector g: + // g = x*u + y*v + // Note that dot(u, g) == dot(u, d) == a, and dot(v, g) == dot(v, d) == b + // Dot the equation with u and v: + // a = x + y*c + // b = x*c + y + // Solve for x and y using Cramer's Rule + // x = (a - b * c) / (1 - c * c) + // y = (b - a * c) / (1 - c * c) + // s = 1 - c * c is greater than 0, so x and y must be positive for n to live between u and v. + // The peak value along d is then dot(g, n): + // dot(g, n) = x*a + y*b = (a*a - a*b*c + b*b - a*b*c) / s = (a*a + b*b - 2*a*b*c) / s = t / s + // If that peak value beats the threshold then this edge might form the best separating axis. + // Using bit ops here to prevent unpredictable branches. + int interior = ( a >= c * b ) & ( b >= c * a ) & ( length >= bound ) & + ( ( bound <= 0.0f ) | ( s < B3_PARALLEL_TOL ) | ( t >= bound * bound * s ) ); + + return endpoint | interior; +} + +// Temporary abbreviations for convenience. +#define NE ( B3_MAX_HULL_EDGES + B3_SIMD_WIDTH ) +#define NF ( B3_MAX_HULL_FACES + B3_SIMD_WIDTH ) +#define NV ( B3_MAX_HULL_VERTICES + B3_SIMD_WIDTH ) // SIMD separating axis test based on an implementation developed by Cairn Overturf. // See his article: https://cairno.substack.com/p/improvements-to-the-separating-axis @@ -1404,17 +1482,65 @@ b3AxisQuery b3ComputeSeparatingAxis( const b3HullData* hullA, const b3HullData* b3Vec3 cB = b3AABB_Center( hullB->aabb ); b3Vec3 hB = b3AABB_Extents( hullB->aabb ); + // The hulls have a precomputed inner radius and centroid. + // A give axis cannot achieve a separation larger than: + // dot(axis, centerB - centerA) - innerRadiusA - innerRadiusB + // So this is the upper bound for the separation of a candidate axis. + // An axis can be skipped if it has an upper bound that is less than the current + // best separation. This lets me skip many of the candidates without computing + // support points. + + b3Vec3 deltaCenter = b3Sub( b3Add( b3MulMV( R, hullB->center ), xfB.p ), hullA->center ); + float centerDistance = b3Length( deltaCenter ); + float radius = hullA->innerRadius + hullB->innerRadius; + + // Adjust the radius to ensure the best axis isn't skipped. + float radiusBound = radius - ( B3_LINEAR_SLOP + 0.001f * ( centerDistance + radius ) ); + + // Compute dot(normalA, centerDelta) for all face normals of hullA. + _Alignas( 16 ) float dotA[NF]; + b3GetFaceDots( hullA, deltaCenter, dotA ); + + // Find the face of hullA that most aligns with centerDelta. + int seedIndexA = 0; + float maxDotA = dotA[0]; + for ( int i = 1; i < faceCountA; ++i ) + { + if ( dotA[i] > maxDotA ) + { + maxDotA = dotA[i]; + seedIndexA = i; + } + } + + // Use the seed to get a lower bound on the separation for the faces of hullA. + float floorA = -INFINITY; + if ( earlyReturn ) + { + b3Plane plane = planesA[seedIndexA]; + b3Vec3 direction = b3Neg( b3MulMV( invR, plane.normal ) ); + float planeSeparation = b3Dot( plane.normal, xfB.p ) - plane.offset; + int vertexIndex; + float separation = + b3GetFaceSeparation( direction, planeSeparation, vxB, vyB, vzB, soaVertexCountB, cB, hB, &vertexIndex ); + floorA = b3MinFloat( separation, speculativeDistance ); + } + // Test A's face planes against B's vertices. for ( int i = 0; i < faceCountA; ++i ) { + // The bound offset ensures the seed will be evaluated. + if ( dotA[i] - radiusBound < b3MaxFloat( floorA, res.faceA.separation ) ) + { + continue; + } + b3Plane plane = planesA[i]; b3Vec3 direction = b3Neg( b3MulMV( invR, plane.normal ) ); float planeSeparation = b3Dot( plane.normal, xfB.p ) - plane.offset; - float biasB = b3Dot( direction, cB ) + 1.0625f * b3Dot( b3Abs( direction ), hB ); - float support; int vertexIndex; - b3GetSupportWide( direction, vxB, vyB, vzB, soaVertexCountB, biasB, &support, &vertexIndex ); - float separation = planeSeparation - support; + float separation = + b3GetFaceSeparation( direction, planeSeparation, vxB, vyB, vzB, soaVertexCountB, cB, hB, &vertexIndex ); if ( separation > res.faceA.separation ) { res.faceA.normal = plane.normal; @@ -1429,6 +1555,8 @@ b3AxisQuery b3ComputeSeparatingAxis( const b3HullData* hullA, const b3HullData* } } + B3_VALIDATE( res.faceA.indexA != B3_NULL_INDEX ); + int faceCountB = hullB->faceCount; const b3Plane* planesB = b3GetHullPlanes( hullB ); @@ -1440,17 +1568,51 @@ b3AxisQuery b3ComputeSeparatingAxis( const b3HullData* hullA, const b3HullData* b3Vec3 cA = b3AABB_Center( hullA->aabb ); b3Vec3 hA = b3AABB_Extents( hullA->aabb ); + // Similarly, find the face of hullB that most aligns with the vector pointing from centerB to centerA. + _Alignas( 16 ) float dotB[NF]; + b3GetFaceDots( hullB, b3Neg( b3MulMV( invR, deltaCenter ) ), dotB ); + + int seedIndexB = 0; + float maxDotB = dotB[0]; + for ( int i = 1; i < faceCountB; ++i ) + { + if ( dotB[i] > maxDotB ) + { + maxDotB = dotB[i]; + seedIndexB = i; + } + } + + // Get a lower bound on the separation for the faces of hullB. + float floorB = -INFINITY; + if ( earlyReturn ) + { + b3Plane plane = planesB[seedIndexB]; + b3Vec3 direction = b3Neg( b3MulMV( R, plane.normal ) ); + float planeSeparation = b3Dot( direction, xfB.p ) - plane.offset; + int vertexIndex; + float separation = + b3GetFaceSeparation( direction, planeSeparation, vxA, vyA, vzA, soaVertexCountA, cA, hA, &vertexIndex ); + + // Include the floor set by hull A faces. + floorB = b3MaxFloat( separation, res.faceA.separation ); + floorB = b3MinFloat( floorB, speculativeDistance ); + } + // Test B's face planes against A's vertices. for ( int i = 0; i < faceCountB; ++i ) { + if ( dotB[i] - radiusBound < b3MaxFloat( floorB, res.faceB.separation ) ) + { + continue; + } + b3Plane plane = planesB[i]; b3Vec3 direction = b3Neg( b3MulMV( R, plane.normal ) ); float planeSeparation = b3Dot( direction, xfB.p ) - plane.offset; - float biasA = b3Dot( direction, cA ) + 1.0625f * b3Dot( b3Abs( direction ), hA ); - float support; int vertexIndex; - b3GetSupportWide( direction, vxA, vyA, vzA, soaVertexCountA, biasA, &support, &vertexIndex ); - float separation = planeSeparation - support; + float separation = + b3GetFaceSeparation( direction, planeSeparation, vxA, vyA, vzA, soaVertexCountA, cA, hA, &vertexIndex ); if ( separation > res.faceB.separation ) { res.faceB.normal = direction; @@ -1466,11 +1628,48 @@ b3AxisQuery b3ComputeSeparatingAxis( const b3HullData* hullA, const b3HullData* } } - // Transform B into A's space once, into SoA arrays. Extra space so - // tail can be set to zero in all cases. - _Static_assert( ( B3_MAX_HULL_EDGES & 3 ) == 0, "must be multiple of 4" ); - _Static_assert( ( B3_MAX_HULL_FACES & 3 ) == 0, "must be multiple of 4" ); - _Static_assert( ( B3_MAX_HULL_VERTICES & 3 ) == 0, "must be multiple of 4" ); + // Transform B into A's space once, into SoA arrays. Extra space so tail can be set to zero. + _Static_assert( ( B3_MAX_HULL_EDGES & ( B3_SIMD_WIDTH - 1 ) ) == 0, "must be multiple of SIMD width" ); + _Static_assert( ( B3_MAX_HULL_FACES & ( B3_SIMD_WIDTH - 1 ) ) == 0, "must be multiple of SIMD width" ); + _Static_assert( ( B3_MAX_HULL_VERTICES & ( B3_SIMD_WIDTH - 1 ) ) == 0, "must be multiple of SIMD width" ); + + // Bound to skip edge tests. Derived from: + // dot(edgeNormal, deltaCenter) - radiusBound > maxSep + float edgeBound = earlyReturn ? b3MaxFloat( res.faceA.separation, res.faceB.separation ) + radiusBound : -INFINITY; + + // Gather edges of A that can feasibly create a winning separating axis. + int halfEdgeCountA = hullA->edgeCount; + const b3HullHalfEdge* halfEdgesA = b3GetHullEdges( hullA ); + int edgeIndicesA[NE]; + int na = 0; + for ( int i = 0; i < halfEdgeCountA; i += 2 ) + { + int i1 = halfEdgesA[i].face; + int i2 = halfEdgesA[i + 1].face; + float c = b3Dot( planesA[i1].normal, planesA[i2].normal ); + edgeIndicesA[na] = i; + na += b3ArcCanReach( dotA[i1], dotA[i2], c, centerDistance, edgeBound ); + } + + // Gather edges of B that can feasibly create a winning separating axis. + int halfEdgeCountB = hullB->edgeCount; + const b3HullHalfEdge* halfEdgesB = b3GetHullEdges( hullB ); + int edgeIndicesB[B3_MAX_HULL_EDGES]; + int nb = 0; + for ( int i = 0; i < halfEdgeCountB; i += 2 ) + { + int i1 = halfEdgesB[i].face; + int i2 = halfEdgesB[i + 1].face; + float c = b3Dot( planesB[i1].normal, planesB[i2].normal ); + edgeIndicesB[nb] = i; + nb += b3ArcCanReach( dotB[i1], dotB[i2], c, centerDistance, edgeBound ); + } + + if ( na == 0 || nb == 0 ) + { + // No edge candidates found. + return res; + } // The alignments below are not necessary, but they don't hurt. @@ -1506,31 +1705,27 @@ b3AxisQuery b3ComputeSeparatingAxis( const b3HullData* hullA, const b3HullData* _Alignas( 16 ) float bDCy[NE]; _Alignas( 16 ) float bDCz[NE]; - int halfEdgeCountB = hullB->edgeCount; - const b3HullHalfEdge* halfEdgesB = b3GetHullEdges( hullB ); - int nb = 0; - for ( int i = 0; i < halfEdgeCountB; i += 2 ) + for ( int k = 0; k < nb; ++k ) { - const b3HullHalfEdge* edge = halfEdgesB + i; + const b3HullHalfEdge* edge = halfEdgesB + edgeIndicesB[k]; const b3HullHalfEdge* twin = edge + 1; int f0 = edge->face; int f1 = twin->face; int v0 = edge->origin; int v1 = twin->origin; - bCx[nb] = bFNx[f0]; - bCy[nb] = bFNy[f0]; - bCz[nb] = bFNz[f0]; - bDx[nb] = bFNx[f1]; - bDy[nb] = bFNy[f1]; - bDz[nb] = bFNz[f1]; - bV0x[nb] = bWx[v0]; - bV0y[nb] = bWy[v0]; - bV0z[nb] = bWz[v0]; - bDCx[nb] = bWx[v1] - bWx[v0]; - bDCy[nb] = bWy[v1] - bWy[v0]; - bDCz[nb] = bWz[v1] - bWz[v0]; - nb += 1; + bCx[k] = bFNx[f0]; + bCy[k] = bFNy[f0]; + bCz[k] = bFNz[f0]; + bDx[k] = bFNx[f1]; + bDy[k] = bFNy[f1]; + bDz[k] = bFNz[f1]; + bV0x[k] = bWx[v0]; + bV0y[k] = bWy[v0]; + bV0z[k] = bWz[v0]; + bDCx[k] = bWx[v1] - bWx[v0]; + bDCy[k] = bWy[v1] - bWy[v0]; + bDCz[k] = bWz[v1] - bWz[v0]; } // Per A edge data, already in A's space so just gathered. n0 and n1 are the two face @@ -1551,37 +1746,32 @@ b3AxisQuery b3ComputeSeparatingAxis( const b3HullData* hullA, const b3HullData* _Alignas( 16 ) float aV0z[NE]; _Alignas( 16 ) float aTol[NE]; - int halfEdgeCountA = hullA->edgeCount; - const b3HullHalfEdge* halfEdgesA = b3GetHullEdges( hullA ); - int na = 0; - float squaredTol = B3_PARALLEL_EDGE_TOL * B3_PARALLEL_EDGE_TOL; - for ( int i = 0; i < halfEdgeCountA; i += 2 ) + for ( int k = 0; k < na; ++k ) { - const b3HullHalfEdge* edge = halfEdgesA + i; + const b3HullHalfEdge* edge = halfEdgesA + edgeIndicesA[k]; const b3HullHalfEdge* twin = edge + 1; b3Vec3 A = planesA[edge->face].normal; b3Vec3 B = planesA[twin->face].normal; - aN0x[na] = A.x; - aN0y[na] = A.y; - aN0z[na] = A.z; - aN1x[na] = B.x; - aN1y[na] = B.y; - aN1z[na] = B.z; + aN0x[k] = A.x; + aN0y[k] = A.y; + aN0z[k] = A.z; + aN1x[k] = B.x; + aN1y[k] = B.y; + aN1z[k] = B.z; int v0 = edge->origin; int v1 = twin->origin; - aDx[na] = vxA[v1] - vxA[v0]; - aDy[na] = vyA[v1] - vyA[v0]; - aDz[na] = vzA[v1] - vzA[v0]; - aV0x[na] = vxA[v0]; - aV0y[na] = vyA[v0]; - aV0z[na] = vzA[v0]; + aDx[k] = vxA[v1] - vxA[v0]; + aDy[k] = vyA[v1] - vyA[v0]; + aDz[k] = vzA[v1] - vzA[v0]; + aV0x[k] = vxA[v0]; + aV0y[k] = vyA[v0]; + aV0z[k] = vzA[v0]; - aTol[na] = squaredTol * ( aDx[na] * aDx[na] + aDy[na] * aDy[na] + aDz[na] * aDz[na] ); - na += 1; + aTol[k] = squaredTol * ( aDx[k] * aDx[k] + aDy[k] * aDy[k] + aDz[k] * aDz[k] ); } // Zero the tail lanes. @@ -1600,8 +1790,6 @@ b3AxisQuery b3ComputeSeparatingAxis( const b3HullData* hullA, const b3HullData* b3StoreW( aV0z + na, zero ); b3StoreW( aTol + na, zero ); - int edgeCountB = halfEdgeCountB / 2; - #if defined( B3_SIMD_NONE ) // The SIMD emulated version of this code is very slow. This is a purely scalar version @@ -1610,7 +1798,7 @@ b3AxisQuery b3ComputeSeparatingAxis( const b3HullData* hullA, const b3HullData* const float EPS = -0.0001f; - for ( int j = 0; j < edgeCountB; ++j ) + for ( int j = 0; j < nb; ++j ) { float Cx = bCx[j]; float Cy = bCy[j]; @@ -1675,8 +1863,8 @@ b3AxisQuery b3ComputeSeparatingAxis( const b3HullData* hullA, const b3HullData* res.edge.separation = separation; // Half edge index - res.edge.indexA = 2 * i; - res.edge.indexB = 2 * j; + res.edge.indexA = edgeIndicesA[i]; + res.edge.indexB = edgeIndicesB[j]; if ( separation > speculativeDistance && earlyReturn ) { @@ -1693,7 +1881,7 @@ b3AxisQuery b3ComputeSeparatingAxis( const b3HullData* hullA, const b3HullData* const b3FloatW EPS = b3SplatW( -0.0001f ); const b3FloatW INF = b3SplatW( INFINITY ); - for ( int j = 0; j < edgeCountB; ++j ) + for ( int j = 0; j < nb; ++j ) { const b3FloatW Cx = b3SplatW( bCx[j] ); const b3FloatW Cy = b3SplatW( bCy[j] ); @@ -1805,8 +1993,8 @@ b3AxisQuery b3ComputeSeparatingAxis( const b3HullData* hullA, const b3HullData* res.edge.separation = s; // Half edge index - res.edge.indexA = 2 * ei; - res.edge.indexB = 2 * j; + res.edge.indexA = edgeIndicesA[ei]; + res.edge.indexB = edgeIndicesB[j]; if ( s > speculativeDistance && earlyReturn ) { @@ -1868,7 +2056,6 @@ void b3CollideHulls( b3LocalManifold* manifold, int capacity, const b3HullData* b3Plane plane = planesA[cache->indexA]; b3Vec3 searchDirectionInB = b3Neg( b3InvRotateVector( transformBtoA.q, plane.normal ) ); - // todo use b3GetSupportWide int vertexIndex = b3FindHullSupportVertex( hullB, searchDirectionInB ); b3Vec3 support = b3TransformPoint( transformBtoA, pointsB[vertexIndex] ); float separation = b3PlaneSeparation( plane, support ); @@ -2116,6 +2303,13 @@ void b3CollideHulls( b3LocalManifold* manifold, int capacity, const b3HullData* b3SeparatingAxis edgeQuery = axisQuery.edge; + // If face clipping yields no points then I need to run a full SAT to get the best edge pair. + if ( manifold->pointCount == 0 && + edgeQuery.separation < b3MaxFloat( axisQuery.faceA.separation, axisQuery.faceB.separation ) ) + { + edgeQuery = b3ComputeSeparatingAxis( hullA, hullB, transformBtoA, false ).edge; + } + if ( edgeQuery.indexA == B3_NULL_INDEX ) { // There are no valid edge pairs (all edges parallel) diff --git a/src/hull.c b/src/hull.c index 33954d4..27cb6f6 100644 --- a/src/hull.c +++ b/src/hull.c @@ -8,6 +8,7 @@ #include "algorithm.h" #include "math_internal.h" #include "shape.h" +#include "simd.h" #include "box3d/collision.h" #include "box3d/constants.h" @@ -1633,19 +1634,46 @@ int b3FindHullSupportVertex( const b3HullData* hull, b3Vec3 direction ) float bestDot = -FLT_MAX; int vertexCount = hull->vertexCount; - const b3Vec3* points = b3GetHullPoints( hull ); + const float* vx = b3GetHullSoaVertices( hull ); - for ( int index = 0; index < vertexCount; ++index ) + int soaVertexCount = ( vertexCount + 3 ) & ~3; + const float* vy = vx + soaVertexCount; + const float* vz = vy + soaVertexCount; + + b3FloatW dx = b3SplatW( direction.x ); + b3FloatW dy = b3SplatW( direction.y ); + b3FloatW dz = b3SplatW( direction.z ); + b3FloatW four = b3SplatW( 4.0f ); + b3FloatW index = b3SetW( 0.0f, 1.0f, 2.0f, 3.0f ); + b3FloatW bestDotW = b3SplatW( -FLT_MAX ); + b3FloatW bestIndexW = b3SplatW( -1.0f ); + + for ( int i = 0; i < soaVertexCount; i += 4 ) { - float dot = b3Dot( direction, points[index] ); - if ( dot > bestDot ) + b3FloatW dot = + b3AddW( b3AddW( b3MulW( dx, b3LoadW( vx + i ) ), b3MulW( dy, b3LoadW( vy + i ) ) ), b3MulW( dz, b3LoadW( vz + i ) ) ); + b3FloatW mask = b3GreaterThanW( dot, bestDotW ); + bestDotW = b3BlendW( bestDotW, dot, mask ); + bestIndexW = b3BlendW( bestIndexW, index, mask ); + index = b3AddW( index, four ); + } + + _Alignas( 16 ) float dots[4]; + _Alignas( 16 ) float indices[4]; + b3StoreW( dots, bestDotW ); + b3StoreW( indices, bestIndexW ); + + for ( int lane = 0; lane < 4; ++lane ) + { + int laneIndex = (int)indices[lane]; + if ( laneIndex >= 0 && ( dots[lane] > bestDot || ( dots[lane] == bestDot && laneIndex < bestIndex ) ) ) { - bestIndex = index; - bestDot = dot; + bestIndex = laneIndex; + bestDot = dots[lane]; } } - B3_ASSERT( bestIndex >= 0 ); + B3_ASSERT( 0 <= bestIndex && bestIndex < vertexCount ); return bestIndex; }