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.

199 lines
5.2KB

  1. //
  2. // Copyright (c) 2013 Mikko Mononen memon@inside.org
  3. //
  4. // This software is provided 'as-is', without any express or implied
  5. // warranty. In no event will the authors be held liable for any damages
  6. // arising from the use of this software.
  7. // Permission is granted to anyone to use this software for any purpose,
  8. // including commercial applications, and to alter it and redistribute it
  9. // freely, subject to the following restrictions:
  10. // 1. The origin of this software must not be misrepresented; you must not
  11. // claim that you wrote the original software. If you use this software
  12. // in a product, an acknowledgment in the product documentation would be
  13. // appreciated but is not required.
  14. // 2. Altered source versions must be plainly marked as such, and must not be
  15. // misrepresented as being the original software.
  16. // 3. This notice may not be removed or altered from any source distribution.
  17. //
  18. #include <stdio.h>
  19. #ifdef NANOVG_GLEW
  20. # include <GL/glew.h>
  21. #endif
  22. #ifdef __APPLE__
  23. # define GLFW_INCLUDE_GLCOREARB
  24. #endif
  25. #define GLFW_INCLUDE_GLEXT
  26. #include <GLFW/glfw3.h>
  27. #include "nanovg.h"
  28. #define NANOVG_GL3_IMPLEMENTATION
  29. #include "nanovg_gl.h"
  30. #include "demo.h"
  31. #include "perf.h"
  32. void errorcb(int error, const char* desc)
  33. {
  34. printf("GLFW error %d: %s\n", error, desc);
  35. }
  36. int blowup = 0;
  37. int screenshot = 0;
  38. int premult = 0;
  39. static void key(GLFWwindow* window, int key, int scancode, int action, int mods)
  40. {
  41. NVG_NOTUSED(scancode);
  42. NVG_NOTUSED(mods);
  43. if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  44. glfwSetWindowShouldClose(window, GL_TRUE);
  45. if (key == GLFW_KEY_SPACE && action == GLFW_PRESS)
  46. blowup = !blowup;
  47. if (key == GLFW_KEY_S && action == GLFW_PRESS)
  48. screenshot = 1;
  49. if (key == GLFW_KEY_P && action == GLFW_PRESS)
  50. premult = !premult;
  51. }
  52. int main()
  53. {
  54. GLFWwindow* window;
  55. DemoData data;
  56. NVGcontext* vg = NULL;
  57. GPUtimer gpuTimer;
  58. PerfGraph fps, cpuGraph, gpuGraph;
  59. double prevt = 0, cpuTime = 0;
  60. if (!glfwInit()) {
  61. printf("Failed to init GLFW.");
  62. return -1;
  63. }
  64. initGraph(&fps, GRAPH_RENDER_FPS, "Frame Time");
  65. initGraph(&cpuGraph, GRAPH_RENDER_MS, "CPU Time");
  66. initGraph(&gpuGraph, GRAPH_RENDER_MS, "GPU Time");
  67. glfwSetErrorCallback(errorcb);
  68. #ifndef _WIN32 // don't require this on win32, and works with more cards
  69. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  70. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
  71. glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  72. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  73. #endif
  74. glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, 1);
  75. #ifdef DEMO_MSAA
  76. glfwWindowHint(GLFW_SAMPLES, 4);
  77. #endif
  78. window = glfwCreateWindow(1000, 600, "NanoVG", NULL, NULL);
  79. // window = glfwCreateWindow(1000, 600, "NanoVG", glfwGetPrimaryMonitor(), NULL);
  80. if (!window) {
  81. glfwTerminate();
  82. return -1;
  83. }
  84. glfwSetKeyCallback(window, key);
  85. glfwMakeContextCurrent(window);
  86. #ifdef NANOVG_GLEW
  87. glewExperimental = GL_TRUE;
  88. if(glewInit() != GLEW_OK) {
  89. printf("Could not init glew.\n");
  90. return -1;
  91. }
  92. // GLEW generates GL error because it calls glGetString(GL_EXTENSIONS), we'll consume it here.
  93. glGetError();
  94. #endif
  95. #ifdef DEMO_MSAA
  96. vg = nvgCreateGL3(NVG_STENCIL_STROKES | NVG_DEBUG);
  97. #else
  98. vg = nvgCreateGL3(NVG_ANTIALIAS | NVG_STENCIL_STROKES | NVG_DEBUG);
  99. #endif
  100. if (vg == NULL) {
  101. printf("Could not init nanovg.\n");
  102. return -1;
  103. }
  104. if (loadDemoData(vg, &data) == -1)
  105. return -1;
  106. glfwSwapInterval(0);
  107. initGPUTimer(&gpuTimer);
  108. glfwSetTime(0);
  109. prevt = glfwGetTime();
  110. while (!glfwWindowShouldClose(window))
  111. {
  112. double mx, my, t, dt;
  113. int winWidth, winHeight;
  114. int fbWidth, fbHeight;
  115. float pxRatio;
  116. float gpuTimes[3];
  117. int i, n;
  118. t = glfwGetTime();
  119. dt = t - prevt;
  120. prevt = t;
  121. startGPUTimer(&gpuTimer);
  122. glfwGetCursorPos(window, &mx, &my);
  123. glfwGetWindowSize(window, &winWidth, &winHeight);
  124. glfwGetFramebufferSize(window, &fbWidth, &fbHeight);
  125. // Calculate pixel ration for hi-dpi devices.
  126. pxRatio = (float)fbWidth / (float)winWidth;
  127. // Update and render
  128. glViewport(0, 0, fbWidth, fbHeight);
  129. if (premult)
  130. glClearColor(0,0,0,0);
  131. else
  132. glClearColor(0.3f, 0.3f, 0.32f, 1.0f);
  133. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
  134. nvgBeginFrame(vg, winWidth, winHeight, pxRatio);
  135. renderDemo(vg, mx,my, winWidth,winHeight, t, blowup, &data);
  136. renderGraph(vg, 5,5, &fps);
  137. renderGraph(vg, 5+200+5,5, &cpuGraph);
  138. if (gpuTimer.supported)
  139. renderGraph(vg, 5+200+5+200+5,5, &gpuGraph);
  140. nvgEndFrame(vg);
  141. // Measure the CPU time taken excluding swap buffers (as the swap may wait for GPU)
  142. cpuTime = glfwGetTime() - t;
  143. updateGraph(&fps, dt);
  144. updateGraph(&cpuGraph, cpuTime);
  145. // We may get multiple results.
  146. n = stopGPUTimer(&gpuTimer, gpuTimes, 3);
  147. for (i = 0; i < n; i++)
  148. updateGraph(&gpuGraph, gpuTimes[i]);
  149. if (screenshot) {
  150. screenshot = 0;
  151. saveScreenShot(fbWidth, fbHeight, premult, "dump.png");
  152. }
  153. glfwSwapBuffers(window);
  154. glfwPollEvents();
  155. }
  156. freeDemoData(vg, &data);
  157. nvgDeleteGL3(vg);
  158. printf("Average Frame Time: %.2f ms\n", getGraphAverage(&fps) * 1000.0f);
  159. printf(" CPU Time: %.2f ms\n", getGraphAverage(&cpuGraph) * 1000.0f);
  160. printf(" GPU Time: %.2f ms\n", getGraphAverage(&gpuGraph) * 1000.0f);
  161. glfwTerminate();
  162. return 0;
  163. }