Report the actual GPU framerate rather than the main thread submit rate when using the filament renderer.

PiperOrigin-RevId: 838775463
Change-Id: If66644d284a4a9e0263cf31bfbd1785b18e0209b
This commit is contained in:
Haroon Qureshi
2025-12-01 08:01:33 -08:00
committed by Copybara-Service
parent 4896e698f6
commit 448fc0e4d5
8 changed files with 44 additions and 22 deletions
@@ -33,6 +33,7 @@
#include <filament/Texture.h>
#include <filament/View.h>
#include <math/vec4.h>
#include <utils/FixedCapacityVector.h>
#include <mujoco/mjmodel.h>
#include <mujoco/mjvisualize.h>
#include <mujoco/mujoco.h>
@@ -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<filament::Renderer::FrameInfo> frame_info =
renderer_->getFrameInfoHistory(1);
if (frame_info.empty()) {
return 0;
}
const int64_t ns = frame_info[0].denoisedGpuFrameDuration;
return 1.0e9 / static_cast<double>(ns);
}
} // namespace mujoco
@@ -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;
@@ -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"
@@ -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
-11
View File
@@ -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<double>(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,
-9
View File
@@ -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<std::chrono::steady_clock>;
// 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
+19 -2
View File
@@ -16,6 +16,9 @@
#include <algorithm>
#include <array>
#if defined(USE_CLASSIC_OPENGL)
#include <chrono>
#endif
#include <cstddef>
#include <cstdint>
#include <cstdio>
@@ -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<double>(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();
}
+4
View File
@@ -182,6 +182,10 @@ class App {
UiState ui_;
UiTempState tmp_;
int frames_ = 0;
TimePoint last_fps_update_;
double fps_ = 0;
};
} // namespace mujoco::studio