BsRenderStats.h 6.2 KB

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