]> git.tdb.fi Git - libs/gl.git/commitdiff
Check uniforms as a hint for texunit to use for material textures
authorMikko Rasa <tdb@tdb.fi>
Thu, 11 Jun 2020 11:13:38 +0000 (14:13 +0300)
committerMikko Rasa <tdb@tdb.fi>
Thu, 11 Jun 2020 15:38:44 +0000 (18:38 +0300)
source/material.cpp
source/programdata.cpp
source/programdata.h
source/uniform.h

index 8bd6e54ca51b4bf1a48d5b86c07da3fe075d76c5..b0f62bbc5fbef9f1d07b7bcfd4b688135394c32e 100644 (file)
@@ -2,6 +2,7 @@
 #include "gl.h"
 #include "resources.h"
 #include "texturing.h"
+#include "uniform.h"
 
 using namespace std;
 
@@ -13,9 +14,17 @@ void Material::attach_texture_to(const Texture *tex, Texturing &texturing, Progr
        if(!tex)
                return;
 
-       int unit = texturing.find_free_unit(name);
+       int unit = -1;
+
+       if(const Uniform *uni = tex_shdata.find_uniform(name))
+               if(const Uniform1i *uni_int = dynamic_cast<const Uniform1i *>(uni))
+                       unit = uni_int->get();
+
+       if(unit<0)
+               unit = texturing.find_free_unit(name);
        if(unit<0)
                throw runtime_error("no free texunit");
+
        texturing.attach(unit, *tex);
        tex_shdata.uniform(name, unit);
 }
index 3561719725501a4ca7663ad4ae6c3e825e6bd10b..68820c59cc9fd0fd829792ebf547190d23c0fe9f 100644 (file)
@@ -421,6 +421,12 @@ const Uniform &ProgramData::get_uniform(const string &name) const
        return *uniforms[i].value;
 }
 
+const Uniform *ProgramData::find_uniform(const string &name) const
+{
+       int i = find_uniform_index(name);
+       return (i>=0 ? uniforms[i].value : 0);
+}
+
 bool ProgramData::uniform_name_compare(const NamedUniform &nu, const string &name)
 {
        return nu.name<name;
index bc2289b52965a4e29262c1754139bec7efdbd33c..c367097bd886752fc94697ebfeb5c013acdffb64 100644 (file)
@@ -219,6 +219,7 @@ public:
 
        std::vector<std::string> get_uniform_names() const;
        const Uniform &get_uniform(const std::string &) const;
+       const Uniform *find_uniform(const std::string &) const;
 
 private:
        static bool uniform_name_compare(const NamedUniform &, const std::string &);
index ad2d88cc477715021baea7a4f7df4798b43173e7..5d288cf50e29485701bb125830aebe4504d0ae07 100644 (file)
@@ -36,6 +36,8 @@ private:
 public:
        UniformScalar(Type v): value(v) { }
 
+       Type get() const { return value; }
+
        virtual void apply(int index) const
        { apply(index, 1, &value); }
 
@@ -69,6 +71,8 @@ public:
        UniformVector(const T *vp)
        { std::copy(vp, vp+vecsize, value); }
 
+       BaseType get(unsigned i) const { return value[i]; }
+
        virtual void apply(int index) const
        { apply(index, 1, value); }