Skip to content

Commit

Permalink
Add stage feature of GpuTimer
Browse files Browse the repository at this point in the history
  • Loading branch information
ueshita committed Feb 24, 2024
1 parent 14a19e9 commit b025e7b
Show file tree
Hide file tree
Showing 7 changed files with 268 additions and 146 deletions.
93 changes: 41 additions & 52 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 Down Expand Up @@ -2248,6 +2232,11 @@ 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;
Expand Down
67 changes: 63 additions & 4 deletions Dev/Cpp/Effekseer/Effekseer/Renderer/Effekseer.GpuTimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,87 @@ namespace Effekseer
class GpuTimer;
using GpuTimerRef = RefPtr<GpuTimer>;

enum class GpuStage : uint8_t
{
None,
Compute,
Draw,
DrawBack,
DrawFront,
};

class GpuTimer : public ReferenceObject
{
public:
GpuTimer() = default;

virtual ~GpuTimer() = default;

virtual void BeginFrame() {}
virtual void BeginStage(GpuStage stage) {}

virtual void EndFrame() {}
virtual void EndStage(GpuStage stage) {}

virtual void AddTimer(const void* object) {}

virtual void RemoveTimer(const void* object) {}

virtual void Start(const void* object, uint32_t phase) {}
virtual void Start(const void* object) {}

virtual void Stop(const void* object, uint32_t phase) {}
virtual void Stop(const void* object) {}

virtual int32_t GetResult(const void* object) { return -1; }
};

class ScopedGpuStage
{
public:
ScopedGpuStage(GpuTimerRef timer, GpuStage stage)
: m_timer(timer), m_stage(stage)
{
if (m_timer)
{
m_timer->BeginStage(m_stage);
}
}

~ScopedGpuStage()
{
if (m_timer)
{
m_timer->EndStage(m_stage);
}
}

private:
GpuTimerRef m_timer;
GpuStage m_stage;
};

class ScopedGpuTime
{
public:
ScopedGpuTime(GpuTimerRef timer, const void* object)
: m_timer(timer), m_object(object)
{
if (m_timer)
{
m_timer->Start(m_object);
}
}

~ScopedGpuTime()
{
if (m_timer)
{
m_timer->Stop(m_object);
}
}

private:
GpuTimerRef m_timer;
const void* m_object;
};

} // namespace Effekseer

#endif // __EFFEKSEER_GPU_TIMER_H__
Loading

0 comments on commit b025e7b

Please sign in to comment.