instancing.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * Copyright 2011-2021 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, const char* _url)
  57. : entry::AppI(_name, _description, _url)
  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. m_use_instancing = true;
  68. m_last_frame_missing = 0;
  69. m_side_size = 11;
  70. bgfx::Init init;
  71. init.type = args.m_type;
  72. init.vendorId = args.m_pciId;
  73. init.resolution.width = m_width;
  74. init.resolution.height = m_height;
  75. init.resolution.reset = m_reset;
  76. bgfx::init(init);
  77. // Enable debug text.
  78. bgfx::setDebug(m_debug);
  79. // Set view 0 clear state.
  80. bgfx::setViewClear(0
  81. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  82. , 0x303030ff
  83. , 1.0f
  84. , 0
  85. );
  86. // Create vertex stream declaration.
  87. PosColorVertex::init();
  88. // Create static vertex buffer.
  89. m_vbh = bgfx::createVertexBuffer(
  90. bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) )
  91. , PosColorVertex::ms_layout
  92. );
  93. // Create static index buffer.
  94. m_ibh = bgfx::createIndexBuffer(
  95. bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) )
  96. );
  97. // Create program from shaders.
  98. m_program = loadProgram("vs_instancing", "fs_instancing");
  99. m_program_non_instanced = loadProgram("vs_cubes", "fs_cubes");
  100. m_timeOffset = bx::getHPCounter();
  101. imguiCreate();
  102. }
  103. int shutdown() override
  104. {
  105. imguiDestroy();
  106. // Cleanup.
  107. bgfx::destroy(m_ibh);
  108. bgfx::destroy(m_vbh);
  109. bgfx::destroy(m_program);
  110. bgfx::destroy(m_program_non_instanced);
  111. // Shutdown bgfx.
  112. bgfx::shutdown();
  113. return 0;
  114. }
  115. bool update() override
  116. {
  117. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  118. {
  119. imguiBeginFrame(m_mouseState.m_mx
  120. , m_mouseState.m_my
  121. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  122. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  123. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  124. , m_mouseState.m_mz
  125. , uint16_t(m_width)
  126. , uint16_t(m_height)
  127. );
  128. showExampleDialog(this);
  129. ImGui::SetNextWindowPos(
  130. ImVec2(m_width - m_width / 5.0f - 10.0f, 10.0f)
  131. , ImGuiCond_FirstUseEver
  132. );
  133. ImGui::SetNextWindowSize(
  134. ImVec2(m_width / 5.0f, m_height / 2.0f)
  135. , ImGuiCond_FirstUseEver
  136. );
  137. ImGui::Begin("Settings"
  138. , NULL
  139. , 0
  140. );
  141. ImGui::Text("%d draw calls", bgfx::getStats()->numDraw);
  142. ImGui::Checkbox("Use Instancing", &m_use_instancing);
  143. ImGui::Text("Grid Side Size:");
  144. ImGui::SliderInt("", (int*)&m_side_size, 1, 512);
  145. if (m_last_frame_missing > 0)
  146. {
  147. ImGui::TextColored(ImVec4(1.0f, 0.0f, 0.0f, 1.0f), "Couldn't draw %d cubes last frame", m_last_frame_missing);
  148. }
  149. if (bgfx::getStats()->numDraw >= bgfx::getCaps()->limits.maxDrawCalls)
  150. {
  151. ImGui::TextColored(ImVec4(1.0f, 0.0f, 0.0f, 1.0f), "Draw call limit reached!");
  152. }
  153. ImGui::End();
  154. imguiEndFrame();
  155. // Set view 0 default viewport.
  156. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  157. // This dummy draw call is here to make sure that view 0 is cleared
  158. // if no other draw calls are submitted to view 0.
  159. bgfx::touch(0);
  160. float time = (float)( (bx::getHPCounter() - m_timeOffset)/double(bx::getHPFrequency() ) );
  161. // Get renderer capabilities info.
  162. const bgfx::Caps* caps = bgfx::getCaps();
  163. // Check if instancing is supported.
  164. if (0 == (BGFX_CAPS_INSTANCING & caps->supported) )
  165. {
  166. // When instancing is not supported by GPU, implement alternative
  167. // code path that doesn't use instancing.
  168. bool blink = uint32_t(time*3.0f)&1;
  169. bgfx::dbgTextPrintf(0, 0, blink ? 0x4f : 0x04, " Instancing is not supported by GPU. ");
  170. m_use_instancing = false;
  171. }
  172. const bx::Vec3 at = { 0.0f, 0.0f, 0.0f };
  173. const bx::Vec3 eye = { 0.0f, 0.0f, -35.0f };
  174. // Set view and projection matrix for view 0.
  175. {
  176. float view[16];
  177. bx::mtxLookAt(view, eye, at);
  178. float proj[16];
  179. bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth);
  180. bgfx::setViewTransform(0, view, proj);
  181. // Set view 0 default viewport.
  182. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  183. }
  184. m_last_frame_missing = 0;
  185. if (m_use_instancing)
  186. {
  187. // 80 bytes stride = 64 bytes for 4x4 matrix + 16 bytes for RGBA color.
  188. const uint16_t instanceStride = 80;
  189. // to total number of instances to draw
  190. uint32_t totalCubes = m_side_size * m_side_size;
  191. // figure out how big of a buffer is available
  192. uint32_t drawnCubes = bgfx::getAvailInstanceDataBuffer(totalCubes, instanceStride);
  193. // save how many we couldn't draw due to buffer room so we can display it
  194. m_last_frame_missing = totalCubes - drawnCubes;
  195. bgfx::InstanceDataBuffer idb;
  196. bgfx::allocInstanceDataBuffer(&idb, drawnCubes, instanceStride);
  197. uint8_t* data = idb.data;
  198. for (uint32_t ii = 0; ii < drawnCubes; ++ii)
  199. {
  200. uint32_t yy = ii / m_side_size;
  201. uint32_t xx = ii % m_side_size;
  202. float* mtx = (float*)data;
  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. float* color = (float*)&data[64];
  208. color[0] = bx::sin(time + float(xx) / 11.0f) * 0.5f + 0.5f;
  209. color[1] = bx::cos(time + float(yy) / 11.0f) * 0.5f + 0.5f;
  210. color[2] = bx::sin(time * 3.0f) * 0.5f + 0.5f;
  211. color[3] = 1.0f;
  212. data += instanceStride;
  213. }
  214. // Set vertex and index buffer.
  215. bgfx::setVertexBuffer(0, m_vbh);
  216. bgfx::setIndexBuffer(m_ibh);
  217. // Set instance data buffer.
  218. bgfx::setInstanceDataBuffer(&idb);
  219. // Set render states.
  220. bgfx::setState(BGFX_STATE_DEFAULT);
  221. // Submit primitive for rendering to view 0.
  222. bgfx::submit(0, m_program);
  223. }
  224. else
  225. {
  226. // non-instanced path
  227. for (uint32_t yy = 0; yy < m_side_size; ++yy)
  228. {
  229. for (uint32_t xx = 0; xx < m_side_size; ++xx)
  230. {
  231. float mtx[16];
  232. bx::mtxRotateXY(mtx, time + xx * 0.21f, time + yy * 0.37f);
  233. mtx[12] = -15.0f + float(xx) * 3.0f;
  234. mtx[13] = -15.0f + float(yy) * 3.0f;
  235. mtx[14] = 0.0f;
  236. // Set model matrix for rendering.
  237. bgfx::setTransform(mtx);
  238. // Set vertex and index buffer.
  239. bgfx::setVertexBuffer(0, m_vbh);
  240. bgfx::setIndexBuffer(m_ibh);
  241. // Set render states.
  242. bgfx::setState(BGFX_STATE_DEFAULT);
  243. // Submit primitive for rendering to view 0.
  244. bgfx::submit(0, m_program_non_instanced);
  245. }
  246. }
  247. }
  248. }
  249. // Advance to next frame. Rendering thread will be kicked to
  250. // process submitted rendering primitives.
  251. bgfx::frame();
  252. return true;
  253. }
  254. entry::MouseState m_mouseState;
  255. uint32_t m_width;
  256. uint32_t m_height;
  257. uint32_t m_debug;
  258. uint32_t m_reset;
  259. bool m_use_instancing;
  260. uint32_t m_last_frame_missing;
  261. uint32_t m_side_size;
  262. bgfx::VertexBufferHandle m_vbh;
  263. bgfx::IndexBufferHandle m_ibh;
  264. bgfx::ProgramHandle m_program;
  265. bgfx::ProgramHandle m_program_non_instanced;
  266. int64_t m_timeOffset;
  267. };
  268. } // namespace
  269. ENTRY_IMPLEMENT_MAIN(
  270. ExampleInstancing
  271. , "05-instancing"
  272. , "Geometry instancing."
  273. , "https://bkaradzic.github.io/bgfx/examples.html#instancing"
  274. );