PerformanceCounters.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include "PerformanceCounters.h"
  2. PerformanceCounters::PerformanceCounters() : counters(_FL_)
  3. {
  4. for(long i = 0; i < ARRSIZE(entryTable); i++)
  5. {
  6. entryTable[i] = -1;
  7. }
  8. }
  9. PerformanceCounters::~PerformanceCounters()
  10. {
  11. }
  12. //Добавить значение к счётчику, который сбросится с конце кадра
  13. void PerformanceCounters::AddPerformanceCounter(const char * name, float value)
  14. {
  15. Element & el = GetCounter(name);
  16. el.current += value;
  17. }
  18. //Установить счётчик, который сбросится с конце кадра
  19. void PerformanceCounters::SetPerformanceCounter(const char * name, float value)
  20. {
  21. Element & el = GetCounter(name);
  22. el.current = value;
  23. }
  24. //Получить количество счётчиков
  25. dword PerformanceCounters::GetNumberOfPerformanceCounters()
  26. {
  27. return counters.Size();
  28. }
  29. //Получить имя счётчика
  30. const char * PerformanceCounters::GetPerformanceName(long index)
  31. {
  32. if(index >= 0 && index < counters)
  33. {
  34. return counters[index].name;
  35. }
  36. return null;
  37. }
  38. //Получить значение счётчика с предыдущего кадра
  39. float PerformanceCounters::GetPerformanceCounter(long index)
  40. {
  41. if(index >= 0 && index < counters)
  42. {
  43. return counters[index].counter;
  44. }
  45. return 0.0f;
  46. }
  47. //Получить значение счётчика с предыдущего кадра
  48. float PerformanceCounters::GetPerformanceCounter(const char * name)
  49. {
  50. Element & el = GetCounter(name);
  51. return el.counter;
  52. }
  53. //Момент следующего отсчёта
  54. void PerformanceCounters::NextFrame()
  55. {
  56. for(long i = 0; i < counters; i++)
  57. {
  58. Element & el = counters[i];
  59. el.counter = el.current;
  60. el.current = 0.0f;
  61. #ifdef _XBOX
  62. PIXAddNamedCounter(el.counter, el.name);
  63. #endif
  64. }
  65. }
  66. //Найти или добавить счётчик
  67. PerformanceCounters::Element & PerformanceCounters::GetCounter(const char * name)
  68. {
  69. if(!name) name = "";
  70. dword len;
  71. dword hash = string::Hash(name, len);
  72. long index = hash & (ARRSIZE(entryTable) - 1);
  73. long i = entryTable[index];
  74. long last = i;
  75. while(i >= 0)
  76. {
  77. Element & el = counters[i];
  78. if(el.hash == hash)
  79. {
  80. if(el.name.Len() == len)
  81. {
  82. if(strcmp(el.name, name) == 0)
  83. {
  84. break;
  85. }
  86. }
  87. }
  88. i = el.next;
  89. }
  90. if(i < 0)
  91. {
  92. Element & el = counters[i = counters.Add()];
  93. el.current = 0.0f;
  94. el.counter = 0.0f;
  95. el.hash = hash;
  96. el.name = name;
  97. el.next = -1;
  98. if(last >= 0)
  99. {
  100. counters[last].next = i;
  101. }else{
  102. entryTable[index] = i;
  103. }
  104. }
  105. return counters[i];
  106. }