dynamic.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * Copyright 2011-2018 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 "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_decl
  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::VertexDecl ms_decl;
  28. };
  29. bgfx::VertexDecl PosColorVertex::ms_decl;
  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)
  75. : entry::AppI(_name, _description)
  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.resolution.width = m_width;
  90. init.resolution.height = m_height;
  91. init.resolution.reset = m_reset;
  92. bgfx::init(init);
  93. // Enable debug text.
  94. bgfx::setDebug(m_debug);
  95. // Set view 0 clear state.
  96. bgfx::setViewClear(0
  97. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  98. , 0x303030ff
  99. , 1.0f
  100. , 0
  101. );
  102. // Create vertex stream declaration.
  103. PosColorVertex::init();
  104. // Create static vertex buffer.
  105. for (uint32_t yy = 0; yy < kDimHeight; ++yy)
  106. {
  107. for (uint32_t xx = 0; xx < kDimWidth; ++xx)
  108. {
  109. m_vbh[yy*kDimWidth+xx] = bgfx::createDynamicVertexBuffer(
  110. // Static data can be passed with bgfx::makeRef
  111. bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) )
  112. , PosColorVertex::ms_decl
  113. );
  114. }
  115. }
  116. // Create static index buffer.
  117. m_ibh = bgfx::createDynamicIndexBuffer(
  118. // Static data can be passed with bgfx::makeRef
  119. bgfx::makeRef(s_cubeTriStrip, sizeof(s_cubeTriStrip) )
  120. );
  121. // Create program from shaders.
  122. m_program = loadProgram("vs_cubes", "fs_cubes");
  123. m_timeOffset = bx::getHPCounter();
  124. imguiCreate();
  125. }
  126. virtual int shutdown() override
  127. {
  128. imguiDestroy();
  129. // Cleanup.
  130. bgfx::destroy(m_ibh);
  131. for (uint32_t yy = 0; yy < kDimHeight; ++yy)
  132. {
  133. for (uint32_t xx = 0; xx < kDimWidth; ++xx)
  134. {
  135. bgfx::destroy(m_vbh[yy*kDimWidth+xx]);
  136. }
  137. }
  138. bgfx::destroy(m_program);
  139. // Shutdown bgfx.
  140. bgfx::shutdown();
  141. return 0;
  142. }
  143. bool update() override
  144. {
  145. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  146. {
  147. imguiBeginFrame(m_mouseState.m_mx
  148. , m_mouseState.m_my
  149. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  150. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  151. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  152. , m_mouseState.m_mz
  153. , uint16_t(m_width)
  154. , uint16_t(m_height)
  155. );
  156. showExampleDialog(this);
  157. imguiEndFrame();
  158. float time = (float)( (bx::getHPCounter()-m_timeOffset)/double(bx::getHPFrequency() ) );
  159. float at[3] = { 0.0f, 0.0f, 0.0f };
  160. float eye[3] = { 0.0f, 0.0f, -35.0f };
  161. // Set view and projection matrix for view 0.
  162. {
  163. float view[16];
  164. bx::mtxLookAt(view, eye, at);
  165. float proj[16];
  166. bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth);
  167. bgfx::setViewTransform(0, view, proj);
  168. // Set view 0 default viewport.
  169. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  170. }
  171. // This dummy draw call is here to make sure that view 0 is cleared
  172. // if no other draw calls are submitted to view 0.
  173. bgfx::touch(0);
  174. {
  175. float angle = bx::frnd(&m_mwc);
  176. float mtx[16];
  177. bx::mtxRotateZ(mtx, angle);
  178. const bgfx::Memory* mem = bgfx::copy(s_cubeVertices, sizeof(s_cubeVertices) );
  179. PosColorVertex* vertex = (PosColorVertex*)mem->data;
  180. const uint32_t abgr = m_mwc.gen();
  181. for (uint32_t ii = 0; ii < BX_COUNTOF(s_cubeVertices); ++ii)
  182. {
  183. bx::vec3MulMtx(&vertex[ii].m_x, &s_cubeVertices[ii].m_x, mtx);
  184. vertex[ii].m_abgr = abgr;
  185. }
  186. uint32_t idx = m_mwc.gen() % (kDimWidth*kDimHeight);
  187. bgfx::update(m_vbh[idx], 0, mem);
  188. }
  189. // Submit 11x11 cubes.
  190. for (uint32_t yy = 0; yy < kDimHeight; ++yy)
  191. {
  192. for (uint32_t xx = 0; xx < kDimWidth; ++xx)
  193. {
  194. float mtx[16];
  195. bx::mtxRotateXY(mtx, time + xx*0.21f, time + yy*0.37f);
  196. mtx[12] = -15.0f + float(xx)*3.0f;
  197. mtx[13] = -15.0f + float(yy)*3.0f;
  198. mtx[14] = 0.0f;
  199. // Set model matrix for rendering.
  200. bgfx::setTransform(mtx);
  201. // Set vertex and index buffer.
  202. bgfx::setVertexBuffer(0, m_vbh[yy*kDimWidth+xx]);
  203. bgfx::setIndexBuffer(m_ibh);
  204. // Set render states.
  205. bgfx::setState(0
  206. | BGFX_STATE_DEFAULT
  207. | BGFX_STATE_PT_TRISTRIP
  208. );
  209. // Submit primitive for rendering to view 0.
  210. bgfx::submit(0, m_program);
  211. }
  212. }
  213. // Advance to next frame. Rendering thread will be kicked to
  214. // process submitted rendering primitives.
  215. bgfx::frame();
  216. return true;
  217. }
  218. return false;
  219. }
  220. entry::MouseState m_mouseState;
  221. bx::RngMwc m_mwc;
  222. uint32_t m_width;
  223. uint32_t m_height;
  224. uint32_t m_debug;
  225. uint32_t m_reset;
  226. bgfx::DynamicVertexBufferHandle m_vbh[kDimWidth*kDimHeight];
  227. bgfx::DynamicIndexBufferHandle m_ibh;
  228. bgfx::ProgramHandle m_program;
  229. int64_t m_timeOffset;
  230. };
  231. } // namespace
  232. ENTRY_IMPLEMENT_MAIN(ExampleDynamic, "35-dynamic", "Dynamic buffers update.");