gfxGLDeviceProfiler.cpp 2.8 KB

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