drawstress.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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 <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_layout
  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::VertexLayout ms_layout;
  38. };
  39. bgfx::VertexLayout PosColorVertex::ms_layout;
  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 init;
  107. init.type = args.m_type;
  108. init.vendorId = args.m_pciId;
  109. init.resolution.width = m_width;
  110. init.resolution.height = m_height;
  111. init.resolution.reset = m_reset;
  112. bgfx::init(init);
  113. const bgfx::Caps* caps = bgfx::getCaps();
  114. m_maxDim = (int32_t)bx::pow(float(caps->limits.maxDrawCalls), 1.0f/3.0f);
  115. // Enable debug text.
  116. bgfx::setDebug(m_debug);
  117. // Set view 0 clear state.
  118. bgfx::setViewClear(0
  119. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  120. , 0x303030ff
  121. , 1.0f
  122. , 0
  123. );
  124. // Create vertex stream declaration.
  125. PosColorVertex::init();
  126. bgfx::RendererType::Enum type = bgfx::getRendererType();
  127. // Create program from shaders.
  128. m_program = bgfx::createProgram(
  129. bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_drawstress")
  130. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_drawstress")
  131. , true /* destroy shaders when program is destroyed */
  132. );
  133. // Create static vertex buffer.
  134. m_vbh = bgfx::createVertexBuffer(
  135. bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) )
  136. , PosColorVertex::ms_layout
  137. );
  138. // Create static index buffer.
  139. m_ibh = bgfx::createIndexBuffer(bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) ) );
  140. // Imgui.
  141. imguiCreate();
  142. m_maxThreads = bx::min<int32_t>(caps->limits.maxEncoders, BX_COUNTOF(m_thread) );
  143. m_numThreads = (m_maxThreads+1)/2;
  144. for (int32_t ii = 0; ii < m_maxThreads; ++ii)
  145. {
  146. m_thread[ii].init(threadFunc, this);
  147. }
  148. }
  149. int shutdown() override
  150. {
  151. for (int32_t ii = 0; ii < m_maxThreads; ++ii)
  152. {
  153. m_thread[ii].push(reinterpret_cast<void*>(UINTPTR_MAX) );
  154. m_thread[ii].shutdown();
  155. }
  156. // Cleanup.
  157. imguiDestroy();
  158. bgfx::destroy(m_ibh);
  159. bgfx::destroy(m_vbh);
  160. bgfx::destroy(m_program);
  161. // Shutdown bgfx.
  162. bgfx::shutdown();
  163. return 0;
  164. }
  165. int32_t thread(bx::Thread* _thread)
  166. {
  167. for (;;)
  168. {
  169. union
  170. {
  171. void* ptr;
  172. uintptr_t id;
  173. } cast;
  174. cast.ptr = _thread->pop();
  175. if (UINTPTR_MAX == cast.id)
  176. {
  177. break;
  178. }
  179. const uint32_t numThreads = uint32_t(cast.id);
  180. const uint32_t idx = uint32_t(_thread - m_thread);
  181. const uint32_t num = uint32_t(m_dim)/numThreads;
  182. const uint32_t rem = idx == numThreads-1 ? uint32_t(m_dim)%numThreads : 0;
  183. const uint32_t xx = idx*num;
  184. submit(idx+1, xx, num + rem);
  185. }
  186. return bx::kExitSuccess;
  187. }
  188. void submit(uint32_t _tid, uint32_t _xstart, uint32_t _num)
  189. {
  190. bgfx::Encoder* encoder = bgfx::begin();
  191. if (0 != _tid)
  192. {
  193. m_sync.post();
  194. }
  195. if (NULL != encoder)
  196. {
  197. const int64_t now = bx::getHPCounter();
  198. const double freq = double(bx::getHPFrequency() );
  199. float time = (float)( (now-m_timeOffset)/freq);
  200. const float* mod = s_mod[_tid%BX_COUNTOF(s_mod)];
  201. float mtxS[16];
  202. const float scale = 0 == m_transform ? 0.25f : 0.0f;
  203. bx::mtxScale(mtxS, scale, scale, scale);
  204. const float step = 0.6f;
  205. float pos[3];
  206. pos[0] = -step*m_dim / 2.0f;
  207. pos[1] = -step*m_dim / 2.0f;
  208. pos[2] = -15.0;
  209. for (uint32_t zz = 0; zz < uint32_t(m_dim); ++zz)
  210. {
  211. for (uint32_t yy = 0; yy < uint32_t(m_dim); ++yy)
  212. {
  213. for (uint32_t xx = _xstart, xend = _xstart+_num; xx < xend; ++xx)
  214. {
  215. float mtxR[16];
  216. bx::mtxRotateXYZ(mtxR
  217. , (time + xx*0.21f)*mod[0]
  218. , (time + yy*0.37f)*mod[1]
  219. , (time + zz*0.13f)*mod[2]
  220. );
  221. float mtx[16];
  222. bx::mtxMul(mtx, mtxS, mtxR);
  223. mtx[12] = pos[0] + float(xx)*step;
  224. mtx[13] = pos[1] + float(yy)*step;
  225. mtx[14] = pos[2] + float(zz)*step;
  226. encoder->setTransform(mtx);
  227. encoder->setVertexBuffer(0, m_vbh);
  228. encoder->setIndexBuffer(m_ibh);
  229. encoder->setState(BGFX_STATE_DEFAULT);
  230. encoder->submit(0, m_program);
  231. }
  232. }
  233. }
  234. bgfx::end(encoder);
  235. }
  236. }
  237. bool update() override
  238. {
  239. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  240. {
  241. int64_t now = bx::getHPCounter();
  242. static int64_t last = now;
  243. const int64_t hpFreq = bx::getHPFrequency();
  244. const int64_t frameTime = now - last;
  245. last = now;
  246. const double freq = double(hpFreq);
  247. const double toMs = 1000.0/freq;
  248. m_deltaTimeNs += frameTime*1000000/hpFreq;
  249. if (m_deltaTimeNs > 1000000)
  250. {
  251. m_deltaTimeAvgNs = m_deltaTimeNs / bx::max<int64_t>(1, m_numFrames);
  252. if (m_autoAdjust)
  253. {
  254. if (m_deltaTimeAvgNs < highwm)
  255. {
  256. m_dim = bx::uint32_min(m_dim + 2, m_maxDim);
  257. }
  258. else if (m_deltaTimeAvgNs > lowwm)
  259. {
  260. m_dim = bx::uint32_max(m_dim - 1, 2);
  261. }
  262. }
  263. m_deltaTimeNs = 0;
  264. m_numFrames = 0;
  265. }
  266. else
  267. {
  268. ++m_numFrames;
  269. }
  270. imguiBeginFrame(m_mouseState.m_mx
  271. , m_mouseState.m_my
  272. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  273. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  274. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  275. , m_mouseState.m_mz
  276. , uint16_t(m_width)
  277. , uint16_t(m_height)
  278. );
  279. showExampleDialog(this);
  280. ImGui::SetNextWindowPos(
  281. ImVec2((float)m_width - (float)m_width / 4.0f - 10.0f, 10.0f)
  282. , ImGuiCond_FirstUseEver
  283. );
  284. ImGui::SetNextWindowSize(
  285. ImVec2((float)m_width / 4.0f, (float)m_height / 2.0f)
  286. , ImGuiCond_FirstUseEver
  287. );
  288. ImGui::Begin("Settings"
  289. , NULL
  290. , 0
  291. );
  292. ImGui::RadioButton("Rotate",&m_transform,0);
  293. ImGui::RadioButton("No fragments",&m_transform,1);
  294. ImGui::Separator();
  295. ImGui::Checkbox("Auto adjust", &m_autoAdjust);
  296. ImGui::SliderInt("Num threads", &m_numThreads, 1, m_maxThreads);
  297. const uint32_t numThreads = m_numThreads;
  298. ImGui::SliderInt("Dim", &m_dim, 5, m_maxDim);
  299. ImGui::Text("Draw calls: %d", m_dim*m_dim*m_dim);
  300. ImGui::Text("Avg Delta Time (1 second) [ms]: %0.4f", m_deltaTimeAvgNs/1000.0f);
  301. ImGui::Separator();
  302. const bgfx::Stats* stats = bgfx::getStats();
  303. ImGui::Text("GPU %0.6f [ms]", double(stats->gpuTimeEnd - stats->gpuTimeBegin)*1000.0/stats->gpuTimerFreq);
  304. ImGui::Text("CPU %0.6f [ms]", double(stats->cpuTimeEnd - stats->cpuTimeBegin)*1000.0/stats->cpuTimerFreq);
  305. ImGui::Text("Waiting for render thread %0.6f [ms]", double(stats->waitRender) * toMs);
  306. ImGui::Text("Waiting for submit thread %0.6f [ms]", double(stats->waitSubmit) * toMs);
  307. ImGui::End();
  308. imguiEndFrame();
  309. const bx::Vec3 at = { 0.0f, 0.0f, 0.0f };
  310. const bx::Vec3 eye = { 0.0f, 0.0f, -35.0f };
  311. float view[16];
  312. bx::mtxLookAt(view, eye, at);
  313. const bgfx::Caps* caps = bgfx::getCaps();
  314. float proj[16];
  315. bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f, caps->homogeneousDepth);
  316. // Set view and projection matrix for view 0.
  317. bgfx::setViewTransform(0, view, proj);
  318. // Set view 0 default viewport.
  319. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  320. // This dummy draw call is here to make sure that view 0 is cleared
  321. // if no other draw calls are submitted to view 0.
  322. bgfx::touch(0);
  323. if (1 < numThreads)
  324. {
  325. for (uint32_t ii = 0; ii < numThreads; ++ii)
  326. {
  327. m_thread[ii].push(reinterpret_cast<void*>(uintptr_t(numThreads) ) );
  328. }
  329. for (uint32_t ii = 0; ii < numThreads; ++ii)
  330. {
  331. m_sync.wait();
  332. }
  333. }
  334. else
  335. {
  336. submit(0, 0, uint32_t(m_dim) );
  337. }
  338. // Advance to next frame. Rendering thread will be kicked to
  339. // process submitted rendering primitives.
  340. bgfx::frame();
  341. return true;
  342. }
  343. return false;
  344. }
  345. entry::MouseState m_mouseState;
  346. uint32_t m_width;
  347. uint32_t m_height;
  348. uint32_t m_debug;
  349. uint32_t m_reset;
  350. bool m_autoAdjust;
  351. int32_t m_scrollArea;
  352. int32_t m_dim;
  353. int32_t m_maxDim;
  354. int32_t m_transform;
  355. int32_t m_numThreads;
  356. int32_t m_maxThreads;
  357. int64_t m_timeOffset;
  358. int64_t m_deltaTimeNs;
  359. int64_t m_deltaTimeAvgNs;
  360. int64_t m_numFrames;
  361. bx::Thread m_thread[5];
  362. bx::Semaphore m_sync;
  363. bgfx::ProgramHandle m_program;
  364. bgfx::VertexBufferHandle m_vbh;
  365. bgfx::IndexBufferHandle m_ibh;
  366. };
  367. int32_t threadFunc(bx::Thread* _thread, void* _userData)
  368. {
  369. ExampleDrawStress* self = static_cast<ExampleDrawStress*>(_userData);
  370. return self->thread(_thread);
  371. }
  372. } // namespace
  373. ENTRY_IMPLEMENT_MAIN(ExampleDrawStress, "17-drawstress", "Draw stress, maximizing number of draw calls.");