bgfx/examples/common/example-glue.cpp

484 lines
13 KiB
C++
Raw Normal View History

/*
2021-01-15 02:53:49 +03:00
* Copyright 2011-2021 Branimir Karadzic. All rights reserved.
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
*/
#include "imgui/imgui.h"
#include "entry/entry.h"
2017-06-30 08:23:18 +03:00
#include "entry/cmd.h"
2019-08-15 07:19:03 +03:00
#include "entry/dialog.h"
2017-06-30 08:23:18 +03:00
#include <bx/string.h>
#include <bx/timer.h>
2017-08-14 07:15:39 +03:00
#include <bx/math.h>
struct SampleData
{
2019-01-07 04:15:56 +03:00
static constexpr uint32_t kNumSamples = 100;
SampleData()
{
2019-01-07 04:15:56 +03:00
reset();
}
2019-01-07 04:15:56 +03:00
void reset()
{
m_offset = 0;
bx::memSet(m_values, 0, sizeof(m_values) );
2019-01-07 08:46:21 +03:00
m_min = 0.0f;
m_max = 0.0f;
m_avg = 0.0f;
}
2019-01-07 04:15:56 +03:00
void pushSample(float value)
{
2019-01-07 04:15:56 +03:00
m_values[m_offset] = value;
m_offset = (m_offset+1) % kNumSamples;
float min = bx::kFloatMax;
float max = -bx::kFloatMax;
float avg = 0.0f;
for (uint32_t ii = 0; ii < kNumSamples; ++ii)
{
2019-01-07 04:15:56 +03:00
const float val = m_values[ii];
2019-01-07 08:46:21 +03:00
min = bx::min(min, val);
max = bx::max(max, val);
2019-01-07 04:15:56 +03:00
avg += val;
}
2019-01-07 04:15:56 +03:00
2019-01-07 08:46:21 +03:00
m_min = min;
m_max = max;
2019-01-07 04:15:56 +03:00
m_avg = avg / kNumSamples;
}
int32_t m_offset;
float m_values[kNumSamples];
2019-01-07 08:46:21 +03:00
float m_min;
float m_max;
float m_avg;
};
static SampleData s_frameTime;
2017-11-04 20:32:31 +03:00
static bool bar(float _width, float _maxWidth, float _height, const ImVec4& _color)
{
const ImGuiStyle& style = ImGui::GetStyle();
ImVec4 hoveredColor(
_color.x + _color.x*0.1f
, _color.y + _color.y*0.1f
, _color.z + _color.z*0.1f
, _color.w + _color.w*0.1f
);
ImGui::PushStyleColor(ImGuiCol_Button, _color);
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, hoveredColor);
ImGui::PushStyleColor(ImGuiCol_ButtonActive, _color);
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 0.0f);
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0.0f, style.ItemSpacing.y) );
bool itemHovered = false;
ImGui::Button("", ImVec2(_width, _height) );
itemHovered |= ImGui::IsItemHovered();
ImGui::SameLine();
ImGui::InvisibleButton("", ImVec2(bx::max(1.0f, _maxWidth-_width), _height) );
2017-11-04 20:32:31 +03:00
itemHovered |= ImGui::IsItemHovered();
ImGui::PopStyleVar(2);
ImGui::PopStyleColor(3);
return itemHovered;
}
2017-12-04 08:43:43 +03:00
static const ImVec4 s_resourceColor(0.5f, 0.5f, 0.5f, 1.0f);
2017-12-05 05:57:57 +03:00
static void resourceBar(const char* _name, const char* _tooltip, uint32_t _num, uint32_t _max, float _maxWidth, float _height)
2017-12-04 08:43:43 +03:00
{
2017-12-05 05:57:57 +03:00
bool itemHovered = false;
2017-12-04 08:43:43 +03:00
ImGui::Text("%s: %4d / %4d", _name, _num, _max);
2017-12-05 05:57:57 +03:00
itemHovered |= ImGui::IsItemHovered();
ImGui::SameLine();
const float percentage = float(_num)/float(_max);
itemHovered |= bar(bx::max(1.0f, percentage*_maxWidth), _maxWidth, _height, s_resourceColor);
2017-12-04 08:43:43 +03:00
ImGui::SameLine();
2017-12-05 05:57:57 +03:00
ImGui::Text("%5.2f%%", percentage*100.0f);
if (itemHovered)
{
ImGui::SetTooltip("%s %5.2f%%"
, _tooltip
, percentage*100.0f
);
}
2017-12-04 08:43:43 +03:00
}
2017-12-05 05:57:57 +03:00
static bool s_showStats = false;
void showExampleDialog(entry::AppI* _app, const char* _errorText)
{
char temp[1024];
bx::snprintf(temp, BX_COUNTOF(temp), "Example: %s", _app->getName() );
2017-06-27 08:21:20 +03:00
ImGui::SetNextWindowPos(
ImVec2(10.0f, 50.0f)
2017-12-02 08:04:27 +03:00
, ImGuiCond_FirstUseEver
2017-06-27 08:21:20 +03:00
);
2017-12-02 08:04:27 +03:00
ImGui::SetNextWindowSize(
2019-01-07 04:15:56 +03:00
ImVec2(300.0f, 210.0f)
2017-12-02 08:04:27 +03:00
, ImGuiCond_FirstUseEver
);
2017-12-02 08:04:27 +03:00
ImGui::Begin(temp);
ImGui::TextWrapped("%s", _app->getDescription() );
2019-08-15 07:19:03 +03:00
bx::StringView url = _app->getUrl();
if (!url.isEmpty() )
{
2019-08-15 08:21:29 +03:00
ImGui::SameLine();
if (ImGui::SmallButton(ICON_FA_LINK) )
2019-08-15 07:19:03 +03:00
{
openUrl(url);
}
2019-08-15 08:21:29 +03:00
else if (ImGui::IsItemHovered() )
{
2020-09-02 06:21:58 +03:00
ImGui::SetTooltip("Documentation: %.*s", url.getLength(), url.getPtr() );
2019-08-15 08:21:29 +03:00
}
2019-08-15 07:19:03 +03:00
}
ImGui::Separator();
if (NULL != _errorText)
{
const int64_t now = bx::getHPCounter();
const int64_t freq = bx::getHPFrequency();
const float time = float(now%freq)/float(freq);
bool blink = time > 0.5f;
ImGui::PushStyleColor(ImGuiCol_Text
, blink
? ImVec4(1.0, 0.0, 0.0, 1.0)
: ImVec4(1.0, 1.0, 1.0, 1.0)
);
ImGui::TextWrapped("%s", _errorText);
ImGui::Separator();
ImGui::PopStyleColor();
}
{
uint32_t num = entry::getNumApps();
const char** items = (const char**)alloca(num*sizeof(void*) );
uint32_t ii = 0;
int32_t current = 0;
for (entry::AppI* app = entry::getFirstApp(); NULL != app; app = app->getNext() )
{
if (app == _app)
{
current = ii;
}
items[ii++] = app->getName();
}
if (1 < num
&& ImGui::Combo("Example", &current, items, num) )
{
2017-06-30 08:23:18 +03:00
char command[1024];
bx::snprintf(command, BX_COUNTOF(command), "app restart %s", items[current]);
cmdExec(command);
}
2017-06-30 08:47:16 +03:00
2017-09-18 07:17:24 +03:00
const bgfx::Caps* caps = bgfx::getCaps();
if (0 != (caps->supported & BGFX_CAPS_GRAPHICS_DEBUGGER) )
{
ImGui::SameLine();
ImGui::Text(ICON_FA_SNOWFLAKE_O);
}
2017-07-04 05:17:32 +03:00
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(3.0f, 3.0f) );
2017-07-02 21:41:37 +03:00
if (ImGui::Button(ICON_FA_REPEAT " Restart" ) )
2017-06-30 08:47:16 +03:00
{
cmdExec("app restart");
}
2017-08-17 06:48:26 +03:00
if (1 < entry::getNumApps() )
2017-07-03 06:17:21 +03:00
{
2017-08-17 06:48:26 +03:00
ImGui::SameLine();
if (ImGui::Button(ICON_KI_PREVIOUS " Prev") )
{
cmdExec("app restart prev");
}
2017-07-03 06:17:21 +03:00
2017-08-17 06:48:26 +03:00
ImGui::SameLine();
if (ImGui::Button(ICON_KI_NEXT " Next") )
{
cmdExec("app restart next");
}
2017-06-30 08:47:16 +03:00
}
2017-07-03 06:17:21 +03:00
ImGui::SameLine();
2017-07-02 21:41:37 +03:00
if (ImGui::Button(ICON_KI_EXIT " Exit") )
{
cmdExec("exit");
}
2017-07-04 05:17:32 +03:00
2017-12-05 05:57:57 +03:00
ImGui::SameLine();
s_showStats ^= ImGui::Button(ICON_FA_BAR_CHART);
2017-07-04 05:17:32 +03:00
ImGui::PopStyleVar();
}
#if 0
{
bgfx::RendererType::Enum supportedRenderers[bgfx::RendererType::Count];
uint8_t num = bgfx::getSupportedRenderers(BX_COUNTOF(supportedRenderers), supportedRenderers);
const bgfx::Caps* caps = bgfx::getCaps();
const char* items[bgfx::RendererType::Count];
int32_t current = 0;
for (uint8_t ii = 0; ii < num; ++ii)
{
items[ii] = bgfx::getRendererName(supportedRenderers[ii]);
if (supportedRenderers[ii] == caps->rendererType)
{
current = ii;
}
}
if (ImGui::Combo("Renderer", &current, items, num) )
{
2017-07-04 23:10:27 +03:00
cmdExec("app restart");
}
2017-09-11 07:36:43 +03:00
num = caps->numGPUs;
if (0 != num)
{
current = 0;
for (uint8_t ii = 0; ii < num; ++ii)
{
const bgfx::Caps::GPU& gpu = caps->gpu[ii];
items[ii] = gpu.vendorId == BGFX_PCI_ID_AMD ? "AMD"
: gpu.vendorId == BGFX_PCI_ID_INTEL ? "Intel"
: gpu.vendorId == BGFX_PCI_ID_NVIDIA ? "nVidia"
: "Unknown?"
;
if (caps->vendorId == gpu.vendorId
&& caps->deviceId == gpu.deviceId)
{
current = ii;
}
}
if (ImGui::Combo("GPU", &current, items, num) )
{
cmdExec("app restart");
}
}
}
#endif // 0
const bgfx::Stats* stats = bgfx::getStats();
2017-09-23 08:26:56 +03:00
const double toMsCpu = 1000.0/stats->cpuTimerFreq;
const double toMsGpu = 1000.0/stats->gpuTimerFreq;
2018-01-08 20:54:34 +03:00
const double frameMs = double(stats->cpuTimeFrame)*toMsCpu;
2019-01-07 04:15:56 +03:00
s_frameTime.pushSample(float(frameMs) );
2019-01-07 04:15:56 +03:00
char frameTextOverlay[256];
bx::snprintf(frameTextOverlay, BX_COUNTOF(frameTextOverlay), "%s%.3fms, %s%.3fms\nAvg: %.3fms, %.1f FPS"
, ICON_FA_ARROW_DOWN
2019-01-07 08:46:21 +03:00
, s_frameTime.m_min
, ICON_FA_ARROW_UP
, s_frameTime.m_max
, s_frameTime.m_avg
, 1000.0f/s_frameTime.m_avg
2019-01-07 04:15:56 +03:00
);
2019-01-07 08:46:21 +03:00
ImGui::PushStyleColor(ImGuiCol_PlotHistogram, ImColor(0.0f, 0.5f, 0.15f, 1.0f).Value);
2019-01-07 04:15:56 +03:00
ImGui::PlotHistogram("Frame"
, s_frameTime.m_values
, SampleData::kNumSamples
, s_frameTime.m_offset
, frameTextOverlay
, 0.0f
, 60.0f
2019-01-07 08:46:21 +03:00
, ImVec2(0.0f, 45.0f)
2019-01-07 04:15:56 +03:00
);
ImGui::PopStyleColor();
2017-09-23 07:39:16 +03:00
ImGui::Text("Submit CPU %0.3f, GPU %0.3f (L: %d)"
2017-09-23 08:26:56 +03:00
, double(stats->cpuTimeEnd - stats->cpuTimeBegin)*toMsCpu
, double(stats->gpuTimeEnd - stats->gpuTimeBegin)*toMsGpu
, stats->maxGpuLatency
);
2017-09-27 08:50:29 +03:00
if (-INT64_MAX != stats->gpuMemoryUsed)
{
char tmp0[64];
bx::prettify(tmp0, BX_COUNTOF(tmp0), stats->gpuMemoryUsed);
char tmp1[64];
bx::prettify(tmp1, BX_COUNTOF(tmp1), stats->gpuMemoryMax);
ImGui::Text("GPU mem: %s / %s", tmp0, tmp1);
}
2017-12-05 05:57:57 +03:00
if (s_showStats)
2017-12-04 06:42:06 +03:00
{
2017-12-05 05:57:57 +03:00
ImGui::SetNextWindowSize(
ImVec2(300.0f, 500.0f)
, ImGuiCond_FirstUseEver
);
2017-12-04 06:42:06 +03:00
2017-12-05 05:57:57 +03:00
if (ImGui::Begin(ICON_FA_BAR_CHART " Stats", &s_showStats) )
2017-08-14 07:15:39 +03:00
{
2017-12-05 05:57:57 +03:00
if (ImGui::CollapsingHeader(ICON_FA_PUZZLE_PIECE " Resources") )
2017-08-14 07:15:39 +03:00
{
2017-12-05 05:57:57 +03:00
const bgfx::Caps* caps = bgfx::getCaps();
2017-08-14 07:15:39 +03:00
const float itemHeight = ImGui::GetTextLineHeightWithSpacing();
2017-12-05 05:57:57 +03:00
const float maxWidth = 90.0f;
2017-08-14 07:15:39 +03:00
2017-12-05 05:57:57 +03:00
ImGui::PushFont(ImGui::Font::Mono);
ImGui::Text("Res: Num / Max");
resourceBar("DIB", "Dynamic index buffers", stats->numDynamicIndexBuffers, caps->limits.maxDynamicIndexBuffers, maxWidth, itemHeight);
resourceBar("DVB", "Dynamic vertex buffers", stats->numDynamicVertexBuffers, caps->limits.maxDynamicVertexBuffers, maxWidth, itemHeight);
resourceBar(" FB", "Frame buffers", stats->numFrameBuffers, caps->limits.maxFrameBuffers, maxWidth, itemHeight);
resourceBar(" IB", "Index buffers", stats->numIndexBuffers, caps->limits.maxIndexBuffers, maxWidth, itemHeight);
resourceBar(" OQ", "Occlusion queries", stats->numOcclusionQueries, caps->limits.maxOcclusionQueries, maxWidth, itemHeight);
resourceBar(" P", "Programs", stats->numPrograms, caps->limits.maxPrograms, maxWidth, itemHeight);
resourceBar(" S", "Shaders", stats->numShaders, caps->limits.maxShaders, maxWidth, itemHeight);
resourceBar(" T", "Textures", stats->numTextures, caps->limits.maxTextures, maxWidth, itemHeight);
resourceBar(" U", "Uniforms", stats->numUniforms, caps->limits.maxUniforms, maxWidth, itemHeight);
resourceBar(" VB", "Vertex buffers", stats->numVertexBuffers, caps->limits.maxVertexBuffers, maxWidth, itemHeight);
2019-08-17 20:35:21 +03:00
resourceBar(" VL", "Vertex layouts", stats->numVertexLayouts, caps->limits.maxVertexLayouts, maxWidth, itemHeight);
2017-12-05 05:57:57 +03:00
ImGui::PopFont();
}
2017-11-03 08:06:39 +03:00
2017-12-05 05:57:57 +03:00
if (ImGui::CollapsingHeader(ICON_FA_CLOCK_O " Profiler") )
{
if (0 == stats->numViews)
{
ImGui::Text("Profiler is not enabled.");
}
else
{
if (ImGui::BeginChild("##view_profiler", ImVec2(0.0f, 0.0f) ) )
2017-11-03 08:06:39 +03:00
{
2017-12-05 05:57:57 +03:00
ImGui::PushFont(ImGui::Font::Mono);
2017-11-03 08:06:39 +03:00
2017-12-05 05:57:57 +03:00
ImVec4 cpuColor(0.5f, 1.0f, 0.5f, 1.0f);
ImVec4 gpuColor(0.5f, 0.5f, 1.0f, 1.0f);
2017-11-03 08:06:39 +03:00
2017-12-05 05:57:57 +03:00
const float itemHeight = ImGui::GetTextLineHeightWithSpacing();
2017-12-15 04:51:55 +03:00
const float itemHeightWithSpacing = ImGui::GetFrameHeightWithSpacing();
2017-12-05 05:57:57 +03:00
const double toCpuMs = 1000.0/double(stats->cpuTimerFreq);
const double toGpuMs = 1000.0/double(stats->gpuTimerFreq);
const float scale = 3.0f;
2017-11-03 08:06:39 +03:00
2017-12-05 05:57:57 +03:00
if (ImGui::ListBoxHeader("Encoders", ImVec2(ImGui::GetWindowWidth(), stats->numEncoders*itemHeightWithSpacing) ) )
{
2020-10-09 18:50:40 +03:00
ImGuiListClipper clipper;
clipper.Begin(stats->numEncoders, itemHeight);
2017-12-05 05:57:57 +03:00
while (clipper.Step() )
2017-11-03 08:06:39 +03:00
{
2017-12-05 05:57:57 +03:00
for (int32_t pos = clipper.DisplayStart; pos < clipper.DisplayEnd; ++pos)
{
const bgfx::EncoderStats& encoderStats = stats->encoderStats[pos];
ImGui::Text("%3d", pos);
ImGui::SameLine(64.0f);
const float maxWidth = 30.0f*scale;
const float cpuMs = float( (encoderStats.cpuTimeEnd-encoderStats.cpuTimeBegin)*toCpuMs);
const float cpuWidth = bx::clamp(cpuMs*scale, 1.0f, maxWidth);
if (bar(cpuWidth, maxWidth, itemHeight, cpuColor) )
{
ImGui::SetTooltip("Encoder %d, CPU: %f [ms]"
, pos
, cpuMs
);
}
}
2017-11-03 08:06:39 +03:00
}
2017-12-05 05:57:57 +03:00
ImGui::ListBoxFooter();
}
2017-11-03 08:06:39 +03:00
2017-12-05 05:57:57 +03:00
ImGui::Separator();
2017-08-14 07:15:39 +03:00
2017-12-05 05:57:57 +03:00
if (ImGui::ListBoxHeader("Views", ImVec2(ImGui::GetWindowWidth(), stats->numViews*itemHeightWithSpacing) ) )
2017-08-14 07:15:39 +03:00
{
2020-10-09 18:50:40 +03:00
ImGuiListClipper clipper;
clipper.Begin(stats->numViews, itemHeight);
2017-11-04 20:32:31 +03:00
2017-12-05 05:57:57 +03:00
while (clipper.Step() )
2017-08-14 07:15:39 +03:00
{
2017-12-05 05:57:57 +03:00
for (int32_t pos = clipper.DisplayStart; pos < clipper.DisplayEnd; ++pos)
{
const bgfx::ViewStats& viewStats = stats->viewStats[pos];
ImGui::Text("%3d %3d %s", pos, viewStats.view, viewStats.name);
const float maxWidth = 30.0f*scale;
const float cpuTimeElapsed = float((viewStats.cpuTimeEnd - viewStats.cpuTimeBegin) * toCpuMs);
const float gpuTimeElapsed = float((viewStats.gpuTimeEnd - viewStats.gpuTimeBegin) * toGpuMs);
const float cpuWidth = bx::clamp(cpuTimeElapsed*scale, 1.0f, maxWidth);
const float gpuWidth = bx::clamp(gpuTimeElapsed*scale, 1.0f, maxWidth);
2017-12-05 05:57:57 +03:00
ImGui::SameLine(64.0f);
if (bar(cpuWidth, maxWidth, itemHeight, cpuColor) )
{
ImGui::SetTooltip("View %d \"%s\", CPU: %f [ms]"
, pos
, viewStats.name
, cpuTimeElapsed
2017-12-05 05:57:57 +03:00
);
}
ImGui::SameLine();
if (bar(gpuWidth, maxWidth, itemHeight, gpuColor) )
{
ImGui::SetTooltip("View: %d \"%s\", GPU: %f [ms]"
, pos
, viewStats.name
, gpuTimeElapsed
2017-12-05 05:57:57 +03:00
);
}
}
2017-08-14 07:15:39 +03:00
}
2017-12-05 05:57:57 +03:00
ImGui::ListBoxFooter();
2017-08-14 07:15:39 +03:00
}
2017-12-05 05:57:57 +03:00
ImGui::PopFont();
2017-08-14 07:15:39 +03:00
}
2017-12-05 05:57:57 +03:00
ImGui::EndChild();
2017-08-14 07:15:39 +03:00
}
}
}
ImGui::End();
2017-08-14 07:15:39 +03:00
}
ImGui::End();
}