]> git.tdb.fi Git - libs/gl.git/blobdiff - source/renderpass.cpp
Fix various issues with constant condition elimination
[libs/gl.git] / source / renderpass.cpp
index 70fa546afcddc0a0f2bcafb04da1313ddbd2b85b..be5bb170280844d88c8b3e8f46fd22cb72e902de 100644 (file)
@@ -1,18 +1,14 @@
-/* $Id$
-
-This file is part of libmspgl
-Copyright © 2007  Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
-#include <msp/core/refptr.h>
 #include <msp/datafile/collection.h>
-#include <msp/strings/formatter.h>
+#include <msp/strings/format.h>
+#include "error.h"
 #include "material.h"
 #include "renderpass.h"
 #include "program.h"
 #include "programdata.h"
+#include "renderer.h"
 #include "texture.h"
+#include "texture2d.h"
+#include "texturing.h"
 
 using namespace std;
 
@@ -22,87 +18,85 @@ namespace GL {
 RenderPass::RenderPass():
        shprog(0),
        shdata(0),
-       own_material(false),
-       material(0)
+       material(0),
+       texturing(0),
+       back_faces(false)
 { }
 
 RenderPass::RenderPass(const RenderPass &other):
        shprog(other.shprog),
-       shdata(other.shdata ? new ProgramData(*other.shdata) : 0),
-       own_material(other.own_material),
-       material(own_material ? new Material(*other.material) : other.material),
-       textures(other.textures)
+       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()
+RenderPass &RenderPass::operator=(const RenderPass &other)
 {
-       delete shdata;
-       if(own_material)
-               delete material;
+       shprog = other.shprog;
+       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;
 }
 
-void RenderPass::set_material(const Material *mat)
+RenderPass::~RenderPass()
 {
-       material = mat;
+       delete texturing;
 }
 
-unsigned RenderPass::get_texture_index(const string &slot) const
+void RenderPass::set_shader_program(const Program *prog, const ProgramData *data)
 {
-       for(unsigned i=0; i<textures.size(); ++i)
-               if(textures[i].name==slot)
-                       return i;
-
-       throw KeyError("Unknown texture slot", slot);
+       shprog = prog;
+       shdata = (data ? new ProgramData(*data) : 0);
 }
 
-void RenderPass::set_texture(const string &slot, const Texture *tex)
+const string &RenderPass::get_slotted_uniform_name(const string &slot) const
 {
-       textures[get_texture_index(slot)] = tex;
+       map<string, string>::const_iterator i = uniform_slots.find(slot);
+       if(i==uniform_slots.end())
+       {
+               static string empty;
+               return empty;
+       }
+       return i->second;
 }
 
-void RenderPass::bind() const
+void RenderPass::set_material(const Material *mat)
 {
-       const RenderPass *old = current();
-       if(!set_current(this))
-               return;
-
-       if(shprog)
-       {
-               shprog->bind();
-               shdata->apply();
-       }
-       else if(old && old->shprog)
-               GL::Program::unbind();
-
-       if(material)
-               material->bind();
-       else if(old && old->material)
-               GL::Material::unbind();
-
-       for(unsigned i=0; i<textures.size(); ++i)
-               if(textures[i].texture)
-                       textures[i].texture->bind_to(i);
-       if(old)
-       {
-               for(unsigned i=textures.size(); i<old->textures.size(); ++i)
-                       GL::Texture::unbind_from(i);
-       }
+       material = mat;
+       material.keep();
 }
 
-void RenderPass::unbind()
+void RenderPass::set_texture(unsigned index, const Texture *tex)
 {
-       const RenderPass *old = current();
-       if(!set_current(0))
-               return;
+       if(!texturing)
+               texturing = new Texturing;
 
-       if(old->shprog)
-               GL::Program::unbind();
+       texturing->attach(index, *tex);
+}
 
-       if(old->material)
-               GL::Material::unbind();
+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;
+}
 
-       for(unsigned i=old->textures.size(); i--; )
-               GL::Texture::unbind_from(i);
+void RenderPass::apply(Renderer &renderer) const
+{
+       renderer.set_texturing(texturing);
+       renderer.set_material(material.get());
+       renderer.set_shader_program(shprog, shdata.get());
+       renderer.set_reverse_winding(back_faces);
 }
 
 
@@ -120,69 +114,97 @@ RenderPass::Loader::Loader(RenderPass &p, Collection &c):
 
 void RenderPass::Loader::init()
 {
-       allow_pointer_reload = false;
-
        add("shader",   &RenderPass::shprog);
+       add("material", &Loader::material_inline);
        add("material", &Loader::material);
-       add("material", &RenderPass::material);
-       add("texture",  &Loader::texture);
+       add("material_slot", &RenderPass::material_slot);
+       add("back_faces",&RenderPass::back_faces);
+       add("texunit",  &Loader::texunit);
+       add("texunit",  &Loader::texunit_named);
        add("uniforms", &Loader::uniforms);
+       add("uniform_slot", &Loader::uniform_slot);
 }
 
-void RenderPass::Loader::finish()
+void RenderPass::Loader::material_inline()
 {
-       if(obj.shprog)
-       {
-               if(!obj.shdata)
-                       obj.shdata = new ProgramData;
-
-               for(unsigned i=0; i<obj.textures.size(); ++i)
-               {
-                       unsigned loc = obj.shprog->get_uniform_location(obj.textures[i].name);
-                       obj.shdata->uniform(loc, static_cast<int>(i));
-               }
-       }
+       RefPtr<Material> mat = new Material;
+       if(coll)
+               load_sub(*mat, get_collection());
+       else
+               load_sub(*mat);
+       obj.material = mat;
 }
 
-void RenderPass::Loader::material()
+void RenderPass::Loader::material(const string &name)
 {
-       if(obj.material)
-               throw InvalidState("A material is already loaded");
+       obj.material = &get_collection().get<Material>(name);
+       obj.material.keep();
+}
 
-       RefPtr<Material> mat = new Material;
-       load_sub(*mat);
-       obj.material = mat.release();
-       obj.own_material = true;
+void RenderPass::Loader::texunit(unsigned i)
+{
+       if(!obj.texturing)
+               obj.texturing = new Texturing;
+       TextureLoader ldr(*obj.texturing, i, coll);
+       load_sub_with(ldr);
 }
 
-void RenderPass::Loader::texture(const string &n)
+void RenderPass::Loader::texunit_named(unsigned i, const string &n)
 {
-       const Texture *tex = (n.empty() ? 0 : get_collection().get<Texture>(n));
-       TextureSlot slot(tex);
-       slot.name = (obj.textures.empty() ? "texture" : format("texture%d", obj.textures.size()));
-       load_sub(slot);
-       obj.textures.push_back(slot);
+       texunit(i);
+       obj.tex_names[n] = i;
 }
 
 void RenderPass::Loader::uniforms()
 {
        if(!obj.shprog)
-               throw InvalidState("Can't load uniforms without a shader program");
-       if(!obj.shdata)
-               obj.shdata = new ProgramData;
-       load_sub(*obj.shdata, *obj.shprog);
+               throw invalid_operation("RenderPass::Loader::uniforms");
+       RefPtr<ProgramData> shd = new ProgramData(obj.shprog);
+       load_sub(*shd);
+       obj.shdata = shd.release();
 }
 
+void RenderPass::Loader::uniform_slot(const string &name)
+{
+       uniform_slot2(name, name);
+}
+
+void RenderPass::Loader::uniform_slot2(const string &name, const string &slot)
+{
+       obj.uniform_slots[slot] = name;
+}
 
-RenderPass::TextureSlot::TextureSlot(const Texture *t):
-       texture(t)
-{ }
 
+RenderPass::TextureLoader::TextureLoader(Texturing &t, unsigned i, Collection *c):
+       DataFile::CollectionObjectLoader<Texturing>(t, c),
+       index(i)
+{
+       add("texture",   &TextureLoader::texture);
+       add("texture2d", &TextureLoader::texture2d);
+}
+
+void RenderPass::TextureLoader::finish()
+{
+       if(tex)
+       {
+               obj.attach(index, *tex);
+               tex.release();
+       }
+}
+
+void RenderPass::TextureLoader::texture(const string &name)
+{
+       tex = &get_collection().get<Texture>(name);
+       tex.keep();
+}
 
-RenderPass::TextureSlot::Loader::Loader(TextureSlot &s):
-       DataFile::ObjectLoader<TextureSlot>(s)
+void RenderPass::TextureLoader::texture2d()
 {
-       add("name", &TextureSlot::name);
+       tex = new Texture2D;
+       if(coll)
+               load_sub(static_cast<Texture2D &>(*tex), get_collection());
+       else
+               load_sub(static_cast<Texture2D &>(*tex));
 }
 
 } // namespace GL