Skip to content

Commit

Permalink
Fix gpu timer (#999)
Browse files Browse the repository at this point in the history
* Rename GPUTime -> GpuTime

* Add stage feature of GpuTimer
  • Loading branch information
ueshita authored Feb 24, 2024
1 parent af65c56 commit bbd47eb
Show file tree
Hide file tree
Showing 21 changed files with 345 additions and 223 deletions.
97 changes: 43 additions & 54 deletions Dev/Cpp/Effekseer/Effekseer/Effekseer.Manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,17 @@ GpuTimerRef ManagerImplemented::GetGpuTimer()

void ManagerImplemented::SetGpuTimer(GpuTimerRef gpuTimer)
{
if (m_gpuTimer && m_gpuParticleSystem)
{
m_gpuTimer->RemoveTimer(m_gpuParticleSystem.Get());
}

m_gpuTimer = gpuTimer;

if (m_gpuTimer && m_gpuParticleSystem)
{
m_gpuTimer->AddTimer(m_gpuParticleSystem.Get());
}
}

GpuParticleSystemRef ManagerImplemented::GetGpuParticleSystem()
Expand All @@ -647,7 +657,17 @@ GpuParticleSystemRef ManagerImplemented::GetGpuParticleSystem()

void ManagerImplemented::SetGpuParticleSystem(GpuParticleSystemRef system)
{
if (m_gpuTimer && m_gpuParticleSystem)
{
m_gpuTimer->RemoveTimer(m_gpuParticleSystem.Get());
}

m_gpuParticleSystem = system;

if (m_gpuTimer && m_gpuParticleSystem)
{
m_gpuTimer->AddTimer(m_gpuParticleSystem.Get());
}
}

GpuParticleFactoryRef ManagerImplemented::GetGpuParticleFactory()
Expand Down Expand Up @@ -1798,8 +1818,12 @@ void ManagerImplemented::ResetAndPlayWithDataSet(DrawSet& drawSet, float frame)

void ManagerImplemented::Compute()
{
ScopedGpuStage gpuPass(m_gpuTimer, GpuStage::Compute);

if (auto gpuParticleSystem = GetGpuParticleSystem())
{
ScopedGpuTime gpuTime(m_gpuTimer, gpuParticleSystem.Get());

for (int i = 0; i < m_nextComputeCount; i++)
{
gpuParticleSystem->ComputeFrame();
Expand All @@ -1821,10 +1845,7 @@ void ManagerImplemented::Draw(const Manager::DrawParameter& drawParameter)
// start to record a time
int64_t beginTime = ::Effekseer::GetTime();

if (m_gpuTimer != nullptr)
{
m_gpuTimer->BeginFrame();
}
ScopedGpuStage gpuPass(m_gpuTimer, GpuStage::Draw);

const auto cullingPlanes = GeometryUtility::CalculateFrustumPlanes(drawParameter.ViewProjectionMatrix, drawParameter.ZNear, drawParameter.ZFar, GetSetting()->GetCoordinateSystem());

Expand All @@ -1837,8 +1858,7 @@ void ManagerImplemented::Draw(const Manager::DrawParameter& drawParameter)

if (drawSet.IsAutoDrawing)
{
if (m_gpuTimer != nullptr)
m_gpuTimer->Start(drawSet.GlobalPointer, 0);
ScopedGpuTime gpuTime(m_gpuTimer, drawSet.GlobalPointer);

if (drawSet.GlobalPointer->RenderedInstanceContainers.size() > 0)
{
Expand All @@ -1854,9 +1874,6 @@ void ManagerImplemented::Draw(const Manager::DrawParameter& drawParameter)
{
drawSet.InstanceContainerPointer->Draw(true);
}

if (m_gpuTimer != nullptr)
m_gpuTimer->Stop(drawSet.GlobalPointer, 0);
}
};

Expand All @@ -1879,12 +1896,9 @@ void ManagerImplemented::Draw(const Manager::DrawParameter& drawParameter)

if (auto gpuParticleSystem = GetGpuParticleSystem())
{
gpuParticleSystem->RenderFrame();
}
ScopedGpuTime gpuTime(m_gpuTimer, gpuParticleSystem.Get());

if (m_gpuTimer != nullptr)
{
m_gpuTimer->EndFrame();
gpuParticleSystem->RenderFrame();
}

// calculate a time
Expand All @@ -1898,6 +1912,8 @@ void ManagerImplemented::DrawBack(const Manager::DrawParameter& drawParameter)
// start to record a time
int64_t beginTime = ::Effekseer::GetTime();

ScopedGpuStage gpuPass(m_gpuTimer, GpuStage::DrawBack);

const auto cullingPlanes = GeometryUtility::CalculateFrustumPlanes(drawParameter.ViewProjectionMatrix, drawParameter.ZNear, drawParameter.ZFar, GetSetting()->GetCoordinateSystem());

const auto render = [this, &drawParameter, &cullingPlanes](DrawSet& drawSet) -> void
Expand All @@ -1909,8 +1925,7 @@ void ManagerImplemented::DrawBack(const Manager::DrawParameter& drawParameter)

if (drawSet.IsAutoDrawing)
{
if (m_gpuTimer != nullptr)
m_gpuTimer->Start(drawSet.GlobalPointer, 0);
ScopedGpuTime gpuTime(m_gpuTimer, drawSet.GlobalPointer);

auto e = (EffectImplemented*)drawSet.ParameterPointer.Get();
for (int32_t j = 0; j < e->renderingNodesThreshold; j++)
Expand All @@ -1920,9 +1935,6 @@ void ManagerImplemented::DrawBack(const Manager::DrawParameter& drawParameter)

drawSet.GlobalPointer->RenderedInstanceContainers[j]->Draw(false);
}

if (m_gpuTimer != nullptr)
m_gpuTimer->Stop(drawSet.GlobalPointer, 0);
}
};

Expand Down Expand Up @@ -1953,11 +1965,8 @@ void ManagerImplemented::DrawFront(const Manager::DrawParameter& drawParameter)

// start to record a time
int64_t beginTime = ::Effekseer::GetTime();

if (m_gpuTimer != nullptr)
{
m_gpuTimer->BeginFrame();
}

ScopedGpuStage gpuPass(m_gpuTimer, GpuStage::DrawFront);

const auto cullingPlanes = GeometryUtility::CalculateFrustumPlanes(drawParameter.ViewProjectionMatrix, drawParameter.ZNear, drawParameter.ZFar, GetSetting()->GetCoordinateSystem());

Expand All @@ -1970,8 +1979,7 @@ void ManagerImplemented::DrawFront(const Manager::DrawParameter& drawParameter)

if (drawSet.IsAutoDrawing)
{
if (m_gpuTimer != nullptr)
m_gpuTimer->Start(drawSet.GlobalPointer, 1);
ScopedGpuTime gpuTime(m_gpuTimer, drawSet.GlobalPointer);

if (drawSet.GlobalPointer->RenderedInstanceContainers.size() > 0)
{
Expand All @@ -1988,9 +1996,6 @@ void ManagerImplemented::DrawFront(const Manager::DrawParameter& drawParameter)
{
drawSet.InstanceContainerPointer->Draw(true);
}

if (m_gpuTimer != nullptr)
m_gpuTimer->Stop(drawSet.GlobalPointer, 1);
}
};

Expand All @@ -2013,12 +2018,9 @@ void ManagerImplemented::DrawFront(const Manager::DrawParameter& drawParameter)

if (auto gpuParticleSystem = GetGpuParticleSystem())
{
gpuParticleSystem->RenderFrame();
}
ScopedGpuTime gpuTime(m_gpuTimer, gpuParticleSystem.Get());

if (m_gpuTimer != nullptr)
{
m_gpuTimer->EndFrame();
gpuParticleSystem->RenderFrame();
}

// calculate a time
Expand Down Expand Up @@ -2111,9 +2113,6 @@ void ManagerImplemented::DrawHandle(Handle handle, const Manager::DrawParameter&
return;
}

if (m_gpuTimer != nullptr)
m_gpuTimer->Start(drawSet.GlobalPointer, 0);

if (drawSet.GlobalPointer->RenderedInstanceContainers.size() > 0)
{
for (auto& c : drawSet.GlobalPointer->RenderedInstanceContainers)
Expand All @@ -2128,9 +2127,6 @@ void ManagerImplemented::DrawHandle(Handle handle, const Manager::DrawParameter&
{
drawSet.InstanceContainerPointer->Draw(true);
}

if (m_gpuTimer != nullptr)
m_gpuTimer->Stop(drawSet.GlobalPointer, 0);
}
}

Expand All @@ -2156,19 +2152,13 @@ void ManagerImplemented::DrawHandleBack(Handle handle, const Manager::DrawParame
return;
}

if (m_gpuTimer != nullptr)
m_gpuTimer->Start(drawSet.GlobalPointer, 0);

for (int32_t i = 0; i < e->renderingNodesThreshold; i++)
{
if (IsClippedWithDepth(drawSet, drawSet.GlobalPointer->RenderedInstanceContainers[i], drawParameter))
continue;

drawSet.GlobalPointer->RenderedInstanceContainers[i]->Draw(false);
}

if (m_gpuTimer != nullptr)
m_gpuTimer->Stop(drawSet.GlobalPointer, 0);
}
}

