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
|
|
@ -235,6 +235,7 @@ int main( int argc, char** argv )
|
|||
{ "trees100", NULL, CreateTrees100, DestroyTrees, NULL, 500 },
|
||||
{ "trees50", NULL, CreateTrees50, DestroyTrees, NULL, 500 },
|
||||
{ "trees25", NULL, CreateTrees25, DestroyTrees, NULL, 500 },
|
||||
{ "village", GetVillageCapacity, CreateVillage, DestroyVillage, StepVillage, VILLAGE_STEP_COUNT },
|
||||
{ "washer", GetWasherCapacity, CreateWasher, NULL, NULL, 1000 },
|
||||
//{ "smash", CreateSmash, NULL, 300 },
|
||||
//{ "tumbler", CreateTumbler, NULL, 750 },
|
||||
|
|
|
|||
|
|
@ -13,10 +13,9 @@ not a runtime mutation primitive. The intended workflow is:
|
|||
|
||||
1. Author geometry offline (level mesh, terrain tile, building shell).
|
||||
2. Bake the compound once with `b3CreateCompound`.
|
||||
3. Serialize it to a byte buffer with `b3ConvertCompoundToBytes` and store it on
|
||||
disk or in a streaming cache.
|
||||
4. At runtime, load the bytes and reconstruct the compound with
|
||||
`b3ConvertBytesToCompound`, then attach it to a static body.
|
||||
3. Write the compound bytes to disk or a streaming cache.
|
||||
4. At runtime, load the bytes and check them with `b3ValidateCompound`, then
|
||||
attach the compound to a static body.
|
||||
|
||||
This makes compounds well-suited to open-world streaming: tiles are baked
|
||||
offline, kept in memory as flat byte buffers (which the engine uses directly
|
||||
|
|
@ -62,37 +61,41 @@ shared material table holds at most 64K unique materials.
|
|||
|
||||
```c
|
||||
typedef struct b3Compound {
|
||||
uint64_t version; // versioned against the tree/mesh/hull formats
|
||||
uint64_t version; // versioned against the mesh/hull formats
|
||||
int byteCount; // total size when serialized
|
||||
// internal tree, child arrays, material table
|
||||
// tree offsets and counts, child arrays, material table
|
||||
// ... (treat as opaque)
|
||||
} b3Compound;
|
||||
```
|
||||
|
||||
`version` is a compile-time constant (`B3_COMPOUND_VERSION`) that incorporates
|
||||
the dynamic tree, mesh, and hull format versions. A version mismatch at load
|
||||
time means the bytes were baked with a different engine version and cannot be
|
||||
used.
|
||||
the mesh and hull format versions. A version mismatch at load time means the
|
||||
bytes were baked with a different engine version and cannot be used.
|
||||
|
||||
## Serialization
|
||||
|
||||
Convert a live compound to a self-contained byte buffer:
|
||||
A compound is a single flat blob that contains no pointers. Everything inside it
|
||||
is addressed by byte offset from the start of the struct, so the bytes are
|
||||
position independent. Write `compound->byteCount` bytes starting at the compound
|
||||
address to disk. A plain `memcpy`, file read, or memory map of those bytes is a
|
||||
valid compound, and no fixup step is needed.
|
||||
|
||||
Validate bytes at runtime before using them (zero-copy; bytes must remain in
|
||||
scope and must not be modified while a shape uses them):
|
||||
|
||||
```c
|
||||
uint8_t* bytes = b3ConvertCompoundToBytes(compound);
|
||||
// compound is now in an unusable state; its pointers have been nullified.
|
||||
// Write bytes[0 .. compound->byteCount - 1] to disk.
|
||||
const b3CompoundData* compound = b3ValidateCompound(bytes, byteCount);
|
||||
if (compound == NULL)
|
||||
{
|
||||
// wrong version, wrong size, or corrupt offsets
|
||||
}
|
||||
```
|
||||
|
||||
Reconstruct from bytes at runtime (zero-copy; bytes must remain in scope):
|
||||
|
||||
```c
|
||||
b3Compound* compound = b3ConvertBytesToCompound(bytes, byteCount);
|
||||
```
|
||||
|
||||
The bytes are mutated in place to fixup internal pointers. Multiple static
|
||||
bodies can use the same byte buffer simultaneously (instancing), since the
|
||||
compound itself holds no per-body state.
|
||||
`b3ValidateCompound` never modifies the bytes. It returns `NULL` unless the
|
||||
version matches, `byteCount` equals the size stored in the compound, and the tree
|
||||
node array lies fully inside the buffer. On success it returns the
|
||||
input address. Multiple static bodies can use the same byte buffer
|
||||
simultaneously (instancing), since the compound itself holds no per-body state.
|
||||
|
||||
## Attaching to a Static Body
|
||||
|
||||
|
|
@ -151,5 +154,5 @@ you manage the buffer lifetime yourself):
|
|||
b3DestroyCompound(compound);
|
||||
```
|
||||
|
||||
Do not call `b3DestroyCompound` on a compound reconstructed from bytes with
|
||||
`b3ConvertBytesToCompound` — free the byte buffer instead.
|
||||
Do not call `b3DestroyCompound` on a compound validated from bytes with
|
||||
`b3ValidateCompound` — free the byte buffer instead.
|
||||
|
|
|
|||
|
|
@ -1001,6 +1001,9 @@ B3_API b3Mesh b3Shape_GetMesh( b3ShapeId shapeId );
|
|||
/// Get the shape's height field. Asserts the type is correct.
|
||||
B3_API const b3HeightFieldData* b3Shape_GetHeightField( b3ShapeId shapeId );
|
||||
|
||||
/// Get the shape's compound. Asserts the type is correct.
|
||||
B3_API const b3CompoundData* b3Shape_GetCompound( b3ShapeId shapeId );
|
||||
|
||||
/// Allows you to change a shape to be a sphere or update the current sphere.
|
||||
/// This does not modify the mass properties.
|
||||
/// @see b3Body_ApplyMassFromShapes
|
||||
|
|
|
|||
|
|
@ -468,13 +468,8 @@ B3_API b3CompoundData* b3CreateCompound( const b3CompoundDef* def );
|
|||
/// Destroy a compound shape.
|
||||
B3_API void b3DestroyCompound( b3CompoundData* compound );
|
||||
|
||||
/// Cast the provided compound data to bytes, setting the internal pointers to null.
|
||||
/// Use this before serializing the compound bytes.
|
||||
B3_API uint8_t* b3ConvertCompoundToBytes( b3CompoundData* compound );
|
||||
|
||||
/// Cast the provided bytes to compound data, setting up internal pointers.
|
||||
/// Use this after de-serializing the compound bytes.
|
||||
B3_API b3CompoundData* b3ConvertBytesToCompound( uint8_t* bytes, int byteCount );
|
||||
// Cast serialized bytes into a compound and validate. Returns null if invalid.
|
||||
B3_API const b3CompoundData* b3ValidateCompound( const uint8_t* bytes, int byteCount );
|
||||
|
||||
/**@}*/ // compound
|
||||
|
||||
|
|
|
|||
|
|
@ -2487,8 +2487,8 @@ typedef struct b3CompoundDef
|
|||
int sphereCount;
|
||||
} b3CompoundDef;
|
||||
|
||||
/// The baked compound version depends on the tree, mesh, and hull versions.
|
||||
#define B3_COMPOUND_VERSION ( 0x7A2F41C9E6D0B358ull ^ B3_DYNAMIC_TREE_VERSION ^ B3_MESH_VERSION ^ B3_HULL_VERSION )
|
||||
/// The baked compound version depends on the mesh and hull versions.
|
||||
#define B3_COMPOUND_VERSION ( 0x9E4B17D3A25C68F1ull ^ B3_MESH_VERSION ^ B3_HULL_VERSION )
|
||||
|
||||
/// The data for a baked compound shape. This is a potentially large yet highly optimized
|
||||
/// data structure. It can contain thousands of child shapes, yet at runtime it populates
|
||||
|
|
@ -2505,15 +2505,17 @@ typedef struct b3CompoundData
|
|||
/// The total number of bytes for this compound.
|
||||
int byteCount;
|
||||
|
||||
// Bounds of the tree.
|
||||
b3AABB bounds;
|
||||
|
||||
// Tree height for diagnostics.
|
||||
int treeHeight;
|
||||
|
||||
/// Offset of the tree node array in bytes from the struct address.
|
||||
int nodeOffset;
|
||||
|
||||
/// Offset of the tree proxy array in bytes from the struct address.
|
||||
int proxyOffset;
|
||||
|
||||
/// Immutable dynamic tree. The node and proxy pointers must be fixed up using the offsets
|
||||
/// above. A baked tree is never inserted into, so the parent array stays null.
|
||||
b3DynamicTree tree;
|
||||
// The number of tree nodes.
|
||||
int nodeCount;
|
||||
|
||||
/// Offset of the material array in bytes from the struct address.
|
||||
int materialOffset;
|
||||
|
|
|
|||
|
|
@ -793,11 +793,10 @@ typedef struct
|
|||
|
||||
// The compound tree stores child indices. Remap to the pool slot the flatten
|
||||
// pass assigned, then append the child under the body transform.
|
||||
static bool CompoundCullCallback( int proxyId, uint64_t userData, void* context )
|
||||
static bool CompoundCullCallback( const b3CompoundData* compound, int childIndex, void* context )
|
||||
{
|
||||
(void)proxyId;
|
||||
(void)compound;
|
||||
CompoundCullContext* ctx = (CompoundCullContext*)context;
|
||||
const int childIndex = (int)userData;
|
||||
if ( childIndex < 0 || childIndex >= ctx->parent->compound.childMapCount )
|
||||
{
|
||||
return true;
|
||||
|
|
@ -888,7 +887,7 @@ static void DrawShape( void* userShape, b3WorldTransform shapeTransform, b3HexCo
|
|||
|
||||
// If the whole compound sits inside the view the query returns every child,
|
||||
// so walking the list is cheaper. Both boxes are in the draw origin frame.
|
||||
b3AABB rootRel = b3AABB_Transform( shapeRelative, b3DynamicTree_GetRootBounds( &us->compound.data->tree ) );
|
||||
b3AABB rootRel = b3AABB_Transform( shapeRelative, us->compound.data->bounds );
|
||||
if ( b3AABB_Contains( viewRel, rootRel ) )
|
||||
{
|
||||
cull = false;
|
||||
|
|
@ -901,7 +900,7 @@ static void DrawShape( void* userShape, b3WorldTransform shapeTransform, b3HexCo
|
|||
b3AABB localCull = b3AABB_Transform( b3InvertTransform( shapeRelative ), viewRel );
|
||||
|
||||
CompoundCullContext ctx = { us, shapeRelative, c, metallic, roughness, shadowCast, hk, 0 };
|
||||
b3DynamicTree_Query( &us->compound.data->tree, localCull, ~0ull, false, CompoundCullCallback, &ctx );
|
||||
b3QueryCompound( us->compound.data, localCull, CompoundCullCallback, &ctx );
|
||||
|
||||
s_adapter.lastCompoundAppended += ctx.appended;
|
||||
s_adapter.lastCompoundTotal += us->compound.childMapCount;
|
||||
|
|
|
|||
|
|
@ -763,6 +763,93 @@ public:
|
|||
|
||||
static int sampleMeshDropBenchmark = RegisterSample( "Benchmark", "Mesh Drop", BenchmarkMeshDrop::Create );
|
||||
|
||||
class BenchmarkVillage : public Sample
|
||||
{
|
||||
public:
|
||||
explicit BenchmarkVillage( SampleContext* context )
|
||||
: Sample( context )
|
||||
{
|
||||
if ( context->restart == false )
|
||||
{
|
||||
m_camera->SetView( 45.0f, 30.0f, 300.0f, b3Pos_zero );
|
||||
}
|
||||
|
||||
b3Capacity capacity = {};
|
||||
GetVillageCapacity( &capacity );
|
||||
CreateWorld( &capacity );
|
||||
|
||||
CreateVillage( m_worldId );
|
||||
}
|
||||
|
||||
~BenchmarkVillage() override
|
||||
{
|
||||
DestroyVillage();
|
||||
}
|
||||
|
||||
void Step() override
|
||||
{
|
||||
StepVillage( m_worldId, m_stepCount );
|
||||
Sample::Step();
|
||||
}
|
||||
|
||||
bool DrawControls() override
|
||||
{
|
||||
ImGui::Checkbox( "Draw Casts", &m_drawCasts );
|
||||
ImGui::Checkbox( "Draw Spheres", &m_drawSpheres );
|
||||
return true;
|
||||
}
|
||||
|
||||
void Render() override
|
||||
{
|
||||
Sample::Render();
|
||||
|
||||
VillageCurtain curtain = GetVillageCurtain();
|
||||
|
||||
int hitCount = 0;
|
||||
for ( int i = 0; i < curtain.castCount; ++i )
|
||||
{
|
||||
const VillageCast& cast = curtain.casts[i];
|
||||
hitCount += cast.hit ? 1 : 0;
|
||||
|
||||
if ( m_drawCasts == false )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
b3Pos end = cast.origin + cast.fraction * curtain.translation;
|
||||
DrawLine( cast.origin, end, MakeColor( cast.hit ? b3_colorYellow : b3_colorGray ) );
|
||||
|
||||
if ( cast.hit == false )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
DrawPoint( cast.point, 4.0f, MakeColor( b3_colorGreen ) );
|
||||
DrawLine( cast.point, cast.point + 0.5f * cast.normal, MakeColor( b3_colorGreen ) );
|
||||
|
||||
if ( m_drawSpheres )
|
||||
{
|
||||
b3WorldTransform transform = b3WorldTransform_identity;
|
||||
transform.p = end;
|
||||
DrawSphereEx( transform, curtain.radius, MakeColorAlpha( b3_colorPurple, 0.5f ), 0.0f, 0.5f,
|
||||
TRANSPARENT_SHADOW_NONE );
|
||||
}
|
||||
}
|
||||
|
||||
DrawTextLine( "casts = %d, hits = %d", curtain.castCount, hitCount );
|
||||
}
|
||||
|
||||
static Sample* Create( SampleContext* context )
|
||||
{
|
||||
return new BenchmarkVillage( context );
|
||||
}
|
||||
|
||||
bool m_drawCasts = true;
|
||||
bool m_drawSpheres = true;
|
||||
};
|
||||
|
||||
static int sampleVillageBenchmark = RegisterSample( "Benchmark", "Village", BenchmarkVillage::Create );
|
||||
|
||||
struct ShapeUserData
|
||||
{
|
||||
int row;
|
||||
|
|
|
|||
|
|
@ -93,7 +93,7 @@ public:
|
|||
b3Transform transform = { { 0.0f, 0.01f, 0.0f }, b3Quat_identity };
|
||||
DrawAxes( b3MakeWorldTransform( transform ), 1.0f );
|
||||
|
||||
int height = b3DynamicTree_GetHeight( &m_compound->tree );
|
||||
int height = m_compound->treeHeight;
|
||||
DrawTextLine( "compound tree height = %d", height );
|
||||
}
|
||||
|
||||
|
|
@ -153,7 +153,7 @@ public:
|
|||
b3Transform transform = { { 0.0f, 0.01f, 0.0f }, b3Quat_identity };
|
||||
DrawAxes( b3MakeWorldTransform( transform ), 1.0f );
|
||||
|
||||
int height = b3DynamicTree_GetHeight( &m_compound->tree );
|
||||
int height = m_compound->treeHeight;
|
||||
DrawTextLine( "compound tree height = %d", height );
|
||||
}
|
||||
|
||||
|
|
@ -227,7 +227,7 @@ public:
|
|||
b3Transform transform = { { 0.0f, 0.01f, 0.0f }, b3Quat_identity };
|
||||
DrawAxes( b3MakeWorldTransform( transform ), 1.0f );
|
||||
|
||||
int height = b3DynamicTree_GetHeight( &m_compound->tree );
|
||||
int height = m_compound->treeHeight;
|
||||
DrawTextLine( "compound tree height = %d", height );
|
||||
}
|
||||
|
||||
|
|
@ -345,8 +345,8 @@ public:
|
|||
DrawTextLine( "compound hull count = %d, mesh count = %d", m_compound->hullCount, m_compound->meshCount );
|
||||
DrawTextLine( "compound byte count = %d", m_compound->byteCount );
|
||||
|
||||
int treeBytes = b3DynamicTree_GetByteCount( &m_compound->tree );
|
||||
int height = b3DynamicTree_GetHeight( &m_compound->tree );
|
||||
int treeBytes = m_compound->nodeCount * (int)sizeof( b3TreeNode );
|
||||
int height = m_compound->treeHeight;
|
||||
DrawTextLine( "compound tree byte count = %d, height = %d", treeBytes, height );
|
||||
}
|
||||
|
||||
|
|
@ -461,8 +461,8 @@ public:
|
|||
|
||||
DrawTextLine( "compound instance count = %d, byte count = %d", m_compound->meshCount, m_compound->byteCount );
|
||||
|
||||
int treeBytes = b3DynamicTree_GetByteCount( &m_compound->tree );
|
||||
int height = b3DynamicTree_GetHeight( &m_compound->tree );
|
||||
int treeBytes = m_compound->nodeCount * (int)sizeof( b3TreeNode );
|
||||
int height = m_compound->treeHeight;
|
||||
DrawTextLine( "compound tree byte count = %d, height = %d", treeBytes, height );
|
||||
}
|
||||
|
||||
|
|
@ -700,8 +700,8 @@ public:
|
|||
m_compound->hullCount, m_compound->meshCount, m_compound->sphereCount );
|
||||
DrawTextLine( "compound byte count = %d", m_compound->byteCount );
|
||||
|
||||
int treeBytes = b3DynamicTree_GetByteCount( &m_compound->tree );
|
||||
int height = b3DynamicTree_GetHeight( &m_compound->tree );
|
||||
int treeBytes = m_compound->nodeCount * (int)sizeof( b3TreeNode );
|
||||
int height = m_compound->treeHeight;
|
||||
DrawTextLine( "compound tree byte count = %d, height = %d", treeBytes, height );
|
||||
|
||||
int total = 0;
|
||||
|
|
|
|||
|
|
@ -1538,6 +1538,20 @@ public:
|
|||
b3AABB aabb = b3Shape_GetAABB( shape );
|
||||
ImGui::Text( "aabb (%.2f, %.2f, %.2f)", aabb.lowerBound.x, aabb.lowerBound.y, aabb.lowerBound.z );
|
||||
ImGui::Text( " (%.2f, %.2f, %.2f)", aabb.upperBound.x, aabb.upperBound.y, aabb.upperBound.z );
|
||||
|
||||
if ( b3Shape_GetType( shape ) == b3_compoundShape )
|
||||
{
|
||||
const b3CompoundData* compound = b3Shape_GetCompound( shape );
|
||||
int childCount = compound->capsuleCount + compound->hullCount + compound->meshCount + compound->sphereCount;
|
||||
ImGui::Text( "children %d", childCount );
|
||||
ImGui::Text( " capsules %d", compound->capsuleCount );
|
||||
ImGui::Text( " hulls %d (%d unique)", compound->hullCount, compound->sharedHullCount );
|
||||
ImGui::Text( " meshes %d (%d unique)", compound->meshCount, compound->sharedMeshCount );
|
||||
ImGui::Text( " spheres %d", compound->sphereCount );
|
||||
ImGui::Text( "materials %d", compound->materialCount );
|
||||
ImGui::Text( "tree height %d nodes %d", compound->treeHeight, compound->nodeCount );
|
||||
ImGui::Text( "bytes %d", compound->byteCount );
|
||||
}
|
||||
}
|
||||
|
||||
void DrawContactDetail( b3BodyId body )
|
||||
|
|
|
|||
|
|
@ -1340,3 +1340,276 @@ void StepSleep( b3WorldId worldId, int stepCount )
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
#define VILLAGE_TILE_WIDTH 4
|
||||
#define VILLAGE_CAST_RADIUS 0.05f
|
||||
#define VILLAGE_CAST_HEIGHT 10.0f
|
||||
#define VILLAGE_CAST_LENGTH 12.0f
|
||||
|
||||
#if BENCHMARK_DEBUG
|
||||
#define VILLAGE_GRID 10
|
||||
#define VILLAGE_BODY_GRID 10
|
||||
#else
|
||||
#define VILLAGE_GRID 100
|
||||
#define VILLAGE_BODY_GRID 70
|
||||
#endif
|
||||
|
||||
// One cast per meter across the full span
|
||||
#define VILLAGE_MAX_CASTS ( VILLAGE_GRID * VILLAGE_TILE_WIDTH + 1 )
|
||||
|
||||
typedef struct VillageData
|
||||
{
|
||||
b3CompoundData* compound;
|
||||
b3Quat rotation;
|
||||
float halfSpan;
|
||||
b3Vec3 castTranslation;
|
||||
int castCount;
|
||||
VillageCast casts[VILLAGE_MAX_CASTS];
|
||||
} VillageData;
|
||||
|
||||
static VillageData g_villageData;
|
||||
|
||||
void GetVillageCapacity( b3Capacity* capacity )
|
||||
{
|
||||
int bodyCount = VILLAGE_BODY_GRID * VILLAGE_BODY_GRID;
|
||||
capacity->staticShapeCount = 1;
|
||||
capacity->staticBodyCount = 1;
|
||||
capacity->dynamicShapeCount = bodyCount;
|
||||
capacity->dynamicBodyCount = bodyCount;
|
||||
capacity->contactCount = 4 * bodyCount;
|
||||
}
|
||||
|
||||
void CreateVillage( b3WorldId worldId )
|
||||
{
|
||||
memset( &g_villageData, 0, sizeof( g_villageData ) );
|
||||
g_randomSeed = 2718281828;
|
||||
|
||||
int gridCount = VILLAGE_GRID;
|
||||
float tileWidth = VILLAGE_TILE_WIDTH;
|
||||
float halfSpan = 0.5f * tileWidth * gridCount;
|
||||
g_villageData.halfSpan = halfSpan;
|
||||
|
||||
{
|
||||
int tileCount = gridCount * gridCount;
|
||||
int propCapacity = tileCount / 8 + 1;
|
||||
|
||||
b3CompoundHullDef* hulls = malloc( tileCount * sizeof( b3CompoundHullDef ) );
|
||||
b3CompoundMeshDef* meshes = malloc( tileCount * sizeof( b3CompoundMeshDef ) );
|
||||
b3CompoundCapsuleDef* capsules = malloc( propCapacity * sizeof( b3CompoundCapsuleDef ) );
|
||||
b3CompoundSphereDef* spheres = malloc( propCapacity * sizeof( b3CompoundSphereDef ) );
|
||||
|
||||
b3SurfaceMaterial material = b3DefaultSurfaceMaterial();
|
||||
b3BoxHull tile = b3MakeBoxHull( 0.5f * tileWidth, 0.5f, 0.5f * tileWidth );
|
||||
|
||||
// A wave patch replaces a sparse set of tiles so the compound carries mesh children
|
||||
b3MeshData* wave = b3CreateWaveMesh( 8, 8, tileWidth / 8.0f, 0.25f, 0.25f, 0.25f );
|
||||
|
||||
int hullCount = 0;
|
||||
int meshCount = 0;
|
||||
int capsuleCount = 0;
|
||||
int sphereCount = 0;
|
||||
|
||||
for ( int i = 0; i < gridCount; ++i )
|
||||
{
|
||||
float x = -halfSpan + ( i + 0.5f ) * tileWidth;
|
||||
|
||||
for ( int j = 0; j < gridCount; ++j )
|
||||
{
|
||||
float z = -halfSpan + ( j + 0.5f ) * tileWidth;
|
||||
|
||||
if ( i % 16 == 8 && j % 16 == 8 )
|
||||
{
|
||||
meshes[meshCount] = (b3CompoundMeshDef){
|
||||
.meshData = wave,
|
||||
.transform = { { x, 0.0f, z }, b3Quat_identity },
|
||||
.scale = b3Vec3_one,
|
||||
.materials = &material,
|
||||
.materialCount = 1,
|
||||
};
|
||||
meshCount += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
hulls[hullCount] = (b3CompoundHullDef){
|
||||
.hull = &tile.base,
|
||||
.transform = { { x, -0.5f, z }, b3Quat_identity },
|
||||
.material = material,
|
||||
};
|
||||
hullCount += 1;
|
||||
|
||||
int tileIndex = i * gridCount + j;
|
||||
if ( tileIndex % 8 != 0 )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( ( tileIndex / 8 ) & 1 )
|
||||
{
|
||||
float radius = 0.25f;
|
||||
capsules[capsuleCount] = (b3CompoundCapsuleDef){
|
||||
.capsule = { { x - 1.0f, radius, z }, { x + 1.0f, radius, z }, radius },
|
||||
.material = material,
|
||||
};
|
||||
capsuleCount += 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
float radius = 0.5f;
|
||||
spheres[sphereCount] = (b3CompoundSphereDef){
|
||||
.sphere = { { x, radius, z }, radius },
|
||||
.material = material,
|
||||
};
|
||||
sphereCount += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
b3CompoundDef def = {
|
||||
.capsules = capsules,
|
||||
.capsuleCount = capsuleCount,
|
||||
.hulls = hulls,
|
||||
.hullCount = hullCount,
|
||||
.meshes = meshes,
|
||||
.meshCount = meshCount,
|
||||
.spheres = spheres,
|
||||
.sphereCount = sphereCount,
|
||||
};
|
||||
|
||||
g_villageData.compound = b3CreateCompound( &def );
|
||||
|
||||
// The compound clones its inputs
|
||||
b3DestroyMesh( wave );
|
||||
free( hulls );
|
||||
free( meshes );
|
||||
free( capsules );
|
||||
free( spheres );
|
||||
|
||||
// Rotated so broad-phase lookups transform into the compound frame
|
||||
g_villageData.rotation = b3MakeQuatFromAxisAngle( (b3Vec3){ 0.0f, 1.0f, 0.0f }, -1.15f * B3_PI );
|
||||
|
||||
b3BodyDef bodyDef = b3DefaultBodyDef();
|
||||
bodyDef.rotation = g_villageData.rotation;
|
||||
b3BodyId groundId = b3CreateBody( worldId, &bodyDef );
|
||||
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
b3CreateBakedCompoundShape( groundId, &shapeDef, g_villageData.compound );
|
||||
}
|
||||
|
||||
{
|
||||
b3Sphere sphere = { { 0.0f, 0.0f, 0.0f }, 0.3f };
|
||||
b3Capsule capsule = { { -0.3f, 0.0f, 0.0f }, { 0.3f, 0.0f, 0.0f }, 0.2f };
|
||||
b3BoxHull box = b3MakeCubeHull( 0.3f );
|
||||
|
||||
b3BodyDef bodyDef = b3DefaultBodyDef();
|
||||
bodyDef.type = b3_dynamicBody;
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
|
||||
int bodyGrid = VILLAGE_BODY_GRID;
|
||||
float spacing = 1.6f;
|
||||
float offset = 0.5f * spacing * ( 1 - bodyGrid );
|
||||
|
||||
for ( int i = 0; i < bodyGrid; ++i )
|
||||
{
|
||||
for ( int j = 0; j < bodyGrid; ++j )
|
||||
{
|
||||
int index = i * bodyGrid + j;
|
||||
|
||||
bodyDef.position = (b3Pos){ offset + spacing * i, RandomFloatRange( 2.0f, 6.0f ), offset + spacing * j };
|
||||
bodyDef.linearVelocity = b3Vec3_zero;
|
||||
bodyDef.angularVelocity = RandomVec3Uniform( -2.0f, 2.0f );
|
||||
|
||||
// Fast enough to need continuous collision against the compound
|
||||
if ( index % 20 == 0 )
|
||||
{
|
||||
bodyDef.position.y = 20.0f;
|
||||
bodyDef.linearVelocity = (b3Vec3){ 0.0f, -80.0f, 0.0f };
|
||||
}
|
||||
|
||||
b3BodyId bodyId = b3CreateBody( worldId, &bodyDef );
|
||||
|
||||
switch ( index % 3 )
|
||||
{
|
||||
case 0:
|
||||
b3CreateSphereShape( bodyId, &shapeDef, &sphere );
|
||||
break;
|
||||
case 1:
|
||||
b3CreateCapsuleShape( bodyId, &shapeDef, &capsule );
|
||||
break;
|
||||
default:
|
||||
b3CreateHullShape( bodyId, &shapeDef, &box.base );
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static float VillageCastCallback( b3ShapeId shapeId, b3Pos point, b3Vec3 normal, float fraction, uint64_t userMaterialId,
|
||||
int triangleIndex, int childIndex, void* context )
|
||||
{
|
||||
(void)shapeId;
|
||||
(void)userMaterialId;
|
||||
(void)triangleIndex;
|
||||
(void)childIndex;
|
||||
|
||||
// Clipping makes each report closer than the last, so the final one is the closest
|
||||
VillageCast* cast = context;
|
||||
cast->point = point;
|
||||
cast->normal = normal;
|
||||
cast->fraction = fraction;
|
||||
cast->hit = true;
|
||||
return fraction;
|
||||
}
|
||||
|
||||
// A curtain of tilted sphere casts, one per meter, sweeps across the compound once per run.
|
||||
void StepVillage( b3WorldId worldId, int stepIndex )
|
||||
{
|
||||
float halfSpan = g_villageData.halfSpan;
|
||||
float t = (float)( stepIndex % VILLAGE_STEP_COUNT ) / (float)( VILLAGE_STEP_COUNT - 1 );
|
||||
float x = -halfSpan + 2.0f * halfSpan * t;
|
||||
|
||||
b3CosSin tilt = b3ComputeCosSin( ( 20.0f / 180.0f ) * B3_PI );
|
||||
b3Vec3 localTranslation = { VILLAGE_CAST_LENGTH * tilt.sine, -VILLAGE_CAST_LENGTH * tilt.cosine, 0.0f };
|
||||
|
||||
// The curtain is laid out in the compound frame
|
||||
b3Quat q = g_villageData.rotation;
|
||||
b3Vec3 translation = b3RotateVector( q, localTranslation );
|
||||
|
||||
b3Vec3 point = b3Vec3_zero;
|
||||
b3ShapeProxy proxy = { &point, 1, VILLAGE_CAST_RADIUS };
|
||||
b3QueryFilter filter = b3DefaultQueryFilter();
|
||||
|
||||
int castCount = (int)( 2.0f * halfSpan ) + 1;
|
||||
assert( castCount <= VILLAGE_MAX_CASTS );
|
||||
|
||||
for ( int i = 0; i < castCount; ++i )
|
||||
{
|
||||
b3Vec3 localOrigin = { x, VILLAGE_CAST_HEIGHT, -halfSpan + (float)i };
|
||||
|
||||
VillageCast* cast = g_villageData.casts + i;
|
||||
*cast = (VillageCast){ 0 };
|
||||
cast->origin = b3ToPos( b3RotateVector( q, localOrigin ) );
|
||||
cast->fraction = 1.0f;
|
||||
|
||||
b3World_CastShape( worldId, cast->origin, &proxy, translation, filter, VillageCastCallback, cast );
|
||||
}
|
||||
|
||||
g_villageData.castTranslation = translation;
|
||||
g_villageData.castCount = castCount;
|
||||
}
|
||||
|
||||
VillageCurtain GetVillageCurtain( void )
|
||||
{
|
||||
return (VillageCurtain){
|
||||
.casts = g_villageData.casts,
|
||||
.castCount = g_villageData.castCount,
|
||||
.translation = g_villageData.castTranslation,
|
||||
.radius = VILLAGE_CAST_RADIUS,
|
||||
};
|
||||
}
|
||||
|
||||
void DestroyVillage( void )
|
||||
{
|
||||
b3DestroyCompound( g_villageData.compound );
|
||||
memset( &g_villageData, 0, sizeof( g_villageData ) );
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
#pragma once
|
||||
|
||||
#include "box3d/id.h"
|
||||
#include "box3d/math_functions.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
|
|
@ -52,6 +53,32 @@ void GetSleepCapacity( b3Capacity* capacity );
|
|||
void CreateSleep( b3WorldId worldId );
|
||||
void StepSleep( b3WorldId worldId, int stepCount );
|
||||
|
||||
#define VILLAGE_STEP_COUNT 600
|
||||
|
||||
typedef struct VillageCast
|
||||
{
|
||||
b3Pos origin;
|
||||
b3Pos point;
|
||||
b3Vec3 normal;
|
||||
float fraction;
|
||||
bool hit;
|
||||
} VillageCast;
|
||||
|
||||
// The sphere casts from the latest village step, for drawing
|
||||
typedef struct VillageCurtain
|
||||
{
|
||||
const VillageCast* casts;
|
||||
int castCount;
|
||||
b3Vec3 translation;
|
||||
float radius;
|
||||
} VillageCurtain;
|
||||
|
||||
void GetVillageCapacity( b3Capacity* capacity );
|
||||
void CreateVillage( b3WorldId worldId );
|
||||
void StepVillage( b3WorldId worldId, int stepIndex );
|
||||
void DestroyVillage( void );
|
||||
VillageCurtain GetVillageCurtain( void );
|
||||
|
||||
// void CreateSmash( b3WorldId worldId );
|
||||
// void CreateTumbler( b3WorldId worldId );
|
||||
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -77,10 +77,9 @@ static int CompoundCreateMixed( void )
|
|||
ENSURE( compound->sharedHullCount == 1 );
|
||||
ENSURE( compound->sharedMeshCount == 1 );
|
||||
|
||||
ENSURE( compound->tree.nodeEnd >= 2 );
|
||||
ENSURE( compound->tree.proxyCount > 0 );
|
||||
ENSURE( compound->tree.nodes != NULL );
|
||||
ENSURE( compound->tree.proxies != NULL );
|
||||
ENSURE( compound->nodeCount >= 2 );
|
||||
ENSURE( compound->nodeOffset > 0 );
|
||||
ENSURE( compound->bounds.lowerBound.x < compound->bounds.upperBound.x );
|
||||
|
||||
b3DestroyCompound( compound );
|
||||
b3DestroyMesh( meshData );
|
||||
|
|
@ -883,7 +882,8 @@ static int CompoundMeshContactMaterial( void )
|
|||
ENSURE( compound != NULL );
|
||||
|
||||
b3ShapeDef groundShapeDef = b3DefaultShapeDef();
|
||||
b3CreateBakedCompoundShape( groundBodyId, &groundShapeDef, compound );
|
||||
b3ShapeId compoundShapeId = b3CreateBakedCompoundShape( groundBodyId, &groundShapeDef, compound );
|
||||
ENSURE( b3Shape_GetCompound( compoundShapeId ) == compound );
|
||||
|
||||
b3BodyDef sphereBodyDef = b3DefaultBodyDef();
|
||||
sphereBodyDef.type = b3_dynamicBody;
|
||||
|
|
@ -1233,18 +1233,24 @@ static int CompoundSerializeRoundtrip( void )
|
|||
uint8_t* buffer = (uint8_t*)malloc( (size_t)byteCount );
|
||||
ENSURE( buffer != NULL );
|
||||
|
||||
uint8_t* serialized = b3ConvertCompoundToBytes( a );
|
||||
memcpy( buffer, serialized, (size_t)byteCount );
|
||||
uint8_t* reference = (uint8_t*)malloc( (size_t)byteCount );
|
||||
ENSURE( reference != NULL );
|
||||
|
||||
memcpy( buffer, a, (size_t)byteCount );
|
||||
memcpy( reference, a, (size_t)byteCount );
|
||||
b3DestroyCompound( a );
|
||||
|
||||
b3CompoundData* b = b3ConvertBytesToCompound( buffer, byteCount );
|
||||
const b3CompoundData* b = b3ValidateCompound( buffer, byteCount );
|
||||
ENSURE( b != NULL );
|
||||
ENSURE( (const uint8_t*)b == buffer );
|
||||
ENSURE( b->byteCount == byteCount );
|
||||
ENSURE( b->capsuleCount == 1 );
|
||||
ENSURE( b->hullCount == 1 );
|
||||
ENSURE( b->meshCount == 1 );
|
||||
ENSURE( b->sphereCount == 1 );
|
||||
ENSURE( b->tree.nodes != NULL );
|
||||
|
||||
// Validation must not touch the bytes
|
||||
ENSURE( memcmp( buffer, reference, (size_t)byteCount ) == 0 );
|
||||
|
||||
// Bounds and ray cast on the deserialized compound match the original.
|
||||
b3AABB aabbB = b3ComputeCompoundAABB( b, b3Transform_identity );
|
||||
|
|
@ -1256,6 +1262,7 @@ static int CompoundSerializeRoundtrip( void )
|
|||
ENSURE_SMALL( rayB.fraction - rayA.fraction, 1e-5f );
|
||||
ENSURE( rayB.childIndex == rayA.childIndex );
|
||||
|
||||
free( reference );
|
||||
free( buffer );
|
||||
b3DestroyMesh( md );
|
||||
return 0;
|
||||
|
|
@ -1268,13 +1275,13 @@ static int CompoundSerializeBadVersion( void )
|
|||
|
||||
int byteCount = a->byteCount;
|
||||
uint8_t* buffer = (uint8_t*)malloc( (size_t)byteCount );
|
||||
memcpy( buffer, b3ConvertCompoundToBytes( a ), (size_t)byteCount );
|
||||
memcpy( buffer, a, (size_t)byteCount );
|
||||
b3DestroyCompound( a );
|
||||
|
||||
// Corrupt the version word (first 8 bytes of b3Compound).
|
||||
( (b3CompoundData*)buffer )->version ^= 0x1ull;
|
||||
|
||||
b3CompoundData* b = b3ConvertBytesToCompound( buffer, byteCount );
|
||||
const b3CompoundData* b = b3ValidateCompound( buffer, byteCount );
|
||||
ENSURE( b == NULL );
|
||||
|
||||
free( buffer );
|
||||
|
|
@ -1289,11 +1296,11 @@ static int CompoundSerializeWrongByteCount( void )
|
|||
|
||||
int byteCount = a->byteCount;
|
||||
uint8_t* buffer = (uint8_t*)malloc( (size_t)byteCount );
|
||||
memcpy( buffer, b3ConvertCompoundToBytes( a ), (size_t)byteCount );
|
||||
memcpy( buffer, a, (size_t)byteCount );
|
||||
b3DestroyCompound( a );
|
||||
|
||||
// Off-by-one in the declared length is rejected.
|
||||
b3CompoundData* b = b3ConvertBytesToCompound( buffer, byteCount + 1 );
|
||||
const b3CompoundData* b = b3ValidateCompound( buffer, byteCount + 1 );
|
||||
ENSURE( b == NULL );
|
||||
|
||||
free( buffer );
|
||||
|
|
@ -1301,6 +1308,35 @@ static int CompoundSerializeWrongByteCount( void )
|
|||
return 0;
|
||||
}
|
||||
|
||||
static int CompoundSerializeBadNodeCount( void )
|
||||
{
|
||||
b3MeshData* md = b3CreateBoxMesh( b3Vec3_zero, (b3Vec3){ 0.5f, 0.5f, 0.5f }, false );
|
||||
b3CompoundData* a = BuildSerializableCompound( md );
|
||||
|
||||
int byteCount = a->byteCount;
|
||||
uint8_t* buffer = (uint8_t*)malloc( (size_t)byteCount );
|
||||
memcpy( buffer, a, (size_t)byteCount );
|
||||
b3DestroyCompound( a );
|
||||
|
||||
b3CompoundData* header = (b3CompoundData*)buffer;
|
||||
int nodeCount = header->nodeCount;
|
||||
|
||||
// Node array runs off the end of the buffer
|
||||
header->nodeCount = byteCount;
|
||||
ENSURE( b3ValidateCompound( buffer, byteCount ) == NULL );
|
||||
|
||||
// Overflow in 32 bit arithmetic must still be caught
|
||||
header->nodeCount = INT32_MAX;
|
||||
ENSURE( b3ValidateCompound( buffer, byteCount ) == NULL );
|
||||
|
||||
header->nodeCount = nodeCount;
|
||||
ENSURE( b3ValidateCompound( buffer, byteCount ) != NULL );
|
||||
|
||||
free( buffer );
|
||||
b3DestroyMesh( md );
|
||||
return 0;
|
||||
}
|
||||
|
||||
int CompoundTest( void )
|
||||
{
|
||||
RUN_SUBTEST( CompoundCreateMixed );
|
||||
|
|
@ -1344,6 +1380,7 @@ int CompoundTest( void )
|
|||
RUN_SUBTEST( CompoundSerializeRoundtrip );
|
||||
RUN_SUBTEST( CompoundSerializeBadVersion );
|
||||
RUN_SUBTEST( CompoundSerializeWrongByteCount );
|
||||
RUN_SUBTEST( CompoundSerializeBadNodeCount );
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -625,8 +625,7 @@ static int TreeQueryClosest( void )
|
|||
return 0;
|
||||
}
|
||||
|
||||
// A saved tree carries only its live node range. A baked compound tree has no parent array, so
|
||||
// saving one has to bail out rather than write from a null pointer.
|
||||
// A saved tree carries only its live node range.
|
||||
static int TreeSaveLoadRoundtrip( void )
|
||||
{
|
||||
s_seed = 4242;
|
||||
|
|
@ -667,34 +666,6 @@ static int TreeSaveLoadRoundtrip( void )
|
|||
|
||||
b3DynamicTree_Destroy( &loaded );
|
||||
b3DynamicTree_Destroy( &tree );
|
||||
|
||||
b3BoxHull box = b3MakeBoxHull( 0.5f, 0.5f, 0.5f );
|
||||
|
||||
b3CompoundHullDef hulls[2];
|
||||
hulls[0].hull = &box.base;
|
||||
hulls[0].transform = (b3Transform){ { -1.0f, 0.0f, 0.0f }, b3Quat_identity };
|
||||
hulls[0].material = b3DefaultSurfaceMaterial();
|
||||
hulls[1].hull = &box.base;
|
||||
hulls[1].transform = (b3Transform){ { 1.0f, 0.0f, 0.0f }, b3Quat_identity };
|
||||
hulls[1].material = b3DefaultSurfaceMaterial();
|
||||
|
||||
b3CompoundDef compoundDef = { 0 };
|
||||
compoundDef.hulls = hulls;
|
||||
compoundDef.hullCount = 2;
|
||||
b3CompoundData* compound = b3CreateCompound( &compoundDef );
|
||||
ENSURE( compound != NULL );
|
||||
ENSURE( compound->tree.parents == NULL );
|
||||
ENSURE( compound->tree.proxyCapacity == compound->tree.proxyCount );
|
||||
|
||||
const char* compoundPath = "test_dynamic_tree_compound.dat";
|
||||
remove( compoundPath );
|
||||
b3DynamicTree_Save( &compound->tree, compoundPath );
|
||||
|
||||
b3DynamicTree noTree = b3DynamicTree_Load( compoundPath, 1.0f );
|
||||
remove( compoundPath );
|
||||
ENSURE( noTree.nodes == NULL );
|
||||
|
||||
b3DestroyCompound( compound );
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1948,6 +1948,11 @@ static int CorruptCompound( void )
|
|||
memcpy( patched + blobOffset - 4, &shortByteCount, sizeof( shortByteCount ) );
|
||||
ENSURE( b3CreatePlayer( patched, recSize, 1 ) == NULL );
|
||||
|
||||
memcpy( patched, recData, (size_t)recSize );
|
||||
int32_t zeroByteCount = 0;
|
||||
memcpy( patched + blobOffset - 4, &zeroByteCount, sizeof( zeroByteCount ) );
|
||||
ENSURE( b3CreatePlayer( patched, recSize, 1 ) == NULL );
|
||||
|
||||
memcpy( patched, recData, (size_t)recSize );
|
||||
patched[blobOffset - 5] = (uint8_t)b3_geometryMesh;
|
||||
ENSURE( b3ValidateReplay( patched, recSize, 1 ) == false );
|
||||
|
|
@ -1963,13 +1968,16 @@ static int CorruptCompound( void )
|
|||
// without growing the registry. Forces the collision by handing the same hash to distinct blobs.
|
||||
static int GeometryHashCollision( void )
|
||||
{
|
||||
const int n = 16;
|
||||
enum
|
||||
{
|
||||
n = 16
|
||||
};
|
||||
const uint64_t sharedHash = 0xABCD1234ull;
|
||||
|
||||
b3GeometryRegistry reg = { 0 };
|
||||
|
||||
uint8_t* blobA = (uint8_t*)b3Alloc( (size_t)n );
|
||||
uint8_t* blobB = (uint8_t*)b3Alloc( (size_t)n );
|
||||
uint8_t blobA[n];
|
||||
uint8_t blobB[n];
|
||||
memset( blobA, 0xAA, (size_t)n );
|
||||
memset( blobB, 0xBB, (size_t)n );
|
||||
|
||||
|
|
@ -1982,12 +1990,12 @@ static int GeometryHashCollision( void )
|
|||
// Re-interning either blob must find it through the hash chain and never grow the registry,
|
||||
// including the one shadowed behind the bucket head. The old single-entry lookup missed the
|
||||
// shadowed blob and appended a duplicate, which is exactly what tripped the keyframe assert.
|
||||
uint8_t* blobA2 = (uint8_t*)b3Alloc( (size_t)n );
|
||||
uint8_t blobA2[n];
|
||||
memset( blobA2, 0xAA, (size_t)n );
|
||||
ENSURE( b3InternGeometry( ®, b3_geometryHull, sharedHash, blobA2, n ) == idA );
|
||||
ENSURE( reg.entries.count == 2 );
|
||||
|
||||
uint8_t* blobB2 = (uint8_t*)b3Alloc( (size_t)n );
|
||||
uint8_t blobB2[n];
|
||||
memset( blobB2, 0xBB, (size_t)n );
|
||||
ENSURE( b3InternGeometry( ®, b3_geometryHull, sharedHash, blobB2, n ) == idB );
|
||||
ENSURE( reg.entries.count == 2 );
|
||||
|
|
@ -1997,9 +2005,9 @@ static int GeometryHashCollision( void )
|
|||
// Seed-then-capture: appending byte-identical duplicate slots keeps id == slot index, and a later
|
||||
// exact intern still resolves to one of them without appending a new entry.
|
||||
b3GeometryRegistry seeded = { 0 };
|
||||
uint8_t* slot0 = (uint8_t*)b3Alloc( (size_t)n );
|
||||
uint8_t* slot1 = (uint8_t*)b3Alloc( (size_t)n );
|
||||
uint8_t* slot2 = (uint8_t*)b3Alloc( (size_t)n );
|
||||
uint8_t slot0[n];
|
||||
uint8_t slot1[n];
|
||||
uint8_t slot2[n];
|
||||
memset( slot0, 0xAA, (size_t)n );
|
||||
memset( slot1, 0xBB, (size_t)n );
|
||||
memset( slot2, 0xAA, (size_t)n ); // duplicate of slot0
|
||||
|
|
@ -2007,7 +2015,7 @@ static int GeometryHashCollision( void )
|
|||
ENSURE( b3AppendGeometry( &seeded, b3_geometryHull, sharedHash, slot1, n ) == 1 );
|
||||
ENSURE( b3AppendGeometry( &seeded, b3_geometryHull, sharedHash, slot2, n ) == 2 );
|
||||
|
||||
uint8_t* live = (uint8_t*)b3Alloc( (size_t)n );
|
||||
uint8_t live[n];
|
||||
memset( live, 0xAA, (size_t)n );
|
||||
uint32_t resolved = b3InternGeometry( &seeded, b3_geometryHull, sharedHash, live, n );
|
||||
ENSURE( seeded.entries.count == 3 ); // no growth
|
||||
|
|
|
|||
Loading…
Reference in a new issue