Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Collision/NarrowPhaseStats.h
9912 views
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)1// SPDX-FileCopyrightText: 2021 Jorrit Rouwe2// SPDX-License-Identifier: MIT34#pragma once56#include <Jolt/Core/TickCounter.h>7#include <Jolt/Physics/Collision/Shape/Shape.h>89JPH_SUPPRESS_WARNING_PUSH10JPH_CLANG_SUPPRESS_WARNING("-Wc++98-compat-pedantic")1112// Shorthand function to ifdef out code if narrow phase stats tracking is off13#ifdef JPH_TRACK_NARROWPHASE_STATS14#define JPH_IF_TRACK_NARROWPHASE_STATS(...) __VA_ARGS__15#else16#define JPH_IF_TRACK_NARROWPHASE_STATS(...)17#endif // JPH_TRACK_NARROWPHASE_STATS1819JPH_SUPPRESS_WARNING_POP2021#ifdef JPH_TRACK_NARROWPHASE_STATS2223JPH_NAMESPACE_BEGIN2425/// Structure that tracks narrow phase timing information for a particular combination of shapes26class NarrowPhaseStat27{28public:29/// Trace an individual stat in CSV form.30void ReportStats(const char *inName, EShapeSubType inType1, EShapeSubType inType2, uint64 inTicks100Pct) const;3132/// Trace the collected broadphase stats in CSV form.33/// This report can be used to judge and tweak the efficiency of the broadphase.34static void sReportStats();3536atomic<uint64> mNumQueries = 0;37atomic<uint64> mHitsReported = 0;38atomic<uint64> mTotalTicks = 0;39atomic<uint64> mChildTicks = 0;4041static NarrowPhaseStat sCollideShape[NumSubShapeTypes][NumSubShapeTypes];42static NarrowPhaseStat sCastShape[NumSubShapeTypes][NumSubShapeTypes];43};4445/// Object that tracks the start and end of a narrow phase operation46class TrackNarrowPhaseStat47{48public:49TrackNarrowPhaseStat(NarrowPhaseStat &inStat) :50mStat(inStat),51mParent(sRoot),52mStart(GetProcessorTickCount())53{54// Make this the new root of the chain55sRoot = this;56}5758~TrackNarrowPhaseStat()59{60uint64 delta_ticks = GetProcessorTickCount() - mStart;6162// Notify parent of time spent in child63if (mParent != nullptr)64mParent->mStat.mChildTicks += delta_ticks;6566// Increment stats at this level67mStat.mNumQueries++;68mStat.mTotalTicks += delta_ticks;6970// Restore root pointer71JPH_ASSERT(sRoot == this);72sRoot = mParent;73}7475NarrowPhaseStat & mStat;76TrackNarrowPhaseStat * mParent;77uint64 mStart;7879static thread_local TrackNarrowPhaseStat *sRoot;80};8182/// Object that tracks the start and end of a hit being processed by a collision collector83class TrackNarrowPhaseCollector84{85public:86TrackNarrowPhaseCollector() :87mStart(GetProcessorTickCount())88{89}9091~TrackNarrowPhaseCollector()92{93// Mark time spent in collector as 'child' time for the parent94uint64 delta_ticks = GetProcessorTickCount() - mStart;95if (TrackNarrowPhaseStat::sRoot != nullptr)96TrackNarrowPhaseStat::sRoot->mStat.mChildTicks += delta_ticks;9798// Notify all parents of a hit99for (TrackNarrowPhaseStat *track = TrackNarrowPhaseStat::sRoot; track != nullptr; track = track->mParent)100track->mStat.mHitsReported++;101}102103private:104uint64 mStart;105};106107JPH_NAMESPACE_END108109#endif // JPH_TRACK_NARROWPHASE_STATS110111112