Expand All @@ -2194,9 +2184,6 @@ void ManagerImplemented::DrawHandleFront(Handle handle, const Manager::DrawParam
return;
}

if (m_gpuTimer != nullptr)
m_gpuTimer->Start(drawSet.GlobalPointer, 1);

if (drawSet.GlobalPointer->RenderedInstanceContainers.size() > 0)
{
for (size_t i = e->renderingNodesThreshold; i < drawSet.GlobalPointer->RenderedInstanceContainers.size(); i++)
Expand All @@ -2211,9 +2198,6 @@ void ManagerImplemented::DrawHandleFront(Handle handle, const Manager::DrawParam
{
drawSet.InstanceContainerPointer->Draw(true);
}

if (m_gpuTimer != nullptr)
m_gpuTimer->Stop(drawSet.GlobalPointer, 1);
}
}

Expand All @@ -2239,7 +2223,7 @@ int ManagerImplemented::GetDrawTime() const
return m_drawTime;
}

int32_t ManagerImplemented::GetGPUTime() const
int32_t ManagerImplemented::GetGpuTime() const
{
if (m_gpuTimer != nullptr)
{
Expand All @@ -2248,12 +2232,17 @@ int32_t ManagerImplemented::GetGPUTime() const
{
timeCount += m_gpuTimer->GetResult(kv.second.GlobalPointer);
}

if (m_gpuParticleSystem)
{
timeCount += m_gpuTimer->GetResult(m_gpuParticleSystem.Get());
}
return timeCount;
}
return 0;
}

