Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/DeterminismLog.h
9906 views
1
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2
// SPDX-FileCopyrightText: 2022 Jorrit Rouwe
3
// SPDX-License-Identifier: MIT
4
5
#pragma once
6
7
//#define JPH_ENABLE_DETERMINISM_LOG
8
#ifdef JPH_ENABLE_DETERMINISM_LOG
9
10
#include <Jolt/Physics/Body/BodyID.h>
11
#include <Jolt/Physics/Collision/Shape/SubShapeID.h>
12
13
JPH_SUPPRESS_WARNINGS_STD_BEGIN
14
#include <iomanip>
15
#include <fstream>
16
JPH_SUPPRESS_WARNINGS_STD_END
17
18
JPH_NAMESPACE_BEGIN
19
20
/// A simple class that logs the state of the simulation. The resulting text file can be used to diff between platforms and find issues in determinism.
21
class DeterminismLog
22
{
23
private:
24
JPH_INLINE uint32 Convert(float inValue) const
25
{
26
return *(uint32 *)&inValue;
27
}
28
29
JPH_INLINE uint64 Convert(double inValue) const
30
{
31
return *(uint64 *)&inValue;
32
}
33
34
public:
35
DeterminismLog()
36
{
37
mLog.open("detlog.txt", std::ios::out | std::ios::trunc | std::ios::binary); // Binary because we don't want a difference between Unix and Windows line endings.
38
mLog.fill('0');
39
}
40
41
DeterminismLog & operator << (char inValue)
42
{
43
mLog << inValue;
44
return *this;
45
}
46
47
DeterminismLog & operator << (const char *inValue)
48
{
49
mLog << std::dec << inValue;
50
return *this;
51
}
52
53
DeterminismLog & operator << (const string &inValue)
54
{
55
mLog << std::dec << inValue;
56
return *this;
57
}
58
59
DeterminismLog & operator << (const BodyID &inValue)
60
{
61
mLog << std::hex << std::setw(8) << inValue.GetIndexAndSequenceNumber();
62
return *this;
63
}
64
65
DeterminismLog & operator << (const SubShapeID &inValue)
66
{
67
mLog << std::hex << std::setw(8) << inValue.GetValue();
68
return *this;
69
}
70
71
DeterminismLog & operator << (float inValue)
72
{
73
mLog << std::hex << std::setw(8) << Convert(inValue);
74
return *this;
75
}
76
77
DeterminismLog & operator << (int inValue)
78
{
79
mLog << inValue;
80
return *this;
81
}
82
83
DeterminismLog & operator << (uint32 inValue)
84
{
85
mLog << std::hex << std::setw(8) << inValue;
86
return *this;
87
}
88
89
DeterminismLog & operator << (uint64 inValue)
90
{
91
mLog << std::hex << std::setw(16) << inValue;
92
return *this;
93
}
94
95
DeterminismLog & operator << (Vec3Arg inValue)
96
{
97
mLog << std::hex << std::setw(8) << Convert(inValue.GetX()) << " " << std::setw(8) << Convert(inValue.GetY()) << " " << std::setw(8) << Convert(inValue.GetZ());
98
return *this;
99
}
100
101
DeterminismLog & operator << (DVec3Arg inValue)
102
{
103
mLog << std::hex << std::setw(16) << Convert(inValue.GetX()) << " " << std::setw(16) << Convert(inValue.GetY()) << " " << std::setw(16) << Convert(inValue.GetZ());
104
return *this;
105
}
106
107
DeterminismLog & operator << (Vec4Arg inValue)
108
{
109
mLog << std::hex << std::setw(8) << Convert(inValue.GetX()) << " " << std::setw(8) << Convert(inValue.GetY()) << " " << std::setw(8) << Convert(inValue.GetZ()) << " " << std::setw(8) << Convert(inValue.GetW());
110
return *this;
111
}
112
113
DeterminismLog & operator << (const Float3 &inValue)
114
{
115
mLog << std::hex << std::setw(8) << Convert(inValue.x) << " " << std::setw(8) << Convert(inValue.y) << " " << std::setw(8) << Convert(inValue.z);
116
return *this;
117
}
118
119
DeterminismLog & operator << (Mat44Arg inValue)
120
{
121
*this << inValue.GetColumn4(0) << " " << inValue.GetColumn4(1) << " " << inValue.GetColumn4(2) << " " << inValue.GetColumn4(3);
122
return *this;
123
}
124
125
DeterminismLog & operator << (DMat44Arg inValue)
126
{
127
*this << inValue.GetColumn4(0) << " " << inValue.GetColumn4(1) << " " << inValue.GetColumn4(2) << " " << inValue.GetTranslation();
128
return *this;
129
}
130
131
DeterminismLog & operator << (QuatArg inValue)
132
{
133
*this << inValue.GetXYZW();
134
return *this;
135
}
136
137
// Singleton instance
138
static DeterminismLog sLog;
139
140
private:
141
std::ofstream mLog;
142
};
143
144
/// Will log something to the determinism log, usage: JPH_DET_LOG("label " << value);
145
#define JPH_DET_LOG(...) DeterminismLog::sLog << __VA_ARGS__ << '\n'
146
147
JPH_NAMESPACE_END
148
149
#else
150
151
JPH_SUPPRESS_WARNING_PUSH
152
JPH_SUPPRESS_WARNINGS
153
154
/// By default we log nothing
155
#define JPH_DET_LOG(...)
156
157
JPH_SUPPRESS_WARNING_POP
158
159
#endif // JPH_ENABLE_DETERMINISM_LOG
160
161