windows.cpp 8.9 KB

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