You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
4.1KB

  1. #include "perf.h"
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <math.h>
  5. #ifdef NANOVG_GLEW
  6. # include <GL/glew.h>
  7. #endif
  8. #include <GLFW/glfw3.h>
  9. #include "nanovg.h"
  10. #ifdef _MSC_VER
  11. #define snprintf _snprintf
  12. #else
  13. #include <iconv.h>
  14. #endif
  15. // timer query support
  16. #ifndef GL_ARB_timer_query
  17. #define GL_TIME_ELAPSED 0x88BF
  18. typedef void (APIENTRY *pfnGLGETQUERYOBJECTUI64V)(GLuint id, GLenum pname, GLuint64* params);
  19. pfnGLGETQUERYOBJECTUI64V glGetQueryObjectui64v = 0;
  20. #endif
  21. void initGPUTimer(struct GPUtimer* timer)
  22. {
  23. memset(timer, 0, sizeof(*timer));
  24. timer->supported = glfwExtensionSupported("GL_ARB_timer_query");
  25. if (timer->supported) {
  26. #ifndef GL_ARB_timer_query
  27. glGetQueryObjectui64v = (pfnGLGETQUERYOBJECTUI64V)glfwGetProcAddress("glGetQueryObjectui64v");
  28. if (!glGetQueryObjectui64v) {
  29. timer->supported = GL_FALSE;
  30. return;
  31. }
  32. #endif
  33. glGenQueries(GPU_QUERY_COUNT, timer->queries);
  34. }
  35. }
  36. void startGPUTimer(struct GPUtimer* timer)
  37. {
  38. if (!timer->supported)
  39. return;
  40. glBeginQuery(GL_TIME_ELAPSED, timer->queries[timer->cur % GPU_QUERY_COUNT] );
  41. timer->cur++;
  42. }
  43. int stopGPUTimer(struct GPUtimer* timer, float* times, int maxTimes)
  44. {
  45. GLint available = 1;
  46. int n = 0;
  47. if (!timer->supported)
  48. return 0;
  49. glEndQuery(GL_TIME_ELAPSED);
  50. while (available && timer->ret <= timer->cur) {
  51. // check for results if there are any
  52. glGetQueryObjectiv(timer->queries[timer->ret % GPU_QUERY_COUNT], GL_QUERY_RESULT_AVAILABLE, &available);
  53. if (available) {
  54. GLuint64 timeElapsed = 0;
  55. glGetQueryObjectui64v(timer->queries[timer->ret % GPU_QUERY_COUNT], GL_QUERY_RESULT, &timeElapsed);
  56. timer->ret++;
  57. if (n < maxTimes) {
  58. times[n] = (float)((double)timeElapsed * 1e-9);
  59. n++;
  60. }
  61. }
  62. }
  63. return n;
  64. }
  65. void initGraph(struct PerfGraph* fps, int style, const char* name)
  66. {
  67. memset(fps, 0, sizeof(struct PerfGraph));
  68. fps->style = style;
  69. strncpy(fps->name, name, sizeof(fps->name));
  70. fps->name[sizeof(fps->name)-1] = '\0';
  71. }
  72. void updateGraph(struct PerfGraph* fps, float frameTime)
  73. {
  74. fps->head = (fps->head+1) % GRAPH_HISTORY_COUNT;
  75. fps->values[fps->head] = frameTime;
  76. }
  77. float getGraphAverage(struct PerfGraph* fps)
  78. {
  79. int i;
  80. float avg = 0;
  81. for (i = 0; i < GRAPH_HISTORY_COUNT; i++) {
  82. avg += fps->values[i];
  83. }
  84. return avg / (float)GRAPH_HISTORY_COUNT;
  85. }
  86. void renderGraph(struct NVGcontext* vg, float x, float y, struct PerfGraph* fps)
  87. {
  88. int i;
  89. float avg, w, h;
  90. char str[64];
  91. avg = getGraphAverage(fps);
  92. w = 200;
  93. h = 35;
  94. nvgBeginPath(vg);
  95. nvgRect(vg, x,y, w,h);
  96. nvgFillColor(vg, nvgRGBA(0,0,0,128));
  97. nvgFill(vg);
  98. nvgBeginPath(vg);
  99. nvgMoveTo(vg, x, y+h);
  100. if (fps->style == GRAPH_RENDER_FPS) {
  101. for (i = 0; i < GRAPH_HISTORY_COUNT; i++) {
  102. float v = 1.0f / (0.00001f + fps->values[(fps->head+i) % GRAPH_HISTORY_COUNT]);
  103. float vx, vy;
  104. if (v > 80.0f) v = 80.0f;
  105. vx = x + ((float)i/(GRAPH_HISTORY_COUNT-1)) * w;
  106. vy = y + h - ((v / 80.0f) * h);
  107. nvgLineTo(vg, vx, vy);
  108. }
  109. } else {
  110. for (i = 0; i < GRAPH_HISTORY_COUNT; i++) {
  111. float v = fps->values[(fps->head+i) % GRAPH_HISTORY_COUNT] * 1000.0f;
  112. float vx, vy;
  113. if (v > 20.0f) v = 20.0f;
  114. vx = x + ((float)i/(GRAPH_HISTORY_COUNT-1)) * w;
  115. vy = y + h - ((v / 20.0f) * h);
  116. nvgLineTo(vg, vx, vy);
  117. }
  118. }
  119. nvgLineTo(vg, x+w, y+h);
  120. nvgFillColor(vg, nvgRGBA(255,192,0,128));
  121. nvgFill(vg);
  122. nvgFontFace(vg, "sans");
  123. if (fps->name[0] != '\0') {
  124. nvgFontSize(vg, 14.0f);
  125. nvgTextAlign(vg, NVG_ALIGN_LEFT|NVG_ALIGN_TOP);
  126. nvgFillColor(vg, nvgRGBA(240,240,240,192));
  127. nvgText(vg, x+3,y+1, fps->name, NULL);
  128. }
  129. if (fps->style == GRAPH_RENDER_FPS) {
  130. nvgFontSize(vg, 18.0f);
  131. nvgTextAlign(vg,NVG_ALIGN_RIGHT|NVG_ALIGN_TOP);
  132. nvgFillColor(vg, nvgRGBA(240,240,240,255));
  133. sprintf(str, "%.2f FPS", 1.0f / avg);
  134. nvgText(vg, x+w-3,y+1, str, NULL);
  135. nvgFontSize(vg, 15.0f);
  136. nvgTextAlign(vg,NVG_ALIGN_RIGHT|NVG_ALIGN_BOTTOM);
  137. nvgFillColor(vg, nvgRGBA(240,240,240,160));
  138. sprintf(str, "%.2f ms", avg * 1000.0f);
  139. nvgText(vg, x+w-3,y+h-1, str, NULL);
  140. } else {
  141. nvgFontSize(vg, 18.0f);
  142. nvgTextAlign(vg,NVG_ALIGN_RIGHT|NVG_ALIGN_TOP);
  143. nvgFillColor(vg, nvgRGBA(240,240,240,255));
  144. sprintf(str, "%.2f ms", avg * 1000.0f);
  145. nvgText(vg, x+w-3,y+1, str, NULL);
  146. }
  147. }