SDEngine
Game Engine
Loading...
Searching...
No Matches
PerformanceLayer.hpp
Go to the documentation of this file.
1
7#pragma once
8#include <chrono>
9
10#if defined(__x86_64__) || defined(_M_X64)
11#include <x86intrin.h>
12#else
13#error "PerformanceLayer requires x86_64 (__rdtsc). Consider using std::chrono instead."
14#endif
15
16#include "SD/core/Layer.hpp"
17#include "SD/core/logging.hpp"
18namespace sd {
19class PerformanceLayer : public Layer {
20public:
21 explicit PerformanceLayer() : Layer("Performance layer") { m_last_cycles = __rdtsc(); }
22
23 void on_update(const float dt) override {
26
28 if (m_last_cycles != 0) {
30 }
32
33 if (m_time_accumulator >= 1.0f) {
34 double fps = m_frame_count / static_cast<double>(m_time_accumulator);
35 double computeTime = static_cast<double>(m_time_accumulator) - m_sleep_time_accumulator;
36 double msPerFrame = (computeTime * 1000.0) / m_frame_count;
37 double avgCycles =
39
40
41 log::engine::info("FPS: {:.2f}, Avg ms: {:.2f}, Avg cycles: {:.0f}", fps, msPerFrame,
42 avgCycles);
43
44 m_frame_count = 0;
45 m_time_accumulator = 0.0f;
49 }
50 }
51
52 void begin_sleep() {
54 m_sleep_start_time = std::chrono::high_resolution_clock::now();
55 }
56
57 void end_sleep() {
58 auto now = std::chrono::high_resolution_clock::now();
60 m_sleep_time_accumulator += std::chrono::duration<float>(now - m_sleep_start_time).count();
61 }
62
63private:
65 float m_time_accumulator = 0.0f;
68
72 std::chrono::time_point<std::chrono::high_resolution_clock> m_sleep_start_time;
73};
74} // namespace sd
Base class for all layers. Prefer using System, RenderStage, or Panel instead.
Definition Layer.hpp:28
Definition PerformanceLayer.hpp:19
u64 m_last_cycles
Definition PerformanceLayer.hpp:67
float m_time_accumulator
Definition PerformanceLayer.hpp:65
void end_sleep()
Definition PerformanceLayer.hpp:57
u32 m_frame_count
Definition PerformanceLayer.hpp:64
u64 m_sleep_cycle_accumulator
Definition PerformanceLayer.hpp:70
void begin_sleep()
Definition PerformanceLayer.hpp:52
float m_sleep_time_accumulator
Definition PerformanceLayer.hpp:69
u64 m_sleep_start_cycles
Definition PerformanceLayer.hpp:71
u64 m_cycle_accumulator
Definition PerformanceLayer.hpp:66
PerformanceLayer()
Definition PerformanceLayer.hpp:21
std::chrono::time_point< std::chrono::high_resolution_clock > m_sleep_start_time
Definition PerformanceLayer.hpp:72
void on_update(const float dt) override
Definition PerformanceLayer.hpp:23
Definition Application.hpp:28
std::uint32_t u32
Definition types.hpp:15
std::uint64_t u64
Definition types.hpp:16
constexpr T g_type_max
Definition types.hpp:21