From 3f21749e5700cbf6cc50f862eb19d08c9f0b7cf6 Mon Sep 17 00:00:00 2001 From: Pavel V Date: Tue, 4 Apr 2023 14:30:52 +0300 Subject: [PATCH] clear color and cpu usage reporting --- CMakeLists.txt | 3 ++- art.hpp | 5 ++-- cloudlife.hpp | 2 +- imgui_elements.cpp | 40 ++++++++++++++++++++++++++++++++ imgui_elements.h | 3 +++ main.cpp | 21 +++++++++++------ timer.cpp | 57 ++++++++++++++++++++++++++++++++++++++++++++++ timer.h | 30 ++++++++++++++++++++++++ 8 files changed, 150 insertions(+), 11 deletions(-) create mode 100644 timer.cpp create mode 100644 timer.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 015ce44..88e252b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -50,6 +50,7 @@ target_sources(cloudlife PUBLIC imgui_elements.cpp random.c cloudlife.cpp - mtron.cpp) + mtron.cpp + timer.cpp) target_link_libraries(cloudlife IMGUI glfw GL colormap) set_target_properties(cloudlife PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) diff --git a/art.hpp b/art.hpp index 25079fa..c8a1e8f 100644 --- a/art.hpp +++ b/art.hpp @@ -7,7 +7,8 @@ class Art { public: Art(std::string _name) - : name(_name) {} + : m_name(_name) {} + const char * name() {return m_name.c_str();} virtual bool render_gui() = 0; virtual void resize(int _w, int _h) {}; virtual void render(uint32_t *p) = 0; @@ -45,5 +46,5 @@ class Art { //uint8_t *data() { return reinterpret_cast(pixels.data()); } uint32_t *data() { return pixels.data(); } std::vector pixels; - std::string name; + std::string m_name; }; diff --git a/cloudlife.hpp b/cloudlife.hpp index 85fa9bd..9eb8ee8 100644 --- a/cloudlife.hpp +++ b/cloudlife.hpp @@ -20,7 +20,7 @@ struct field { class Cloudlife : public Art { public: Cloudlife() - : Art("Cloudlife") + : Art("Cloudlife from xscreensaver") , f(new field) {} virtual bool render_gui() override; virtual void resize(int _w, int _h) override; diff --git a/imgui_elements.cpp b/imgui_elements.cpp index 650ecef..be074ec 100644 --- a/imgui_elements.cpp +++ b/imgui_elements.cpp @@ -140,3 +140,43 @@ bool ScrollableSliderInt(const char* label, int* v, int v_min, int v_max, const bool ScrollableSliderUInt(const char* label, unsigned* v, unsigned v_min, unsigned v_max, const char* format, float scrollFactor) { return ScrollableSliderInt(label, (int*)v, (int)v_min, (int)v_max, format, scrollFactor); } + + +#include +#include "timer.h" + +void cpu_load_text() +{ + static int c = 0; + static double up = 0, sp = 0; + static common::Timer old_t; + static struct rusage old_usage; + static double outt = 0, ostt = 0; + static double ru_maxrss = 0; + + if (c % 120 == 0) { + common::Timer t; + + struct rusage usage; + getrusage(RUSAGE_SELF, &usage); + double utt = usage.ru_utime.tv_sec + usage.ru_utime.tv_usec / 1e6; + double stt = usage.ru_stime.tv_sec + usage.ru_stime.tv_usec / 1e6; + + double dt = (t - old_t).seconds(); + up = 100.0 * (utt - outt) / dt; + sp = 100.0 * (stt - ostt) / dt; + + printf("%.3f %.3f %.3f %.3f %.3f %.3f %.3f\n", dt, + up, sp, utt, stt, outt, ostt); + + old_t = t; + outt = utt; + ostt = stt; + + ru_maxrss = usage.ru_maxrss / 1024; + } + c++; + + ImGui::Text("Usr + Sys = %.2f + %.2f = %.2f", up, sp, up+sp); + ImGui::Text("maxrss %.2f MB", ru_maxrss); +} diff --git a/imgui_elements.h b/imgui_elements.h index adf0732..8bf9e2d 100644 --- a/imgui_elements.h +++ b/imgui_elements.h @@ -3,3 +3,6 @@ bool BitField(const char* label, unsigned* bits, unsigned* hoverIndex); bool ScrollableSliderFloat(const char* label, float* v, float v_min, float v_max, const char* format, float scrollFactor); bool ScrollableSliderInt(const char* label, int* v, int v_min, int v_max, const char* format, float scrollFactor); bool ScrollableSliderUInt(const char* label, unsigned* v, unsigned v_min, unsigned v_max, const char* format, float scrollFactor); + +void cpu_load_text(); + diff --git a/main.cpp b/main.cpp index 371c66a..055be37 100644 --- a/main.cpp +++ b/main.cpp @@ -9,6 +9,7 @@ #include "imgui.h" +#include "imgui_elements.h" #include "imgui_impl_glfw.h" #include "imgui_impl_opengl3.h" @@ -19,11 +20,12 @@ #endif #include // Will drag system OpenGL headers - - #include "cloudlife.hpp" #include "mtron.hpp" + + + std::unique_ptr art; @@ -62,6 +64,8 @@ GLuint image_texture; GLuint pboIds[2]; int pbo_index = 0; +ImVec4 clear_color = ImVec4(0, 0, 0, 1.00f); + void make_pbos() { if (!art->override_texture_size(tex_w, tex_h)) tex_w = sw, tex_h = sh; @@ -158,8 +162,8 @@ int main(int argc, char *argv[]) glfwMakeContextCurrent(window); glfwSwapInterval(vsync); - art.reset(new Cloudlife); - //art.reset(new Minskytron); + //art.reset(new Cloudlife); + art.reset(new Minskytron); get_window_size(0,0); @@ -187,8 +191,9 @@ int main(int argc, char *argv[]) ImGui::NewFrame(); + ImGui::Begin(art->name()); - ImGui::Begin("Cloudlife from xscreensaver"); + ImGui::ColorEdit4("Clear color", (float*)&clear_color); art->render_gui(); @@ -196,6 +201,8 @@ int main(int argc, char *argv[]) 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate); + cpu_load_text(); + ImGui::End(); if (get_window_size(0,0)) { @@ -233,8 +240,8 @@ int main(int argc, char *argv[]) //glViewport(0, 0, sw, sh); glViewport(0, 0, display_w, display_h); - //glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w); - //glClear(GL_COLOR_BUFFER_BIT); + glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w); + glClear(GL_COLOR_BUFFER_BIT); ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); glfwSwapBuffers(window); diff --git a/timer.cpp b/timer.cpp new file mode 100644 index 0000000..37c0093 --- /dev/null +++ b/timer.cpp @@ -0,0 +1,57 @@ +#include "timer.h" +#include + +namespace common +{ + +Timer::Timer() +{ + restart(); +} + +Timer Timer::restart() +{ + Timer tmp = *this; + clock_gettime( CLOCK_MONOTONIC_RAW, this ); + return tmp; +} + + +Timer Timer::operator-(const Timer & a) +{ + Timer t(*this); + return t -= a; +} + +Timer & Timer::operator-=(const Timer & a) +{ + tv_sec -= a.tv_sec; + tv_nsec -= a.tv_nsec; + if(tv_nsec < 0) + { + --tv_sec; + tv_nsec += 1e9; + } + return *this; +} + +Timer Timer::operator+(const Timer & a) +{ + Timer t(*this); + return t += a; +} + +Timer & Timer::operator+=(const Timer & a) +{ + tv_sec += a.tv_sec; + tv_nsec += a.tv_nsec; + if(tv_nsec < 1e9) + { + ++tv_sec; + tv_nsec -= 1e9; + } + return *this; +} + + +} // namespace common diff --git a/timer.h b/timer.h new file mode 100644 index 0000000..3bb64f9 --- /dev/null +++ b/timer.h @@ -0,0 +1,30 @@ +#ifndef TIMER_H +#define TIMER_H + +#include + +namespace common +{ + +class Timer : public timespec +{ +public: + Timer(); + Timer restart(); + + Timer operator-(const Timer & a); + Timer & operator-=(const Timer & a); + Timer operator+(const Timer & a); + Timer & operator+=(const Timer & a); + + double seconds() { + return tv_sec + (double)tv_nsec/1e9; + }; + long unsigned us() { + return tv_sec*1e9 + tv_nsec; + }; +}; + +} // namespace common + +#endif // TIMER_H