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.

216 lines
5.7KB

  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. // timer query support
  33. #ifndef GL_ARB_timer_query
  34. #define GL_TIME_ELAPSED 0x88BF
  35. typedef void (APIENTRY *pfnGLGETQUERYOBJECTUI64V)(GLuint id, GLenum pname, GLuint64* params);
  36. pfnGLGETQUERYOBJECTUI64V glGetQueryObjectui64v = 0;
  37. #endif
  38. void errorcb(int error, const char* desc)
  39. {
  40. printf("GLFW error %d: %s\n", error, desc);
  41. }
  42. int blowup = 0;
  43. static void key(GLFWwindow* window, int key, int scancode, int action, int mods)
  44. {
  45. NVG_NOTUSED(scancode);
  46. NVG_NOTUSED(mods);
  47. if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  48. glfwSetWindowShouldClose(window, GL_TRUE);
  49. if (key == GLFW_KEY_SPACE && action == GLFW_PRESS)
  50. blowup = !blowup;
  51. }
  52. enum numqueries {
  53. NUM_QUERIES = 5
  54. };
  55. int main()
  56. {
  57. GLFWwindow* window;
  58. struct DemoData data;
  59. struct NVGcontext* vg = NULL;
  60. struct FPScounter fps, cpuTimes, gpuTimes;
  61. double prevt = 0, cpuTime = 0;
  62. int timerquery = GL_FALSE, currquery = 0, retquery = 0;
  63. GLuint timerqueryid[NUM_QUERIES];
  64. if (!glfwInit()) {
  65. printf("Failed to init GLFW.");
  66. return -1;
  67. }
  68. initFPS(&fps);
  69. initFPS(&cpuTimes);
  70. initFPS(&gpuTimes);
  71. glfwSetErrorCallback(errorcb);
  72. #ifndef _WIN32 // don't require this on win32, and works with more cards
  73. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  74. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
  75. glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  76. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  77. #endif
  78. #ifdef DEMO_MSAA
  79. glfwWindowHint(GLFW_SAMPLES, 4);
  80. #endif
  81. window = glfwCreateWindow(1000, 600, "NanoVG", NULL, NULL);
  82. // window = glfwCreateWindow(1000, 600, "NanoVG", glfwGetPrimaryMonitor(), NULL);
  83. if (!window) {
  84. glfwTerminate();
  85. return -1;
  86. }
  87. glfwSetKeyCallback(window, key);
  88. glfwMakeContextCurrent(window);
  89. #ifdef NANOVG_GLEW
  90. glewExperimental = GL_TRUE;
  91. if(glewInit() != GLEW_OK) {
  92. printf("Could not init glew.\n");
  93. return -1;
  94. }
  95. #endif
  96. #ifdef DEMO_MSAA
  97. vg = nvgCreateGL3(512, 512, 0);
  98. #else
  99. vg = nvgCreateGL3(512, 512, NVG_ANTIALIAS);
  100. #endif
  101. if (vg == NULL) {
  102. printf("Could not init nanovg.\n");
  103. return -1;
  104. }
  105. if (loadDemoData(vg, &data) == -1)
  106. return -1;
  107. glfwSwapInterval(0);
  108. timerquery = glfwExtensionSupported("GL_ARB_timer_query");
  109. if( timerquery ) {
  110. #ifndef GL_ARB_timer_query
  111. glGetQueryObjectui64v = (pfnGLGETQUERYOBJECTUI64V)glfwGetProcAddress("glGetQueryObjectui64v");
  112. if( !glGetQueryObjectui64v )
  113. {
  114. timerquery = GL_FALSE;
  115. }
  116. #endif
  117. glGenQueries(NUM_QUERIES, timerqueryid);
  118. }
  119. glfwSetTime(0);
  120. prevt = glfwGetTime();
  121. while (!glfwWindowShouldClose(window))
  122. {
  123. double mx, my, t, dt;
  124. int winWidth, winHeight;
  125. int fbWidth, fbHeight;
  126. float pxRatio;
  127. t = glfwGetTime();
  128. dt = t - prevt;
  129. prevt = t;
  130. updateFPS(&fps, dt);
  131. updateFPS(&cpuTimes, cpuTime);
  132. if( timerquery ) {
  133. glBeginQuery(GL_TIME_ELAPSED, timerqueryid[currquery % NUM_QUERIES] );
  134. currquery = ++currquery;
  135. }
  136. glfwGetCursorPos(window, &mx, &my);
  137. glfwGetWindowSize(window, &winWidth, &winHeight);
  138. glfwGetFramebufferSize(window, &fbWidth, &fbHeight);
  139. // Calculate pixel ration for hi-dpi devices.
  140. pxRatio = (float)fbWidth / (float)winWidth;
  141. // Update and render
  142. glViewport(0, 0, fbWidth, fbHeight);
  143. glClearColor(0.3f, 0.3f, 0.32f, 1.0f);
  144. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
  145. glEnable(GL_BLEND);
  146. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  147. glEnable(GL_CULL_FACE);
  148. glDisable(GL_DEPTH_TEST);
  149. nvgBeginFrame(vg, winWidth, winHeight, pxRatio);
  150. renderDemo(vg, mx,my, winWidth,winHeight, t, blowup, &data);
  151. renderFPS(vg, 5,5, &fps);
  152. renderFPSEx(vg, 310,5, &cpuTimes, RENDER_MS, "CPU Time");
  153. renderFPSEx(vg, 615,5, &gpuTimes, RENDER_MS, "GPU Time");
  154. nvgEndFrame(vg);
  155. glEnable(GL_DEPTH_TEST);
  156. // Measure the CPU time taken excluding swap buffers (as the swap may wait for GPU)
  157. cpuTime = glfwGetTime() - t;
  158. if( timerquery ) {
  159. GLint available = 1;
  160. glEndQuery(GL_TIME_ELAPSED);
  161. while( available && retquery <= currquery ) {
  162. // check for results if there are any
  163. glGetQueryObjectiv(timerqueryid[retquery % NUM_QUERIES], GL_QUERY_RESULT_AVAILABLE, &available);
  164. if( available ) {
  165. GLuint64 timeElapsed = 0;
  166. double gpuTime;
  167. glGetQueryObjectui64v(timerqueryid[retquery % NUM_QUERIES], GL_QUERY_RESULT, &timeElapsed);
  168. retquery = ++retquery ;
  169. gpuTime = (double)timeElapsed * 1e-9;
  170. updateFPS(&gpuTimes, (float)gpuTime);
  171. }
  172. }
  173. }
  174. glfwSwapBuffers(window);
  175. glfwPollEvents();
  176. }
  177. freeDemoData(vg, &data);
  178. nvgDeleteGL3(vg);
  179. glfwTerminate();
  180. return 0;
  181. }