diff --git a/src/experimental/filament/filament/filament_context.cc b/src/experimental/filament/filament/filament_context.cc index b5f27805..d82f3f1e 100644 --- a/src/experimental/filament/filament/filament_context.cc +++ b/src/experimental/filament/filament/filament_context.cc @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include @@ -310,4 +311,15 @@ void FilamentContext::UploadFont(const uint8_t* pixels, int width, int height, int id) { object_manager_->UploadFont(pixels, width, height, id); } + +double FilamentContext::GetFrameRate() const { + utils::FixedCapacityVector frame_info = + renderer_->getFrameInfoHistory(1); + if (frame_info.empty()) { + return 0; + } + const int64_t ns = frame_info[0].denoisedGpuFrameDuration; + return 1.0e9 / static_cast(ns); +} + } // namespace mujoco diff --git a/src/experimental/filament/filament/filament_context.h b/src/experimental/filament/filament/filament_context.h index f957eaf9..5aa6e28d 100644 --- a/src/experimental/filament/filament/filament_context.h +++ b/src/experimental/filament/filament/filament_context.h @@ -53,6 +53,8 @@ class FilamentContext { void UploadFont(const uint8_t* pixels, int width, int height, int id); + double GetFrameRate() const; + FilamentContext(const FilamentContext&) = delete; FilamentContext& operator=(const FilamentContext&) = delete; diff --git a/src/experimental/filament/render_context_filament.cc b/src/experimental/filament/render_context_filament.cc index 6edce96e..c4ccff82 100644 --- a/src/experimental/filament/render_context_filament.cc +++ b/src/experimental/filament/render_context_filament.cc @@ -146,4 +146,9 @@ void mjr_uploadFont(const unsigned char* pixels, int width, int height, int bpp, g_filament_context->UploadFont(pixels, width, height, id); } +double mjr_getFrameRate(const mjrContext* con) { + CheckFilamentContext(); + return g_filament_context->GetFrameRate(); +} + } // extern "C" diff --git a/src/experimental/filament/render_context_filament.h b/src/experimental/filament/render_context_filament.h index 7f913f5e..6d33b655 100644 --- a/src/experimental/filament/render_context_filament.h +++ b/src/experimental/filament/render_context_filament.h @@ -78,6 +78,8 @@ MJAPI void mjr_setBuffer(int framebuffer, mjrContext* con); MJAPI void mjr_readPixels(unsigned char* rgb, float* depth, mjrRect viewport, const mjrContext* con); +double mjr_getFrameRate(const mjrContext* con); + #if defined(__cplusplus) } // extern "C" #endif diff --git a/src/experimental/platform/renderer.cc b/src/experimental/platform/renderer.cc index 6d0e22bc..9ab2a79d 100644 --- a/src/experimental/platform/renderer.cc +++ b/src/experimental/platform/renderer.cc @@ -57,17 +57,6 @@ void Renderer::Render(const mjModel* model, mjData* data, mjrRect main_viewport = {0, 0, width, height}; mjr_render(main_viewport, data ? &scene_ : nullptr, &render_context_); - - auto now = std::chrono::steady_clock::now(); - auto delta_time = now - last_fps_update_; - const double interval = std::chrono::duration(delta_time).count(); - - ++frames_; - if (interval > 0.2) { // only update FPS stat at most 5 times per second - last_fps_update_ = now; - fps_ = frames_ / interval; - frames_ = 0; - } } void Renderer::SaveScreenshot(const std::string& filename, int width, diff --git a/src/experimental/platform/renderer.h b/src/experimental/platform/renderer.h index ca1b0d0d..8f2e5590 100644 --- a/src/experimental/platform/renderer.h +++ b/src/experimental/platform/renderer.h @@ -54,15 +54,10 @@ class Renderer { // Rendering flags. mjtByte* GetRenderFlags() { return scene_.flags; } - // Returns the current, average frame rate. - double GetFrameRate() const { return fps_; } - // Returns the render context. const mjrContext& GetContext() const { return render_context_; } private: - using TimePoint = std::chrono::time_point; - // Resets the renderer; no rendering will occur until Init() is called again. void Deinit(); @@ -70,10 +65,6 @@ class Renderer { mjrContext render_context_; mjvScene scene_; bool initialized_ = false; - - int frames_ = 0; - TimePoint last_fps_update_; - double fps_ = 0; }; } // namespace mujoco::platform diff --git a/src/experimental/studio/app.cc b/src/experimental/studio/app.cc index cd7811b4..a78fde75 100644 --- a/src/experimental/studio/app.cc +++ b/src/experimental/studio/app.cc @@ -16,6 +16,9 @@ #include #include +#if defined(USE_CLASSIC_OPENGL) +#include +#endif #include #include #include @@ -369,6 +372,21 @@ void App::Render() { data_->timer[i].number = 0; } } + +#ifdef USE_CLASSIC_OPENGL + TimePoint now = std::chrono::steady_clock::now(); + TimePoint::duration delta_time = now - last_fps_update_; + const double interval = std::chrono::duration(delta_time).count(); + + ++frames_; + if (interval > 0.2) { // only update FPS stat at most 5 times per second + last_fps_update_ = now; + fps_ = frames_ / interval; + frames_ = 0; + } +#else + fps_ = mjr_getFrameRate(&renderer_->GetContext()); +#endif } void App::HandleMouseEvents() { @@ -797,8 +815,7 @@ void App::BuildGui() { platform::ScopedStyle style; style.Var(ImGuiStyleVar_Alpha, 0.6f); if (ImGui::Begin("Info", &tmp_.info)) { - platform::InfoGui(model_, data_, step_control_.IsPaused(), - renderer_->GetFrameRate()); + platform::InfoGui(model_, data_, step_control_.IsPaused(), fps_); } ImGui::End(); } diff --git a/src/experimental/studio/app.h b/src/experimental/studio/app.h index 764dbbc8..ca6e2ac5 100644 --- a/src/experimental/studio/app.h +++ b/src/experimental/studio/app.h @@ -182,6 +182,10 @@ class App { UiState ui_; UiTempState tmp_; + + int frames_ = 0; + TimePoint last_fps_update_; + double fps_ = 0; }; } // namespace mujoco::studio