BsRenderStats.h 6.8 KB

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