]> git.tdb.fi Git - libs/gl.git/commitdiff
Store uniform information inside Program to reduce glGetUniformLocation calls
authorMikko Rasa <tdb@tdb.fi>
Tue, 10 May 2011 07:37:55 +0000 (07:37 +0000)
committerMikko Rasa <tdb@tdb.fi>
Tue, 10 May 2011 07:37:55 +0000 (07:37 +0000)
source/program.cpp
source/program.h

index 77fe7e7510d9e09597712803ade770267486b26e..1343c00f48cf0556984d65ff7db245a7628d3b7e 100644 (file)
@@ -215,11 +215,28 @@ void Program::link()
                if(!(*i)->is_compiled())
                        (*i)->compile();
 
+       uniforms.clear();
+
        glLinkProgramARB(id);
        int value;
        glGetObjectParameterivARB(id, GL_OBJECT_LINK_STATUS_ARB, &value);
        if(!(linked = value))
                throw CompileError(get_info_log());
+
+       glGetObjectParameterivARB(id, GL_OBJECT_ACTIVE_UNIFORMS_ARB, &value);
+       for(int i=0; i<value; ++i)
+       {
+               UniformInfo info;
+               char name[128];
+               int len = 0;
+               glGetActiveUniformARB(id, i, 128, &len, &info.size, &info.type, name);
+               if(len)
+               {
+                       info.name = name;
+                       info.location = glGetUniformLocationARB(id, name);
+                       uniforms[name] = info;
+               }
+       }
 }
 
 string Program::get_info_log() const
@@ -244,7 +261,11 @@ void Program::bind() const
 
 int Program::get_uniform_location(const string &n) const
 {
-       return glGetUniformLocationARB(id, n.c_str());
+       map<string, UniformInfo>::const_iterator i = uniforms.find(n);
+       if(i==uniforms.end())
+               return -1;
+
+       return i->second.location;
 }
 
 void Program::unbind()
index 4ff3f53a41c149a2e2d411d25cd52f4ae6873260..80ddf15ca33e282ff71a17cd79695db4fd324972 100644 (file)
@@ -56,11 +56,20 @@ public:
                std::string create_flags() const;
        };
 
+       struct UniformInfo
+       {
+               std::string name;
+               int location;
+               int size;
+               GLenum type;
+       };
+
 private:
        unsigned id;
        std::list<Shader *> shaders;
        bool del_shaders;
        bool linked;
+       std::map<std::string, UniformInfo> uniforms;
 
 public:
        Program();