Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/DeterminismLog.h
9906 views
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)1// SPDX-FileCopyrightText: 2022 Jorrit Rouwe2// SPDX-License-Identifier: MIT34#pragma once56//#define JPH_ENABLE_DETERMINISM_LOG7#ifdef JPH_ENABLE_DETERMINISM_LOG89#include <Jolt/Physics/Body/BodyID.h>10#include <Jolt/Physics/Collision/Shape/SubShapeID.h>1112JPH_SUPPRESS_WARNINGS_STD_BEGIN13#include <iomanip>14#include <fstream>15JPH_SUPPRESS_WARNINGS_STD_END1617JPH_NAMESPACE_BEGIN1819/// 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.20class DeterminismLog21{22private:23JPH_INLINE uint32 Convert(float inValue) const24{25return *(uint32 *)&inValue;26}2728JPH_INLINE uint64 Convert(double inValue) const29{30return *(uint64 *)&inValue;31}3233public:34DeterminismLog()35{36mLog.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.37mLog.fill('0');38}3940DeterminismLog & operator << (char inValue)41{42mLog << inValue;43return *this;44}4546DeterminismLog & operator << (const char *inValue)47{48mLog << std::dec << inValue;49return *this;50}5152DeterminismLog & operator << (const string &inValue)53{54mLog << std::dec << inValue;55return *this;56}5758DeterminismLog & operator << (const BodyID &inValue)59{60mLog << std::hex << std::setw(8) << inValue.GetIndexAndSequenceNumber();61return *this;62}6364DeterminismLog & operator << (const SubShapeID &inValue)65{66mLog << std::hex << std::setw(8) << inValue.GetValue();67return *this;68}6970DeterminismLog & operator << (float inValue)71{72mLog << std::hex << std::setw(8) << Convert(inValue);73return *this;74}7576DeterminismLog & operator << (int inValue)77{78mLog << inValue;79return *this;80}8182DeterminismLog & operator << (uint32 inValue)83{84mLog << std::hex << std::setw(8) << inValue;85return *this;86}8788DeterminismLog & operator << (uint64 inValue)89{90mLog << std::hex << std::setw(16) << inValue;91return *this;92}9394DeterminismLog & operator << (Vec3Arg inValue)95{96mLog << std::hex << std::setw(8) << Convert(inValue.GetX()) << " " << std::setw(8) << Convert(inValue.GetY()) << " " << std::setw(8) << Convert(inValue.GetZ());97return *this;98}99100DeterminismLog & operator << (DVec3Arg inValue)101{102mLog << std::hex << std::setw(16) << Convert(inValue.GetX()) << " " << std::setw(16) << Convert(inValue.GetY()) << " " << std::setw(16) << Convert(inValue.GetZ());103return *this;104}105106DeterminismLog & operator << (Vec4Arg inValue)107{108mLog << 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());109return *this;110}111112DeterminismLog & operator << (const Float3 &inValue)113{114mLog << std::hex << std::setw(8) << Convert(inValue.x) << " " << std::setw(8) << Convert(inValue.y) << " " << std::setw(8) << Convert(inValue.z);115return *this;116}117118DeterminismLog & operator << (Mat44Arg inValue)119{120*this << inValue.GetColumn4(0) << " " << inValue.GetColumn4(1) << " " << inValue.GetColumn4(2) << " " << inValue.GetColumn4(3);121return *this;122}123124DeterminismLog & operator << (DMat44Arg inValue)125{126*this << inValue.GetColumn4(0) << " " << inValue.GetColumn4(1) << " " << inValue.GetColumn4(2) << " " << inValue.GetTranslation();127return *this;128}129130DeterminismLog & operator << (QuatArg inValue)131{132*this << inValue.GetXYZW();133return *this;134}135136// Singleton instance137static DeterminismLog sLog;138139private:140std::ofstream mLog;141};142143/// Will log something to the determinism log, usage: JPH_DET_LOG("label " << value);144#define JPH_DET_LOG(...) DeterminismLog::sLog << __VA_ARGS__ << '\n'145146JPH_NAMESPACE_END147148#else149150JPH_SUPPRESS_WARNING_PUSH151JPH_SUPPRESS_WARNINGS152153/// By default we log nothing154#define JPH_DET_LOG(...)155156JPH_SUPPRESS_WARNING_POP157158#endif // JPH_ENABLE_DETERMINISM_LOG159160161