diff --git a/application/include/sandbox.hpp b/application/include/sandbox.hpp index 21d8524..129b719 100755 --- a/application/include/sandbox.hpp +++ b/application/include/sandbox.hpp @@ -1,6 +1,8 @@ #ifndef SANDBOX_HPP #define SANDBOX_HPP +#include "open_engine/core/time.hpp" +#include #include #include @@ -27,8 +29,7 @@ class SandboxLayer : public OpenEngine::Layer -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, }; - OpenEngine::Ref vertex_buffer; - vertex_buffer.reset(OpenEngine::VertexBuffer::Create(square_vertices, sizeof(square_vertices))); + OpenEngine::Ref vertex_buffer = OpenEngine::VertexBuffer::Create(square_vertices, sizeof(square_vertices)); OpenEngine::BufferLayout layout = { { OpenEngine::ShaderDataType::Float3, "a_Position" }, @@ -39,8 +40,7 @@ class SandboxLayer : public OpenEngine::Layer square_vertex_array->AddVertexBuffer(vertex_buffer); uint32_t indices[6] = { 0, 1, 2, 2, 3, 0 }; - OpenEngine::Ref index_buffer; - index_buffer.reset(OpenEngine::IndexBuffer::Create(indices, sizeof(indices) / sizeof(uint32_t))); + OpenEngine::Ref index_buffer = OpenEngine::IndexBuffer::Create(indices, sizeof(indices) / sizeof(uint32_t)); square_vertex_array->SetIndexBuffer(index_buffer); @@ -57,8 +57,12 @@ class SandboxLayer : public OpenEngine::Layer camera((float)OpenEngine::Application::Get().GetWindow().GetWidth() / OpenEngine::Application::Get().GetWindow().GetHeight(), 1.0f) { + OE_PROFILE_FUNCTION(); //vertex_array.reset(OpenEngine::VertexArray::Create()); - square = CreateSquare(); + { + OE_PROFILE_SCOPE("Creating Square"); + square = CreateSquare(); + } //float vertices[3 * 7] = { // -0.5f, -0.5f, 0.0f, 0.8f, 0.2f, 0.8f, 1.0f, @@ -86,14 +90,24 @@ class SandboxLayer : public OpenEngine::Layer //texture_shader = OpenEngine::Shader::Create("./assets/shaders/texture.glsl"); auto texture_shader = shader_library.Load("./assets/shaders/texture.glsl"); - texture = OpenEngine::Texture2D::Create("./assets/textures/container.jpg"); - face = OpenEngine::Texture2D::Create("./assets/textures/awesomeface.png"); + { + OE_PROFILE_SCOPE("Creating Textures"); + texture = OpenEngine::Texture2D::Create("./assets/textures/container.jpg"); + face = OpenEngine::Texture2D::Create("./assets/textures/awesomeface.png"); + } - texture_shader->Bind(); - texture_shader->SetInt("u_Texture", 0); + { + OE_PROFILE_SCOPE("Setting up shader uniforms"); + texture_shader->Bind(); + texture_shader->SetInt("u_Texture", 0); + texture_shader->SetVec4("u_Color", glm::vec4(1.0f)); + } - for (auto joystick : OpenEngine::Input::GetJoystickList()) - OE_DEBUG("Joystick {}: {}", joystick.first, joystick.second); + { + OE_PROFILE_SCOPE("Listing Joysticks"); + for (auto joystick : OpenEngine::Input::GetJoystickList()) + OE_DEBUG("Joystick {}: {}", joystick.first, joystick.second); + } bindings = { {"fwd/bckwd", 1}, diff --git a/application/include/sandbox2d.hpp b/application/include/sandbox2d.hpp old mode 100644 new mode 100755 index 7f0c1ea..fe938d9 --- a/application/include/sandbox2d.hpp +++ b/application/include/sandbox2d.hpp @@ -1,9 +1,9 @@ #ifndef SANDBOX2D_HPP #define SANDBOX2D_HPP -#include #include +#include #include "imgui.h" #include @@ -22,13 +22,17 @@ class Sandbox2DLayer : public OpenEngine::Layer void OnAttach() override { + OE_PROFILE_FUNCTION(); bindings = { {"fwd/bckwd", 1}, {"right/left", 0}, {"yaw", 2} }; - face = OpenEngine::Texture2D::Create("assets/textures/awesomeface.png"); + { + OE_PROFILE_SCOPE("Texture2D Creation"); + face = OpenEngine::Texture2D::Create("assets/textures/awesomeface.png"); + } } void OnDetach() override @@ -37,15 +41,19 @@ class Sandbox2DLayer : public OpenEngine::Layer void OnUpdate() override { - OpenEngine::RenderCommand::SetClearColor({0.11f, 0.11f, 0.15f, 1.0f}); - OpenEngine::RenderCommand::Clear(); - - OpenEngine::Renderer2D::BeginScene(camera.GetCamera()); - - OpenEngine::Renderer2D::DrawQuad(glm::vec2(0.5f, 0.5f), glm::vec2(0.3f), glm::vec4(color[0], color[1], color[2], color[3])); - OpenEngine::Renderer2D::DrawQuad(glm::vec2(-0.2f, -0.2f), glm::vec2(0.5f, 0.2f), {0.5f, 0.3f, 0.8f, 1.0f}); - OpenEngine::Renderer2D::DrawQuad(glm::vec3(0.0f, 0.0f, -0.1f), glm::vec2(1.0f, 1.0f), face); - + OE_PROFILE_FUNCTION() + { + OE_PROFILE_SCOPE("Setting up Rendering"); + OpenEngine::RenderCommand::SetClearColor({0.11f, 0.11f, 0.15f, 1.0f}); + OpenEngine::RenderCommand::Clear(); + OpenEngine::Renderer2D::BeginScene(camera.GetCamera()); + } + { + OE_PROFILE_SCOPE("Drawing Quads"); + OpenEngine::Renderer2D::DrawQuad(glm::vec2(0.5f, 0.5f), glm::vec2(0.3f), glm::vec4(color[0], color[1], color[2], color[3])); + OpenEngine::Renderer2D::DrawQuad(glm::vec2(-0.2f, -0.2f), glm::vec2(0.5f, 0.2f), {0.5f, 0.3f, 0.8f, 1.0f}); + OpenEngine::Renderer2D::DrawQuad(glm::vec3(0.0f, 0.0f, -0.1f), glm::vec2(1.0f, 1.0f), face); + } OpenEngine::Renderer2D::EndScene(); } @@ -62,22 +70,38 @@ class Sandbox2DLayer : public OpenEngine::Layer void OnEvent(OpenEngine::Event& event) override { + OE_PROFILE_FUNCTION(); OpenEngine::EventDispatcher dispatcher(event); //dispatcher.Dispatch(BIND_EVENT_FN(Sandbox2DLayer::StopRunning)); - camera.OnEvent(event); + { + OE_PROFILE_SCOPE("Camera OnEvent"); + camera.OnEvent(event); + } } void OnImGuiRender() override { + OE_PROFILE_FUNCTION(); + ImGui::Begin("Square settings"); ImGui::ColorEdit4("Square color", color); + + for (auto& result : profiling_results) + { + char label[50]; + strcpy(label, "%.3fms "); + strcat(label, result.name); + ImGui::Text(label, result.duration); + } + + profiling_results.clear(); + ImGui::End(); } private: //OpenEngine::ShaderLibrary shader_library; - glm::vec3 square_pos = glm::vec3(0.0f); glm::vec4 square_color = glm::vec4(1.0f); @@ -85,7 +109,9 @@ class Sandbox2DLayer : public OpenEngine::Layer OpenEngine::Ref face; std::unordered_map bindings; - OpenEngine::OrthographicCameraController camera = {1280.0f / 1440.0f, 1.0f}; + OpenEngine::OrthographicCameraController camera; + + std::vector profiling_results; }; #endif // SANDBOX2D_HPP diff --git a/application/src/control_layer.cpp b/application/src/control_layer.cpp index e7a328c..b44d12c 100644 --- a/application/src/control_layer.cpp +++ b/application/src/control_layer.cpp @@ -1,7 +1,7 @@ -#include "open_engine/core.hpp" +#include +#include #include #include -#include ControlLayer::ControlLayer(OpenEngine::Ref layer) : active_layer(layer), OpenEngine::Layer("control_layer") @@ -14,12 +14,19 @@ void ControlLayer::OnUpdate() bool ControlLayer::SwitchLayer(OpenEngine::KeyPressedEvent& event) { + OE_PROFILE_FUNCTION(); + auto& app = OpenEngine::Application::Get(); OpenEngine::Ref layer; if (event.GetKeyCode() == OE_KEY_1) { + OE_PROFILE_SCOPE("Press Key 1"); + OE_DEBUG("Sandbox2D Layer"); - layer = OpenEngine::CreateRef(); + { + OE_PROFILE_SCOPE("Creating SB2D"); + layer = OpenEngine::CreateRef(); + } app.QueueLayerPush(layer); app.QueueLayerPop(active_layer); @@ -27,8 +34,13 @@ bool ControlLayer::SwitchLayer(OpenEngine::KeyPressedEvent& event) return true; } else if (event.GetKeyCode() == OE_KEY_2) { + OE_PROFILE_SCOPE("Press Key 2"); + OE_DEBUG("Sandbox Layer"); - layer = OpenEngine::CreateRef(); + { + OE_PROFILE_SCOPE("Creating SB"); + layer = OpenEngine::CreateRef(); + } app.QueueLayerPush(layer); app.QueueLayerPop(active_layer); diff --git a/open_engine/include/open_engine.hpp b/open_engine/include/open_engine.hpp index a4b03e3..55ad6f8 100644 --- a/open_engine/include/open_engine.hpp +++ b/open_engine/include/open_engine.hpp @@ -1,6 +1,7 @@ #ifndef OPEN_ENGINE_HPP #define OPEN_ENGINE_HPP +#include "open_engine/core.hpp" #include "open_engine/application.hpp" #include "open_engine/logging.hpp" #include "open_engine/events/key_event.hpp" diff --git a/open_engine/include/open_engine/application.hpp b/open_engine/include/open_engine/application.hpp index 64d4030..3491b34 100644 --- a/open_engine/include/open_engine/application.hpp +++ b/open_engine/include/open_engine/application.hpp @@ -1,17 +1,15 @@ #ifndef APPLICATION_HPP #define APPLICATION_HPP -#include "core.hpp" - -#include "events/application_event.hpp" -#include "imgui/imgui_layer.hpp" -#include "layer.hpp" -#include "layer_stack.hpp" -#include "window/window.hpp" -#include +#include "open_engine/core.hpp" +#include "open_engine/events/application_event.hpp" +#include "open_engine/imgui/imgui_layer.hpp" +#include "open_engine/window/window.hpp" +#include "open_engine/layer_stack.hpp" +#include "open_engine/layer.hpp" namespace OpenEngine { - class OE_API Application + class Application { public: Application(); @@ -21,20 +19,15 @@ namespace OpenEngine { virtual void OnEvent(Event& event); - //void PushOverlay(Ref overlay); - //void PopOverlay(Ref overlay); - - //void PushLayer(Ref layer); - //void PopLayer(); void QueueLayerPush(Ref layer); void QueueLayerPop(Ref layer); void QueueOverlayPush(Ref layer); void QueueOverlayPop(Ref layer); - inline static Application& Get() { return *instance; } + inline static Application& Get() { return *instance; }; - inline Window& GetWindow() { return *window; } - inline void StopRunning() { running = false; } + inline Window& GetWindow() { return *window; }; + inline void StopRunning() { running = false; }; private: bool OnWindowClose(WindowCloseEvent& event); diff --git a/open_engine/include/open_engine/core.hpp b/open_engine/include/open_engine/core.hpp index 8f1a83c..1f608c1 100644 --- a/open_engine/include/open_engine/core.hpp +++ b/open_engine/include/open_engine/core.hpp @@ -1,16 +1,9 @@ #ifndef CORE_HPP #define CORE_HPP +#include "open_engine/instrumentor.hpp" + #include -#ifdef __linux__ - #ifdef OE_EXPORT - #define OE_API __attribute__((visibility("default"))) - #else - #define OE_API - #endif -#elif defined(_WIN32) || defined(WIN32) - #error Windows is not yet supported -#endif #ifdef OE_ENABLE_ASSERTS #include diff --git a/open_engine/include/open_engine/core/time.hpp b/open_engine/include/open_engine/core/time.hpp old mode 100644 new mode 100755 index c9c0af7..7d1f73d --- a/open_engine/include/open_engine/core/time.hpp +++ b/open_engine/include/open_engine/core/time.hpp @@ -1,13 +1,16 @@ #ifndef TIME_HPP #define TIME_HPP +#include #include namespace OpenEngine { + using highres_time_point = std::chrono::time_point; + class Time { public: - Time(const Time&) = delete; // No copy constructor + Time(const Time&) = delete; Time& operator=(const Time&) = delete; static void Update(); @@ -16,19 +19,76 @@ namespace OpenEngine { { if (instance == nullptr) instance.reset(new Time); - } + }; static double DeltaTime() { return instance->delta_time.count(); }; + struct ProfilingResult + { + double duration = 0; + const char* name; + }; + + /* TODO: Consider: + * + * Timer::Start(); + * + * Timer::SetMark("name"); + * Timer::GetSinceMark("name"); + * Timer::StopMark("name"); + * Timer::GetMarkDuration("name"); + * + * Timer::Stop(); + */ + template + class Timer + { + public: + + Timer(const char* name, Fn funct) + : name(name), func(funct) + { + start_timepoint = std::chrono::high_resolution_clock::now(); + }; + ~Timer() + { + if (!stopped) + Stop(); + }; + + void Stop() + { + auto end_timepoint = std::chrono::high_resolution_clock::now(); + + long long start = std::chrono::time_point_cast(start_timepoint).time_since_epoch().count(); + long long end = std::chrono::time_point_cast(end_timepoint).time_since_epoch().count(); + + stopped = true; + + ProfilingResult result = {0.001f * (end - start), name}; + + func(result); + }; + + private: + std::unordered_map time_points; + const char* name; + highres_time_point start_timepoint; + bool stopped = false; + Fn func; + }; + private: Time() {}; - std::chrono::high_resolution_clock::time_point previous_frame; + + private: static std::unique_ptr