From: Mikko Rasa Date: Sun, 31 May 2020 09:43:03 +0000 (+0300) Subject: Add a texunit statement which automatically determines the unit number X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=commitdiff_plain;h=96e13fc706ba4e1702ff90eb999ee0660278cb0d Add a texunit statement which automatically determines the unit number --- diff --git a/source/renderpass.cpp b/source/renderpass.cpp index 4e3b1f13..d2ce1652 100644 --- a/source/renderpass.cpp +++ b/source/renderpass.cpp @@ -131,6 +131,7 @@ void RenderPass::Loader::init() add("material_slot", &RenderPass::material_slot); add("back_faces",&RenderPass::back_faces); add("texunit", &Loader::texunit); + add("texunit", &Loader::texunit_auto); add("texunit", &Loader::texunit_named); add("uniforms", &Loader::uniforms); add("uniform_slot", &Loader::uniform_slot); @@ -160,6 +161,16 @@ void RenderPass::Loader::texunit(unsigned i) load_sub_with(ldr); } +void RenderPass::Loader::texunit_auto(const string &n) +{ + if(!obj.texturing) + obj.texturing = new Texturing; + int i = obj.texturing->find_free_unit(n); + if(i<0) + throw runtime_error("no free texunit"); + texunit_named(i, n); +} + void RenderPass::Loader::texunit_named(unsigned i, const string &n) { texunit(i); diff --git a/source/renderpass.h b/source/renderpass.h index 66768772..d4f7a34d 100644 --- a/source/renderpass.h +++ b/source/renderpass.h @@ -31,9 +31,11 @@ public: private: void init(); + void material_inline(); void material(const std::string &); void texunit(unsigned); + void texunit_auto(const std::string &); void texunit_named(unsigned, const std::string &); void uniforms(); void uniform_slot(const std::string &); diff --git a/source/texturing.cpp b/source/texturing.cpp index d8d178b3..c30216f1 100644 --- a/source/texturing.cpp +++ b/source/texturing.cpp @@ -1,3 +1,4 @@ +#include #include "texture.h" #include "texturing.h" #include "texunit.h" @@ -13,6 +14,23 @@ Texturing::~Texturing() unbind(); } +int Texturing::find_free_unit(const string &name_hint) const +{ + unsigned max_unit = TexUnit::get_n_units(); + // Leave some space for effect textures + max_unit -= min(max_unit/4, 8U); + unsigned initial_unit = (name_hint.empty() ? 0 : hash32(name_hint)%max_unit); + unsigned unit = initial_unit; + while(get_attached_texture(unit)) + { + unit = (unit+1)%max_unit; + if(unit==initial_unit) + return -1; + } + + return unit; +} + void Texturing::attach(unsigned attch, const Texture &tex) { set_attachment(attch, &tex); diff --git a/source/texturing.h b/source/texturing.h index 79647f1d..cba77e3f 100644 --- a/source/texturing.h +++ b/source/texturing.h @@ -25,6 +25,7 @@ private: public: ~Texturing(); + int find_free_unit(const std::string & = std::string()) const; void attach(unsigned, const Texture &); void detach(unsigned); private: