windows.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. * Copyright 2011-2025 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  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_layout
  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::VertexLayout ms_layout;
  30. };
  31. bgfx::VertexLayout PosColorVertex::ms_layout;
  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, const char* _url)
  62. : entry::AppI(_name, _description, _url)
  63. {
  64. }
  65. void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override
  66. {
  67. Args args(_argc, _argv);
  68. m_width = _width;
  69. m_height = _height;
  70. m_debug = BGFX_DEBUG_TEXT;
  71. m_reset = BGFX_RESET_VSYNC;
  72. bgfx::Init init;
  73. init.type = args.m_type;
  74. init.vendorId = args.m_pciId;
  75. init.platformData.nwh = entry::getNativeWindowHandle(entry::kDefaultWindowHandle);
  76. init.platformData.ndt = entry::getNativeDisplayHandle();
  77. init.platformData.type = entry::getNativeWindowHandleType();
  78. init.resolution.width = m_width;
  79. init.resolution.height = m_height;
  80. init.resolution.reset = m_reset;
  81. bgfx::init(init);
  82. const bgfx::Caps* caps = bgfx::getCaps();
  83. bool swapChainSupported = 0 != (caps->supported & BGFX_CAPS_SWAP_CHAIN);
  84. if (swapChainSupported)
  85. {
  86. m_bindings = (InputBinding*)bx::alloc(entry::getAllocator(), sizeof(InputBinding)*3);
  87. m_bindings[0].set(entry::Key::KeyC, entry::Modifier::None, 1, cmdCreateWindow, this);
  88. m_bindings[1].set(entry::Key::KeyD, entry::Modifier::None, 1, cmdDestroyWindow, this);
  89. m_bindings[2].end();
  90. inputAddBindings("22-windows", m_bindings);
  91. }
  92. else
  93. {
  94. m_bindings = NULL;
  95. }
  96. // Enable m_debug text.
  97. bgfx::setDebug(m_debug);
  98. // Set view 0 clear state.
  99. bgfx::setViewClear(0
  100. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  101. , 0x303030ff
  102. , 1.0f
  103. , 0
  104. );
  105. // Create vertex stream declaration.
  106. PosColorVertex::init();
  107. // Create static vertex buffer.
  108. m_vbh = bgfx::createVertexBuffer(
  109. // Static data can be passed with bgfx::makeRef
  110. bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) )
  111. , PosColorVertex::ms_layout
  112. );
  113. // Create static index buffer.
  114. m_ibh = bgfx::createIndexBuffer(
  115. // Static data can be passed with bgfx::makeRef
  116. bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) )
  117. );
  118. // Create program from shaders.
  119. m_program = loadProgram("vs_cubes", "fs_cubes");
  120. m_timeOffset = bx::getHPCounter();
  121. bx::memSet(m_fbh, 0xff, sizeof(m_fbh) );
  122. imguiCreate();
  123. }
  124. virtual int shutdown() override
  125. {
  126. imguiDestroy();
  127. for (uint32_t ii = 0; ii < MAX_WINDOWS; ++ii)
  128. {
  129. if (bgfx::isValid(m_fbh[ii]) )
  130. {
  131. bgfx::destroy(m_fbh[ii]);
  132. }
  133. }
  134. inputRemoveBindings("22-windows");
  135. bx::free(entry::getAllocator(), m_bindings);
  136. // Cleanup.
  137. bgfx::destroy(m_ibh);
  138. bgfx::destroy(m_vbh);
  139. bgfx::destroy(m_program);
  140. // Shutdown bgfx.
  141. bgfx::shutdown();
  142. return 0;
  143. }
  144. bool update() override
  145. {
  146. if (!entry::processWindowEvents(m_state, m_debug, m_reset) )
  147. {
  148. entry::MouseState mouseState = m_state.m_mouse;
  149. if (isValid(m_state.m_handle) )
  150. {
  151. if (0 == m_state.m_handle.idx)
  152. {
  153. m_width = m_state.m_width;
  154. m_height = m_state.m_height;
  155. }
  156. else
  157. {
  158. uint8_t viewId = (uint8_t)m_state.m_handle.idx;
  159. entry::WindowState& win = m_windows[viewId];
  160. if (win.m_nwh != m_state.m_nwh
  161. || (win.m_width != m_state.m_width
  162. || win.m_height != m_state.m_height) )
  163. {
  164. // When window changes size or native window handle changed
  165. // frame buffer must be recreated.
  166. if (bgfx::isValid(m_fbh[viewId]) )
  167. {
  168. bgfx::destroy(m_fbh[viewId]);
  169. m_fbh[viewId].idx = bgfx::kInvalidHandle;
  170. }
  171. // Before we reattach a SwapChain to the window
  172. // we must actually free up the previous one.
  173. // The DestroyFrameBuffer command goes in the
  174. // cmdPost CommandBuffer, which happens after
  175. // the frame. The CreateFrameBuffer command goes
  176. // int the cmdPre CommandBuffer, which happens
  177. // at the beginning of the frame. Without this
  178. // bgfx::frame() call, the creation would happen
  179. // before it's destroyed, which would cause
  180. // the platform window to have two SwapChains
  181. // associated with it.
  182. // Ideally, we have an operation of ResizeFrameBuffer.
  183. bgfx::frame();
  184. win.m_nwh = m_state.m_nwh;
  185. win.m_width = m_state.m_width;
  186. win.m_height = m_state.m_height;
  187. if (NULL != win.m_nwh)
  188. {
  189. m_fbh[viewId] = bgfx::createFrameBuffer(win.m_nwh, uint16_t(win.m_width), uint16_t(win.m_height) );
  190. }
  191. else
  192. {
  193. win.m_handle.idx = UINT16_MAX;
  194. }
  195. }
  196. }
  197. }
  198. imguiBeginFrame(mouseState.m_mx
  199. , mouseState.m_my
  200. , (mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  201. | (mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  202. | (mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  203. , mouseState.m_mz
  204. , uint16_t(m_width)
  205. , uint16_t(m_height)
  206. );
  207. showExampleDialog(this);
  208. imguiEndFrame();
  209. const bx::Vec3 at = { 0.0f, 0.0f, 0.0f };
  210. const bx::Vec3 eye = { 0.0f, 0.0f, -35.0f };
  211. float view[16];
  212. bx::mtxLookAt(view, eye, at);
  213. float proj[16];
  214. bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth);
  215. bgfx::setViewTransform(0, view, proj);
  216. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  217. // This dummy draw call is here to make sure that view 0 is cleared
  218. // if no other draw calls are submitted to view 0.
  219. bgfx::touch(0);
  220. // Set view and projection matrix for view 0.
  221. for (uint8_t ii = 1; ii < MAX_WINDOWS; ++ii)
  222. {
  223. bgfx::setViewTransform(ii, view, proj);
  224. bgfx::setViewFrameBuffer(ii, m_fbh[ii]);
  225. if (!bgfx::isValid(m_fbh[ii]) )
  226. {
  227. // Set view to default viewport.
  228. bgfx::setViewRect(ii, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  229. bgfx::setViewClear(ii, BGFX_CLEAR_NONE);
  230. }
  231. else
  232. {
  233. bgfx::setViewRect(ii, 0, 0, uint16_t(m_windows[ii].m_width), uint16_t(m_windows[ii].m_height) );
  234. bgfx::setViewClear(ii
  235. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  236. , 0x303030ff
  237. , 1.0f
  238. , 0
  239. );
  240. }
  241. }
  242. int64_t now = bx::getHPCounter();
  243. float time = (float)( (now-m_timeOffset)/double(bx::getHPFrequency() ) );
  244. bgfx::dbgTextClear();
  245. if (NULL != m_bindings)
  246. {
  247. bgfx::dbgTextPrintf(0, 1, 0x2f, "Press 'c' to create or 'd' to destroy window.");
  248. }
  249. else
  250. {
  251. bool blink = uint32_t(time*3.0f)&1;
  252. bgfx::dbgTextPrintf(0, 0, blink ? 0x4f : 0x04, " Multiple windows is not supported by `%s` renderer. ", bgfx::getRendererName(bgfx::getCaps()->rendererType) );
  253. }
  254. uint32_t count = 0;
  255. // Submit 11x11 cubes.
  256. for (uint32_t yy = 0; yy < 11; ++yy)
  257. {
  258. for (uint32_t xx = 0; xx < 11; ++xx)
  259. {
  260. float mtx[16];
  261. bx::mtxRotateXY(mtx, time + xx*0.21f, time + yy*0.37f);
  262. mtx[12] = -15.0f + float(xx)*3.0f;
  263. mtx[13] = -15.0f + float(yy)*3.0f;
  264. mtx[14] = 0.0f;
  265. // Set model matrix for rendering.
  266. bgfx::setTransform(mtx);
  267. // Set vertex and index buffer.
  268. bgfx::setVertexBuffer(0, m_vbh);
  269. bgfx::setIndexBuffer(m_ibh);
  270. // Set render states.
  271. bgfx::setState(BGFX_STATE_DEFAULT);
  272. // Submit primitive for rendering.
  273. bgfx::submit(count%MAX_WINDOWS, m_program);
  274. ++count;
  275. }
  276. }
  277. // Advance to next frame. Rendering thread will be kicked to
  278. // process submitted rendering primitives.
  279. bgfx::frame();
  280. return true;
  281. }
  282. return false;
  283. }
  284. void createWindow()
  285. {
  286. entry::WindowHandle handle = entry::createWindow(rand()%1280, rand()%720, 640, 480);
  287. if (entry::isValid(handle) )
  288. {
  289. char str[256];
  290. bx::snprintf(str, BX_COUNTOF(str), "Window - handle %d", handle.idx);
  291. entry::setWindowTitle(handle, str);
  292. m_windows[handle.idx].m_handle = handle;
  293. }
  294. }
  295. void destroyWindow()
  296. {
  297. for (uint32_t ii = 0; ii < MAX_WINDOWS; ++ii)
  298. {
  299. if (bgfx::isValid(m_fbh[ii]) )
  300. {
  301. bgfx::destroy(m_fbh[ii]);
  302. m_fbh[ii].idx = bgfx::kInvalidHandle;
  303. // Flush destruction of swap chain before destroying window!
  304. bgfx::frame();
  305. bgfx::frame();
  306. }
  307. if (entry::isValid(m_windows[ii].m_handle) )
  308. {
  309. entry::destroyWindow(m_windows[ii].m_handle);
  310. m_windows[ii].m_handle.idx = UINT16_MAX;
  311. return;
  312. }
  313. }
  314. }
  315. entry::WindowState m_state;
  316. uint32_t m_width;
  317. uint32_t m_height;
  318. uint32_t m_debug;
  319. uint32_t m_reset;
  320. bgfx::VertexBufferHandle m_vbh;
  321. bgfx::IndexBufferHandle m_ibh;
  322. bgfx::ProgramHandle m_program;
  323. entry::WindowState m_windows[MAX_WINDOWS];
  324. bgfx::FrameBufferHandle m_fbh[MAX_WINDOWS];
  325. InputBinding* m_bindings;
  326. int64_t m_timeOffset;
  327. };
  328. } // namespace
  329. ENTRY_IMPLEMENT_MAIN(
  330. ExampleWindows
  331. , "22-windows"
  332. , "Rendering into multiple windows."
  333. , "https://bkaradzic.github.io/bgfx/examples.html#windows"
  334. );
  335. void cmdCreateWindow(const void* _userData)
  336. {
  337. ( (ExampleWindows*)_userData)->createWindow();
  338. }
  339. void cmdDestroyWindow(const void* _userData)
  340. {
  341. ( (ExampleWindows*)_userData)->destroyWindow();
  342. }