]> git.tdb.fi Git - libs/gl.git/blobdiff - source/materials/renderpass.cpp
Overhaul texture management in rendering classes
[libs/gl.git] / source / materials / renderpass.cpp
index 7431a4461d9977fe6ef601019a12a5705bc63d98..702bddeaab393034521ccfb93a78f1152218b697 100644 (file)
@@ -1,8 +1,8 @@
+#include <msp/core/algorithm.h>
 #include <msp/datafile/collection.h>
 #include <msp/io/print.h>
 #include <msp/strings/format.h>
 #include "error.h"
-#include "material.h"
 #include "renderpass.h"
 #include "program.h"
 #include "programdata.h"
@@ -10,6 +10,7 @@
 #include "texture.h"
 #include "texture2d.h"
 #include "texturing.h"
+#include "uniform.h"
 
 using namespace std;
 
@@ -21,49 +22,17 @@ RenderPass::RenderPass():
        shprog_from_material(false),
        shdata(0),
        material(0),
-       texturing(0),
        back_faces(false)
 { }
 
-RenderPass::RenderPass(const RenderPass &other):
-       shprog(other.shprog),
-       shprog_from_material(other.shprog_from_material),
-       shdata(other.shdata),
-       uniform_slots(other.uniform_slots),
-       material(other.material),
-       material_slot(other.material_slot),
-       texturing(other.texturing ? new Texturing(*other.texturing) : 0),
-       tex_names(other.tex_names),
-       back_faces(other.back_faces)
-{ }
-
-RenderPass &RenderPass::operator=(const RenderPass &other)
-{
-       shprog = other.shprog;
-       shprog_from_material = other.shprog_from_material;
-       shdata = other.shdata;
-       uniform_slots = other.uniform_slots;
-       material = other.material;
-       material_slot = other.material_slot;
-       texturing = other.texturing ? new Texturing(*other.texturing) : 0;
-       tex_names = other.tex_names;
-       back_faces = other.back_faces;
-       return *this;
-}
-
-RenderPass::~RenderPass()
-{
-       delete texturing;
-}
-
 void RenderPass::finalize_material(DataFile::Collection *coll)
 {
        maybe_create_material_shader(coll);
        ensure_private_shader_data();
 
-       if(!texturing)
-               texturing = new Texturing;
-       material->attach_textures_to(*texturing, *shdata);
+       const Tag *material_texture_tags = material->get_texture_tags();
+       for(const Tag *tag=material_texture_tags; tag->id; ++tag)
+               set_texture(*tag, material->get_texture(*tag), material->get_sampler());
 }
 
 void RenderPass::maybe_create_material_shader(DataFile::Collection *coll)
@@ -121,25 +90,60 @@ void RenderPass::set_material(const Material *mat, DataFile::Collection *coll)
        finalize_material(coll);
 }
 
+void RenderPass::set_texture(Tag tag, const Texture *tex, const Sampler *samp)
+{
+       vector<TextureSlot>::iterator i = find_member(textures, tag, &TextureSlot::tag);
+       if(i==textures.end())
+       {
+               textures.push_back(TextureSlot(tag));
+               i = textures.end()-1;
+       }
+       i->texture = tex;
+       if(samp)
+               i->sampler = samp;
+}
+
+Tag RenderPass::get_texture_tag(const string &slot) const
+{
+       vector<TextureSlot>::const_iterator i = find_member(textures, slot, &TextureSlot::slot_name);
+       return (i!=textures.end() ? i->tag : Tag());
+}
+
 void RenderPass::set_texture(unsigned index, const Texture *tex, const Sampler *samp)
 {
-       if(!texturing)
-               texturing = new Texturing;
+       if(!shprog)
+               throw invalid_operation("RenderPass::set_texture");
+
+       const vector<Program::UniformInfo> &uniforms = shprog->get_uniforms();
+       for(vector<Program::UniformInfo>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
+               if(is_image(i->type) && i->binding==static_cast<int>(index))
+                       return set_texture(i->tag, tex, samp);
 
-       texturing->attach(index, *tex, (samp ? samp : texturing->get_attached_sampler(index)));
+       if(shdata)
+       {
+               const vector<Tag> &tags = shdata->get_uniform_tags();
+               for(vector<Tag>::const_iterator i=tags.begin(); i!=tags.end(); ++i)
+               {
+                       vector<Program::UniformInfo>::const_iterator j = find_member(uniforms, *i, &Program::UniformInfo::tag);
+                       if(j==uniforms.end() || !is_image(j->type))
+                               continue;
+                       if(const Uniform1i *uni1i = dynamic_cast<const Uniform1i *>(shdata->find_uniform(*i)))
+                               if(uni1i->get()==static_cast<int>(index))
+                                       return set_texture(*i, tex, samp);
+               }
+       }
 }
 
 int RenderPass::get_texture_index(const string &n) const
 {
-       map<string, unsigned>::const_iterator i = tex_names.find(n);
-       if(i==tex_names.end())
-               return -1;
-       return i->second;
+       vector<TextureSlot>::const_iterator i = find_member(textures, n, &TextureSlot::slot_name);
+       return (shprog && i!=textures.end() ? shprog->get_uniform_binding(i->tag) : -1);
 }
 
 void RenderPass::apply(Renderer &renderer) const
 {
-       renderer.set_texturing(texturing);
+       for(vector<TextureSlot>::const_iterator i=textures.begin(); i!=textures.end(); ++i)
+               renderer.set_texture(i->tag, i->texture, i->sampler);
        renderer.set_material(material.get());
        renderer.set_shader_program(shprog.get(), shdata.get());
        renderer.set_reverse_winding(back_faces);
@@ -165,12 +169,15 @@ void RenderPass::Loader::init()
        add("material", &Loader::material);
        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("texture", &Loader::texture);
        add("uniforms", &Loader::uniforms);
        add("uniform_slot", &Loader::uniform_slot);
        add("uniform_slot", &Loader::uniform_slot2);
+
+       // Deprecated
+       add("texunit",  &Loader::texunit);
+       add("texunit",  &Loader::texture);
+       add("texunit",  &Loader::texunit_named);
 }
 
 // Temporary compatibility feature
@@ -210,30 +217,46 @@ void RenderPass::Loader::shader(const string &n)
                obj.finalize_material(coll);
 }
 
