BsRenderStats.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsModule.h"
  4. namespace BansheeEngine
  5. {
  6. /** @cond INTERNAL */
  7. /** @addtogroup Profiling
  8. * @{
  9. */
  10. /** Common object types to track resource statistics for. */
  11. enum RenderStatResourceType
  12. {
  13. RenderStatObject_IndexBuffer,
  14. RenderStatObject_VertexBuffer,
  15. RenderStatObject_GpuBuffer,
  16. RenderStatObject_GpuParamBuffer,
  17. RenderStatObject_Texture,
  18. RenderStatObject_GpuProgram,
  19. RenderStatObject_Query
  20. };
  21. /** Object that stores various render statistics. */
  22. struct BS_CORE_EXPORT RenderStatsData
  23. {
  24. RenderStatsData()
  25. : numDrawCalls(0), numRenderTargetChanges(0), numPresents(0), numClears(0),
  26. numVertices(0), numPrimitives(0), numBlendStateChanges(0), numRasterizerStateChanges(0),
  27. numDepthStencilStateChanges(0), numTextureBinds(0), numSamplerBinds(0), numVertexBufferBinds(0),
  28. numIndexBufferBinds(0), numGpuParamBufferBinds(0), numGpuProgramBinds(0)
  29. { }
  30. UINT64 numDrawCalls;
  31. UINT64 numRenderTargetChanges;
  32. UINT64 numPresents;
  33. UINT64 numClears;
  34. UINT64 numVertices;
  35. UINT64 numPrimitives;
  36. UINT64 numBlendStateChanges;
  37. UINT64 numRasterizerStateChanges;
  38. UINT64 numDepthStencilStateChanges;
  39. UINT64 numTextureBinds;
  40. UINT64 numSamplerBinds;
  41. UINT64 numVertexBufferBinds;
  42. UINT64 numIndexBufferBinds;
  43. UINT64 numGpuParamBufferBinds;
  44. UINT64 numGpuProgramBinds;
  45. UINT64 numResourceWrites;
  46. UINT64 numResourceReads;
  47. UINT64 numObjectsCreated;
  48. UINT64 numObjectsDestroyed;
  49. };
  50. /**
  51. * Tracks various render system statistics.
  52. *
  53. * @note Core thread only.
  54. */
  55. class BS_CORE_EXPORT RenderStats : public Module<RenderStats>
  56. {
  57. public:
  58. /** Increments draw call counter indicating how many times were render system API Draw methods called. */
  59. void incNumDrawCalls() { mData.numDrawCalls++; }
  60. /** Increments render target change counter indicating how many times did the active render target change. */
  61. void incNumRenderTargetChanges() { mData.numRenderTargetChanges++; }
  62. /** Increments render target present counter indicating how many times did the buffer swap happen. */
  63. void incNumPresents() { mData.numPresents++; }
  64. /**
  65. * Increments render target clear counter indicating how many times did the target the cleared, entirely or
  66. * partially.
  67. */
  68. void incNumClears() { mData.numClears++; }
  69. /** Increments vertex draw counter indicating how many vertices were sent to the pipeline. */
  70. void addNumVertices(UINT32 count) { mData.numVertices += count; }
  71. /** Increments primitive draw counter indicating how many primitives were sent to the pipeline. */
  72. void addNumPrimitives(UINT32 count) { mData.numPrimitives += count; }
  73. /** Increments blend state change counter indicating how many times was a blend state bound to the pipeline. */
  74. void incNumBlendStateChanges() { mData.numBlendStateChanges++; }
  75. /**
  76. * Increments rasterizer state change counter indicating how many times was a rasterizer state bound to the
  77. * pipeline.
  78. */
  79. void incNumRasterizerStateChanges() { mData.numRasterizerStateChanges++; }
  80. /**
  81. * Increments depth/stencil state change counter indicating how many times was a depth/stencil state bound to the
  82. * pipeline.
  83. */
  84. void incNumDepthStencilStateChanges() { mData.numDepthStencilStateChanges++; }
  85. /** Increments texture change counter indicating how many times was a texture bound to the pipeline. */
  86. void incNumTextureBinds() { mData.numTextureBinds++; }
  87. /** Increments sampler state change counter indicating how many times was a sampler state bound to the pipeline. */
  88. void incNumSamplerBinds() { mData.numSamplerBinds++; }
  89. /** Increments vertex buffer change counter indicating how many times was a vertex buffer bound to the pipeline. */
  90. void incNumVertexBufferBinds() { mData.numVertexBufferBinds++; }
  91. /** Increments index buffer change counter indicating how many times was a index buffer bound to the pipeline. */
  92. void incNumIndexBufferBinds() { mData.numIndexBufferBinds++; }
  93. /**
  94. * Increments GPU parameter buffer change counter indicating how many times was a GPU parameter buffer bound to the
  95. * pipeline.
  96. */
  97. void incNumGpuParamBufferBinds() { mData.numGpuParamBufferBinds++; }
  98. /** Increments GPU program change counter indicating how many times was a GPU program bound to the pipeline. */
  99. void incNumGpuProgramBinds() { mData.numGpuProgramBinds++; }
  100. /**
  101. * Increments created GPU resource counter.
  102. *
  103. * @param[in] category Category of the resource.
  104. */
  105. void incResCreated(UINT32 category)
  106. {
  107. // TODO - I'm ignoring resourceType for now. Later I will want to
  108. // count object creation/destruction/read/write per type. I will
  109. // also want to allow the caller to assign names to specific "resourceType" id.
  110. // (Since many types will be RenderAPI specific).
  111. // TODO - I should also track number of active GPU objects using this method, instead
  112. // of just keeping track of how many were created and destroyed during the frame.
  113. mData.numObjectsCreated++;
  114. }
  115. /**
  116. * Increments destroyed GPU resource counter.
  117. *
  118. * @param[in] category Category of the resource.
  119. */
  120. void incResDestroyed(UINT32 category) { mData.numObjectsDestroyed++; }
  121. /**
  122. * Increments GPU resource read counter.
  123. *
  124. * @param[in] category Category of the resource.
  125. */
  126. void incResRead(UINT32 category) { mData.numResourceReads++; }
  127. /**
  128. * Increments GPU resource write counter.
  129. *
  130. * @param[in] category Category of the resource.
  131. */
  132. void incResWrite(UINT32 category) { mData.numResourceWrites++; }
  133. /**
  134. * Returns an object containing various rendering statistics.
  135. *
  136. * @note
  137. * Do not modify the returned state unless you know what you are doing, it will change the actual internal object.
  138. */
  139. RenderStatsData& getData() { return mData; }
  140. private:
  141. RenderStatsData mData;
  142. };
  143. #if BS_PROFILING_ENABLED
  144. #define BS_INC_RENDER_STAT_CAT(Stat, Category) RenderStats::instance().inc##Stat##((UINT32)##Category##)
  145. #define BS_INC_RENDER_STAT(Stat) RenderStats::instance().inc##Stat##()
  146. #define BS_ADD_RENDER_STAT(Stat, Count) RenderStats::instance().add##Stat##(##Count##)
  147. #else
  148. #define BS_INC_RENDER_STAT_CAT(Stat, Category)
  149. #define BS_INC_RENDER_STAT(Stat)
  150. #define BS_ADD_RENDER_STAT(Stat, Count)
  151. #endif
  152. /** @} */
  153. /** @endcond */
  154. }