BsRenderStats.h 5.5 KB

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