Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Visualise Random Seed and Environment Properties #94

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion include/flamegpu/visualiser/FLAMEGPU_Visualisation.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <map>
#include <string>
#include <cstdint>

#include "flamegpu/visualiser/config/TexBufferConfig.h"

Expand Down Expand Up @@ -35,11 +36,19 @@ class FLAMEGPU_Visualisation {
const std::map<TexBufferConfig::Function, TexBufferConfig>& core_tex_buffers, const std::multimap<TexBufferConfig::Function, CustomTexBufferConfig>& 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, bool is_const);
/**
* 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
*/
Expand Down
6 changes: 6 additions & 0 deletions src/flamegpu/visualiser/FLAMEGPU_Visualisation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,15 @@ void FLAMEGPU_Visualisation::updateAgentStateBuffer(const char *agent_name, cons
const std::map<TexBufferConfig::Function, TexBufferConfig>& core_tex_buffers, const std::multimap<TexBufferConfig::Function, CustomTexBufferConfig>& 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, const bool is_const) {
vis->registerEnvironmentProperty(property_name, ptr, type, elements, is_const);
}
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();
}
Expand Down
85 changes: 83 additions & 2 deletions src/flamegpu/visualiser/Visualiser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Text>("", 16, glm::vec3(1.0f), fonts::findFont({ "Consolas", "Arial" }, fonts::GenericFontFamily::SANS).c_str());
environmentMenu->setBackgroundColor(glm::vec4(*reinterpret_cast<const glm::vec3*>(&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<Draw>();
lines->setViewMatPtr(camera->getViewMatPtr());
lines->setProjectionMatPtr(&this->projMat);
Expand Down Expand Up @@ -404,6 +410,7 @@ void Visualiser::render() {
// Update lighting
lighting->update();
updateDebugMenu();
updateEnvironmentMenu();
// Render
render_buffer->use();
for (auto &sm : staticModels)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -873,7 +896,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
Expand All @@ -889,6 +912,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;
Expand Down Expand Up @@ -1101,6 +1127,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();
Expand Down Expand Up @@ -1133,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<const int8_t*>(ptr)[offset];
} else if (type == std::type_index(typeid(int16_t))) {
ss << static_cast<const int16_t*>(ptr)[offset];
} else if (type == std::type_index(typeid(int32_t))) {
ss << static_cast<const int32_t*>(ptr)[offset];
} else if (type == std::type_index(typeid(int64_t))) {
ss << static_cast<const int64_t*>(ptr)[offset];
} else if (type == std::type_index(typeid(uint8_t))) {
ss << static_cast<const uint8_t*>(ptr)[offset];
} else if (type == std::type_index(typeid(uint16_t))) {
ss << static_cast<const uint16_t*>(ptr)[offset];
} else if (type == std::type_index(typeid(uint32_t))) {
ss << static_cast<const uint32_t*>(ptr)[offset];
} else if (type == std::type_index(typeid(uint64_t))) {
ss << static_cast<const uint64_t*>(ptr)[offset];
} else if (type == std::type_index(typeid(float))) {
ss << static_cast<const float*>(ptr)[offset];
} else if (type == std::type_index(typeid(double))) {
ss << static_cast<const double*>(ptr)[offset];
} else if (type == std::type_index(typeid(bool))) {
ss << (static_cast<const bool*>(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
24 changes: 23 additions & 1 deletion src/flamegpu/visualiser/Visualiser.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <unordered_map>
#include <utility>
#include <map>
#include <cstdint>
#undef main // SDL breaks the regular main entry point, this fixes
#define GLM_FORCE_NO_CTOR_INIT
#include <glm/glm.hpp>
Expand Down Expand Up @@ -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<TexBufferConfig::Function, TexBufferConfig>& _core_tex_buffers, const std::multimap<TexBufferConfig::Function, CustomTexBufferConfig>& _tex_buffers);
/**
* Todo
*/
void registerEnvironmentProperty(const std::string& property_name, void* ptr, std::type_index type, unsigned int elements, bool is_const);

private:
void run();
Expand Down Expand Up @@ -231,10 +236,15 @@ 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();
void updateEnvironmentMenu();
SDL_Window *window;
SDL_Rect windowedBounds;
SDL_GLContext context;
Expand Down Expand Up @@ -304,11 +314,23 @@ class Visualiser : public ViewportExt {
* 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<std::string, EnvPropReference> env_properties;
std::shared_ptr<Text> 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)
Expand Down