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.

198 lines
5.1KB

  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. #include <GLFW/glfw3.h>
  26. #include "nanovg.h"
  27. #define NANOVG_GL3_IMPLEMENTATION
  28. #include "nanovg_gl.h"
  29. #include "demo.h"
  30. #include "perf.h"
  31. void errorcb(int error, const char* desc)
  32. {
  33. printf("GLFW error %d: %s\n", error, desc);
  34. }
  35. int blowup = 0;
  36. int screenshot = 0;
  37. int premult = 0;
  38. static void key(GLFWwindow* window, int key, int scancode, int action, int mods)
  39. {
  40. NVG_NOTUSED(scancode);
  41. NVG_NOTUSED(mods);
  42. if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  43. glfwSetWindowShouldClose(window, GL_TRUE);
  44. if (key == GLFW_KEY_SPACE && action == GLFW_PRESS)
  45. blowup = !blowup;
  46. if (key == GLFW_KEY_S && action == GLFW_PRESS)
  47. screenshot = 1;
  48. if (key == GLFW_KEY_P && action == GLFW_PRESS)
  49. premult = !premult;
  50. }
  51. int main()
  52. {
  53. GLFWwindow* window;
  54. struct DemoData data;
  55. struct NVGcontext* vg = NULL;
  56. struct GPUtimer gpuTimer;
  57. struct PerfGraph fps, cpuGraph, gpuGraph;
  58. double prevt = 0, cpuTime = 0;
  59. if (!glfwInit()) {
  60. printf("Failed to init GLFW.");
  61. return -1;
  62. }
  63. initGraph(&fps, GRAPH_RENDER_FPS, "Frame Time");
  64. initGraph(&cpuGraph, GRAPH_RENDER_MS, "CPU Time");
  65. initGraph(&gpuGraph, GRAPH_RENDER_MS, "GPU Time");
  66. glfwSetErrorCallback(errorcb);
  67. #ifndef _WIN32 // don't require this on win32, and works with more cards
  68. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  69. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
  70. glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  71. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  72. #endif
  73. glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, 1);
  74. #ifdef DEMO_MSAA
  75. glfwWindowHint(GLFW_SAMPLES, 4);
  76. #endif
  77. window = glfwCreateWindow(1000, 600, "NanoVG", NULL, NULL);
  78. // window = glfwCreateWindow(1000, 600, "NanoVG", glfwGetPrimaryMonitor(), NULL);
  79. if (!window) {
  80. glfwTerminate();
  81. return -1;
  82. }
  83. glfwSetKeyCallback(window, key);
  84. glfwMakeContextCurrent(window);
  85. #ifdef NANOVG_GLEW
  86. glewExperimental = GL_TRUE;
  87. if(glewInit() != GLEW_OK) {
  88. printf("Could not init glew.\n");
  89. return -1;
  90. }
  91. // GLEW generates GL error because it calls glGetString(GL_EXTENSIONS), we'll consume it here.
  92. glGetError();
  93. #endif
  94. #ifdef DEMO_MSAA
  95. vg = nvgCreateGL3(NVG_STENCIL_STROKES);
  96. #else
  97. vg = nvgCreateGL3(NVG_ANTIALIAS | NVG_STENCIL_STROKES);
  98. #endif
  99. if (vg == NULL) {
  100. printf("Could not init nanovg.\n");
  101. return -1;
  102. }
  103. if (loadDemoData(vg, &data) == -1)
  104. return -1;
  105. glfwSwapInterval(0);
  106. initGPUTimer(&gpuTimer);
  107. glfwSetTime(0);
  108. prevt = glfwGetTime();
  109. while (!glfwWindowShouldClose(window))
  110. {
  111. double mx, my, t, dt;
  112. int winWidth, winHeight;
  113. int fbWidth, fbHeight;
  114. float pxRatio;
  115. float gpuTimes[3];
  116. int i, n;
  117. t = glfwGetTime();
  118. dt = t - prevt;
  119. prevt = t;
  120. startGPUTimer(&gpuTimer);
  121. glfwGetCursorPos(window, &mx, &my);
  122. glfwGetWindowSize(window, &winWidth, &winHeight);
  123. glfwGetFramebufferSize(window, &fbWidth, &fbHeight);
  124. // Calculate pixel ration for hi-dpi devices.
  125. pxRatio = (float)fbWidth / (float)winWidth;
  126. // Update and render
  127. glViewport(0, 0, fbWidth, fbHeight);
  128. if (premult)
  129. glClearColor(0,0,0,0);
  130. else
  131. glClearColor(0.3f, 0.3f, 0.32f, 1.0f);
  132. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
  133. nvgBeginFrame(vg, winWidth, winHeight, pxRatio);
  134. renderDemo(vg, mx,my, winWidth,winHeight, t, blowup, &data);
  135. renderGraph(vg, 5,5, &fps);
  136. renderGraph(vg, 5+200+5,5, &cpuGraph);
  137. if (gpuTimer.supported)
  138. renderGraph(vg, 5+200+5+200+5,5, &gpuGraph);
  139. nvgEndFrame(vg);
  140. // Measure the CPU time taken excluding swap buffers (as the swap may wait for GPU)
  141. cpuTime = glfwGetTime() - t;
  142. updateGraph(&fps, dt);
  143. updateGraph(&cpuGraph, cpuTime);
  144. // We may get multiple results.
  145. n = stopGPUTimer(&gpuTimer, gpuTimes, 3);
  146. for (i = 0; i < n; i++)
  147. updateGraph(&gpuGraph, gpuTimes[i]);
  148. if (screenshot) {
  149. screenshot = 0;
  150. saveScreenShot(fbWidth, fbHeight, premult, "dump.png");
  151. }
  152. glfwSwapBuffers(window);
  153. glfwPollEvents();
  154. }
  155. freeDemoData(vg, &data);
  156. nvgDeleteGL3(vg);
  157. printf("Average Frame Time: %.2f ms\n", getGraphAverage(&fps) * 1000.0f);
  158. printf(" CPU Time: %.2f ms\n", getGraphAverage(&cpuGraph) * 1000.0f);
  159. printf(" GPU Time: %.2f ms\n", getGraphAverage(&gpuGraph) * 1000.0f);
  160. glfwTerminate();
  161. return 0;
  162. }