example-glue.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. * Copyright 2011-2017 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 bool resourceBar(const char* _name, uint32_t _num, uint32_t _max, float _maxWidth, float _height)
  37. {
  38. ImGui::Text("%s: %4d / %4d", _name, _num, _max);
  39. ImGui::SameLine();
  40. return bar(bx::max(1.0f, float(_num*_maxWidth)/float(_max) ), _maxWidth, _height, s_resourceColor);
  41. }
  42. void showExampleDialog(entry::AppI* _app, const char* _errorText)
  43. {
  44. char temp[1024];
  45. bx::snprintf(temp, BX_COUNTOF(temp), "Example: %s", _app->getName() );
  46. ImGui::SetNextWindowPos(
  47. ImVec2(10.0f, 50.0f)
  48. , ImGuiCond_FirstUseEver
  49. );
  50. ImGui::SetNextWindowSize(
  51. ImVec2(256.0f, 200.0f)
  52. , ImGuiCond_FirstUseEver
  53. );
  54. ImGui::Begin(temp);
  55. ImGui::TextWrapped("%s", _app->getDescription() );
  56. ImGui::Separator();
  57. if (NULL != _errorText)
  58. {
  59. const int64_t now = bx::getHPCounter();
  60. const int64_t freq = bx::getHPFrequency();
  61. const float time = float(now%freq)/float(freq);
  62. bool blink = time > 0.5f;
  63. ImGui::PushStyleColor(ImGuiCol_Text
  64. , blink
  65. ? ImVec4(1.0, 0.0, 0.0, 1.0)
  66. : ImVec4(1.0, 1.0, 1.0, 1.0)
  67. );
  68. ImGui::TextWrapped("%s", _errorText);
  69. ImGui::Separator();
  70. ImGui::PopStyleColor();
  71. }
  72. {
  73. uint32_t num = entry::getNumApps();
  74. const char** items = (const char**)alloca(num*sizeof(void*) );
  75. uint32_t ii = 0;
  76. int32_t current = 0;
  77. for (entry::AppI* app = entry::getFirstApp(); NULL != app; app = app->getNext() )
  78. {
  79. if (app == _app)
  80. {
  81. current = ii;
  82. }
  83. items[ii++] = app->getName();
  84. }
  85. if (1 < num
  86. && ImGui::Combo("Example", &current, items, num) )
  87. {
  88. char command[1024];
  89. bx::snprintf(command, BX_COUNTOF(command), "app restart %s", items[current]);
  90. cmdExec(command);
  91. }
  92. const bgfx::Caps* caps = bgfx::getCaps();
  93. if (0 != (caps->supported & BGFX_CAPS_GRAPHICS_DEBUGGER) )
  94. {
  95. ImGui::SameLine();
  96. ImGui::Text(ICON_FA_SNOWFLAKE_O);
  97. }
  98. ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(3.0f, 3.0f) );
  99. if (ImGui::Button(ICON_FA_REPEAT " Restart" ) )
  100. {
  101. cmdExec("app restart");
  102. }
  103. if (1 < entry::getNumApps() )
  104. {
  105. ImGui::SameLine();
  106. if (ImGui::Button(ICON_KI_PREVIOUS " Prev") )
  107. {
  108. cmdExec("app restart prev");
  109. }
  110. ImGui::SameLine();
  111. if (ImGui::Button(ICON_KI_NEXT " Next") )
  112. {
  113. cmdExec("app restart next");
  114. }
  115. }
  116. ImGui::SameLine();
  117. if (ImGui::Button(ICON_KI_EXIT " Exit") )
  118. {
  119. cmdExec("exit");
  120. }
  121. ImGui::PopStyleVar();
  122. }
  123. #if 0
  124. {
  125. bgfx::RendererType::Enum supportedRenderers[bgfx::RendererType::Count];
  126. uint8_t num = bgfx::getSupportedRenderers(BX_COUNTOF(supportedRenderers), supportedRenderers);
  127. const bgfx::Caps* caps = bgfx::getCaps();
  128. const char* items[bgfx::RendererType::Count];
  129. int32_t current = 0;
  130. for (uint8_t ii = 0; ii < num; ++ii)
  131. {
  132. items[ii] = bgfx::getRendererName(supportedRenderers[ii]);
  133. if (supportedRenderers[ii] == caps->rendererType)
  134. {
  135. current = ii;
  136. }
  137. }
  138. if (ImGui::Combo("Renderer", &current, items, num) )
  139. {
  140. cmdExec("app restart");
  141. }
  142. num = caps->numGPUs;
  143. if (0 != num)
  144. {
  145. current = 0;
  146. for (uint8_t ii = 0; ii < num; ++ii)
  147. {
  148. const bgfx::Caps::GPU& gpu = caps->gpu[ii];
  149. items[ii] = gpu.vendorId == BGFX_PCI_ID_AMD ? "AMD"
  150. : gpu.vendorId == BGFX_PCI_ID_INTEL ? "Intel"
  151. : gpu.vendorId == BGFX_PCI_ID_NVIDIA ? "nVidia"
  152. : "Unknown?"
  153. ;
  154. if (caps->vendorId == gpu.vendorId
  155. && caps->deviceId == gpu.deviceId)
  156. {
  157. current = ii;
  158. }
  159. }
  160. if (ImGui::Combo("GPU", &current, items, num) )
  161. {
  162. cmdExec("app restart");
  163. }
  164. }
  165. }
  166. #endif // 0
  167. const bgfx::Stats* stats = bgfx::getStats();
  168. const double toMsCpu = 1000.0/stats->cpuTimerFreq;
  169. const double toMsGpu = 1000.0/stats->gpuTimerFreq;
  170. ImGui::Text("Frame %0.3f"
  171. , double(stats->cpuTimeFrame)*toMsCpu
  172. );
  173. ImGui::Text("Submit CPU %0.3f, GPU %0.3f (L: %d)"
  174. , double(stats->cpuTimeEnd - stats->cpuTimeBegin)*toMsCpu
  175. , double(stats->gpuTimeEnd - stats->gpuTimeBegin)*toMsGpu
  176. , stats->maxGpuLatency
  177. );
  178. if (-INT64_MAX != stats->gpuMemoryUsed)
  179. {
  180. char tmp0[64];
  181. bx::prettify(tmp0, BX_COUNTOF(tmp0), stats->gpuMemoryUsed);
  182. char tmp1[64];
  183. bx::prettify(tmp1, BX_COUNTOF(tmp1), stats->gpuMemoryMax);
  184. ImGui::Text("GPU mem: %s / %s", tmp0, tmp1);
  185. }
  186. if (ImGui::CollapsingHeader("Resources") )
  187. {
  188. const bgfx::Caps* caps = bgfx::getCaps();
  189. const float itemHeight = ImGui::GetTextLineHeightWithSpacing();
  190. ImGui::PushFont(ImGui::Font::Mono);
  191. ImGui::Text("Res: Num / Max");
  192. resourceBar("DIB", stats->numDynamicIndexBuffers,caps->limits.maxDynamicIndexBuffers, 30.0f, itemHeight);
  193. resourceBar("DVB", stats->numDynamicVertexBuffers, caps->limits.maxDynamicVertexBuffers, 30.0f, itemHeight);
  194. resourceBar(" FB", stats->numFrameBuffers, caps->limits.maxFrameBuffers, 30.0f, itemHeight);
  195. resourceBar(" IB", stats->numIndexBuffers, caps->limits.maxIndexBuffers, 30.0f, itemHeight);
  196. resourceBar(" OQ", stats->numOcclusionQueries, caps->limits.maxOcclusionQueries, 30.0f, itemHeight);
  197. resourceBar(" P", stats->numPrograms, caps->limits.maxPrograms, 30.0f, itemHeight);
  198. resourceBar(" S", stats->numShaders, caps->limits.maxShaders, 30.0f, itemHeight);
  199. resourceBar(" T", stats->numTextures, caps->limits.maxTextures, 30.0f, itemHeight);
  200. resourceBar(" U", stats->numUniforms, caps->limits.maxUniforms, 30.0f, itemHeight);
  201. resourceBar(" VB", stats->numVertexBuffers, caps->limits.maxVertexBuffers, 30.0f, itemHeight);
  202. resourceBar(" VD", stats->numVertexDecls, caps->limits.maxVertexDecls, 30.0f, itemHeight);
  203. ImGui::PopFont();
  204. }
  205. if (0 != stats->numViews)
  206. {
  207. if (ImGui::CollapsingHeader(ICON_FA_CLOCK_O " Profiler") )
  208. {
  209. if (ImGui::BeginChild("##view_profiler", ImVec2(0.0f, 0.0f) ) )
  210. {
  211. ImGui::PushFont(ImGui::Font::Mono);
  212. ImVec4 cpuColor(0.5f, 1.0f, 0.5f, 1.0f);
  213. ImVec4 gpuColor(0.5f, 0.5f, 1.0f, 1.0f);
  214. const float itemHeight = ImGui::GetTextLineHeightWithSpacing();
  215. const float itemHeightWithSpacing = ImGui::GetItemsLineHeightWithSpacing();
  216. const double toCpuMs = 1000.0/double(stats->cpuTimerFreq);
  217. const double toGpuMs = 1000.0/double(stats->gpuTimerFreq);
  218. const float scale = 3.0f;
  219. if (ImGui::ListBoxHeader("Encoders", ImVec2(ImGui::GetWindowWidth(), stats->numEncoders*itemHeightWithSpacing) ) )
  220. {
  221. ImGuiListClipper clipper(stats->numEncoders, itemHeight);
  222. while (clipper.Step() )
  223. {
  224. for (int32_t pos = clipper.DisplayStart; pos < clipper.DisplayEnd; ++pos)
  225. {
  226. const bgfx::EncoderStats& encoderStats = stats->encoderStats[pos];
  227. ImGui::Text("%3d", pos);
  228. ImGui::SameLine(64.0f);
  229. const float maxWidth = 30.0f*scale;
  230. const float cpuMs = float( (encoderStats.cpuTimeEnd-encoderStats.cpuTimeBegin)*toCpuMs);
  231. const float cpuWidth = bx::clamp(cpuMs*scale, 1.0f, maxWidth);
  232. if (bar(cpuWidth, maxWidth, itemHeight, cpuColor) )
  233. {
  234. ImGui::SetTooltip("Encoder %d, CPU: %f [ms]"
  235. , pos
  236. , cpuMs
  237. );
  238. }
  239. }
  240. }
  241. ImGui::ListBoxFooter();
  242. }
  243. ImGui::Separator();
  244. if (ImGui::ListBoxHeader("Views", ImVec2(ImGui::GetWindowWidth(), stats->numViews*itemHeightWithSpacing) ) )
  245. {
  246. ImGuiListClipper clipper(stats->numViews, itemHeight);
  247. while (clipper.Step() )
  248. {
  249. for (int32_t pos = clipper.DisplayStart; pos < clipper.DisplayEnd; ++pos)
  250. {
  251. const bgfx::ViewStats& viewStats = stats->viewStats[pos];
  252. ImGui::Text("%3d %3d %s", pos, viewStats.view, viewStats.name);
  253. const float maxWidth = 30.0f*scale;
  254. const float cpuWidth = bx::clamp(float(viewStats.cpuTimeElapsed*toCpuMs)*scale, 1.0f, maxWidth);
  255. const float gpuWidth = bx::clamp(float(viewStats.gpuTimeElapsed*toGpuMs)*scale, 1.0f, maxWidth);
  256. ImGui::SameLine(64.0f);
  257. if (bar(cpuWidth, maxWidth, itemHeight, cpuColor) )
  258. {
  259. ImGui::SetTooltip("View %d \"%s\", CPU: %f [ms]"
  260. , pos
  261. , viewStats.name
  262. , viewStats.cpuTimeElapsed*toCpuMs
  263. );
  264. }
  265. ImGui::SameLine();
  266. if (bar(gpuWidth, maxWidth, itemHeight, gpuColor) )
  267. {
  268. ImGui::SetTooltip("View: %d \"%s\", GPU: %f [ms]"
  269. , pos
  270. , viewStats.name
  271. , viewStats.gpuTimeElapsed*toGpuMs
  272. );
  273. }
  274. }
  275. }
  276. ImGui::ListBoxFooter();
  277. }
  278. ImGui::PopFont();
  279. }
  280. ImGui::EndChild();
  281. }
  282. }
  283. ImGui::End();
  284. }