Skip to content

Commit

Permalink
Fix for parallel objects update
Browse files Browse the repository at this point in the history
  • Loading branch information
vTurbine committed Nov 19, 2023
1 parent ef1208d commit 7b84468
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 69 deletions.
1 change: 0 additions & 1 deletion src/xrEngine/xrEngine.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@
<ClCompile Include="XR_IOConsole_control.cpp" />
<ClCompile Include="XR_IOConsole_get.cpp" />
<ClCompile Include="xr_ioc_cmd.cpp" />
<ClCompile Include="xr_object.cpp" />
<ClCompile Include="xr_object_list.cpp" />
<ClCompile Include="x_ray.cpp" />
<ClCompile Include="main.cpp" />
Expand Down
3 changes: 0 additions & 3 deletions src/xrEngine/xrEngine.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -449,9 +449,6 @@
<ClCompile Include="pure_relcase.cpp">
<Filter>Game API\Objects</Filter>
</ClCompile>
<ClCompile Include="xr_object.cpp">
<Filter>Game API\Objects</Filter>
</ClCompile>
<ClCompile Include="xr_object_list.cpp">
<Filter>Game API\Objects</Filter>
</ClCompile>
Expand Down
21 changes: 0 additions & 21 deletions src/xrEngine/xr_object.cpp

This file was deleted.

65 changes: 38 additions & 27 deletions src/xrEngine/xr_object_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include "GameFont.h"
#include "PerformanceAlert.hpp"

#include <xrCore/Threading/TaskManager.hpp>

class fClassEQ
{
CLASS_ID cls;
Expand All @@ -38,10 +40,11 @@ void CObjectList::DumpStatistics(IGameFont& font, IPerformanceAlert* alert)
alert->Print(font, "UpdateCL > 3ms: %3.1f", stats.Update.result);
}

CObjectList::CObjectList() : m_owner_thread_id(Threading::GetCurrThreadId())
CObjectList::CObjectList()
{
statsFrame = u32(-1);
ZeroMemory(map_NETID, 0xffff * sizeof(IGameObject*));
m_secondary_crows.resize(TaskScheduler->GetWorkersCount());
}

CObjectList::~CObjectList()
Expand Down Expand Up @@ -218,6 +221,13 @@ void CObjectList::Update(bool bForce)
statsFrame = Device.dwFrame;
stats.FrameStart();
}

for (auto& crows_list : m_secondary_crows)
{
m_primary_crows.insert(m_primary_crows.end(), crows_list.cbegin(), crows_list.cend());
crows_list.clear();
}

if (!Device.Paused() || bForce)
{
// Clients
Expand All @@ -226,9 +236,6 @@ void CObjectList::Update(bool bForce)
// Select Crow-Mode
stats.Updated = 0;

m_primary_crows.insert(m_primary_crows.end(), m_secondary_crows.begin(), m_secondary_crows.end());
m_secondary_crows.clear();

#if 0
std::sort (m_own_crows.begin(), m_own_crows.end());
m_own_crows.erase (
Expand Down Expand Up @@ -498,33 +505,18 @@ void CObjectList::Destroy(IGameObject* game_obj)
return;
net_Unregister(game_obj);

if (!Device.Paused())
{
// if a game is paused list of other crows should be empty - Why?
if (!m_secondary_crows.empty())
{
Msg("assertion !m_other_crows.empty() failed: %d", m_secondary_crows.size());

u32 j = 0;
for (auto& iter : m_secondary_crows)
Msg("%d %s", j++, iter->cName().c_str());
VERIFY(Device.Paused() || m_secondary_crows.empty());
m_secondary_crows.clear();
}
}
else
{
// if game is paused remove the object from list of other crows
auto iter = std::find(m_secondary_crows.begin(), m_secondary_crows.end(), game_obj);
if (iter != m_secondary_crows.end())
m_secondary_crows.erase(iter);
}

{
// Always remove the object from list of own crows. The object may be not a crow.
auto iter = std::find(m_primary_crows.begin(), m_primary_crows.end(), game_obj);
if (iter != m_primary_crows.end())
m_primary_crows.erase(iter);

for (auto& crows_list : m_secondary_crows)
{
auto iter = std::find(crows_list.begin(), crows_list.end(), game_obj);
if (iter != crows_list.end())
crows_list.erase(iter);
}
}

// Remove the object from list of active objects if the object is active,
Expand Down Expand Up @@ -583,7 +575,10 @@ bool CObjectList::dump_all_objects()
dump_list(objects_active, "objects_active");
dump_list(objects_sleeping, "objects_sleeping");
dump_list(m_primary_crows, "m_own_crows");
dump_list(m_secondary_crows, "m_other_crows");
for (auto& crows_list : m_secondary_crows)
{
dump_list(crows_list, "m_other_crows");
}
return false;
}

Expand Down Expand Up @@ -619,6 +614,22 @@ void CObjectList::register_object_to_destroy(IGameObject* object_to_destroy)
}
}

