NarrowPhaseStats.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. #include <Jolt/Core/TickCounter.h>
  6. #include <Jolt/Physics/Collision/Shape/Shape.h>
  7. JPH_SUPPRESS_WARNING_PUSH
  8. JPH_CLANG_SUPPRESS_WARNING("-Wc++98-compat-pedantic")
  9. // Shorthand function to ifdef out code if narrow phase stats tracking is off
  10. #ifdef JPH_TRACK_NARROWPHASE_STATS
  11. #define JPH_IF_TRACK_NARROWPHASE_STATS(...) __VA_ARGS__
  12. #else
  13. #define JPH_IF_TRACK_NARROWPHASE_STATS(...)
  14. #endif // JPH_TRACK_NARROWPHASE_STATS
  15. JPH_SUPPRESS_WARNING_POP
  16. #ifdef JPH_TRACK_NARROWPHASE_STATS
  17. JPH_NAMESPACE_BEGIN
  18. /// Structure that tracks narrow phase timing information for a particular combination of shapes
  19. class NarrowPhaseStat
  20. {
  21. public:
  22. /// Trace an individual stat in CSV form.
  23. void ReportStats(const char *inName, EShapeSubType inType1, EShapeSubType inType2) const;
  24. /// Trace the collected broadphase stats in CSV form.
  25. /// This report can be used to judge and tweak the efficiency of the broadphase.
  26. static void sReportStats();
  27. atomic<uint64> mNumQueries = 0;
  28. atomic<uint64> mHitsReported = 0;
  29. atomic<uint64> mTotalTicks = 0;
  30. atomic<uint64> mChildTicks = 0;
  31. static NarrowPhaseStat sCollideShape[NumSubShapeTypes][NumSubShapeTypes];
  32. static NarrowPhaseStat sCastShape[NumSubShapeTypes][NumSubShapeTypes];
  33. };
  34. /// Object that tracks the start and end of a narrow phase operation
  35. class TrackNarrowPhaseStat
  36. {
  37. public:
  38. TrackNarrowPhaseStat(NarrowPhaseStat &inStat) :
  39. mStat(inStat),
  40. mParent(sRoot),
  41. mStart(GetProcessorTickCount())
  42. {
  43. // Make this the new root of the chain
  44. sRoot = this;
  45. }
  46. ~TrackNarrowPhaseStat()
  47. {
  48. uint64 delta_ticks = GetProcessorTickCount() - mStart;
  49. // Notify parent of time spent in child
  50. if (mParent != nullptr)
  51. mParent->mStat.mChildTicks += delta_ticks;
  52. // Increment stats at this level
  53. mStat.mNumQueries++;
  54. mStat.mTotalTicks += delta_ticks;
  55. // Restore root pointer
  56. JPH_ASSERT(sRoot == this);
  57. sRoot = mParent;
  58. }
  59. NarrowPhaseStat & mStat;
  60. TrackNarrowPhaseStat * mParent;
  61. uint64 mStart;
  62. static thread_local TrackNarrowPhaseStat *sRoot;
  63. };
  64. /// Object that tracks the start and end of a hit being processed by a collision collector
  65. class TrackNarrowPhaseCollector
  66. {
  67. public:
  68. TrackNarrowPhaseCollector() :
  69. mStart(GetProcessorTickCount())
  70. {
  71. }
  72. ~TrackNarrowPhaseCollector()
  73. {
  74. // Mark time spent in collector as 'child' time for the parent
  75. uint64 delta_ticks = GetProcessorTickCount() - mStart;
  76. if (TrackNarrowPhaseStat::sRoot != nullptr)
  77. TrackNarrowPhaseStat::sRoot->mStat.mChildTicks += delta_ticks;
  78. // Notify all parents of a hit
  79. for (TrackNarrowPhaseStat *track = TrackNarrowPhaseStat::sRoot; track != nullptr; track = track->mParent)
  80. track->mStat.mHitsReported++;
  81. }
  82. private:
  83. uint64 mStart;
  84. };
  85. JPH_NAMESPACE_END
  86. #endif // JPH_TRACK_NARROWPHASE_STATS