drawindirect.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /*
  2. * Copyright 2022-2022 Liam Twigger. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  4. */
  5. // Draw Indirect
  6. // Render multiple different objects with one draw command. This example shows a minimal implementation of indirect drawing
  7. // Reading References:
  8. // https://litasa.github.io/blog/2017/09/04/OpenGL-MultiDrawIndirect-with-Individual-Textures
  9. // https://cpp-rendering.io/indirect-rendering/
  10. #include "common.h"
  11. #include "bgfx_utils.h"
  12. #include "imgui/imgui.h"
  13. namespace
  14. {
  15. struct PosColorVertex
  16. {
  17. float m_x;
  18. float m_y;
  19. float m_z;
  20. uint32_t m_abgr;
  21. static void init()
  22. {
  23. ms_layout
  24. .begin()
  25. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  26. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  27. .end();
  28. };
  29. static bgfx::VertexLayout ms_layout;
  30. };
  31. bgfx::VertexLayout PosColorVertex::ms_layout;
  32. struct ObjectInstance
  33. {
  34. float m_vertexOffset;
  35. float m_vertexCount;
  36. float m_indexOffset;
  37. float m_indexCount;
  38. static void init()
  39. {
  40. ms_layout
  41. .begin()
  42. .add(bgfx::Attrib::TexCoord0, 4, bgfx::AttribType::Float)
  43. .end();
  44. };
  45. static bgfx::VertexLayout ms_layout;
  46. };
  47. bgfx::VertexLayout ObjectInstance::ms_layout;
  48. struct RenderInstance
  49. {
  50. float m_mtx[16];
  51. float m_color[4];
  52. static void init()
  53. {
  54. ms_layout
  55. .begin()
  56. .add(bgfx::Attrib::TexCoord0, 4, bgfx::AttribType::Float)
  57. .add(bgfx::Attrib::TexCoord1, 4, bgfx::AttribType::Float)
  58. .add(bgfx::Attrib::TexCoord2, 4, bgfx::AttribType::Float)
  59. .add(bgfx::Attrib::TexCoord3, 4, bgfx::AttribType::Float)
  60. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Float)
  61. .end();
  62. };
  63. static bgfx::VertexLayout ms_layout;
  64. };
  65. bgfx::VertexLayout RenderInstance::ms_layout;
  66. static PosColorVertex s_multiMeshVertices[12] =
  67. {
  68. // Cube Model
  69. {-1.0f, 1.0f, 1.0f, 0xff000000 },
  70. { 1.0f, 1.0f, 1.0f, 0xff0000ff },
  71. {-1.0f, -1.0f, 1.0f, 0xff00ff00 },
  72. { 1.0f, -1.0f, 1.0f, 0xff00ffff },
  73. {-1.0f, 1.0f, -1.0f, 0xffff0000 },
  74. { 1.0f, 1.0f, -1.0f, 0xffff00ff },
  75. {-1.0f, -1.0f, -1.0f, 0xffffff00 },
  76. { 1.0f, -1.0f, -1.0f, 0xffffffff },
  77. // Tetrahedron Model (offset = 8)
  78. { 1.0f, 1.0f, 1.0f, 0xff0000ff },
  79. { 1.0f, -1.0f, -1.0f, 0xff000000 },
  80. {-1.0f, 1.0f, -1.0f, 0xff00ff00 },
  81. {-1.0f, -1.0f, 1.0f, 0xff00ffff },
  82. };
  83. static const uint16_t s_multiMeshIndices[48] =
  84. {
  85. // Cube Indicies
  86. 0, 1, 2, // 0
  87. 1, 3, 2,
  88. 4, 6, 5, // 2
  89. 5, 6, 7,
  90. 0, 2, 4, // 4
  91. 4, 2, 6,
  92. 1, 5, 3, // 6
  93. 5, 7, 3,
  94. 0, 4, 1, // 8
  95. 4, 5, 1,
  96. 2, 3, 6, // 10
  97. 6, 3, 7,
  98. // Tetrahedron Indices (offset = 36)
  99. 0, 2, 1,
  100. 1, 2, 3,
  101. 0, 3, 2,
  102. 1, 3, 0,
  103. };
  104. static constexpr uint32_t kMaxSideSize = 81;
  105. class DrawIndirect : public entry::AppI
  106. {
  107. public:
  108. DrawIndirect(const char* _name, const char* _description, const char* _url)
  109. : entry::AppI(_name, _description, _url)
  110. {
  111. }
  112. void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override
  113. {
  114. Args args(_argc, _argv);
  115. m_width = _width;
  116. m_height = _height;
  117. m_debug = BGFX_DEBUG_TEXT;
  118. m_reset = BGFX_RESET_VSYNC;
  119. m_sideSize = 11;
  120. m_maxDrawElements = kMaxSideSize*kMaxSideSize;
  121. m_useIndirectCount = false;
  122. bgfx::Init init;
  123. init.type = args.m_type;
  124. init.vendorId = args.m_pciId;
  125. init.platformData.nwh = entry::getNativeWindowHandle(entry::kDefaultWindowHandle);
  126. init.platformData.ndt = entry::getNativeDisplayHandle();
  127. init.platformData.type = entry::getNativeWindowHandleType();
  128. init.resolution.width = m_width;
  129. init.resolution.height = m_height;
  130. init.resolution.reset = m_reset;
  131. bgfx::init(init);
  132. // Enable debug text.
  133. bgfx::setDebug(m_debug);
  134. // Set view 0 clear state.
  135. bgfx::setViewClear(0
  136. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  137. , 0x303030ff
  138. , 1.0f
  139. , 0
  140. );
  141. // Create vertex stream declaration.
  142. PosColorVertex::init();
  143. ObjectInstance::init();
  144. RenderInstance::init();
  145. // Create static vertex buffer.
  146. m_vbh = bgfx::createVertexBuffer(
  147. bgfx::makeRef(s_multiMeshVertices, sizeof(s_multiMeshVertices) )
  148. , PosColorVertex::ms_layout
  149. );
  150. // Create static index buffer.
  151. m_ibh = bgfx::createIndexBuffer(
  152. bgfx::makeRef(s_multiMeshIndices, sizeof(s_multiMeshIndices) )
  153. );
  154. // Create program from shaders.
  155. m_program = loadProgram("vs_instancing", "fs_instancing"); // These are reused from 05-instancing
  156. m_indirectProgram = BGFX_INVALID_HANDLE;
  157. m_indirectCountProgram = BGFX_INVALID_HANDLE;
  158. m_indirectBuffer = BGFX_INVALID_HANDLE;
  159. m_indirectCountBuffer = BGFX_INVALID_HANDLE;
  160. m_objectListBuffer = BGFX_INVALID_HANDLE;
  161. u_drawParams = bgfx::createUniform("u_drawParams", bgfx::UniformType::Vec4);
  162. const uint64_t supportedCaps = bgfx::getCaps()->supported;
  163. const bool computeSupported = !!(BGFX_CAPS_COMPUTE & supportedCaps);
  164. const bool indirectSupported = !!(BGFX_CAPS_DRAW_INDIRECT & supportedCaps);
  165. const bool instancingSupported = !!(BGFX_CAPS_INSTANCING & supportedCaps);
  166. const bool indirectCountSupported = !!(BGFX_CAPS_DRAW_INDIRECT_COUNT & supportedCaps);
  167. if (computeSupported
  168. && indirectSupported
  169. && instancingSupported)
  170. {
  171. // Set up indirect program
  172. // This is a barebones program that populates the indirect buffer handle with draw requests
  173. m_indirectProgram = bgfx::createProgram(loadShader("cs_drawindirect"), true);
  174. m_indirectBuffer = bgfx::createIndirectBuffer(m_maxDrawElements);
  175. if (indirectCountSupported)
  176. {
  177. m_useIndirectCount = true;
  178. m_indirectCountProgram = bgfx::createProgram(loadShader("cs_drawindirect_count"), true);
  179. const bgfx::Memory* mem = bgfx::alloc(sizeof(uint32_t) );
  180. *(uint32_t*)mem->data = 0;
  181. m_indirectCountBuffer = bgfx::createIndexBuffer(mem, BGFX_BUFFER_INDEX32 | BGFX_BUFFER_COMPUTE_WRITE | BGFX_BUFFER_DRAW_INDIRECT);
  182. }
  183. const bgfx::Memory* mem = bgfx::alloc(sizeof(ObjectInstance) * m_maxDrawElements);
  184. ObjectInstance* objs = (ObjectInstance*)mem->data;
  185. for (uint32_t ii = 0; ii < m_maxDrawElements; ++ii)
  186. {
  187. if (ii % 2)
  188. {
  189. // Tetrahedron
  190. objs[ii].m_vertexOffset = 8;
  191. objs[ii].m_vertexCount = 4; // m_vertexCount is unused in compute shader, its only here for completeness
  192. objs[ii].m_indexOffset = 36;
  193. objs[ii].m_indexCount = 12;
  194. }
  195. else
  196. {
  197. // Cube
  198. objs[ii].m_vertexOffset = 0;
  199. objs[ii].m_vertexCount = 8;
  200. objs[ii].m_indexOffset = 0;
  201. objs[ii].m_indexCount = 36;
  202. }
  203. }
  204. // This is a list of objects to be rendered via the indirect program
  205. m_objectListBuffer = bgfx::createVertexBuffer(mem, ObjectInstance::ms_layout, BGFX_BUFFER_COMPUTE_READ);
  206. bgfx::setName(m_objectListBuffer, "Object buffer");
  207. // This is the instance buffer used for rendering.
  208. // You could instead use a dynamic instance buffer when rendering (use bgfx::allocInstanceDataBuffer in draw loop)
  209. m_instanceBuffer = bgfx::createDynamicVertexBuffer(m_maxDrawElements, RenderInstance::ms_layout, BGFX_BUFFER_COMPUTE_WRITE);
  210. bgfx::setName(m_objectListBuffer, "Instance buffer");
  211. }
  212. imguiCreate();
  213. m_frameTime.reset();
  214. }
  215. int shutdown() override
  216. {
  217. imguiDestroy();
  218. // Cleanup.
  219. bgfx::destroy(m_ibh);
  220. bgfx::destroy(m_vbh);
  221. bgfx::destroy(m_program);
  222. if (bgfx::isValid(m_indirectProgram) )
  223. {
  224. bgfx::destroy(m_indirectProgram);
  225. }
  226. if (bgfx::isValid(m_indirectCountProgram) )
  227. {
  228. bgfx::destroy(m_indirectCountProgram);
  229. }
  230. if (bgfx::isValid(m_indirectBuffer) )
  231. {
  232. bgfx::destroy(m_indirectBuffer);
  233. }
  234. if (bgfx::isValid(m_indirectCountBuffer) )
  235. {
  236. bgfx::destroy(m_indirectCountBuffer);
  237. }
  238. if (bgfx::isValid(m_objectListBuffer) )
  239. {
  240. bgfx::destroy(m_objectListBuffer);
  241. }
  242. if (bgfx::isValid(m_instanceBuffer) )
  243. {
  244. bgfx::destroy(m_instanceBuffer);
  245. }
  246. bgfx::destroy(u_drawParams);
  247. // Shutdown bgfx.
  248. bgfx::shutdown();
  249. return 0;
  250. }
  251. bool update() override
  252. {
  253. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  254. {
  255. m_frameTime.frame();
  256. const float time = bx::toSeconds<float>(m_frameTime.getDurationTime() );
  257. // Get renderer capabilities info.
  258. const uint64_t supportedCaps = bgfx::getCaps()->supported;
  259. const bool computeSupported = !!(BGFX_CAPS_COMPUTE & supportedCaps);
  260. const bool indirectSupported = !!(BGFX_CAPS_DRAW_INDIRECT & supportedCaps);
  261. const bool instancingSupported = !!(BGFX_CAPS_INSTANCING & supportedCaps);
  262. const bool indirectCountSupported = !!(BGFX_CAPS_DRAW_INDIRECT_COUNT & supportedCaps);
  263. imguiBeginFrame(m_mouseState.m_mx
  264. , m_mouseState.m_my
  265. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  266. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  267. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  268. , m_mouseState.m_mz
  269. , uint16_t(m_width)
  270. , uint16_t(m_height)
  271. );
  272. showExampleDialog(this);
  273. ImGui::SetNextWindowPos(
  274. ImVec2(m_width - m_width / 5.0f - 10.0f, 10.0f)
  275. , ImGuiCond_FirstUseEver
  276. );
  277. ImGui::SetNextWindowSize(
  278. ImVec2(m_width / 5.0f, m_height / 2.0f)
  279. , ImGuiCond_FirstUseEver
  280. );
  281. ImGui::Begin("Settings"
  282. , NULL
  283. , 0
  284. );
  285. ImGui::Text("%d draw calls", bgfx::getStats()->numDraw);
  286. ImGui::Text("%d objects drawn", m_sideSize*m_sideSize);
  287. ImGui::Text("Grid Side Size:");
  288. ImGui::SliderInt("##size", (int32_t*)&m_sideSize, 1, kMaxSideSize);
  289. ImGui::BeginDisabled(!indirectCountSupported);
  290. ImGui::Checkbox("Indirect Count", &m_useIndirectCount);
  291. ImGui::EndDisabled();
  292. if (!indirectCountSupported
  293. && ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled) )
  294. {
  295. ImGui::SetTooltip("Indirect Count is not supported by GPU.");
  296. }
  297. ImGui::End();
  298. imguiEndFrame();
  299. // Set view 0 default viewport.
  300. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  301. // This dummy draw call is here to make sure that view 0 is cleared
  302. // if no other draw calls are submitted to view 0.
  303. bgfx::touch(0);
  304. const bx::Vec3 at = { 0.0f, 0.0f, 0.0f };
  305. const bx::Vec3 eye = { 0.0f, 0.0f, -35.0f };
  306. // Set view and projection matrix for view 0.
  307. {
  308. float view[16];
  309. bx::mtxLookAt(view, eye, at);
  310. float proj[16];
  311. bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 500.0f, bgfx::getCaps()->homogeneousDepth);
  312. bgfx::setViewTransform(0, view, proj);
  313. // Set view 0 default viewport.
  314. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  315. }
  316. if (computeSupported && indirectSupported && instancingSupported)
  317. {
  318. // Build indirect buffer & prepare instance buffer
  319. // NOTE: IF you are rendering static data then
  320. // this could be done once on startup and results stored
  321. // This is done here for demonstration purposes
  322. // The model matrix for each instance is also set on compute
  323. // you could modify this to, eg, do frustum culling on the GPU
  324. const float ud[4] = { float(m_maxDrawElements), float(m_sideSize), float(time), 0 };
  325. const uint32_t numToDraw = (m_sideSize*m_sideSize);
  326. bgfx::setUniform(u_drawParams, ud);
  327. bgfx::setBuffer(0, m_objectListBuffer, bgfx::Access::Read);
  328. bgfx::setBuffer(1, m_indirectBuffer, bgfx::Access::Write);
  329. bgfx::setBuffer(2, m_instanceBuffer, bgfx::Access::Write);
  330. // Dispatch the call. We are using 64 local threads on the GPU to process the object list
  331. // So lets dispatch ceil(numToDraw/64) workgroups of 64 local threads
  332. if (m_useIndirectCount)
  333. {
  334. bgfx::setBuffer(3, m_indirectCountBuffer, bgfx::Access::Write);
  335. bgfx::dispatch(0, m_indirectCountProgram, uint32_t(numToDraw/64 + 1), 1, 1);
  336. }
  337. else
  338. {
  339. bgfx::dispatch(0, m_indirectProgram, uint32_t(numToDraw/64 + 1), 1, 1);
  340. }
  341. // Submit our 1 draw call
  342. // Set vertex and index buffer.
  343. bgfx::setIndexBuffer(m_ibh);
  344. bgfx::setVertexBuffer(0, m_vbh);
  345. bgfx::setInstanceDataBuffer(m_instanceBuffer, 0, numToDraw);
  346. // Set render states.
  347. bgfx::setState(BGFX_STATE_DEFAULT);
  348. // Submit primitive for rendering to view 0.
  349. if (m_useIndirectCount)
  350. {
  351. // With indirect count, the number of draws is read from a buffer
  352. bgfx::submit(0, m_program, m_indirectBuffer, 0, m_indirectCountBuffer);
  353. }
  354. else
  355. {
  356. bgfx::submit(0, m_program, m_indirectBuffer, 0, numToDraw);
  357. }
  358. }
  359. else
  360. {
  361. // Compute/Indirect/Instancing is not supported
  362. bool blink = uint32_t(time*3.0f)&1;
  363. bgfx::dbgTextPrintf(0, 0, blink ? 0x4f : 0x04, " Compute/Indirect/Instancing is not supported by GPU. ");
  364. }
  365. // Advance to next frame. Rendering thread will be kicked to
  366. // process submitted rendering primitives.
  367. bgfx::frame();
  368. return true;
  369. }
  370. return false;
  371. }
  372. entry::MouseState m_mouseState;
  373. uint32_t m_width;
  374. uint32_t m_height;
  375. uint32_t m_debug;
  376. uint32_t m_reset;
  377. uint32_t m_sideSize;
  378. uint32_t m_maxDrawElements;
  379. bool m_useIndirectCount;
  380. bgfx::VertexBufferHandle m_vbh;
  381. bgfx::IndexBufferHandle m_ibh;
  382. bgfx::ProgramHandle m_program;
  383. bgfx::ProgramHandle m_indirectProgram;
  384. bgfx::ProgramHandle m_indirectCountProgram;
  385. bgfx::IndirectBufferHandle m_indirectBuffer;
  386. bgfx::IndexBufferHandle m_indirectCountBuffer;
  387. bgfx::VertexBufferHandle m_objectListBuffer;
  388. bgfx::DynamicVertexBufferHandle m_instanceBuffer;
  389. bgfx::UniformHandle u_drawParams;
  390. FrameTime m_frameTime;
  391. };
  392. } // namespace
  393. ENTRY_IMPLEMENT_MAIN(
  394. DrawIndirect
  395. , "48-drawindirect"
  396. , "Simple example of indirect rendering to render multiple different meshes with 1 draw call"
  397. , "https://bkaradzic.github.io/bgfx/examples.html#drawindirect"
  398. );