Finality Engine
Loading...
Searching...
No Matches
BenchmarkScene.h
Go to the documentation of this file.
1#pragma once
2
3#include <Finality.h>
5#include <chrono>
6#include <vector>
7#include <memory>
10
11#include "MainScene.h"
12
15{
16public:
17 void OnInit() override
18 {
20
21 auto cameraEntity = CreateEntity("Benchmark Camera");
22 auto& camComp = cameraEntity.AddComponent<FINALITY::CameraComponent>(
23 45.0f,
24 1280.0f / 720.0f,
25 0.01f,
26 500.0f
27 );
28
29 auto& cameraTransform =
30 cameraEntity.GetComponent<FINALITY::TransformComponent>();
31
32 cameraTransform.Position = glm::vec3(0.0f, 5.0f, 15.0f);
33 cameraTransform.Rotation = glm::vec3(-10.0f, 0.0f, 0.0f);
34
35 FINALITY::Renderer::SetActiveCamera(camComp.CameraInstance.get());
36
37 cameraEntity.AddScript<FINALITY::CameraMovementScript>();
38
39 std::vector<FINALITY::Vertex> cubeVertices = {
40 {{-0.3f, -0.3f, 0.3f}, {1.0f, 1.0f, 1.0f}, {0.0f, 1.0f}},
41 {{ 0.3f, -0.3f, 0.3f}, {1.0f, 1.0f, 1.0f}, {1.0f, 1.0f}},
42 {{ 0.3f, 0.3f, 0.3f}, {1.0f, 1.0f, 1.0f}, {1.0f, 0.0f}},
43 {{-0.3f, 0.3f, 0.3f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}},
44 {{ 0.3f, -0.3f, -0.3f}, {1.0f, 1.0f, 1.0f}, {0.0f, 1.0f}},
45 {{-0.3f, -0.3f, -0.3f}, {1.0f, 1.0f, 1.0f}, {1.0f, 1.0f}},
46 {{-0.3f, 0.3f, -0.3f}, {1.0f, 1.0f, 1.0f}, {1.0f, 0.0f}},
47 {{ 0.3f, 0.3f, -0.3f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}},
48 {{-0.3f, 0.3f, 0.3f}, {1.0f, 1.0f, 1.0f}, {0.0f, 1.0f}},
49 {{ 0.3f, 0.3f, 0.3f}, {1.0f, 1.0f, 1.0f}, {1.0f, 1.0f}},
50 {{ 0.3f, 0.3f, -0.3f}, {1.0f, 1.0f, 1.0f}, {1.0f, 0.0f}},
51 {{-0.3f, 0.3f, -0.3f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}},
52 {{-0.3f, -0.3f, -0.3f}, {1.0f, 1.0f, 1.0f}, {0.0f, 1.0f}},
53 {{ 0.3f, -0.3f, -0.3f}, {1.0f, 1.0f, 1.0f}, {1.0f, 1.0f}},
54 {{ 0.3f, -0.3f, 0.3f}, {1.0f, 1.0f, 1.0f}, {1.0f, 0.0f}},
55 {{-0.3f, -0.3f, 0.3f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}},
56 {{ 0.3f, -0.3f, 0.3f}, {1.0f, 1.0f, 1.0f}, {0.0f, 1.0f}},
57 {{ 0.3f, -0.3f, -0.3f}, {1.0f, 1.0f, 1.0f}, {1.0f, 1.0f}},
58 {{ 0.3f, 0.3f, -0.3f}, {1.0f, 1.0f, 1.0f}, {1.0f, 0.0f}},
59 {{ 0.3f, 0.3f, 0.3f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}},
60 {{-0.3f, -0.3f, -0.3f}, {1.0f, 1.0f, 1.0f}, {0.0f, 1.0f}},
61 {{-0.3f, -0.3f, 0.3f}, {1.0f, 1.0f, 1.0f}, {1.0f, 1.0f}},
62 {{-0.3f, 0.3f, 0.3f}, {1.0f, 1.0f, 1.0f}, {1.0f, 0.0f}},
63 {{-0.3f, 0.3f, -0.3f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}}
64 };
65
66 std::vector<uint32_t> cubeIndices = {
67 0, 1, 2, 2, 3, 0,
68 4, 5, 6, 6, 7, 4,
69 8, 9, 10, 10, 11, 8,
70 12, 13, 14, 14, 15, 12,
71 16, 17, 18, 18, 19, 16,
72 20, 21, 22, 22, 23, 20
73 };
74
75 std::shared_ptr<FINALITY::Mesh> cubeMesh =
76 FINALITY::Mesh::Create(cubeVertices, cubeIndices);
77
80
81 auto vertexShader =
83 "assets/shaders/shader.vert",
84 device
85 );
86
87 auto fragmentShader =
89 "assets/shaders/shader.frag",
90 device
91 );
92
93 FINALITY::PipelineConfig cubeConfig{};
94 cubeConfig.VertexShader = std::move(vertexShader);
95 cubeConfig.FragmentShader = std::move(fragmentShader);
96 cubeConfig.EnableBlending = false;
97
98 std::shared_ptr<FINALITY::Pipeline> materialPipeline =
100
101 auto sharedMaterial =
102 std::make_shared<FINALITY::Material>(materialPipeline);
103
104 auto pngTexture =
106 "assets/textures/test.png"
107 );
108
109 sharedMaterial->SetTexture("textureSampler", pngTexture);
110
111 constexpr int GRID_X = 20;
112 constexpr int GRID_Y = 10;
113 constexpr int GRID_Z = 20;
114 constexpr float SPACING = 1.5f;
115
116 m_TotalEntities = 0;
117 m_FrameCount = 0;
118 m_AccumulatedTime = 0.0f;
119 m_UiFps = 0.0f;
120 m_UiAvgFrameTimeMs = 0.0f;
121 m_HistoryOffset = 0;
122 m_ShowPerformanceOverlay = false;
123 m_F3PressedLastFrame = false;
124
125 for (float& frameTime : m_FrameTimeHistory)
126 frameTime = 0.0f;
127
128 for (int x = 0; x < GRID_X; x++)
129 {
130 for (int y = 0; y < GRID_Y; y++)
131 {
132 for (int z = 0; z < GRID_Z; z++)
133 {
134 auto entity = CreateEntity(
135 "Cube_" +
136 std::to_string(x) + "_" +
137 std::to_string(y) + "_" +
138 std::to_string(z)
139 );
140
141 auto& transform =
142 entity.GetComponent<FINALITY::TransformComponent>();
143
144 transform.Position = glm::vec3(
145 (x - GRID_X * 0.5f) * SPACING,
146 (y - GRID_Y * 0.5f) * SPACING,
147 (z - GRID_Z * 0.5f) * SPACING
148 );
149
150 transform.Scale = glm::vec3(1.0f);
151
152 entity.AddComponent<FINALITY::MeshComponent>(cubeMesh);
153 entity.AddComponent<FINALITY::MaterialComponent>(sharedMaterial);
154 entity.AddScript<FINALITY::TestScript>();
155
156 m_TotalEntities++;
157 }
158 }
159 }
160
161 m_LastLogTime = std::chrono::steady_clock::now();
162 }
163
164 void OnUpdate(float ts) override
165 {
167
169 0.05f,
170 0.02f,
171 0.02f,
172 1.0f
173 );
174
176 {
177 std::cout << "L key pressed, changing scene!" << std::endl;
180 }
181
182 const bool f3Down =
184
185 if (f3Down && !m_F3PressedLastFrame)
186 m_ShowPerformanceOverlay = !m_ShowPerformanceOverlay;
187
188 m_F3PressedLastFrame = f3Down;
189
190 m_FrameCount++;
191 m_AccumulatedTime += ts;
192
193 m_FrameTimeHistory[m_HistoryOffset] =
194 ts * 1000.0f;
195
196 m_HistoryOffset =
197 (m_HistoryOffset + 1) % 120;
198
199 const auto now =
200 std::chrono::steady_clock::now();
201
202 const float elapsed =
203 std::chrono::duration<float>(
204 now - m_LastLogTime
205 ).count();
206
207 if (elapsed >= 1.0f)
208 {
209 if (m_FrameCount > 0)
210 {
211 m_UiFps =
212 static_cast<float>(m_FrameCount) /
213 elapsed;
214
215 m_UiAvgFrameTimeMs =
216 (m_AccumulatedTime /
217 static_cast<float>(m_FrameCount)) *
218 1000.0f;
219 }
220
221 m_FrameCount = 0;
222 m_AccumulatedTime = 0.0f;
223 m_LastLogTime = now;
224 }
225
226 if (!m_ShowPerformanceOverlay)
227 return;
228
229 FINALITY::PanelFlags overlayFlags =
236
238 glm::vec2(10.0f, 10.0f),
240 glm::vec2(0.0f, 0.0f)
241 );
242
244
245 FINALITY::ImGUIPanel perfPanel(
246 "Performance Overlay",
247 nullptr,
248 overlayFlags
249 );
250
251 perfPanel.Text("System Benchmarks");
252 perfPanel.Text("FPS: %.1f", m_UiFps);
253
254 const int latestIndex =
255 (m_HistoryOffset + 119) % 120;
256
257 perfPanel.Text(
258 "Frame Time: %.2f ms",
259 m_FrameTimeHistory[latestIndex]
260 );
261
262 perfPanel.Text(
263 "Avg (1s): %.2f ms",
264 m_UiAvgFrameTimeMs
265 );
266
267 perfPanel.Text(
268 "GPU main render: %.2f ms",
269 static_cast<float>(FINALITY::Renderer::GetDevice()->GetGPUTimeMS())
270 );
271
272 perfPanel.PlotLines(
273 "##TimeGraph",
274 m_FrameTimeHistory,
275 120,
276 m_HistoryOffset,
277 "Frame History (ms)",
278 0.0f,
279 33.3f,
280 glm::vec2(200.0f, 60.0f)
281 );
282
283 perfPanel.Text("Renderer Statistics");
284
285 const uint32_t currentVisible =
287
288 const uint32_t currentCulled =
290
291 const float cullPercent =
292 m_TotalEntities > 0
293 ? 100.0f *
294 static_cast<float>(currentCulled) /
295 static_cast<float>(m_TotalEntities)
296 : 0.0f;
297
298 perfPanel.Text(
299 "Total Entities: %u",
300 m_TotalEntities
301 );
302
303 perfPanel.Text(
304 "Visible Count: %u",
305 currentVisible
306 );
307
308 perfPanel.Text(
309 "Culled Count: %u (%.1f%%)",
310 currentCulled,
311 cullPercent
312 );
313 }
314
315 void OnDestroy() override
316 {
318 }
319
320private:
321 std::unique_ptr<MainScene> next = std::make_unique<MainScene>();
322 uint32_t m_TotalEntities = 0;
323 uint32_t m_FrameCount = 0;
324
325 float m_AccumulatedTime = 0.0f;
326 float m_UiFps = 0.0f;
327 float m_UiAvgFrameTimeMs = 0.0f;
328
329 std::chrono::steady_clock::time_point m_LastLogTime;
330
331 bool m_ShowPerformanceOverlay = false;
332 bool m_F3PressedLastFrame = false;
333
334 float m_FrameTimeHistory[120]{};
335 int m_HistoryOffset = 0;
336};
void OnUpdate(float ts) override
Dispatches activation and simulation callbacks for a scene update.
void OnInit() override
void OnDestroy() override
static std::shared_ptr< T > CreateAsset(const std::string &filePath, Args &&... args)
void Text(std::string_view text)
static void SetNextWindowBgAlpha(float alpha)
void PlotLines(std::string_view label, const float *values, int values_count, int values_offset=0, std::string_view overlay_text="", float scale_min=FLT_MAX, float scale_max=FLT_MAX, glm::vec2 graph_size=glm::vec2(0.0f, 0.0f))
static void SetNextWindowPos(const glm::vec2 &pos, PanelCondition cond=PanelCondition_Always, const glm::vec2 &pivot=glm::vec2(0.0f, 0.0f))
static bool IsKeyUp(KeyCode key)
static bool IsKeyDown(KeyCode key)
static std::shared_ptr< Mesh > Create(const std::vector< Vertex > &vertices)
static std::shared_ptr< Pipeline > Create(const PipelineConfig &config)
static void SetClearColor(float r, float g, float b, float a)
Defines the graphics backend boundary and render submission contract.
static uint32_t GetCulledCount()
Definition Renderer.h:66
static void SetActiveCamera(Camera *camera)
Definition Renderer.h:59
static uint32_t GetSubmittedCount()
Definition Renderer.h:65
static RenderDevice * GetDevice()
Definition Renderer.h:57
Owns entities and dispatches script simulation.
Definition Scene.h:36
virtual void OnDestroy()
virtual void OnInit()=0
Entity CreateEntity(const std::string &name=std::string())
virtual void OnUpdate(float ts)
Dispatches activation and simulation callbacks for a scene update.
static SceneManager & GetInstance()
void SetScene(std::unique_ptr< Scene > scene)
@ PanelFlags_NoFocusOnAppearing
Definition ImGUIPanel.h:93
@ PanelFlags_NoMove
Definition ImGUIPanel.h:87
@ PanelFlags_NoNav
Definition ImGUIPanel.h:94
@ PanelFlags_NoDecoration
Definition ImGUIPanel.h:95
@ PanelFlags_AlwaysAutoResize
Definition ImGUIPanel.h:90
@ PanelFlags_NoSavedSettings
Definition ImGUIPanel.h:92
@ PanelCondition_Always
Definition ImGUIPanel.h:104
int PanelFlags
Definition ImGUIPanel.h:98
std::shared_ptr< Shader > VertexShader
Definition Pipeline.h:26
std::shared_ptr< Shader > FragmentShader
Definition Pipeline.h:27