drawstress.cpp 8.3 KB

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