From 7e7797e0fd16ee5f3959423d3e2eb7f8b04a6334 Mon Sep 17 00:00:00 2001 From: Patricio Gonzalez Vivo Date: Mon, 27 Mar 2023 04:40:02 -0400 Subject: [PATCH] CSV stremable uniforms --- src/core/tools/console.cpp | 7 +++ src/core/uniforms.cpp | 107 ++++++++++++++++++++++++++++++------- src/core/uniforms.h | 36 ++++++++----- src/main.cpp | 8 +++ 4 files changed, 126 insertions(+), 32 deletions(-) diff --git a/src/core/tools/console.cpp b/src/core/tools/console.cpp index ec69b74c..76fde370 100644 --- a/src/core/tools/console.cpp +++ b/src/core/tools/console.cpp @@ -149,6 +149,13 @@ void refresh_stt_win() { if (it->second.present && it->second.print) mvwprintw(stt_win, y++, stt_x, "%23s %s", it->first.c_str(), it->second.print().c_str() ); + for (UniformSequenceMap::iterator it= uniforms->sequences.begin(); it != uniforms->sequences.end(); ++it) { + if (it->second.size() > 0) { + size_t frame = uniforms->getFrame() % it->second.size(); + mvwprintw(stt_win, y++, stt_x, "%23s %s", it->first.c_str(), it->second[frame].print().c_str() ); + } + } + for (vera::TexturesMap::iterator it = uniforms->textures.begin(); it != uniforms->textures.end(); ++it) mvwprintw(stt_win, y++, stt_x, "%23s %.1f,%.1f", (it->first + "Resolution").c_str(), (float)it->second->getWidth(), (float)it->second->getHeight()); diff --git a/src/core/uniforms.cpp b/src/core/uniforms.cpp index b7026e2d..80a6bb72 100644 --- a/src/core/uniforms.cpp +++ b/src/core/uniforms.cpp @@ -1,6 +1,7 @@ #include "uniforms.h" #include +#include #include #include #include @@ -12,12 +13,13 @@ #include "vera/ops/string.h" #include "vera/xr/xr.h" + std::string UniformData::getType() { if (size == 1) return (bInt ? "int" : "float"); else return (bInt ? "ivec" : "vec") + vera::toString(size); } -void UniformData::set(const UniformValue &_value, size_t _size, bool _int, bool _queue ) { +void UniformData::set(const UniformValue &_value, size_t _size, bool _int, bool _queue) { bInt = _int; size = _size; @@ -29,12 +31,22 @@ void UniformData::set(const UniformValue &_value, size_t _size, bool _int, bool change = true; } -void UniformData::parse(const std::vector& _command, size_t _start) {; +void UniformData::parse(const std::vector& _command, size_t _start, bool _queue) {; UniformValue candidate; - for (size_t i = _start; i < _command.size() && i < 5; i++) + for (size_t i = _start; i < _command.size() && i < 5; i++) candidate[i-_start] = vera::toFloat(_command[i]); - set(candidate, _command.size() - _start, true); + set(candidate, _command.size() - _start, _queue); +} + +std::string UniformData::print() { + std::string rta = ""; + for (size_t i = 0; i < size; i++) { + rta += vera::toString(value[i]); + if (i < size - 1) + rta += ","; + } + return rta; } bool UniformData::check() { @@ -225,7 +237,7 @@ void Uniforms::clear() { bool Uniforms::feedTo(vera::Shader *_shader, bool _lights, bool _buffers ) { bool update = false; - // Pass Native uniforms + // Pass native uniforms functions (u_time, u_data, etc...) for (UniformFunctionsMap::iterator it = functions.begin(); it != functions.end(); ++it) { if (!_lights && ( it->first == "u_scene" || it->first == "u_sceneDepth" || it->first == "u_sceneNormal" || it->first == "u_scenePosition") ) continue; @@ -235,7 +247,7 @@ bool Uniforms::feedTo(vera::Shader *_shader, bool _lights, bool _buffers ) { it->second.assign( *_shader ); } - // Pass User defined uniforms + // Pass user defined uniforms (only if the shader code or scene had changed) if (m_change) { for (UniformDataMap::iterator it = data.begin(); it != data.end(); ++it) { if (it->second.change) { @@ -245,6 +257,15 @@ bool Uniforms::feedTo(vera::Shader *_shader, bool _lights, bool _buffers ) { } } + // Pass sequence uniforms (the change every frame) + for (UniformSequenceMap::iterator it = sequences.begin(); it != sequences.end(); ++it) { + if (it->second.size() > 0) { + size_t frame = m_frame % it->second.size(); + _shader->setUniform(it->first, it->second[frame].value.data(), it->second[frame].size); + update += true; + } + } + // Pass Textures Uniforms for (vera::TexturesMap::iterator it = textures.begin(); it != textures.end(); ++it) { _shader->setUniformTexture(it->first, it->second, _shader->textureIndex++ ); @@ -367,6 +388,18 @@ bool Uniforms::haveChange() { return false; } +void Uniforms::checkUniforms( const std::string &_vert_src, const std::string &_frag_src ) { + // Check active native uniforms + for (UniformFunctionsMap::iterator it = functions.begin(); it != functions.end(); ++it) { + std::string name = it->first + ";"; + std::string arrayName = it->first + "["; + bool present = ( findId(_vert_src, name.c_str()) || findId(_frag_src, name.c_str()) || findId(_frag_src, arrayName.c_str()) ); + if ( it->second.present != present ) { + it->second.present = present; + m_change = true; + } + } +} void Uniforms::set(const std::string& _name, float _value) { UniformValue value; @@ -422,17 +455,39 @@ bool Uniforms::parseLine( const std::string &_line ) { return false; } -void Uniforms::checkUniforms( const std::string &_vert_src, const std::string &_frag_src ) { - // Check active native uniforms - for (UniformFunctionsMap::iterator it = functions.begin(); it != functions.end(); ++it) { - std::string name = it->first + ";"; - std::string arrayName = it->first + "["; - bool present = ( findId(_vert_src, name.c_str()) || findId(_frag_src, name.c_str()) || findId(_frag_src, arrayName.c_str()) ); - if ( it->second.present != present ) { - it->second.present = present; - m_change = true; - } +bool Uniforms::addSequence( const std::string& _name, const std::string& _filename) { + std::cout << "Load values for " << _name << " from " << _filename << std::endl; + + std::vector uniform_data_sequence; + + // Open file _filename and read all lines + std::ifstream infile(_filename); + std::string line = ""; + while (std::getline(infile, line)) { + if (line.size() > 0) { + std::vector values = vera::split(line,',', true); + if (values.size() > 0) { + UniformData data; + data.parse(values, 0, false); + uniform_data_sequence.push_back(data); + } + } } + + if (uniform_data_sequence.size() == 0) + return false; + + sequences[_name] = uniform_data_sequence; + + return true; +} + +void Uniforms::update() { + Scene::update(); + + m_frame++; + if (m_frame >= std::numeric_limits::max()-1) + m_frame = 0; } @@ -469,15 +524,28 @@ void Uniforms::printDefinedUniforms(bool _csv){ } std::cout << std::endl; } + + for (UniformSequenceMap::iterator it= sequences.begin(); it != sequences.end(); ++it) { + if (it->second.size() > 0) { + size_t frame = m_frame % it->second.size(); + std::cout << it->first << ',' << it->second[frame].print() << std::endl; + } + } } else { for (UniformDataMap::iterator it= data.begin(); it != data.end(); ++it) { std::cout << "uniform " << it->second.getType() << " " << it->first << ";"; for (int i = 0; i < it->second.size; i++) std::cout << ((i == 0)? " // " : "," ) << it->second.value[i]; - std::cout << std::endl; } + + for (UniformSequenceMap::iterator it= sequences.begin(); it != sequences.end(); ++it) { + if (it->second.size() > 0) { + size_t frame = m_frame % it->second.size(); + std::cout << "uniform " << it->second[frame].getType() << " " << it->first << "; // " << it->second[frame].print() << std::endl; + } + } } } @@ -531,13 +599,14 @@ void Uniforms::clearBuffers() { void Uniforms::clearUniforms() { data.clear(); + sequences.clear(); for (UniformFunctionsMap::iterator it = functions.begin(); it != functions.end(); ++it) it->second.present = false; } -bool Uniforms::addCameraPath( const std::string& _name ) { - std::fstream is( _name.c_str(), std::ios::in); +bool Uniforms::addCameraPath( const std::string& _filename ) { + std::fstream is( _filename.c_str(), std::ios::in); if (is.is_open()) { glm::vec3 position; diff --git a/src/core/uniforms.h b/src/core/uniforms.h index 6b34a31d..4c186465 100644 --- a/src/core/uniforms.h +++ b/src/core/uniforms.h @@ -26,9 +26,10 @@ typedef std::array UniformValue; struct UniformData { std::string getType(); - void set(const UniformValue &_value, size_t _size, bool _int, bool _queue = true); - void parse(const std::vector& _command, size_t _start = 1); - bool check(); + void set(const UniformValue &_value, size_t _size, bool _int, bool _queue = true); + void parse(const std::vector& _command, size_t _start = 1, bool _queue = true); + std::string print(); + bool check(); std::queue queue; UniformValue value; @@ -50,8 +51,9 @@ struct UniformFunction { }; // Uniforms values types (float, vecs and functions) -typedef std::map UniformDataMap; -typedef std::map UniformFunctionsMap; +typedef std::map UniformFunctionsMap; +typedef std::map UniformDataMap; +typedef std::map> UniformSequenceMap; // Buffers types typedef std::vector BuffersList; @@ -90,28 +92,36 @@ class Uniforms : public vera::Scene { std::mutex loadMutex; ImagesMap loadQueue; - // Ingest new uniforms - // float, vec2, vec3, vec4 and functions (u_time, u_data, etc.) - UniformDataMap data; + // Uniforms that trigger functions (u_time, u_data, etc.) UniformFunctionsMap functions; + virtual void checkUniforms( const std::string &_vert_src, const std::string &_frag_src ); + + // Manually added uniforms + UniformDataMap data; virtual void set( const std::string& _name, float _value); virtual void set( const std::string& _name, float _x, float _y); virtual void set( const std::string& _name, float _x, float _y, float _z); virtual void set( const std::string& _name, float _x, float _y, float _z, float _w); virtual void set( const std::string& _name, const std::vector& _data, bool _queue = true); - virtual void checkUniforms( const std::string &_vert_src, const std::string &_frag_src ); virtual bool parseLine( const std::string &_line ); + + UniformSequenceMap sequences; + virtual bool addSequence( const std::string& _name, const std::string& _filename); + + CameraPath cameraPath; + virtual bool addCameraPath( const std::string& _filename ); + virtual void clearUniforms(); virtual void printAvailableUniforms(bool _non_active); virtual void printDefinedUniforms(bool _csv = false); - - - CameraPath cameraPath; - virtual bool addCameraPath( const std::string& _name ); Tracker tracker; + void update(); + size_t getFrame() const { return m_frame; } + protected: + size_t m_frame; bool m_change; }; diff --git a/src/main.cpp b/src/main.cpp index 00e5877e..420d0f38 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -323,6 +323,9 @@ int main(int argc, char **argv) { haveGeometry = true; } else if ( argument == "-noncurses"|| argument == "--noncurses" ) commands_ncurses = false; + else if ( vera::haveExt(argument,"csv") || vera::haveExt(argument,"csv") ) { + + } else if ( vera::haveExt(argument,"hdr") || vera::haveExt(argument,"HDR") || vera::haveExt(argument,"exr") || vera::haveExt(argument,"EXR") || vera::haveExt(argument,"png") || vera::haveExt(argument,"PNG") || @@ -677,6 +680,11 @@ int main(int argc, char **argv) { vera::haveWildcard(argument) ) { sandbox.uniforms.addStreamingTexture(parameterPair, argument, vFlip, false); } + + // Load a sequence of uniform data + else if ( vera::haveExt(argument,"csv") || vera::haveExt(argument,"CSV") ) + sandbox.uniforms.addSequence(parameterPair, argument); + // Else load it as a single texture else sandbox.uniforms.addTexture(parameterPair, argument, vFlip);