Improve compound serialization (#183)
- no more pointer fixups - reduced storage 20% by removing leaf proxies
This commit is contained in:
parent
3d9db79ee1
commit
5d83df83ab
25 changed files with 845 additions and 376 deletions
|
|
@ -244,8 +244,8 @@ static void b3EmitCompoundPairs( b3PairContext* context, int compoundShapeId, in
|
|||
|
||||
int startCount = context->pairKeys->count;
|
||||
|
||||
b3DynamicTree_Query( &compoundShape->compound->tree, localAABB, B3_DEFAULT_MASK_BITS, false, b3CompoundChildCallback,
|
||||
&compoundContext );
|
||||
b3TreeView view = b3GetCompoundTreeView( compoundShape->compound );
|
||||
b3TreeView_Query( &view, localAABB, B3_DEFAULT_MASK_BITS, false, b3CompoundChildCallback, &compoundContext );
|
||||
|
||||
if ( context->pairKeys->count > startCount && b3ShouldCreatePair( world, compoundShapeId, otherShapeId ) == false )
|
||||
{
|
||||
|
|
|
|||
100
src/compound.c
100
src/compound.c
|
|
@ -50,26 +50,7 @@ typedef struct b3MeshInstance
|
|||
|
||||
_Static_assert( sizeof( b3CompoundCapsule ) == sizeof( b3Capsule ) + 2 * sizeof( uint16_t ), "review padding" );
|
||||
_Static_assert( sizeof( b3CompoundSphere ) == sizeof( b3Sphere ) + 2 * sizeof( uint16_t ), "review padding" );
|
||||
|
||||
static inline b3TreeNode* b3GetCompoundNodes( b3CompoundData* compound )
|
||||
{
|
||||
if ( compound->nodeOffset == 0 )
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return (b3TreeNode*)( (intptr_t)compound + compound->nodeOffset );
|
||||
}
|
||||
|
||||
static inline b3TreeProxy* b3GetCompoundProxies( b3CompoundData* compound )
|
||||
{
|
||||
if ( compound->proxyOffset == 0 )
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return (b3TreeProxy*)( (intptr_t)compound + compound->proxyOffset );
|
||||
}
|
||||
_Static_assert( sizeof( b3CompoundData ) == sizeof( uint64_t ) + sizeof( b3AABB ) + 16 * sizeof( int ), "review padding" );
|
||||
|
||||
const b3SurfaceMaterial* b3GetCompoundMaterials( const b3CompoundData* compound )
|
||||
{
|
||||
|
|
@ -507,12 +488,10 @@ b3CompoundData* b3CreateCompound( const b3CompoundDef* def )
|
|||
|
||||
b3DynamicTree_Rebuild( &tree, true );
|
||||
|
||||
// Tree nodes and proxies. The rebuild is dense, so only the live nodes travel.
|
||||
// Tree nodes. The rebuild is dense, so only the live nodes travel.
|
||||
size_t byteCount = b3AlignUp8( sizeof( b3CompoundData ) );
|
||||
int nodeOffset = (int)byteCount;
|
||||
byteCount += b3AlignUp8( tree.nodeEnd * sizeof( b3TreeNode ) );
|
||||
int proxyOffset = (int)byteCount;
|
||||
byteCount += b3AlignUp8( tree.proxyCount * sizeof( b3TreeProxy ) );
|
||||
int materialOffset = (int)byteCount;
|
||||
byteCount += b3AlignUp8( materialCount * sizeof( b3SurfaceMaterial ) );
|
||||
int capsuleOffset = (int)byteCount;
|
||||
|
|
@ -562,15 +541,9 @@ b3CompoundData* b3CreateCompound( const b3CompoundDef* def )
|
|||
compound->version = B3_COMPOUND_VERSION;
|
||||
compound->byteCount = (int)byteCount;
|
||||
compound->nodeOffset = nodeOffset;
|
||||
compound->proxyOffset = proxyOffset;
|
||||
compound->tree.version = tree.version;
|
||||
compound->tree.nodeEnd = tree.nodeEnd;
|
||||
compound->tree.nodeCapacity = tree.nodeEnd;
|
||||
compound->tree.pairFreeList = B3_NULL_INDEX;
|
||||
compound->tree.proxyCount = tree.proxyCount;
|
||||
compound->tree.proxyCapacity = tree.proxyCount;
|
||||
compound->tree.proxyFreeList = B3_NULL_INDEX;
|
||||
compound->tree.dfsOrdered = tree.dfsOrdered;
|
||||
compound->bounds = b3DynamicTree_GetRootBounds( &tree );
|
||||
compound->treeHeight = b3DynamicTree_GetHeight( &tree );
|
||||
compound->nodeCount = tree.nodeEnd;
|
||||
compound->materialOffset = materialOffset;
|
||||
compound->materialCount = materialCount;
|
||||
compound->capsuleOffset = capsuleOffset;
|
||||
|
|
@ -582,14 +555,9 @@ b3CompoundData* b3CreateCompound( const b3CompoundDef* def )
|
|||
compound->sphereOffset = sphereOffset;
|
||||
compound->sphereCount = sphereCount;
|
||||
|
||||
// Tree nodes and proxies
|
||||
b3TreeNode* nodes = b3GetCompoundNodes( compound );
|
||||
// Tree nodes
|
||||
b3TreeNode* nodes = (b3TreeNode*)( (intptr_t)compound + nodeOffset );
|
||||
memcpy( nodes, tree.nodes, tree.nodeEnd * sizeof( b3TreeNode ) );
|
||||
compound->tree.nodes = nodes;
|
||||
|
||||
b3TreeProxy* proxies = b3GetCompoundProxies( compound );
|
||||
memcpy( proxies, tree.proxies, tree.proxyCount * sizeof( b3TreeProxy ) );
|
||||
compound->tree.proxies = proxies;
|
||||
|
||||
// Materials
|
||||
B3_ASSERT( materialCount > 0 );
|
||||
|
|
@ -685,50 +653,46 @@ void b3DestroyCompound( b3CompoundData* compound )
|
|||
b3Free( compound, compound->byteCount );
|
||||
}
|
||||
|
||||
uint8_t* b3ConvertCompoundToBytes( b3CompoundData* compound )
|
||||
const b3CompoundData* b3ValidateCompound( const uint8_t* bytes, int byteCount )
|
||||
{
|
||||
// scrub these pointers before serialization
|
||||
compound->tree.nodes = NULL;
|
||||
compound->tree.proxies = NULL;
|
||||
return (uint8_t*)compound;
|
||||
}
|
||||
if ( bytes == NULL || byteCount < (int)sizeof( b3CompoundData ) )
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
b3CompoundData* b3ConvertBytesToCompound( uint8_t* bytes, int byteCount )
|
||||
{
|
||||
b3CompoundData* compound = (b3CompoundData*)bytes;
|
||||
const b3CompoundData* compound = (const b3CompoundData*)bytes;
|
||||
if ( compound->version != B3_COMPOUND_VERSION )
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if ( compound->byteCount < (int)sizeof( b3CompoundData ) )
|
||||
if ( compound->byteCount != byteCount )
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if ( byteCount != compound->byteCount )
|
||||
if ( compound->nodeOffset < (int)sizeof( b3CompoundData ) || ( compound->nodeOffset & 7 ) != 0 )
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if ( compound->nodeOffset <= 0 || compound->proxyOffset <= 0 )
|
||||
if ( compound->nodeCount < 2 )
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int64_t nodeEnd = (int64_t)compound->nodeOffset + (int64_t)compound->nodeCount * (int64_t)sizeof( b3TreeNode );
|
||||
if ( nodeEnd > byteCount )
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// this mutates the input bytes
|
||||
compound->tree.nodes = (b3TreeNode*)( (intptr_t)compound + compound->nodeOffset );
|
||||
compound->tree.proxies = (b3TreeProxy*)( (intptr_t)compound + compound->proxyOffset );
|
||||
return compound;
|
||||
}
|
||||
|
||||
b3AABB b3ComputeCompoundAABB( const b3CompoundData* shape, b3Transform transform )
|
||||
{
|
||||
B3_ASSERT( shape->nodeOffset > 0 );
|
||||
|
||||
const b3TreeNode* nodes = (const b3TreeNode*)( (intptr_t)shape + shape->nodeOffset );
|
||||
b3AABB aabb = nodes[B3_ROOT_NODE].aabb;
|
||||
return b3AABB_Transform( transform, aabb );
|
||||
return b3AABB_Transform( transform, shape->bounds );
|
||||
}
|
||||
|
||||
struct b3CompoundOverlapContext
|
||||
|
|
@ -800,7 +764,8 @@ bool b3OverlapCompound( const b3CompoundData* shape, b3Transform shapeTransform,
|
|||
b3AABB aabb = b3ComputeProxyAABB( &context.proxy );
|
||||
|
||||
// This query must be in the compound frame.
|
||||
(void)b3DynamicTree_Query( &shape->tree, aabb, ~0ull, false, b3CompoundOverlapCallback, &context );
|
||||
b3TreeView view = b3GetCompoundTreeView( shape );
|
||||
(void)b3TreeView_Query( &view, aabb, ~0ull, false, b3CompoundOverlapCallback, &context );
|
||||
|
||||
return context.overlap;
|
||||
}
|
||||
|
|
@ -881,7 +846,8 @@ b3CastOutput b3RayCastCompound( const b3CompoundData* shape, const b3RayCastInpu
|
|||
.compound = shape,
|
||||
.output = &result,
|
||||
};
|
||||
(void)b3DynamicTree_RayCast( &shape->tree, input, ~0ull, false, b3CompoundRayCastCallback, &context );
|
||||
b3TreeView view = b3GetCompoundTreeView( shape );
|
||||
(void)b3TreeView_RayCast( &view, input, ~0ull, false, b3CompoundRayCastCallback, &context );
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
@ -967,7 +933,8 @@ b3CastOutput b3ShapeCastCompound( const b3CompoundData* shape, const b3ShapeCast
|
|||
// The compound tree is in the compound local frame, so the proxy box needs no origin offset
|
||||
b3AABB box = b3MakeAABB( input->proxy.points, input->proxy.count, input->proxy.radius );
|
||||
b3BoxCastInput treeInput = { box, input->translation, input->maxFraction };
|
||||
(void)b3DynamicTree_BoxCast( &shape->tree, &treeInput, ~0ull, false, b3CompoundShapeCastCallback, &context );
|
||||
b3TreeView view = b3GetCompoundTreeView( shape );
|
||||
(void)b3TreeView_BoxCast( &view, &treeInput, ~0ull, false, b3CompoundShapeCastCallback, &context );
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
@ -993,7 +960,8 @@ void b3QueryCompound( const b3CompoundData* compound, b3AABB aabb, b3CompoundQue
|
|||
.userContext = context,
|
||||
};
|
||||
|
||||
b3DynamicTree_Query( &compound->tree, aabb, B3_DEFAULT_MASK_BITS, false, TreeQueryCallbackFcn, &compoundContext );
|
||||
b3TreeView view = b3GetCompoundTreeView( compound );
|
||||
b3TreeView_Query( &view, aabb, B3_DEFAULT_MASK_BITS, false, TreeQueryCallbackFcn, &compoundContext );
|
||||
}
|
||||
|
||||
#if 0
|
||||
|
|
@ -1128,7 +1096,8 @@ b3TOIOutput b3CompoundTimeOfImpact(const b3CompoundData* compound, b3Transform t
|
|||
b3AABB localBounds = b3AABB_Transform( b3InvertTransform( context.compoundTransform ), bounds );
|
||||
context.localSweepBoundsB = localBounds;
|
||||
|
||||
b3DynamicTree_Query( &compound->tree, aabb, B3_DEFAULT_MASK_BITS, false, TreeQueryCallbackFcn, &compoundContext );
|
||||
b3TreeView view = b3GetCompoundTreeView( compound );
|
||||
b3TreeView_Query( &view, aabb, B3_DEFAULT_MASK_BITS, false, TreeQueryCallbackFcn, &compoundContext );
|
||||
|
||||
return context.toiOutput;
|
||||
}
|
||||
|
|
@ -1233,7 +1202,8 @@ int b3CollideMoverAndCompound( b3PlaneResult* planes, int capacity, const b3Comp
|
|||
aabb.lowerBound = b3Sub( aabb.lowerBound, r );
|
||||
aabb.upperBound = b3Add( aabb.upperBound, r );
|
||||
|
||||
(void)b3DynamicTree_Query( &shape->tree, aabb, ~0ull, false, b3CompoundMoverCallback, &context );
|
||||
b3TreeView view = b3GetCompoundTreeView( shape );
|
||||
(void)b3TreeView_Query( &view, aabb, ~0ull, false, b3CompoundMoverCallback, &context );
|
||||
|
||||
return context.planeCount;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,8 +3,19 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "dynamic_tree.h"
|
||||
|
||||
#include "box3d/types.h"
|
||||
|
||||
static inline b3TreeView b3GetCompoundTreeView( const b3CompoundData* compound )
|
||||
{
|
||||
b3TreeView view = {
|
||||
.nodes = (const b3TreeNode*)( (intptr_t)compound + compound->nodeOffset ),
|
||||
.proxies = NULL,
|
||||
};
|
||||
return view;
|
||||
}
|
||||
|
||||
b3TOIOutput b3CompoundTimeOfImpact( const b3CompoundData* compound, b3Transform transform, const b3ShapeProxy* proxy,
|
||||
const b3Sweep* sweep, float maxFraction );
|
||||
|
||||
|
|
|
|||
|
|
@ -867,17 +867,17 @@ B3_FORCE_INLINE bool b3TestCategory( uint64_t categoryBits, uint64_t maskBits, b
|
|||
return requireAllBits ? ( categoryBits & maskBits ) == maskBits : ( categoryBits & maskBits ) != 0;
|
||||
}
|
||||
|
||||
b3TreeStats b3DynamicTree_Query( const b3DynamicTree* tree, b3AABB aabb, uint64_t maskBits, bool requireAllBits,
|
||||
b3TreeQueryCallbackFcn* callback, void* context )
|
||||
B3_FORCE_INLINE b3TreeStats b3QueryTreeView( const b3TreeView* view, b3AABB aabb, uint64_t maskBits, bool requireAllBits,
|
||||
b3TreeQueryCallbackFcn* callback, void* context, bool hasProxies )
|
||||
{
|
||||
b3TreeStats result = { 0 };
|
||||
|
||||
if ( tree->proxyCount == 0 )
|
||||
if ( b3IsEmptyNode( view->nodes + B3_ROOT_NODE ) )
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
const b3TreeNode* nodes = tree->nodes;
|
||||
const b3TreeNode* nodes = view->nodes;
|
||||
|
||||
int stack[B3_TREE_STACK_SIZE];
|
||||
int stackCount = 0;
|
||||
|
|
@ -901,16 +901,23 @@ b3TreeStats b3DynamicTree_Query( const b3DynamicTree* tree, b3AABB aabb, uint64_
|
|||
if ( b3IsLeaf( node ) )
|
||||
{
|
||||
int proxyId = b3GetProxyId( node );
|
||||
const b3TreeProxy* proxy = tree->proxies + proxyId;
|
||||
if ( b3TestCategory( proxy->categoryBits, maskBits, requireAllBits ) )
|
||||
uint64_t userData = (uint64_t)(uint32_t)node->shapeIndex;
|
||||
if ( hasProxies )
|
||||
{
|
||||
bool proceed = callback( proxyId, proxy->userData, context );
|
||||
result.leafVisits += 1;
|
||||
|
||||
if ( proceed == false )
|
||||
const b3TreeProxy* proxy = view->proxies + proxyId;
|
||||
if ( b3TestCategory( proxy->categoryBits, maskBits, requireAllBits ) == false )
|
||||
{
|
||||
return result;
|
||||
continue;
|
||||
}
|
||||
userData = proxy->userData;
|
||||
}
|
||||
|
||||
bool proceed = callback( proxyId, userData, context );
|
||||
result.leafVisits += 1;
|
||||
|
||||
if ( proceed == false )
|
||||
{
|
||||
return result;
|
||||
}
|
||||
}
|
||||
else
|
||||
|
|
@ -942,17 +949,18 @@ struct b3QueryClosestItem
|
|||
float distanceToNodeSqr;
|
||||
};
|
||||
|
||||
b3TreeStats b3DynamicTree_QueryClosest( const b3DynamicTree* tree, b3Vec3 point, uint64_t maskBits, bool requireAllBits,
|
||||
b3TreeQueryClosestCallbackFcn* callback, void* context, float* minDistanceSqr )
|
||||
B3_FORCE_INLINE b3TreeStats b3QueryClosestTreeView( const b3TreeView* view, b3Vec3 point, uint64_t maskBits, bool requireAllBits,
|
||||
b3TreeQueryClosestCallbackFcn* callback, void* context, float* minDistanceSqr,
|
||||
bool hasProxies )
|
||||
{
|
||||
b3TreeStats result = { 0 };
|
||||
|
||||
if ( tree->proxyCount == 0 )
|
||||
if ( b3IsEmptyNode( view->nodes + B3_ROOT_NODE ) )
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
const b3TreeNode* nodes = tree->nodes;
|
||||
const b3TreeNode* nodes = view->nodes;
|
||||
|
||||
float minSqr = *minDistanceSqr;
|
||||
struct b3QueryClosestItem stack[B3_TREE_STACK_SIZE];
|
||||
|
|
@ -1002,14 +1010,19 @@ b3TreeStats b3DynamicTree_QueryClosest( const b3DynamicTree* tree, b3Vec3 point,
|
|||
if ( b3IsLeaf( node ) )
|
||||
{
|
||||
int proxyId = b3GetProxyId( node );
|
||||
const b3TreeProxy* proxy = tree->proxies + proxyId;
|
||||
if ( b3TestCategory( proxy->categoryBits, maskBits, requireAllBits ) == false )
|
||||
uint64_t userData = (uint64_t)(uint32_t)node->shapeIndex;
|
||||
if ( hasProxies )
|
||||
{
|
||||
continue;
|
||||
const b3TreeProxy* proxy = view->proxies + proxyId;
|
||||
if ( b3TestCategory( proxy->categoryBits, maskBits, requireAllBits ) == false )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
userData = proxy->userData;
|
||||
}
|
||||
|
||||
// callback to user code with minimum distance squared so far and proxy id
|
||||
float dd = callback( minSqr, proxyId, proxy->userData, context );
|
||||
float dd = callback( minSqr, proxyId, userData, context );
|
||||
|
||||
if ( dd < minSqr )
|
||||
{
|
||||
|
|
@ -1059,12 +1072,13 @@ b3TreeStats b3DynamicTree_QueryClosest( const b3DynamicTree* tree, b3Vec3 point,
|
|||
|
||||
// A lot of optimization work went into this. The children of a popped pair are both tested
|
||||
// before either is followed, then the survivors are ordered so the closer one narrows first.
|
||||
b3TreeStats b3DynamicTree_RayCast( const b3DynamicTree* tree, const b3RayCastInput* input, uint64_t maskBits, bool requireAllBits,
|
||||
b3TreeRayCastCallbackFcn* callback, void* context )
|
||||
B3_FORCE_INLINE b3TreeStats b3RayCastTreeView( const b3TreeView* view, const b3RayCastInput* input, uint64_t maskBits,
|
||||
bool requireAllBits, b3TreeRayCastCallbackFcn* callback, void* context,
|
||||
bool hasProxies )
|
||||
{
|
||||
b3TreeStats result = { 0 };
|
||||
|
||||
if ( tree->proxyCount == 0 )
|
||||
if ( b3IsEmptyNode( view->nodes + B3_ROOT_NODE ) )
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
|
@ -1083,7 +1097,7 @@ b3TreeStats b3DynamicTree_RayCast( const b3DynamicTree* tree, const b3RayCastInp
|
|||
b3AABB segmentAABB = { b3Min( p1, p2 ), b3Max( p1, p2 ) };
|
||||
b3AABBV boxv = b3LoadAABBV( &segmentAABB );
|
||||
|
||||
const b3TreeNode* nodes = tree->nodes;
|
||||
const b3TreeNode* nodes = view->nodes;
|
||||
|
||||
int stack[B3_TREE_STACK_SIZE];
|
||||
int stackCount = 0;
|
||||
|
|
@ -1140,16 +1154,20 @@ b3TreeStats b3DynamicTree_RayCast( const b3DynamicTree* tree, const b3RayCastInp
|
|||
if ( isLeaf[i] )
|
||||
{
|
||||
int proxyId = b3GetProxyId( hit[i] );
|
||||
const b3TreeProxy* proxy = tree->proxies + proxyId;
|
||||
|
||||
if ( b3TestCategory( proxy->categoryBits, maskBits, requireAllBits ) == false )
|
||||
uint64_t userData = (uint64_t)(uint32_t)hit[i]->shapeIndex;
|
||||
if ( hasProxies )
|
||||
{
|
||||
continue;
|
||||
const b3TreeProxy* proxy = view->proxies + proxyId;
|
||||
if ( b3TestCategory( proxy->categoryBits, maskBits, requireAllBits ) == false )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
userData = proxy->userData;
|
||||
}
|
||||
|
||||
subInput.maxFraction = maxFraction;
|
||||
|
||||
float value = callback( &subInput, proxyId, proxy->userData, context );
|
||||
float value = callback( &subInput, proxyId, userData, context );
|
||||
result.leafVisits += 1;
|
||||
|
||||
// The user may return -1 to indicate this shape should be skipped
|
||||
|
|
@ -1188,12 +1206,13 @@ b3TreeStats b3DynamicTree_RayCast( const b3DynamicTree* tree, const b3RayCastInp
|
|||
}
|
||||
|
||||
// Follows structure of ray cast with small tweaks to handle a swept box.
|
||||
b3TreeStats b3DynamicTree_BoxCast( const b3DynamicTree* tree, const b3BoxCastInput* input, uint64_t maskBits, bool requireAllBits,
|
||||
b3TreeBoxCastCallbackFcn* callback, void* context )
|
||||
B3_FORCE_INLINE b3TreeStats b3BoxCastTreeView( const b3TreeView* view, const b3BoxCastInput* input, uint64_t maskBits,
|
||||
bool requireAllBits, b3TreeBoxCastCallbackFcn* callback, void* context,
|
||||
bool hasProxies )
|
||||
{
|
||||
b3TreeStats result = { 0 };
|
||||
|
||||
if ( tree->proxyCount == 0 )
|
||||
if ( b3IsEmptyNode( view->nodes + B3_ROOT_NODE ) )
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
|
@ -1220,7 +1239,7 @@ b3TreeStats b3DynamicTree_BoxCast( const b3DynamicTree* tree, const b3BoxCastInp
|
|||
};
|
||||
b3AABBV boxv = b3LoadAABBV( &totalAABB );
|
||||
|
||||
const b3TreeNode* nodes = tree->nodes;
|
||||
const b3TreeNode* nodes = view->nodes;
|
||||
|
||||
int stack[B3_TREE_STACK_SIZE];
|
||||
int stackCount = 0;
|
||||
|
|
@ -1278,16 +1297,20 @@ b3TreeStats b3DynamicTree_BoxCast( const b3DynamicTree* tree, const b3BoxCastInp
|
|||
if ( isLeaf[i] )
|
||||
{
|
||||
int proxyId = b3GetProxyId( hit[i] );
|
||||
const b3TreeProxy* proxy = tree->proxies + proxyId;
|
||||
|
||||
if ( b3TestCategory( proxy->categoryBits, maskBits, requireAllBits ) == false )
|
||||
uint64_t userData = (uint64_t)(uint32_t)hit[i]->shapeIndex;
|
||||
if ( hasProxies )
|
||||
{
|
||||
continue;
|
||||
const b3TreeProxy* proxy = view->proxies + proxyId;
|
||||
if ( b3TestCategory( proxy->categoryBits, maskBits, requireAllBits ) == false )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
userData = proxy->userData;
|
||||
}
|
||||
|
||||
subInput.maxFraction = maxFraction;
|
||||
|
||||
float value = callback( &subInput, proxyId, proxy->userData, context );
|
||||
float value = callback( &subInput, proxyId, userData, context );
|
||||
result.leafVisits += 1;
|
||||
|
||||
if ( value == 0.0f )
|
||||
|
|
@ -1322,6 +1345,97 @@ b3TreeStats b3DynamicTree_BoxCast( const b3DynamicTree* tree, const b3BoxCastInp
|
|||
return result;
|
||||
}
|
||||
|
||||
b3TreeStats b3DynamicTree_Query( const b3DynamicTree* tree, b3AABB aabb, uint64_t maskBits, bool requireAllBits,
|
||||
b3TreeQueryCallbackFcn* callback, void* context )
|
||||
{
|
||||
if ( tree->proxyCount == 0 )
|
||||
{
|
||||
return B3_LITERAL( b3TreeStats ){ 0 };
|
||||
}
|
||||
|
||||
b3TreeView view = b3MakeTreeView( tree );
|
||||
return b3QueryTreeView( &view, aabb, maskBits, requireAllBits, callback, context, true );
|
||||
}
|
||||
|
||||
b3TreeStats b3DynamicTree_QueryClosest( const b3DynamicTree* tree, b3Vec3 point, uint64_t maskBits, bool requireAllBits,
|
||||
b3TreeQueryClosestCallbackFcn* callback, void* context, float* minDistanceSqr )
|
||||
{
|
||||
if ( tree->proxyCount == 0 )
|
||||
{
|
||||
return B3_LITERAL( b3TreeStats ){ 0 };
|
||||
}
|
||||
|
||||
b3TreeView view = b3MakeTreeView( tree );
|
||||
return b3QueryClosestTreeView( &view, point, maskBits, requireAllBits, callback, context, minDistanceSqr, true );
|
||||
}
|
||||
|
||||
b3TreeStats b3DynamicTree_RayCast( const b3DynamicTree* tree, const b3RayCastInput* input, uint64_t maskBits, bool requireAllBits,
|
||||
b3TreeRayCastCallbackFcn* callback, void* context )
|
||||
{
|
||||
if ( tree->proxyCount == 0 )
|
||||
{
|
||||
return B3_LITERAL( b3TreeStats ){ 0 };
|
||||
}
|
||||
|
||||
b3TreeView view = b3MakeTreeView( tree );
|
||||
return b3RayCastTreeView( &view, input, maskBits, requireAllBits, callback, context, true );
|
||||
}
|
||||
|
||||
b3TreeStats b3DynamicTree_BoxCast( const b3DynamicTree* tree, const b3BoxCastInput* input, uint64_t maskBits, bool requireAllBits,
|
||||
b3TreeBoxCastCallbackFcn* callback, void* context )
|
||||
{
|
||||
if ( tree->proxyCount == 0 )
|
||||
{
|
||||
return B3_LITERAL( b3TreeStats ){ 0 };
|
||||
}
|
||||
|
||||
b3TreeView view = b3MakeTreeView( tree );
|
||||
return b3BoxCastTreeView( &view, input, maskBits, requireAllBits, callback, context, true );
|
||||
}
|
||||
|
||||
b3TreeStats b3TreeView_Query( const b3TreeView* view, b3AABB aabb, uint64_t maskBits, bool requireAllBits,
|
||||
b3TreeQueryCallbackFcn* callback, void* context )
|
||||
{
|
||||
if ( view->proxies == NULL )
|
||||
{
|
||||
return b3QueryTreeView( view, aabb, maskBits, requireAllBits, callback, context, false );
|
||||
}
|
||||
|
||||
return b3QueryTreeView( view, aabb, maskBits, requireAllBits, callback, context, true );
|
||||
}
|
||||
|
||||
b3TreeStats b3TreeView_QueryClosest( const b3TreeView* view, b3Vec3 point, uint64_t maskBits, bool requireAllBits,
|
||||
b3TreeQueryClosestCallbackFcn* callback, void* context, float* minDistanceSqr )
|
||||
{
|
||||
if ( view->proxies == NULL )
|
||||
{
|
||||
return b3QueryClosestTreeView( view, point, maskBits, requireAllBits, callback, context, minDistanceSqr, false );
|
||||
}
|
||||
|
||||
return b3QueryClosestTreeView( view, point, maskBits, requireAllBits, callback, context, minDistanceSqr, true );
|
||||
}
|
||||
|
||||
b3TreeStats b3TreeView_RayCast( const b3TreeView* view, const b3RayCastInput* input, uint64_t maskBits, bool requireAllBits,
|
||||
b3TreeRayCastCallbackFcn* callback, void* context )
|
||||
{
|
||||
if ( view->proxies == NULL )
|
||||
{
|
||||
return b3RayCastTreeView( view, input, maskBits, requireAllBits, callback, context, false );
|
||||
}
|
||||
|
||||
return b3RayCastTreeView( view, input, maskBits, requireAllBits, callback, context, true );
|
||||
}
|
||||
|
||||
b3TreeStats b3TreeView_BoxCast( const b3TreeView* view, const b3BoxCastInput* input, uint64_t maskBits, bool requireAllBits,
|
||||
b3TreeBoxCastCallbackFcn* callback, void* context )
|
||||
{
|
||||
if ( view->proxies == NULL )
|
||||
{
|
||||
return b3BoxCastTreeView( view, input, maskBits, requireAllBits, callback, context, false );
|
||||
}
|
||||
|
||||
return b3BoxCastTreeView( view, input, maskBits, requireAllBits, callback, context, true );
|
||||
}
|
||||
|
||||
// Median split == 0, Surface area heuristic == 1
|
||||
#define B3_TREE_HEURISTIC 0
|
||||
|
|
|
|||
|
|
@ -69,6 +69,30 @@ B3_FORCE_INLINE b3TreeNode b3MakeEmptyNode( void )
|
|||
};
|
||||
}
|
||||
|
||||
typedef struct b3TreeView
|
||||
{
|
||||
const b3TreeNode* nodes;
|
||||
const b3TreeProxy* proxies;
|
||||
} b3TreeView;
|
||||
|
||||
static inline b3TreeView b3MakeTreeView( const b3DynamicTree* tree )
|
||||
{
|
||||
b3TreeView view = {
|
||||
.nodes = tree->nodes,
|
||||
.proxies = tree->proxies,
|
||||
};
|
||||
return view;
|
||||
}
|
||||
|
||||
b3TreeStats b3TreeView_Query( const b3TreeView* view, b3AABB aabb, uint64_t maskBits, bool requireAllBits,
|
||||
b3TreeQueryCallbackFcn* callback, void* context );
|
||||
b3TreeStats b3TreeView_QueryClosest( const b3TreeView* view, b3Vec3 point, uint64_t maskBits, bool requireAllBits,
|
||||
b3TreeQueryClosestCallbackFcn* callback, void* context, float* minDistanceSqr );
|
||||
b3TreeStats b3TreeView_RayCast( const b3TreeView* view, const b3RayCastInput* input, uint64_t maskBits, bool requireAllBits,
|
||||
b3TreeRayCastCallbackFcn* callback, void* context );
|
||||
b3TreeStats b3TreeView_BoxCast( const b3TreeView* view, const b3BoxCastInput* input, uint64_t maskBits, bool requireAllBits,
|
||||
b3TreeBoxCastCallbackFcn* callback, void* context );
|
||||
|
||||
static inline bool b3HasTreeMoved( const b3DynamicTree* tree )
|
||||
{
|
||||
return b3IsNodeMoved( tree->nodes + B3_ROOT_NODE );
|
||||
|
|
|
|||
|
|
@ -762,16 +762,19 @@ void b3RecEndRecord( b3Recording* rec )
|
|||
|
||||
// Append a fresh entry and splice it onto the front of its hash chain. The map value is the chain head.
|
||||
static uint32_t b3RegistryPush( b3GeometryRegistry* reg, b3GeometryHashMap* map, b3GeometryHashMap_itr itr, bool hashPresent,
|
||||
b3GeometryKind kind, uint64_t contentHash, uint8_t* bytes, int byteCount )
|
||||
b3GeometryKind kind, uint64_t contentHash, const uint8_t* bytes, int byteCount )
|
||||
{
|
||||
B3_ASSERT( byteCount > 0 );
|
||||
uint8_t* copy = b3Alloc( (size_t)byteCount );
|
||||
memcpy( copy, bytes, (size_t)byteCount );
|
||||
|
||||
uint32_t id = (uint32_t)reg->entries.count;
|
||||
b3GeometryEntry* entry = b3Array_Emplace( reg->entries );
|
||||
entry->contentHash = contentHash;
|
||||
entry->id = id;
|
||||
entry->kind = kind;
|
||||
entry->byteCount = byteCount;
|
||||
// Take ownership.
|
||||
entry->bytes = bytes;
|
||||
entry->bytes = copy;
|
||||
entry->hashNext = hashPresent ? (int)itr.data->val : B3_NULL_INDEX;
|
||||
|
||||
if ( hashPresent )
|
||||
|
|
@ -796,7 +799,7 @@ static b3GeometryHashMap* b3RegistryMap( b3GeometryRegistry* reg )
|
|||
return reg->dedupMap;
|
||||
}
|
||||
|
||||
uint32_t b3InternGeometry( b3GeometryRegistry* reg, b3GeometryKind kind, uint64_t contentHash, uint8_t* bytes, int byteCount )
|
||||
uint32_t b3InternGeometry( b3GeometryRegistry* reg, b3GeometryKind kind, uint64_t contentHash, const uint8_t* bytes, int byteCount )
|
||||
{
|
||||
b3GeometryHashMap* map = b3RegistryMap( reg );
|
||||
|
||||
|
|
@ -810,8 +813,6 @@ uint32_t b3InternGeometry( b3GeometryRegistry* reg, b3GeometryKind kind, uint64_
|
|||
b3GeometryEntry* e = reg->entries.data + index;
|
||||
if ( e->byteCount == byteCount && memcmp( e->bytes, bytes, (size_t)byteCount ) == 0 )
|
||||
{
|
||||
// Duplicate. Free bytes because the caller transferred ownership. Return existing id.
|
||||
b3Free( bytes, (size_t)byteCount );
|
||||
return e->id;
|
||||
}
|
||||
}
|
||||
|
|
@ -820,7 +821,7 @@ uint32_t b3InternGeometry( b3GeometryRegistry* reg, b3GeometryKind kind, uint64_
|
|||
return b3RegistryPush( reg, map, itr, hashPresent, kind, contentHash, bytes, byteCount );
|
||||
}
|
||||
|
||||
uint32_t b3AppendGeometry( b3GeometryRegistry* reg, b3GeometryKind kind, uint64_t contentHash, uint8_t* bytes, int byteCount )
|
||||
uint32_t b3AppendGeometry( b3GeometryRegistry* reg, b3GeometryKind kind, uint64_t contentHash, const uint8_t* bytes, int byteCount )
|
||||
{
|
||||
b3GeometryHashMap* map = b3RegistryMap( reg );
|
||||
b3GeometryHashMap_itr itr = b3GeometryHashMap_get( map, contentHash );
|
||||
|
|
@ -1146,43 +1147,31 @@ b3Recording* b3LoadRecordingFromFile( const char* path )
|
|||
|
||||
// Geometry interning helpers
|
||||
|
||||
static uint32_t b3RecInternBlob( b3Recording* rec, b3GeometryKind kind, const void* blob, int byteCount )
|
||||
{
|
||||
const uint8_t* bytes = (const uint8_t*)blob;
|
||||
uint64_t h = b3Hash64NonZero( bytes, byteCount );
|
||||
return b3InternGeometry( &rec->registry, kind, h, bytes, byteCount );
|
||||
}
|
||||
|
||||
uint32_t b3RecInternHull( b3Recording* rec, const b3HullData* hull )
|
||||
{
|
||||
int byteCount = hull->byteCount;
|
||||
uint8_t* bytes = b3Alloc( (size_t)byteCount );
|
||||
memcpy( bytes, hull, (size_t)byteCount );
|
||||
uint64_t h = b3Hash64NonZero( bytes, byteCount );
|
||||
return b3InternGeometry( &rec->registry, b3_geometryHull, h, bytes, byteCount );
|
||||
return b3RecInternBlob( rec, b3_geometryHull, hull, hull->byteCount );
|
||||
}
|
||||
|
||||
uint32_t b3RecInternMesh( b3Recording* rec, const b3MeshData* mesh )
|
||||
{
|
||||
int byteCount = mesh->byteCount;
|
||||
uint8_t* bytes = b3Alloc( (size_t)byteCount );
|
||||
memcpy( bytes, mesh, (size_t)byteCount );
|
||||
uint64_t h = b3Hash64NonZero( bytes, byteCount );
|
||||
return b3InternGeometry( &rec->registry, b3_geometryMesh, h, bytes, byteCount );
|
||||
return b3RecInternBlob( rec, b3_geometryMesh, mesh, mesh->byteCount );
|
||||
}
|
||||
|
||||
uint32_t b3RecInternHeightField( b3Recording* rec, const b3HeightFieldData* hf )
|
||||
{
|
||||
int byteCount = hf->byteCount;
|
||||
uint8_t* bytes = b3Alloc( (size_t)byteCount );
|
||||
memcpy( bytes, hf, (size_t)byteCount );
|
||||
uint64_t h = b3Hash64NonZero( bytes, byteCount );
|
||||
return b3InternGeometry( &rec->registry, b3_geometryHeightField, h, bytes, byteCount );
|
||||
return b3RecInternBlob( rec, b3_geometryHeightField, hf, hf->byteCount );
|
||||
}
|
||||
|
||||
uint32_t b3RecInternCompound( b3Recording* rec, const b3CompoundData* compound )
|
||||
{
|
||||
int byteCount = compound->byteCount;
|
||||
uint8_t* bytes = b3Alloc( (size_t)byteCount );
|
||||
memcpy( bytes, compound, (size_t)byteCount );
|
||||
// Null the tree pointers in the copy so the canonical bytes are pointer-free.
|
||||
// b3ConvertBytesToCompound fixes it back on load via nodeOffset.
|
||||
b3ConvertCompoundToBytes( (b3CompoundData*)bytes );
|
||||
uint64_t h = b3Hash64NonZero( bytes, byteCount );
|
||||
return b3InternGeometry( &rec->registry, b3_geometryCompound, h, bytes, byteCount );
|
||||
return b3RecInternBlob( rec, b3_geometryCompound, compound, compound->byteCount );
|
||||
}
|
||||
|
||||
uint64_t b3HashWorldState( b3World* world )
|
||||
|
|
|
|||
|
|
@ -351,11 +351,11 @@ float b3RecCastTrampoline( b3ShapeId id, b3Pos point, b3Vec3 normal, float fract
|
|||
bool b3RecPlaneTrampoline( b3ShapeId id, const b3PlaneResult* planes, int planeCount, void* ctx );
|
||||
|
||||
// Geometry registry
|
||||
uint32_t b3InternGeometry( b3GeometryRegistry* reg, b3GeometryKind kind, uint64_t contentHash, uint8_t* bytes, int byteCount );
|
||||
uint32_t b3InternGeometry( b3GeometryRegistry* reg, b3GeometryKind kind, uint64_t contentHash, const uint8_t* bytes, int byteCount );
|
||||
// Append an entry unconditionally and return its id, which equals its array index. Unlike
|
||||
// b3InternGeometry it never deduplicates, so the keyframe seed can mirror slots 1:1 even when an
|
||||
// already-recorded file carries byte-identical duplicate slots (a hash collision wrote them apart).
|
||||
uint32_t b3AppendGeometry( b3GeometryRegistry* reg, b3GeometryKind kind, uint64_t contentHash, uint8_t* bytes, int byteCount );
|
||||
uint32_t b3AppendGeometry( b3GeometryRegistry* reg, b3GeometryKind kind, uint64_t contentHash, const uint8_t* bytes, int byteCount );
|
||||
void b3FreeRegistry( b3GeometryRegistry* reg );
|
||||
void b3RecWriteRegistry( b3Recording* rec );
|
||||
|
||||
|
|
@ -367,7 +367,6 @@ uint64_t b3HashQueryTag( uint64_t id, const char* name );
|
|||
void b3RecInternTag( b3Recording* rec, uint64_t key, uint64_t id, const char* name );
|
||||
|
||||
// Intern each large geometry kind and return a stable u32 id for use in create ops.
|
||||
// Caller does NOT free bytes; b3InternGeometry takes ownership (frees on duplicate).
|
||||
uint32_t b3RecInternHull( b3Recording* rec, const b3HullData* hull );
|
||||
uint32_t b3RecInternMesh( b3Recording* rec, const b3MeshData* mesh );
|
||||
uint32_t b3RecInternHeightField( b3Recording* rec, const b3HeightFieldData* hf );
|
||||
|
|
|
|||
173
src/replay.c
173
src/replay.c
|
|
@ -710,9 +710,6 @@ static void b3RecCheckJointId( b3RecReader* rdr, b3JointId got, b3JointId rec )
|
|||
b3RecCheckId( rdr, "joint", got.index1, got.generation, rec.index1, rec.generation );
|
||||
}
|
||||
|
||||
// Registry slot reconstruction. Returns the live pointer for the given slot, building it
|
||||
// on first use. The hull case is handled inline at the call site since it doesn't cache.
|
||||
|
||||
b3RegistrySlot* b3RecGetSlot( b3RecReader* rdr, uint32_t id, b3GeometryKind kind )
|
||||
{
|
||||
if ( rdr == NULL || id >= (uint32_t)rdr->slotCount || rdr->slots[id].kind != kind )
|
||||
|
|
@ -722,42 +719,6 @@ b3RegistrySlot* b3RecGetSlot( b3RecReader* rdr, uint32_t id, b3GeometryKind kind
|
|||
return rdr->slots + id;
|
||||
}
|
||||
|
||||
static void* b3RecGetLiveMesh( b3RegistrySlot* slot )
|
||||
{
|
||||
// Mesh is a self-contained blob used by reference, with no pointer fixup. Hand back the pristine
|
||||
// bytes directly: they already outlive the world and are freed at teardown, so a copy would just
|
||||
// double the memory. Compound can't do this, see b3RecGetLiveCompound.
|
||||
return slot->bytes;
|
||||
}
|
||||
|
||||
static void* b3RecGetLiveHeightField( b3RegistrySlot* slot )
|
||||
{
|
||||
// Self-contained blob used by reference, like b3RecGetLiveMesh. The bytes already are a
|
||||
// valid b3HeightFieldData with no pointer fixup, so hand them back directly.
|
||||
return slot->bytes;
|
||||
}
|
||||
|
||||
const b3CompoundData* b3RecGetLiveCompound( b3RegistrySlot* slot )
|
||||
{
|
||||
B3_ASSERT( slot->kind == b3_geometryCompound );
|
||||
if ( slot->live != NULL )
|
||||
{
|
||||
return (const b3CompoundData*)slot->live;
|
||||
}
|
||||
// The copy is unavoidable here: b3ConvertBytesToCompound rewrites its input in place, while the
|
||||
// pristine bytes must survive for keyframe registry seeding (b3RecSeedKeyframeRegistry). So we
|
||||
// keep both the serialized bytes and a separate converted live object.
|
||||
uint8_t* live = (uint8_t*)b3Alloc( (size_t)slot->byteCount );
|
||||
memcpy( live, slot->bytes, (size_t)slot->byteCount );
|
||||
if ( b3ConvertBytesToCompound( live, slot->byteCount ) == NULL )
|
||||
{
|
||||
b3Free( live, (size_t)slot->byteCount );
|
||||
return NULL;
|
||||
}
|
||||
slot->live = live;
|
||||
return (const b3CompoundData*)live;
|
||||
}
|
||||
|
||||
// Dispatch functions, one per op
|
||||
|
||||
static void b3RecDispatch_DestroyWorld( const b3RecArgs_DestroyWorld* a, b3RecReader* rdr )
|
||||
|
|
@ -1058,7 +1019,7 @@ static void b3RecDispatch_CreateMeshShape( const b3RecArgs_CreateMeshShape* a, b
|
|||
rdr->ok = false;
|
||||
return;
|
||||
}
|
||||
const b3MeshData* mesh = b3RecGetLiveMesh( slot );
|
||||
const b3MeshData* mesh = (const b3MeshData*)slot->bytes;
|
||||
b3BodyId bodyId = b3RecMakeBodyId( rdr, a->body );
|
||||
b3ShapeId gotId = b3CreateMeshShape( bodyId, &a->def, mesh, a->scale );
|
||||
b3RecCheckShapeId( rdr, gotId, recId );
|
||||
|
|
@ -1079,13 +1040,7 @@ static void b3RecDispatch_CreateHeightFieldShape( const b3RecArgs_CreateHeightFi
|
|||
rdr->ok = false;
|
||||
return;
|
||||
}
|
||||
const b3HeightFieldData* hf = (const b3HeightFieldData*)b3RecGetLiveHeightField( slot );
|
||||
if ( hf == NULL )
|
||||
{
|
||||
printf( "b3ReplayFile: heightfield geometry %u is corrupt\n", id );
|
||||
rdr->ok = false;
|
||||
return;
|
||||
}
|
||||
const b3HeightFieldData* hf = (const b3HeightFieldData*)slot->bytes;
|
||||
b3BodyId bodyId = b3RecMakeBodyId( rdr, a->body );
|
||||
b3ShapeId gotId = b3CreateHeightFieldShape( bodyId, &a->def, hf );
|
||||
b3RecCheckShapeId( rdr, gotId, recId );
|
||||
|
|
@ -1106,13 +1061,7 @@ static void b3RecDispatch_CreateCompoundShape( const b3RecArgs_CreateCompoundSha
|
|||
rdr->ok = false;
|
||||
return;
|
||||
}
|
||||
const b3CompoundData* compound = b3RecGetLiveCompound( slot );
|
||||
if ( compound == NULL )
|
||||
{
|
||||
printf( "b3ReplayFile: compound geometry %u is corrupt\n", id );
|
||||
rdr->ok = false;
|
||||
return;
|
||||
}
|
||||
const b3CompoundData* compound = (const b3CompoundData*)slot->bytes;
|
||||
b3BodyId bodyId = b3RecMakeBodyId( rdr, a->body );
|
||||
// b3CreateCompoundShape takes a non-const def pointer; cast away const for the scratch def
|
||||
b3ShapeDef shapeDef = a->def;
|
||||
|
|
@ -1215,7 +1164,7 @@ static void b3RecDispatch_ShapeSetMesh( const b3RecArgs_ShapeSetMesh* a, b3RecRe
|
|||
return;
|
||||
}
|
||||
b3ShapeId shapeId = b3RecMakeShapeId( rdr, a->shape );
|
||||
const b3MeshData* mesh = b3RecGetLiveMesh( slot );
|
||||
const b3MeshData* mesh = (const b3MeshData*)slot->bytes;
|
||||
b3Shape_SetMesh( shapeId, mesh, a->scale );
|
||||
}
|
||||
|
||||
|
|
@ -2440,6 +2389,24 @@ static void b3RecLoadTags( b3RecReader* rdr, const uint8_t* rp, const uint8_t* d
|
|||
rdr->tagMap = map;
|
||||
}
|
||||
|
||||
// Free slots loaded by b3RecLoadSlots.
|
||||
static void b3RecFreeSlots( b3RegistrySlot* slots, int slotCount )
|
||||
{
|
||||
if ( slots == NULL )
|
||||
{
|
||||
return;
|
||||
}
|
||||
for ( int i = 0; i < slotCount; ++i )
|
||||
{
|
||||
b3RegistrySlot* slot = slots + i;
|
||||
if ( slot->bytes != NULL )
|
||||
{
|
||||
b3Free( slot->bytes, (size_t)slot->byteCount );
|
||||
}
|
||||
}
|
||||
b3Free( slots, (size_t)slotCount * sizeof( b3RegistrySlot ) );
|
||||
}
|
||||
|
||||
// Load the trailing registry block and fill rdr->slots/slotCount, then the optional tag table.
|
||||
// Returns true on success. On failure sets rdr->ok = false and returns false.
|
||||
static bool b3RecLoadSlots( b3RecReader* rdr, const void* data, int size, uint64_t registryOffset, uint64_t registryByteCount )
|
||||
|
|
@ -2493,51 +2460,43 @@ static bool b3RecLoadSlots( b3RecReader* rdr, const void* data, int size, uint64
|
|||
if ( rp + 5 > dataEnd )
|
||||
{
|
||||
printf( "b3ReplayFile: registry truncated at entry %u\n", i );
|
||||
for ( uint32_t j = 0; j < i; ++j )
|
||||
{
|
||||
if ( slots[j].bytes != NULL )
|
||||
{
|
||||
b3Free( slots[j].bytes, (size_t)slots[j].byteCount );
|
||||
}
|
||||
}
|
||||
b3Free( slots, (size_t)count * sizeof( b3RegistrySlot ) );
|
||||
b3RecFreeSlots( slots, (int)count );
|
||||
return false;
|
||||
}
|
||||
uint8_t kind = rp[0];
|
||||
uint32_t byteCount = (uint32_t)rp[1] | ( (uint32_t)rp[2] << 8 ) | ( (uint32_t)rp[3] << 16 ) | ( (uint32_t)rp[4] << 24 );
|
||||
rp += 5;
|
||||
const char* error = NULL;
|
||||
if ( rp + byteCount > dataEnd )
|
||||
if ( byteCount == 0 || byteCount > INT32_MAX )
|
||||
{
|
||||
error = "has an invalid byte count";
|
||||
}
|
||||
else if ( (size_t)byteCount > (size_t)( dataEnd - rp ) )
|
||||
{
|
||||
error = "bytes out of bounds";
|
||||
}
|
||||
else if ( kind == b3_geometryCompound && byteCount < (uint32_t)sizeof( b3CompoundData ) )
|
||||
else
|
||||
{
|
||||
error = "too small for a compound";
|
||||
uint8_t* bytes = (uint8_t*)b3Alloc( (size_t)byteCount );
|
||||
memcpy( bytes, rp, (size_t)byteCount );
|
||||
slots[i].kind = (b3GeometryKind)kind;
|
||||
slots[i].byteCount = (int)byteCount;
|
||||
slots[i].bytes = bytes;
|
||||
|
||||
// Validate the compound.
|
||||
// todo validate other types?
|
||||
if ( kind == b3_geometryCompound && b3ValidateCompound( bytes, (int)byteCount ) == NULL )
|
||||
{
|
||||
error = "is not a valid compound";
|
||||
}
|
||||
}
|
||||
if ( error != NULL )
|
||||
{
|
||||
printf( "b3ReplayFile: registry entry %u %s\n", i, error );
|
||||
for ( uint32_t j = 0; j < i; ++j )
|
||||
{
|
||||
if ( slots[j].bytes != NULL )
|
||||
{
|
||||
b3Free( slots[j].bytes, (size_t)slots[j].byteCount );
|
||||
}
|
||||
}
|
||||
b3Free( slots, (size_t)count * sizeof( b3RegistrySlot ) );
|
||||
b3RecFreeSlots( slots, (int)count );
|
||||
return false;
|
||||
}
|
||||
uint8_t* bytes = (uint8_t*)b3Alloc( byteCount > 0 ? (size_t)byteCount : 1u );
|
||||
if ( byteCount > 0 )
|
||||
{
|
||||
memcpy( bytes, rp, (size_t)byteCount );
|
||||
}
|
||||
rp += byteCount;
|
||||
slots[i].kind = (b3GeometryKind)kind;
|
||||
slots[i].byteCount = (int)byteCount;
|
||||
slots[i].bytes = bytes;
|
||||
slots[i].live = NULL;
|
||||
}
|
||||
|
||||
rdr->slots = slots;
|
||||
|
|
@ -2546,36 +2505,6 @@ static bool b3RecLoadSlots( b3RecReader* rdr, const void* data, int size, uint64
|
|||
return true;
|
||||
}
|
||||
|
||||
// Free slots loaded by b3RecLoadSlots.
|
||||
static void b3RecFreeSlots( b3RegistrySlot* slots, int slotCount )
|
||||
{
|
||||
if ( slots == NULL )
|
||||
{
|
||||
return;
|
||||
}
|
||||
for ( int i = 0; i < slotCount; ++i )
|
||||
{
|
||||
b3RegistrySlot* slot = slots + i;
|
||||
if ( slot->live != NULL )
|
||||
{
|
||||
switch ( slot->kind )
|
||||
{
|
||||
// Mesh and height field have no separate live object; they borrow the bytes freed below.
|
||||
case b3_geometryCompound:
|
||||
b3Free( slot->live, (size_t)slot->byteCount );
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ( slot->bytes != NULL )
|
||||
{
|
||||
b3Free( slot->bytes, slot->byteCount > 0 ? (size_t)slot->byteCount : 1u );
|
||||
}
|
||||
}
|
||||
b3Free( slots, (size_t)slotCount * sizeof( b3RegistrySlot ) );
|
||||
}
|
||||
|
||||
// Walk the op stream once without dispatching: count Step ops and grab the first step's tuning.
|
||||
static void b3RecScanFile( b3RecPlayer* player )
|
||||
{
|
||||
|
|
@ -2643,20 +2572,8 @@ static void b3RecSeedKeyframeRegistry( b3RecPlayer* player )
|
|||
for ( int i = 0; i < player->rdr.slotCount; ++i )
|
||||
{
|
||||
b3RegistrySlot* slot = player->rdr.slots + i;
|
||||
// Copy so the registry can take ownership.
|
||||
int n = slot->byteCount > 0 ? slot->byteCount : 1;
|
||||
uint8_t* copy = (uint8_t*)b3Alloc( (size_t)n );
|
||||
if ( slot->byteCount > 0 )
|
||||
{
|
||||
memcpy( copy, slot->bytes, (size_t)slot->byteCount );
|
||||
}
|
||||
if ( slot->kind == b3_geometryCompound )
|
||||
{
|
||||
// Null the pointers in the compound
|
||||
b3ConvertCompoundToBytes( (b3CompoundData*)copy );
|
||||
}
|
||||
uint64_t h = b3Hash64NonZero( copy, slot->byteCount );
|
||||
uint32_t id = b3AppendGeometry( reg, slot->kind, h, copy, slot->byteCount );
|
||||
uint64_t h = b3Hash64NonZero( slot->bytes, slot->byteCount );
|
||||
uint32_t id = b3AppendGeometry( reg, slot->kind, h, slot->bytes, slot->byteCount );
|
||||
// Seeding in order without dedup keeps id == slot index.
|
||||
B3_ASSERT( id == (uint32_t)i );
|
||||
(void)id;
|
||||
|
|
@ -2945,7 +2862,7 @@ void b3DestroyPlayer( b3RecPlayer* player )
|
|||
b3DestroyWorld( player->rdr.replayWorldId );
|
||||
}
|
||||
|
||||
// Free live geometry after destroying the world (slot->live may be used by the world).
|
||||
// Free shape data.
|
||||
b3RecFreeSlots( player->rdr.slots, player->rdr.slotCount );
|
||||
|
||||
// Free reader scratch.
|
||||
|
|
|
|||
|
|
@ -63,13 +63,12 @@ typedef struct b3RecDrawQuery
|
|||
} b3RecDrawQuery;
|
||||
|
||||
// One slot in the preloaded geometry registry. Loaded from the trailing block before any
|
||||
// ops run; live pointer built lazily on first shape create that references the id.
|
||||
// ops run.
|
||||
typedef struct b3RegistrySlot
|
||||
{
|
||||
b3GeometryKind kind;
|
||||
int byteCount;
|
||||
uint8_t* bytes; // raw bytes from the file (always freed at teardown)
|
||||
void* live; // reconstructed live object, freed after b3DestroyWorld
|
||||
} b3RegistrySlot;
|
||||
|
||||
// This is used to simplify the scratch buffer lifetime. Names longer than this probably
|
||||
|
|
@ -259,6 +258,3 @@ void b3RecEnsureHits( b3RecReader* rdr, int n );
|
|||
|
||||
// Get a geometry slot with kind validation.
|
||||
b3RegistrySlot* b3RecGetSlot( b3RecReader* rdr, uint32_t id, b3GeometryKind kind );
|
||||
|
||||
// Get the recorded compound with the pointers fixed up.
|
||||
const b3CompoundData* b3RecGetLiveCompound( b3RegistrySlot* slot );
|
||||
|
|
|
|||
|
|
@ -1567,6 +1567,14 @@ const b3HeightFieldData* b3Shape_GetHeightField( b3ShapeId shapeId )
|
|||
return shape->heightField;
|
||||
}
|
||||
|
||||
const b3CompoundData* b3Shape_GetCompound( b3ShapeId shapeId )
|
||||
{
|
||||
b3World* world = b3GetWorld( shapeId.world0 );
|
||||
b3Shape* shape = b3GetShape( world, shapeId );
|
||||
B3_ASSERT( shape->type == b3_compoundShape );
|
||||
return shape->compound;
|
||||
}
|
||||
|
||||
void b3Shape_SetSphere( b3ShapeId shapeId, const b3Sphere* sphere )
|
||||
{
|
||||
b3World* world = b3GetUnlockedWorld( shapeId.world0 );
|
||||
|
|
|
|||
|
|
@ -65,6 +65,26 @@ static uint32_t b3ComputeLayoutHash( void )
|
|||
MIX( sizeof( b3DynamicTree ) )
|
||||
MIX( sizeof( b3TreeNode ) )
|
||||
MIX( sizeof( b3TreeProxy ) )
|
||||
MIX( sizeof( b3HullData ) )
|
||||
MIX( sizeof( b3HullVertex ) )
|
||||
MIX( sizeof( b3HullHalfEdge ) )
|
||||
MIX( sizeof( b3HullFace ) )
|
||||
MIX( sizeof( b3Plane ) )
|
||||
MIX( sizeof( b3MeshData ) )
|
||||
MIX( sizeof( b3MeshNode ) )
|
||||
MIX( sizeof( b3MeshTriangle ) )
|
||||
MIX( sizeof( b3HeightFieldData ) )
|
||||
MIX( sizeof( b3CompoundData ) )
|
||||
MIX( sizeof( b3CompoundCapsule ) )
|
||||
MIX( sizeof( b3CompoundSphere ) )
|
||||
MIX( B3_HULL_VERSION & 0xFFFFFFFFu )
|
||||
MIX( B3_HULL_VERSION >> 32 )
|
||||
MIX( B3_MESH_VERSION & 0xFFFFFFFFu )
|
||||
MIX( B3_MESH_VERSION >> 32 )
|
||||
MIX( B3_HEIGHT_FIELD_VERSION & 0xFFFFFFFFu )
|
||||
MIX( B3_HEIGHT_FIELD_VERSION >> 32 )
|
||||
MIX( B3_COMPOUND_VERSION & 0xFFFFFFFFu )
|
||||
MIX( B3_COMPOUND_VERSION >> 32 )
|
||||
MIX( sizeof( b3SetItem ) )
|
||||
MIX( sizeof( b3IdPool ) )
|
||||
MIX( sizeof( b3SurfaceMaterial ) )
|
||||
|
|
@ -780,11 +800,12 @@ static void b3DesShapes( b3SnapReader* r, b3World* world, b3RecReader* rdr )
|
|||
break;
|
||||
}
|
||||
b3RegistrySlot* slot = b3RecGetSlot( rdr, gid, b3_geometryCompound );
|
||||
dst->compound = slot != NULL ? b3RecGetLiveCompound( slot ) : NULL;
|
||||
if ( dst->compound == NULL )
|
||||
if ( slot == NULL )
|
||||
{
|
||||
r->ok = false;
|
||||
break;
|
||||
}
|
||||
dst->compound = (const b3CompoundData*)slot->bytes;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
|
|
|
|||
Loading…
Reference in a new issue