BsProfilerGPU.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "Profiling/BsProfilerGPU.h"
  4. #include "Profiling/BsRenderStats.h"
  5. #include "RenderAPI/BsTimerQuery.h"
  6. #include "RenderAPI/BsOcclusionQuery.h"
  7. #include "Error/BsException.h"
  8. namespace bs
  9. {
  10. const UINT32 ProfilerGPU::MAX_QUEUE_ELEMENTS = 5;
  11. ProfilerGPU::ProfilerGPU()
  12. :mIsFrameActive(false), mReadyReports(nullptr), mReportHeadPos(0), mReportCount(0)
  13. {
  14. mReadyReports = bs_newN<GPUProfilerReport>(MAX_QUEUE_ELEMENTS);
  15. }
  16. ProfilerGPU::~ProfilerGPU()
  17. {
  18. bs_deleteN(mReadyReports, MAX_QUEUE_ELEMENTS);
  19. }
  20. void ProfilerGPU::beginFrame()
  21. {
  22. if (mIsFrameActive)
  23. BS_EXCEPT(InvalidStateException, "Cannot begin a frame because another frame is active.");
  24. mActiveFrame = ActiveFrame();
  25. mActiveFrame.frameSample.sampleName = "Frame";
  26. beginSampleInternal(mActiveFrame.frameSample);
  27. mIsFrameActive = true;
  28. }
  29. void ProfilerGPU::endFrame()
  30. {
  31. if (mActiveSampleIndexes.size() > 0)
  32. BS_EXCEPT(InvalidStateException, "Attempting to end a frame while a sample is active.");
  33. if (!mIsFrameActive)
  34. return;
  35. endSampleInternal(mActiveFrame.frameSample);
  36. mUnresolvedFrames.push(mActiveFrame);
  37. mIsFrameActive = false;
  38. }
  39. void ProfilerGPU::beginSample(const ProfilerString& name)
  40. {
  41. if (!mIsFrameActive)
  42. BS_EXCEPT(InvalidStateException, "Cannot begin a sample because no frame is active.");
  43. mActiveFrame.samples.push_back(ActiveSample());
  44. ActiveSample& sample = mActiveFrame.samples.back();
  45. sample.sampleName = name;
  46. beginSampleInternal(sample);
  47. mActiveSampleIndexes.push((UINT32)mActiveFrame.samples.size() - 1);
  48. }
  49. void ProfilerGPU::endSample(const ProfilerString& name)
  50. {
  51. if (mActiveSampleIndexes.size() == 0)
  52. return;
  53. UINT32 lastSampleIdx = mActiveSampleIndexes.top();
  54. ActiveSample& sample = mActiveFrame.samples[lastSampleIdx];
  55. if (sample.sampleName != name)
  56. {
  57. String errorStr = "Attempting to end a sample that doesn't match. Got: " +
  58. String(name.c_str()) + ". Expected: " + String(sample.sampleName.c_str());
  59. BS_EXCEPT(InvalidStateException, errorStr);
  60. }
  61. endSampleInternal(sample);
  62. mActiveSampleIndexes.pop();
  63. }
  64. UINT32 ProfilerGPU::getNumAvailableReports()
  65. {
  66. Lock lock(mMutex);
  67. return mReportCount;
  68. }
  69. GPUProfilerReport ProfilerGPU::getNextReport()
  70. {
  71. Lock lock(mMutex);
  72. if (mReportCount == 0)
  73. BS_EXCEPT(InvalidStateException, "No reports are available.")
  74. GPUProfilerReport report = mReadyReports[mReportHeadPos];
  75. mReportHeadPos = (mReportHeadPos + 1) % MAX_QUEUE_ELEMENTS;
  76. mReportCount--;
  77. return report;
  78. }
  79. void ProfilerGPU::_update()
  80. {
  81. while (!mUnresolvedFrames.empty())
  82. {
  83. ActiveFrame& frame = mUnresolvedFrames.front();
  84. // Frame sample timer query is the last query we issued
  85. // so if it is complete, we may assume all queries are complete.
  86. if (frame.frameSample.activeTimeQuery->isReady())
  87. {
  88. GPUProfilerReport report = resolveFrame(frame);
  89. mUnresolvedFrames.pop();
  90. {
  91. Lock lock(mMutex);
  92. mReadyReports[(mReportHeadPos + mReportCount) % MAX_QUEUE_ELEMENTS] = report;
  93. if (mReportCount == MAX_QUEUE_ELEMENTS)
  94. mReportHeadPos = (mReportHeadPos + 1) % MAX_QUEUE_ELEMENTS;
  95. else
  96. mReportCount++;
  97. }
  98. }
  99. else
  100. break;
  101. }
  102. }
  103. GPUProfilerReport ProfilerGPU::resolveFrame(ActiveFrame& frame)
  104. {
  105. GPUProfilerReport report;
  106. resolveSample(frame.frameSample, report.frameSample);
  107. for (auto& sample : frame.samples)
  108. {
  109. report.samples.push_back(GPUProfileSample());
  110. GPUProfileSample& newSample = report.samples.back();
  111. resolveSample(sample, newSample);
  112. }
  113. return report;
  114. }
  115. void ProfilerGPU::resolveSample(const ActiveSample& sample, GPUProfileSample& reportSample)
  116. {
  117. reportSample.name = String(sample.sampleName.c_str());
  118. reportSample.timeMs = sample.activeTimeQuery->getTimeMs();
  119. reportSample.numDrawnSamples = sample.activeOcclusionQuery->getNumSamples();
  120. reportSample.numDrawCalls = (UINT32)(sample.endStats.numDrawCalls - sample.startStats.numDrawCalls);
  121. reportSample.numRenderTargetChanges = (UINT32)(sample.endStats.numRenderTargetChanges - sample.startStats.numRenderTargetChanges);
  122. reportSample.numPresents = (UINT32)(sample.endStats.numPresents - sample.startStats.numPresents);
  123. reportSample.numClears = (UINT32)(sample.endStats.numClears - sample.startStats.numClears);
  124. reportSample.numVertices = (UINT32)(sample.endStats.numVertices - sample.startStats.numVertices);
  125. reportSample.numPrimitives = (UINT32)(sample.endStats.numPrimitives - sample.startStats.numPrimitives);
  126. reportSample.numPipelineStateChanges = (UINT32)(sample.endStats.numPipelineStateChanges - sample.startStats.numPipelineStateChanges);
  127. reportSample.numGpuParamBinds = (UINT32)(sample.endStats.numGpuParamBinds - sample.startStats.numGpuParamBinds);
  128. reportSample.numVertexBufferBinds = (UINT32)(sample.endStats.numVertexBufferBinds - sample.startStats.numVertexBufferBinds);
  129. reportSample.numIndexBufferBinds = (UINT32)(sample.endStats.numIndexBufferBinds - sample.startStats.numIndexBufferBinds);
  130. reportSample.numResourceWrites = (UINT32)(sample.endStats.numResourceWrites - sample.startStats.numResourceWrites);
  131. reportSample.numResourceReads = (UINT32)(sample.endStats.numResourceReads - sample.startStats.numResourceReads);
  132. reportSample.numObjectsCreated = (UINT32)(sample.endStats.numObjectsCreated - sample.startStats.numObjectsCreated);
  133. reportSample.numObjectsDestroyed = (UINT32)(sample.endStats.numObjectsDestroyed - sample.startStats.numObjectsDestroyed);
  134. mFreeTimerQueries.push(sample.activeTimeQuery);
  135. mFreeOcclusionQueries.push(sample.activeOcclusionQuery);
  136. }
  137. void ProfilerGPU::beginSampleInternal(ActiveSample& sample)
  138. {
  139. sample.startStats = RenderStats::instance().getData();
  140. sample.activeTimeQuery = getTimerQuery();
  141. sample.activeTimeQuery->begin();
  142. sample.activeOcclusionQuery = getOcclusionQuery();
  143. sample.activeOcclusionQuery->begin();
  144. }
  145. void ProfilerGPU::endSampleInternal(ActiveSample& sample)
  146. {
  147. sample.endStats = RenderStats::instance().getData();
  148. sample.activeOcclusionQuery->end();
  149. sample.activeTimeQuery->end();
  150. }
  151. SPtr<ct::TimerQuery> ProfilerGPU::getTimerQuery() const
  152. {
  153. if (!mFreeTimerQueries.empty())
  154. {
  155. SPtr<ct::TimerQuery> timerQuery = mFreeTimerQueries.top();
  156. mFreeTimerQueries.pop();
  157. return timerQuery;
  158. }
  159. return ct::TimerQuery::create();
  160. }
  161. SPtr<ct::OcclusionQuery> ProfilerGPU::getOcclusionQuery() const
  162. {
  163. if (!mFreeOcclusionQueries.empty())
  164. {
  165. SPtr<ct::OcclusionQuery> occlusionQuery = mFreeOcclusionQueries.top();
  166. mFreeOcclusionQueries.pop();
  167. return occlusionQuery;
  168. }
  169. return ct::OcclusionQuery::create(false);
  170. }
  171. ProfilerGPU& gProfilerGPU()
  172. {
  173. return ProfilerGPU::instance();
  174. }
  175. }