Skip to content

Commit

Permalink
clear color and cpu usage reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
yekm committed Apr 4, 2023
1 parent 6cf0fce commit 3f21749
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 11 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
5 changes: 3 additions & 2 deletions art.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -45,5 +46,5 @@ class Art {
//uint8_t *data() { return reinterpret_cast<uint8_t*>(pixels.data()); }
uint32_t *data() { return pixels.data(); }
std::vector<uint32_t> pixels;
std::string name;
std::string m_name;
};
2 changes: 1 addition & 1 deletion cloudlife.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
40 changes: 40 additions & 0 deletions imgui_elements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <sys/resource.h>
#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);
}
3 changes: 3 additions & 0 deletions imgui_elements.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

21 changes: 14 additions & 7 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@


#include "imgui.h"
#include "imgui_elements.h"

#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.h"
Expand All @@ -19,11 +20,12 @@
#endif
#include <GLFW/glfw3.h> // Will drag system OpenGL headers



#include "cloudlife.hpp"
#include "mtron.hpp"




std::unique_ptr<Art> art;


Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -187,15 +191,18 @@ 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();

ImGui::Text("Application average %.3f ms/frame (%.1f FPS)",
1000.0f / ImGui::GetIO().Framerate,
ImGui::GetIO().Framerate);

cpu_load_text();

ImGui::End();

if (get_window_size(0,0)) {
Expand Down Expand Up @@ -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);
Expand Down
57 changes: 57 additions & 0 deletions timer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include "timer.h"
#include <time.h>

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
30 changes: 30 additions & 0 deletions timer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef TIMER_H
#define TIMER_H

#include <time.h>

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

0 comments on commit 3f21749

Please sign in to comment.