dynamic.cpp 6.6 KB

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