int32_t ManagerImplemented::GetGPUTime(Handle handle) const
int32_t ManagerImplemented::GetGpuTime(Handle handle) const
{
if (m_gpuTimer != nullptr)
{
Expand Down
4 changes: 2 additions & 2 deletions Dev/Cpp/Effekseer/Effekseer/Effekseer.Manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -960,14 +960,14 @@ class Manager : public IReference
\~English Gets the GPU time (microseconds) taken to render the all effects.
\~Japanese エフェクト全ての描画処理にかかるGPU時間(マイクロ秒)を取得する。
*/
virtual int32_t GetGPUTime() const = 0;
virtual int32_t GetGpuTime() const = 0;

/**
@brief
\~English Gets the GPU time (microseconds) taken to render the effect.
\~Japanese エフェクトの描画処理にかかるGPU時間(マイクロ秒)を取得する。
*/
virtual int32_t GetGPUTime(Handle handle) const = 0;
virtual int32_t GetGpuTime(Handle handle) const = 0;

/**
@brief
Expand Down
4 changes: 2 additions & 2 deletions Dev/Cpp/Effekseer/Effekseer/Effekseer.ManagerImplemented.h
Original file line number Diff line number Diff line change
Expand Up @@ -453,9 +453,9 @@ class ManagerImplemented : public Manager, public ReferenceObject

int GetDrawTime() const override;

int32_t GetGPUTime() const override;
int32_t GetGpuTime() const override;

int32_t GetGPUTime(Handle handle) const override;
int32_t GetGpuTime(Handle handle) const override;

int32_t GetRestInstancesCount() const override;

Expand Down
6 changes: 3 additions & 3 deletions Dev/Cpp/Effekseer/Effekseer/Network/Effekseer.Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ void ClientImplemented::OnProfileSample(const Session::Message& msg)
for (auto fbManager : *fb->managers())
{
ProfileSample::Manager profileManager;
profileManager.CPUTime = fbManager->cpu_time();
profileManager.GPUTime = fbManager->gpu_time();
profileManager.CpuTime = fbManager->cpu_time();
profileManager.GpuTime = fbManager->gpu_time();
profileManager.HandleCount = fbManager->handle_count();
profileSample.Managers.emplace_back(profileManager);
}
Expand All @@ -129,7 +129,7 @@ void ClientImplemented::OnProfileSample(const Session::Message& msg)
{
ProfileSample::Effect profileEffect;
profileEffect.Key.assign((const char16_t*)fbEffect->key()->data(), (size_t)fbEffect->key()->size());
profileEffect.GPUTime = fbEffect->gpu_time();
profileEffect.GpuTime = fbEffect->gpu_time();
profileEffect.HandleCount = fbEffect->handle_count();
profileSample.Effects.emplace_back(profileEffect);
}
Expand Down
6 changes: 3 additions & 3 deletions Dev/Cpp/Effekseer/Effekseer/Network/Effekseer.Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ class Client : public IReference
struct Manager
{
uint32_t HandleCount = 0;
float CPUTime = 0.0f;
float GPUTime = 0.0f;
float CpuTime = 0.0f;
float GpuTime = 0.0f;
};
std::vector<Manager> Managers;

struct Effect
{
std::u16string Key;
uint32_t HandleCount = 0;
float GPUTime = 0.0f;
float GpuTime = 0.0f;
};
std::vector<Effect> Effects;
};
Expand Down
4 changes: 2 additions & 2 deletions Dev/Cpp/Effekseer/Effekseer/Network/Effekseer.Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ void ServerImplemented::UpdateProfiler(InternalClient& client)
if (handleAndDrawSet.second.ParameterPointer == profile.effect)
{
profile.handleCount += 1;
profile.gpuTime += manager->GetGPUTime(handleAndDrawSet.first);
profile.gpuTime += manager->GetGpuTime(handleAndDrawSet.first);
break;
}
}
Expand All @@ -227,7 +227,7 @@ void ServerImplemented::UpdateProfiler(InternalClient& client)
fbManagers.emplace_back(Data::CreateNetworkManagerProfile(fbb,
(uint32_t)drawSets.size(),
manager->GetUpdateTime() + manager->GetDrawTime(),
manager->GetGPUTime()));
manager->GetGpuTime()));
}

for (auto& profile : effectProfiles)
Expand Down
Loading

0 comments on commit bbd47eb

Please sign in to comment.