NarrowPhaseStats.h 2.7 KB

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