2022-10-20 11:30:15 -04:00
|
|
|
/*
|
|
|
|
* Created by Brett Terpstra 6920201 on 18/10/22.
|
|
|
|
* Copyright (c) 2022 Brett Terpstra. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* this is a direct one-to-one copy of the profiler class used in my Game Engine
|
|
|
|
* it functions very well especially when used in a GUI context,
|
|
|
|
* so why reinvent the wheel right?
|
|
|
|
* So to avoid any kind of self plagiarism, I fully credit the source which is here:
|
|
|
|
* https://github.com/Tri11Paragon/Trapdoor-Engine/tree/dev/C%2B%2B%20Engine
|
2022-12-13 01:33:31 -05:00
|
|
|
*
|
|
|
|
* 2022-12-12 update:
|
|
|
|
* the above is not 100% true anymore. The functions are very similar though.
|
2022-10-20 11:30:15 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef STEP_2_DEBUG_H
|
|
|
|
#define STEP_2_DEBUG_H
|
|
|
|
|
2022-10-23 23:46:12 -04:00
|
|
|
#include "std.h"
|
2022-10-31 00:51:51 -04:00
|
|
|
#include <config.h>
|
2022-11-16 17:34:17 -05:00
|
|
|
#include <mutex>
|
2022-12-13 01:33:31 -05:00
|
|
|
|
2022-10-31 00:51:51 -04:00
|
|
|
#ifdef COMPILE_GUI
|
2022-12-13 01:33:31 -05:00
|
|
|
|
2022-10-31 00:51:51 -04:00
|
|
|
#include <graphics/debug_gui.h>
|
2022-12-13 01:33:31 -05:00
|
|
|
|
2022-10-31 00:51:51 -04:00
|
|
|
#endif
|
2022-10-20 11:30:15 -04:00
|
|
|
|
|
|
|
namespace Raytracing {
|
|
|
|
class profiler;
|
2022-12-13 01:33:31 -05:00
|
|
|
|
2022-11-13 13:10:58 -05:00
|
|
|
extern std::unordered_map<std::string, std::shared_ptr<profiler>> profiles;
|
2022-10-20 11:30:15 -04:00
|
|
|
|
2022-12-13 01:33:31 -05:00
|
|
|
class DebugTab {
|
2022-10-20 11:30:15 -04:00
|
|
|
protected:
|
|
|
|
std::string name;
|
|
|
|
public:
|
|
|
|
virtual void render() {}
|
2022-12-13 01:33:31 -05:00
|
|
|
|
2022-10-20 11:30:15 -04:00
|
|
|
std::string getName() {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class profiler : public DebugTab {
|
|
|
|
private:
|
|
|
|
long _start = 0;
|
|
|
|
long _end = 0;
|
|
|
|
std::unordered_map<std::string, std::pair<long, long>> timings;
|
2022-12-13 01:33:31 -05:00
|
|
|
std::mutex timerLock{};
|
2022-10-20 11:30:15 -04:00
|
|
|
public:
|
|
|
|
explicit profiler(std::string name);
|
|
|
|
|
|
|
|
void start();
|
2022-12-13 01:33:31 -05:00
|
|
|
|
2022-10-20 11:30:15 -04:00
|
|
|
void start(const std::string& name);
|
2022-12-13 01:33:31 -05:00
|
|
|
|
2022-10-20 11:30:15 -04:00
|
|
|
static void start(const std::string& name, const std::string& tabName) {
|
2022-11-16 17:34:17 -05:00
|
|
|
static std::mutex staticLock{};
|
2022-12-13 01:33:31 -05:00
|
|
|
// since we are using threads here the change is the use of this mutex.
|
2022-11-16 17:34:17 -05:00
|
|
|
std::scoped_lock lock(staticLock);
|
2022-10-25 01:06:26 -04:00
|
|
|
if (profiles.contains(name)) {
|
|
|
|
auto p = profiles.at(name);
|
|
|
|
p->start(tabName);
|
|
|
|
} else {
|
2022-11-13 13:10:58 -05:00
|
|
|
auto p = std::make_shared<profiler>(name);
|
2022-10-25 01:06:26 -04:00
|
|
|
profiles.insert(std::pair(name, p));
|
|
|
|
p->start(tabName);
|
|
|
|
}
|
2022-10-20 11:30:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void end();
|
2022-12-13 01:33:31 -05:00
|
|
|
|
2022-10-20 11:30:15 -04:00
|
|
|
void end(const std::string& name);
|
2022-12-13 01:33:31 -05:00
|
|
|
|
|
|
|
static void end(const std::string& name, const std::string& tabName) {
|
2022-11-16 17:34:17 -05:00
|
|
|
static std::mutex staticLock{};
|
|
|
|
std::scoped_lock lock(staticLock);
|
2022-10-20 11:30:15 -04:00
|
|
|
try {
|
|
|
|
profiles.at(name)->end(tabName);
|
2022-12-13 01:33:31 -05:00
|
|
|
} catch (std::exception& e) {}
|
2022-10-20 11:30:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void print();
|
2022-12-13 01:33:31 -05:00
|
|
|
|
|
|
|
static void print(const std::string& name) {
|
2022-11-16 17:34:17 -05:00
|
|
|
static std::mutex staticLock{};
|
|
|
|
std::scoped_lock lock(staticLock);
|
2022-10-20 11:30:15 -04:00
|
|
|
try {
|
|
|
|
profiles.at(name)->print();
|
2022-12-13 01:33:31 -05:00
|
|
|
} catch (std::exception& e) {}
|
2022-10-20 11:30:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void endAndPrint();
|
2022-12-13 01:33:31 -05:00
|
|
|
|
|
|
|
static void endAndPrint(const std::string& name, const std::string& tabName) {
|
2022-10-20 11:30:15 -04:00
|
|
|
profiler::end(name, tabName);
|
|
|
|
profiler::print(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
void render();
|
2022-12-13 01:33:31 -05:00
|
|
|
|
2022-10-20 11:30:15 -04:00
|
|
|
static void render(int count) {
|
2022-11-13 13:10:58 -05:00
|
|
|
for (const auto& p : profiles)
|
2022-10-20 11:30:15 -04:00
|
|
|
p.second->render();
|
|
|
|
}
|
|
|
|
|
2022-11-13 13:10:58 -05:00
|
|
|
~profiler() = default;
|
2022-10-20 11:30:15 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif //STEP_2_DEBUG_H
|