From 5a47d3bad4eabf8d8a4727a312214150303f7eec Mon Sep 17 00:00:00 2001 From: mitchellhansen Date: Tue, 27 Feb 2018 22:13:59 -0800 Subject: [PATCH] Debugging of a mouse polling issue, refactored the graph thing and finished tweaking it, added a few profiles to the graph, so pretty --- CMakeLists.txt | 7 +- include/Application.h | 3 +- include/Camera.h | 5 +- include/GraphTimer.h | 34 ++++++++++ include/Input.h | 1 + include/imgui/imgui-multilines.hpp | 7 +- include/util.hpp | 100 +---------------------------- src/Application.cpp | 16 ++++- src/Camera.cpp | 24 ++++--- src/GraphTimer.cpp | 88 +++++++++++++++++++++++++ src/imgui/imgui_demo.cpp | 2 +- src/map/Octree.cpp | 1 + 12 files changed, 169 insertions(+), 119 deletions(-) create mode 100644 include/GraphTimer.h create mode 100644 src/GraphTimer.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index edbfb09..8c81ac3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -42,6 +42,9 @@ endif() # beignet only supports EGL and not GLX for the cl_khr_gl_sharing extension if (UNIX) + # Find X11 + find_package(X11 REQUIRED) + message(STATUS "XSS found: ${X11_FOUND}") # find_package(EGL REQUIRED) #message(STATUS "EGL found: ${EGL_FOUND}") endif (UNIX) @@ -51,6 +54,7 @@ include_directories(${SFML_INCLUDE_DIR}) include_directories(${OpenCL_INCLUDE_DIRS}) include_directories(${OpenGL_INCLUDE_DIRS}) if (UNIX) + include_directories(${X11_INCLUDE_DIR}) # include_directories(${EGL_INCLUDE_DIRS}) endif() include_directories(include) @@ -62,7 +66,7 @@ file(GLOB_RECURSE KERNELS "kernels/*.cl") file(GLOB_RECURSE SHADERS "shaders/*.vert" "shaders/*.tesc" "shaders/*.tese" "shaders/*.geom" "shaders/*.frag" "shaders/*.comp") -add_executable(${PNAME} ${SOURCES} ${HEADERS} ${KERNELS} ${SHADERS} include/imgui/imgui-multilines.hpp) +add_executable(${PNAME} ${SOURCES} ${HEADERS} ${KERNELS} ${SHADERS}) # Follow the sub directory structure to add sub-filters in VS # Gotta do it one by one unfortunately @@ -133,6 +137,7 @@ target_link_libraries (${PNAME} ${OpenCL_LIBRARY}) target_link_libraries (${PNAME} ${OPENGL_LIBRARIES}) if (UNIX) + target_link_libraries (${PNAME} ${X11_LIBRARIES}) # target_link_libraries (${PNAME} ${EGL_LIBRARIES}) endif() diff --git a/include/Application.h b/include/Application.h index bc1f30d..e341b08 100644 --- a/include/Application.h +++ b/include/Application.h @@ -30,6 +30,7 @@ #include "LightHandle.h" #include "map/Map.h" #include "util.hpp" +#include "GraphTimer.h" // Srsly people who macro error codes are the devil #undef ERROR @@ -77,7 +78,7 @@ private: // The sfml imgui wrapper I'm using requires Update be called with sf::Time // Might modify it to also accept seconds sf::Clock sf_delta_clock; - fps_counter fps; + GraphTimer fps; // vars for us to use with ImGUI float light_color[4] = { 0, 0, 0, 0 }; diff --git a/include/Camera.h b/include/Camera.h index 36173b7..cc78b06 100644 --- a/include/Camera.h +++ b/include/Camera.h @@ -2,10 +2,13 @@ #include #include #include + #include "Gui.h" #include "Pub_Sub.h" +#include "imgui/imgui.h" +#include "imgui/imgui-SFML.h" #include "util.hpp" - +#include /** diff --git a/include/GraphTimer.h b/include/GraphTimer.h new file mode 100644 index 0000000..720d051 --- /dev/null +++ b/include/GraphTimer.h @@ -0,0 +1,34 @@ +#pragma once +#include +#include +#include +#include +#include + +struct GraphTimer { +public: + GraphTimer(); + + ~GraphTimer(); + + unsigned int create_line(std::string label); + unsigned int delete_line(unsigned int idx); + + void start(unsigned int idx); + void stop(unsigned int idx); + void frame(unsigned int idx, double delta_time); + + void draw(); + +private: + + static std::chrono::time_point start_time; + static bool started; + + const unsigned int FPS_ARRAY_LENGTH = 1000; + std::vector> fps_vectors; + std::vector checkpoints; + std::vector counters; + std::vector labels; + +}; \ No newline at end of file diff --git a/include/Input.h b/include/Input.h index bc6dddf..931e3ab 100644 --- a/include/Input.h +++ b/include/Input.h @@ -9,6 +9,7 @@ #include "LightHandle.h" #include "Logger.h" #include "Pub_Sub.h" +#include #include /** * diff --git a/include/imgui/imgui-multilines.hpp b/include/imgui/imgui-multilines.hpp index 3a82c7d..c32935f 100644 --- a/include/imgui/imgui-multilines.hpp +++ b/include/imgui/imgui-multilines.hpp @@ -1,10 +1,9 @@ - #pragma once +#include +#include #include "imgui.h" #include "imgui_internal.h" - - namespace ImGui { static ImU32 InvertColorU32(ImU32 in) @@ -24,7 +23,7 @@ namespace ImGui { } static void PlotMultiLines( - const std::vector> &data, + const std::vector> &data, std::string title, const std::vector &labels, const std::vector &colors, diff --git a/include/util.hpp b/include/util.hpp index 6af0cc8..46e7e50 100644 --- a/include/util.hpp +++ b/include/util.hpp @@ -1,6 +1,7 @@ #pragma once #include #include + #include #include #include @@ -10,109 +11,12 @@ #include #include #include -#include -#include + #include "Vector4.hpp" const double PI = 3.141592653589793238463; const float PI_F = 3.14159265358979f; -struct fps_counter { -public: - fps_counter() { - - }; - - ~fps_counter() { - for (auto i: fps_vectors){ - delete[] i; - } - }; - - void frame(double delta_time) { - - // Apply 100 units of smoothing - if (frame_count == 100) { - frame_count = 0; - fps_average = 0; - } - frame_count++; - fps_average += (delta_time - fps_average) / frame_count; - instant_fps = delta_time; - } - - static float edit(const void* data, int idx){ - *(int*)data = idx; - //return *(int*)(data+sizeof(int)*idx); - }; - void draw() { - - if (arr_pos == 1000) - arr_pos = 0; - - fps_array[arr_pos] = static_cast(1.0 / instant_fps); - arr_pos++; - - ImGui::Begin("Performance"); - //ImVec2 wh = ImGui::GetContentRegionAvail(); - ImVec2 wh(100, 200); - - int a[3] = {1, 2, 7}; - int b[3] = {5, 3, 1}; - int c[3] = {8, 1, 4}; - const void* to_data[3] = {&a, &b, &c}; - const char* to_names[3] = {"a", "b", "z"}; - ImGuiPlotType plottype = ImGuiPlotType_Lines; - ImColor color = ImColor(255, 255, 255); - //ImGui::PlotMultiLines( - // "label", 3, to_names, &color, &edit, - // to_data, 3, 0.0f, 10.0f, ImVec2(300, 300)); - - std::vector> data = { - {1, 2, 3, 4}, - {9, 3, 7, 1}, - {8, 3, 6, 2} - }; - - std::string title = "HELLO"; - - std::vector labels = { - "ONE", - "TWO", - "THREE" - }; - - std::vector colors = { - ImColor(255, 255, 255), - ImColor( 0, 255, 0), - ImColor(255, 0, 0), - }; - - sf::Vector2f graph_size(300, 300); - - ImGui::PlotMultiLines(data, title, labels, colors, 10, 0, graph_size); - - ImGui::PlotLines("FPS", fps_array, 1000, 0, - std::to_string(1.0 / fps_average).c_str(), - 0.0f, 150.0f, wh); - ImGui::End(); - } - -private: - - const unsigned int FPS_ARRAY_LENGTH = 1000; - std::vector fps_vectors; - - - float fps_array[1000]{60}; - int arr_pos = 0; - - double instant_fps = 0; - double frame_count = 0; - double fps_average = 0; - -}; - inline sf::Vector3f SphereToCart(sf::Vector2f i) { auto r = sf::Vector3f( diff --git a/src/Application.cpp b/src/Application.cpp index 4600ace..9526800 100644 --- a/src/Application.cpp +++ b/src/Application.cpp @@ -14,6 +14,8 @@ Application::Application() { ImGui::SFML::Init(*window); window->resetGLStates(); + + } Application::~Application() { @@ -108,12 +110,18 @@ bool Application::init_events() { bool Application::game_loop() { + int fps_idx = fps.create_line("FPS"); + int compute_fps_idx = fps.create_line("Compute"); + int event_fps_idx = fps.create_line("Event"); + while (true) { // Have the input handler empty the event stack, generate events for held keys, and then dispatch the events to listeners - input_handler.consume_sf_events(window.get()); - input_handler.handle_held_keys(); + fps.start(event_fps_idx); + input_handler.consume_sf_events(window.get()); + input_handler.handle_held_keys(); input_handler.dispatch_events(); + fps.stop(event_fps_idx); if (!window->isOpen()) break; @@ -142,16 +150,18 @@ bool Application::game_loop() { light_handle->update(delta_time); // Run the raycast + fps.start(compute_fps_idx); if (!raycaster->compute()) { abort(); }; + fps.stop(compute_fps_idx); } // Let the raycaster draw it screen buffer raycaster->draw(window.get()); // Give the frame counter the frame time and draw the average frame time - fps.frame(delta_time); + fps.frame(fps_idx, delta_time); fps.draw(); Gui::do_render(); diff --git a/src/Camera.cpp b/src/Camera.cpp index 8105217..d82db07 100644 --- a/src/Camera.cpp +++ b/src/Camera.cpp @@ -1,7 +1,9 @@ #include "Camera.h" -Camera::Camera() {} + +Camera::Camera() { +} Camera::Camera(sf::Vector3f position, sf::Vector2f direction, sf::RenderWindow* window) : @@ -194,11 +196,13 @@ void Camera::event_handler(VrEventPublisher *publisher, std::unique_ptrx, mouse_event->y); if (deltas != sf::Vector2i(0, 0) && mouse_enabled == true) { - sf::Mouse::setPosition(fixed, *window); - slew_camera(sf::Vector2f( - deltas.y / 1200.0f, - deltas.x / 1200.0f - )); + // TODO: Set Position causes some weird frame-limiting behaviour + // TODO: consider using some other mouse interaction + sf::Mouse::setPosition(fixed, *window); + slew_camera(sf::Vector2f( + deltas.y / 1200.0f, + deltas.x / 1200.0f + )); } } } @@ -207,10 +211,10 @@ void Camera::event_handler(VrEventPublisher *publisher, std::unique_ptr(event.get()); if (mouse_event->button == sf::Mouse::Middle) { - mouse_enabled = !mouse_enabled; - sf::Mouse::setPosition(fixed, *window); - } - + mouse_enabled = !mouse_enabled; + sf::Mouse::setPosition(fixed, *window); + } + } else if (event->type == vr::Event::JoystickMoved) { diff --git a/src/GraphTimer.cpp b/src/GraphTimer.cpp new file mode 100644 index 0000000..a81b758 --- /dev/null +++ b/src/GraphTimer.cpp @@ -0,0 +1,88 @@ +#include "GraphTimer.h" + +GraphTimer::GraphTimer() { + if (!started) { + start_time = std::chrono::system_clock::now(); + started = true; + } +} + +GraphTimer::~GraphTimer() { + +} + +unsigned int GraphTimer::create_line(std::string label) { + fps_vectors.push_back(std::vector(FPS_ARRAY_LENGTH, 0)); + counters.push_back(0); + checkpoints.push_back(0); + labels.push_back(label); + return fps_vectors.size()-1; +} + +unsigned int GraphTimer::delete_line(unsigned int idx){ + fps_vectors.erase(fps_vectors.begin() + idx); +} + +void GraphTimer::start(unsigned int idx){ + + std::chrono::time_point now = std::chrono::system_clock::now(); + std::chrono::duration elapsed_time = now - start_time; + + checkpoints.at(idx) = elapsed_time.count(); +} + +void GraphTimer::stop(unsigned int idx){ + + std::chrono::time_point now = std::chrono::system_clock::now(); + std::chrono::duration elapsed_time = now - start_time; + + fps_vectors.at(idx).at(counters.at(idx)) = elapsed_time.count() - checkpoints.at(idx); + fps_vectors.at(idx).at(counters.at(idx)) = 1.0f / fps_vectors.at(idx).at(counters.at(idx)); + if (++counters.at(idx) >= FPS_ARRAY_LENGTH - 1) + counters.at(idx) = 0; +} + +void GraphTimer::frame(unsigned int idx, double delta_time) { + fps_vectors.at(idx).at(counters.at(idx)) = 1.0 / delta_time; + if (++counters.at(idx) >= FPS_ARRAY_LENGTH - 1) + counters.at(idx) = 0; +} + +void GraphTimer::draw() { + + ImGui::Begin("Performance"); + + std::vector> data = { + {1, 2, 3, 4}, + {9, 3, 7, 1}, + {8, 3, 6, 2} + }; + + std::string title = std::to_string(fps_vectors.at(0).at(counters.at(0))); + + + std::vector colors = { + ImColor(255, 255, 255), + ImColor(0, 255, 0), + ImColor(255, 0, 0), + }; + + ImVec2 wh = ImGui::GetContentRegionAvail(); + wh.x -= wh.x * 0.15; + sf::Vector2f graph_size(wh.x, wh.y); + + ImGui::PlotMultiLines(fps_vectors, title, labels, colors, 200, 0, + graph_size); + + + //ImVec2 wh(100, 200); +// ImGui::PlotLines("FPS", fps_array, 1000, 0, +// std::to_string(1.0 / fps_average).c_str(), +// 0.0f, 150.0f, wh); + + ImGui::End(); + +} + +std::chrono::time_point GraphTimer::start_time; +bool GraphTimer::started; \ No newline at end of file diff --git a/src/imgui/imgui_demo.cpp b/src/imgui/imgui_demo.cpp index d739dfa..c46ce80 100644 --- a/src/imgui/imgui_demo.cpp +++ b/src/imgui/imgui_demo.cpp @@ -22,7 +22,7 @@ #define _CRT_SECURE_NO_WARNINGS #endif -#include "imgui.h" +#include "imgui/imgui.h" #include // toupper, isprint #include // sqrtf, powf, cosf, sinf, floorf, ceilf #include // vsnprintf, sscanf, printf diff --git a/src/map/Octree.cpp b/src/map/Octree.cpp index 302571d..32fb8b6 100644 --- a/src/map/Octree.cpp +++ b/src/map/Octree.cpp @@ -1,3 +1,4 @@ +#include #include "map/Octree.h" Octree::Octree() {