Atom Tools: Moved performance monitor system component from ME to ATF

Moved the performance monitor system component and metrics gathering from the material editor into atom tools framework so it can be reused and extended by other applications.
Replaced the custom performance monitor docked window in the material editor with status bar widgets that are always visible and take up no screen real estate. This could possibly be moved to the base application class or rendered on top of the viewport.

Signed-off-by: Guthrie Adams <guthadam@amazon.com>
This commit is contained in:
Guthrie Adams
2022-01-24 22:17:01 -06:00
parent e1cb40fa33
commit 191db77e4d
14 changed files with 136 additions and 330 deletions
@@ -10,6 +10,7 @@
#include <AtomToolsFrameworkSystemComponent.h>
#include <Document/AtomToolsDocumentSystemComponent.h>
#include <Window/AtomToolsMainWindowSystemComponent.h>
#include <PerformanceMonitor/PerformanceMonitorSystemComponent.h>
#include <PreviewRenderer/PreviewRendererSystemComponent.h>
namespace AtomToolsFramework
@@ -20,6 +21,7 @@ namespace AtomToolsFramework
AtomToolsFrameworkSystemComponent::CreateDescriptor(),
AtomToolsDocumentSystemComponent::CreateDescriptor(),
AtomToolsMainWindowSystemComponent::CreateDescriptor(),
PerformanceMonitorSystemComponent::CreateDescriptor(),
PreviewRendererSystemComponent::CreateDescriptor(),
});
}
@@ -30,6 +32,7 @@ namespace AtomToolsFramework
azrtti_typeid<AtomToolsFrameworkSystemComponent>(),
azrtti_typeid<AtomToolsDocumentSystemComponent>(),
azrtti_typeid<AtomToolsMainWindowSystemComponent>(),
azrtti_typeid<PerformanceMonitorSystemComponent>(),
azrtti_typeid<PreviewRendererSystemComponent>(),
};
}
@@ -0,0 +1,118 @@
/*
* Copyright (c) Contributors to the Open 3D Engine Project.
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
*
* SPDX-License-Identifier: Apache-2.0 OR MIT
*
*/
#include <Atom/RHI/RHISystemInterface.h>
#include <Atom/RPI.Public/Pass/ParentPass.h>
#include <Atom/RPI.Public/Pass/PassSystemInterface.h>
#include <AzCore/Serialization/SerializeContext.h>
#include <PerformanceMonitor/PerformanceMonitorSystemComponent.h>
namespace AtomToolsFramework
{
void PerformanceMonitorSystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
{
provided.push_back(AZ_CRC_CE("PerformanceMonitorService"));
}
void PerformanceMonitorSystemComponent::Reflect(AZ::ReflectContext* context)
{
if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
{
serialize->Class<PerformanceMonitorSystemComponent, AZ::Component>()->Version(0);
}
}
void PerformanceMonitorSystemComponent::Init()
{
}
void PerformanceMonitorSystemComponent::Activate()
{
AZ::TickBus::Handler::BusConnect();
PerformanceMonitorRequestBus::Handler::BusConnect();
}
void PerformanceMonitorSystemComponent::Deactivate()
{
PerformanceMonitorRequestBus::Handler::BusDisconnect();
AZ::TickBus::Handler::BusDisconnect();
}
void PerformanceMonitorSystemComponent::OnTick([[maybe_unused]] float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint time)
{
if (!m_profilingEnabled)
{
return;
}
if (++m_sample > SampleCount)
{
m_sample = 0;
UpdateMetrics();
ResetStats();
}
double frameTime = AZ::RHI::RHISystemInterface::Get()->GetCpuFrameTime();
if (frameTime > 0)
{
m_cpuFrameTimeMs.PushSample(frameTime);
}
AZ::RHI::Ptr<AZ::RPI::ParentPass> rootPass = AZ::RPI::PassSystemInterface::Get()->GetRootPass();
if (rootPass)
{
AZ::RPI::TimestampResult timestampResult = rootPass->GetLatestTimestampResult();
double gpuFrameTimeMs = aznumeric_cast<double>(timestampResult.GetDurationInNanoseconds()) / 1000000;
m_gpuFrameTimeMs.PushSample(gpuFrameTimeMs);
}
}
void PerformanceMonitorSystemComponent::SetProfilerEnabled(bool enabled)
{
if (m_profilingEnabled == enabled)
{
return;
}
AZ::RHI::Ptr<AZ::RPI::ParentPass> rootPass = AZ::RPI::PassSystemInterface::Get()->GetRootPass();
if (rootPass)
{
rootPass->SetTimestampQueryEnabled(enabled);
}
else
{
AZ_Error("PerformanceMonitorSystemComponent", false, "Failed to find root pass.");
}
if (enabled)
{
ResetStats();
}
m_profilingEnabled = enabled;
}
const PerformanceMetrics& PerformanceMonitorSystemComponent::GetMetrics()
{
UpdateMetrics();
return m_metrics;
}
void PerformanceMonitorSystemComponent::UpdateMetrics()
{
m_metrics.m_cpuFrameTimeMs = m_cpuFrameTimeMs.GetAverage();
m_metrics.m_gpuFrameTimeMs = m_gpuFrameTimeMs.GetAverage();
}
void PerformanceMonitorSystemComponent::ResetStats()
{
m_cpuFrameTimeMs.Reset();
m_gpuFrameTimeMs.Reset();
}
} // namespace AtomToolsFramework
@@ -0,0 +1,61 @@
/*
* Copyright (c) Contributors to the Open 3D Engine Project.
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
*
* SPDX-License-Identifier: Apache-2.0 OR MIT
*
*/
#pragma once
#include <Atom/RPI.Public/Pass/Pass.h>
#include <AzCore/Component/Component.h>
#include <AzCore/Component/TickBus.h>
#include <AzCore/Statistics/RunningStatistic.h>
#include <AtomToolsFramework/PerformanceMonitor/PerformanceMonitorRequestBus.h>
namespace AtomToolsFramework
{
//! PerformanceMonitorSystemComponent monitors performance
class PerformanceMonitorSystemComponent
: public AZ::Component
, private PerformanceMonitorRequestBus::Handler
, private AZ::TickBus::Handler
{
public:
AZ_COMPONENT(PerformanceMonitorSystemComponent, "{C2F54D1B-A106-4922-82BE-ACB7A168D4AF}");
static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided);
static void Reflect(AZ::ReflectContext* context);
PerformanceMonitorSystemComponent() = default;
~PerformanceMonitorSystemComponent() = default;
private:
// AZ::Component overrides...
void Init() override;
void Activate() override;
void Deactivate() override;
// AZ::TickBus::Handler overrides...
void OnTick(float deltaTime, AZ::ScriptTimePoint time) override;
// PerformanceMonitorRequestBus::Handler overrides...
void SetProfilerEnabled(bool enabled) override;
const PerformanceMetrics& GetMetrics() override;
void UpdateMetrics();
void ResetStats();
bool m_profilingEnabled = false;
AZ::Statistics::RunningStatistic m_cpuFrameTimeMs;
AZ::Statistics::RunningStatistic m_gpuFrameTimeMs;
PerformanceMetrics m_metrics;
// Number of samples to average for each metric
static constexpr int SampleCount = 10;
int m_sample = 0;
};
} // namespace AtomToolsFramework