windows.cpp 8.5 KB

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