windows.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * Copyright 2011-2016 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-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. Args args(_argc, _argv);
  86. uint32_t width = 1280;
  87. uint32_t height = 720;
  88. uint32_t debug = BGFX_DEBUG_TEXT;
  89. uint32_t reset = BGFX_RESET_VSYNC;
  90. bgfx::init(args.m_type, args.m_pciId);
  91. bgfx::reset(width, height, reset);
  92. const bgfx::Caps* caps = bgfx::getCaps();
  93. bool swapChainSupported = 0 != (caps->supported & BGFX_CAPS_SWAP_CHAIN);
  94. if (swapChainSupported)
  95. {
  96. inputAddBindings("22-windows", s_bindings);
  97. }
  98. // Enable debug text.
  99. bgfx::setDebug(debug);
  100. // Set view 0 clear state.
  101. bgfx::setViewClear(0
  102. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  103. , 0x303030ff
  104. , 1.0f
  105. , 0
  106. );
  107. // Create vertex stream declaration.
  108. PosColorVertex::init();
  109. // Create static vertex buffer.
  110. bgfx::VertexBufferHandle vbh = bgfx::createVertexBuffer(
  111. // Static data can be passed with bgfx::makeRef
  112. bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) )
  113. , PosColorVertex::ms_decl
  114. );
  115. // Create static index buffer.
  116. bgfx::IndexBufferHandle ibh = bgfx::createIndexBuffer(
  117. // Static data can be passed with bgfx::makeRef
  118. bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) )
  119. );
  120. // Create program from shaders.
  121. bgfx::ProgramHandle program = loadProgram("vs_cubes", "fs_cubes");
  122. float at[3] = { 0.0f, 0.0f, 0.0f };
  123. float eye[3] = { 0.0f, 0.0f, -35.0f };
  124. int64_t timeOffset = bx::getHPCounter();
  125. bgfx::FrameBufferHandle fbh[MAX_WINDOWS];
  126. memset(fbh, 0xff, sizeof(fbh) );
  127. entry::WindowState state;
  128. while (!entry::processWindowEvents(state, debug, reset) )
  129. {
  130. if (isValid(state.m_handle) )
  131. {
  132. if (0 == state.m_handle.idx)
  133. {
  134. width = state.m_width;
  135. height = state.m_height;
  136. }
  137. else
  138. {
  139. uint8_t viewId = (uint8_t)state.m_handle.idx;
  140. entry::WindowState& win = windows[viewId];
  141. if (win.m_nwh != state.m_nwh
  142. || (win.m_width != state.m_width
  143. || win.m_height != state.m_height) )
  144. {
  145. // When window changes size or native window handle changed
  146. // frame buffer must be recreated.
  147. if (bgfx::isValid(fbh[viewId]) )
  148. {
  149. bgfx::destroyFrameBuffer(fbh[viewId]);
  150. fbh[viewId].idx = bgfx::invalidHandle;
  151. }
  152. win.m_nwh = state.m_nwh;
  153. win.m_width = state.m_width;
  154. win.m_height = state.m_height;
  155. if (NULL != win.m_nwh)
  156. {
  157. fbh[viewId] = bgfx::createFrameBuffer(win.m_nwh, win.m_width, win.m_height);
  158. }
  159. else
  160. {
  161. win.m_handle.idx = UINT16_MAX;
  162. }
  163. }
  164. }
  165. }
  166. float view[16];
  167. float proj[16];
  168. bx::mtxLookAt(view, eye, at);
  169. bx::mtxProj(proj, 60.0f, float(width)/float(height), 0.1f, 100.0f);
  170. bgfx::setViewTransform(0, view, proj);
  171. bgfx::setViewRect(0, 0, 0, width, height);
  172. // This dummy draw call is here to make sure that view 0 is cleared
  173. // if no other draw calls are submitted to view 0.
  174. bgfx::touch(0);
  175. // Set view and projection matrix for view 0.
  176. for (uint32_t ii = 1; ii < MAX_WINDOWS; ++ii)
  177. {
  178. bgfx::setViewTransform(ii, view, proj);
  179. bgfx::setViewFrameBuffer(ii, fbh[ii]);
  180. if (!bgfx::isValid(fbh[ii]) )
  181. {
  182. // Set view to default viewport.
  183. bgfx::setViewRect(ii, 0, 0, width, height);
  184. bgfx::setViewClear(ii, BGFX_CLEAR_NONE);
  185. }
  186. else
  187. {
  188. bgfx::setViewRect(ii, 0, 0, windows[ii].m_width, windows[ii].m_height);
  189. bgfx::setViewClear(ii
  190. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  191. , 0x303030ff
  192. , 1.0f
  193. , 0
  194. );
  195. }
  196. }
  197. int64_t now = bx::getHPCounter();
  198. static int64_t last = now;
  199. const int64_t frameTime = now - last;
  200. last = now;
  201. const double freq = double(bx::getHPFrequency() );
  202. const double toMs = 1000.0/freq;
  203. float time = (float)( (now-timeOffset)/double(bx::getHPFrequency() ) );
  204. // Use debug font to print information about this example.
  205. bgfx::dbgTextClear();
  206. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/22-windows");
  207. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Rendering into multiple windows.");
  208. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  209. if (swapChainSupported)
  210. {
  211. bgfx::dbgTextPrintf(0, 5, 0x2f, "Press 'c' to create or 'd' to destroy window.");
  212. }
  213. else
  214. {
  215. bool blink = uint32_t(time*3.0f)&1;
  216. bgfx::dbgTextPrintf(0, 5, blink ? 0x1f : 0x01, " Multiple windows is not supported by `%s` renderer. ", bgfx::getRendererName(caps->rendererType) );
  217. }
  218. uint32_t count = 0;
  219. // Submit 11x11 cubes.
  220. for (uint32_t yy = 0; yy < 11; ++yy)
  221. {
  222. for (uint32_t xx = 0; xx < 11; ++xx)
  223. {
  224. float mtx[16];
  225. bx::mtxRotateXY(mtx, time + xx*0.21f, time + yy*0.37f);
  226. mtx[12] = -15.0f + float(xx)*3.0f;
  227. mtx[13] = -15.0f + float(yy)*3.0f;
  228. mtx[14] = 0.0f;
  229. // Set model matrix for rendering.
  230. bgfx::setTransform(mtx);
  231. // Set vertex and index buffer.
  232. bgfx::setVertexBuffer(vbh);
  233. bgfx::setIndexBuffer(ibh);
  234. // Set render states.
  235. bgfx::setState(BGFX_STATE_DEFAULT);
  236. // Submit primitive for rendering.
  237. bgfx::submit(count%MAX_WINDOWS, program);
  238. ++count;
  239. }
  240. }
  241. // Advance to next frame. Rendering thread will be kicked to
  242. // process submitted rendering primitives.
  243. bgfx::frame();
  244. }
  245. for (uint32_t ii = 0; ii < MAX_WINDOWS; ++ii)
  246. {
  247. if (bgfx::isValid(fbh[ii]) )
  248. {
  249. bgfx::destroyFrameBuffer(fbh[ii]);
  250. }
  251. }
  252. // entry::destroyWindow(win.m_handle);
  253. // Cleanup.
  254. bgfx::destroyIndexBuffer(ibh);
  255. bgfx::destroyVertexBuffer(vbh);
  256. bgfx::destroyProgram(program);
  257. // Shutdown bgfx.
  258. bgfx::shutdown();
  259. return 0;
  260. }