From 3435046b9f9d9918a004c97ece105f083509c912 Mon Sep 17 00:00:00 2001 From: ueshita Date: Sun, 11 Feb 2024 00:15:02 +0900 Subject: [PATCH] Fix an issue where GpuParticles would stop when the parent instance killed --- .../Effekseer/Effekseer.Instance.cpp | 6 +-- .../Effekseer/Effekseer.InstanceGlobal.h | 2 + .../Effekseer/Effekseer/Effekseer.Manager.cpp | 14 +++++-- .../Renderer/Effekseer.GpuParticles.h | 11 ++--- .../EffekseerRenderer.GpuParticles.cpp | 42 +++++++++---------- .../EffekseerRenderer.GpuParticles.h | 12 +++--- 6 files changed, 43 insertions(+), 44 deletions(-) diff --git a/Dev/Cpp/Effekseer/Effekseer/Effekseer.Instance.cpp b/Dev/Cpp/Effekseer/Effekseer/Effekseer.Instance.cpp index 24878dc559..021a5a5611 100644 --- a/Dev/Cpp/Effekseer/Effekseer/Effekseer.Instance.cpp +++ b/Dev/Cpp/Effekseer/Effekseer/Effekseer.Instance.cpp @@ -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) { @@ -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(); @@ -469,6 +467,8 @@ void Instance::Update(float deltaFrame, bool shown) { gpuParticles->SetColor(m_gpuEmitterID, ColorInheritance); } + + GetInstanceGlobal()->IsUsingGpuParticles = true; } } diff --git a/Dev/Cpp/Effekseer/Effekseer/Effekseer.InstanceGlobal.h b/Dev/Cpp/Effekseer/Effekseer/Effekseer.InstanceGlobal.h index 2e65dd3654..7568477670 100644 --- a/Dev/Cpp/Effekseer/Effekseer/Effekseer.InstanceGlobal.h +++ b/Dev/Cpp/Effekseer/Effekseer/Effekseer.InstanceGlobal.h @@ -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); diff --git a/Dev/Cpp/Effekseer/Effekseer/Effekseer.Manager.cpp b/Dev/Cpp/Effekseer/Effekseer/Effekseer.Manager.cpp index ed5f3b6a3b..de386c2f72 100644 --- a/Dev/Cpp/Effekseer/Effekseer/Effekseer.Manager.cpp +++ b/Dev/Cpp/Effekseer/Effekseer/Effekseer.Manager.cpp @@ -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; } @@ -370,7 +370,7 @@ void ManagerImplemented::ExecuteEvents() if (GetGpuParticles() != nullptr) { - GetGpuParticles()->KillParticles((GpuParticles::ParticleGroupID)ds.second.GlobalPointer); + GetGpuParticles()->KillParticles(ds.second.GlobalPointer); } } @@ -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(); } @@ -1786,8 +1794,6 @@ void ManagerImplemented::Compute() { gpuParticles->ComputeFrame(); } - - gpuParticles->ResetDeltaTime(); } } diff --git a/Dev/Cpp/Effekseer/Effekseer/Renderer/Effekseer.GpuParticles.h b/Dev/Cpp/Effekseer/Effekseer/Renderer/Effekseer.GpuParticles.h index 499f3625d2..30684b118d 100644 --- a/Dev/Cpp/Effekseer/Effekseer/Renderer/Effekseer.GpuParticles.h +++ b/Dev/Cpp/Effekseer/Effekseer/Renderer/Effekseer.GpuParticles.h @@ -279,7 +279,6 @@ class GpuParticles : public ReferenceObject using ResourceRef = Effekseer::RefPtr; using EmitterID = int32_t; - using ParticleGroupID = int64_t; static constexpr int32_t InvalidID = -1; public: @@ -295,7 +294,7 @@ 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) {} @@ -303,17 +302,15 @@ class GpuParticles : public ReferenceObject 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; diff --git a/Dev/Cpp/EffekseerRendererCommon/EffekseerRendererCommon/EffekseerRenderer.GpuParticles.cpp b/Dev/Cpp/EffekseerRendererCommon/EffekseerRendererCommon/EffekseerRenderer.GpuParticles.cpp index 4ae905c5c6..995328f35a 100644 --- a/Dev/Cpp/EffekseerRendererCommon/EffekseerRendererCommon/EffekseerRenderer.GpuParticles.cpp +++ b/Dev/Cpp/EffekseerRendererCommon/EffekseerRendererCommon/EffekseerRenderer.GpuParticles.cpp @@ -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); } @@ -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 lock(m_mutex); @@ -676,7 +676,7 @@ GpuParticles::EmitterID GpuParticles::NewEmitter(ResourceRef resource, ParticleG Emitter& emitter = m_emitters[emitterID]; emitter.resource = resource.DownCast(); - emitter.groupID = groupID; + emitter.instanceGlobal = instanceGlobal; emitter.data = {}; emitter.data.Color = 0xFFFFFFFF; emitter.data.Transform = { @@ -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 }); @@ -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()); @@ -774,42 +767,45 @@ void GpuParticles::SetColor(EmitterID emitterID, Effekseer::Color color) emitter.data.Color = *reinterpret_cast(&color); } -void GpuParticles::KillParticles(ParticleGroupID groupID) +void GpuParticles::SetDeltaTime(Effekseer::InstanceGlobal* instanceGlobal, float deltaTime) { - std::lock_guard 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 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) diff --git a/Dev/Cpp/EffekseerRendererCommon/EffekseerRendererCommon/EffekseerRenderer.GpuParticles.h b/Dev/Cpp/EffekseerRendererCommon/EffekseerRendererCommon/EffekseerRenderer.GpuParticles.h index ce4fc8de20..25bafe91af 100644 --- a/Dev/Cpp/EffekseerRendererCommon/EffekseerRendererCommon/EffekseerRenderer.GpuParticles.h +++ b/Dev/Cpp/EffekseerRendererCommon/EffekseerRendererCommon/EffekseerRenderer.GpuParticles.h @@ -51,7 +51,7 @@ class GpuParticles : public Effekseer::GpuParticles EmitterData data; UniformBufferRef buffer; Effekseer::RefPtr resource; - ParticleGroupID groupID; + Effekseer::InstanceGlobal* instanceGlobal; bool IsAlive() const { @@ -112,7 +112,7 @@ 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; @@ -120,17 +120,15 @@ class GpuParticles : public Effekseer::GpuParticles 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);