Skip to content

Commit

Permalink
Add optimization for many bodies + build fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Lamakaio authored and jonesmz committed Jun 24, 2024
1 parent 8e16c03 commit c70effd
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 30 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ out/
.vs/
build/
.vscode/
.cache
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@
url = https://github.com/freetype/freetype
[submodule "3rdparty/JoltPhysics"]
path = 3rdparty/JoltPhysics
url = git@github.com:jrouwe/JoltPhysics.git
url = https://github.com/jrouwe/JoltPhysics.git
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ IF(MSVC)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/$<CONFIG> CACHE PATH "")
# Force multiprocessor compilation for all projects
add_compile_options($<$<CXX_COMPILER_ID:MSVC>:/MP>)
# Force dynamic runtime library to be in debug mode in debug mode.
add_compile_options($<$<CONFIG:Debug>:$<$<CXX_COMPILER_ID:MSVC>:/MDd>>)
# Don't generate a blah.manifest file for each build target.
add_link_options(LINKER:/MANIFEST:NO)
ELSEIF(UNIX)
Expand Down
9 changes: 1 addition & 8 deletions src/ospjolt/activescene/joltinteg.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,18 @@ JPH_SUPPRESS_WARNINGS

#include <Jolt/RegisterTypes.h>
#include <Jolt/Core/Factory.h>
#include <Jolt/Core/TempAllocator.h>
#include <Jolt/Core/JobSystemThreadPool.h>
#include <Jolt/Physics/PhysicsSettings.h>
#include <Jolt/Physics/PhysicsSystem.h>
#include <Jolt/Physics/Collision/Shape/BoxShape.h>
#include <Jolt/Physics/Collision/Shape/SphereShape.h>
#include <Jolt/Physics/Collision/Shape/CylinderShape.h>
#include <Jolt/Physics/Collision/Shape/RotatedTranslatedShape.h>
#include <Jolt/Physics/Body/BodyCreationSettings.h>
#include <Jolt/Physics/Body/BodyActivationListener.h>
#include <Jolt/Core/TempAllocator.h>
#include <Jolt/Physics/Collision/Shape/CompoundShape.h>
#include <Jolt/Physics/Collision/Shape/ScaledShape.h>
#include <Jolt/Physics/Collision/Shape/MutableCompoundShape.h>
#include <Jolt/Physics/PhysicsStepListener.h>
#include <Jolt/Core/JobSystemSingleThreaded.h>

JPH_SUPPRESS_WARNING_POP

Expand All @@ -65,10 +61,7 @@ JPH_SUPPRESS_WARNING_POP

#include <iostream>
#include <cstdarg>
#include <thread>
#include <osp/util/logging.h>
#include <concepts>
#include <typeindex>

#include "osp/core/strong_id.h"