-void RenderPass::Loader::texunit(unsigned i)
+void RenderPass::Loader::texture(const string &n)
 {
-       if(!obj.texturing)
-               obj.texturing = new Texturing;
-       TextureLoader ldr(*obj.texturing, i, coll);
+       vector<TextureSlot>::iterator i = find_member(obj.textures, Tag(n), &TextureSlot::tag);
+       if(i==obj.textures.end())
+       {
+               obj.textures.push_back(TextureSlot(n));
+               i = obj.textures.end()-1;
+       }
+       TextureSlot::Loader ldr(*i, n, coll);
        load_sub_with(ldr);
 }
 
-void RenderPass::Loader::texunit_auto(const string &n)
+void RenderPass::Loader::texunit(unsigned)
 {
-       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);
+       IO::print(IO::cerr, "Warning: specifying textures by unit number is deprecated and may not produce expected results");
+       string name;
+       if(obj.shprog)
+       {
+               const vector<Program::UniformInfo> &uniforms = obj.shprog->get_uniforms();
+               for(vector<Program::UniformInfo>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
+                       if(is_image(i->type) && i->binding>=0)
+                       {
+                               if(!name.empty())
+                               {
+                                       name.clear();
+                                       break;
+                               }
+                               name = i->name;
+                       }
+       }
+
+       if(name.empty())
+               throw runtime_error("Could not determine name for texture");
+
+       texture(name);
 }
 
-void RenderPass::Loader::texunit_named(unsigned i, const string &n)
+void RenderPass::Loader::texunit_named(unsigned, const string &n)
 {
-       texunit(i);
-       obj.tex_names[n] = i;
-       obj.ensure_private_shader_data();
-       obj.shdata->uniform(n, static_cast<int>(i));
+       texture(n);
 }
 
 void RenderPass::Loader::uniforms()
@@ -253,32 +276,19 @@ void RenderPass::Loader::uniform_slot2(const string &name, const string &slot)
 }
 
 
-RenderPass::TextureLoader::TextureLoader(Texturing &t, unsigned i, Collection *c):
-       DataFile::CollectionObjectLoader<Texturing>(t, c),
-       index(i),
-       tex(0),
-       samp(0)
-{
-       add("sampler",   &TextureLoader::sampler);
-       add("texture",   &TextureLoader::texture);
-}
-
-void RenderPass::TextureLoader::finish()
-{
-       if(tex)
-               obj.attach(index, *tex, samp);
-       else if(samp)
-               obj.attach(index, *samp);
-}
-
-void RenderPass::TextureLoader::sampler(const string &name)
+RenderPass::TextureSlot::Loader::Loader(TextureSlot &ts, const string &an, Collection *c):
+       CollectionObjectLoader<TextureSlot>(ts, c),
+       auto_slot_name(an)
 {
-       samp = &get_collection().get<Sampler>(name);
+       add("sampler", &TextureSlot::sampler);
+       add("slot", &Loader::slot_auto);
+       add("slot", &TextureSlot::slot_name);
+       add("texture", &TextureSlot::texture);
 }
 
-void RenderPass::TextureLoader::texture(const string &name)
+void RenderPass::TextureSlot::Loader::slot_auto()
 {
-       tex = &get_collection().get<Texture>(name);
+       obj.slot_name = auto_slot_name;
 }
 
 } // namespace GL