ProfilingCaptureSystemComponent.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <AzCore/Component/Component.h>
  10. #include <AzCore/Component/TickBus.h>
  11. #include <Atom/Feature/Utils/ProfilingCaptureBus.h>
  12. namespace AZ
  13. {
  14. namespace RPI
  15. {
  16. class Pass;
  17. }
  18. namespace Render
  19. {
  20. //! Helper class that executes a function when a certain amount of frames have passed.
  21. class DelayedQueryCaptureHelper
  22. {
  23. using CaptureCallback = AZStd::function<void()>;
  24. enum DelayedCaptureState
  25. {
  26. Idle,
  27. Pending
  28. };
  29. // Wait 6 frames before calling the callback.
  30. const uint32_t FrameThreshold = 6u;
  31. public:
  32. //! Starts a delayed capture.
  33. bool StartCapture(CaptureCallback&& saveCallback);
  34. //! Decrements the threshold value, and executes the functor when the threshold reaches 0.
  35. void UpdateCapture();
  36. //! Returns whether the state is idle.
  37. bool IsIdle() const;
  38. private:
  39. uint32_t m_frameThreshold = 0u;
  40. DelayedCaptureState m_state = DelayedCaptureState::Idle;
  41. CaptureCallback m_captureCallback;
  42. };
  43. //! System component to handle ProfilingCaptureSystemComponent.
  44. class ProfilingCaptureSystemComponent final
  45. : public AZ::Component
  46. , public ProfilingCaptureRequestBus::Handler
  47. , public AZ::TickBus::Handler
  48. {
  49. public:
  50. AZ_COMPONENT(ProfilingCaptureSystemComponent, "{B715C113-E697-41D3-87BF-27D0ED1055BA}");
  51. static void Reflect(AZ::ReflectContext* context);
  52. void Activate() override;
  53. void Deactivate() override;
  54. // ProfilingCaptureRequestBus overrides...
  55. bool CapturePassTimestamp(const AZStd::string& outputFilePath) override;
  56. bool CaptureCpuFrameTime(const AZStd::string& outputFilePath) override;
  57. bool CapturePassPipelineStatistics(const AZStd::string& outputFilePath) override;
  58. bool CaptureBenchmarkMetadata(const AZStd::string& benchmarkName, const AZStd::string& outputFilePath) override;
  59. private:
  60. void OnTick(float deltaTime, ScriptTimePoint time) override;
  61. // Recursively collect all the passes from the root pass.
  62. AZStd::vector<const RPI::Pass*> CollectPassesRecursively(const RPI::Pass* root) const;
  63. DelayedQueryCaptureHelper m_timestampCapture;
  64. DelayedQueryCaptureHelper m_cpuFrameTimeStatisticsCapture;
  65. DelayedQueryCaptureHelper m_pipelineStatisticsCapture;
  66. DelayedQueryCaptureHelper m_benchmarkMetadataCapture;
  67. };
  68. }
  69. }