BsProfilerGPU.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsModule.h"
  4. #include "BsRenderStats.h"
  5. namespace BansheeEngine
  6. {
  7. /** @addtogroup Profiling
  8. * @{
  9. */
  10. /** Contains various profiler statistics about a single GPU profiling sample. */
  11. struct GPUProfileSample
  12. {
  13. String name; /**< Name of the sample for easier identification. */
  14. float timeMs; /**< Time in milliseconds it took to execute the sampled block. */
  15. UINT32 numDrawCalls; /**< Number of draw calls that happened. */
  16. UINT32 numRenderTargetChanges; /**< How many times was render target changed. */
  17. UINT32 numPresents; /**< How many times did a buffer swap happen on a double buffered render target. */
  18. UINT32 numClears; /**< How many times was render target cleared. */
  19. UINT32 numVertices; /**< Total number of vertices sent to the GPU. */
  20. UINT32 numPrimitives; /**< Total number of primitives sent to the GPU. */
  21. UINT32 numDrawnSamples; /**< Number of samples drawn by the GPU. */
  22. UINT32 numBlendStateChanges; /**< How many times did the blend state change. */
  23. UINT32 numRasterizerStateChanges; /**< How many times did the rasterizer state change. */
  24. UINT32 numDepthStencilStateChanges; /**< How many times did the depth stencil state change. */
  25. UINT32 numTextureBinds; /**< How many times was a texture bound. */
  26. UINT32 numSamplerBinds; /**< How many times was a sampler bound. */
  27. UINT32 numVertexBufferBinds; /**< How many times was a vertex buffer bound. */
  28. UINT32 numIndexBufferBinds; /**< How many times was an index buffer bound. */
  29. UINT32 numGpuParamBufferBinds; /**< How many times was an GPU parameter buffer bound. */
  30. UINT32 numGpuProgramBinds; /**< How many times was a GPU program bound. */
  31. UINT32 numResourceWrites; /**< How many times were GPU resources written to. */
  32. UINT32 numResourceReads; /**< How many times were GPU resources read from. */
  33. UINT32 numObjectsCreated; /**< How many GPU objects were created. */
  34. UINT32 numObjectsDestroyed; /**< How many GPU objects were destroyed. */
  35. };
  36. /** Profiler report containing information about GPU sampling data from a single frame. */
  37. struct GPUProfilerReport
  38. {
  39. GPUProfileSample frameSample; /**< Sample containing data for entire frame. */
  40. Vector<GPUProfileSample> samples;
  41. };
  42. /**
  43. * Profiler that measures time and amount of various GPU operations.
  44. *
  45. * @note Core thread only except where noted otherwise.
  46. */
  47. class BS_CORE_EXPORT ProfilerGPU : public Module<ProfilerGPU>
  48. {
  49. private:
  50. struct ActiveSample
  51. {
  52. ProfilerString sampleName;
  53. RenderStatsData startStats;
  54. RenderStatsData endStats;
  55. TimerQueryPtr activeTimeQuery;
  56. OcclusionQueryPtr activeOcclusionQuery;
  57. };
  58. struct ActiveFrame
  59. {
  60. ActiveSample frameSample;
  61. Vector<ActiveSample> samples;
  62. };
  63. public:
  64. ProfilerGPU();
  65. /**
  66. * Signals a start of a new frame. Every frame will generate a separate profiling report. This call must be followed
  67. * by endFrame(), and any sampling operations must happen between beginFrame() and endFrame().
  68. */
  69. void beginFrame();
  70. /**
  71. * Signals an end of the currently sampled frame. Results of the sampling will be available once
  72. * getNumAvailableReports increments. This may take a while as the sampling is scheduled on the core thread and
  73. * on the GPU.
  74. */
  75. void endFrame();
  76. /**
  77. * Begins sample measurement. Must be followed by endSample().
  78. *
  79. * @param[in] name Unique name for the sample you can later use to find the sampling data.
  80. *
  81. * @note Must be called between beginFrame()/endFrame() calls.
  82. */
  83. void beginSample(const ProfilerString& name);
  84. /**
  85. * Ends sample measurement.
  86. *
  87. * @param[in] name Unique name for the sample.
  88. *
  89. * @note
  90. * Unique name is primarily needed to more easily identify mismatched begin/end sample pairs. Otherwise the name in
  91. * beginSample() would be enough. Must be called between beginFrame()/endFrame() calls.
  92. */
  93. void endSample(const ProfilerString& name);
  94. /**
  95. * Returns number of profiling reports that are ready but haven't been retrieved yet.
  96. *
  97. * @note
  98. * There is an internal limit of maximum number of available reports, where oldest ones will get deleted so make
  99. * sure to call this often if you don't want to miss some.
  100. * @note
  101. * Thread safe.
  102. */
  103. UINT32 getNumAvailableReports();
  104. /**
  105. * Gets the oldest report available and removes it from the internal list. Throws an exception if no reports are
  106. * available.
  107. *
  108. * @note Thread safe.
  109. */
  110. GPUProfilerReport getNextReport();
  111. /** @cond INTERNAL */
  112. /**
  113. * To be called once per frame from the Core thread.
  114. */
  115. void _update();
  116. /** @endcond */
  117. private:
  118. /** Assigns start values for the provided sample. */
  119. void beginSampleInternal(ActiveSample& sample);
  120. /** Assigns end values for the provided sample. */
  121. void endSampleInternal(ActiveSample& sample);
  122. /** Creates a new timer query or returns an existing free query. */
  123. TimerQueryPtr getTimerQuery() const;
  124. /** Creates a new occlusion query or returns an existing free query. */
  125. OcclusionQueryPtr getOcclusionQuery() const;
  126. /**
  127. * Interprets the active frame results and generates a profiler report for the frame. Provided frame queries must
  128. * have finished before calling this.
  129. */
  130. GPUProfilerReport resolveFrame(ActiveFrame& frame);
  131. /** Resolves an active sample and converts it to report sample. */
  132. void resolveSample(const ActiveSample& sample, GPUProfileSample& reportSample);
  133. private:
  134. ActiveFrame mActiveFrame;
  135. bool mIsFrameActive;
  136. Stack<UINT32> mActiveSampleIndexes;
  137. Queue<ActiveFrame> mUnresolvedFrames;
  138. Queue<GPUProfilerReport> mReadyReports;
  139. mutable Stack<TimerQueryPtr> mFreeTimerQueries;
  140. mutable Stack<OcclusionQueryPtr> mFreeOcclusionQueries;
  141. BS_MUTEX(mMutex);
  142. };
  143. /** @} */
  144. }