Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Collision/NarrowPhaseStats.h
9912 views
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
7
#include <Jolt/Core/TickCounter.h>
8
#include <Jolt/Physics/Collision/Shape/Shape.h>
9
10
JPH_SUPPRESS_WARNING_PUSH
11
JPH_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
20
JPH_SUPPRESS_WARNING_POP
21
22
#ifdef JPH_TRACK_NARROWPHASE_STATS
23
24
JPH_NAMESPACE_BEGIN
25
26
/// Structure that tracks narrow phase timing information for a particular combination of shapes
27
class NarrowPhaseStat
28
{
29
public:
30
/// Trace an individual stat in CSV form.
31
void ReportStats(const char *inName, EShapeSubType inType1, EShapeSubType inType2, uint64 inTicks100Pct) const;
32
33
/// Trace the collected broadphase stats in CSV form.
34
/// This report can be used to judge and tweak the efficiency of the broadphase.
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
46
/// Object that tracks the start and end of a narrow phase operation
47
class TrackNarrowPhaseStat
48
{
49
public:
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
83
/// Object that tracks the start and end of a hit being processed by a collision collector
84
class TrackNarrowPhaseCollector
85
{
86
public:
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
104
private:
105
uint64 mStart;
106
};
107
108
JPH_NAMESPACE_END
109
110
#endif // JPH_TRACK_NARROWPHASE_STATS
111
112