BsRenderStats.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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), numPipelineStateChanges(0), numTextureBinds(0), numSamplerBinds(0)
  28. , numVertexBufferBinds(0), numIndexBufferBinds(0), numGpuParamBufferBinds(0)
  29. { }
  30. UINT64 numDrawCalls;
  31. UINT64 numComputeCalls;
  32. UINT64 numRenderTargetChanges;
  33. UINT64 numPresents;
  34. UINT64 numClears;
  35. UINT64 numVertices;
  36. UINT64 numPrimitives;
  37. UINT64 numPipelineStateChanges;
  38. UINT64 numTextureBinds;
  39. UINT64 numSamplerBinds;
  40. UINT64 numVertexBufferBinds;
  41. UINT64 numIndexBufferBinds;
  42. UINT64 numGpuParamBufferBinds;
  43. UINT64 numResourceWrites;
  44. UINT64 numResourceReads;
  45. UINT64 numObjectsCreated;
  46. UINT64 numObjectsDestroyed;
  47. };
  48. /**
  49. * Tracks various render system statistics.
  50. *
  51. * @note Core thread only.
  52. */
  53. class BS_CORE_EXPORT RenderStats : public Module<RenderStats>
  54. {
  55. public:
  56. /** Increments draw call counter indicating how many times were render system API Draw methods called. */
  57. void incNumDrawCalls() { mData.numDrawCalls++; }
  58. /** Increments compute call counter indicating how many times were compute shaders dispatched. */
  59. void incNumComputeCalls() { mData.numComputeCalls++; }
  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 pipeline state change counter indicating how many times was a pipeline state bound. */
  74. void incNumPipelineStateChanges() { mData.numPipelineStateChanges++; }
  75. /** Increments texture change counter indicating how many times was a texture bound to the pipeline. */
  76. void incNumTextureBinds() { mData.numTextureBinds++; }
  77. /** Increments sampler state change counter indicating how many times was a sampler state bound to the pipeline. */
  78. void incNumSamplerBinds() { mData.numSamplerBinds++; }
  79. /** Increments vertex buffer change counter indicating how many times was a vertex buffer bound to the pipeline. */
  80. void incNumVertexBufferBinds() { mData.numVertexBufferBinds++; }
  81. /** Increments index buffer change counter indicating how many times was a index buffer bound to the pipeline. */
  82. void incNumIndexBufferBinds() { mData.numIndexBufferBinds++; }
  83. /**
  84. * Increments GPU parameter buffer change counter indicating how many times was a GPU parameter buffer bound to the
  85. * pipeline.
  86. */
  87. void incNumGpuParamBufferBinds() { mData.numGpuParamBufferBinds++; }
  88. /**
  89. * Increments created GPU resource counter.
  90. *
  91. * @param[in] category Category of the resource.
  92. */
  93. void incResCreated(UINT32 category)
  94. {
  95. // TODO - I'm ignoring resourceType for now. Later I will want to
  96. // count object creation/destruction/read/write per type. I will
  97. // also want to allow the caller to assign names to specific "resourceType" id.
  98. // (Since many types will be RenderAPI specific).
  99. // TODO - I should also track number of active GPU objects using this method, instead
  100. // of just keeping track of how many were created and destroyed during the frame.
  101. mData.numObjectsCreated++;
  102. }
  103. /**
  104. * Increments destroyed GPU resource counter.
  105. *
  106. * @param[in] category Category of the resource.
  107. */
  108. void incResDestroyed(UINT32 category) { mData.numObjectsDestroyed++; }
  109. /**
  110. * Increments GPU resource read counter.
  111. *
  112. * @param[in] category Category of the resource.
  113. */
  114. void incResRead(UINT32 category) { mData.numResourceReads++; }
  115. /**
  116. * Increments GPU resource write counter.
  117. *
  118. * @param[in] category Category of the resource.
  119. */
  120. void incResWrite(UINT32 category) { mData.numResourceWrites++; }
  121. /**
  122. * Returns an object containing various rendering statistics.
  123. *
  124. * @note
  125. * Do not modify the returned state unless you know what you are doing, it will change the actual internal object.
  126. */
  127. RenderStatsData& getData() { return mData; }
  128. private:
  129. RenderStatsData mData;
  130. };
  131. #if BS_PROFILING_ENABLED
  132. #define BS_INC_RENDER_STAT_CAT(Stat, Category) RenderStats::instance().inc##Stat((UINT32)Category)
  133. #define BS_INC_RENDER_STAT(Stat) RenderStats::instance().inc##Stat()
  134. #define BS_ADD_RENDER_STAT(Stat, Count) RenderStats::instance().add##Stat(Count)
  135. #else
  136. #define BS_INC_RENDER_STAT_CAT(Stat, Category)
  137. #define BS_INC_RENDER_STAT(Stat)
  138. #define BS_ADD_RENDER_STAT(Stat, Count)
  139. #endif
  140. /** @} */
  141. }