SDEngine
Game Engine
Loading...
Searching...
No Matches
PerformanceLayer.hpp
Go to the documentation of this file.
1#pragma once
2#include <chrono>
3
4#if defined(__x86_64__) || defined(_M_X64)
5#include <x86intrin.h>
6#else
7#error "PerformanceLayer requires x86_64 (__rdtsc). Consider using std::chrono instead."
8#endif
9
10#include "SD/core/Layer.hpp"
11#include "SD/core/logging.hpp"
12
13namespace sd {
15 void on_update(float dt) {
16 if (!m_last_cycles)
18
21
23 if (m_last_cycles != 0) {
25 }
27
28 if (m_time_accumulator >= 1.0f) {
29 double fps = m_frame_count / static_cast<double>(m_time_accumulator);
30 double computeTime = static_cast<double>(m_time_accumulator) - m_sleep_time_accumulator;
31 double msPerFrame = (computeTime * 1000.0) / m_frame_count;
32 double avgCycles =
34
35 log::engine::info("FPS: {:.2f}, Avg ms: {:.2f}, Avg cycles: {:.0f}",
36 fps,
38 avgCycles);
39
40 m_frame_count = 0;
41 m_time_accumulator = 0.0f;
45 }
46 }
47
48 void begin_sleep() {
50 m_sleep_start_time = std::chrono::high_resolution_clock::now();
51 }
52
53 void end_sleep() {
54 auto now = std::chrono::high_resolution_clock::now();
56 m_sleep_time_accumulator += std::chrono::duration<float>(now - m_sleep_start_time).count();
57 }
58
59
61 float m_time_accumulator = 0.0f;
64
68 std::chrono::time_point<std::chrono::high_resolution_clock> m_sleep_start_time;
69};
70} // namespace sd
Definition Application.hpp:22
Definition Layer.hpp:12
Definition PerformanceLayer.hpp:14
float m_time_accumulator
Definition PerformanceLayer.hpp:61
void end_sleep()
Definition PerformanceLayer.hpp:53
void begin_sleep()
Definition PerformanceLayer.hpp:48
float m_sleep_time_accumulator
Definition PerformanceLayer.hpp:65
U32 m_frame_count
Definition PerformanceLayer.hpp:60
U64 m_last_cycles
Definition PerformanceLayer.hpp:63
void on_update(float dt)
Definition PerformanceLayer.hpp:15
std::chrono::time_point< std::chrono::high_resolution_clock > m_sleep_start_time
Definition PerformanceLayer.hpp:68
U64 m_sleep_cycle_accumulator
Definition PerformanceLayer.hpp:66
U64 m_cycle_accumulator
Definition PerformanceLayer.hpp:62
U64 m_sleep_start_cycles
Definition PerformanceLayer.hpp:67
consteval U64 type_id_of()
Definition type_id.hpp:6
std::uint32_t U32
Definition types.hpp:15
std::uint64_t U64
Definition types.hpp:16