instancing.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. namespace
  9. {
  10. struct PosColorVertex
  11. {
  12. float m_x;
  13. float m_y;
  14. float m_z;
  15. uint32_t m_abgr;
  16. static void init()
  17. {
  18. ms_decl
  19. .begin()
  20. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  21. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  22. .end();
  23. };
  24. static bgfx::VertexDecl ms_decl;
  25. };
  26. bgfx::VertexDecl PosColorVertex::ms_decl;
  27. static PosColorVertex s_cubeVertices[8] =
  28. {
  29. {-1.0f, 1.0f, 1.0f, 0xff000000 },
  30. { 1.0f, 1.0f, 1.0f, 0xff0000ff },
  31. {-1.0f, -1.0f, 1.0f, 0xff00ff00 },
  32. { 1.0f, -1.0f, 1.0f, 0xff00ffff },
  33. {-1.0f, 1.0f, -1.0f, 0xffff0000 },
  34. { 1.0f, 1.0f, -1.0f, 0xffff00ff },
  35. {-1.0f, -1.0f, -1.0f, 0xffffff00 },
  36. { 1.0f, -1.0f, -1.0f, 0xffffffff },
  37. };
  38. static const uint16_t s_cubeIndices[36] =
  39. {
  40. 0, 1, 2, // 0
  41. 1, 3, 2,
  42. 4, 6, 5, // 2
  43. 5, 6, 7,
  44. 0, 2, 4, // 4
  45. 4, 2, 6,
  46. 1, 5, 3, // 6
  47. 5, 7, 3,
  48. 0, 4, 1, // 8
  49. 4, 5, 1,
  50. 2, 3, 6, // 10
  51. 6, 3, 7,
  52. };
  53. class ExampleInstancing : public entry::AppI
  54. {
  55. public:
  56. ExampleInstancing(const char* _name, const char* _description)
  57. : entry::AppI(_name, _description)
  58. {
  59. }
  60. void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override
  61. {
  62. Args args(_argc, _argv);
  63. m_width = _width;
  64. m_height = _height;
  65. m_debug = BGFX_DEBUG_TEXT;
  66. m_reset = BGFX_RESET_VSYNC;
  67. bgfx::init(args.m_type, args.m_pciId);
  68. bgfx::reset(m_width, m_height, m_reset);
  69. // Enable debug text.
  70. bgfx::setDebug(m_debug);
  71. // Set view 0 clear state.
  72. bgfx::setViewClear(0
  73. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  74. , 0x303030ff
  75. , 1.0f
  76. , 0
  77. );
  78. // Create vertex stream declaration.
  79. PosColorVertex::init();
  80. // Create static vertex buffer.
  81. m_vbh = bgfx::createVertexBuffer(
  82. bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) )
  83. , PosColorVertex::ms_decl
  84. );
  85. // Create static index buffer.
  86. m_ibh = bgfx::createIndexBuffer(
  87. bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) )
  88. );
  89. // Create program from shaders.
  90. m_program = loadProgram("vs_instancing", "fs_instancing");
  91. m_timeOffset = bx::getHPCounter();
  92. imguiCreate();
  93. }
  94. int shutdown() override
  95. {
  96. imguiDestroy();
  97. // Cleanup.
  98. bgfx::destroy(m_ibh);
  99. bgfx::destroy(m_vbh);
  100. bgfx::destroy(m_program);
  101. // Shutdown bgfx.
  102. bgfx::shutdown();
  103. return 0;
  104. }
  105. bool update() override
  106. {
  107. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  108. {
  109. imguiBeginFrame(m_mouseState.m_mx
  110. , m_mouseState.m_my
  111. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  112. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  113. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  114. , m_mouseState.m_mz
  115. , uint16_t(m_width)
  116. , uint16_t(m_height)
  117. );
  118. showExampleDialog(this);
  119. imguiEndFrame();
  120. // Set view 0 default viewport.
  121. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  122. // This dummy draw call is here to make sure that view 0 is cleared
  123. // if no other draw calls are submitted to view 0.
  124. bgfx::touch(0);
  125. float time = (float)( (bx::getHPCounter() - m_timeOffset)/double(bx::getHPFrequency() ) );
  126. // Get renderer capabilities info.
  127. const bgfx::Caps* caps = bgfx::getCaps();
  128. // Check if instancing is supported.
  129. if (0 == (BGFX_CAPS_INSTANCING & caps->supported) )
  130. {
  131. // When instancing is not supported by GPU, implement alternative
  132. // code path that doesn't use instancing.
  133. bool blink = uint32_t(time*3.0f)&1;
  134. bgfx::dbgTextPrintf(0, 0, blink ? 0x1f : 0x01, " Instancing is not supported by GPU. ");
  135. }
  136. else
  137. {
  138. float at[3] = { 0.0f, 0.0f, 0.0f };
  139. float eye[3] = { 0.0f, 0.0f, -35.0f };
  140. // Set view and projection matrix for view 0.
  141. const bgfx::HMD* hmd = bgfx::getHMD();
  142. if (NULL != hmd && 0 != (hmd->flags & BGFX_HMD_RENDERING) )
  143. {
  144. float view[16];
  145. bx::mtxQuatTranslationHMD(view, hmd->eye[0].rotation, eye);
  146. bgfx::setViewTransform(0, view, hmd->eye[0].projection, BGFX_VIEW_STEREO, hmd->eye[1].projection);
  147. // Set view 0 default viewport.
  148. //
  149. // Use HMD's width/height since HMD's internal frame buffer size
  150. // might be much larger than window size.
  151. bgfx::setViewRect(0, 0, 0, hmd->width, hmd->height);
  152. }
  153. else
  154. {
  155. float view[16];
  156. bx::mtxLookAt(view, eye, at);
  157. float proj[16];
  158. bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth);
  159. bgfx::setViewTransform(0, view, proj);
  160. // Set view 0 default viewport.
  161. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  162. }
  163. // 80 bytes stride = 64 bytes for 4x4 matrix + 16 bytes for RGBA color.
  164. const uint16_t instanceStride = 80;
  165. // 11x11 cubes
  166. const uint32_t numInstances = 121;
  167. if (numInstances == bgfx::getAvailInstanceDataBuffer(numInstances, instanceStride) )
  168. {
  169. bgfx::InstanceDataBuffer idb;
  170. bgfx::allocInstanceDataBuffer(&idb, numInstances, instanceStride);
  171. uint8_t* data = idb.data;
  172. // Write instance data for 11x11 cubes.
  173. for (uint32_t yy = 0; yy < 11; ++yy)
  174. {
  175. for (uint32_t xx = 0; xx < 11; ++xx)
  176. {
  177. float* mtx = (float*)data;
  178. bx::mtxRotateXY(mtx, time + xx*0.21f, time + yy*0.37f);
  179. mtx[12] = -15.0f + float(xx)*3.0f;
  180. mtx[13] = -15.0f + float(yy)*3.0f;
  181. mtx[14] = 0.0f;
  182. float* color = (float*)&data[64];
  183. color[0] = bx::fsin(time+float(xx)/11.0f)*0.5f+0.5f;
  184. color[1] = bx::fcos(time+float(yy)/11.0f)*0.5f+0.5f;
  185. color[2] = bx::fsin(time*3.0f)*0.5f+0.5f;
  186. color[3] = 1.0f;
  187. data += instanceStride;
  188. }
  189. }
  190. // Set vertex and index buffer.
  191. bgfx::setVertexBuffer(0, m_vbh);
  192. bgfx::setIndexBuffer(m_ibh);
  193. // Set instance data buffer.
  194. bgfx::setInstanceDataBuffer(&idb);
  195. // Set render states.
  196. bgfx::setState(BGFX_STATE_DEFAULT);
  197. // Submit primitive for rendering to view 0.
  198. bgfx::submit(0, m_program);
  199. }
  200. }
  201. // Advance to next frame. Rendering thread will be kicked to
  202. // process submitted rendering primitives.
  203. bgfx::frame();
  204. return true;
  205. }
  206. return false;
  207. }
  208. entry::MouseState m_mouseState;
  209. uint32_t m_width;
  210. uint32_t m_height;
  211. uint32_t m_debug;
  212. uint32_t m_reset;
  213. bgfx::VertexBufferHandle m_vbh;
  214. bgfx::IndexBufferHandle m_ibh;
  215. bgfx::ProgramHandle m_program;
  216. int64_t m_timeOffset;
  217. };
  218. } // namespace
  219. ENTRY_IMPLEMENT_MAIN(ExampleInstancing, "05-instancing", "Geometry instancing.");