drawstress.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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 <bx/uint32_t.h>
  8. #include <bx/thread.h>
  9. #include <bx/os.h>
  10. #include "imgui/imgui.h"
  11. #include <bgfx/embedded_shader.h>
  12. // embedded shaders
  13. #include "vs_drawstress.bin.h"
  14. #include "fs_drawstress.bin.h"
  15. namespace
  16. {
  17. static const bgfx::EmbeddedShader s_embeddedShaders[] =
  18. {
  19. BGFX_EMBEDDED_SHADER(vs_drawstress),
  20. BGFX_EMBEDDED_SHADER(fs_drawstress),
  21. BGFX_EMBEDDED_SHADER_END()
  22. };
  23. struct PosColorVertex
  24. {
  25. float m_x;
  26. float m_y;
  27. float m_z;
  28. uint32_t m_abgr;
  29. static void init()
  30. {
  31. ms_decl
  32. .begin()
  33. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  34. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  35. .end();
  36. }
  37. static bgfx::VertexDecl ms_decl;
  38. };
  39. bgfx::VertexDecl PosColorVertex::ms_decl;
  40. static PosColorVertex s_cubeVertices[8] =
  41. {
  42. {-1.0f, 1.0f, 1.0f, 0xff000000 },
  43. { 1.0f, 1.0f, 1.0f, 0xff0000ff },
  44. {-1.0f, -1.0f, 1.0f, 0xff00ff00 },
  45. { 1.0f, -1.0f, 1.0f, 0xff00ffff },
  46. {-1.0f, 1.0f, -1.0f, 0xffff0000 },
  47. { 1.0f, 1.0f, -1.0f, 0xffff00ff },
  48. {-1.0f, -1.0f, -1.0f, 0xffffff00 },
  49. { 1.0f, -1.0f, -1.0f, 0xffffffff },
  50. };
  51. static const uint16_t s_cubeIndices[36] =
  52. {
  53. 0, 1, 2, // 0
  54. 1, 3, 2,
  55. 4, 6, 5, // 2
  56. 5, 6, 7,
  57. 0, 2, 4, // 4
  58. 4, 2, 6,
  59. 1, 5, 3, // 6
  60. 5, 7, 3,
  61. 0, 4, 1, // 8
  62. 4, 5, 1,
  63. 2, 3, 6, // 10
  64. 6, 3, 7,
  65. };
  66. static const float s_mod[6][3] =
  67. {
  68. { 1.0f, 1.0f, 1.0f },
  69. { 1.0f, 0.0f, 0.0f },
  70. { 0.0f, 1.0f, 0.0f },
  71. { 0.0f, 0.0f, 1.0f },
  72. { 1.0f, 1.0f, 0.0f },
  73. { 0.0f, 1.0f, 1.0f },
  74. };
  75. #if BX_PLATFORM_EMSCRIPTEN
  76. static const int64_t highwm = 1000000/35;
  77. static const int64_t lowwm = 1000000/27;
  78. #else
  79. static const int64_t highwm = 1000000/65;
  80. static const int64_t lowwm = 1000000/57;
  81. #endif // BX_PLATFORM_EMSCRIPTEN
  82. int32_t threadFunc(bx::Thread* _thread, void* _userData);
  83. class ExampleDrawStress : public entry::AppI
  84. {
  85. public:
  86. ExampleDrawStress(const char* _name, const char* _description)
  87. : entry::AppI(_name, _description)
  88. {
  89. }
  90. void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override
  91. {
  92. Args args(_argc, _argv);
  93. m_width = _width;
  94. m_height = _height;
  95. m_debug = BGFX_DEBUG_NONE;
  96. m_reset = BGFX_RESET_NONE;
  97. m_autoAdjust = true;
  98. m_scrollArea = 0;
  99. m_dim = 16;
  100. m_maxDim = 40;
  101. m_transform = 0;
  102. m_timeOffset = bx::getHPCounter();
  103. m_deltaTimeNs = 0;
  104. m_deltaTimeAvgNs = 0;
  105. m_numFrames = 0;
  106. bgfx::init(args.m_type, args.m_pciId);
  107. bgfx::reset(m_width, m_height, m_reset);
  108. const bgfx::Caps* caps = bgfx::getCaps();
  109. m_maxDim = (int32_t)bx::fpow(float(caps->limits.maxDrawCalls), 1.0f/3.0f);
  110. // Enable debug text.
  111. bgfx::setDebug(m_debug);
  112. // Set view 0 clear state.
  113. bgfx::setViewClear(0
  114. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  115. , 0x303030ff
  116. , 1.0f
  117. , 0
  118. );
  119. // Create vertex stream declaration.
  120. PosColorVertex::init();
  121. bgfx::RendererType::Enum type = bgfx::getRendererType();
  122. // Create program from shaders.
  123. m_program = bgfx::createProgram(
  124. bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_drawstress")
  125. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_drawstress")
  126. , true /* destroy shaders when program is destroyed */
  127. );
  128. // Create static vertex buffer.
  129. m_vbh = bgfx::createVertexBuffer(
  130. bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) )
  131. , PosColorVertex::ms_decl
  132. );
  133. // Create static index buffer.
  134. m_ibh = bgfx::createIndexBuffer(bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) ) );
  135. // Imgui.
  136. imguiCreate();
  137. m_maxThreads = bx::min<int32_t>(caps->limits.maxEncoders, BX_COUNTOF(m_thread) );
  138. m_numThreads = (m_maxThreads+1)/2;
  139. for (int32_t ii = 0; ii < m_maxThreads; ++ii)
  140. {
  141. m_thread[ii].init(threadFunc, this);
  142. }
  143. }
  144. int shutdown() override
  145. {
  146. for (int32_t ii = 0; ii < m_maxThreads; ++ii)
  147. {
  148. m_thread[ii].push(reinterpret_cast<void*>(UINTPTR_MAX) );
  149. m_thread[ii].shutdown();
  150. }
  151. // Cleanup.
  152. imguiDestroy();
  153. bgfx::destroy(m_ibh);
  154. bgfx::destroy(m_vbh);
  155. bgfx::destroy(m_program);
  156. // Shutdown bgfx.
  157. bgfx::shutdown();
  158. return 0;
  159. }
  160. int32_t thread(bx::Thread* _thread)
  161. {
  162. for (;;)
  163. {
  164. union
  165. {
  166. void* ptr;
  167. uintptr_t id;
  168. } cast;
  169. cast.ptr = _thread->pop();
  170. if (UINTPTR_MAX == cast.id)
  171. {
  172. break;
  173. }
  174. const uint32_t numThreads = uint32_t(cast.id);
  175. const uint32_t idx = uint32_t(_thread - m_thread);
  176. const uint32_t num = uint32_t(m_dim)/numThreads;
  177. const uint32_t rem = idx == numThreads-1 ? uint32_t(m_dim)%numThreads : 0;
  178. const uint32_t xx = idx*num;
  179. submit(idx+1, xx, num + rem);
  180. }
  181. return bx::kExitSuccess;
  182. }
  183. void submit(uint32_t _tid, uint32_t _xstart, uint32_t _num)
  184. {
  185. bgfx::Encoder* encoder = bgfx::begin();
  186. if (0 != _tid)
  187. {
  188. m_sync.post();
  189. }
  190. if (NULL != encoder)
  191. {
  192. const int64_t now = bx::getHPCounter();
  193. const double freq = double(bx::getHPFrequency() );
  194. float time = (float)( (now-m_timeOffset)/freq);
  195. const float* mod = s_mod[_tid%BX_COUNTOF(s_mod)];
  196. float mtxS[16];
  197. const float scale = 0 == m_transform ? 0.25f : 0.0f;
  198. bx::mtxScale(mtxS, scale, scale, scale);
  199. const float step = 0.6f;
  200. float pos[3];
  201. pos[0] = -step*m_dim / 2.0f;
  202. pos[1] = -step*m_dim / 2.0f;
  203. pos[2] = -15.0;
  204. for (uint32_t zz = 0; zz < uint32_t(m_dim); ++zz)
  205. {
  206. for (uint32_t yy = 0; yy < uint32_t(m_dim); ++yy)
  207. {
  208. for (uint32_t xx = _xstart, xend = _xstart+_num; xx < xend; ++xx)
  209. {
  210. float mtxR[16];
  211. bx::mtxRotateXYZ(mtxR
  212. , (time + xx*0.21f)*mod[0]
  213. , (time + yy*0.37f)*mod[1]
  214. , (time + zz*0.13f)*mod[2]
  215. );
  216. float mtx[16];
  217. bx::mtxMul(mtx, mtxS, mtxR);
  218. mtx[12] = pos[0] + float(xx)*step;
  219. mtx[13] = pos[1] + float(yy)*step;
  220. mtx[14] = pos[2] + float(zz)*step;
  221. encoder->setTransform(mtx);
  222. encoder->setVertexBuffer(0, m_vbh);
  223. encoder->setIndexBuffer(m_ibh);
  224. encoder->setState(BGFX_STATE_DEFAULT);
  225. encoder->submit(0, m_program);
  226. }
  227. }
  228. }
  229. bgfx::end(encoder);
  230. }
  231. }
  232. bool update() override
  233. {
  234. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  235. {
  236. int64_t now = bx::getHPCounter();
  237. static int64_t last = now;
  238. const int64_t hpFreq = bx::getHPFrequency();
  239. const int64_t frameTime = now - last;
  240. last = now;
  241. const double freq = double(hpFreq);
  242. const double toMs = 1000.0/freq;
  243. m_deltaTimeNs += frameTime*1000000/hpFreq;
  244. if (m_deltaTimeNs > 1000000)
  245. {
  246. m_deltaTimeAvgNs = m_deltaTimeNs / bx::int64_max(1, m_numFrames);
  247. if (m_autoAdjust)
  248. {
  249. if (m_deltaTimeAvgNs < highwm)
  250. {
  251. m_dim = bx::uint32_min(m_dim + 2, m_maxDim);
  252. }
  253. else if (m_deltaTimeAvgNs > lowwm)
  254. {
  255. m_dim = bx::uint32_max(m_dim - 1, 2);
  256. }
  257. }
  258. m_deltaTimeNs = 0;
  259. m_numFrames = 0;
  260. }
  261. else
  262. {
  263. ++m_numFrames;
  264. }
  265. imguiBeginFrame(m_mouseState.m_mx
  266. , m_mouseState.m_my
  267. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  268. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  269. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  270. , m_mouseState.m_mz
  271. , uint16_t(m_width)
  272. , uint16_t(m_height)
  273. );
  274. showExampleDialog(this);
  275. ImGui::SetNextWindowPos(
  276. ImVec2((float)m_width - (float)m_width / 4.0f - 10.0f, 10.0f)
  277. , ImGuiCond_FirstUseEver
  278. );
  279. ImGui::SetNextWindowSize(
  280. ImVec2((float)m_width / 4.0f, (float)m_height / 2.0f)
  281. , ImGuiCond_FirstUseEver
  282. );
  283. ImGui::Begin("Settings"
  284. , NULL
  285. , 0
  286. );
  287. ImGui::RadioButton("Rotate",&m_transform,0);
  288. ImGui::RadioButton("No fragments",&m_transform,1);
  289. ImGui::Separator();
  290. ImGui::Checkbox("Auto adjust", &m_autoAdjust);
  291. ImGui::SliderInt("Num threads", &m_numThreads, 1, m_maxThreads);
  292. const uint32_t numThreads = m_numThreads;
  293. ImGui::SliderInt("Dim", &m_dim, 5, m_maxDim);
  294. ImGui::Text("Draw calls: %d", m_dim*m_dim*m_dim);
  295. ImGui::Text("Avg Delta Time (1 second) [ms]: %0.4f", m_deltaTimeAvgNs/1000.0f);
  296. ImGui::Separator();
  297. const bgfx::Stats* stats = bgfx::getStats();
  298. ImGui::Text("GPU %0.6f [ms]", double(stats->gpuTimeEnd - stats->gpuTimeBegin)*1000.0/stats->gpuTimerFreq);
  299. ImGui::Text("CPU %0.6f [ms]", double(stats->cpuTimeEnd - stats->cpuTimeBegin)*1000.0/stats->cpuTimerFreq);
  300. ImGui::Text("Waiting for render thread %0.6f [ms]", double(stats->waitRender) * toMs);
  301. ImGui::Text("Waiting for submit thread %0.6f [ms]", double(stats->waitSubmit) * toMs);
  302. ImGui::End();
  303. imguiEndFrame();
  304. float at[3] = { 0.0f, 0.0f, 0.0f };
  305. float eye[3] = { 0.0f, 0.0f, -35.0f };
  306. float view[16];
  307. bx::mtxLookAt(view, eye, at);
  308. const bgfx::Caps* caps = bgfx::getCaps();
  309. float proj[16];
  310. bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f, caps->homogeneousDepth);
  311. // Set view and projection matrix for view 0.
  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. // This dummy draw call is here to make sure that view 0 is cleared
  316. // if no other draw calls are submitted to view 0.
  317. bgfx::touch(0);
  318. if (1 < numThreads)
  319. {
  320. for (uint32_t ii = 0; ii < numThreads; ++ii)
  321. {
  322. m_thread[ii].push(reinterpret_cast<void*>(uintptr_t(numThreads) ) );
  323. }
  324. for (uint32_t ii = 0; ii < numThreads; ++ii)
  325. {
  326. m_sync.wait();
  327. }
  328. }
  329. else
  330. {
  331. submit(0, 0, uint32_t(m_dim) );
  332. }
  333. // Advance to next frame. Rendering thread will be kicked to
  334. // process submitted rendering primitives.
  335. bgfx::frame();
  336. return true;
  337. }
  338. return false;
  339. }
  340. entry::MouseState m_mouseState;
  341. uint32_t m_width;
  342. uint32_t m_height;
  343. uint32_t m_debug;
  344. uint32_t m_reset;
  345. bool m_autoAdjust;
  346. int32_t m_scrollArea;
  347. int32_t m_dim;
  348. int32_t m_maxDim;
  349. int32_t m_transform;
  350. int32_t m_numThreads;
  351. int32_t m_maxThreads;
  352. int64_t m_timeOffset;
  353. int64_t m_deltaTimeNs;
  354. int64_t m_deltaTimeAvgNs;
  355. int64_t m_numFrames;
  356. bx::Thread m_thread[5];
  357. bx::Semaphore m_sync;
  358. bgfx::ProgramHandle m_program;
  359. bgfx::VertexBufferHandle m_vbh;
  360. bgfx::IndexBufferHandle m_ibh;
  361. };
  362. int32_t threadFunc(bx::Thread* _thread, void* _userData)
  363. {
  364. ExampleDrawStress* self = static_cast<ExampleDrawStress*>(_userData);
  365. return self->thread(_thread);
  366. }
  367. } // namespace
  368. ENTRY_IMPLEMENT_MAIN(ExampleDrawStress, "17-drawstress", "Draw stress, maximizing number of draw calls.");