TracyProfilerEventForwarder.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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/TickBus.h>
  10. #include <AzCore/Debug/Profiler.h>
  11. #include <AzCore/Memory/SystemAllocator.h>
  12. #include <AzCore/Name/Name.h>
  13. #include <AzCore/RTTI/RTTI.h>
  14. #include <AzCore/std/containers/vector.h>
  15. #include <AzCore/std/parallel/mutex.h>
  16. #include <AzCore/std/parallel/shared_mutex.h>
  17. #include <tracy/TracyC.h>
  18. namespace TracyProfiler
  19. {
  20. //! Listen to 03DE frame/profiling events and forward them to the Tracy profiling library
  21. class TracyProfilerEventForwarder final
  22. : public AZ::Debug::Profiler
  23. , public AZ::TickBus::Handler
  24. {
  25. public:
  26. AZ_RTTI(TracyProfilerEventForwarder, "{9467E3F6-0581-4E46-A98A-F3C249FD7B24}", AZ::Debug::Profiler);
  27. AZ_CLASS_ALLOCATOR(TracyProfilerEventForwarder, AZ::SystemAllocator);
  28. TracyProfilerEventForwarder() = default;
  29. ~TracyProfilerEventForwarder() = default;
  30. //! Registers/un-registers the AZ::Debug::Profiler instance to the interface
  31. void Init();
  32. void Shutdown();
  33. //! AZ::Debug::Profiler overrides...
  34. void BeginRegion(const AZ::Debug::Budget* budget, const char* eventName, ...) final override;
  35. void EndRegion(const AZ::Debug::Budget* budget) final override;
  36. //! AZ::TickBus::Handler overrides
  37. int GetTickOrder() override;
  38. void OnTick(float deltaTime, AZ::ScriptTimePoint timePoint) override;
  39. private:
  40. using EventIdStack = AZStd::vector<TracyCZoneCtx>;
  41. static thread_local EventIdStack ms_threadLocalStorage;
  42. // This lock will only be contested when the TracyProfilerEventForwarder's Shutdown() method has been called
  43. AZStd::shared_mutex m_shutdownMutex;
  44. bool m_initialized = false;
  45. };
  46. } // namespace TracyProfiler