drawstress.cpp 7.8 KB

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