BsProfilerGPU.cpp 6.9 KB

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