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.

169 lines
4.2KB

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