Skip to content

Commit

Permalink
Threadsafe and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ForserX committed Mar 4, 2025
1 parent 413a47b commit ac69b8b
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/xrCore/shared_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ using str_c = const char*;
#pragma warning(disable : 4200)
struct XRCORE_API str_value
{
u32 dwReference;
xr_atomic_u32 dwReference;
u32 dwLength;
u32 dwCRC;

Expand Down
4 changes: 4 additions & 0 deletions src/xrEngine/PS_instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class ENGINE_API CPS_Instance :
public IRenderable
{
friend class IGame_Persistent;
friend class CParticlesAsync;

template <bool _is_pm, typename T>
friend struct xr_special_free;
Expand All @@ -17,10 +18,12 @@ class ENGINE_API CPS_Instance :
bool m_destroy_on_game_load;

protected:
u32 dwLastTime;
int m_iLifeTime ;
BOOL m_bAutoRemove ;
BOOL m_bDead ;
volatile bool m_NeedDestroy = false;

protected:
virtual ~CPS_Instance ();
virtual void PSI_internal_delete ();
Expand All @@ -36,6 +39,7 @@ class ENGINE_API CPS_Instance :

virtual void Play (bool bHudMode) = 0;
virtual BOOL Locked () { return FALSE; }
virtual void Update (u32 dt) {};

virtual shared_str shedule_Name () const { return shared_str("particle_instance"); };

Expand Down
34 changes: 12 additions & 22 deletions src/xrParticles/ParticlesAsyncManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

#include "../Include/xrRender/ParticleCustom.h"
#include "../Include/xrRender/RenderVisual.h"
#include "../xrEngine/IGame_Persistent.h"

static CParticlesAsync Instance;
//static xr_task_group ParticleObjectTasks;

void CParticlesAsync::Play()
{
Expand All @@ -22,17 +22,17 @@ void CParticlesAsync::Start()

{
PROF_EVENT("Particle Update");
for (CParticlesObject* particle : Instance.Particles)
for (xr_shared_ptr<CPS_Instance> particle : g_pGamePersistent->ps_active)
{
if (particle->m_bDead)
if (particle == nullptr || particle->m_bDead)
continue;

Instance.UpdateParticle(particle);
Instance.UpdateParticle(particle.get());
}
}

PROF_EVENT("Particle Shedule");
for (CParticlesObject* particle : Instance.Particles)
for (xr_shared_ptr<CPS_Instance> particle : g_pGamePersistent->ps_active)
{
particle->Update(Device.dwTimeDelta);
}
Expand All @@ -49,32 +49,22 @@ void CParticlesAsync::Wait()
{
std::this_thread::sleep_for(std::chrono::milliseconds(0));
}
//ParticleObjectTasks.wait();
}
}

bool CParticlesAsync::Push(CParticlesObject* Obj)
{
Instance.Particles.push_back(Obj);

return Instance.IsStarted;
}

void CParticlesAsync::Pop(CParticlesObject* Obj)
{
Wait();

Instance.Particles.remove(Obj);
}

void CParticlesAsync::ForceUpdate(CParticlesObject* Obj)
void CParticlesAsync::ForceUpdate(CPS_Instance* Obj)
{
if (!Instance.IsStarted)
return;

Instance.UpdateParticle(Obj);
}

bool CParticlesAsync::NeedForceUpdate()
{
return Instance.IsStarted;
}

CParticlesAsync::CParticlesAsync()
{
if (!DevicePtr || Device.IsEditorMode())
Expand All @@ -83,7 +73,7 @@ CParticlesAsync::CParticlesAsync()
Device.ParticleWorkerCallback = Start;
}

void CParticlesAsync::UpdateParticle(CParticlesObject* particle) const
void CParticlesAsync::UpdateParticle(CPS_Instance* particle) const
{
u32 dt = Device.dwTimeGlobal - particle->dwLastTime;
IParticleCustom* V = smart_cast<IParticleCustom*>(particle->renderable.visual);
Expand Down
11 changes: 4 additions & 7 deletions src/xrParticles/ParticlesAsyncManager.h
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
#pragma once
class CParticlesObject;
class CPS_Instance;

class PARTICLES_API CParticlesAsync
{
public:
static void Play();
static void Wait();

static bool Push(CParticlesObject* Obj);
static void Pop(CParticlesObject* Obj);

static void ForceUpdate(CParticlesObject* Obj);
static void ForceUpdate(CPS_Instance* Obj);
static bool NeedForceUpdate();

private:
void UpdateParticle(CParticlesObject* particle) const;
void UpdateParticle(CPS_Instance* particle) const;
static void Start();

public:
CParticlesAsync();

private:
xr_list<CParticlesObject*> Particles;
volatile bool IsStarted = false;
};
3 changes: 1 addition & 2 deletions src/xrParticles/ParticlesObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,14 @@ void CParticlesObject::Init (LPCSTR p_name, IRender_Sector* S, BOOL bAutoRemove)
SpatialComponent->spatial.type = 0;
SpatialComponent->spatial.sector = S;

NeedUpdate = CParticlesAsync::Push(this);
NeedUpdate = CParticlesAsync::NeedForceUpdate();

dwLastTime = Device.dwTimeGlobal;
}

//----------------------------------------------------
CParticlesObject::~CParticlesObject()
{
CParticlesAsync::Pop(this);
}

void CParticlesObject::UpdateSpatial()
Expand Down
3 changes: 1 addition & 2 deletions src/xrParticles/ParticlesObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ class PARTICLES_API CParticlesObject :
friend class CParticlesAsync;
using inherited = CPS_Instance;

u32 dwLastTime;
void Init (LPCSTR p_name, IRender_Sector* S, BOOL bAutoRemove);
void UpdateSpatial ();

Expand All @@ -32,7 +31,7 @@ class PARTICLES_API CParticlesObject :
Fvector& Position ();
void SetXFORM (const Fmatrix& m);
IC Fmatrix& XFORM () {return renderable.xform;}
void Update (u32 dt);
virtual void Update (u32 dt) override;
void UpdateParent (const Fmatrix& m, const Fvector& vel);
void SetLiveUpdate (BOOL b);
BOOL GetLiveUpdate ();
Expand Down

0 comments on commit ac69b8b

Please sign in to comment.