BsRenderStats.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. //__________________________ Banshee Project - A modern game development toolkit _________________________________//
  2. //_____________________________________ www.banshee-project.com __________________________________________________//
  3. //________________________ Copyright (c) 2014 Marko Pintera. All rights reserved. ________________________________//
  4. #pragma once
  5. #include "BsCorePrerequisites.h"
  6. #include "BsModule.h"
  7. namespace BansheeEngine
  8. {
  9. /**
  10. * @brief Common types to track resource statistics for.
  11. */
  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. /**
  23. * @brief Object that stores various render statistics.
  24. */
  25. struct BS_CORE_EXPORT RenderStatsData
  26. {
  27. RenderStatsData()
  28. : numDrawCalls(0), numRenderTargetChanges(0), numPresents(0), numClears(0),
  29. numVertices(0), numPrimitives(0), numBlendStateChanges(0), numRasterizerStateChanges(0),
  30. numDepthStencilStateChanges(0), numTextureBinds(0), numSamplerBinds(0), numVertexBufferBinds(0),
  31. numIndexBufferBinds(0), numGpuParamBufferBinds(0), numGpuProgramBinds(0)
  32. { }
  33. UINT64 numDrawCalls;
  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. * @brief 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
  62. * render system API Draw methods called. */
  63. void incNumDrawCalls() { mData.numDrawCalls++; }
  64. /** Increments render target change counter indicating how many
  65. * times did the active render target change. */
  66. void incNumRenderTargetChanges() { mData.numRenderTargetChanges++; }
  67. /** Increments render target present counter indicating how many
  68. * times did the buffer swap happen. */
  69. void incNumPresents() { mData.numPresents++; }
  70. /** Increments render target clear counter indicating how many
  71. * times did the target the cleared, entirely or partially. */
  72. void incNumClears() { mData.numClears++; }
  73. /** Increments vertex draw counter indicating how many
  74. * vertices were sent to the pipeline. */
  75. void addNumVertices(UINT32 count) { mData.numVertices += count; }
  76. /** Increments primitive draw counter indicating how many
  77. * primitives were sent to the pipeline. */
  78. void addNumPrimitives(UINT32 count) { mData.numPrimitives += count; }
  79. /** Increments blend state change counter indicating how many
  80. * times was a blend state bound to the pipeline. */
  81. void incNumBlendStateChanges() { mData.numBlendStateChanges++; }
  82. /** Increments rasterizer state change counter indicating how many
  83. * times was a rasterizer state bound to the pipeline. */
  84. void incNumRasterizerStateChanges() { mData.numRasterizerStateChanges++; }
  85. /** Increments depth/stencil state change counter indicating how many
  86. * times was a depth/stencil state bound to the pipeline. */
  87. void incNumDepthStencilStateChanges() { mData.numDepthStencilStateChanges++; }
  88. /** Increments texture change counter indicating how many
  89. * times was a texture bound to the pipeline. */
  90. void incNumTextureBinds() { mData.numTextureBinds++; }
  91. /** Increments sampler state change counter indicating how many
  92. * times was a sampler state bound to the pipeline. */
  93. void incNumSamplerBinds() { mData.numSamplerBinds++; }
  94. /** Increments vertex buffer change counter indicating how many
  95. * times was a vertex buffer bound to the pipeline. */
  96. void incNumVertexBufferBinds() { mData.numVertexBufferBinds++; }
  97. /** Increments index buffer change counter indicating how many
  98. * times was a index buffer bound to the pipeline. */
  99. void incNumIndexBufferBinds() { mData.numIndexBufferBinds++; }
  100. /** Increments GPU parameter buffer change counter indicating how many
  101. * times was a GPU parameter buffer bound to the pipeline. */
  102. void incNumGpuParamBufferBinds() { mData.numGpuParamBufferBinds++; }
  103. /** Increments GPU program change counter indicating how many
  104. * times was a GPU program bound to the pipeline. */
  105. void incNumGpuProgramBinds() { mData.numGpuProgramBinds++; }
  106. /**
  107. * Increments created GPU resource counter.
  108. *
  109. * @param category Category of the resource.
  110. */
  111. void incResCreated(UINT32 category)
  112. {
  113. // TODO - I'm ignoring resourceType for now. Later I will want to
  114. // count object creation/destruction/read/write per type. I will
  115. // also want to allow the caller to assign names to specific "resourceType" id.
  116. // (Since many types will be RenderSystem specific).
  117. // TODO - I should also track number of active GPU objects using this method, instead
  118. // of just keeping track of how many were created and destroyed during the frame.
  119. mData.numObjectsCreated++;
  120. }
  121. /**
  122. * Increments destroyed GPU resource counter.
  123. *
  124. * @param category Category of the resource.
  125. */
  126. void incResDestroyed(UINT32 category) { mData.numObjectsDestroyed++; }
  127. /**
  128. * Increments GPU resource read counter.
  129. *
  130. * @param category Category of the resource.
  131. */
  132. void incResRead(UINT32 category) { mData.numResourceReads++; }
  133. /**
  134. * Increments GPU resource write counter.
  135. *
  136. * @param category Category of the resource.
  137. */
  138. void incResWrite(UINT32 category) { mData.numResourceWrites++; }
  139. /**
  140. * Returns an object containing various rendering statistics.
  141. *
  142. * @note Do not modify the returned state unless you know what you are doing, it will
  143. * change the actual internal object.
  144. */
  145. RenderStatsData& getData() { return mData; }
  146. private:
  147. RenderStatsData mData;
  148. };
  149. #if BS_PROFILING_ENABLED
  150. #define BS_INC_RENDER_STAT_CAT(Stat, Category) RenderStats::instance().inc##Stat##((UINT32)##Category##)
  151. #define BS_INC_RENDER_STAT(Stat) RenderStats::instance().inc##Stat##()
  152. #define BS_ADD_RENDER_STAT(Stat, Count) RenderStats::instance().add##Stat##(##Count##)
  153. #else
  154. #define BS_INC_RENDER_STAT_CAT(Stat, Category)
  155. #define BS_INC_RENDER_STAT(Stat)
  156. #define BS_ADD_RENDER_STAT(Stat, Count)
  157. #endif
  158. }