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.

196 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. #ifdef DEMO_MSAA
  74. glfwWindowHint(GLFW_SAMPLES, 4);
  75. #endif
  76. window = glfwCreateWindow(1000, 600, "NanoVG", NULL, NULL);
  77. // window = glfwCreateWindow(1000, 600, "NanoVG", glfwGetPrimaryMonitor(), NULL);
  78. if (!window) {
  79. glfwTerminate();
  80. return -1;
  81. }
  82. glfwSetKeyCallback(window, key);
  83. glfwMakeContextCurrent(window);
  84. #ifdef NANOVG_GLEW
  85. glewExperimental = GL_TRUE;
  86. if(glewInit() != GLEW_OK) {
  87. printf("Could not init glew.\n");
  88. return -1;
  89. }
  90. // GLEW generates GL error because it calls glGetString(GL_EXTENSIONS), we'll consume it here.
  91. glGetError();
  92. #endif
  93. #ifdef DEMO_MSAA
  94. vg = nvgCreateGL3(512, 512, 0);
  95. #else
  96. vg = nvgCreateGL3(512, 512, NVG_ANTIALIAS);
  97. #endif
  98. if (vg == NULL) {
  99. printf("Could not init nanovg.\n");
  100. return -1;
  101. }
  102. if (loadDemoData(vg, &data) == -1)
  103. return -1;
  104. glfwSwapInterval(0);
  105. initGPUTimer(&gpuTimer);
  106. glfwSetTime(0);
  107. prevt = glfwGetTime();
  108. while (!glfwWindowShouldClose(window))
  109. {
  110. double mx, my, t, dt;
  111. int winWidth, winHeight;
  112. int fbWidth, fbHeight;
  113. float pxRatio;
  114. float gpuTimes[3];
  115. int i, n;
  116. t = glfwGetTime();
  117. dt = t - prevt;
  118. prevt = t;
  119. startGPUTimer(&gpuTimer);
  120. glfwGetCursorPos(window, &mx, &my);
  121. glfwGetWindowSize(window, &winWidth, &winHeight);
  122. glfwGetFramebufferSize(window, &fbWidth, &fbHeight);
  123. // Calculate pixel ration for hi-dpi devices.
  124. pxRatio = (float)fbWidth / (float)winWidth;
  125. // Update and render
  126. glViewport(0, 0, fbWidth, fbHeight);
  127. if (premult)
  128. glClearColor(0,0,0,0);
  129. else
  130. glClearColor(0.3f, 0.3f, 0.32f, 1.0f);
  131. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
  132. nvgBeginFrame(vg, winWidth, winHeight, pxRatio, premult ? NVG_PREMULTIPLIED_ALPHA : NVG_STRAIGHT_ALPHA);
  133. renderDemo(vg, mx,my, winWidth,winHeight, t, blowup, &data);
  134. renderGraph(vg, 5,5, &fps);
  135. renderGraph(vg, 5+200+5,5, &cpuGraph);
  136. if (gpuTimer.supported)
  137. renderGraph(vg, 5+200+5+200+5,5, &gpuGraph);
  138. nvgEndFrame(vg);
  139. // Measure the CPU time taken excluding swap buffers (as the swap may wait for GPU)
  140. cpuTime = glfwGetTime() - t;
  141. updateGraph(&fps, dt);
  142. updateGraph(&cpuGraph, cpuTime);
  143. // We may get multiple results.
  144. n = stopGPUTimer(&gpuTimer, gpuTimes, 3);
  145. for (i = 0; i < n; i++)
  146. updateGraph(&gpuGraph, gpuTimes[i]);
  147. if (screenshot) {
  148. screenshot = 0;
  149. saveScreenShot(fbWidth, fbHeight, premult, "dump.png");
  150. }
  151. glfwSwapBuffers(window);
  152. glfwPollEvents();
  153. }
  154. freeDemoData(vg, &data);
  155. nvgDeleteGL3(vg);
  156. printf("Average Frame Time: %.2f ms\n", getGraphAverage(&fps) * 1000.0f);
  157. printf(" CPU Time: %.2f ms\n", getGraphAverage(&cpuGraph) * 1000.0f);
  158. printf(" GPU Time: %.2f ms\n", getGraphAverage(&gpuGraph) * 1000.0f);
  159. glfwTerminate();
  160. return 0;
  161. }