BsGPUProfiler.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #pragma once
  2. #include "CmPrerequisites.h"
  3. #include "CmModule.h"
  4. #include "BsProfilerFwd.h"
  5. namespace BansheeEngine
  6. {
  7. /**
  8. * @brief Profiler report containing information about GPU sampling data
  9. * from a single frame.
  10. */
  11. struct GPUProfilerReport
  12. {
  13. /**
  14. * @brief Contains various profiler statistics about a single GPU profiling sample
  15. */
  16. struct Sample
  17. {
  18. String name; /**< Name of the sample for easier identification. */
  19. float timeMs; /**< Time in milliseconds it took to execute the sampled block. */
  20. UINT32 numDrawCalls; /**< Number of draw calls that happened. */
  21. UINT32 numRenderTargetChanges; /**< How many times was render target changed. */
  22. UINT32 numPresents; /**< How many times did a buffer swap happen on a double buffered render target. */
  23. UINT32 numClears; /**< How many times was render target cleared. */
  24. UINT32 numVertices; /**< Total number of vertices sent to the GPU. */
  25. UINT32 numTriangles; /**< Total number of triangles sent to the GPU. */
  26. UINT32 numDrawnSamples; /**< Number of samples drawn by the GPU. */
  27. UINT32 numBlendStateChanges; /**< How many times did the blend state change. */
  28. UINT32 numRasterizerStateChanges; /**< How many times did the rasterizer state change. */
  29. UINT32 numDepthStencilStateChanges; /**< How many times did the depth stencil state change. */
  30. UINT32 numTextureBinds; /**< How many times was a texture bound. */
  31. UINT32 numSamplerBinds; /**< How many times was a sampler bound. */
  32. UINT32 numVertexBufferBinds; /**< How many times was a vertex buffer bound. */
  33. UINT32 numIndexBufferBinds; /**< How many times was an index buffer bound. */
  34. UINT32 numGpuParamBufferBinds; /**< How many times was an GPU parameter buffer bound. */
  35. UINT32 numGpuProgramBinds; /**< How many times was a GPU program bound. */
  36. UINT32 numResourceWrites; /**< How many times were GPU resources written to. */
  37. UINT32 numResourceReads; /**< How many times were GPU resources read from. */
  38. UINT32 numObjectsCreated; /**< How many GPU objects were created. */
  39. UINT32 numObjectsDestroyed; /**< How many GPU objects were destroyed. */
  40. };
  41. Sample frameSample; /**< Sample containing data for entire frame. */
  42. Vector<Sample> samples;
  43. };
  44. /**
  45. * @brief Profiler that measures time and amount of various GPU operations.
  46. *
  47. * @note Sim thread only. However most operations will be queued on the core thread or
  48. * on the GPU itself so the results will not be immediately available.
  49. */
  50. class CM_EXPORT GPUProfiler : public Module<GPUProfiler>
  51. {
  52. /**
  53. * @brief Signals a start of a new frame. Every frame will generate a separate profiling report.
  54. * This call must be followed by "endFrame", and any sampling operations must happen between
  55. * beginFrame and endFrame.
  56. */
  57. void beginFrame();
  58. /**
  59. * @brief Signals an end of the currently sampled frame. Results of the sampling will be available
  60. * once getNumAvailableReports increments. This may take a while as the sampling is scheduled on
  61. * the core thread and on the GPU.
  62. */
  63. void endFrame();
  64. /**
  65. * @brief Begins sample measurement. Must be followed by endSample.
  66. *
  67. * @param name Unique name for the sample you can later use to find the sampling data.
  68. *
  69. * @note Must be called between beginFrame/endFrame calls.
  70. */
  71. void beginSample(const ProfilerString& name);
  72. /**
  73. * @brief Ends sample measurement.
  74. *
  75. * @param name Unique name for the sample.
  76. *
  77. * @note Unique name is primarily needed to more easily identify mismatched
  78. * begin/end sample pairs. Otherwise the name in beginSample would be enough.
  79. * Must be called between beginFrame/endFrame calls.
  80. */
  81. void endSample(const ProfilerString& name);
  82. /**
  83. * @brief Returns number of profiling reports that are ready but haven't been
  84. * retrieved yet.
  85. *
  86. * @note There is an internal limit of maximum number of available reports, where oldest ones will
  87. * get deleted so make sure to call this often if you don't want to miss some.
  88. */
  89. UINT32 getNumAvailableReports();
  90. /**
  91. * @brief Gets the oldest report available and removes it from the internal list.
  92. * Throws an exception if no reports are available.
  93. */
  94. const GPUProfilerReport& getNextReport();
  95. };
  96. }