Skip to content

Commit

Permalink
Fix an issue where GpuParticles would stop when the parent instance k…
Browse files Browse the repository at this point in the history
…illed
  • Loading branch information
ueshita committed Feb 10, 2024
1 parent 8a8996d commit 3435046
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 44 deletions.
6 changes: 3 additions & 3 deletions Dev/Cpp/Effekseer/Effekseer/Effekseer.Instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ void Instance::FirstUpdate()
{
if (auto gpuParticles = m_pManager->GetGpuParticles())
{
m_gpuEmitterID = gpuParticles->NewEmitter(m_pEffectNode->GpuParticlesResource, (GpuParticles::ParticleGroupID)GetInstanceGlobal());
m_gpuEmitterID = gpuParticles->NewEmitter(m_pEffectNode->GpuParticlesResource, GetInstanceGlobal());

if (m_gpuEmitterID >= 0)
{
Expand Down Expand Up @@ -455,8 +455,6 @@ void Instance::Update(float deltaFrame, bool shown)
{
if (auto gpuParticles = m_pManager->GetGpuParticles())
{
gpuParticles->SetDeltaTime(m_gpuEmitterID, deltaFrame);

gpuParticles->SetTransform(m_gpuEmitterID, ToStruct(globalMatrix_rendered));

auto& paramSet = m_pEffectNode->GpuParticlesResource->GetParamSet();
Expand All @@ -469,6 +467,8 @@ void Instance::Update(float deltaFrame, bool shown)
{
gpuParticles->SetColor(m_gpuEmitterID, ColorInheritance);
}

GetInstanceGlobal()->IsUsingGpuParticles = true;
}
}

Expand Down
2 changes: 2 additions & 0 deletions Dev/Cpp/Effekseer/Effekseer/Effekseer.InstanceGlobal.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ class InstanceGlobal
// Used for collision detection by kill rules
SIMD::Mat44f InvertedEffectGlobalMatrix;

bool IsUsingGpuParticles = false;

bool IsGlobalColorSet = false;
Color GlobalColor = Color(255, 255, 255, 255);

Expand Down
14 changes: 10 additions & 4 deletions Dev/Cpp/Effekseer/Effekseer/Effekseer.Manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ void ManagerImplemented::StopStoppingEffects()
// when gpu particles are found
if (auto gpuParticles = GetGpuParticles())
{
if (gpuParticles->GetParticleCount((GpuParticles::ParticleGroupID)draw_set.GlobalPointer) > 0)
if (gpuParticles->GetParticleCount(draw_set.GlobalPointer) > 0)
{
continue;
}
Expand Down Expand Up @@ -370,7 +370,7 @@ void ManagerImplemented::ExecuteEvents()

if (GetGpuParticles() != nullptr)
{
GetGpuParticles()->KillParticles((GpuParticles::ParticleGroupID)ds.second.GlobalPointer);
GetGpuParticles()->KillParticles(ds.second.GlobalPointer);
}
}

Expand Down Expand Up @@ -1663,6 +1663,14 @@ void ManagerImplemented::UpdateHandleInternal(DrawSet& drawSet)
}
}

if (drawSet.GlobalPointer->IsUsingGpuParticles)
{
if (auto gpuParticles = GetGpuParticles())
{
gpuParticles->SetDeltaTime(drawSet.GlobalPointer, drawSet.GlobalPointer->GetNextDeltaFrame());
}
}

drawSet.GlobalPointer->EndDeltaFrame();
}

Expand Down Expand Up @@ -1786,8 +1794,6 @@ void ManagerImplemented::Compute()
{
gpuParticles->ComputeFrame();
}

gpuParticles->ResetDeltaTime();
}
}

Expand Down
11 changes: 4 additions & 7 deletions Dev/Cpp/Effekseer/Effekseer/Renderer/Effekseer.GpuParticles.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,6 @@ class GpuParticles : public ReferenceObject
using ResourceRef = Effekseer::RefPtr<Resource>;

using EmitterID = int32_t;
using ParticleGroupID = int64_t;
static constexpr int32_t InvalidID = -1;

