DPF OpenGL examples
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.

165 lines
4.2KB

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