From: Mikko Rasa Date: Mon, 25 Jan 2021 02:07:49 +0000 (+0200) Subject: Refactor setting uniform values in ProgramData X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=6a137d39cc846f973ae7a1e157c320c04d900dd0;p=libs%2Fgl.git Refactor setting uniform values in ProgramData This eliminates copies when changing values of existing uniforms. --- diff --git a/source/programdata.cpp b/source/programdata.cpp index 4d8ca4dd..5005373b 100644 --- a/source/programdata.cpp +++ b/source/programdata.cpp @@ -89,30 +89,86 @@ void ProgramData::uniform(const string &name, Uniform *uni) { try { - if(tied_program) - tied_program->get_uniform_info(name); - else if(name[name.size()-1]==']') - throw invalid_argument("ProgramData::uniform"); + if(!validate_name(name)) + { + delete uni; + return; + } } - catch(const exception &e) + catch(...) { delete uni; -#ifdef DEBUG - IO::print(IO::cerr, "Error while setting uniform %s: %s: %s\n", name, Debug::demangle(typeid(e).name()), e.what()); - return; -#else throw; -#endif } int i = find_uniform_index(name); - if(i>=0) - { - uniforms[i].replace_value(uni); - dirty |= 1< +void ProgramData::uniform(const string &name, V value) +{ + if(!validate_name(name)) return; + + int i = find_uniform_index(name); + if(i<0) + return add_uniform(name, new T(value)); + + if(T *uni = dynamic_cast(uniforms[i].value)) + uni->set(value); + else + uniforms[i].replace_value(new T(value)); + + dirty |= 1< +void ProgramData::uniform_array(const string &name, unsigned n, V value) +{ + if(!validate_name(name)) + return; + + int i = find_uniform_index(name); + if(i<0) + return add_uniform(name, new UniformArray(n, value)); + + UniformArray *uni = dynamic_cast *>(uniforms[i].value); + if(uni && n==uni->size()) + uni->set(value); + else + uniforms[i].replace_value(new UniformArray(n, value)); + + dirty |= 1<get_uniform_info(name); + else if(name[name.size()-1]==']') + throw invalid_argument("ProgramData::uniform"); + return true; } +#ifdef DEBUG + catch(const exception &e) + { + IO::print(IO::cerr, "Error while setting uniform %s: %s: %s\n", name, Debug::demangle(typeid(e).name()), e.what()); + return false; + } +#endif +} +void ProgramData::add_uniform(const string &name, Uniform *uni) +{ if(uniforms.size()>=MASK_BITS) { delete uni; @@ -136,12 +192,12 @@ void ProgramData::uniform(const string &name, const Uniform &u) void ProgramData::uniform(const string &name, int v) { - uniform(name, new Uniform1i(v)); + uniform(name, v); } void ProgramData::uniform(const string &name, float v) { - uniform(name, new Uniform1f(v)); + uniform(name, v); } void ProgramData::uniform(const string &name, int v0, int v1) @@ -158,12 +214,12 @@ void ProgramData::uniform(const string &name, float v0, float v1) void ProgramData::uniform2(const string &name, const int *v) { - uniform(name, new Uniform2i(v)); + uniform(name, v); } void ProgramData::uniform2(const string &name, const float *v) { - uniform(name, new Uniform2f(v)); + uniform(name, v); } void ProgramData::uniform(const string &name, int v0, int v1, int v2) @@ -185,12 +241,12 @@ void ProgramData::uniform(const string &name, const Vector3 &v) void ProgramData::uniform3(const string &name, const int *v) { - uniform(name, new Uniform3i(v)); + uniform(name, v); } void ProgramData::uniform3(const string &name, const float *v) { - uniform(name, new Uniform3f(v)); + uniform(name, v); } void ProgramData::uniform(const string &name, int v0, int v1, int v2, int v3) @@ -217,12 +273,12 @@ void ProgramData::uniform(const string &name, const Color &c) void ProgramData::uniform4(const string &name, const int *v) { - uniform(name, new Uniform4i(v)); + uniform(name, v); } void ProgramData::uniform4(const string &name, const float *v) { - uniform(name, new Uniform4f(v)); + uniform(name, v); } void ProgramData::uniform(const string &name, const LinAl::Matrix &m) @@ -232,7 +288,7 @@ void ProgramData::uniform(const string &name, const LinAl::Matrix & void ProgramData::uniform_matrix2(const string &name, const float *v) { - uniform(name, new UniformMatrix2x2f(v)); + uniform(name, v); } void ProgramData::uniform(const string &name, const LinAl::Matrix &m) @@ -242,7 +298,7 @@ void ProgramData::uniform(const string &name, const LinAl::Matrix & void ProgramData::uniform_matrix3x2(const string &name, const float *v) { - uniform(name, new UniformMatrix3x2f(v)); + uniform(name, v); } void ProgramData::uniform(const string &name, const LinAl::Matrix &m) @@ -252,7 +308,7 @@ void ProgramData::uniform(const string &name, const LinAl::Matrix & void ProgramData::uniform_matrix4x2(const string &name, const float *v) { - uniform(name, new UniformMatrix4x2f(v)); + uniform(name, v); } void ProgramData::uniform(const string &name, const LinAl::Matrix &m) @@ -262,7 +318,7 @@ void ProgramData::uniform(const string &name, const LinAl::Matrix & void ProgramData::uniform_matrix2x3(const string &name, const float *v) { - uniform(name, new UniformMatrix2x3f(v)); + uniform(name, v); } void ProgramData::uniform(const string &name, const LinAl::Matrix &m) @@ -272,7 +328,7 @@ void ProgramData::uniform(const string &name, const LinAl::Matrix & void ProgramData::uniform_matrix3(const string &name, const float *v) { - uniform(name, new UniformMatrix3x3f(v)); + uniform(name, v); } void ProgramData::uniform(const string &name, const LinAl::Matrix &m) @@ -282,7 +338,7 @@ void ProgramData::uniform(const string &name, const LinAl::Matrix & void ProgramData::uniform_matrix4x3(const string &name, const float *v) { - uniform(name, new UniformMatrix4x3f(v)); + uniform(name, v); } void ProgramData::uniform(const string &name, const LinAl::Matrix &m) @@ -292,7 +348,7 @@ void ProgramData::uniform(const string &name, const LinAl::Matrix & void ProgramData::uniform_matrix2x4(const string &name, const float *v) { - uniform(name, new UniformMatrix2x4f(v)); + uniform(name, v); } void ProgramData::uniform(const string &name, const LinAl::Matrix &m) @@ -302,7 +358,7 @@ void ProgramData::uniform(const string &name, const LinAl::Matrix & void ProgramData::uniform_matrix3x4(const string &name, const float *v) { - uniform(name, new UniformMatrix3x4f(v)); + uniform(name, v); } void ProgramData::uniform(const string &name, const Matrix &m) @@ -312,92 +368,92 @@ void ProgramData::uniform(const string &name, const Matrix &m) void ProgramData::uniform_matrix4(const string &name, const float *v) { - uniform(name, new UniformMatrix4x4f(v)); + uniform(name, v); } void ProgramData::uniform1_array(const string &name, unsigned n, const int *v) { - uniform(name, new UniformArray(n, v)); + uniform_array(name, n, v); } void ProgramData::uniform1_array(const string &name, unsigned n, const float *v) { - uniform(name, new UniformArray(n, v)); + uniform_array(name, n, v); } void ProgramData::uniform2_array(const string &name, unsigned n, const int *v) { - uniform(name, new UniformArray(n, v)); + uniform_array(name, n, v); } void ProgramData::uniform2_array(const string &name, unsigned n, const float *v) { - uniform(name, new UniformArray(n, v)); + uniform_array(name, n, v); } void ProgramData::uniform3_array(const string &name, unsigned n, const int *v) { - uniform(name, new UniformArray(n, v)); + uniform_array(name, n, v); } void ProgramData::uniform3_array(const string &name, unsigned n, const float *v) { - uniform(name, new UniformArray(n, v)); + uniform_array(name, n, v); } void ProgramData::uniform4_array(const string &name, unsigned n, const int *v) { - uniform(name, new UniformArray(n, v)); + uniform_array(name, n, v); } void ProgramData::uniform4_array(const string &name, unsigned n, const float *v) { - uniform(name, new UniformArray(n, v)); + uniform_array(name, n, v); } void ProgramData::uniform_matrix2_array(const string &name, unsigned n, const float *v) { - uniform(name, new UniformArray(n, v)); + uniform_array(name, n, v); } void ProgramData::uniform_matrix3x2_array(const string &name, unsigned n, const float *v) { - uniform(name, new UniformArray(n, v)); + uniform_array(name, n, v); } void ProgramData::uniform_matrix4x2_array(const string &name, unsigned n, const float *v) { - uniform(name, new UniformArray(n, v)); + uniform_array(name, n, v); } void ProgramData::uniform_matrix2x3_array(const string &name, unsigned n, const float *v) { - uniform(name, new UniformArray(n, v)); + uniform_array(name, n, v); } void ProgramData::uniform_matrix3_array(const string &name, unsigned n, const float *v) { - uniform(name, new UniformArray(n, v)); + uniform_array(name, n, v); } void ProgramData::uniform_matrix4x3_array(const string &name, unsigned n, const float *v) { - uniform(name, new UniformArray(n, v)); + uniform_array(name, n, v); } void ProgramData::uniform_matrix2x4_array(const string &name, unsigned n, const float *v) { - uniform(name, new UniformArray(n, v)); + uniform_array(name, n, v); } void ProgramData::uniform_matrix3x4_array(const string &name, unsigned n, const float *v) { - uniform(name, new UniformArray(n, v)); + uniform_array(name, n, v); } void ProgramData::uniform_matrix4_array(const string &name, unsigned n, const float *v) { - uniform(name, new UniformArray(n, v)); + uniform_array(name, n, v); } void ProgramData::remove_uniform(const string &name) diff --git a/source/programdata.h b/source/programdata.h index d9eaf746..e96a73ac 100644 --- a/source/programdata.h +++ b/source/programdata.h @@ -169,6 +169,12 @@ public: private: void uniform(const std::string &, Uniform *); + template + void uniform(const std::string &, V); + template + void uniform_array(const std::string &, unsigned, V); + bool validate_name(const std::string &) const; + void add_uniform(const std::string &, Uniform *); public: void uniform(const std::string &, const Uniform &); void uniform(const std::string &, int); diff --git a/source/uniform.h b/source/uniform.h index 5d288cf5..bc78bab8 100644 --- a/source/uniform.h +++ b/source/uniform.h @@ -36,6 +36,8 @@ private: public: UniformScalar(Type v): value(v) { } + void set(Type v) { value = v; } + Type get() const { return value; } virtual void apply(int index) const @@ -68,7 +70,9 @@ private: Type value; public: - UniformVector(const T *vp) + UniformVector(const T *vp) { set(vp); } + + void set(const T *vp) { std::copy(vp, vp+vecsize, value); } BaseType get(unsigned i) const { return value[i]; } @@ -107,7 +111,9 @@ private: Type value; public: - UniformMatrix(const T *vp) + UniformMatrix(const T *vp) { set(vp); } + + void set(const T *vp) { std::copy(vp, vp+rows*cols, value); } virtual void apply(int index) const @@ -147,15 +153,15 @@ private: typedef typename T::BaseType BaseType; enum { elemsize = sizeof(typename T::Type)/sizeof(typename T::BaseType) }; + unsigned size_; BaseType *values; - unsigned size; public: UniformArray(unsigned n, const BaseType *vp): - size(n) + size_(n), + values(new BaseType[elemsize*size_]) { - values = new BaseType[elemsize*size]; - std::copy(vp, vp+elemsize*size, values); + set(vp); } ~UniformArray() @@ -163,17 +169,22 @@ public: delete[] values; } + unsigned size() const { return size_; } + + void set(const BaseType *vp) + { std::copy(vp, vp+elemsize*size_, values); } + virtual void apply(int index) const - { T::apply(index, size, values); } + { T::apply(index, size_, values); } virtual void store(const Program::UniformInfo &info, void *buffer) const { - for(unsigned i=0; i(buffer)+i*info.array_stride, values+i*elemsize); } virtual UniformArray *clone() const - { return new UniformArray(size, values); } + { return new UniformArray(size_, values); } }; } // namespace GL