BsProfilerGPU.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 numPipelineStateChanges; /**< How many times did the pipeline state change. */
  25. UINT32 numGpuParamBinds; /**< How many times were GPU parameters bound. */
  26. UINT32 numVertexBufferBinds; /**< How many times was a vertex buffer bound. */
  27. UINT32 numIndexBufferBinds; /**< How many times was an index buffer bound. */
  28. UINT32 numResourceWrites; /**< How many times were GPU resources written to. */
  29. UINT32 numResourceReads; /**< How many times were GPU resources read from. */
  30. UINT32 numObjectsCreated; /**< How many GPU objects were created. */
  31. UINT32 numObjectsDestroyed; /**< How many GPU objects were destroyed. */
  32. };
  33. /** Profiler report containing information about GPU sampling data from a single frame. */
  34. struct GPUProfilerReport
  35. {
  36. GPUProfileSample frameSample; /**< Sample containing data for entire frame. */
  37. Vector<GPUProfileSample> samples;
  38. };
  39. /**
  40. * Profiler that measures time and amount of various GPU operations.
  41. *
  42. * @note Core thread only except where noted otherwise.
  43. */
  44. class BS_CORE_EXPORT ProfilerGPU : public Module<ProfilerGPU>
  45. {
  46. private:
  47. struct ActiveSample
  48. {
  49. ProfilerString sampleName;
  50. RenderStatsData startStats;
  51. RenderStatsData endStats;
  52. SPtr<TimerQuery> activeTimeQuery;
  53. SPtr<OcclusionQuery> activeOcclusionQuery;
  54. };
  55. struct ActiveFrame
  56. {
  57. ActiveSample frameSample;
  58. Vector<ActiveSample> samples;
  59. };
  60. public:
  61. ProfilerGPU();
  62. /**
  63. * Signals a start of a new frame. Every frame will generate a separate profiling report. This call must be followed
  64. * by endFrame(), and any sampling operations must happen between beginFrame() and endFrame().
  65. */
  66. void beginFrame();
  67. /**
  68. * Signals an end of the currently sampled frame. Results of the sampling will be available once
  69. * getNumAvailableReports increments. This may take a while as the sampling is scheduled on the core thread and
  70. * on the GPU.
  71. */
  72. void endFrame();
  73. /**
  74. * Begins sample measurement. Must be followed by endSample().
  75. *
  76. * @param[in] name Unique name for the sample you can later use to find the sampling data.
  77. *
  78. * @note Must be called between beginFrame()/endFrame() calls.
  79. */
  80. void beginSample(const ProfilerString& name);
  81. /**
  82. * Ends sample measurement.
  83. *
  84. * @param[in] name Unique name for the sample.
  85. *
  86. * @note
  87. * Unique name is primarily needed to more easily identify mismatched begin/end sample pairs. Otherwise the name in
  88. * beginSample() would be enough. Must be called between beginFrame()/endFrame() calls.
  89. */
  90. void endSample(const ProfilerString& name);
  91. /**
  92. * Returns number of profiling reports that are ready but haven't been retrieved yet.
  93. *
  94. * @note
  95. * There is an internal limit of maximum number of available reports, where oldest ones will get deleted so make
  96. * sure to call this often if you don't want to miss some.
  97. * @note
  98. * Thread safe.
  99. */
  100. UINT32 getNumAvailableReports();
  101. /**
  102. * Gets the oldest report available and removes it from the internal list. Throws an exception if no reports are
  103. * available.
  104. *
  105. * @note Thread safe.
  106. */
  107. GPUProfilerReport getNextReport();
  108. public: // ***** INTERNAL ******
  109. /** @name Internal
  110. * @{
  111. */
  112. /**
  113. * To be called once per frame from the Core thread.
  114. */
  115. void _update();
  116. /** @} */
  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. SPtr<TimerQuery> getTimerQuery() const;
  124. /** Creates a new occlusion query or returns an existing free query. */
  125. SPtr<OcclusionQuery> 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<SPtr<TimerQuery>> mFreeTimerQueries;
  140. mutable Stack<SPtr<OcclusionQuery>> mFreeOcclusionQueries;
  141. Mutex mMutex;
  142. };
  143. /** @} */
  144. }