example-glue.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. }
  106. #endif // 0
  107. const bgfx::Stats* stats = bgfx::getStats();
  108. ImGui::Text("CPU %0.3f"
  109. , double(stats->cpuTimeEnd-stats->cpuTimeBegin)/stats->cpuTimerFreq*1000.0
  110. );
  111. ImGui::Text("GPU %0.3f (latency: %d)"
  112. , double(stats->gpuTimeEnd-stats->gpuTimeBegin)/stats->gpuTimerFreq*1000.0
  113. , stats->maxGpuLatency
  114. );
  115. if (0 != stats->numViews)
  116. {
  117. if (ImGui::CollapsingHeader(ICON_FA_CLOCK_O " Profiler") )
  118. {
  119. if (ImGui::BeginChild("##view_profiler", ImVec2(0.0f, 0.0f) ) )
  120. {
  121. ImGui::PushFont(ImGui::Font::Mono);
  122. ImVec4 cpuColor(0.5f, 1.0f, 0.5f, 1.0f);
  123. ImVec4 gpuColor(0.5f, 0.5f, 1.0f, 1.0f);
  124. const float itemHeight = ImGui::GetTextLineHeightWithSpacing();
  125. if (ImGui::ListBoxHeader("##empty", ImVec2(ImGui::GetWindowWidth(), stats->numViews*itemHeight) ) )
  126. {
  127. ImGuiListClipper clipper(stats->numViews, itemHeight);
  128. const double toCpuMs = 1000.0/stats->cpuTimerFreq;
  129. const double toGpuMs = 1000.0/stats->gpuTimerFreq;
  130. const float scale = 3.0f;
  131. while (clipper.Step() )
  132. {
  133. for (int32_t pos = clipper.DisplayStart; pos < clipper.DisplayEnd; ++pos)
  134. {
  135. const bgfx::ViewStats& viewStats = stats->viewStats[pos];
  136. ImGui::Text("%3d %3d %s", pos, viewStats.view, viewStats.name);
  137. ImGui::SameLine(64.0f);
  138. ImGui::PushStyleColor(ImGuiCol_Button, cpuColor);
  139. ImGui::PushStyleColor(ImGuiCol_ButtonHovered, cpuColor);
  140. ImGui::PushStyleColor(ImGuiCol_ButtonActive, cpuColor);
  141. const float maxWidth = 30.0f*scale;
  142. const float cpuWidth = bx::fclamp(float(viewStats.cpuTimeElapsed*toCpuMs)*scale, 1.0f, maxWidth);
  143. const float gpuWidth = bx::fclamp(float(viewStats.gpuTimeElapsed*toGpuMs)*scale, 1.0f, maxWidth);
  144. ImGui::Button("", ImVec2(cpuWidth, itemHeight) );
  145. if (ImGui::IsItemHovered() )
  146. {
  147. ImGui::SetTooltip("CPU: %f [ms]", viewStats.cpuTimeElapsed*toCpuMs);
  148. }
  149. ImGui::PopStyleColor(3);
  150. ImGui::SameLine();
  151. ImGui::InvisibleButton("", ImVec2(maxWidth-cpuWidth, itemHeight) );
  152. ImGui::SameLine();
  153. ImGui::PushStyleColor(ImGuiCol_Button, gpuColor);
  154. ImGui::PushStyleColor(ImGuiCol_ButtonHovered, gpuColor);
  155. ImGui::PushStyleColor(ImGuiCol_ButtonActive, gpuColor);
  156. ImGui::Button("", ImVec2(gpuWidth, itemHeight) );
  157. if (ImGui::IsItemHovered() )
  158. {
  159. ImGui::SetTooltip("GPU: %f [ms]", viewStats.gpuTimeElapsed*toGpuMs);
  160. }
  161. ImGui::PopStyleColor(3);
  162. }
  163. }
  164. ImGui::ListBoxFooter();
  165. }
  166. ImGui::PopFont();
  167. ImGui::EndChild();
  168. }
  169. }
  170. }
  171. ImGui::End();
  172. }