PhysicsBasedAnimationToolkit 0.0.10
Cross-platform C++20 library of algorithms and data structures commonly used in computer graphics research on physically-based simulation.
Loading...
Searching...
No Matches
Profiling.h
Go to the documentation of this file.
1
9
10#ifndef PBAT_PROFILING_PROFILING_H
11#define PBAT_PROFILING_PROFILING_H
12
13#include "PhysicsBasedAnimationToolkitExport.h"
14
15#if defined(PBAT_HAS_TRACY_PROFILER) and not defined(__CUDACC__)
16 #define PBAT_CAN_USE_TRACY_CPP
17 #include <tracy/Tracy.hpp>
18 #define PBAT_PROFILE_SCOPE ZoneScoped
19 #define PBAT_PROFILE_NAMED_SCOPE(name) ZoneScopedN(name)
20 #define PBAT_PROFILE_SCOPED_LOG(txt, size) ZoneText(txt, size)
21 #define PBAT_PROFILE_LOG(txt, size) TracyMessage(txt, size)
22 #define PBAT_PROFILE_PLOT(name, value) TracyPlot(name, value)
23#else
24 #define PBAT_PROFILE_SCOPE
25 #define PBAT_PROFILE_NAMED_SCOPE(name)
26 #define PBAT_PROFILE_LOG(txt, size)
27 #define PBAT_PROFILE_SCOPED_LOG(txt, size)
28 #define PBAT_PROFILE_PLOT(name, value)
29#endif // defined(PBAT_HAS_TRACY_PROFILER) and not defined(__CUDACC__)
30
31#include <map>
32#include <string>
33#include <string_view>
34#include <type_traits>
35
36namespace pbat {
37namespace profiling {
38
45PBAT_API void BeginFrame(std::string_view name);
46
53PBAT_API void EndFrame(std::string_view name);
54
60PBAT_API bool IsConnectedToServer();
61
73template <class Func, class... Args>
74std::invoke_result_t<Func, Args...> Profile(std::string const& zoneName, Func&& f, Args&&... args)
75{
76#ifdef PBAT_CAN_USE_TRACY_CPP
77 static auto constexpr line = (uint32_t)TracyLine;
78 struct SourceLocationData
79 {
80 SourceLocationData(std::string_view zoneNameView)
81 : name(zoneNameView), function(TracyFunction), file(TracyFile), data()
82 {
83 data.name = name.data();
84 data.function = function.data();
85 data.file = file.data();
86 data.line = line;
87 data.color = 0;
88 }
89 std::string name;
90 std::string function;
91 std::string file;
92 tracy::SourceLocationData data;
93 };
94 static std::map<std::string, SourceLocationData> zones{};
95 auto it = zones.find(zoneName);
96 if (it == zones.end())
97 {
98 bool inserted{false};
99 std::tie(it, inserted) = zones.insert({zoneName, SourceLocationData(zoneName)});
100 assert(inserted);
101 }
102 SourceLocationData const& data = it->second;
103 tracy::ScopedZone zone(&(data.data));
104#endif // PBAT_CAN_USE_TRACY
105 return f(std::forward<Args>(args)...);
106}
107
108} // namespace profiling
109} // namespace pbat
110
111#endif // PBAT_PROFILING_PROFILING_H
The main namespace of the library.
Definition Aliases.h:15
std::invoke_result_t< Func, Args... > Profile(std::string const &zoneName, Func &&f, Args &&... args)
Profile a function as a Tracy named zone.
Definition Profiling.h:74