Hull sharing (#13)

Save memory when there are many duplicate hulls. Can improve performance
in some cases.

For consistency with b3MeshData, b3Hull is renamed to b3HullData.
This commit is contained in:
Erin Catto 2026-06-11 22:39:26 -07:00 • committed by GitHub
commit a252f03b19
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
66 changed files with 881 additions and 549 deletions

View file

@ -222,7 +222,7 @@ B3_API void b3World_SetWorkerCount( b3WorldId worldId, int count );
/// Get the worker count.
B3_API int b3World_GetWorkerCount( b3WorldId worldId );
/// Dump memory stats to box3d_memory.txt
/// Dump memory stats to log.
B3_API void b3World_DumpMemoryStats( b3WorldId worldId );
/// Dump shape bounds to box3d_bounds.txt
@ -558,15 +558,16 @@ B3_API b3ShapeId b3CreateSphereShape( b3BodyId bodyId, const b3ShapeDef* def, co
/// @return the shape id for accessing the shape
B3_API b3ShapeId b3CreateCapsuleShape( b3BodyId bodyId, const b3ShapeDef* def, const b3Capsule* capsule );
/// Create a convex hull shape and attach it to a body. The hull is cloned.
/// The shape definition and geometry are fully cloned. Contacts are not created until the next time step.
/// Create a convex hull shape and attach it to a body. The shape definition is fully cloned. Contacts are not created
/// until the next time step.
/// @return the shape id for accessing the shape
B3_API b3ShapeId b3CreateHullShape( b3BodyId bodyId, const b3ShapeDef* def, const b3Hull* hull );
B3_API b3ShapeId b3CreateHullShape( b3BodyId bodyId, const b3ShapeDef* def, const b3HullData* hull );
/// Create a convex hull shape and attach it to a body. The hull is cloned then transformed with scale applied first.
/// The shape definition and geometry are fully cloned. Contacts are not created until the next time step.
/// Use this for non-uniform or mirrored scale or a baked local transform. The baked result is shared through the
/// world hull database. The shape definition and geometry are fully cloned. Contacts are not created until the next time step.
/// @return the shape id for accessing the shape
B3_API b3ShapeId b3CreateTransformedHullShape( b3BodyId bodyId, const b3ShapeDef* def, const b3Hull* hull, b3Transform transform,
B3_API b3ShapeId b3CreateTransformedHullShape( b3BodyId bodyId, const b3ShapeDef* def, const b3HullData* hull, b3Transform transform,
b3Vec3 scale );
/// Create a mesh hull shape and attach it to a body. The shape definition is fully cloned but the mesh is not.
@ -703,7 +704,7 @@ B3_API b3Sphere b3Shape_GetSphere( b3ShapeId shapeId );
B3_API b3Capsule b3Shape_GetCapsule( b3ShapeId shapeId );
/// Get the shape's convex hull. Asserts the type is correct.
B3_API const b3Hull* b3Shape_GetHull( b3ShapeId shapeId );
B3_API const b3HullData* b3Shape_GetHull( b3ShapeId shapeId );
/// Get the shape's mesh. Asserts the type is correct.
B3_API b3Mesh b3Shape_GetMesh( b3ShapeId shapeId );
@ -724,7 +725,7 @@ B3_API void b3Shape_SetCapsule( b3ShapeId shapeId, const b3Capsule* capsule );
/// Allows you to change a shape to be a hull or update the current hull.
/// This does not modify the mass properties.
/// @see b3Body_ApplyMassFromShapes
B3_API void b3Shape_SetHull( b3ShapeId shapeId, const b3Hull* hull );
B3_API void b3Shape_SetHull( b3ShapeId shapeId, const b3HullData* hull );
/// Allows you to change a shape to be a mesh or update the current mesh.
/// This does not modify the mass properties.

View file

@ -143,7 +143,7 @@ B3_INLINE b3AABB b3DynamicTree_GetAABB( const b3DynamicTree* tree, int proxyId )
*/
/// Get read only hull vertices.
B3_INLINE const b3HullVertex* b3GetHullVertices( const b3Hull* hull )
B3_INLINE const b3HullVertex* b3GetHullVertices( const b3HullData* hull )
{
if ( hull->vertexOffset == 0 )
{
@ -154,7 +154,7 @@ B3_INLINE const b3HullVertex* b3GetHullVertices( const b3Hull* hull )
}
/// Get read only hull points.
B3_INLINE const b3Vec3* b3GetHullPoints( const b3Hull* hull )
B3_INLINE const b3Vec3* b3GetHullPoints( const b3HullData* hull )
{
if ( hull->pointOffset == 0 )
{
@ -165,7 +165,7 @@ B3_INLINE const b3Vec3* b3GetHullPoints( const b3Hull* hull )
}
/// Get read only hull half edges.
B3_INLINE const b3HullHalfEdge* b3GetHullEdges( const b3Hull* hull )
B3_INLINE const b3HullHalfEdge* b3GetHullEdges( const b3HullData* hull )
{
if ( hull->edgeOffset == 0 )
{
@ -176,7 +176,7 @@ B3_INLINE const b3HullHalfEdge* b3GetHullEdges( const b3Hull* hull )
}
/// Get read only hull faces.
B3_INLINE const b3HullFace* b3GetHullFaces( const b3Hull* hull )
B3_INLINE const b3HullFace* b3GetHullFaces( const b3HullData* hull )
{
if ( hull->faceOffset == 0 )
{
@ -187,7 +187,7 @@ B3_INLINE const b3HullFace* b3GetHullFaces( const b3Hull* hull )
}
/// Get read only hull planes.
B3_INLINE const b3Plane* b3GetHullPlanes( const b3Hull* hull )
B3_INLINE const b3Plane* b3GetHullPlanes( const b3HullData* hull )
{
if ( hull->planeOffset == 0 )
{
@ -198,22 +198,22 @@ B3_INLINE const b3Plane* b3GetHullPlanes( const b3Hull* hull )
}
/// Create a tessellated cylinder as a hull.
B3_API b3Hull* b3CreateCylinder( float height, float radius, float yOffset, int sides );
B3_API b3HullData* b3CreateCylinder( float height, float radius, float yOffset, int sides );
/// Create a tessellated cone as a hull.
B3_API b3Hull* b3CreateCone( float height, float radius1, float radius2, int slices );
B3_API b3HullData* b3CreateCone( float height, float radius1, float radius2, int slices );
/// Create a generic convex hull.
B3_API b3Hull* b3CreateHull( const b3Vec3* points, int pointCount, int maxVertexCount );
B3_API b3HullData* b3CreateHull( const b3Vec3* points, int pointCount, int maxVertexCount );
/// Deep clone a hull.
B3_API b3Hull* b3CloneHull( const b3Hull* hull );
B3_API b3HullData* b3CloneHull( const b3HullData* hull );
/// Clone and transform a hull. Supports non-uniform and mirroring scale.
B3_API b3Hull* b3CloneAndTransformHull( const b3Hull* original, b3Transform transform, b3Vec3 scale );
B3_API b3HullData* b3CloneAndTransformHull( const b3HullData* original, b3Transform transform, b3Vec3 scale );
/// Destroy a hull.
B3_API void b3DestroyHull( b3Hull* hull );
B3_API void b3DestroyHull( b3HullData* hull );
/// Make a cube as a hull. Do not call b3DestroyHull on this.
B3_API b3BoxHull b3MakeCubeHull( float halfWidth );
@ -426,7 +426,7 @@ B3_API b3MassData b3ComputeSphereMass( const b3Sphere* shape, float density );
B3_API b3MassData b3ComputeCapsuleMass( const b3Capsule* shape, float density );
/// Compute mass properties of a hull
B3_API b3MassData b3ComputeHullMass( const b3Hull* shape, float density );
B3_API b3MassData b3ComputeHullMass( const b3HullData* shape, float density );
/// Compute the bounding box of a transformed sphere
B3_API b3AABB b3ComputeSphereAABB( const b3Sphere* shape, b3Transform transform );
@ -435,7 +435,7 @@ B3_API b3AABB b3ComputeSphereAABB( const b3Sphere* shape, b3Transform transform
B3_API b3AABB b3ComputeCapsuleAABB( const b3Capsule* shape, b3Transform transform );
/// Compute the bounding box of a transformed hull
B3_API b3AABB b3ComputeHullAABB( const b3Hull* shape, b3Transform transform );
B3_API b3AABB b3ComputeHullAABB( const b3HullData* shape, b3Transform transform );
/// Compute the bounding box of a transformed mesh. Scale may be non-uniform and have negative components.
B3_API b3AABB b3ComputeMeshAABB( const b3MeshData* shape, b3Transform transform, b3Vec3 scale );
@ -466,7 +466,7 @@ B3_API bool b3OverlapCompound( const b3Compound* shape, b3Transform shapeTransfo
B3_API bool b3OverlapHeightField( const b3HeightField* shape, b3Transform shapeTransform, const b3ShapeProxy* proxy );
/// Overlap shape versus hull
B3_API bool b3OverlapHull( const b3Hull* shape, b3Transform shapeTransform, const b3ShapeProxy* proxy );
B3_API bool b3OverlapHull( const b3HullData* shape, b3Transform shapeTransform, const b3ShapeProxy* proxy );
/// Overlap shape versus mesh
B3_API bool b3OverlapMesh( const b3Mesh* shape, b3Transform shapeTransform, const b3ShapeProxy* proxy );
@ -487,7 +487,7 @@ B3_API b3CastOutput b3RayCastCapsule( const b3Capsule* shape, const b3RayCastInp
B3_API b3CastOutput b3RayCastCompound( const b3Compound* shape, const b3RayCastInput* input );
/// Ray cast versus hull shape in local space. Initial overlap is treated as a miss.
B3_API b3CastOutput b3RayCastHull( const b3Hull* shape, const b3RayCastInput* input );
B3_API b3CastOutput b3RayCastHull( const b3HullData* shape, const b3RayCastInput* input );
/// Ray cast versus mesh in local space.
B3_API b3CastOutput b3RayCastMesh( const b3Mesh* shape, const b3RayCastInput* input );
@ -505,7 +505,7 @@ B3_API b3CastOutput b3ShapeCastCapsule( const b3Capsule* shape, const b3ShapeCas
B3_API b3CastOutput b3ShapeCastCompound( const b3Compound* shape, const b3ShapeCastInput* input );
/// Shape cast versus a hull. Initial overlap is treated as a miss.
B3_API b3CastOutput b3ShapeCastHull( const b3Hull* shape, const b3ShapeCastInput* input );
B3_API b3CastOutput b3ShapeCastHull( const b3HullData* shape, const b3ShapeCastInput* input );
/// Shape cast versus a mesh. Initial overlap is treated as a miss.
B3_API b3CastOutput b3ShapeCastMesh( const b3Mesh* shape, const b3ShapeCastInput* input );
@ -564,7 +564,7 @@ B3_API void b3CollideCapsuleAndSphere( b3LocalManifold* manifold, int capacity,
const b3Sphere* sphereB, b3Transform transformBtoA );
/// Collide a hull and a sphere.
B3_API void b3CollideHullAndSphere( b3LocalManifold* manifold, int capacity, const b3Hull* hullA, const b3Sphere* sphereB,
B3_API void b3CollideHullAndSphere( b3LocalManifold* manifold, int capacity, const b3HullData* hullA, const b3Sphere* sphereB,
b3Transform transformBtoA, b3SimplexCache* cache );
/// Collide two capsules.
@ -572,11 +572,11 @@ B3_API void b3CollideCapsules( b3LocalManifold* manifold, int capacity, const b3
b3Transform transformBtoA );
/// Collide a hull and a capsule.
B3_API void b3CollideHullAndCapsule( b3LocalManifold* manifold, int capacity, const b3Hull* hullA, const b3Capsule* capsuleB,
B3_API void b3CollideHullAndCapsule( b3LocalManifold* manifold, int capacity, const b3HullData* hullA, const b3Capsule* capsuleB,
b3Transform transformBtoA, b3SimplexCache* cache );
/// Collide two hulls.
B3_API void b3CollideHulls( b3LocalManifold* manifold, int capacity, const b3Hull* hullA, const b3Hull* hullB,
B3_API void b3CollideHulls( b3LocalManifold* manifold, int capacity, const b3HullData* hullA, const b3HullData* hullB,
b3Transform transformBtoA, b3SATCache* cache );
/// Collide a capsule and a triangle.
@ -584,7 +584,7 @@ B3_API void b3CollideCapsuleAndTriangle( b3LocalManifold* manifold, int capacity
const b3Vec3* triangleB, b3SimplexCache* cache );
/// Collide a hull and a triangle.
B3_API void b3CollideHullAndTriangle( b3LocalManifold* manifold, int capacity, const b3Hull* hullA, b3Vec3 v1, b3Vec3 v2,
B3_API void b3CollideHullAndTriangle( b3LocalManifold* manifold, int capacity, const b3HullData* hullA, b3Vec3 v1, b3Vec3 v2,
b3Vec3 v3, int triangleFlags, b3SATCache* cache );
/// Collide a sphere and a triangle.

View file

@ -1937,7 +1937,7 @@ typedef struct b3HullFace
/// A convex hull.
/// @note This data structure has data hanging off the end and cannot be directly copied.
typedef struct b3Hull
typedef struct b3HullData
{
/// Version must be first and match B3_HULL_VERSION
uint64_t version;
@ -1989,17 +1989,22 @@ typedef struct b3Hull
/// Offset of the face plane array in bytes from the struct address.
int planeOffset;
} b3Hull;
/// Explicit padding. Hull identity is a content hash and memcmp over raw bytes,
/// so there must be no unnamed padding for struct copies to scramble.
int padding;
} b3HullData;
/// Efficient box hull
typedef struct b3BoxHull
{
/// The embedded hull. So the offsets index into the arrays that follow.
b3Hull base;
b3HullData base;
b3HullVertex boxVertices[8]; ///< Box vertices.
b3Vec3 boxPoints[8]; ///< Box points.
b3HullHalfEdge boxEdges[24]; ///< Box half-edges.
b3HullFace boxFaces[6]; ///< Box faces.
uint8_t padding[2]; ///< Explicit padding, see b3HullData::padding.
b3Plane boxPlanes[6]; ///< Box face planes.
} b3BoxHull;
@ -2293,7 +2298,7 @@ typedef struct b3CompoundCapsuleDef
typedef struct b3CompoundHullDef
{
/// Shared hull.
const b3Hull* hull;
const b3HullData* hull;
/// Transform of the shared hull into compound local space.
b3Transform transform;
@ -2438,7 +2443,7 @@ typedef struct b3CompoundCapsule
typedef struct b3CompoundHull
{
/// Pointer to the unique shared hull.
const b3Hull* hull;
const b3HullData* hull;
/// The transform of this hull instance.
b3Transform transform;
@ -2483,7 +2488,7 @@ typedef struct b3ChildShape
union
{
b3Capsule capsule; ///< Capsule.
const b3Hull* hull; ///< Hull.
const b3HullData* hull; ///< Hull.
b3Mesh mesh; ///< Mesh.
b3Sphere sphere; ///< Sphere.
};
@ -2898,7 +2903,7 @@ typedef struct b3DebugShape
const b3Capsule* capsule; ///< Capsule shape.
const b3Compound* compound; ///< Compound shape.
const b3HeightField* heightField; ///< Height-field shape.
const b3Hull* hull; ///< Convex hull shape.
const b3HullData* hull; ///< Convex hull shape.
const b3Mesh* mesh; ///< Mesh shape with scale.
const b3Sphere* sphere; ///< Sphere shape.
};