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.

166 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. if (v > 80.0f) v = 80.0f;
  104. float vx = x + ((float)i/(GRAPH_HISTORY_COUNT-1)) * w;
  105. float vy = y + h - ((v / 80.0f) * h);
  106. nvgLineTo(vg, vx, vy);
  107. }
  108. } else {
  109. for (i = 0; i < GRAPH_HISTORY_COUNT; i++) {
  110. float v = fps->values[(fps->head+i) % GRAPH_HISTORY_COUNT] * 1000.0f;
  111. if (v > 20.0f) v = 20.0f;
  112. float vx = x + ((float)i/(GRAPH_HISTORY_COUNT-1)) * w;
  113. float vy = y + h - ((v / 20.0f) * h);
  114. nvgLineTo(vg, vx, vy);
  115. }
  116. }
  117. nvgLineTo(vg, x+w, y+h);
  118. nvgFillColor(vg, nvgRGBA(255,192,0,128));
  119. nvgFill(vg);
  120. nvgFontFace(vg, "sans");
  121. if (fps->name[0] != '\0') {
  122. nvgFontSize(vg, 14.0f);
  123. nvgTextAlign(vg, NVG_ALIGN_LEFT|NVG_ALIGN_TOP);
  124. nvgFillColor(vg, nvgRGBA(240,240,240,192));
  125. nvgText(vg, x+3,y+1, fps->name, NULL);
  126. }
  127. if (fps->style == GRAPH_RENDER_FPS) {
  128. nvgFontSize(vg, 18.0f);
  129. nvgTextAlign(vg,NVG_ALIGN_RIGHT|NVG_ALIGN_TOP);
  130. nvgFillColor(vg, nvgRGBA(240,240,240,255));
  131. sprintf(str, "%.2f FPS", 1.0f / avg);
  132. nvgText(vg, x+w-3,y+1, str, NULL);
  133. nvgFontSize(vg, 15.0f);
  134. nvgTextAlign(vg,NVG_ALIGN_RIGHT|NVG_ALIGN_BOTTOM);
  135. nvgFillColor(vg, nvgRGBA(240,240,240,160));
  136. sprintf(str, "%.2f ms", avg * 1000.0f);
  137. nvgText(vg, x+w-3,y+h-1, str, NULL);
  138. } else {
  139. nvgFontSize(vg, 18.0f);
  140. nvgTextAlign(vg,NVG_ALIGN_RIGHT|NVG_ALIGN_TOP);
  141. nvgFillColor(vg, nvgRGBA(240,240,240,255));
  142. sprintf(str, "%.2f ms", avg * 1000.0f);
  143. nvgText(vg, x+w-3,y+1, str, NULL);
  144. }
  145. }