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.

183 lines
4.7KB

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