drawstress.cpp 7.5 KB

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