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.

225 lines
6.0KB

  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 "nanovg_gl_utils.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. struct DemoData data;
  56. struct NVGcontext* vg = NULL;
  57. struct GPUtimer gpuTimer;
  58. struct PerfGraph fps, cpuGraph, gpuGraph;
  59. double prevt = 0, cpuTime = 0;
  60. struct NVGLUframebuffer* fb = NULL;
  61. if (!glfwInit()) {
  62. printf("Failed to init GLFW.");
  63. return -1;
  64. }
  65. initGraph(&fps, GRAPH_RENDER_FPS, "Frame Time");
  66. initGraph(&cpuGraph, GRAPH_RENDER_MS, "CPU Time");
  67. initGraph(&gpuGraph, GRAPH_RENDER_MS, "GPU Time");
  68. glfwSetErrorCallback(errorcb);
  69. #ifndef _WIN32 // don't require this on win32, and works with more cards
  70. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  71. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
  72. glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  73. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  74. #endif
  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(512, 512, 0);
  97. #else
  98. vg = nvgCreateGL3(512, 512, NVG_ANTIALIAS);
  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. fb = nvgluCreateFramebuffer(vg, 600, 600);
  111. while (!glfwWindowShouldClose(window))
  112. {
  113. double mx, my, t, dt;
  114. int winWidth, winHeight;
  115. int fbWidth, fbHeight;
  116. float pxRatio;
  117. float gpuTimes[3];
  118. int i, n;
  119. t = glfwGetTime();
  120. dt = t - prevt;
  121. prevt = t;
  122. startGPUTimer(&gpuTimer);
  123. glfwGetCursorPos(window, &mx, &my);
  124. glfwGetWindowSize(window, &winWidth, &winHeight);
  125. glfwGetFramebufferSize(window, &fbWidth, &fbHeight);
  126. // Calculate pixel ration for hi-dpi devices.
  127. pxRatio = (float)fbWidth / (float)winWidth;
  128. if (fb != NULL) {
  129. int fboWidth, fboHeight;
  130. nvgImageSize(vg, fb->image, &fboWidth, &fboHeight);
  131. // Draw some stull to an FBO as a test
  132. nvgluBindFramebuffer(fb);
  133. glViewport(0, 0, fboWidth, fboHeight);
  134. glClearColor(0, 0, 0, 0);
  135. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
  136. nvgBeginFrame(vg, fboWidth, fboHeight, pxRatio, NVG_PREMULTIPLIED_ALPHA);
  137. renderDemo(vg, mx, my, fboWidth, fboHeight, t, blowup, &data);
  138. nvgEndFrame(vg);
  139. nvgluBindFramebuffer(NULL);
  140. }
  141. // Update and render
  142. glViewport(0, 0, fbWidth, fbHeight);
  143. if (premult)
  144. glClearColor(0,0,0,0);
  145. else
  146. glClearColor(0.3f, 0.3f, 0.32f, 1.0f);
  147. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
  148. nvgBeginFrame(vg, winWidth, winHeight, pxRatio, premult ? NVG_PREMULTIPLIED_ALPHA : NVG_STRAIGHT_ALPHA);
  149. renderDemo(vg, mx,my, winWidth,winHeight, t, blowup, &data);
  150. renderGraph(vg, 5,5, &fps);
  151. renderGraph(vg, 5+200+5,5, &cpuGraph);
  152. if (gpuTimer.supported)
  153. renderGraph(vg, 5+200+5+200+5,5, &gpuGraph);
  154. if (fb != NULL) {
  155. struct NVGpaint img = nvgImagePattern(vg, 0, 0, 150, 150, 0, fb->image, 0);
  156. nvgBeginPath(vg);
  157. nvgTranslate(vg, 540, 300);
  158. nvgRect(vg, 0, 0, 150, 150);
  159. nvgFillPaint(vg, img);
  160. nvgFill(vg);
  161. }
  162. nvgEndFrame(vg);
  163. // Measure the CPU time taken excluding swap buffers (as the swap may wait for GPU)
  164. cpuTime = glfwGetTime() - t;
  165. updateGraph(&fps, dt);
  166. updateGraph(&cpuGraph, cpuTime);
  167. // We may get multiple results.
  168. n = stopGPUTimer(&gpuTimer, gpuTimes, 3);
  169. for (i = 0; i < n; i++)
  170. updateGraph(&gpuGraph, gpuTimes[i]);
  171. if (screenshot) {
  172. screenshot = 0;
  173. saveScreenShot(fbWidth, fbHeight, premult, "dump.png");
  174. }
  175. glfwSwapBuffers(window);
  176. glfwPollEvents();
  177. }
  178. freeDemoData(vg, &data);
  179. nvgluDeleteFramebuffer(vg, fb);
  180. nvgDeleteGL3(vg);
  181. printf("Average Frame Time: %.2f ms\n", getGraphAverage(&fps) * 1000.0f);
  182. printf(" CPU Time: %.2f ms\n", getGraphAverage(&cpuGraph) * 1000.0f);
  183. printf(" GPU Time: %.2f ms\n", getGraphAverage(&gpuGraph) * 1000.0f);
  184. glfwTerminate();
  185. return 0;
  186. }