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.

203 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. #define GLFW_NO_GLU
  23. #ifndef _WIN32
  24. # define GLFW_INCLUDE_GLCOREARB
  25. #endif
  26. #include <GLFW/glfw3.h>
  27. #include "nanovg.h"
  28. #define NANOVG_GL3_IMPLEMENTATION
  29. //#include "nanovg_gl3.h"
  30. #include "nanovg_gl3buf.h"
  31. #include "demo.h"
  32. #include "perf.h"
  33. void errorcb(int error, const char* desc)
  34. {
  35. printf("GLFW error %d: %s\n", error, desc);
  36. }
  37. int blowup = 0;
  38. int screenshot = 0;
  39. int premult = 0;
  40. static void key(GLFWwindow* window, int key, int scancode, int action, int mods)
  41. {
  42. NVG_NOTUSED(scancode);
  43. NVG_NOTUSED(mods);
  44. if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  45. glfwSetWindowShouldClose(window, GL_TRUE);
  46. if (key == GLFW_KEY_SPACE && action == GLFW_PRESS)
  47. blowup = !blowup;
  48. if (key == GLFW_KEY_S && action == GLFW_PRESS)
  49. screenshot = 1;
  50. if (key == GLFW_KEY_P && action == GLFW_PRESS)
  51. premult = !premult;
  52. }
  53. int main()
  54. {
  55. GLFWwindow* window;
  56. struct DemoData data;
  57. struct NVGcontext* vg = NULL;
  58. struct GPUtimer gpuTimer;
  59. struct PerfGraph fps, cpuGraph, gpuGraph;
  60. double prevt = 0, cpuTime = 0;
  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. #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. glEnable(GL_BLEND);
  133. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  134. glEnable(GL_CULL_FACE);
  135. glDisable(GL_DEPTH_TEST);
  136. nvgBeginFrame(vg, winWidth, winHeight, pxRatio, premult ? NVG_PREMULTIPLIED_ALPHA : NVG_STRAIGHT_ALPHA);
  137. renderDemo(vg, mx,my, winWidth,winHeight, t, blowup, &data);
  138. renderGraph(vg, 5,5, &fps);
  139. renderGraph(vg, 5+200+5,5, &cpuGraph);
  140. if (gpuTimer.supported)
  141. renderGraph(vg, 5+200+5+200+5,5, &gpuGraph);
  142. nvgEndFrame(vg);
  143. glEnable(GL_DEPTH_TEST);
  144. // Measure the CPU time taken excluding swap buffers (as the swap may wait for GPU)
  145. cpuTime = glfwGetTime() - t;
  146. updateGraph(&fps, dt);
  147. updateGraph(&cpuGraph, cpuTime);
  148. // We may get multiple results.
  149. n = stopGPUTimer(&gpuTimer, gpuTimes, 3);
  150. for (i = 0; i < n; i++)
  151. updateGraph(&gpuGraph, gpuTimes[i]);
  152. if (screenshot) {
  153. screenshot = 0;
  154. saveScreenShot(fbWidth, fbHeight, premult, "dump.png");
  155. }
  156. glfwSwapBuffers(window);
  157. glfwPollEvents();
  158. }
  159. freeDemoData(vg, &data);
  160. nvgDeleteGL3(vg);
  161. printf("Average Frame Time: %.2f ms\n", getGraphAverage(&fps) * 1000.0f);
  162. printf(" CPU Time: %.2f ms\n", getGraphAverage(&cpuGraph) * 1000.0f);
  163. printf(" GPU Time: %.2f ms\n", getGraphAverage(&gpuGraph) * 1000.0f);
  164. glfwTerminate();
  165. return 0;
  166. }