example_fbo.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. #ifdef NANOVG_GLAD
  26. # include <glad/glad.h>
  27. #else
  28. # define GLFW_INCLUDE_GLEXT
  29. #endif
  30. #include <GLFW/glfw3.h>
  31. #ifndef DEMO_ANTIALIAS
  32. # define DEMO_ANTIALIAS 1
  33. #endif
  34. #ifndef DEMO_STENCIL_STROKES
  35. # define DEMO_STENCIL_STROKES 1
  36. #endif
  37. #ifndef DEMO_MSAA
  38. # define DEMO_MSAA 0
  39. #endif
  40. #include "nanovg.h"
  41. #define NANOVG_GL3_IMPLEMENTATION
  42. #include "nanovg_gl.h"
  43. #include "nanovg_gl_utils.h"
  44. #include "perf.h"
  45. void renderPattern(NVGcontext* vg, NVGLUframebuffer* fb, float t, float pxRatio)
  46. {
  47. int winWidth, winHeight;
  48. int fboWidth, fboHeight;
  49. int pw, ph, x, y;
  50. float s = 20.0f;
  51. float sr = (cosf(t)+1)*0.5f;
  52. float r = s * 0.6f * (0.2f + 0.8f * sr);
  53. if (fb == NULL) return;
  54. nvgImageSize(vg, fb->image, &fboWidth, &fboHeight);
  55. winWidth = (int)(fboWidth / pxRatio);
  56. winHeight = (int)(fboHeight / pxRatio);
  57. // Draw some stuff to an FBO as a test
  58. nvgluBindFramebuffer(fb);
  59. glViewport(0, 0, fboWidth, fboHeight);
  60. glClearColor(0, 0, 0, 0);
  61. glClear(GL_COLOR_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
  62. nvgBeginFrame(vg, winWidth, winHeight, pxRatio);
  63. pw = (int)ceilf(winWidth / s);
  64. ph = (int)ceilf(winHeight / s);
  65. nvgBeginPath(vg);
  66. for (y = 0; y < ph; y++) {
  67. for (x = 0; x < pw; x++) {
  68. float cx = (x+0.5f) * s;
  69. float cy = (y+0.5f) * s;
  70. nvgCircle(vg, cx,cy, r);
  71. }
  72. }
  73. nvgFillColor(vg, nvgRGBA(220,160,0,200));
  74. nvgFill(vg);
  75. nvgEndFrame(vg);
  76. nvgluBindFramebuffer(NULL);
  77. }
  78. int loadFonts(NVGcontext* vg)
  79. {
  80. int font;
  81. font = nvgCreateFont(vg, "sans", "../example/Roboto-Regular.ttf");
  82. if (font == -1) {
  83. printf("Could not add font regular.\n");
  84. return -1;
  85. }
  86. font = nvgCreateFont(vg, "sans-bold", "../example/Roboto-Bold.ttf");
  87. if (font == -1) {
  88. printf("Could not add font bold.\n");
  89. return -1;
  90. }
  91. return 0;
  92. }
  93. void errorcb(int error, const char* desc)
  94. {
  95. printf("GLFW error %d: %s\n", error, desc);
  96. }
  97. static void key(GLFWwindow* window, int key, int scancode, int action, int mods)
  98. {
  99. NVG_NOTUSED(scancode);
  100. NVG_NOTUSED(mods);
  101. if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  102. glfwSetWindowShouldClose(window, GL_TRUE);
  103. }
  104. int main()
  105. {
  106. GLFWwindow* window;
  107. NVGcontext* vg = NULL;
  108. GPUtimer gpuTimer;
  109. PerfGraph fps, cpuGraph, gpuGraph;
  110. double prevt = 0, cpuTime = 0;
  111. NVGLUframebuffer* fb = NULL;
  112. int winWidth, winHeight;
  113. int fbWidth, fbHeight;
  114. float pxRatio;
  115. if (!glfwInit()) {
  116. printf("Failed to init GLFW.");
  117. return -1;
  118. }
  119. initGraph(&fps, GRAPH_RENDER_FPS, "Frame Time");
  120. initGraph(&cpuGraph, GRAPH_RENDER_MS, "CPU Time");
  121. initGraph(&gpuGraph, GRAPH_RENDER_MS, "GPU Time");
  122. glfwSetErrorCallback(errorcb);
  123. #ifndef _WIN32 // don't require this on win32, and works with more cards
  124. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  125. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
  126. glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  127. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  128. #endif
  129. glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, 1);
  130. #ifdef DEMO_MSAA
  131. glfwWindowHint(GLFW_SAMPLES, 4);
  132. #endif
  133. window = glfwCreateWindow(1000, 600, "NanoVG", NULL, NULL);
  134. // window = glfwCreateWindow(1000, 600, "NanoVG", glfwGetPrimaryMonitor(), NULL);
  135. if (!window) {
  136. glfwTerminate();
  137. return -1;
  138. }
  139. glfwSetKeyCallback(window, key);
  140. glfwMakeContextCurrent(window);
  141. #ifdef NANOVG_GLEW
  142. glewExperimental = GL_TRUE;
  143. if(glewInit() != GLEW_OK) {
  144. printf("Could not init glew.\n");
  145. return -1;
  146. }
  147. // GLEW generates GL error because it calls glGetString(GL_EXTENSIONS), we'll consume it here.
  148. glGetError();
  149. #endif
  150. #ifdef NANOVG_GLAD
  151. if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)){
  152. printf("Could not init glad.\n");
  153. return -1;
  154. }
  155. #endif
  156. int flags = 0;
  157. #ifndef NDEBUG
  158. flags |= NVG_DEBUG;
  159. #endif
  160. #if !DEMO_MSAA && DEMO_ANTIALIAS
  161. flags |= NVG_ANTIALIAS;
  162. #endif
  163. #if DEMO_STENCIL_STROKES
  164. flags |= NVG_STENCIL_STROKES;
  165. #endif
  166. vg = nvgCreateGL3(flags);
  167. if (vg == NULL) {
  168. printf("Could not init nanovg.\n");
  169. return -1;
  170. }
  171. // Create hi-dpi FBO for hi-dpi screens.
  172. glfwGetWindowSize(window, &winWidth, &winHeight);
  173. glfwGetFramebufferSize(window, &fbWidth, &fbHeight);
  174. // Calculate pixel ration for hi-dpi devices.
  175. pxRatio = (float)fbWidth / (float)winWidth;
  176. // The image pattern is tiled, set repeat on x and y.
  177. fb = nvgluCreateFramebuffer(vg, (int)(100*pxRatio), (int)(100*pxRatio), NVG_IMAGE_REPEATX | NVG_IMAGE_REPEATY);
  178. if (fb == NULL) {
  179. printf("Could not create FBO.\n");
  180. return -1;
  181. }
  182. if (loadFonts(vg) == -1) {
  183. printf("Could not load fonts\n");
  184. return -1;
  185. }
  186. glfwSwapInterval(0);
  187. initGPUTimer(&gpuTimer);
  188. glfwSetTime(0);
  189. prevt = glfwGetTime();
  190. while (!glfwWindowShouldClose(window))
  191. {
  192. double mx, my, t, dt;
  193. float gpuTimes[3];
  194. int i, n;
  195. t = glfwGetTime();
  196. dt = t - prevt;
  197. prevt = t;
  198. startGPUTimer(&gpuTimer);
  199. glfwGetCursorPos(window, &mx, &my);
  200. glfwGetWindowSize(window, &winWidth, &winHeight);
  201. glfwGetFramebufferSize(window, &fbWidth, &fbHeight);
  202. // Calculate pixel ration for hi-dpi devices.
  203. pxRatio = (float)fbWidth / (float)winWidth;
  204. renderPattern(vg, fb, t, pxRatio);
  205. // Update and render
  206. glViewport(0, 0, fbWidth, fbHeight);
  207. glClearColor(0.3f, 0.3f, 0.32f, 1.0f);
  208. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
  209. nvgBeginFrame(vg, winWidth, winHeight, pxRatio);
  210. // Use the FBO as image pattern.
  211. if (fb != NULL) {
  212. NVGpaint img = nvgImagePattern(vg, 0, 0, 100, 100, 0, fb->image, 1.0f);
  213. nvgSave(vg);
  214. for (i = 0; i < 20; i++) {
  215. nvgBeginPath(vg);
  216. nvgRect(vg, 10 + i*30,10, 10, winHeight-20);
  217. nvgFillColor(vg, nvgHSLA(i/19.0f, 0.5f, 0.5f, 255));
  218. nvgFill(vg);
  219. }
  220. nvgBeginPath(vg);
  221. nvgRoundedRect(vg, 140 + sinf(t*1.3f)*100, 140 + cosf(t*1.71244f)*100, 250, 250, 20);
  222. nvgFillPaint(vg, img);
  223. nvgFill(vg);
  224. nvgStrokeColor(vg, nvgRGBA(220,160,0,255));
  225. nvgStrokeWidth(vg, 3.0f);
  226. nvgStroke(vg);
  227. nvgRestore(vg);
  228. }
  229. renderGraph(vg, 5,5, &fps);
  230. renderGraph(vg, 5+200+5,5, &cpuGraph);
  231. if (gpuTimer.supported)
  232. renderGraph(vg, 5+200+5+200+5,5, &gpuGraph);
  233. nvgEndFrame(vg);
  234. // Measure the CPU time taken excluding swap buffers (as the swap may wait for GPU)
  235. cpuTime = glfwGetTime() - t;
  236. updateGraph(&fps, dt);
  237. updateGraph(&cpuGraph, cpuTime);
  238. // We may get multiple results.
  239. n = stopGPUTimer(&gpuTimer, gpuTimes, 3);
  240. for (i = 0; i < n; i++)
  241. updateGraph(&gpuGraph, gpuTimes[i]);
  242. glfwSwapBuffers(window);
  243. glfwPollEvents();
  244. }
  245. nvgluDeleteFramebuffer(fb);
  246. nvgDeleteGL3(vg);
  247. printf("Average Frame Time: %.2f ms\n", getGraphAverage(&fps) * 1000.0f);
  248. printf(" CPU Time: %.2f ms\n", getGraphAverage(&cpuGraph) * 1000.0f);
  249. printf(" GPU Time: %.2f ms\n", getGraphAverage(&gpuGraph) * 1000.0f);
  250. glfwTerminate();
  251. return 0;
  252. }