]> git.tdb.fi Git - libs/gl.git/commitdiff
Add a texunit statement which automatically determines the unit number
authorMikko Rasa <tdb@tdb.fi>
Sun, 31 May 2020 09:43:03 +0000 (12:43 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sun, 31 May 2020 21:53:23 +0000 (00:53 +0300)
source/renderpass.cpp
source/renderpass.h
source/texturing.cpp
source/texturing.h

index 4e3b1f1332bbd0dcda294940821d61be091604f3..d2ce1652fa682138bd5592dd7698e7b4b01060d5 100644 (file)
@@ -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);
index 66768772f2d545a6e817c612a39873e5a9234b6d..d4f7a34d1a5887b12edb8af57bb81b8e2a5db462 100644 (file)
@@ -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 &);
index d8d178b385c05e54da6d588df0e4866e6da1a460..c30216f139ebbf91e4a7fdcb53021e1fa6cbc39f 100644 (file)
@@ -1,3 +1,4 @@
+#include <msp/core/hash.h>
 #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);
index 79647f1d95a5b0467a106ddac206066f28267111..cba77e3fb3e49a57e4ca98f7bb698f7ddf7f8489 100644 (file)
@@ -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: