drawstress.cpp 7.5 KB

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