Expand Down Expand Up @@ -240,7 +233,7 @@ class PhysicsStepListenerImpl : public PhysicsStepListener
{
public:
PhysicsStepListenerImpl(ACtxJoltWorld* pContext) : m_context(pContext) {};
void OnStep(float inDeltaTime, PhysicsSystem& inPhysicsSystem) override;
void OnStep(float inDeltaTime, PhysicsSystem& rPhysicsSystem) override;

private:
ACtxJoltWorld* m_context;
Expand Down
18 changes: 9 additions & 9 deletions src/ospjolt/activescene/joltinteg_fn.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Open Space Program
* Copyright © 2019-2020 Open Space Program Project
* Copyright © 2019-2024 Open Space Program Project
*
* MIT License
*
Expand Down Expand Up @@ -99,7 +99,7 @@ void SysJolt::update_world(

rCtxWorld.m_pTransform = std::addressof(rTf);

uint collisionSteps = 1;
int collisionSteps = 1;
pJoltWorld->Update(timestep, collisionSteps, &rCtxWorld.m_temp_allocator, rCtxWorld.m_joltJobSystem.get());
}

Expand Down Expand Up @@ -135,7 +135,7 @@ Ref<Shape> SysJolt::create_primitive(ACtxJoltWorld &rCtxWorld, osp::EShape shape
return RotatedTranslatedShapeSettings(
Vec3Arg::sZero(),
Quat::sRotation(Vec3::sAxisX(), JPH_PI/2),
new CylinderShapeSettings(scale.GetY(), 2.0f * scale.GetX())
new CylinderShapeSettings(scale.GetZ(), 2.0f * scale.GetX())
).Create().Get();

default:
Expand All @@ -146,7 +146,7 @@ Ref<Shape> SysJolt::create_primitive(ACtxJoltWorld &rCtxWorld, osp::EShape shape
void SysJolt::scale_shape(Ref<Shape> rShape, Vec3Arg scale) {
if (rShape->GetSubType() == EShapeSubType::Scaled) {
ScaledShape* rScaledShape = dynamic_cast<ScaledShape*>(rShape.GetPtr());
if (rScaledShape)
if (rScaledShape != nullptr)
{
rShape = new ScaledShape(rScaledShape->GetInnerShape(), scale * rScaledShape->GetScale());
}
Expand Down Expand Up @@ -180,7 +180,7 @@ void SysJolt::find_shapes_recurse(
ACompTransformStorage_t const& rTf,
ActiveEnt ent,
Matrix4 const& transform,
CompoundShapeSettings& pCompound) noexcept
CompoundShapeSettings& rCompound) noexcept
{
// Add jolt shape if exists
if (rCtxWorld.m_shapes.contains(ent))
Expand All @@ -189,7 +189,7 @@ void SysJolt::find_shapes_recurse(

// Set transform relative to root body
SysJolt::scale_shape(rShape, Vec3MagnumToJolt(transform.scaling()));
pCompound.AddShape(
rCompound.AddShape(
Vec3MagnumToJolt(transform.translation()),
QuatMagnumToJolt(osp::Quaternion::fromMatrix(transform.rotation())),
rShape);
Expand All @@ -210,7 +210,7 @@ void SysJolt::find_shapes_recurse(
Matrix4 const childMatrix = transform * rChildTransform.m_transform;

find_shapes_recurse(
rCtxPhys, rCtxWorld, rScnGraph, rTf, child, childMatrix, pCompound);
rCtxPhys, rCtxWorld, rScnGraph, rTf, child, childMatrix, rCompound);
}

}
Expand All @@ -220,10 +220,10 @@ void SysJolt::find_shapes_recurse(
//TODO this is locking on all bodies. Is it bad ?
//The easy fix is to provide multiple step listeners for disjoint sets of bodies, which can then run in parallel.
//It might not be worth it considering this function should be quite fast.
void PhysicsStepListenerImpl::OnStep(float inDeltaTime, PhysicsSystem &rJoltWorld)
void PhysicsStepListenerImpl::OnStep(float inDeltaTime, PhysicsSystem &rPhysicsSystem)
{
//no lock as all bodies are already locked
BodyInterface &bodyInterface = rJoltWorld.GetBodyInterfaceNoLock();
BodyInterface &bodyInterface = rPhysicsSystem.GetBodyInterfaceNoLock();
for (BodyId bodyId : m_context->m_bodyIds)
{
JPH::BodyID joltBodyId = BToJolt(bodyId);
Expand Down
4 changes: 2 additions & 2 deletions src/ospjolt/activescene/joltinteg_fn.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class SysJolt
}
}
//Apply a scale to a shape.
static void scale_shape(Ref<Shape> rJoltShap, Vec3Arg scale);
static void scale_shape(Ref<Shape> rShape, Vec3Arg scale);

//Get the inverse mass of a jolt body
static float get_inverse_mass_no_lock(PhysicsSystem& physicsSystem, BodyId bodyId);
Expand Down Expand Up @@ -120,4 +120,4 @@ class SysJolt

};

}
}
37 changes: 27 additions & 10 deletions src/testapp/sessions/jolt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,16 @@ Session setup_phys_shapes_jolt(
.args({ idBasic, idPhysShapes, idPhys, idJolt, idJoltFactors })
.func([] (ACtxBasic const &rBasic, ACtxPhysShapes& rPhysShapes, ACtxPhysics& rPhys, ACtxJoltWorld& rJolt, ForceFactors_t joltFactors) noexcept
{
for (std::size_t i = 0; i < rPhysShapes.m_spawnRequest.size(); ++i)

PhysicsSystem *pJoltWorld = rJolt.m_pPhysicsSystem.get();
BodyInterface &bodyInterface = pJoltWorld->GetBodyInterface();

int numBodies = static_cast<int>(rPhysShapes.m_spawnRequest.size());

std::vector<JPH::BodyID> addedBodies;
addedBodies.reserve(numBodies);

for (std::size_t i = 0; i < numBodies; ++i)
{
SpawnShape const &spawn = rPhysShapes.m_spawnRequest[i];
ActiveEnt const root = rPhysShapes.m_ents[i * 2];
Expand Down Expand Up @@ -235,21 +244,23 @@ Session setup_phys_shapes_jolt(
bodyCreation.mObjectLayer = Layers::MOVING;
}
//TODO helper function ?
PhysicsSystem *pJoltWorld = rJolt.m_pPhysicsSystem.get();
BodyInterface &bodyInterface = pJoltWorld->GetBodyInterface();

JPH::BodyID joltBodyId = BToJolt(bodyId);
bodyInterface.CreateBodyWithID(joltBodyId, bodyCreation);
bodyInterface.AddBody(joltBodyId, EActivation::Activate);
addedBodies.push_back(joltBodyId);

rJolt.m_bodyToEnt[bodyId] = root;
rJolt.m_bodyFactors[bodyId] = joltFactors;
rJolt.m_entToBody.emplace(root, bodyId);

}
//Bodies are added all at once for performance reasons.
BodyInterface::AddState addState = bodyInterface.AddBodiesPrepare(addedBodies.data(), numBodies);
bodyInterface.AddBodiesFinalize(addedBodies.data(), numBodies, addState, EActivation::Activate);
});

return out;
} // setup_phys_shapes_newton
} // setup_phys_shapes_jolt


void compound_collect_recurse(
Expand Down Expand Up @@ -431,6 +442,11 @@ Session setup_vehicle_spawn_jolt(
auto const& itWeldOffsetsLast = std::end(rVehicleSpawn.spawnedWeldOffsets);
auto itWeldOffsets = std::begin(rVehicleSpawn.spawnedWeldOffsets);

PhysicsSystem *pJoltWorld = rJolt.m_pPhysicsSystem.get();
BodyInterface &bodyInterface = pJoltWorld->GetBodyInterface();

std::vector<JPH::BodyID> addedBodies;

for (ACtxVehicleSpawn::TmpToInit const& toInit : rVehicleSpawn.spawnRequest)
{
auto const itWeldOffsetsNext = std::next(itWeldOffsets);
Expand All @@ -440,7 +456,7 @@ Session setup_vehicle_spawn_jolt(

std::for_each(itWeldsFirst + std::ptrdiff_t{*itWeldOffsets},
itWeldsFirst + std::ptrdiff_t{weldOffsetNext},
[&rBasic, &rScnParts, &rVehicleSpawn, &toInit, &rPhys, &rJolt] (WeldId const weld)
[&rBasic, &rScnParts, &rVehicleSpawn, &toInit, &rPhys, &rJolt, &addedBodies] (WeldId const weld)
{
ActiveEnt const weldEnt = rScnParts.weldToActive[weld];

Expand Down Expand Up @@ -494,13 +510,16 @@ Session setup_vehicle_spawn_jolt(
BodyInterface &bodyInterface = pJoltWorld->GetBodyInterface();
JPH::BodyID joltBodyId = BToJolt(bodyId);
bodyInterface.CreateBodyWithID(joltBodyId, bodyCreation);
bodyInterface.AddBody(joltBodyId, EActivation::Activate);

addedBodies.push_back(joltBodyId);
rPhys.m_setVelocity.emplace_back(weldEnt, toInit.velocity);
});

itWeldOffsets = itWeldOffsetsNext;
}
//Bodies are added all at once for performance reasons.
int numBodies = static_cast<int>(addedBodies.size());
BodyInterface::AddState addState = bodyInterface.AddBodiesPrepare(addedBodies.data(), numBodies);
bodyInterface.AddBodiesFinalize(addedBodies.data(), numBodies, addState, EActivation::Activate);
});

return out;
Expand Down Expand Up @@ -745,5 +764,3 @@ Session setup_rocket_thrust_jolt(


} // namespace testapp::scenes


0 comments on commit c70effd

Please sign in to comment.