drawstress.cpp 8.2 KB

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