example-glue.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. void showExampleDialog(entry::AppI* _app, const char* _errorText)
  12. {
  13. char temp[1024];
  14. bx::snprintf(temp, BX_COUNTOF(temp), "Example: %s", _app->getName() );
  15. ImGui::SetNextWindowPos(
  16. ImVec2(10.0f, 50.0f)
  17. , ImGuiSetCond_FirstUseEver
  18. );
  19. ImGui::Begin(temp
  20. , NULL
  21. , ImVec2(256.0f, 200.0f)
  22. , ImGuiWindowFlags_AlwaysAutoResize
  23. );
  24. ImGui::TextWrapped("%s", _app->getDescription() );
  25. ImGui::Separator();
  26. if (NULL != _errorText)
  27. {
  28. const int64_t now = bx::getHPCounter();
  29. const int64_t freq = bx::getHPFrequency();
  30. const float time = float(now%freq)/float(freq);
  31. bool blink = time > 0.5f;
  32. ImGui::PushStyleColor(ImGuiCol_Text
  33. , blink
  34. ? ImVec4(1.0, 0.0, 0.0, 1.0)
  35. : ImVec4(1.0, 1.0, 1.0, 1.0)
  36. );
  37. ImGui::TextWrapped("%s", _errorText);
  38. ImGui::Separator();
  39. ImGui::PopStyleColor();
  40. }
  41. {
  42. uint32_t num = entry::getNumApps();
  43. const char** items = (const char**)alloca(num*sizeof(void*) );
  44. uint32_t ii = 0;
  45. int32_t current = 0;
  46. for (entry::AppI* app = entry::getFirstApp(); NULL != app; app = app->getNext() )
  47. {
  48. if (app == _app)
  49. {
  50. current = ii;
  51. }
  52. items[ii++] = app->getName();
  53. }
  54. if (1 < num
  55. && ImGui::Combo("Example", &current, items, num) )
  56. {
  57. char command[1024];
  58. bx::snprintf(command, BX_COUNTOF(command), "app restart %s", items[current]);
  59. cmdExec(command);
  60. }
  61. ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(3.0f, 3.0f) );
  62. if (ImGui::Button(ICON_FA_REPEAT " Restart" ) )
  63. {
  64. cmdExec("app restart");
  65. }
  66. if (1 < entry::getNumApps() )
  67. {
  68. ImGui::SameLine();
  69. if (ImGui::Button(ICON_KI_PREVIOUS " Prev") )
  70. {
  71. cmdExec("app restart prev");
  72. }
  73. ImGui::SameLine();
  74. if (ImGui::Button(ICON_KI_NEXT " Next") )
  75. {
  76. cmdExec("app restart next");
  77. }
  78. }
  79. ImGui::SameLine();
  80. if (ImGui::Button(ICON_KI_EXIT " Exit") )
  81. {
  82. cmdExec("exit");
  83. }
  84. ImGui::PopStyleVar();
  85. }
  86. #if 0
  87. {
  88. bgfx::RendererType::Enum supportedRenderers[bgfx::RendererType::Count];
  89. uint8_t num = bgfx::getSupportedRenderers(BX_COUNTOF(supportedRenderers), supportedRenderers);
  90. const bgfx::Caps* caps = bgfx::getCaps();
  91. const char* items[bgfx::RendererType::Count];
  92. int32_t current = 0;
  93. for (uint8_t ii = 0; ii < num; ++ii)
  94. {
  95. items[ii] = bgfx::getRendererName(supportedRenderers[ii]);
  96. if (supportedRenderers[ii] == caps->rendererType)
  97. {
  98. current = ii;
  99. }
  100. }
  101. if (ImGui::Combo("Renderer", &current, items, num) )
  102. {
  103. cmdExec("app restart");
  104. }
  105. num = caps->numGPUs;
  106. if (0 != num)
  107. {
  108. current = 0;
  109. for (uint8_t ii = 0; ii < num; ++ii)
  110. {
  111. const bgfx::Caps::GPU& gpu = caps->gpu[ii];
  112. items[ii] = gpu.vendorId == BGFX_PCI_ID_AMD ? "AMD"
  113. : gpu.vendorId == BGFX_PCI_ID_INTEL ? "Intel"
  114. : gpu.vendorId == BGFX_PCI_ID_NVIDIA ? "nVidia"
  115. : "Unknown?"
  116. ;
  117. if (caps->vendorId == gpu.vendorId
  118. && caps->deviceId == gpu.deviceId)
  119. {
  120. current = ii;
  121. }
  122. }
  123. if (ImGui::Combo("GPU", &current, items, num) )
  124. {
  125. cmdExec("app restart");
  126. }
  127. }
  128. }
  129. #endif // 0
  130. const bgfx::Stats* stats = bgfx::getStats();
  131. ImGui::Text("CPU %0.3f"
  132. , double(stats->cpuTimeEnd-stats->cpuTimeBegin)/stats->cpuTimerFreq*1000.0
  133. );
  134. ImGui::Text("GPU %0.3f (latency: %d)"
  135. , double(stats->gpuTimeEnd-stats->gpuTimeBegin)/stats->gpuTimerFreq*1000.0
  136. , stats->maxGpuLatency
  137. );
  138. if (0 != stats->numViews)
  139. {
  140. if (ImGui::CollapsingHeader(ICON_FA_CLOCK_O " Profiler") )
  141. {
  142. if (ImGui::BeginChild("##view_profiler", ImVec2(0.0f, 0.0f) ) )
  143. {
  144. ImGui::PushFont(ImGui::Font::Mono);
  145. ImVec4 cpuColor(0.5f, 1.0f, 0.5f, 1.0f);
  146. ImVec4 gpuColor(0.5f, 0.5f, 1.0f, 1.0f);
  147. const float itemHeight = ImGui::GetTextLineHeightWithSpacing();
  148. if (ImGui::ListBoxHeader("##empty", ImVec2(ImGui::GetWindowWidth(), stats->numViews*itemHeight) ) )
  149. {
  150. ImGuiListClipper clipper(stats->numViews, itemHeight);
  151. const double toCpuMs = 1000.0/stats->cpuTimerFreq;
  152. const double toGpuMs = 1000.0/stats->gpuTimerFreq;
  153. const float scale = 3.0f;
  154. while (clipper.Step() )
  155. {
  156. for (int32_t pos = clipper.DisplayStart; pos < clipper.DisplayEnd; ++pos)
  157. {
  158. const bgfx::ViewStats& viewStats = stats->viewStats[pos];
  159. ImGui::Text("%3d %3d %s", pos, viewStats.view, viewStats.name);
  160. ImGui::SameLine(64.0f);
  161. ImGui::PushStyleColor(ImGuiCol_Button, cpuColor);
  162. ImGui::PushStyleColor(ImGuiCol_ButtonHovered, cpuColor);
  163. ImGui::PushStyleColor(ImGuiCol_ButtonActive, cpuColor);
  164. const float maxWidth = 30.0f*scale;
  165. const float cpuWidth = bx::fclamp(float(viewStats.cpuTimeElapsed*toCpuMs)*scale, 1.0f, maxWidth);
  166. const float gpuWidth = bx::fclamp(float(viewStats.gpuTimeElapsed*toGpuMs)*scale, 1.0f, maxWidth);
  167. ImGui::Button("", ImVec2(cpuWidth, itemHeight) );
  168. if (ImGui::IsItemHovered() )
  169. {
  170. ImGui::SetTooltip("CPU: %f [ms]", viewStats.cpuTimeElapsed*toCpuMs);
  171. }
  172. ImGui::PopStyleColor(3);
  173. ImGui::SameLine();
  174. ImGui::InvisibleButton("", ImVec2(maxWidth-cpuWidth, itemHeight) );
  175. ImGui::SameLine();
  176. ImGui::PushStyleColor(ImGuiCol_Button, gpuColor);
  177. ImGui::PushStyleColor(ImGuiCol_ButtonHovered, gpuColor);
  178. ImGui::PushStyleColor(ImGuiCol_ButtonActive, gpuColor);
  179. ImGui::Button("", ImVec2(gpuWidth, itemHeight) );
  180. if (ImGui::IsItemHovered() )
  181. {
  182. ImGui::SetTooltip("GPU: %f [ms]", viewStats.gpuTimeElapsed*toGpuMs);
  183. }
  184. ImGui::PopStyleColor(3);
  185. }
  186. }
  187. ImGui::ListBoxFooter();
  188. }
  189. ImGui::PopFont();
  190. }
  191. ImGui::EndChild();
  192. }
  193. }
  194. ImGui::End();
  195. }