BsProfilerGPU.h 5.9 KB

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