]> git.tdb.fi Git - libs/gl.git/blobdiff - source/programdata.cpp
Support loading uniform arrays from data files
[libs/gl.git] / source / programdata.cpp
index 7705fac9786420bf47b5fc02edf088cd54b64ec5..d0de3c05565d182663e099f2fde904afa32ed4b8 100644 (file)
@@ -21,6 +21,7 @@ ProgramData::ProgramData():
 
 // Blocks are intentionally left uncopied
 ProgramData::ProgramData(const ProgramData &other):
+       uniform_slots(other.uniform_slots),
        uniforms(other.uniforms),
        last_block(0),
        buffer(0),
@@ -36,6 +37,7 @@ ProgramData &ProgramData::operator=(const ProgramData &other)
                delete *i;
        uniforms.clear();
 
+       uniform_slots = other.uniform_slots;
        for(vector<Uniform *>::const_iterator i=other.uniforms.begin(); i!=other.uniforms.end(); ++i)
                uniforms.push_back((*i)->clone());
 
@@ -61,6 +63,9 @@ ProgramData::~ProgramData()
 
 void ProgramData::uniform(const string &name, Uniform *uni)
 {
+       if(name[name.size()-1]==']')
+               throw invalid_argument("ProgramData::uniform");
+
        SlotMap::iterator i = uniform_slots.find(name);
        if(i!=uniform_slots.end())
        {
@@ -142,11 +147,21 @@ void ProgramData::uniform4(const string &name, const float *v)
        uniform(name, new Uniform4f(v));
 }
 
+void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 2, 2> &m)
+{
+       uniform_matrix2(name, &m(0, 0));
+}
+
 void ProgramData::uniform_matrix2(const string &name, const float *v)
 {
        uniform(name, new UniformMatrix2x2f(v));
 }
 
+void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 3, 3> &m)
+{
+       uniform_matrix3(name, &m(0, 0));
+}
+
 void ProgramData::uniform_matrix3(const string &name, const float *v)
 {
        uniform(name, new UniformMatrix3x3f(v));
@@ -162,6 +177,11 @@ void ProgramData::uniform_matrix4(const string &name, const float *v)
        uniform(name, new UniformMatrix4x4f(v));
 }
 
