Skip to content

Commit

Permalink
optimization - parallel module update
Browse files Browse the repository at this point in the history
  • Loading branch information
nem0 committed Nov 22, 2024
1 parent 6e54e5c commit b3f650f
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 17 deletions.
16 changes: 10 additions & 6 deletions src/animation/animation_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -755,19 +755,23 @@ struct AnimationModuleImpl final : AnimationModule {
}


void update(float time_delta) override
{
void updateParallel(float time_delta) override {
PROFILE_FUNCTION();
if (!m_is_game_running) return;

updateAnimables(time_delta);
updatePropertyAnimators(time_delta);


jobs::forEach(m_animators.size(), 1, [&](i32 idx, i32){
updateAnimator(m_animators[idx], time_delta);
});
}

void update(float time_delta) override {
PROFILE_FUNCTION();
if (!m_is_game_running) return;

updateAnimables(time_delta);
updatePropertyAnimators(time_delta);
}


PropertyAnimation* loadPropertyAnimation(const Path& path) const
{
Expand Down
8 changes: 6 additions & 2 deletions src/engine/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,16 +309,20 @@ struct EngineImpl final : Engine {
computeSmoothTimeDelta();

if (!m_paused || m_next_frame) {
auto& modules = world.getModules();
jobs::forEach(modules.size(), 1, [&](u32 idx, u32){
modules[idx]->updateParallel(dt);
});
{
PROFILE_BLOCK("update modules");
for (UniquePtr<IModule>& module : world.getModules())
for (UniquePtr<IModule>& module : modules)
{
module->update(dt);
}
}
{
PROFILE_BLOCK("late update modules");
for (UniquePtr<IModule>& module : world.getModules())
for (UniquePtr<IModule>& module : modules)
{
module->lateUpdate(dt);
}
Expand Down
6 changes: 6 additions & 0 deletions src/engine/plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,14 @@ struct LUMIX_ENGINE_API IModule
virtual void afterReload(InputMemoryStream& serializer) {}
virtual const char* getName() const = 0;
virtual ISystem& getSystem() const = 0;

// called for all modules at once, i.e. all modules are updated in parallel
virtual void updateParallel(float time_delta) {}
// called after all updateParallel calls are finished, called on "main thread"
virtual void update(float time_delta) = 0;
// called after all update calls are finished, called on "main thread"
virtual void lateUpdate(float time_delta) {}

virtual void endFrame() {}
virtual struct World& getWorld() = 0;
virtual void startGame() {}
Expand Down
2 changes: 1 addition & 1 deletion src/lua/lua_script_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2961,7 +2961,7 @@ void LuaScriptModuleImpl::ScriptInstance::onScriptLoaded(LuaScriptModuleImpl& mo
lua_pop(m_state, 1); // []
}

module.m_to_start.push({cmp.m_entity, (u32)scr_index, false, module.m_is_game_running});
module.m_to_start.push({cmp.m_entity, (u32)scr_index, false, is_reload && module.m_is_game_running});
}


Expand Down
16 changes: 10 additions & 6 deletions src/navigation/navigation_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,12 +363,6 @@ struct NavigationModuleImpl final : NavigationModule


void update(RecastZone& zone, float time_delta) {
if (!zone.crowd) return;
{
PROFILE_BLOCK("dtCrowd::update");
zone.crowd->update(time_delta, nullptr);
}

for (auto& agent : m_agents) {
if (agent.agent < 0) continue;
if (agent.zone != zone.entity) continue;
Expand Down Expand Up @@ -398,6 +392,16 @@ struct NavigationModuleImpl final : NavigationModule
}
}

void updateParallel(float time_delta) override {
if (!m_is_game_running) return;

for (RecastZone& zone : m_zones) {
if (!zone.crowd) continue;
PROFILE_BLOCK("dtCrowd::update");
zone.crowd->update(time_delta, nullptr);
}
}

void lateUpdate(RecastZone& zone, float time_delta) {
if (!zone.crowd) return;

Expand Down
8 changes: 6 additions & 2 deletions src/physics/physics_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1836,14 +1836,18 @@ struct PhysicsModuleImpl final : PhysicsModule
updateDynamicActors(false);
}

void update(float time_delta) override
{
void updateParallel(float time_delta) override {
if (!m_is_game_running) return;

time_delta = minimum(1 / 20.0f, time_delta);
updateVehicles(time_delta);
simulateScene(time_delta);
fetchResults();
}

void update(float time_delta) override {
if (!m_is_game_running) return;

updateDynamicActors(true);
updateControllers(time_delta);

Expand Down
1 change: 1 addition & 0 deletions src/renderer/render_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@ struct RenderModuleImpl final : RenderModule {
StackArray<EntityRef, 16> to_delete(m_allocator);
jobs::Mutex mutex;
ParticleSystem::Stats stats = {};
// TODO move to parallel update?
jobs::forEach(m_particle_emitters.capacity(), 1, [&](i32 idx, i32){
ParticleSystem* ps = m_particle_emitters.getFromIndex(idx);
if (!ps) return;
Expand Down

0 comments on commit b3f650f

Please sign in to comment.