Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
NarrowPhaseStats.h
Go to the documentation of this file.
1// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2// SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
9
10JPH_SUPPRESS_WARNING_PUSH
11JPH_CLANG_SUPPRESS_WARNING("-Wc++98-compat-pedantic")
12
13// Shorthand function to ifdef out code if narrow phase stats tracking is off
14#ifdef JPH_TRACK_NARROWPHASE_STATS
15 #define JPH_IF_TRACK_NARROWPHASE_STATS(...) __VA_ARGS__
16#else
17 #define JPH_IF_TRACK_NARROWPHASE_STATS(...)
18#endif // JPH_TRACK_NARROWPHASE_STATS
19
20JPH_SUPPRESS_WARNING_POP
21
22#ifdef JPH_TRACK_NARROWPHASE_STATS
23
25
27class NarrowPhaseStat
28{
29public:
31 void ReportStats(const char *inName, EShapeSubType inType1, EShapeSubType inType2, uint64 inTicks100Pct) const;
32
35 static void sReportStats();
36
37 atomic<uint64> mNumQueries = 0;
38 atomic<uint64> mHitsReported = 0;
39 atomic<uint64> mTotalTicks = 0;
40 atomic<uint64> mChildTicks = 0;
41
42 static NarrowPhaseStat sCollideShape[NumSubShapeTypes][NumSubShapeTypes];
43 static NarrowPhaseStat sCastShape[NumSubShapeTypes][NumSubShapeTypes];
44};
45
47class TrackNarrowPhaseStat
48{
49public:
50 TrackNarrowPhaseStat(NarrowPhaseStat &inStat) :
51 mStat(inStat),
52 mParent(sRoot),
53 mStart(GetProcessorTickCount())
54 {
55 // Make this the new root of the chain
56 sRoot = this;
57 }
58
59 ~TrackNarrowPhaseStat()
60 {
61 uint64 delta_ticks = GetProcessorTickCount() - mStart;
62
63 // Notify parent of time spent in child
64 if (mParent != nullptr)
65 mParent->mStat.mChildTicks += delta_ticks;
66
67 // Increment stats at this level
68 mStat.mNumQueries++;
69 mStat.mTotalTicks += delta_ticks;
70
71 // Restore root pointer
72 JPH_ASSERT(sRoot == this);
73 sRoot = mParent;
74 }
75
76 NarrowPhaseStat & mStat;
77 TrackNarrowPhaseStat * mParent;
78 uint64 mStart;
79
80 static thread_local TrackNarrowPhaseStat *sRoot;
81};
82
84class TrackNarrowPhaseCollector
85{
86public:
87 TrackNarrowPhaseCollector() :
88 mStart(GetProcessorTickCount())
89 {
90 }
91
92 ~TrackNarrowPhaseCollector()
93 {
94 // Mark time spent in collector as 'child' time for the parent
95 uint64 delta_ticks = GetProcessorTickCount() - mStart;
96 if (TrackNarrowPhaseStat::sRoot != nullptr)
97 TrackNarrowPhaseStat::sRoot->mStat.mChildTicks += delta_ticks;
98
99 // Notify all parents of a hit
100 for (TrackNarrowPhaseStat *track = TrackNarrowPhaseStat::sRoot; track != nullptr; track = track->mParent)
101 track->mStat.mHitsReported++;
102 }
103
104private:
105 uint64 mStart;
106};
107
109
110#endif // JPH_TRACK_NARROWPHASE_STATS
std::uint64_t uint64
Definition: Core.h:456
#define JPH_NAMESPACE_END
Definition: Core.h:378
#define JPH_CLANG_SUPPRESS_WARNING(w)
Definition: Core.h:263
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:372
#define JPH_ASSERT(...)
Definition: IssueReporting.h:33
EShapeSubType
This enumerates all shape types, each shape can return its type through Shape::GetSubType.
Definition: Shape.h:74
JPH_NAMESPACE_BEGIN JPH_INLINE uint64 GetProcessorTickCount()
Functionality to get the processors cycle counter.
Definition: TickCounter.h:26