Skip to content

Add support for shape serialization #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
220 changes: 220 additions & 0 deletions libs/JoltC/JoltPhysicsC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ FN(toJph)(const JPC_Body *in) { assert(in); return reinterpret_cast<const JPH::B
FN(toJpc)(JPH::Body *in) { assert(in); return reinterpret_cast<JPC_Body *>(in); }
FN(toJph)(JPC_Body *in) { assert(in); return reinterpret_cast<JPH::Body *>(in); }

FN(toJph)(JPC_PhysicsMaterial *in) { return reinterpret_cast<JPH::PhysicsMaterial *>(in); }
FN(toJph)(const JPC_PhysicsMaterial *in) { return reinterpret_cast<const JPH::PhysicsMaterial *>(in); }
FN(toJpc)(JPH::PhysicsMaterial *in) { return reinterpret_cast<JPC_PhysicsMaterial *>(in); }
FN(toJpc)(const JPH::PhysicsMaterial *in) { return reinterpret_cast<const JPC_PhysicsMaterial *>(in); }

FN(toJph)(const JPC_ShapeSettings *in) {
Expand Down Expand Up @@ -232,6 +234,36 @@ FN(toJph)(const JPC_ConvexHullShape *in) { assert(in); return reinterpret_cast<c
FN(toJpc)(JPH::ConvexHullShape *in) { assert(in); return reinterpret_cast<JPC_ConvexHullShape *>(in); }
FN(toJpc)(const JPH::ConvexHullShape *in) { assert(in); return reinterpret_cast<const JPC_ConvexHullShape *>(in); }

FN(toJph)(JPC_DecoratedShape *in) { assert(in); return reinterpret_cast<JPH::DecoratedShape *>(in); }
FN(toJph)(const JPC_DecoratedShape *in) { assert(in); return reinterpret_cast<const JPH::DecoratedShape *>(in); }
FN(toJpc)(JPH::DecoratedShape *in) { assert(in); return reinterpret_cast<JPC_DecoratedShape *>(in); }
FN(toJpc)(const JPH::DecoratedShape *in) { assert(in); return reinterpret_cast<const JPC_DecoratedShape *>(in); }

FN(toJph)(JPC_RotatedTranslatedShape *in) { assert(in); return reinterpret_cast<JPH::RotatedTranslatedShape *>(in); }
FN(toJph)(const JPC_RotatedTranslatedShape *in) { assert(in); return reinterpret_cast<const JPH::RotatedTranslatedShape *>(in); }
FN(toJpc)(JPH::RotatedTranslatedShape *in) { assert(in); return reinterpret_cast<JPC_RotatedTranslatedShape *>(in); }
FN(toJpc)(const JPH::RotatedTranslatedShape *in) { assert(in); return reinterpret_cast<const JPC_RotatedTranslatedShape *>(in); }

FN(toJph)(JPC_ShapeToIDMap *in) { assert(in); return reinterpret_cast<JPH::Shape::ShapeToIDMap *>(in); }
FN(toJph)(const JPC_ShapeToIDMap *in) { assert(in); return reinterpret_cast<const JPH::Shape::ShapeToIDMap *>(in); }
FN(toJpc)(JPH::Shape::ShapeToIDMap *in) { assert(in); return reinterpret_cast<JPC_ShapeToIDMap *>(in); }
FN(toJpc)(const JPH::Shape::ShapeToIDMap *in) { assert(in); return reinterpret_cast<const JPC_ShapeToIDMap *>(in); }

FN(toJph)(JPC_MaterialToIDMap *in) { assert(in); return reinterpret_cast<JPH::Shape::MaterialToIDMap *>(in); }
FN(toJph)(const JPC_MaterialToIDMap *in) { assert(in); return reinterpret_cast<const JPH::Shape::MaterialToIDMap *>(in); }
FN(toJpc)(JPH::Shape::MaterialToIDMap *in) { assert(in); return reinterpret_cast<JPC_MaterialToIDMap *>(in); }
FN(toJpc)(const JPH::Shape::MaterialToIDMap *in) { assert(in); return reinterpret_cast<const JPC_MaterialToIDMap *>(in); }

FN(toJph)(JPC_IDToShapeMap *in) { assert(in); return reinterpret_cast<JPH::Shape::IDToShapeMap *>(in); }
FN(toJph)(const JPC_IDToShapeMap *in) { assert(in); return reinterpret_cast<const JPH::Shape::IDToShapeMap *>(in); }
FN(toJpc)(JPH::Shape::IDToShapeMap *in) { assert(in); return reinterpret_cast<JPC_IDToShapeMap *>(in); }
FN(toJpc)(const JPH::Shape::IDToShapeMap *in) { assert(in); return reinterpret_cast<const JPC_IDToShapeMap *>(in); }

FN(toJph)(JPC_IDToMaterialMap *in) { assert(in); return reinterpret_cast<JPH::Shape::IDToMaterialMap *>(in); }
FN(toJph)(const JPC_IDToMaterialMap *in) { assert(in); return reinterpret_cast<const JPH::Shape::IDToMaterialMap *>(in); }
FN(toJpc)(JPH::Shape::IDToMaterialMap *in) { assert(in); return reinterpret_cast<JPC_IDToMaterialMap *>(in); }
FN(toJpc)(const JPH::Shape::IDToMaterialMap *in) { assert(in); return reinterpret_cast<const JPC_IDToMaterialMap *>(in); }

FN(toJph)(const JPC_ConstraintSettings *in) {
ENSURE_TYPE(in, JPH::ConstraintSettings);
return reinterpret_cast<const JPH::ConstraintSettings *>(in);
Expand Down Expand Up @@ -1936,6 +1968,168 @@ JPC_Shape_CastRay(const JPC_Shape *in_shape,
assert(in_shape && in_ray && in_id_creator && io_hit);
return toJph(in_shape)->CastRay(*toJph(in_ray), *toJph(in_id_creator), *toJph(io_hit));
}

JPC_API void
JPC_Shape_SaveBinaryState(const JPC_Shape *in_shape, void *in_stream_out)
{
assert(in_shape && in_stream_out);
return toJph(in_shape)->SaveBinaryState(*static_cast<JPH::StreamOut *>(in_stream_out));
}

JPC_API void
JPC_Shape_SaveWithChildren(const JPC_Shape *in_shape, void *in_stream_out, JPC_ShapeToIDMap *io_shape_map, JPC_MaterialToIDMap *io_material_map)
{
assert(in_shape && io_shape_map && io_material_map);
return toJph(in_shape)->SaveWithChildren(*static_cast<JPH::StreamOut *>(in_stream_out), *toJph(io_shape_map), *toJph(io_material_map));
}

JPC_API void
JPC_Shape_SaveWithChildren_All(const JPC_Shape *in_shape, void *in_stream_out)
{
assert(in_shape);
JPH::Shape::ShapeToIDMap tmp_shape_map;
JPH::Shape::MaterialToIDMap tmp_material_map;
return toJph(in_shape)->SaveWithChildren(*static_cast<JPH::StreamOut *>(in_stream_out), tmp_shape_map, tmp_material_map);
}

JPC_API JPC_Shape*
JPC_Shape_sRestoreFromBinaryState(void *in_stream_in)
{
assert(in_stream_in);
const JPH::Result result = JPH::Shape::sRestoreFromBinaryState(*static_cast<JPH::StreamIn *>(in_stream_in));
if (result.HasError()) return nullptr;
JPH::Shape *shape = const_cast<JPH::Shape*>(result.Get().GetPtr());
shape->AddRef();
return toJpc(shape);
}

JPC_API JPC_Shape*
JPC_Shape_sRestoreWithChildren(void *in_stream_in, JPC_IDToShapeMap *io_shape_map, JPC_IDToMaterialMap *io_material_map)
{
assert(in_stream_in && io_shape_map && io_material_map);
const JPH::Result result = JPH::Shape::sRestoreWithChildren(*static_cast<JPH::StreamIn *>(in_stream_in),
*toJph(io_shape_map),
*toJph(io_material_map));

if (result.HasError()) return nullptr;
JPH::Shape *shape = const_cast<JPH::Shape*>(result.Get().GetPtr());
shape->AddRef();
return toJpc(shape);
}

JPC_API JPC_Shape*
JPC_Shape_sRestoreWithChildren_All(void *in_stream_in)
{
assert(in_stream_in);
JPH::Shape::IDToShapeMap tmp_shape_map;
JPH::Shape::IDToMaterialMap tmp_material_map;
const JPH::Result result = JPH::Shape::sRestoreWithChildren(*static_cast<JPH::StreamIn *>(in_stream_in),
tmp_shape_map,
tmp_material_map);

if (result.HasError()) return nullptr;
JPH::Shape *shape = const_cast<JPH::Shape*>(result.Get().GetPtr());
shape->AddRef();
return toJpc(shape);
}
//--------------------------------------------------------------------------------------------------
//
// JPC_Shape Serialization Structures
//
//--------------------------------------------------------------------------------------------------
JPC_API JPC_ShapeToIDMap *
JPC_ShapeToIDMap_Create()
{
return toJpc(new JPH::Shape::ShapeToIDMap());
}

JPC_API void
JPC_ShapeToIDMap_Add(JPC_ShapeToIDMap *in_map, const JPC_Shape *const *in_shapes, uint32_t in_num_shapes)
{
assert(in_map);
JPH::Shape::ShapeToIDMap& map = *toJph(in_map);
for (uint32_t i = 0; i < in_num_shapes; ++i)
{
uint32_t shape_id = (uint32_t)map.size();
map[toJph(in_shapes[i])] = shape_id;
}
}

JPC_API void
JPC_ShapeToIDMap_Destroy(JPC_ShapeToIDMap *in_map)
{
JPH::Free(toJph(in_map));
}

JPC_API JPC_MaterialToIDMap *
JPC_MaterialToIDMap_Create()
{
return toJpc(new JPH::Shape::MaterialToIDMap());
}

JPC_API void
JPC_MaterialToIDMap_Add(JPC_MaterialToIDMap *in_map, const JPC_PhysicsMaterial *const *in_materials, uint32_t in_num_materials)
{
assert(in_map);
JPH::Shape::MaterialToIDMap& map = *toJph(in_map);
for (uint32_t i = 0; i < in_num_materials; ++i)
{
uint32_t material_id = (uint32_t)map.size();
map[toJph(in_materials[i])] = material_id;
}
}

JPC_API void
JPC_MaterialToIDMap_Destroy(JPC_MaterialToIDMap *in_map)
{
JPH::Free(toJph(in_map));
}

JPC_API JPC_IDToShapeMap*
JPC_IDToShapeMap_Create()
{
return toJpc(new JPH::Shape::IDToShapeMap());
}

JPC_API void
JPC_IDToShapeMap_Add(JPC_IDToShapeMap *in_map, JPC_Shape *const *in_shapes, uint32_t in_num_shapes)
{
assert(in_map);
JPH::Shape::IDToShapeMap& map = *toJph(in_map);
for (uint32_t i = 0; i < in_num_shapes; ++i)
{
map.push_back(toJph(in_shapes[i]));
}
}

JPC_API void
JPC_IDToShapeMap_Destroy(JPC_ShapeToIDMap *in_map)
{
JPH::Free(toJph(in_map));
}

JPC_API JPC_IDToMaterialMap*
JPC_IDToMaterialMap_Create()
{
return toJpc(new JPH::Shape::IDToMaterialMap());
}

JPC_API void
JPC_IDToMaterialMap_Add(JPC_IDToMaterialMap *in_map, JPC_PhysicsMaterial *const *in_materials, uint32_t in_num_materials)
{
assert(in_map);
JPH::Shape::IDToMaterialMap& map = *toJph(in_map);
for (uint32_t i = 0; i < in_num_materials; ++i)
{
map.push_back(toJph(in_materials[i]));
}
}

JPC_API void
JPC_IDToMaterialMap_Destroy(JPC_ShapeToIDMap *in_map)
{
JPH::Free(toJph(in_map));
}
//--------------------------------------------------------------------------------------------------
//
// JPC_BoxShape
Expand Down Expand Up @@ -1985,6 +2179,32 @@ JPC_ConvexHullShape_GetFaceVertices(const JPC_ConvexHullShape *in_shape,
}
//--------------------------------------------------------------------------------------------------
//
// JPC_DecoratedShape
//
//--------------------------------------------------------------------------------------------------
JPC_API const JPC_Shape*
JPC_DecoratedShape_GetInnerShape(const JPC_DecoratedShape *in_shape)
{
return toJpc(toJph(in_shape)->GetInnerShape());
}
//--------------------------------------------------------------------------------------------------
//
// JPC_RotatedTranslatedShape
//
//--------------------------------------------------------------------------------------------------
JPC_API void
JPC_RotatedTranslatedShape_GetRotation(const JPC_RotatedTranslatedShape *in_shape, float out_rotation[4])
{
storeVec4(out_rotation, toJph(in_shape)->GetRotation().GetXYZW());
}
//--------------------------------------------------------------------------------------------------
JPC_API void
JPC_RotatedTranslatedShape_GetPosition(const JPC_RotatedTranslatedShape *in_shape, float out_position[3])
{
storeVec3(out_position, toJph(in_shape)->GetPosition());
}
//--------------------------------------------------------------------------------------------------
//
// JPC_ConstraintSettings
//
//--------------------------------------------------------------------------------------------------
Expand Down
123 changes: 120 additions & 3 deletions libs/JoltC/JoltPhysicsC.h
Original file line number Diff line number Diff line change
Expand Up @@ -345,9 +345,16 @@ typedef struct JPC_FixedConstraintSettings JPC_FixedConstraintSettings;
typedef struct JPC_PhysicsSystem JPC_PhysicsSystem;
typedef struct JPC_SharedMutex JPC_SharedMutex;

typedef struct JPC_Shape JPC_Shape;
typedef struct JPC_BoxShape JPC_BoxShape;
typedef struct JPC_ConvexHullShape JPC_ConvexHullShape;
typedef struct JPC_Shape JPC_Shape;
typedef struct JPC_BoxShape JPC_BoxShape;
typedef struct JPC_ConvexHullShape JPC_ConvexHullShape;
typedef struct JPC_DecoratedShape JPC_DecoratedShape;
typedef struct JPC_RotatedTranslatedShape JPC_RotatedTranslatedShape;

typedef struct JPC_ShapeToIDMap JPC_ShapeToIDMap;
typedef struct JPC_MaterialToIDMap JPC_MaterialToIDMap;
typedef struct JPC_IDToShapeMap JPC_IDToShapeMap;
typedef struct JPC_IDToMaterialMap JPC_IDToMaterialMap;

typedef struct JPC_Constraint JPC_Constraint;
typedef struct JPC_PhysicsMaterial JPC_PhysicsMaterial;
Expand Down Expand Up @@ -751,6 +758,36 @@ typedef bool (*JPC_BodyDrawFilterFunc)(const JPC_Body *);
// Interfaces (virtual tables)
//
//--------------------------------------------------------------------------------------------------
typedef struct JPC_StreamOutVTable
{
_JPC_VTABLE_HEADER;

// Required, *cannot* be NULL.
void
(*WriteBytes)(void *in_self, const void *in_data, size_t in_num_bytes);

// Required, *cannot* be NULL.
bool
(*IsFailed)(const void *in_self);
} JPC_StreamOutVTable;

typedef struct JPC_StreamInVTable
{
_JPC_VTABLE_HEADER;

// Required, *cannot* be NULL.
void
(*ReadBytes)(void *in_self, void *out_data, size_t in_num_bytes);

// Required, *cannot* be NULL.
bool
(*IsEOF)(const void *in_self);

// Required, *cannot* be NULL.
bool
(*IsFailed)(const void *in_self);
} JPC_StreamInVTable;

typedef struct JPC_BroadPhaseLayerInterfaceVTable
{
_JPC_VTABLE_HEADER;
Expand Down Expand Up @@ -1738,6 +1775,69 @@ JPC_Shape_CastRay(const JPC_Shape *in_shape,
const JPC_RayCast *in_ray,
const JPC_SubShapeIDCreator *in_id_creator,
JPC_RayCastResult *io_hit); // *Must* be default initialized (see JPC_RayCastResult)

// `in_stream_out` *must* point to a struct that has JPC_StreamOutVTable as its first member
JPC_API void
JPC_Shape_SaveBinaryState(const JPC_Shape *in_shape, void *in_stream_out);

JPC_API void
JPC_Shape_SaveWithChildren(const JPC_Shape *in_shape, void *in_stream_out, JPC_ShapeToIDMap *io_shape_map, JPC_MaterialToIDMap *io_material_map);

// This version uses temporary maps and thus will save all shape IDs.
JPC_API void
JPC_Shape_SaveWithChildren_All(const JPC_Shape *in_shape, void *in_stream_out);

// `in_stream_in` *must *point to a struct that has JPC_StreamInVTable as its first member
JPC_API JPC_Shape*
JPC_Shape_sRestoreFromBinaryState(void *in_stream_in);

// `in_stream_in` *must *point to a struct that has JPC_StreamInVTable as its first member
JPC_API JPC_Shape*
JPC_Shape_sRestoreWithChildren(void *in_stream_in, JPC_IDToShapeMap *io_shape_map, JPC_IDToMaterialMap *io_material_map);

// This version uses temporary reverse mappings and thus will restore all shape IDs.
JPC_API JPC_Shape*
JPC_Shape_sRestoreWithChildren_All(void *in_stream_in);
//--------------------------------------------------------------------------------------------------
//
// JPC_Shape Serialization Structures
//
//--------------------------------------------------------------------------------------------------
JPC_API JPC_ShapeToIDMap*
JPC_ShapeToIDMap_Create();

JPC_API void
JPC_ShapeToIDMap_Add(JPC_ShapeToIDMap *in_map, const JPC_Shape *const *in_shapes, uint32_t in_num_shapes);

JPC_API void
JPC_ShapeToIDMap_Destroy(JPC_ShapeToIDMap *in_map);

JPC_API JPC_MaterialToIDMap*
JPC_MaterialToIDMap_Create();

JPC_API void
JPC_MaterialToIDMap_Add(JPC_MaterialToIDMap *in_map, const JPC_PhysicsMaterial *const *in_materials, uint32_t in_num_materials);

JPC_API void
JPC_MaterialToIDMap_Destroy(JPC_MaterialToIDMap *in_map);

JPC_API JPC_IDToShapeMap*
JPC_IDToShapeMap_Create();

JPC_API void
JPC_IDToShapeMap_Add(JPC_IDToShapeMap *in_map, JPC_Shape *const *in_shapes, uint32_t in_num_shapes);

JPC_API void
JPC_IDToShapeMap_Destroy(JPC_ShapeToIDMap *in_map);

JPC_API JPC_IDToMaterialMap*
JPC_IDToMaterialMap_Create();

JPC_API void
JPC_IDToMaterialMap_Add(JPC_IDToMaterialMap *in_map, JPC_PhysicsMaterial *const *in_materials, uint32_t in_num_materials);

JPC_API void
JPC_IDToMaterialMap_Destroy(JPC_IDToMaterialMap *in_map);
//--------------------------------------------------------------------------------------------------
//
// JPC_BoxShape
Expand Down Expand Up @@ -1770,6 +1870,23 @@ JPC_ConvexHullShape_GetFaceVertices(const JPC_ConvexHullShape *in_shape,
uint32_t *out_vertices);
//--------------------------------------------------------------------------------------------------
//
// JPC_DecoratedShape
//
//--------------------------------------------------------------------------------------------------
JPC_API const JPC_Shape*
JPC_DecoratedShape_GetInnerShape(const JPC_DecoratedShape *in_shape);
//--------------------------------------------------------------------------------------------------
//
// JPC_RotatedTranslatedShape
//
//--------------------------------------------------------------------------------------------------
JPC_API void
JPC_RotatedTranslatedShape_GetRotation(const JPC_RotatedTranslatedShape *in_shape, float out_rotation[4]);

JPC_API void
JPC_RotatedTranslatedShape_GetPosition(const JPC_RotatedTranslatedShape *in_shape, float out_position[3]);
//--------------------------------------------------------------------------------------------------
//
// JPC_ConstraintSettings
//
//--------------------------------------------------------------------------------------------------
Expand Down
Loading