+void ProgramData::uniform1_array(const string &name, unsigned n, const int *v)
+{
+       uniform(name, new UniformArray<Uniform1i>(n, v));
+}
+
 void ProgramData::uniform1_array(const string &name, unsigned n, const float *v)
 {
        uniform(name, new UniformArray<Uniform1f>(n, v));
@@ -250,14 +270,15 @@ void ProgramData::apply() const
        Program::LayoutHash layout = prog->get_uniform_layout_hash();
        ProgramUniforms &pu = programs[layout];
 
-       if((dirty&pu.used)|pu.dirty)
+       Mask force_dirty = (dirty==ALL_ONES ? ALL_ONES : 0U);
+       Mask affected = (dirty&pu.used) | force_dirty;
+       if(affected|pu.dirty)
        {
                /* If the global dirty flag affects this program, add it to per-program
                dirty flags and clear the global flag.  A previously unseen program will
                always cause this to happen. */
-               if(dirty&pu.used)
+               if(affected)
                {
-                       Mask force_dirty = (dirty==ALL_ONES ? ALL_ONES : 0U);
                        for(BlockMap::iterator i=blocks.begin(); i!=blocks.end(); ++i)
                                i->second.dirty |= (dirty&i->second.used) | force_dirty;
                        for(ProgramMap::iterator i=programs.begin(); i!=programs.end(); ++i)
@@ -345,11 +366,22 @@ ProgramData::ProgramUniforms::ProgramUniforms():
 ProgramData::Loader::Loader(ProgramData &pd):
        DataFile::ObjectLoader<ProgramData>(pd)
 {
+       add("uniform", &Loader::uniform1i);
        add("uniform1i", &Loader::uniform1i);
+       add("uniform", &Loader::uniform1f);
        add("uniform1f", &Loader::uniform1f);
+       add("uniform", &Loader::uniform2f);
        add("uniform2f", &Loader::uniform2f);
+       add("uniform", &Loader::uniform3f);
        add("uniform3f", &Loader::uniform3f);
+       add("uniform", &Loader::uniform4f);
        add("uniform4f", &Loader::uniform4f);
+       add("uniform1i_array", &Loader::uniform1i_array);
+       add("uniform1f_array", &Loader::uniform1f_array);
+       add("uniform2f_array", &Loader::uniform2f_array);
+       add("uniform3f_array", &Loader::uniform3f_array);
+       add("uniform4f_array", &Loader::uniform4f_array);
+       add("uniform_array", &Loader::uniform_array);
 }
 
 void ProgramData::Loader::uniform1i(const string &n, int v)
@@ -377,5 +409,131 @@ void ProgramData::Loader::uniform4f(const string &n, float v0, float v1, float v
        obj.uniform(n, v0, v1, v2, v3);
 }
 
+void ProgramData::Loader::uniform_array_(const string &n, DataType t, unsigned e)
+{
+       ArrayLoader ldr(t, e);
+       load_sub_with(ldr);
+       unsigned size = ldr.get_size();
+       if(!size)
+               throw logic_error("empty uniform array");
+
+       DataType type = ldr.get_data_type();
+       unsigned elem_size = ldr.get_element_size();
+       if(type==INT)
+       {
+               const int *data = reinterpret_cast<const int *>(ldr.get_data());
+               if(elem_size==1)
+                       obj.uniform1_array(n, size, data);
+               else
+                       throw logic_error("unsupported combination of array type and element size");
+       }
+       else if(type==FLOAT)
+       {
+               const float *data = reinterpret_cast<const float *>(ldr.get_data());
+               if(elem_size==1)
+                       obj.uniform1_array(n, size, data);
+               else if(elem_size==2)
+                       obj.uniform2_array(n, size, data);
+               else if(elem_size==3)
+                       obj.uniform3_array(n, size, data);
+               else if(elem_size==4)
+                       obj.uniform4_array(n, size, data);
+               else
+                       throw logic_error("unsupported combination of array type and element size");
+       }
+       else
+               throw logic_error("unsupported array type");
+}
+
+void ProgramData::Loader::uniform1i_array(const string &n)
+{
+       uniform_array_(n, INT, 1);
+}
+
+void ProgramData::Loader::uniform1f_array(const string &n)
+{
+       uniform_array_(n, FLOAT, 1);
+}
+
+void ProgramData::Loader::uniform2f_array(const string &n)
+{
+       uniform_array_(n, FLOAT, 2);
+}
+
+void ProgramData::Loader::uniform3f_array(const string &n)
+{
+       uniform_array_(n, FLOAT, 3);
+}
+
+void ProgramData::Loader::uniform4f_array(const string &n)
+{
+       uniform_array_(n, FLOAT, 4);
+}
+
+void ProgramData::Loader::uniform_array(const string &n)
+{
+       uniform_array_(n, static_cast<DataType>(0), 0);
+}
+
+
+ProgramData::ArrayLoader::ArrayLoader(DataType t, unsigned e):
+       type(t),
+       element_size(e)
+{
+       add("uniform", &ArrayLoader::uniform1i);
+       add("uniform1i", &ArrayLoader::uniform1i);
+       add("uniform", &ArrayLoader::uniform1f);
+       add("uniform1f", &ArrayLoader::uniform1f);
+       add("uniform", &ArrayLoader::uniform2f);
+       add("uniform2f", &ArrayLoader::uniform2f);
+       add("uniform", &ArrayLoader::uniform3f);
+       add("uniform3f", &ArrayLoader::uniform3f);
+       add("uniform", &ArrayLoader::uniform4f);
+       add("uniform4f", &ArrayLoader::uniform4f);
+}
+
+void ProgramData::ArrayLoader::uniform(DataType t, unsigned e, const void *v)
+{
+       if(element_size && (t!=type || e!=element_size))
+               throw logic_error("heterogeneous array contents");
+
+       if(!element_size)
+       {
+               type = t;
+               element_size = e;
+       }
+
+       const char *cv = reinterpret_cast<const char *>(v);
+       data.insert(data.end(), cv, cv+element_size*4);
+}
+
+void ProgramData::ArrayLoader::uniform1i(int v)
+{
+       uniform(INT, 1, &v);
+}
+
+void ProgramData::ArrayLoader::uniform1f(float v)
+{
+       uniform(FLOAT, 1, &v);
+}
+
+void ProgramData::ArrayLoader::uniform2f(float v0, float v1)
+{
+       float va[2] = { v0, v1 };
+       uniform(FLOAT, 2, va);
+}
+
+void ProgramData::ArrayLoader::uniform3f(float v0, float v1, float v2)
+{
+       float va[3] = { v0, v1, v2 };
+       uniform(FLOAT, 3, va);
+}
+
+void ProgramData::ArrayLoader::uniform4f(float v0, float v1, float v2, float v3)
+{
+       float va[4] = { v0, v1, v2, v3 };
+       uniform(FLOAT, 4, va);
+}
+
 } // namespace GL
 } // namespace Msp