Helios Engine
A modular ECS based data-oriented C++23 game engine framework
Loading...
Searching...
No Matches
zone.cpp
Go to the documentation of this file.
1#include <pch.hpp>
2
4
6
7#include <cstddef>
8#include <cstdint>
9#include <span>
10#include <string_view>
11
12namespace helios::profile {
13
14namespace {
15
16thread_local std::span<std::byte> g_current_zone_storage = {};
17thread_local bool g_zone_annotations_suppressed = false;
18
19} // namespace
20
21ScopedZone::ScopedZone(const ZoneSpec& spec) noexcept
22 : ScopedZone(spec, spec.active) {}
23
24ScopedZone::ScopedZone(const ZoneSpec& spec, bool active) noexcept
25 : active_(active && Profiler::Instance().Finalized()) {
26 previous_storage_ = g_current_zone_storage;
27 previous_annotations_suppressed_ = g_zone_annotations_suppressed;
28 if (!active_) [[unlikely]] {
29 g_current_zone_storage = {};
30 g_zone_annotations_suppressed = true;
31 return;
32 }
33
34 const auto storage = std::span<std::byte>{storage_};
35 Profiler::Instance().BeginZone(spec, storage);
36 g_current_zone_storage = storage;
37}
38
40 if (!active_) [[unlikely]] {
41 g_current_zone_storage = previous_storage_;
42 g_zone_annotations_suppressed = previous_annotations_suppressed_;
43 return;
44 }
45
46 const auto storage = std::span<std::byte>{storage_};
47 Profiler::Instance().EndZone(storage);
48 g_current_zone_storage = previous_storage_;
49 g_zone_annotations_suppressed = previous_annotations_suppressed_;
50}
51
52void ScopedZone::SetText(std::string_view text) noexcept {
53 if (!active_) [[unlikely]] {
54 return;
55 }
56 Profiler::Instance().ZoneText(storage_, text);
57}
58
59void ScopedZone::SetValue(uint64_t value) noexcept {
60 if (!active_) [[unlikely]] {
61 return;
62 }
63 Profiler::Instance().ZoneValue(storage_, value);
64}
65
66void ScopedZone::SetName(std::string_view name) noexcept {
67 if (!active_) [[unlikely]] {
68 return;
69 }
70 Profiler::Instance().ZoneName(storage_, name);
71}
72
73auto ScopedZone::CurrentZoneStorage() noexcept -> std::span<std::byte> {
74 if (g_zone_annotations_suppressed) {
75 return {};
76 }
77 return g_current_zone_storage;
78}
79
80} // namespace helios::profile
void ZoneText(std::span< std::byte > storage, std::string_view text) noexcept
Dispatches a zone text event to all registered backends.
Definition profiler.cpp:145
static Profiler & Instance() noexcept
Returns the singleton profiler instance.
Definition profiler.hpp:403
void EndZone(std::span< std::byte > storage) noexcept
Dispatches a zone end event to all registered backends.
Definition profiler.cpp:134
void ZoneName(std::span< std::byte > storage, std::string_view name) noexcept
Dispatches a zone name event to all registered backends.
Definition profiler.cpp:169
void BeginZone(const ZoneSpec &spec, std::span< std::byte > storage) noexcept
Dispatches a zone begin event to all registered backends.
Definition profiler.cpp:122
void ZoneValue(std::span< std::byte > storage, uint64_t value) noexcept
Dispatches a zone value event to all registered backends.
Definition profiler.cpp:157
static auto CurrentZoneStorage() noexcept -> std::span< std::byte >
Retrieves the current zone storage span.
Definition zone.cpp:73
void SetText(std::string_view text) noexcept
Updates the zone's text.
Definition zone.cpp:52
ScopedZone(const ZoneSpec &spec) noexcept
Creates a new profiling zone with the given specification.
Definition zone.cpp:21
void SetName(std::string_view name) noexcept
Updates the zone's name.
Definition zone.cpp:66
void SetValue(uint64_t value) noexcept
Updates the zone's value.
Definition zone.cpp:59
std::span< std::byte > g_current_zone_storage
Definition zone.cpp:16
STL namespace.
Immutable zone description for a single instrumentation site.
Definition common.hpp:22