IC CObjectList::Objects& CObjectList::get_crows()
{
const size_t list_id = TaskScheduler->GetCurrentWorkerID();
VERIFY(list_id < m_secondary_crows.size());
return m_secondary_crows[list_id];
}

void CObjectList::o_crow(IGameObject* O)
{
Objects& crows = get_crows();
VERIFY(std::find(crows.begin(), crows.end(), O) == crows.end());
crows.push_back(O);

O->SetCrowUpdateFrame(Device.dwFrame);
}

#ifdef DEBUG
bool CObjectList::registered_object_to_destroy(const IGameObject* object_to_destroy) const
{
Expand Down
12 changes: 3 additions & 9 deletions src/xrEngine/xr_object_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ class ENGINE_API CObjectList
* @brief m_primary_crows - list of items of the primary thread
* @brief m_secondary_crows - list of items of the secondary thread
*/
Objects m_primary_crows, m_secondary_crows;
Threading::ThreadId m_owner_thread_id;
Objects m_primary_crows;
xr_vector<Objects> m_secondary_crows;
ObjectUpdateStatistics stats;
u32 statsFrame;

Expand Down Expand Up @@ -130,13 +130,7 @@ class ENGINE_API CObjectList
#endif // #ifdef DEBUG

private:
IC Objects& get_crows()
{
if (Threading::ThreadIdsAreEqual(Threading::GetCurrThreadId(), m_owner_thread_id))
return (m_primary_crows);

return (m_secondary_crows);
}
IC Objects& get_crows();

static void clear_crow_vec(Objects& o);
static void dump_list(Objects& v, pcstr reason);
Expand Down
2 changes: 0 additions & 2 deletions src/xrGame/GameObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,6 @@ CGameObject::~CGameObject()

void CGameObject::MakeMeCrow()
{
ScopeLock lock{ &render_lock };

if (Props.crow)
return;
if (!processing_enabled())
Expand Down
1 change: 0 additions & 1 deletion src/xrGame/GameObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ class CGameObject : public IGameObject,
ai_obstacle* m_ai_obstacle;
Fmatrix m_previous_matrix;
CALLBACK_VECTOR m_visual_callback;
Lock render_lock{};

protected:
CScriptBinder scriptBinder;
Expand Down
3 changes: 0 additions & 3 deletions src/xrGame/ai/crow/ai_crow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,15 +325,12 @@ void CAI_Crow::UpdateCL()
}
void CAI_Crow::renderable_Render(u32 context_id, IRenderable* root)
{
ScopeLock lock{ &render_lock };
UpdateWorkload(Device.fTimeDelta * (Device.dwFrame - o_workload_frame));
inherited::renderable_Render(context_id, root);
o_workload_rframe = Device.dwFrame;
}
void CAI_Crow::shedule_Update(u32 DT)
{
ScopeLock lock{ &render_lock };

float fDT = float(DT) / 1000.F;
spatial.type &= ~STYPE_VISIBLEFORAI;

Expand Down
2 changes: 0 additions & 2 deletions src/xrGame/ai/crow/ai_crow.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ class CAI_Crow : public CEntity
void Unload();
};

Lock render_lock{}; // TODO: this can be avoided as well.

public:
void OnHitEndPlaying(CBlend* B);

Expand Down

0 comments on commit 7b84468

Please sign in to comment.