timings.cpp

cross platform rendering playground

src/editor/timings.cpp

2.18 KB
#ifdef ENABLE_EDITOR
#define IMGUI_DEFINE_MATH_OPERATORS
#include "editor.h"

#include <imgui.h>

#include "passes/fg_utils.h"
#include "renderer.h"
#include "scene/scene.h"

namespace editor {

void draw_timings_window(
    Scene &scene, RenderData &rd, const PassInspectorEntry *passes, u32 pass_count, Editor &editor, ImFont *mono_font
) {
    if (ImGui::Begin("Timings", nullptr, PANEL_FLAGS)) {
        ImGui::PushFont(mono_font);

        const FrameStats &st = rd.frame_stats;
        f32 gpu_ms = 0.0f;
        for (u32 i = 0; i < pass_count; ++i) {
            if (passes[i].timing_valid) {
                gpu_ms += passes[i].gpu_ms;
            }
        }

        ImGui::Text("FPS: %.1f", st.fps());
        ImGui::Text("CPU: last %.3f | avg %.3f | min %.3f | max %.3f ms", st.last_ms, st.avg_ms, st.min_ms, st.max_ms);
        ImGui::Text("GPU: %.3f ms total", gpu_ms);

        ImGui::Separator();

        if (ImGui::BeginTable("PassInspector", 2, ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg)) {
            ImGui::TableSetupColumn("Pass");
            ImGui::TableSetupColumn("GPU ms", ImGuiTableColumnFlags_WidthFixed, 80.0f);
            ImGui::TableHeadersRow();

            f32 total_gpu_ms = 0.0f;
            for (u32 i = 0; i < pass_count; ++i) {
                const PassInspectorEntry &pass = passes[i];
                ImGui::TableNextRow();
                ImGui::TableSetColumnIndex(0);
                ImGui::TextUnformatted(pass.name ? pass.name : "Unnamed Pass");
                ImGui::TableSetColumnIndex(1);
                if (pass.timing_valid) {
                    ImGui::Text("%.3f", pass.gpu_ms);
                    total_gpu_ms += pass.gpu_ms;
                } else {
                    ImGui::TextUnformatted("pending");
                }
            }

            if (pass_count > 0) {
                ImGui::TableNextRow();
                ImGui::TableSetColumnIndex(0);
                ImGui::TextUnformatted("Total GPU");
                ImGui::TableSetColumnIndex(1);
                ImGui::Text("%.3f", total_gpu_ms);
            }
            ImGui::EndTable();
        }

        ImGui::PopFont();
    }
    ImGui::End();
}

} // namespace editor

#endif // ENABLE_EDITOR