From 4f3da2e2639e59ff63d183caa1c388ef190fd260 Mon Sep 17 00:00:00 2001 From: Robert Chisholm Date: Sat, 6 Aug 2022 16:30:13 +0100 Subject: [PATCH 1/2] Include Random Seed in Debug Menu. Also some early changes towards a menu to view (and change?) env props. --- .../flamegpu/visualiser/FLAMEGPU_Visualisation.h | 11 ++++++++++- .../visualiser/FLAMEGPU_Visualisation.cpp | 6 ++++++ src/flamegpu/visualiser/Visualiser.cpp | 6 +++++- src/flamegpu/visualiser/Visualiser.h | 15 ++++++++++++++- 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/include/flamegpu/visualiser/FLAMEGPU_Visualisation.h b/include/flamegpu/visualiser/FLAMEGPU_Visualisation.h index 3d118fb..fb42195 100644 --- a/include/flamegpu/visualiser/FLAMEGPU_Visualisation.h +++ b/include/flamegpu/visualiser/FLAMEGPU_Visualisation.h @@ -3,6 +3,7 @@ #include #include +#include #include "flamegpu/visualiser/config/TexBufferConfig.h" @@ -35,11 +36,19 @@ class FLAMEGPU_Visualisation { const std::map& core_tex_buffers, const std::multimap& tex_buffers) { updateAgentStateBuffer(agent_name.c_str(), state_name.c_str(), buffLen, core_tex_buffers, tex_buffers); } + /** + * Provide the random seed, so it can be displayed in the debug menu + */ + void registerEnvironmentProperty(const std::string& property_name, void* ptr, std::type_index type, unsigned int elements); /** * Update the UI step counter * @note When this value is first set non-0, the visualiser assumes sim has begun executing */ - void setStepCount(const unsigned int stepCount); + void setStepCount(unsigned int stepCount); + /** + * Provide the random seed, so it can be displayed in the debug menu + */ + void setRandomSeed(uint64_t randomSeed); /* * Start visualiser in background thread */ diff --git a/src/flamegpu/visualiser/FLAMEGPU_Visualisation.cpp b/src/flamegpu/visualiser/FLAMEGPU_Visualisation.cpp index 530d979..d4a6a6f 100644 --- a/src/flamegpu/visualiser/FLAMEGPU_Visualisation.cpp +++ b/src/flamegpu/visualiser/FLAMEGPU_Visualisation.cpp @@ -37,9 +37,15 @@ void FLAMEGPU_Visualisation::updateAgentStateBuffer(const char *agent_name, cons const std::map& core_tex_buffers, const std::multimap& tex_buffers) { vis->updateAgentStateBuffer(agent_name, state_name, buffLen, core_tex_buffers, tex_buffers); } +void FLAMEGPU_Visualisation::registerEnvironmentProperty(const std::string& property_name, void* ptr, const std::type_index type, const unsigned int elements) { + vis->registerEnvironmentProperty(property_name, ptr, type, elements); +} void FLAMEGPU_Visualisation::setStepCount(const unsigned int stepCount) { vis->setStepCount(stepCount); } +void FLAMEGPU_Visualisation::setRandomSeed(const uint64_t randomSeed) { + vis->setRandomSeed(randomSeed); +} void FLAMEGPU_Visualisation::start() { vis->start(); } diff --git a/src/flamegpu/visualiser/Visualiser.cpp b/src/flamegpu/visualiser/Visualiser.cpp index 8083747..c6c1830 100644 --- a/src/flamegpu/visualiser/Visualiser.cpp +++ b/src/flamegpu/visualiser/Visualiser.cpp @@ -873,7 +873,7 @@ void Visualiser::updateFPS() { this->frameCount = 0; } } -void Visualiser::setStepCount(const unsigned int &_stepCount) { +void Visualiser::setStepCount(const unsigned int _stepCount) { // This boring value is used to display the number of steps stepCount = _stepCount; // The rest is to calcualted steps/second, basically the same as FPS @@ -889,6 +889,9 @@ void Visualiser::setStepCount(const unsigned int &_stepCount) { this->lastStepCount = _stepCount; } } +void Visualiser::setRandomSeed(const uint64_t _randomSeed) { + randomSeed = _randomSeed; +} // Overrides unsigned Visualiser::getWindowWidth() const { return windowDims.x; @@ -1101,6 +1104,7 @@ void Visualiser::updateDebugMenu() { ss << std::setprecision(3); ss << std::fixed; // To stop camera floats from changing box width ss << "===Debug Menu===" << "\n"; + ss << "Random Seed: " << randomSeed << "\n"; const glm::vec3 eye = camera->getEye(); const glm::vec3 look = camera->getLook(); const glm::vec3 up = camera->getUp(); diff --git a/src/flamegpu/visualiser/Visualiser.h b/src/flamegpu/visualiser/Visualiser.h index 1bf29ee..182cc21 100644 --- a/src/flamegpu/visualiser/Visualiser.h +++ b/src/flamegpu/visualiser/Visualiser.h @@ -12,6 +12,7 @@ #include #include #include +#include #undef main // SDL breaks the regular main entry point, this fixes #define GLM_FORCE_NO_CTOR_INIT #include @@ -117,6 +118,10 @@ class Visualiser : public ViewportExt { */ void updateAgentStateBuffer(const std::string &agent_name, const std::string &state_name, const unsigned int buffLen, const std::map& _core_tex_buffers, const std::multimap& _tex_buffers); + /** + * Todo + */ + void registerEnvironmentProperty(const std::string& property_name, void* ptr, std::type_index type, unsigned int elements) { } private: void run(); @@ -231,7 +236,11 @@ class Visualiser : public ViewportExt { * Sets the value to be rendered to the HUD step counter (if enabled) * @param stepCount The step value to be displayed */ - void setStepCount(const unsigned int &stepCount); + void setStepCount(unsigned int stepCount); + /** + * Sets the value to be rendered for random seed in the debug menu + */ + void setRandomSeed(uint64_t randomSeed); private: void updateDebugMenu(); @@ -300,6 +309,10 @@ class Visualiser : public ViewportExt { * Displays internal status info to screen */ std::shared_ptr debugMenu; + /** + * Random seed, displayed in debugMenu + */ + uint64_t randomSeed = 0; /** * If set true, we don't display agent states in debug menu because agents only have one state. */ From 214ad2d35d8788d6bd8436b130f507579e85c52a Mon Sep 17 00:00:00 2001 From: Robert Chisholm Date: Sat, 6 Aug 2022 20:45:17 +0100 Subject: [PATCH 2/2] Draft support for visualising environment properties (F2 menu) See main repo PR for suggested refinements. --- .../visualiser/FLAMEGPU_Visualisation.h | 2 +- .../visualiser/FLAMEGPU_Visualisation.cpp | 4 +- src/flamegpu/visualiser/Visualiser.cpp | 79 ++++++++++++++++++- src/flamegpu/visualiser/Visualiser.h | 19 +++-- 4 files changed, 95 insertions(+), 9 deletions(-) diff --git a/include/flamegpu/visualiser/FLAMEGPU_Visualisation.h b/include/flamegpu/visualiser/FLAMEGPU_Visualisation.h index fb42195..6438afc 100644 --- a/include/flamegpu/visualiser/FLAMEGPU_Visualisation.h +++ b/include/flamegpu/visualiser/FLAMEGPU_Visualisation.h @@ -39,7 +39,7 @@ class FLAMEGPU_Visualisation { /** * Provide the random seed, so it can be displayed in the debug menu */ - void registerEnvironmentProperty(const std::string& property_name, void* ptr, std::type_index type, unsigned int elements); + void registerEnvironmentProperty(const std::string& property_name, void* ptr, std::type_index type, unsigned int elements, bool is_const); /** * Update the UI step counter * @note When this value is first set non-0, the visualiser assumes sim has begun executing diff --git a/src/flamegpu/visualiser/FLAMEGPU_Visualisation.cpp b/src/flamegpu/visualiser/FLAMEGPU_Visualisation.cpp index d4a6a6f..877c74b 100644 --- a/src/flamegpu/visualiser/FLAMEGPU_Visualisation.cpp +++ b/src/flamegpu/visualiser/FLAMEGPU_Visualisation.cpp @@ -37,8 +37,8 @@ void FLAMEGPU_Visualisation::updateAgentStateBuffer(const char *agent_name, cons const std::map& core_tex_buffers, const std::multimap& tex_buffers) { vis->updateAgentStateBuffer(agent_name, state_name, buffLen, core_tex_buffers, tex_buffers); } -void FLAMEGPU_Visualisation::registerEnvironmentProperty(const std::string& property_name, void* ptr, const std::type_index type, const unsigned int elements) { - vis->registerEnvironmentProperty(property_name, ptr, type, elements); +void FLAMEGPU_Visualisation::registerEnvironmentProperty(const std::string& property_name, void* ptr, const std::type_index type, const unsigned int elements, const bool is_const) { + vis->registerEnvironmentProperty(property_name, ptr, type, elements, is_const); } void FLAMEGPU_Visualisation::setStepCount(const unsigned int stepCount) { vis->setStepCount(stepCount); diff --git a/src/flamegpu/visualiser/Visualiser.cpp b/src/flamegpu/visualiser/Visualiser.cpp index c6c1830..f50b9cf 100644 --- a/src/flamegpu/visualiser/Visualiser.cpp +++ b/src/flamegpu/visualiser/Visualiser.cpp @@ -144,6 +144,12 @@ Visualiser::Visualiser(const ModelConfig& modelcfg) debugMenu->setVisible(false); hud->add(debugMenu, HUD::AnchorV::North, HUD::AnchorH::West, glm::ivec2(0, 0), INT_MAX); } + { // Environment menu + environmentMenu = std::make_shared("", 16, glm::vec3(1.0f), fonts::findFont({ "Consolas", "Arial" }, fonts::GenericFontFamily::SANS).c_str()); + environmentMenu->setBackgroundColor(glm::vec4(*reinterpret_cast(&modelcfg.clearColor[0]), 0.65f)); + environmentMenu->setVisible(false); + hud->add(environmentMenu, HUD::AnchorV::North, HUD::AnchorH::West, glm::ivec2(0, 0), INT_MAX); + } lines = std::make_shared(); lines->setViewMatPtr(camera->getViewMatPtr()); lines->setProjectionMatPtr(&this->projMat); @@ -404,6 +410,7 @@ void Visualiser::render() { // Update lighting lighting->update(); updateDebugMenu(); + updateEnvironmentMenu(); // Render render_buffer->use(); for (auto &sm : staticModels) @@ -604,6 +611,11 @@ void Visualiser::updateAgentStateBuffer(const std::string &agent_name, const std } } +void Visualiser::registerEnvironmentProperty(const std::string& property_name, void* ptr, std::type_index type, unsigned int elements, bool is_const) { + // Construct an (ordered) map of the registered properties + env_properties.emplace(property_name, EnvPropReference{ ptr, type, elements, is_const}); +} + // Items taken from sdl_exp bool Visualiser::init() { if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER) != 0) { @@ -708,6 +720,7 @@ void Visualiser::deallocateGLObjects() { stepDisplay.reset(); spsDisplay.reset(); debugMenu.reset(); + environmentMenu.reset(); this->hud->clear(); // Don't clear the map, as update buffer methods might still be called for (auto &as : agentStates) { @@ -804,8 +817,18 @@ void Visualiser::handleKeypress(SDL_Keycode keycode, int /*x*/, int /*y*/) { this->hud->reload(); break; case SDLK_F1: - if (this->debugMenu) + if (this->debugMenu) { this->debugMenu->setVisible(!this->debugMenu->getVisible()); + if (this->debugMenu->getVisible() && this->environmentMenu) + this->environmentMenu->setVisible(false); + } + break; + case SDLK_F2: + if (this->environmentMenu) { + this->environmentMenu->setVisible(!this->environmentMenu->getVisible()); + if (this->environmentMenu->getVisible() && this->debugMenu) + this->debugMenu->setVisible(false); + } break; case SDLK_p: if (this->pause_guard) { @@ -1137,6 +1160,60 @@ void Visualiser::updateDebugMenu() { debugMenu->setString(ss.str().c_str()); } } +void writeType(std::stringstream &ss, const std::type_index &type, const void * ptr, const unsigned int offset = 0) { + // Would be nice if we could grab the appropriate function pointer to the function for each type once and cache it? + if (type == std::type_index(typeid(int8_t))) { + ss << static_cast(ptr)[offset]; + } else if (type == std::type_index(typeid(int16_t))) { + ss << static_cast(ptr)[offset]; + } else if (type == std::type_index(typeid(int32_t))) { + ss << static_cast(ptr)[offset]; + } else if (type == std::type_index(typeid(int64_t))) { + ss << static_cast(ptr)[offset]; + } else if (type == std::type_index(typeid(uint8_t))) { + ss << static_cast(ptr)[offset]; + } else if (type == std::type_index(typeid(uint16_t))) { + ss << static_cast(ptr)[offset]; + } else if (type == std::type_index(typeid(uint32_t))) { + ss << static_cast(ptr)[offset]; + } else if (type == std::type_index(typeid(uint64_t))) { + ss << static_cast(ptr)[offset]; + } else if (type == std::type_index(typeid(float))) { + ss << static_cast(ptr)[offset]; + } else if (type == std::type_index(typeid(double))) { + ss << static_cast(ptr)[offset]; + } else if (type == std::type_index(typeid(bool))) { + ss << (static_cast(ptr)[offset] ? "True" : "False"); + } else if (offset == 0) { + ss << "Unsupported"; + } +} +void Visualiser::updateEnvironmentMenu() { + if (environmentMenu && environmentMenu->getVisible()) { + std::stringstream ss; + ss << std::setprecision(5); // Stop changing properties from changing panel width + ss << std::fixed; + ss << "===Environment Properties===" << "\n"; + for (const auto &prop : env_properties) { + // Can't (easily) align all colons, the Text code does multi-space width wrong (and we can't guarantee fixed width font) + ss << prop.first.c_str() <<": "; + if (prop.second.elements == 1) { + writeType(ss, prop.second.type, prop.second.ptr); + } else { + // Array + ss << "["; + for (unsigned int i = 0; i < prop.second.elements; ++i) { + if (i != 0) + ss << ", "; + writeType(ss, prop.second.type, prop.second.ptr, i); + } + ss << "]"; + } + ss << "\n"; + } + environmentMenu->setString(ss.str().c_str()); + } +} } // namespace visualiser } // namespace flamegpu diff --git a/src/flamegpu/visualiser/Visualiser.h b/src/flamegpu/visualiser/Visualiser.h index 182cc21..030180b 100644 --- a/src/flamegpu/visualiser/Visualiser.h +++ b/src/flamegpu/visualiser/Visualiser.h @@ -121,7 +121,7 @@ class Visualiser : public ViewportExt { /** * Todo */ - void registerEnvironmentProperty(const std::string& property_name, void* ptr, std::type_index type, unsigned int elements) { } + void registerEnvironmentProperty(const std::string& property_name, void* ptr, std::type_index type, unsigned int elements, bool is_const); private: void run(); @@ -244,6 +244,7 @@ class Visualiser : public ViewportExt { private: void updateDebugMenu(); + void updateEnvironmentMenu(); SDL_Window *window; SDL_Rect windowedBounds; SDL_GLContext context; @@ -309,19 +310,27 @@ class Visualiser : public ViewportExt { * Displays internal status info to screen */ std::shared_ptr debugMenu; - /** - * Random seed, displayed in debugMenu - */ - uint64_t randomSeed = 0; /** * If set true, we don't display agent states in debug menu because agents only have one state. */ bool debugMenu_showStateNames = false; + /** + * Random seed, displayed in debugMenu + */ + uint64_t randomSeed = 0; /** * Steps equivalent of FPS. * Calculated in the wrong thread, so we update it whenever FPS updates */ double stepsPerSecond = 0.0; + struct EnvPropReference { + void *ptr; + std::type_index type; + unsigned int elements; + bool is_const; + }; + std::map env_properties; + std::shared_ptr environmentMenu; /** * Pressing F8 changes whether FPS is displayed in the bottom corner, according to this schema * The list is iterated in reverse (2, 1, 0)