BsProfilerGPU.h 6.2 KB

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