dynamic.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * Copyright 2011-2022 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 "imgui/imgui.h"
  8. #include <bx/rng.h>
  9. namespace
  10. {
  11. const uint32_t kDimWidth = 11;
  12. const uint32_t kDimHeight = 11;
  13. struct PosColorVertex
  14. {
  15. float m_x;
  16. float m_y;
  17. float m_z;
  18. uint32_t m_abgr;
  19. static void init()
  20. {
  21. ms_layout
  22. .begin()
  23. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  24. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  25. .end();
  26. };
  27. static bgfx::VertexLayout ms_layout;
  28. };
  29. bgfx::VertexLayout PosColorVertex::ms_layout;
  30. static PosColorVertex s_cubeVertices[] =
  31. {
  32. {-1.0f, 1.0f, 1.0f, 0xff000000 },
  33. { 1.0f, 1.0f, 1.0f, 0xff0000ff },
  34. {-1.0f, -1.0f, 1.0f, 0xff00ff00 },
  35. { 1.0f, -1.0f, 1.0f, 0xff00ffff },
  36. {-1.0f, 1.0f, -1.0f, 0xffff0000 },
  37. { 1.0f, 1.0f, -1.0f, 0xffff00ff },
  38. {-1.0f, -1.0f, -1.0f, 0xffffff00 },
  39. { 1.0f, -1.0f, -1.0f, 0xffffffff },
  40. };
  41. static const uint16_t s_cubeTriList[] =
  42. {
  43. 0, 1, 2, // 0
  44. 1, 3, 2,
  45. 4, 6, 5, // 2
  46. 5, 6, 7,
  47. 0, 2, 4, // 4
  48. 4, 2, 6,
  49. 1, 5, 3, // 6
  50. 5, 7, 3,
  51. 0, 4, 1, // 8
  52. 4, 5, 1,
  53. 2, 3, 6, // 10
  54. 6, 3, 7,
  55. };
  56. static const uint16_t s_cubeTriStrip[] =
  57. {
  58. 0, 1, 2,
  59. 3,
  60. 7,
  61. 1,
  62. 5,
  63. 0,
  64. 4,
  65. 2,
  66. 6,
  67. 7,
  68. 4,
  69. 5,
  70. };
  71. class ExampleDynamic : public entry::AppI
  72. {
  73. public:
  74. ExampleDynamic(const char* _name, const char* _description, const char* _url)
  75. : entry::AppI(_name, _description, _url)
  76. {
  77. }
  78. void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override
  79. {
  80. BX_UNUSED(s_cubeTriList, s_cubeTriStrip);
  81. Args args(_argc, _argv);
  82. m_width = _width;
  83. m_height = _height;
  84. m_debug = BGFX_DEBUG_NONE;
  85. m_reset = BGFX_RESET_VSYNC;
  86. bgfx::Init init;
  87. init.type = args.m_type;
  88. init.vendorId = args.m_pciId;
  89. init.platformData.nwh = entry::getNativeWindowHandle(entry::kDefaultWindowHandle);
  90. init.platformData.ndt = entry::getNativeDisplayHandle();
  91. init.resolution.width = m_width;
  92. init.resolution.height = m_height;
  93. init.resolution.reset = m_reset;
  94. bgfx::init(init);
  95. // Enable debug text.
  96. bgfx::setDebug(m_debug);
  97. // Set view 0 clear state.
  98. bgfx::setViewClear(0
  99. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  100. , 0x303030ff
  101. , 1.0f
  102. , 0
  103. );
  104. // Create vertex stream declaration.
  105. PosColorVertex::init();
  106. // Create static vertex buffer.
  107. for (uint32_t yy = 0; yy < kDimHeight; ++yy)
  108. {
  109. for (uint32_t xx = 0; xx < kDimWidth; ++xx)
  110. {
  111. m_vbh[yy*kDimWidth+xx] = bgfx::createDynamicVertexBuffer(
  112. // Static data can be passed with bgfx::makeRef
  113. bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) )
  114. , PosColorVertex::ms_layout
  115. );
  116. }
  117. }
  118. // Create static index buffer.
  119. m_ibh = bgfx::createDynamicIndexBuffer(
  120. // Static data can be passed with bgfx::makeRef
  121. bgfx::makeRef(s_cubeTriStrip, sizeof(s_cubeTriStrip) )
  122. );
  123. // Create program from shaders.
  124. m_program = loadProgram("vs_cubes", "fs_cubes");
  125. m_timeOffset = bx::getHPCounter();
  126. imguiCreate();
  127. }
  128. virtual int shutdown() override
  129. {
  130. imguiDestroy();
  131. // Cleanup.
  132. bgfx::destroy(m_ibh);
  133. for (uint32_t yy = 0; yy < kDimHeight; ++yy)
  134. {
  135. for (uint32_t xx = 0; xx < kDimWidth; ++xx)
  136. {
  137. bgfx::destroy(m_vbh[yy*kDimWidth+xx]);
  138. }
  139. }
  140. bgfx::destroy(m_program);
  141. // Shutdown bgfx.
  142. bgfx::shutdown();
  143. return 0;
  144. }
  145. bool update() override
  146. {
  147. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  148. {
  149. imguiBeginFrame(m_mouseState.m_mx
  150. , m_mouseState.m_my
  151. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  152. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  153. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  154. , m_mouseState.m_mz
  155. , uint16_t(m_width)
  156. , uint16_t(m_height)
  157. );
  158. showExampleDialog(this);
  159. imguiEndFrame();
  160. float time = (float)( (bx::getHPCounter()-m_timeOffset)/double(bx::getHPFrequency() ) );
  161. const bx::Vec3 at = { 0.0f, 0.0f, 0.0f };
  162. const bx::Vec3 eye = { 0.0f, 0.0f, -35.0f };
  163. // Set view and projection matrix for view 0.
  164. {
  165. float view[16];
  166. bx::mtxLookAt(view, eye, at);
  167. float proj[16];
  168. bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth);
  169. bgfx::setViewTransform(0, view, proj);
  170. // Set view 0 default viewport.
  171. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  172. }
  173. // This dummy draw call is here to make sure that view 0 is cleared
  174. // if no other draw calls are submitted to view 0.
  175. bgfx::touch(0);
  176. {
  177. float angle = bx::frnd(&m_mwc);
  178. float mtx[16];
  179. bx::mtxRotateZ(mtx, angle);
  180. const bgfx::Memory* mem = bgfx::copy(s_cubeVertices, sizeof(s_cubeVertices) );
  181. PosColorVertex* vertex = (PosColorVertex*)mem->data;
  182. const uint32_t abgr = m_mwc.gen();
  183. for (uint32_t ii = 0; ii < BX_COUNTOF(s_cubeVertices); ++ii)
  184. {
  185. bx::store(&vertex[ii].m_x, bx::mul(bx::load<bx::Vec3>(&s_cubeVertices[ii].m_x), mtx) );
  186. vertex[ii].m_abgr = abgr;
  187. }
  188. uint32_t idx = m_mwc.gen() % (kDimWidth*kDimHeight);
  189. bgfx::update(m_vbh[idx], 0, mem);
  190. }
  191. // Submit 11x11 cubes.
  192. for (uint32_t yy = 0; yy < kDimHeight; ++yy)
  193. {
  194. for (uint32_t xx = 0; xx < kDimWidth; ++xx)
  195. {
  196. float mtx[16];
  197. bx::mtxRotateXY(mtx, time + xx*0.21f, time + yy*0.37f);
  198. mtx[12] = -15.0f + float(xx)*3.0f;
  199. mtx[13] = -15.0f + float(yy)*3.0f;
  200. mtx[14] = 0.0f;
  201. // Set model matrix for rendering.
  202. bgfx::setTransform(mtx);
  203. // Set vertex and index buffer.
  204. bgfx::setVertexBuffer(0, m_vbh[yy*kDimWidth+xx]);
  205. bgfx::setIndexBuffer(m_ibh);
  206. // Set render states.
  207. bgfx::setState(0
  208. | BGFX_STATE_DEFAULT
  209. | BGFX_STATE_PT_TRISTRIP
  210. );
  211. // Submit primitive for rendering to view 0.
  212. bgfx::submit(0, m_program);
  213. }
  214. }
  215. // Advance to next frame. Rendering thread will be kicked to
  216. // process submitted rendering primitives.
  217. bgfx::frame();
  218. return true;
  219. }
  220. return false;
  221. }
  222. entry::MouseState m_mouseState;
  223. bx::RngMwc m_mwc;
  224. uint32_t m_width;
  225. uint32_t m_height;
  226. uint32_t m_debug;
  227. uint32_t m_reset;
  228. bgfx::DynamicVertexBufferHandle m_vbh[kDimWidth*kDimHeight];
  229. bgfx::DynamicIndexBufferHandle m_ibh;
  230. bgfx::ProgramHandle m_program;
  231. int64_t m_timeOffset;
  232. };
  233. } // namespace
  234. ENTRY_IMPLEMENT_MAIN(
  235. ExampleDynamic
  236. , "35-dynamic"
  237. , "Dynamic buffers update."
  238. , "https://bkaradzic.github.io/bgfx/examples.html#dynamic"
  239. );