From 4af69ec90120a0be828a1ae475a38674087110b5 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Thu, 11 Jun 2020 14:13:38 +0300 Subject: [PATCH] Check uniforms as a hint for texunit to use for material textures --- source/material.cpp | 11 ++++++++++- source/programdata.cpp | 6 ++++++ source/programdata.h | 1 + source/uniform.h | 4 ++++ 4 files changed, 21 insertions(+), 1 deletion(-) 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); } -- 2.43.0