BsRenderStats.h 6.6 KB

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