1+ /* ***************************************************************************
2+ * Copyright (C) 2023-2025 Zarklord
3+ *
4+ * This file is part of Spore LuaAPI.
5+ *
6+ * Spore LuaAPI is free software: you can redistribute it and/or modify
7+ * it under the terms of the GNU General Public License as published by
8+ * the Free Software Foundation, either version 3 of the License, or
9+ * (at your option) any later version.
10+ *
11+ * This program is distributed in the hope that it will be useful,
12+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+ * GNU General Public License for more details.
15+ *
16+ * You should have received a copy of the GNU General Public License
17+ * along with Spore LuaAPI. If not, see <http://www.gnu.org/licenses/>.
18+ ****************************************************************************/
19+
20+ #pragma once
21+
22+ #include < tracy/Tracy.hpp>
23+
24+ namespace TracyUtil
25+ {
26+ #ifdef TRACY_ON_DEMAND
27+ inline thread_local uint32_t tracy_counter = 0 ;
28+ inline thread_local bool tracy_active = false ;
29+ #endif
30+
31+ inline void TracyZonePush (const tracy::SourceLocationData* srcloc)
32+ {
33+ using namespace tracy ;
34+ #ifdef TRACY_ENABLE
35+ #ifdef TRACY_ON_DEMAND
36+ const auto zone_count = tracy_counter++;
37+ if (zone_count != 0 && !tracy_active) return ;
38+ tracy_active = TracyIsConnected;
39+ if (!tracy_active) return ;
40+ #endif
41+ TracyQueuePrepare (QueueType::ZoneBegin)
42+ MemWrite (&item->zoneBegin .time , Profiler::GetTime ());
43+ MemWrite (&item->zoneBegin .srcloc , srcloc);
44+ TracyQueueCommit (zoneBeginThread);
45+ #endif
46+ }
47+
48+ inline void TracyZonePop ()
49+ {
50+ using namespace tracy ;
51+ #ifdef TRACY_ENABLE
52+ #ifdef TRACY_ON_DEMAND
53+ assert (tracy_counter != 0 );
54+ tracy_counter--;
55+ if (!tracy_active) return ;
56+ if (!TracyIsConnected)
57+ {
58+ tracy_active = false ;
59+ return ;
60+ }
61+ #endif
62+ TracyQueuePrepare (QueueType::ZoneEnd);
63+ MemWrite (&item->zoneEnd .time , Profiler::GetTime ());
64+ TracyQueueCommit (zoneEndThread);
65+ #endif
66+ }
67+ }
68+
69+ #define ZonePush static constexpr tracy::SourceLocationData TracyConcat (__tracy_source_location,TracyLine) { nullptr , TracyFunction, TracyFile, (uint32_t )TracyLine, 0 }; TracyUtil::TracyZonePush(&TracyConcat (__tracy_source_location,TracyLine))
70+ #define ZonePushN (name ) static constexpr tracy::SourceLocationData TracyConcat (__tracy_source_location,TracyLine) { name, TracyFunction, TracyFile, (uint32_t )TracyLine, 0 }; TracyUtil::TracyZonePush(&TracyConcat (__tracy_source_location,TracyLine))
71+ #define ZonePop TracyUtil::TracyZonePop ()
0 commit comments