drawstress.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. * Copyright 2011-2015 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #include <bgfx/bgfx.h>
  6. #include <bx/uint32_t.h>
  7. #include "common.h"
  8. #include "imgui/imgui.h"
  9. // embedded shaders
  10. #include "vs_drawstress.bin.h"
  11. #include "fs_drawstress.bin.h"
  12. struct PosColorVertex
  13. {
  14. float m_x;
  15. float m_y;
  16. float m_z;
  17. uint32_t m_abgr;
  18. static void init()
  19. {
  20. ms_decl
  21. .begin()
  22. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  23. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  24. .end();
  25. }
  26. static bgfx::VertexDecl ms_decl;
  27. };
  28. bgfx::VertexDecl PosColorVertex::ms_decl;
  29. static PosColorVertex s_cubeVertices[8] =
  30. {
  31. {-1.0f, 1.0f, 1.0f, 0xff000000 },
  32. { 1.0f, 1.0f, 1.0f, 0xff0000ff },
  33. {-1.0f, -1.0f, 1.0f, 0xff00ff00 },
  34. { 1.0f, -1.0f, 1.0f, 0xff00ffff },
  35. {-1.0f, 1.0f, -1.0f, 0xffff0000 },
  36. { 1.0f, 1.0f, -1.0f, 0xffff00ff },
  37. {-1.0f, -1.0f, -1.0f, 0xffffff00 },
  38. { 1.0f, -1.0f, -1.0f, 0xffffffff },
  39. };
  40. static const uint16_t s_cubeIndices[36] =
  41. {
  42. 0, 1, 2, // 0
  43. 1, 3, 2,
  44. 4, 6, 5, // 2
  45. 5, 6, 7,
  46. 0, 2, 4, // 4
  47. 4, 2, 6,
  48. 1, 5, 3, // 6
  49. 5, 7, 3,
  50. 0, 4, 1, // 8
  51. 4, 5, 1,
  52. 2, 3, 6, // 10
  53. 6, 3, 7,
  54. };
  55. #if BX_PLATFORM_EMSCRIPTEN || BX_PLATFORM_NACL
  56. static const int64_t highwm = 1000000/35;
  57. static const int64_t lowwm = 1000000/27;
  58. #else
  59. static const int64_t highwm = 1000000/65;
  60. static const int64_t lowwm = 1000000/57;
  61. #endif // BX_PLATFORM_EMSCRIPTEN || BX_PLATFORM_NACL
  62. class DrawStress : public entry::AppI
  63. {
  64. void init(int /*_argc*/, char** /*_argv*/) BX_OVERRIDE
  65. {
  66. m_width = 1280;
  67. m_height = 720;
  68. m_debug = BGFX_DEBUG_TEXT;
  69. m_reset = BGFX_RESET_NONE;
  70. m_autoAdjust = true;
  71. m_scrollArea = 0;
  72. m_dim = 16;
  73. m_maxDim = 40;
  74. m_transform = 0;
  75. m_timeOffset = bx::getHPCounter();
  76. m_deltaTimeNs = 0;
  77. m_deltaTimeAvgNs = 0;
  78. m_numFrames = 0;
  79. bgfx::init();
  80. bgfx::reset(m_width, m_height, m_reset);
  81. const bgfx::Caps* caps = bgfx::getCaps();
  82. m_maxDim = (int32_t)powf(float(caps->maxDrawCalls), 1.0f/3.0f);
  83. // Enable debug text.
  84. bgfx::setDebug(m_debug);
  85. // Set view 0 clear state.
  86. bgfx::setViewClear(0
  87. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  88. , 0x303030ff
  89. , 1.0f
  90. , 0
  91. );
  92. // Create vertex stream declaration.
  93. PosColorVertex::init();
  94. const bgfx::Memory* vs_drawstress;
  95. const bgfx::Memory* fs_drawstress;
  96. switch (bgfx::getRendererType() )
  97. {
  98. case bgfx::RendererType::Direct3D9:
  99. vs_drawstress = bgfx::makeRef(vs_drawstress_dx9, sizeof(vs_drawstress_dx9) );
  100. fs_drawstress = bgfx::makeRef(fs_drawstress_dx9, sizeof(fs_drawstress_dx9) );
  101. break;
  102. case bgfx::RendererType::Direct3D11:
  103. case bgfx::RendererType::Direct3D12:
  104. vs_drawstress = bgfx::makeRef(vs_drawstress_dx11, sizeof(vs_drawstress_dx11) );
  105. fs_drawstress = bgfx::makeRef(fs_drawstress_dx11, sizeof(fs_drawstress_dx11) );
  106. break;
  107. default:
  108. vs_drawstress = bgfx::makeRef(vs_drawstress_glsl, sizeof(vs_drawstress_glsl) );
  109. fs_drawstress = bgfx::makeRef(fs_drawstress_glsl, sizeof(fs_drawstress_glsl) );
  110. break;
  111. }
  112. // Create program from shaders.
  113. m_program = bgfx::createProgram(
  114. bgfx::createShader(vs_drawstress)
  115. , bgfx::createShader(fs_drawstress)
  116. , true /* destroy shaders when program is destroyed */
  117. );
  118. // Create static vertex buffer.
  119. m_vbh = bgfx::createVertexBuffer(
  120. bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) )
  121. , PosColorVertex::ms_decl
  122. );
  123. // Create static index buffer.
  124. m_ibh = bgfx::createIndexBuffer(bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) ) );
  125. // Imgui.
  126. imguiCreate();
  127. }
  128. int shutdown() BX_OVERRIDE
  129. {
  130. // Cleanup.
  131. imguiDestroy();
  132. bgfx::destroyIndexBuffer(m_ibh);
  133. bgfx::destroyVertexBuffer(m_vbh);
  134. bgfx::destroyProgram(m_program);
  135. // Shutdown bgfx.
  136. bgfx::shutdown();
  137. return 0;
  138. }
  139. bool update() BX_OVERRIDE
  140. {
  141. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  142. {
  143. int64_t now = bx::getHPCounter();
  144. static int64_t last = now;
  145. const int64_t hpFreq = bx::getHPFrequency();
  146. const int64_t frameTime = now - last;
  147. last = now;
  148. const double freq = double(hpFreq);
  149. const double toMs = 1000.0/freq;
  150. m_deltaTimeNs += frameTime*1000000/hpFreq;
  151. if (m_deltaTimeNs > 1000000)
  152. {
  153. m_deltaTimeAvgNs = m_deltaTimeNs / bx::int64_max(1, m_numFrames);
  154. if (m_autoAdjust)
  155. {
  156. if (m_deltaTimeAvgNs < highwm)
  157. {
  158. m_dim = bx::uint32_min(m_dim + 2, m_maxDim);
  159. }
  160. else if (m_deltaTimeAvgNs > lowwm)
  161. {
  162. m_dim = bx::uint32_max(m_dim - 1, 2);
  163. }
  164. }
  165. m_deltaTimeNs = 0;
  166. m_numFrames = 0;
  167. }
  168. else
  169. {
  170. ++m_numFrames;
  171. }
  172. float time = (float)( (now-m_timeOffset)/freq);
  173. imguiBeginFrame(m_mouseState.m_mx
  174. , m_mouseState.m_my
  175. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  176. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  177. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  178. , m_mouseState.m_mz
  179. , m_width
  180. , m_height
  181. );
  182. imguiBeginScrollArea("Settings", m_width - m_width / 4 - 10, 10, m_width / 4, m_height / 3, &m_scrollArea);
  183. imguiSeparatorLine();
  184. m_transform = imguiChoose(m_transform
  185. , "Rotate"
  186. , "No fragments"
  187. );
  188. imguiSeparatorLine();
  189. if (imguiCheck("Auto adjust", m_autoAdjust) )
  190. {
  191. m_autoAdjust ^= true;
  192. }
  193. imguiSlider("Dim", m_dim, 5, m_maxDim);
  194. imguiLabel("Draw calls: %d", m_dim*m_dim*m_dim);
  195. imguiLabel("Avg Delta Time (1 second) [ms]: %0.4f", m_deltaTimeAvgNs/1000.0f);
  196. imguiEndScrollArea();
  197. imguiEndFrame();
  198. float at[3] = { 0.0f, 0.0f, 0.0f };
  199. float eye[3] = { 0.0f, 0.0f, -35.0f };
  200. float view[16];
  201. float proj[16];
  202. bx::mtxLookAt(view, eye, at);
  203. bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f);
  204. // Set view and projection matrix for view 0.
  205. bgfx::setViewTransform(0, view, proj);
  206. // Set view 0 default viewport.
  207. bgfx::setViewRect(0, 0, 0, m_width, m_height);
  208. // This dummy draw call is here to make sure that view 0 is cleared
  209. // if no other draw calls are submitted to view 0.
  210. bgfx::touch(0);
  211. // Use debug font to print information about this example.
  212. bgfx::dbgTextClear();
  213. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/17-drawstress");
  214. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Draw stress, maximizing number of draw calls.");
  215. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: %7.3f[ms]", double(frameTime)*toMs);
  216. float mtxS[16];
  217. const float scale = 0 == m_transform ? 0.25f : 0.0f;
  218. bx::mtxScale(mtxS, scale, scale, scale);
  219. const float step = 0.6f;
  220. float pos[3];
  221. pos[0] = -step*m_dim / 2.0f;
  222. pos[1] = -step*m_dim / 2.0f;
  223. pos[2] = -15.0;
  224. for (uint32_t zz = 0; zz < uint32_t(m_dim); ++zz)
  225. {
  226. for (uint32_t yy = 0; yy < uint32_t(m_dim); ++yy)
  227. {
  228. for (uint32_t xx = 0; xx < uint32_t(m_dim); ++xx)
  229. {
  230. float mtxR[16];
  231. bx::mtxRotateXYZ(mtxR, time + xx*0.21f, time + yy*0.37f, time + yy*0.13f);
  232. float mtx[16];
  233. bx::mtxMul(mtx, mtxS, mtxR);
  234. mtx[12] = pos[0] + float(xx)*step;
  235. mtx[13] = pos[1] + float(yy)*step;
  236. mtx[14] = pos[2] + float(zz)*step;
  237. // Set model matrix for rendering.
  238. bgfx::setTransform(mtx);
  239. // Set vertex and index buffer.
  240. bgfx::setVertexBuffer(m_vbh);
  241. bgfx::setIndexBuffer(m_ibh);
  242. // Set render states.
  243. bgfx::setState(BGFX_STATE_DEFAULT);
  244. // Submit primitive for rendering to view 0.
  245. bgfx::submit(0, m_program);
  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. return false;
  255. }
  256. entry::MouseState m_mouseState;
  257. uint32_t m_width;
  258. uint32_t m_height;
  259. uint32_t m_debug;
  260. uint32_t m_reset;
  261. bool m_autoAdjust;
  262. int32_t m_scrollArea;
  263. int32_t m_dim;
  264. int32_t m_maxDim;
  265. uint32_t m_transform;
  266. int64_t m_timeOffset;
  267. int64_t m_deltaTimeNs;
  268. int64_t m_deltaTimeAvgNs;
  269. int64_t m_numFrames;
  270. bgfx::ProgramHandle m_program;
  271. bgfx::VertexBufferHandle m_vbh;
  272. bgfx::IndexBufferHandle m_ibh;
  273. };
  274. ENTRY_IMPLEMENT_MAIN(DrawStress);