From: Mikko Rasa Date: Thu, 11 Jun 2020 11:13:38 +0000 (+0300) Subject: Check uniforms as a hint for texunit to use for material textures X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=4af69ec90120a0be828a1ae475a38674087110b5;p=libs%2Fgl.git Check uniforms as a hint for texunit to use for material textures --- diff --git a/source/material.cpp b/source/material.cpp index 8bd6e54c..b0f62bbc 100644 --- a/source/material.cpp +++ b/source/material.cpp @@ -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(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); } diff --git a/source/programdata.cpp b/source/programdata.cpp index 35617197..68820c59 100644 --- a/source/programdata.cpp +++ b/source/programdata.cpp @@ -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 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 &); diff --git a/source/uniform.h b/source/uniform.h index ad2d88cc..5d288cf5 100644 --- a/source/uniform.h +++ b/source/uniform.h @@ -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); }