gfxGLDeviceProfiler.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #include "gui/core/guiCanvas.h"
  2. #include "console/engineAPI.h"
  3. #include "console/script.h"
  4. #include "gfx/gfxDebugEvent.h"
  5. #include "gfx/gl/gfxGLDevice.h"
  6. #ifndef TORQUE_BASIC_GPU_PROFILER
  7. //#define TORQUE_BASIC_GPU_PROFILER
  8. #endif
  9. class GLTimer
  10. {
  11. public:
  12. void begin()
  13. {
  14. glBeginQuery(GL_TIME_ELAPSED, mQueryId);
  15. }
  16. void end()
  17. {
  18. glEndQuery(GL_TIME_ELAPSED);
  19. }
  20. F64 getTime()
  21. {
  22. GLuint64 time;
  23. glGetQueryObjectui64v(mQueryId, GL_QUERY_RESULT, &time);
  24. return static_cast<F64>(time)/1000000.0f;
  25. }
  26. class Data
  27. {
  28. public:
  29. Data() {}
  30. void init()
  31. {
  32. }
  33. void onBeginFrame()
  34. {
  35. }
  36. void onEndFrame()
  37. {
  38. }
  39. };
  40. typedef Data DataType;
  41. GLTimer(GFXDevice *device, Data &data) : mName(NULL), mData(&data)
  42. {
  43. glGenQueries(1, &mQueryId);
  44. }
  45. GLTimer() : mName(NULL), mData(NULL), mQueryId(0)
  46. {
  47. }
  48. GLTimer& operator=(const GLTimer &b)
  49. {
  50. mName = b.mName;
  51. mQueryId = b.mQueryId;
  52. return *this;
  53. }
  54. StringTableEntry mName;
  55. protected:
  56. Data *mData;
  57. GLuint mQueryId;
  58. };
  59. #ifdef TORQUE_BASIC_GPU_PROFILER
  60. #include "gfx/gfxProfiler.h"
  61. GFXProfiler<GLTimer> gfxProfiler;
  62. DefineEngineFunction(printGFXGLTimers, void,(), ,"")
  63. {
  64. gfxProfiler.printTimes();
  65. }
  66. #endif
  67. bool initGLProfiler(GFXDevice::GFXDeviceEventType ev)
  68. {
  69. if(ev != GFXDevice::deInit || GFX->getAdapterType() != OpenGL)
  70. return true;
  71. Con::evaluatef("GlobalActionMap.bindCmd(keyboard, \"alt F4\", \"printGFXGLTimers();\");");
  72. return true;
  73. }
  74. void GFXGLDevice::enterDebugEvent(ColorI color, const char *name)
  75. {
  76. #ifdef TORQUE_BASIC_GPU_PROFILER
  77. gfxProfiler.enterDebugEvent(color, name);
  78. #endif
  79. if (mCapabilities.khrDebug)
  80. {
  81. glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, name);
  82. }
  83. else if(mCapabilities.extDebugMarker)
  84. {
  85. glPushGroupMarkerEXT(0, name);
  86. }
  87. }
  88. void GFXGLDevice::leaveDebugEvent()
  89. {
  90. #ifdef TORQUE_BASIC_GPU_PROFILER
  91. gfxProfiler.leaveDebugEvent();
  92. #endif
  93. if (mCapabilities.khrDebug)
  94. {
  95. glPopDebugGroup();
  96. }
  97. else if(mCapabilities.extDebugMarker)
  98. {
  99. glPopGroupMarkerEXT();
  100. }
  101. }
  102. void GFXGLDevice::setDebugMarker(ColorI color, const char *name)
  103. {
  104. if (mCapabilities.khrDebug)
  105. {
  106. glDebugMessageInsert(GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_MARKER, 0,
  107. GL_DEBUG_SEVERITY_NOTIFICATION, -1, name);
  108. }
  109. else if(mCapabilities.extDebugMarker)
  110. {
  111. glInsertEventMarkerEXT(0, name);
  112. }
  113. }
  114. #ifdef TORQUE_BASIC_GPU_PROFILER
  115. AFTER_MODULE_INIT(Sim)
  116. {
  117. // GFXGLDevice Profiler
  118. GuiCanvas::getGuiCanvasFrameSignal().notify(&gfxProfiler, &GFXProfiler<GLTimer>::onEndFrame);
  119. GFXDevice::getDeviceEventSignal().notify( &initGLProfiler );
  120. }
  121. #endif