NarrowPhaseStats.h 2.8 KB

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