BsProfilerGPU.cpp 7.0 KB

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