gfxGLDeviceProfiler.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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) : mData(&data)
  41. {
  42. glGenQueries(1, &mQueryId);
  43. }
  44. GLTimer() : mName(NULL), mQueryId(0), mData(NULL)
  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. DefineConsoleFunction(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. }
  79. void GFXGLDevice::leaveDebugEvent()
  80. {
  81. #ifdef TORQUE_BASIC_GPU_PROFILER
  82. gfxProfiler.leaveDebugEvent();
  83. #endif
  84. }
  85. void GFXGLDevice::setDebugMarker(ColorI color, const char *name)
  86. {
  87. }
  88. #ifdef TORQUE_BASIC_GPU_PROFILER
  89. AFTER_MODULE_INIT(Sim)
  90. {
  91. // GFXGLDevice Profiler
  92. GuiCanvas::getGuiCanvasFrameSignal().notify(&gfxProfiler, &GFXProfiler<GLTimer>::onEndFrame);
  93. GFXDevice::getDeviceEventSignal().notify( &initGLProfiler );
  94. }
  95. #endif