instancing.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * Copyright 2011-2019 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_layout
  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::VertexLayout ms_layout;
  25. };
  26. bgfx::VertexLayout PosColorVertex::ms_layout;
  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 init;
  68. init.type = args.m_type;
  69. init.vendorId = args.m_pciId;
  70. init.resolution.width = m_width;
  71. init.resolution.height = m_height;
  72. init.resolution.reset = m_reset;
  73. bgfx::init(init);
  74. // Enable debug text.
  75. bgfx::setDebug(m_debug);
  76. // Set view 0 clear state.
  77. bgfx::setViewClear(0
  78. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  79. , 0x303030ff
  80. , 1.0f
  81. , 0
  82. );
  83. // Create vertex stream declaration.
  84. PosColorVertex::init();
  85. // Create static vertex buffer.
  86. m_vbh = bgfx::createVertexBuffer(
  87. bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) )
  88. , PosColorVertex::ms_layout
  89. );
  90. // Create static index buffer.
  91. m_ibh = bgfx::createIndexBuffer(
  92. bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) )
  93. );
  94. // Create program from shaders.
  95. m_program = loadProgram("vs_instancing", "fs_instancing");
  96. m_timeOffset = bx::getHPCounter();
  97. imguiCreate();
  98. }
  99. int shutdown() override
  100. {
  101. imguiDestroy();
  102. // Cleanup.
  103. bgfx::destroy(m_ibh);
  104. bgfx::destroy(m_vbh);
  105. bgfx::destroy(m_program);
  106. // Shutdown bgfx.
  107. bgfx::shutdown();
  108. return 0;
  109. }
  110. bool update() override
  111. {
  112. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  113. {
  114. imguiBeginFrame(m_mouseState.m_mx
  115. , m_mouseState.m_my
  116. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  117. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  118. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  119. , m_mouseState.m_mz
  120. , uint16_t(m_width)
  121. , uint16_t(m_height)
  122. );
  123. showExampleDialog(this);
  124. imguiEndFrame();
  125. // Set view 0 default viewport.
  126. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  127. // This dummy draw call is here to make sure that view 0 is cleared
  128. // if no other draw calls are submitted to view 0.
  129. bgfx::touch(0);
  130. float time = (float)( (bx::getHPCounter() - m_timeOffset)/double(bx::getHPFrequency() ) );
  131. // Get renderer capabilities info.
  132. const bgfx::Caps* caps = bgfx::getCaps();
  133. // Check if instancing is supported.
  134. if (0 == (BGFX_CAPS_INSTANCING & caps->supported) )
  135. {
  136. // When instancing is not supported by GPU, implement alternative
  137. // code path that doesn't use instancing.
  138. bool blink = uint32_t(time*3.0f)&1;
  139. bgfx::dbgTextPrintf(0, 0, blink ? 0x4f : 0x04, " Instancing is not supported by GPU. ");
  140. }
  141. else
  142. {
  143. const bx::Vec3 at = { 0.0f, 0.0f, 0.0f };
  144. const bx::Vec3 eye = { 0.0f, 0.0f, -35.0f };
  145. // Set view and projection matrix for view 0.
  146. {
  147. float view[16];
  148. bx::mtxLookAt(view, eye, at);
  149. float proj[16];
  150. bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth);
  151. bgfx::setViewTransform(0, view, proj);
  152. // Set view 0 default viewport.
  153. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  154. }
  155. // 80 bytes stride = 64 bytes for 4x4 matrix + 16 bytes for RGBA color.
  156. const uint16_t instanceStride = 80;
  157. // 11x11 cubes
  158. const uint32_t numInstances = 121;
  159. if (numInstances == bgfx::getAvailInstanceDataBuffer(numInstances, instanceStride) )
  160. {
  161. bgfx::InstanceDataBuffer idb;
  162. bgfx::allocInstanceDataBuffer(&idb, numInstances, instanceStride);
  163. uint8_t* data = idb.data;
  164. // Write instance data for 11x11 cubes.
  165. for (uint32_t yy = 0; yy < 11; ++yy)
  166. {
  167. for (uint32_t xx = 0; xx < 11; ++xx)
  168. {
  169. float* mtx = (float*)data;
  170. bx::mtxRotateXY(mtx, time + xx*0.21f, time + yy*0.37f);
  171. mtx[12] = -15.0f + float(xx)*3.0f;
  172. mtx[13] = -15.0f + float(yy)*3.0f;
  173. mtx[14] = 0.0f;
  174. float* color = (float*)&data[64];
  175. color[0] = bx::sin(time+float(xx)/11.0f)*0.5f+0.5f;
  176. color[1] = bx::cos(time+float(yy)/11.0f)*0.5f+0.5f;
  177. color[2] = bx::sin(time*3.0f)*0.5f+0.5f;
  178. color[3] = 1.0f;
  179. data += instanceStride;
  180. }
  181. }
  182. // Set vertex and index buffer.
  183. bgfx::setVertexBuffer(0, m_vbh);
  184. bgfx::setIndexBuffer(m_ibh);
  185. // Set instance data buffer.
  186. bgfx::setInstanceDataBuffer(&idb);
  187. // Set render states.
  188. bgfx::setState(BGFX_STATE_DEFAULT);
  189. // Submit primitive for rendering to view 0.
  190. bgfx::submit(0, m_program);
  191. }
  192. }
  193. // Advance to next frame. Rendering thread will be kicked to
  194. // process submitted rendering primitives.
  195. bgfx::frame();
  196. return true;
  197. }
  198. return false;
  199. }
  200. entry::MouseState m_mouseState;
  201. uint32_t m_width;
  202. uint32_t m_height;
  203. uint32_t m_debug;
  204. uint32_t m_reset;
  205. bgfx::VertexBufferHandle m_vbh;
  206. bgfx::IndexBufferHandle m_ibh;
  207. bgfx::ProgramHandle m_program;
  208. int64_t m_timeOffset;
  209. };
  210. } // namespace
  211. ENTRY_IMPLEMENT_MAIN(ExampleInstancing, "05-instancing", "Geometry instancing.");