example-glue.cpp 9.0 KB

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