windows.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * Copyright 2011-2015 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #include "common.h"
  6. #include "bgfx_utils.h"
  7. #include <entry/input.h>
  8. #include <bx/string.h>
  9. struct PosColorVertex
  10. {
  11. float m_x;
  12. float m_y;
  13. float m_z;
  14. uint32_t m_abgr;
  15. static void init()
  16. {
  17. ms_decl
  18. .begin()
  19. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  20. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  21. .end();
  22. };
  23. static bgfx::VertexDecl ms_decl;
  24. };
  25. bgfx::VertexDecl PosColorVertex::ms_decl;
  26. static PosColorVertex s_cubeVertices[8] =
  27. {
  28. {-1.0f, 1.0f, 1.0f, 0xff000000 },
  29. { 1.0f, 1.0f, 1.0f, 0xff0000ff },
  30. {-1.0f, -1.0f, 1.0f, 0xff00ff00 },
  31. { 1.0f, -1.0f, 1.0f, 0xff00ffff },
  32. {-1.0f, 1.0f, -1.0f, 0xffff0000 },
  33. { 1.0f, 1.0f, -1.0f, 0xffff00ff },
  34. {-1.0f, -1.0f, -1.0f, 0xffffff00 },
  35. { 1.0f, -1.0f, -1.0f, 0xffffffff },
  36. };
  37. static const uint16_t s_cubeIndices[36] =
  38. {
  39. 0, 1, 2, // 0
  40. 1, 3, 2,
  41. 4, 6, 5, // 2
  42. 5, 6, 7,
  43. 0, 2, 4, // 4
  44. 4, 2, 6,
  45. 1, 5, 3, // 6
  46. 5, 7, 3,
  47. 0, 4, 1, // 8
  48. 4, 5, 1,
  49. 2, 3, 6, // 10
  50. 6, 3, 7,
  51. };
  52. #define MAX_WINDOWS 8
  53. entry::WindowState windows[MAX_WINDOWS];
  54. void cmdCreateWindow(const void* /*_userData*/)
  55. {
  56. entry::WindowHandle handle = entry::createWindow(rand()%1280, rand()%720, 640, 480);
  57. if (entry::isValid(handle) )
  58. {
  59. char str[256];
  60. bx::snprintf(str, BX_COUNTOF(str), "Window - handle %d", handle.idx);
  61. entry::setWindowTitle(handle, str);
  62. windows[handle.idx].m_handle = handle;
  63. }
  64. }
  65. void cmdDestroyWindow(const void* /*_userData*/)
  66. {
  67. for (uint32_t ii = 0; ii < MAX_WINDOWS; ++ii)
  68. {
  69. if (entry::isValid(windows[ii].m_handle) )
  70. {
  71. entry::destroyWindow(windows[ii].m_handle);
  72. windows[ii].m_handle.idx = UINT16_MAX;
  73. return;
  74. }
  75. }
  76. }
  77. static const InputBinding s_bindings[] =
  78. {
  79. { entry::Key::KeyC, entry::Modifier::None, 1, cmdCreateWindow, NULL },
  80. { entry::Key::KeyD, entry::Modifier::None, 1, cmdDestroyWindow, NULL },
  81. INPUT_BINDING_END
  82. };
  83. int _main_(int /*_argc*/, char** /*_argv*/)
  84. {
  85. uint32_t width = 1280;
  86. uint32_t height = 720;
  87. uint32_t debug = BGFX_DEBUG_TEXT;
  88. uint32_t reset = BGFX_RESET_VSYNC;
  89. bgfx::init();
  90. bgfx::reset(width, height, reset);
  91. const bgfx::Caps* caps = bgfx::getCaps();
  92. bool swapChainSupported = 0 != (caps->supported & BGFX_CAPS_SWAP_CHAIN);
  93. if (swapChainSupported)
  94. {
  95. inputAddBindings("22-windows", s_bindings);
  96. }
  97. // Enable debug text.
  98. bgfx::setDebug(debug);
  99. // Set view 0 clear state.
  100. bgfx::setViewClear(0
  101. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  102. , 0x303030ff
  103. , 1.0f
  104. , 0
  105. );
  106. // Create vertex stream declaration.
  107. PosColorVertex::init();
  108. // Create static vertex buffer.
  109. bgfx::VertexBufferHandle vbh = bgfx::createVertexBuffer(
  110. // Static data can be passed with bgfx::makeRef
  111. bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) )
  112. , PosColorVertex::ms_decl
  113. );
  114. // Create static index buffer.
  115. bgfx::IndexBufferHandle ibh = bgfx::createIndexBuffer(
  116. // Static data can be passed with bgfx::makeRef
  117. bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) )
  118. );
  119. // Create program from shaders.
  120. bgfx::ProgramHandle program = loadProgram("vs_cubes", "fs_cubes");
  121. float at[3] = { 0.0f, 0.0f, 0.0f };
  122. float eye[3] = { 0.0f, 0.0f, -35.0f };
  123. int64_t timeOffset = bx::getHPCounter();
  124. bgfx::FrameBufferHandle fbh[MAX_WINDOWS];
  125. memset(fbh, 0xff, sizeof(fbh) );
  126. entry::WindowState state;
  127. while (!entry::processWindowEvents(state, debug, reset) )
  128. {
  129. if (isValid(state.m_handle) )
  130. {
  131. if (0 == state.m_handle.idx)
  132. {
  133. width = state.m_width;
  134. height = state.m_height;
  135. }
  136. else
  137. {
  138. uint8_t viewId = (uint8_t)state.m_handle.idx;
  139. entry::WindowState& win = windows[viewId];
  140. if (win.m_nwh != state.m_nwh
  141. || (win.m_width != state.m_width
  142. || win.m_height != state.m_height) )
  143. {
  144. // When window changes size or native window handle changed
  145. // frame buffer must be recreated.
  146. if (bgfx::isValid(fbh[viewId]) )
  147. {
  148. bgfx::destroyFrameBuffer(fbh[viewId]);
  149. fbh[viewId].idx = bgfx::invalidHandle;
  150. }
  151. win.m_nwh = state.m_nwh;
  152. win.m_width = state.m_width;
  153. win.m_height = state.m_height;
  154. if (NULL != win.m_nwh)
  155. {
  156. fbh[viewId] = bgfx::createFrameBuffer(win.m_nwh, win.m_width, win.m_height);
  157. }
  158. else
  159. {
  160. win.m_handle.idx = UINT16_MAX;
  161. }
  162. }
  163. }
  164. }
  165. float view[16];
  166. float proj[16];
  167. bx::mtxLookAt(view, eye, at);
  168. bx::mtxProj(proj, 60.0f, float(width)/float(height), 0.1f, 100.0f);
  169. bgfx::setViewTransform(0, view, proj);
  170. bgfx::setViewRect(0, 0, 0, width, height);
  171. // This dummy draw call is here to make sure that view 0 is cleared
  172. // if no other draw calls are submitted to view 0.
  173. bgfx::submit(0);
  174. // Set view and projection matrix for view 0.
  175. for (uint32_t ii = 1; ii < MAX_WINDOWS; ++ii)
  176. {
  177. bgfx::setViewTransform(ii, view, proj);
  178. bgfx::setViewFrameBuffer(ii, fbh[ii]);
  179. if (!bgfx::isValid(fbh[ii]) )
  180. {
  181. // Set view to default viewport.
  182. bgfx::setViewRect(ii, 0, 0, width, height);
  183. bgfx::setViewClear(ii, BGFX_CLEAR_NONE);
  184. }
  185. else
  186. {
  187. bgfx::setViewRect(ii, 0, 0, windows[ii].m_width, windows[ii].m_height);
  188. bgfx::setViewClear(ii
  189. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  190. , 0x303030ff
  191. , 1.0f
  192. , 0
  193. );
  194. }
  195. }
  196. int64_t now = bx::getHPCounter();
  197. static int64_t last = now;
  198. const int64_t frameTime = now - last;
  199. last = now;
  200. const double freq = double(bx::getHPFrequency() );
  201. const double toMs = 1000.0/freq;
  202. float time = (float)( (now-timeOffset)/double(bx::getHPFrequency() ) );
  203. // Use debug font to print information about this example.
  204. bgfx::dbgTextClear();
  205. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/22-windows");
  206. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Rendering into multiple windows.");
  207. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  208. if (swapChainSupported)
  209. {
  210. bgfx::dbgTextPrintf(0, 5, 0x2f, "Press 'c' to create or 'd' to destroy window.");
  211. }
  212. else
  213. {
  214. bool blink = uint32_t(time*3.0f)&1;
  215. bgfx::dbgTextPrintf(0, 5, blink ? 0x1f : 0x01, " Multiple windows is not supported by `%s` renderer. ", bgfx::getRendererName(caps->rendererType) );
  216. }
  217. uint32_t count = 0;
  218. // Submit 11x11 cubes.
  219. for (uint32_t yy = 0; yy < 11; ++yy)
  220. {
  221. for (uint32_t xx = 0; xx < 11; ++xx)
  222. {
  223. float mtx[16];
  224. bx::mtxRotateXY(mtx, time + xx*0.21f, time + yy*0.37f);
  225. mtx[12] = -15.0f + float(xx)*3.0f;
  226. mtx[13] = -15.0f + float(yy)*3.0f;
  227. mtx[14] = 0.0f;
  228. // Set model matrix for rendering.
  229. bgfx::setTransform(mtx);
  230. // Set vertex and fragment shaders.
  231. bgfx::setProgram(program);
  232. // Set vertex and index buffer.
  233. bgfx::setVertexBuffer(vbh);
  234. bgfx::setIndexBuffer(ibh);
  235. // Set render states.
  236. bgfx::setState(BGFX_STATE_DEFAULT);
  237. // Submit primitive for rendering to view 0.
  238. bgfx::submit(count%MAX_WINDOWS);
  239. ++count;
  240. }
  241. }
  242. // Advance to next frame. Rendering thread will be kicked to
  243. // process submitted rendering primitives.
  244. bgfx::frame();
  245. }
  246. for (uint32_t ii = 0; ii < MAX_WINDOWS; ++ii)
  247. {
  248. if (bgfx::isValid(fbh[ii]) )
  249. {
  250. bgfx::destroyFrameBuffer(fbh[ii]);
  251. }
  252. }
  253. // entry::destroyWindow(win.m_handle);
  254. // Cleanup.
  255. bgfx::destroyIndexBuffer(ibh);
  256. bgfx::destroyVertexBuffer(vbh);
  257. bgfx::destroyProgram(program);
  258. // Shutdown bgfx.
  259. bgfx::shutdown();
  260. return 0;
  261. }