public:
Expand All @@ -295,25 +294,23 @@ class GpuParticles : public ReferenceObject

virtual ResourceRef CreateResource(const ParamSet& paramSet, const Effect* effect) { return nullptr; }

virtual EmitterID NewEmitter(ResourceRef paramRes, ParticleGroupID groupID) { return InvalidID; }
virtual EmitterID NewEmitter(ResourceRef paramRes, Effekseer::InstanceGlobal* instanceGlobal) { return InvalidID; }

virtual void StartEmit(EmitterID emitterID) {}

virtual void StopEmit(EmitterID emitterID) {}

virtual void SetRandomSeed(EmitterID emitterID, uint32_t seed) {}

virtual void SetDeltaTime(EmitterID emitterID, float deltaTime) {}

virtual void SetTransform(EmitterID emitterID, const Effekseer::Matrix43& transform) {}

virtual void SetColor(EmitterID emitterID, Effekseer::Color color) {}

virtual void KillParticles(ParticleGroupID groupID) {}
virtual void SetDeltaTime(Effekseer::InstanceGlobal* instanceGlobal, float deltaTime) {}

virtual int32_t GetParticleCount(ParticleGroupID groupID) { return 0; }
virtual void KillParticles(Effekseer::InstanceGlobal* instanceGlobal) {}

virtual void ResetDeltaTime() {}
virtual int32_t GetParticleCount(Effekseer::InstanceGlobal* instanceGlobal) { return 0; }

protected:
Settings m_settings;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ void GpuParticles::ComputeFrame()
command.ThreadCount = { 1, 1, 1 };
graphics->Dispatch(command);
}
else if (GetEmitterParticleCount(emitter, paramSet) == 0)
else if (emitter.data.TimeCount >= paramSet.Basic.EmitOffset && GetEmitterParticleCount(emitter, paramSet) == 0)
{
FreeEmitter(emitterID);
}
Expand Down Expand Up @@ -658,7 +658,7 @@ GpuParticles::ResourceRef GpuParticles::CreateResource(const ParamSet& paramSet,
return resource;
}

