Skip to content

Commit

Permalink
CSV stremable uniforms
Browse files Browse the repository at this point in the history
  • Loading branch information
patriciogonzalezvivo committed Mar 27, 2023
1 parent e924077 commit 7e7797e
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 32 deletions.
7 changes: 7 additions & 0 deletions src/core/tools/console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
107 changes: 88 additions & 19 deletions src/core/uniforms.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "uniforms.h"

#include <regex>
#include <limits>
#include <fstream>
#include <sstream>
#include <sys/stat.h>
Expand All @@ -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;

Expand All @@ -29,12 +31,22 @@ void UniformData::set(const UniformValue &_value, size_t _size, bool _int, bool
change = true;
}

void UniformData::parse(const std::vector<std::string>& _command, size_t _start) {;
void UniformData::parse(const std::vector<std::string>& _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() {
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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++ );
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<UniformData> 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<std::string> 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<size_t>::max()-1)
m_frame = 0;
}


Expand Down Expand Up @@ -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;
}
}
}
}

Expand Down Expand Up @@ -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;
Expand Down
36 changes: 23 additions & 13 deletions src/core/uniforms.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ typedef std::array<float, 4> UniformValue;
struct UniformData {
std::string getType();

void set(const UniformValue &_value, size_t _size, bool _int, bool _queue = true);
void parse(const std::vector<std::string>& _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<std::string>& _command, size_t _start = 1, bool _queue = true);
std::string print();
bool check();

std::queue<UniformValue> queue;
UniformValue value;
Expand All @@ -50,8 +51,9 @@ struct UniformFunction {
};

// Uniforms values types (float, vecs and functions)
typedef std::map<std::string, UniformData> UniformDataMap;
typedef std::map<std::string, UniformFunction> UniformFunctionsMap;
typedef std::map<std::string, UniformFunction> UniformFunctionsMap;
typedef std::map<std::string, UniformData> UniformDataMap;
typedef std::map<std::string, std::vector<UniformData>> UniformSequenceMap;

// Buffers types
typedef std::vector<vera::Fbo*> BuffersList;
Expand Down Expand Up @@ -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<float>& _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;

};
Expand Down
8 changes: 8 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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") ||
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 7e7797e

Please sign in to comment.