example-glue.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * Copyright 2011-2018 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. #include "imgui/imgui.h"
  6. #include "entry/entry.h"
  7. #include "entry/cmd.h"
  8. #include <bx/string.h>
  9. #include <bx/timer.h>
  10. #include <bx/math.h>
  11. static bool bar(float _width, float _maxWidth, float _height, const ImVec4& _color)
  12. {
  13. const ImGuiStyle& style = ImGui::GetStyle();
  14. ImVec4 hoveredColor(
  15. _color.x + _color.x*0.1f
  16. , _color.y + _color.y*0.1f
  17. , _color.z + _color.z*0.1f
  18. , _color.w + _color.w*0.1f
  19. );
  20. ImGui::PushStyleColor(ImGuiCol_Button, _color);
  21. ImGui::PushStyleColor(ImGuiCol_ButtonHovered, hoveredColor);
  22. ImGui::PushStyleColor(ImGuiCol_ButtonActive, _color);
  23. ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 0.0f);
  24. ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0.0f, style.ItemSpacing.y) );
  25. bool itemHovered = false;
  26. ImGui::Button("", ImVec2(_width, _height) );
  27. itemHovered |= ImGui::IsItemHovered();
  28. ImGui::SameLine();
  29. ImGui::InvisibleButton("", ImVec2(_maxWidth-_width, _height) );
  30. itemHovered |= ImGui::IsItemHovered();
  31. ImGui::PopStyleVar(2);
  32. ImGui::PopStyleColor(3);
  33. return itemHovered;
  34. }
  35. static const ImVec4 s_resourceColor(0.5f, 0.5f, 0.5f, 1.0f);
  36. static void resourceBar(const char* _name, const char* _tooltip, uint32_t _num, uint32_t _max, float _maxWidth, float _height)
  37. {
  38. bool itemHovered = false;
  39. ImGui::Text("%s: %4d / %4d", _name, _num, _max);
  40. itemHovered |= ImGui::IsItemHovered();
  41. ImGui::SameLine();
  42. const float percentage = float(_num)/float(_max);
  43. itemHovered |= bar(bx::max(1.0f, percentage*_maxWidth), _maxWidth, _height, s_resourceColor);
  44. ImGui::SameLine();
  45. ImGui::Text("%5.2f%%", percentage*100.0f);
  46. if (itemHovered)
  47. {
  48. ImGui::SetTooltip("%s %5.2f%%"
  49. , _tooltip
  50. , percentage*100.0f
  51. );
  52. }
  53. }
  54. static bool s_showStats = false;
  55. void showExampleDialog(entry::AppI* _app, const char* _errorText)
  56. {
  57. char temp[1024];
  58. bx::snprintf(temp, BX_COUNTOF(temp), "Example: %s", _app->getName() );
  59. ImGui::SetNextWindowPos(
  60. ImVec2(10.0f, 50.0f)
  61. , ImGuiCond_FirstUseEver
  62. );
  63. ImGui::SetNextWindowSize(
  64. ImVec2(300.0f, 200.0f)
  65. , ImGuiCond_FirstUseEver
  66. );
  67. ImGui::Begin(temp);
  68. ImGui::TextWrapped("%s", _app->getDescription() );
  69. ImGui::Separator();
  70. if (NULL != _errorText)
  71. {
  72. const int64_t now = bx::getHPCounter();
  73. const int64_t freq = bx::getHPFrequency();
  74. const float time = float(now%freq)/float(freq);
  75. bool blink = time > 0.5f;
  76. ImGui::PushStyleColor(ImGuiCol_Text
  77. , blink
  78. ? ImVec4(1.0, 0.0, 0.0, 1.0)
  79. : ImVec4(1.0, 1.0, 1.0, 1.0)
  80. );
  81. ImGui::TextWrapped("%s", _errorText);
  82. ImGui::Separator();
  83. ImGui::PopStyleColor();
  84. }
  85. {
  86. uint32_t num = entry::getNumApps();
  87. const char** items = (const char**)alloca(num*sizeof(void*) );
  88. uint32_t ii = 0;
  89. int32_t current = 0;
  90. for (entry::AppI* app = entry::getFirstApp(); NULL != app; app = app->getNext() )
  91. {
  92. if (app == _app)
  93. {
  94. current = ii;
  95. }
  96. items[ii++] = app->getName();
  97. }
  98. if (1 < num
  99. && ImGui::Combo("Example", &current, items, num) )
  100. {
  101. char command[1024];
  102. bx::snprintf(command, BX_COUNTOF(command), "app restart %s", items[current]);
  103. cmdExec(command);
  104. }
  105. const bgfx::Caps* caps = bgfx::getCaps();
  106. if (0 != (caps->supported & BGFX_CAPS_GRAPHICS_DEBUGGER) )
  107. {
  108. ImGui::SameLine();
  109. ImGui::Text(ICON_FA_SNOWFLAKE_O);
  110. }
  111. ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(3.0f, 3.0f) );
  112. if (ImGui::Button(ICON_FA_REPEAT " Restart" ) )
  113. {
  114. cmdExec("app restart");
  115. }
  116. if (1 < entry::getNumApps() )
  117. {
  118. ImGui::SameLine();
  119. if (ImGui::Button(ICON_KI_PREVIOUS " Prev") )
  120. {
  121. cmdExec("app restart prev");
  122. }
  123. ImGui::SameLine();
  124. if (ImGui::Button(ICON_KI_NEXT " Next") )
  125. {
  126. cmdExec("app restart next");
  127. }
  128. }
  129. ImGui::SameLine();
  130. if (ImGui::Button(ICON_KI_EXIT " Exit") )
  131. {
  132. cmdExec("exit");
  133. }
  134. ImGui::SameLine();
  135. s_showStats ^= ImGui::Button(ICON_FA_BAR_CHART);
  136. ImGui::PopStyleVar();
  137. }
  138. #if 0
  139. {
  140. bgfx::RendererType::Enum supportedRenderers[bgfx::RendererType::Count];
  141. uint8_t num = bgfx::getSupportedRenderers(BX_COUNTOF(supportedRenderers), supportedRenderers);
  142. const bgfx::Caps* caps = bgfx::getCaps();
  143. const char* items[bgfx::RendererType::Count];
  144. int32_t current = 0;
  145. for (uint8_t ii = 0; ii < num; ++ii)
  146. {
  147. items[ii] = bgfx::getRendererName(supportedRenderers[ii]);
  148. if (supportedRenderers[ii] == caps->rendererType)
  149. {
  150. current = ii;
  151. }
  152. }
  153. if (ImGui::Combo("Renderer", &current, items, num) )
  154. {
  155. cmdExec("app restart");
  156. }
  157. num = caps->numGPUs;
  158. if (0 != num)
  159. {
  160. current = 0;
  161. for (uint8_t ii = 0; ii < num; ++ii)
  162. {
  163. const bgfx::Caps::GPU& gpu = caps->gpu[ii];
  164. items[ii] = gpu.vendorId == BGFX_PCI_ID_AMD ? "AMD"
  165. : gpu.vendorId == BGFX_PCI_ID_INTEL ? "Intel"
  166. : gpu.vendorId == BGFX_PCI_ID_NVIDIA ? "nVidia"
  167. : "Unknown?"
  168. ;
  169. if (caps->vendorId == gpu.vendorId
  170. && caps->deviceId == gpu.deviceId)
  171. {
  172. current = ii;
  173. }
  174. }
  175. if (ImGui::Combo("GPU", &current, items, num) )
  176. {
  177. cmdExec("app restart");
  178. }
  179. }
  180. }
  181. #endif // 0
  182. const bgfx::Stats* stats = bgfx::getStats();
  183. const double toMsCpu = 1000.0/stats->cpuTimerFreq;
  184. const double toMsGpu = 1000.0/stats->gpuTimerFreq;
  185. const double frameMs = double(stats->cpuTimeFrame)*toMsCpu;
  186. ImGui::Text("Frame %0.3f [ms], %0.3f FPS"
  187. , frameMs
  188. , 1000.0/frameMs
  189. );
  190. ImGui::Text("Submit CPU %0.3f, GPU %0.3f (L: %d)"
  191. , double(stats->cpuTimeEnd - stats->cpuTimeBegin)*toMsCpu
  192. , double(stats->gpuTimeEnd - stats->gpuTimeBegin)*toMsGpu
  193. , stats->maxGpuLatency
  194. );
  195. if (-INT64_MAX != stats->gpuMemoryUsed)
  196. {
  197. char tmp0[64];
  198. bx::prettify(tmp0, BX_COUNTOF(tmp0), stats->gpuMemoryUsed);
  199. char tmp1[64];
  200. bx::prettify(tmp1, BX_COUNTOF(tmp1), stats->gpuMemoryMax);
  201. ImGui::Text("GPU mem: %s / %s", tmp0, tmp1);
  202. }
  203. if (s_showStats)
  204. {
  205. ImGui::SetNextWindowSize(
  206. ImVec2(300.0f, 500.0f)
  207. , ImGuiCond_FirstUseEver
  208. );
  209. if (ImGui::Begin(ICON_FA_BAR_CHART " Stats", &s_showStats) )
  210. {
  211. if (ImGui::CollapsingHeader(ICON_FA_PUZZLE_PIECE " Resources") )
  212. {
  213. const bgfx::Caps* caps = bgfx::getCaps();
  214. const float itemHeight = ImGui::GetTextLineHeightWithSpacing();
  215. const float maxWidth = 90.0f;
  216. ImGui::PushFont(ImGui::Font::Mono);
  217. ImGui::Text("Res: Num / Max");
  218. resourceBar("DIB", "Dynamic index buffers", stats->numDynamicIndexBuffers, caps->limits.maxDynamicIndexBuffers, maxWidth, itemHeight);
  219. resourceBar("DVB", "Dynamic vertex buffers", stats->numDynamicVertexBuffers, caps->limits.maxDynamicVertexBuffers, maxWidth, itemHeight);
  220. resourceBar(" FB", "Frame buffers", stats->numFrameBuffers, caps->limits.maxFrameBuffers, maxWidth, itemHeight);
  221. resourceBar(" IB", "Index buffers", stats->numIndexBuffers, caps->limits.maxIndexBuffers, maxWidth, itemHeight);
  222. resourceBar(" OQ", "Occlusion queries", stats->numOcclusionQueries, caps->limits.maxOcclusionQueries, maxWidth, itemHeight);
  223. resourceBar(" P", "Programs", stats->numPrograms, caps->limits.maxPrograms, maxWidth, itemHeight);
  224. resourceBar(" S", "Shaders", stats->numShaders, caps->limits.maxShaders, maxWidth, itemHeight);
  225. resourceBar(" T", "Textures", stats->numTextures, caps->limits.maxTextures, maxWidth, itemHeight);
  226. resourceBar(" U", "Uniforms", stats->numUniforms, caps->limits.maxUniforms, maxWidth, itemHeight);
  227. resourceBar(" VB", "Vertex buffers", stats->numVertexBuffers, caps->limits.maxVertexBuffers, maxWidth, itemHeight);
  228. resourceBar(" VD", "Vertex declarations", stats->numVertexDecls, caps->limits.maxVertexDecls, maxWidth, itemHeight);
  229. ImGui::PopFont();
  230. }
  231. if (ImGui::CollapsingHeader(ICON_FA_CLOCK_O " Profiler") )
  232. {
  233. if (0 == stats->numViews)
  234. {
  235. ImGui::Text("Profiler is not enabled.");
  236. }
  237. else
  238. {
  239. if (ImGui::BeginChild("##view_profiler", ImVec2(0.0f, 0.0f) ) )
  240. {
  241. ImGui::PushFont(ImGui::Font::Mono);
  242. ImVec4 cpuColor(0.5f, 1.0f, 0.5f, 1.0f);
  243. ImVec4 gpuColor(0.5f, 0.5f, 1.0f, 1.0f);
  244. const float itemHeight = ImGui::GetTextLineHeightWithSpacing();
  245. const float itemHeightWithSpacing = ImGui::GetFrameHeightWithSpacing();
  246. const double toCpuMs = 1000.0/double(stats->cpuTimerFreq);
  247. const double toGpuMs = 1000.0/double(stats->gpuTimerFreq);
  248. const float scale = 3.0f;
  249. if (ImGui::ListBoxHeader("Encoders", ImVec2(ImGui::GetWindowWidth(), stats->numEncoders*itemHeightWithSpacing) ) )
  250. {
  251. ImGuiListClipper clipper(stats->numEncoders, itemHeight);
  252. while (clipper.Step() )
  253. {
  254. for (int32_t pos = clipper.DisplayStart; pos < clipper.DisplayEnd; ++pos)
  255. {
  256. const bgfx::EncoderStats& encoderStats = stats->encoderStats[pos];
  257. ImGui::Text("%3d", pos);
  258. ImGui::SameLine(64.0f);
  259. const float maxWidth = 30.0f*scale;
  260. const float cpuMs = float( (encoderStats.cpuTimeEnd-encoderStats.cpuTimeBegin)*toCpuMs);
  261. const float cpuWidth = bx::clamp(cpuMs*scale, 1.0f, maxWidth);
  262. if (bar(cpuWidth, maxWidth, itemHeight, cpuColor) )
  263. {
  264. ImGui::SetTooltip("Encoder %d, CPU: %f [ms]"
  265. , pos
  266. , cpuMs
  267. );
  268. }
  269. }
  270. }
  271. ImGui::ListBoxFooter();
  272. }
  273. ImGui::Separator();
  274. if (ImGui::ListBoxHeader("Views", ImVec2(ImGui::GetWindowWidth(), stats->numViews*itemHeightWithSpacing) ) )
  275. {
  276. ImGuiListClipper clipper(stats->numViews, itemHeight);
  277. while (clipper.Step() )
  278. {
  279. for (int32_t pos = clipper.DisplayStart; pos < clipper.DisplayEnd; ++pos)
  280. {
  281. const bgfx::ViewStats& viewStats = stats->viewStats[pos];
  282. ImGui::Text("%3d %3d %s", pos, viewStats.view, viewStats.name);
  283. const float maxWidth = 30.0f*scale;
  284. const float cpuWidth = bx::clamp(float(viewStats.cpuTimeElapsed*toCpuMs)*scale, 1.0f, maxWidth);
  285. const float gpuWidth = bx::clamp(float(viewStats.gpuTimeElapsed*toGpuMs)*scale, 1.0f, maxWidth);
  286. ImGui::SameLine(64.0f);
  287. if (bar(cpuWidth, maxWidth, itemHeight, cpuColor) )
  288. {
  289. ImGui::SetTooltip("View %d \"%s\", CPU: %f [ms]"
  290. , pos
  291. , viewStats.name
  292. , viewStats.cpuTimeElapsed*toCpuMs
  293. );
  294. }
  295. ImGui::SameLine();
  296. if (bar(gpuWidth, maxWidth, itemHeight, gpuColor) )
  297. {
  298. ImGui::SetTooltip("View: %d \"%s\", GPU: %f [ms]"
  299. , pos
  300. , viewStats.name
  301. , viewStats.gpuTimeElapsed*toGpuMs
  302. );
  303. }
  304. }
  305. }
  306. ImGui::ListBoxFooter();
  307. }
  308. ImGui::PopFont();
  309. }
  310. ImGui::EndChild();
  311. }
  312. }
  313. ImGui::End();
  314. }
  315. }
  316. ImGui::End();
  317. }