GpuParticles::EmitterID GpuParticles::NewEmitter(ResourceRef resource, ParticleGroupID groupID)
GpuParticles::EmitterID GpuParticles::NewEmitter(ResourceRef resource, Effekseer::InstanceGlobal* instanceGlobal)
{
std::lock_guard<std::mutex> lock(m_mutex);

Expand All @@ -676,7 +676,7 @@ GpuParticles::EmitterID GpuParticles::NewEmitter(ResourceRef resource, ParticleG

Emitter& emitter = m_emitters[emitterID];
emitter.resource = resource.DownCast<Resource>();
emitter.groupID = groupID;
emitter.instanceGlobal = instanceGlobal;
emitter.data = {};
emitter.data.Color = 0xFFFFFFFF;
emitter.data.Transform = {
Expand Down Expand Up @@ -718,7 +718,7 @@ void GpuParticles::FreeEmitter(EmitterID emitterID)

Emitter& emitter = m_emitters[emitterID];
emitter.resource = nullptr;
emitter.groupID = 0;
emitter.instanceGlobal = nullptr;
emitter.data.FlagBits = 0;

m_particleAllocator.Deallocate({ emitter.data.ParticleHead, emitter.data.ParticleSize });
Expand Down Expand Up @@ -749,13 +749,6 @@ void GpuParticles::SetRandomSeed(EmitterID emitterID, uint32_t seed)
emitter.data.Seed = seed;
}

void GpuParticles::SetDeltaTime(EmitterID emitterID, float deltaTime)
{
assert(emitterID >= 0 && emitterID < m_emitters.size());
Emitter& emitter = m_emitters[emitterID];
emitter.data.DeltaTime = deltaTime;
}

void GpuParticles::SetTransform(EmitterID emitterID, const Effekseer::Matrix43& transform)
{
assert(emitterID >= 0 && emitterID < m_emitters.size());
Expand All @@ -774,42 +767,45 @@ void GpuParticles::SetColor(EmitterID emitterID, Effekseer::Color color)
emitter.data.Color = *reinterpret_cast<uint32_t*>(&color);
}

void GpuParticles::KillParticles(ParticleGroupID groupID)
void GpuParticles::SetDeltaTime(Effekseer::InstanceGlobal* instanceGlobal, float deltaTime)
{
std::lock_guard<std::mutex> lock(m_mutex);

for (EmitterID emitterID = 0; emitterID < (EmitterID)m_emitters.size(); emitterID++)
{
auto& emitter = m_emitters[emitterID];
if (emitter.IsAlive() && emitter.groupID == groupID)
if (emitter.IsAlive() && emitter.instanceGlobal == instanceGlobal)
{
FreeEmitter(emitterID);
emitter.data.DeltaTime = deltaTime;
}
}
}

int32_t GpuParticles::GetParticleCount(ParticleGroupID groupID)
void GpuParticles::KillParticles(Effekseer::InstanceGlobal* instanceGlobal)
{
int32_t count = 0;
std::lock_guard<std::mutex> lock(m_mutex);

for (EmitterID emitterID = 0; emitterID < (EmitterID)m_emitters.size(); emitterID++)
{
auto& emitter = m_emitters[emitterID];
if (emitter.IsAlive() && emitter.groupID == groupID)
if (emitter.IsAlive() && emitter.instanceGlobal == instanceGlobal)
{
count += GetEmitterParticleCount(emitter, emitter.resource->paramSet);
FreeEmitter(emitterID);
}
}
return count;
}

void GpuParticles::ResetDeltaTime()
int32_t GpuParticles::GetParticleCount(Effekseer::InstanceGlobal* instanceGlobal)
{
int32_t count = 0;

for (EmitterID emitterID = 0; emitterID < (EmitterID)m_emitters.size(); emitterID++)
{
auto& emitter = m_emitters[emitterID];
emitter.data.DeltaTime = 0.0f;
if (emitter.IsAlive() && emitter.instanceGlobal == instanceGlobal)
{
count += GetEmitterParticleCount(emitter, emitter.resource->paramSet);
}
}
return count;
}

GpuParticles::ParameterData GpuParticles::ToParamData(const ParamSet& paramSet)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class GpuParticles : public Effekseer::GpuParticles
EmitterData data;
UniformBufferRef buffer;
Effekseer::RefPtr<Resource> resource;
ParticleGroupID groupID;
Effekseer::InstanceGlobal* instanceGlobal;

bool IsAlive() const
{
Expand Down Expand Up @@ -112,25 +112,23 @@ class GpuParticles : public Effekseer::GpuParticles

virtual ResourceRef CreateResource(const ParamSet& paramSet, const Effekseer::Effect* effect) override;

virtual EmitterID NewEmitter(ResourceRef resource, ParticleGroupID groupID) override;
virtual EmitterID NewEmitter(ResourceRef resource, Effekseer::InstanceGlobal* instanceGlobal) override;

virtual void StartEmit(EmitterID emitterID) override;

virtual void StopEmit(EmitterID emitterID) override;

virtual void SetRandomSeed(EmitterID emitterID, uint32_t seed) override;

virtual void SetDeltaTime(EmitterID emitterID, float deltaTime) override;

virtual void SetTransform(EmitterID emitterID, const Effekseer::Matrix43& transform) override;

virtual void SetColor(EmitterID emitterID, Effekseer::Color color) override;

virtual void KillParticles(ParticleGroupID groupID) override;
virtual void SetDeltaTime(Effekseer::InstanceGlobal* instanceGlobal, float deltaTime) override;

virtual int32_t GetParticleCount(ParticleGroupID groupID) override;
virtual void KillParticles(Effekseer::InstanceGlobal* instanceGlobal) override;

virtual void ResetDeltaTime() override;
virtual int32_t GetParticleCount(Effekseer::InstanceGlobal* instanceGlobal) override;

protected:
PipelineStateRef CreatePiplineState(const ParamSet& paramSet);
Expand Down

0 comments on commit 3435046

Please sign in to comment.