BsRenderStats.h 6.9 KB

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