2
0

drawstress.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. * Copyright 2011-2016 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. // 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 ExampleDrawStress : public entry::AppI
  63. {
  64. void init(int _argc, char** _argv) BX_OVERRIDE
  65. {
  66. Args args(_argc, _argv);
  67. m_width = 1280;
  68. m_height = 720;
  69. m_debug = BGFX_DEBUG_TEXT;
  70. m_reset = BGFX_RESET_NONE;
  71. m_autoAdjust = true;
  72. m_scrollArea = 0;
  73. m_dim = 16;
  74. m_maxDim = 40;
  75. m_transform = 0;
  76. m_timeOffset = bx::getHPCounter();
  77. m_deltaTimeNs = 0;
  78. m_deltaTimeAvgNs = 0;
  79. m_numFrames = 0;
  80. bgfx::init(args.m_type, args.m_pciId);
  81. bgfx::reset(m_width, m_height, m_reset);
  82. const bgfx::Caps* caps = bgfx::getCaps();
  83. m_maxDim = (int32_t)powf(float(caps->maxDrawCalls), 1.0f/3.0f);
  84. // Enable debug text.
  85. bgfx::setDebug(m_debug);
  86. // Set view 0 clear state.
  87. bgfx::setViewClear(0
  88. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  89. , 0x303030ff
  90. , 1.0f
  91. , 0
  92. );
  93. // Create vertex stream declaration.
  94. PosColorVertex::init();
  95. const bgfx::Memory* vs_drawstress;
  96. const bgfx::Memory* fs_drawstress;
  97. switch (bgfx::getRendererType() )
  98. {
  99. case bgfx::RendererType::Direct3D9:
  100. vs_drawstress = bgfx::makeRef(vs_drawstress_dx9, sizeof(vs_drawstress_dx9) );
  101. fs_drawstress = bgfx::makeRef(fs_drawstress_dx9, sizeof(fs_drawstress_dx9) );
  102. break;
  103. case bgfx::RendererType::Direct3D11:
  104. case bgfx::RendererType::Direct3D12:
  105. vs_drawstress = bgfx::makeRef(vs_drawstress_dx11, sizeof(vs_drawstress_dx11) );
  106. fs_drawstress = bgfx::makeRef(fs_drawstress_dx11, sizeof(fs_drawstress_dx11) );
  107. break;
  108. case bgfx::RendererType::Metal:
  109. vs_drawstress = bgfx::makeRef(vs_drawstress_mtl, sizeof(vs_drawstress_mtl) );
  110. fs_drawstress = bgfx::makeRef(fs_drawstress_mtl, sizeof(fs_drawstress_mtl) );
  111. break;
  112. default:
  113. vs_drawstress = bgfx::makeRef(vs_drawstress_glsl, sizeof(vs_drawstress_glsl) );
  114. fs_drawstress = bgfx::makeRef(fs_drawstress_glsl, sizeof(fs_drawstress_glsl) );
  115. break;
  116. }
  117. // Create program from shaders.
  118. m_program = bgfx::createProgram(
  119. bgfx::createShader(vs_drawstress)
  120. , bgfx::createShader(fs_drawstress)
  121. , true /* destroy shaders when program is destroyed */
  122. );
  123. // Create static vertex buffer.
  124. m_vbh = bgfx::createVertexBuffer(
  125. bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) )
  126. , PosColorVertex::ms_decl
  127. );
  128. // Create static index buffer.
  129. m_ibh = bgfx::createIndexBuffer(bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) ) );
  130. // Imgui.
  131. imguiCreate();
  132. }
  133. int shutdown() BX_OVERRIDE
  134. {
  135. // Cleanup.
  136. imguiDestroy();
  137. bgfx::destroyIndexBuffer(m_ibh);
  138. bgfx::destroyVertexBuffer(m_vbh);
  139. bgfx::destroyProgram(m_program);
  140. // Shutdown bgfx.
  141. bgfx::shutdown();
  142. return 0;
  143. }
  144. bool update() BX_OVERRIDE
  145. {
  146. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  147. {
  148. int64_t now = bx::getHPCounter();
  149. static int64_t last = now;
  150. const int64_t hpFreq = bx::getHPFrequency();
  151. const int64_t frameTime = now - last;
  152. last = now;
  153. const double freq = double(hpFreq);
  154. const double toMs = 1000.0/freq;
  155. m_deltaTimeNs += frameTime*1000000/hpFreq;
  156. if (m_deltaTimeNs > 1000000)
  157. {
  158. m_deltaTimeAvgNs = m_deltaTimeNs / bx::int64_max(1, m_numFrames);
  159. if (m_autoAdjust)
  160. {
  161. if (m_deltaTimeAvgNs < highwm)
  162. {
  163. m_dim = bx::uint32_min(m_dim + 2, m_maxDim);
  164. }
  165. else if (m_deltaTimeAvgNs > lowwm)
  166. {
  167. m_dim = bx::uint32_max(m_dim - 1, 2);
  168. }
  169. }
  170. m_deltaTimeNs = 0;
  171. m_numFrames = 0;
  172. }
  173. else
  174. {
  175. ++m_numFrames;
  176. }
  177. float time = (float)( (now-m_timeOffset)/freq);
  178. imguiBeginFrame(m_mouseState.m_mx
  179. , m_mouseState.m_my
  180. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  181. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  182. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  183. , m_mouseState.m_mz
  184. , m_width
  185. , m_height
  186. );
  187. imguiBeginScrollArea("Settings", m_width - m_width / 4 - 10, 10, m_width / 4, m_height / 2, &m_scrollArea);
  188. imguiSeparatorLine();
  189. m_transform = imguiChoose(m_transform
  190. , "Rotate"
  191. , "No fragments"
  192. );
  193. imguiSeparatorLine();
  194. if (imguiCheck("Auto adjust", m_autoAdjust) )
  195. {
  196. m_autoAdjust ^= true;
  197. }
  198. imguiSlider("Dim", m_dim, 5, m_maxDim);
  199. imguiLabel("Draw calls: %d", m_dim*m_dim*m_dim);
  200. imguiLabel("Avg Delta Time (1 second) [ms]: %0.4f", m_deltaTimeAvgNs/1000.0f);
  201. imguiSeparatorLine();
  202. const bgfx::Stats* stats = bgfx::getStats();
  203. imguiLabel("GPU %0.6f [ms]", double(stats->gpuTimeEnd - stats->gpuTimeBegin)*1000.0/stats->gpuTimerFreq);
  204. imguiLabel("CPU %0.6f [ms]", double(stats->cpuTimeEnd - stats->cpuTimeBegin)*1000.0/stats->cpuTimerFreq);
  205. imguiLabel("Waiting for render thread %0.6f [ms]", double(stats->waitRender) * toMs);
  206. imguiLabel("Waiting for submit thread %0.6f [ms]", double(stats->waitSubmit) * toMs);
  207. imguiEndScrollArea();
  208. imguiEndFrame();
  209. float at[3] = { 0.0f, 0.0f, 0.0f };
  210. float eye[3] = { 0.0f, 0.0f, -35.0f };
  211. float view[16];
  212. float proj[16];
  213. bx::mtxLookAt(view, eye, at);
  214. bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f);
  215. // Set view and projection matrix for view 0.
  216. bgfx::setViewTransform(0, view, proj);
  217. // Set view 0 default viewport.
  218. bgfx::setViewRect(0, 0, 0, m_width, m_height);
  219. // This dummy draw call is here to make sure that view 0 is cleared
  220. // if no other draw calls are submitted to view 0.
  221. bgfx::touch(0);
  222. // Use debug font to print information about this example.
  223. bgfx::dbgTextClear();
  224. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/17-drawstress");
  225. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Draw stress, maximizing number of draw calls.");
  226. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: %7.3f[ms]", double(frameTime)*toMs);
  227. float mtxS[16];
  228. const float scale = 0 == m_transform ? 0.25f : 0.0f;
  229. bx::mtxScale(mtxS, scale, scale, scale);
  230. const float step = 0.6f;
  231. float pos[3];
  232. pos[0] = -step*m_dim / 2.0f;
  233. pos[1] = -step*m_dim / 2.0f;
  234. pos[2] = -15.0;
  235. for (uint32_t zz = 0; zz < uint32_t(m_dim); ++zz)
  236. {
  237. for (uint32_t yy = 0; yy < uint32_t(m_dim); ++yy)
  238. {
  239. for (uint32_t xx = 0; xx < uint32_t(m_dim); ++xx)
  240. {
  241. float mtxR[16];
  242. bx::mtxRotateXYZ(mtxR, time + xx*0.21f, time + yy*0.37f, time + yy*0.13f);
  243. float mtx[16];
  244. bx::mtxMul(mtx, mtxS, mtxR);
  245. mtx[12] = pos[0] + float(xx)*step;
  246. mtx[13] = pos[1] + float(yy)*step;
  247. mtx[14] = pos[2] + float(zz)*step;
  248. // Set model matrix for rendering.
  249. bgfx::setTransform(mtx);
  250. // Set vertex and index buffer.
  251. bgfx::setVertexBuffer(m_vbh);
  252. bgfx::setIndexBuffer(m_ibh);
  253. // Set render states.
  254. bgfx::setState(BGFX_STATE_DEFAULT);
  255. // Submit primitive for rendering to view 0.
  256. bgfx::submit(0, m_program);
  257. }
  258. }
  259. }
  260. // Advance to next frame. Rendering thread will be kicked to
  261. // process submitted rendering primitives.
  262. bgfx::frame();
  263. return true;
  264. }
  265. return false;
  266. }
  267. entry::MouseState m_mouseState;
  268. uint32_t m_width;
  269. uint32_t m_height;
  270. uint32_t m_debug;
  271. uint32_t m_reset;
  272. bool m_autoAdjust;
  273. int32_t m_scrollArea;
  274. int32_t m_dim;
  275. int32_t m_maxDim;
  276. uint32_t m_transform;
  277. int64_t m_timeOffset;
  278. int64_t m_deltaTimeNs;
  279. int64_t m_deltaTimeAvgNs;
  280. int64_t m_numFrames;
  281. bgfx::ProgramHandle m_program;
  282. bgfx::VertexBufferHandle m_vbh;
  283. bgfx::IndexBufferHandle m_ibh;
  284. };
  285. ENTRY_IMPLEMENT_MAIN(ExampleDrawStress);